Skip to content

Commit

Permalink
Final (I hope) tweaks to the new code
Browse files Browse the repository at this point in the history
  • Loading branch information
hbushouse committed May 14, 2024
1 parent f3dcac1 commit 7f3b1ab
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 61 deletions.
20 changes: 11 additions & 9 deletions jwst/assign_wcs/nirspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,20 +637,23 @@ def get_open_msa_slits(msa_file, msa_metadata_id, dither_position,
quadrant = slitlets_sid[0]['shutter_quadrant']
ycen = j
xcen = slitlets_sid[0]['shutter_row'] # grab the first as they are all the same
source_xpos = 0.5
source_ypos = 0.5

# Background slits all have source_id=0 in the msa_file,
# so assign a unique id based on the slitlet_id
source_id = slitlet_id
log.info(f'Slitlet_id {slitlet_id} is background only; assigned source_id = {source_id}')

# Hardwire the source info for background slits, because there's
# no source info for them in the msa_file
source_xpos = 0.5
source_ypos = 0.5
source_name = "background_{}".format(slitlet_id)
source_alias = "bkg_{}".format(slitlet_id)
stellarity = 0.0
source_ra = 0.0
source_dec = 0.0
log.info(f'Slitlet {slitlet_id} is background only; assigned source_id={source_id}')

# There is 1 main shutter: this is a normal slit
# There is 1 main shutter: this is a slit containing either a real or virtual source
elif n_main_shutter == 1:
xcen, ycen, quadrant, source_xpos, source_ypos = [
(s['shutter_row'], s['shutter_column'], s['shutter_quadrant'],
Expand All @@ -669,7 +672,7 @@ def get_open_msa_slits(msa_file, msa_metadata_id, dither_position,
if slitlets_sid[i]['primary_source'] == 'Y':
source_id = slitlets_sid[i]['source_id']

# Normal slits with a source assigned have a source_id > 0
# Slits with a real source assigned have a source_id > 0
if source_id > 0:
shutter_id = xcen + (ycen - 1) * 365 # shutter numbers in MSA file are 1-indexed
# Get source info for this normal slitlet
Expand All @@ -682,16 +685,15 @@ def get_open_msa_slits(msa_file, msa_metadata_id, dither_position,

# Slits with source_id < 0 are "virtual" slits, with no source assigned
else:
source_id = abs(source_id)
log.info(f'Slitlet_id {slitlet_id} is virtual; assigned source_id = {source_id}')
# Hardwire the source info for this virtual slit, because there's none in the MSA file
source_xpos = 0.5
source_ypos = 0.5
source_name = "virtual_{}".format(source_id)
source_alias = "vrt_{}".format(source_id)
source_name = "virtual_{}".format(abs(source_id))
source_alias = "vrt_{}".format(abs(source_id))
stellarity = 0.0
source_ra = 0.0
source_dec = 0.0
log.info(f'Slitlet {slitlet_id} is virtual, with source_id={source_id}')

# More than 1 main shutter: Not allowed!
else:
Expand Down
1 change: 0 additions & 1 deletion jwst/outlier_detection/outlier_detection_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ def process(self, input_data):
model.meta.cal_step.outlier_detection = "SKIPPED"
else:
self.input_models.meta.cal_step.outlier_detection = "SKIPPED"
self.skip = True
return self.input_models

self.log.debug(f"Using {detection_step.__name__} class for outlier_detection")
Expand Down
95 changes: 44 additions & 51 deletions jwst/pipeline/calwebb_spec3.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,69 +169,32 @@ def process(self, input):
for name, model in multislit_to_container(source_models).items()
]

# Check for negative and large source_id values
#if len(sources) > 99999:
# self.log.critical("Data contain more than 100,000 sources;"
# "filename does not support 6 digit source ids.")
# raise Exception

#available_src_ids = set(np.arange(99999) + 1)
#used_src_ids = set()
#for src in sources:
# src_id, model = src
# src_id = int(src_id)
# used_src_ids.add(src_id)
# if 0 < src_id <= 99999:
# available_src_ids.remove(src_id)

#hotfixed_sources = []
# now find and reset bad source_id values
#for src in sources:
# src_id, model = src
# src_id = int(src_id)
# Replace ids that aren't positive 5-digit integers
# if src_id < 0 or src_id > 99999:
# src_id_new = available_src_ids.pop()
# self.log.info(f"Source ID {src_id} falls outside allowed range.")
# self.log.info(f"Reassigning {src_id} to {str(src_id_new).zfill(5)}.")
# # Replace source_id for each model in the SourceModelContainers
# for contained_model in model:
# contained_model.source_id = src_id_new
# src_id = src_id_new
# hotfixed_sources.append((str(src_id), model))

#sources = hotfixed_sources

# Process each source
for source in sources:

# If each source is a SourceModelContainer,
# the output name needs to be updated with the source ID, and potentially
# also the slit name (for NIRSpec fixed-slit only).
# the output name needs to be updated based on the source ID,
# and potentially also the slit name (for NIRSpec fixed-slit only).
if isinstance(source, tuple):
source_id, result = source

# NIRSpec fixed-slit data
if result[0].meta.exposure.type == "NRS_FIXEDSLIT":
# Output file name is constructed using the source_id and the slit name
slit_name = self._create_nrsfs_slit_name(result)
srcid = f's{source_id.lower()}'
self.output_file = format_product(
output_file, source_id=srcid, slit_name=slit_name)
self.output_file = format_product(output_file, source_id=srcid, slit_name=slit_name)

# NIRSpec MOS/MSA data
elif result[0].meta.exposure.type == "NRS_MSASPEC":
self.log.debug(f" source_id = {source_id}")
self.log.debug(f" result[0].name = {result[0].name.lower()}")
source_name = result[0].source_name
source_id = str(result[0].source_id)
self.log.debug(f" result[0].source_id = {source_id}")
if "back" in source_name:
self.log.debug(" background slit")
srcid = f'b{source_id:>09s}'
elif "virt" in source_name:
self.log.debug(" virtual slit")
srcid = f'v{source_id:>09s}'
else:
srcid = f's{source_id:>09s}'
# Construct the specially formatted source_id to use in the output file
# name that separates source, background, and virtual slits
srcid = self._create_nrsmos_source_id(result)
self.output_file = format_product(output_file, source_id=srcid)
self.log.debug(f" output_file = {self.output_file}")
self.log.debug(f"output_file = {self.output_file}")

else:
# All other types just use the source_id directly in the file name
srcid = f's{source_id.lower()}'
self.output_file = format_product(output_file, source_id=srcid)
else:
Expand Down Expand Up @@ -334,3 +297,33 @@ def _create_nrsfs_slit_name(self, source_models):
slit_name = "-".join(slit_names) # append slit names using a dash separator

return slit_name

def _create_nrsmos_source_id(self, source_models):
"""Create the complete source_id product field for NIRSpec MOS products.
The original source_id value has a "s", "b", or "v" character prepended
to uniquely identify source, background, and virtual slits.
"""

# Get the original source name and ID from the input models
source_name = source_models[0].source_name
source_id = str(source_models[0].source_id)

# MOS background sources have "background" in the source name
if "back" in source_name:
# prepend "b" to the source_id number and format to 9 chars
srcid = f'b{source_id:>09s}'
self.log.debug(f"Source {source_name} is a MOS background slitlet: ID={srcid}")

# MOS virtual sources have "virtual" in the source name
elif "virt" in source_name:
# prepend "v" to the source_id number and remove the leading negative sign
srcid = f'v{source_id[1:]:>09s}'
self.log.debug(f"Source {source_name} is a MOS virtual slitlet: ID={srcid}")

# Regular MOS sources
else:
# prepend "s" to the source_id number and format to 9 chars
srcid = f's{source_id:>09s}'

return srcid

0 comments on commit 7f3b1ab

Please sign in to comment.