Repository: spacetelescope/jwql

Issue:
o001 radio button
Some image view images have a radio button for o001 as an option. I think these are actually from the crfints files, which have the filenaming convention of e.g. jw01442001001_04103_00001-seg004_nrca3_o001_crfints.fits. The filename parser must be grabbing o001 as the part of the filename after the detector name, and assuming that is the calibration suffix.

We should:
1) see if clicking on the radio button shows the correct image or not -- it doesn't. It assumes a filename ending in _o001.fits
2) change the listed suffix to be crfints


Discussion:
One place (hopefully the only place?) that this needs to change is in get_image_info in data_containers.py. It is assuming the 4th part of the filename is the calibration suffix.

Resolving patch:
diff --git a/jwql/utils/constants.py b/jwql/utils/constants.py
--- a/jwql/utils/constants.py
+++ b/jwql/utils/constants.py
@@ -370,6 +370,14 @@
 
 SUBARRAYS_ONE_OR_FOUR_AMPS = ['SUBGRISMSTRIPE64', 'SUBGRISMSTRIPE128', 'SUBGRISMSTRIPE256']
 
+# Filename suffixes that need to include the association value in the suffix in
+# order to identify the preview image file. This should only be crf and crfints,
+# since those are essentially level 2 files that are output by the level 3 pipeline.
+SUFFIXES_TO_ADD_ASSOCIATION = ['crf', 'crfints']
+
+# Filename suffixes where data have been averaged over integrations
+SUFFIXES_WITH_AVERAGED_INTS = ['rate', 'cal', 'crf', 'i2d', 'bsub']
+
 # Base name for the file listing the thumbnail images for a given instrument.
 # The complete name will have "_{instrument.lower}.txt" added to the end of this.
 THUMBNAIL_LISTFILE = 'thumbnail_inventory'
diff --git a/jwql/website/apps/jwql/data_containers.py b/jwql/website/apps/jwql/data_containers.py
--- a/jwql/website/apps/jwql/data_containers.py
+++ b/jwql/website/apps/jwql/data_containers.py
@@ -49,7 +49,9 @@
 from jwql.instrument_monitors.nirspec_monitors.data_trending import dashboard as nirspec_dash
 from jwql.utils.utils import check_config_for_key, ensure_dir_exists, filesystem_path, filename_parser, get_config
 from jwql.utils.constants import MONITORS, PREVIEW_IMAGE_LISTFILE, THUMBNAIL_LISTFILE
-from jwql.utils.constants import IGNORED_SUFFIXES, INSTRUMENT_SERVICE_MATCH, JWST_INSTRUMENT_NAMES_MIXEDCASE
+from jwql.utils.constants import IGNORED_SUFFIXES, INSTRUMENT_SERVICE_MATCH, JWST_INSTRUMENT_NAMES_MIXEDCASE, \
+                                 JWST_INSTRUMENT_NAMES_SHORTHAND, SUFFIXES_TO_ADD_ASSOCIATION, SUFFIXES_WITH_AVERAGED_INTS
+from jwql.utils.preview_image import PreviewImage
 from jwql.utils.credentials import get_mast_token
 from .forms import InstrumentAnomalySubmitForm
 from astroquery.mast import Mast
@@ -786,6 +788,7 @@ def get_image_info(file_root, rewrite):
     image_info['suffixes'] = []
     image_info['num_ints'] = {}
     image_info['available_ints'] = {}
+    image_info['total_ints'] = {}
 
     # Find all of the matching files
     proposal_dir = file_root[:7]
@@ -804,7 +807,14 @@ def get_image_info(file_root, rewrite):
     for filename in image_info['all_files']:
 
         # Get suffix information
-        suffix = os.path.basename(filename).split('_')[4].split('.')[0]
+        suffix = filename_parser(filename)['suffix']
+
+        # For crf or crfints suffixes, we need to also include the association value
+        # in the suffix, so that preview images can be found later.
+        if suffix in SUFFIXES_TO_ADD_ASSOCIATION:
+            assn = filename.split('_')[-2]
+            suffix = f'{assn}_{suffix}'
+
         image_info['suffixes'].append(suffix)
 
         # Determine JPEG file location
@@ -816,12 +826,20 @@ def get_image_info(file_root, rewrite):
         if os.path.exists(jpg_filepath) and not rewrite:
             pass
 
-        # Record how many integrations there are per filetype
-        jpgs = glob.glob(os.path.join(prev_img_filesys, observation_dir, '{}_{}_integ*.jpg'.format(file_root, suffix)))
+        # Record how many integrations have been saved as preview images per filetype
+        jpgs = glob.glob(os.path.join(prev_img_filesys, proposal_dir, '{}_{}_integ*.jpg'.format(file_root, suffix)))
         image_info['num_ints'][suffix] = len(jpgs)
         image_info['available_ints'][suffix] = sorted([int(jpg.split('_')[-1].replace('.jpg', '').replace('integ', '')) for jpg in jpgs])
         image_info['all_jpegs'].append(jpg_filepath)
 
+        # Record how many integrations exist per filetype. crf needs to be treated
+        # separately because the suffix includes the association number, which can't
+        # be predicted for a given program.
+        if ((suffix not in SUFFIXES_WITH_AVERAGED_INTS) and (suffix[-3:] != 'crf')):
+            image_info['total_ints'][suffix] = fits.getheader(filename)['NINTS']
+        else:
+            image_info['total_ints'][suffix] = 1
+
     return image_info
 
 
diff --git a/jwql/website/apps/jwql/views.py b/jwql/website/apps/jwql/views.py
--- a/jwql/website/apps/jwql/views.py
+++ b/jwql/website/apps/jwql/views.py
@@ -971,6 +971,7 @@ def view_image(request, inst, file_root, rewrite=False):
                'suffixes': image_info['suffixes'],
                'num_ints': image_info['num_ints'],
                'available_ints': image_info['available_ints'],
+               'total_ints': image_info['total_ints'],
                'form': form}
 
     return render(request, template, context)


Test patch: