spacetelescope / jwst

Python library for science observations from the James Webb Space Telescope
https://jwst-pipeline.readthedocs.io/en/latest/
Other
570 stars 167 forks source link

extract1d cannot find dispaxis for NIS WFSS spectra #2007

Closed stscieisenhamer closed 4 years ago

stscieisenhamer commented 6 years ago

Issue

When running calwebb_spec2 on NIS WFSS spectra, the extract_1d step has issues finding dispaxis. Example:

$ strun ../cfgs/calwebb_spec2.cfg spec2_single_asn.json --output_dir=/tmp
*****cut lots of logging******
2018-05-08 15:20:21,174 - stpipe.Spec2Pipeline.extract_2d - INFO - Step extract_2d running with args (<ImageModel(2048, 2048) from jw87800017001_02102_00008_nis_rate.fits>,).
2018-05-08 15:20:21,198 - stpipe.Spec2Pipeline.extract_2d - INFO - EXP_TYPE is NIS_WFSS
2018-05-08 15:20:21,459 - stpipe.Spec2Pipeline.extract_2d - INFO - Partial order on detector for obj: 1 order: 1
2018-05-08 15:20:21,459 - stpipe.Spec2Pipeline.extract_2d - INFO - Excluding off-image object: 1, order 1
2018-05-08 15:20:21,467 - stpipe.Spec2Pipeline.extract_2d - INFO - Partial order on detector for obj: 1 order: 2
2018-05-08 15:20:21,467 - stpipe.Spec2Pipeline.extract_2d - INFO - Excluding off-image object: 1, order 2
2018-05-08 15:20:21,474 - stpipe.Spec2Pipeline.extract_2d - INFO - Partial order on detector for obj: 1 order: 3
2018-05-08 15:20:21,474 - stpipe.Spec2Pipeline.extract_2d - INFO - Excluding off-image object: 1, order 3
2018-05-08 15:20:21,474 - stpipe.Spec2Pipeline.extract_2d - INFO - Grism object list created from source catalog: jw87800-o017_t002_niriss_f200w_cat.ecsv
2018-05-08 15:20:21,474 - stpipe.Spec2Pipeline.extract_2d - INFO - Creating output model
2018-05-08 15:20:21,508 - stpipe.Spec2Pipeline.extract_2d - WARNING - /Users/eisenham/Documents/ssbdev/jwst/jwst/datamodels/util.py:434: ValidationWarning: None is not of type 'string'

Failed validating 'type' in schema['properties']['meta']['properties']['cal_step']['properties']['extract_2d']:
    OrderedDict([('title', '2-D Spectral Extraction'),
                 ('type', 'string'),
                 ('fits_keyword', 'S_EXTR2D'),
                 ('blend_table', True)])

On instance['meta']['cal_step']['extract_2d']:
    None
  warnings.warn(str(errmsg), ValidationWarning)

2018-05-08 15:20:21,904 - stpipe.Spec2Pipeline.extract_2d - INFO - Subarray extracted for obj: 1 order: -1:
2018-05-08 15:20:21,904 - stpipe.Spec2Pipeline.extract_2d - INFO - Subarray extents are: (xmin:1731, ymin:495), (xmax:1679, ymax:601)
2018-05-08 15:20:24,928 - stpipe.Spec2Pipeline.extract_2d - INFO - Subarray extracted for obj: 1 order: 0:
2018-05-08 15:20:24,928 - stpipe.Spec2Pipeline.extract_2d - INFO - Subarray extents are: (xmin:1921, ymin:197), (xmax:1934, ymax:201)
2018-05-08 15:20:35,885 - stpipe.Spec2Pipeline.extract_2d - INFO - Step extract_2d done
2018-05-08 15:20:35,958 - stpipe.Spec2Pipeline.srctype - INFO - Step srctype running with args (<MultiSlitModel from jw87800017001_02102_00008_nis_rate.fits>,).
2018-05-08 15:20:35,976 - stpipe.Spec2Pipeline.srctype - INFO - Input EXP_TYPE is NIS_WFSS
2018-05-08 15:20:35,976 - stpipe.Spec2Pipeline.srctype - WARNING - EXP_TYPE NIS_WFSS not applicable to this operation
2018-05-08 15:20:35,976 - stpipe.Spec2Pipeline.srctype - WARNING - Step will be skipped
2018-05-08 15:20:35,977 - stpipe.Spec2Pipeline.srctype - INFO - Step srctype done
2018-05-08 15:20:36,027 - stpipe.Spec2Pipeline.photom - INFO - Step photom running with args (<MultiSlitModel from jw87800017001_02102_00008_nis_rate.fits>,).
2018-05-08 15:20:36,068 - stpipe.Spec2Pipeline.photom - INFO - Using photom reference file: /Users/eisenham/Documents/ssbdev/testdata/crds-cache/references/jwst/niriss/jwst_niriss_photom_0028.fits
2018-05-08 15:20:36,070 - stpipe.Spec2Pipeline.photom - INFO - Using area reference file: N/A
2018-05-08 15:20:36,071 - stpipe.Spec2Pipeline.photom - INFO - Using instrument: NIRISS
2018-05-08 15:20:36,071 - stpipe.Spec2Pipeline.photom - INFO -  detector: NIS
2018-05-08 15:20:36,071 - stpipe.Spec2Pipeline.photom - INFO -  exp_type: NIS_WFSS
2018-05-08 15:20:36,071 - stpipe.Spec2Pipeline.photom - INFO -  filter: GR150R
2018-05-08 15:20:36,071 - stpipe.Spec2Pipeline.photom - INFO -  pupil: F200W
2018-05-08 15:20:36,158 - stpipe.Spec2Pipeline.photom - INFO - Working on slit: 1 order: -1
2018-05-08 15:20:36,162 - stpipe.Spec2Pipeline.photom - INFO - PHOTMJSR value: 0.140939
2018-05-08 15:20:36,164 - stpipe.Spec2Pipeline.photom - INFO - Storing relative response table
2018-05-08 15:20:36,166 - stpipe.Spec2Pipeline.photom - INFO - Working on slit: 1 order: 0
2018-05-08 15:20:36,181 - stpipe.Spec2Pipeline.photom - WARNING - No match in reference file
2018-05-08 15:20:36,184 - stpipe.Spec2Pipeline.photom - INFO - Step photom done
2018-05-08 15:20:36,235 - stpipe.Spec2Pipeline.extract_1d - INFO - Step extract_1d running with args (<MultiSlitModel from jw87800017001_02102_00008_nis_rate.fits>,).
2018-05-08 15:20:36,252 - stpipe.Spec2Pipeline.extract_1d - INFO - No EXTRACT1D reference file will be used
2018-05-08 15:20:36,280 - stpipe.Spec2Pipeline.extract_1d - WARNING - /Users/eisenham/Documents/ssbdev/jwst/jwst/datamodels/util.py:434: ValidationWarning: None is not of type 'string'

Failed validating 'type' in schema['properties']['meta']['properties']['instrument']['properties']['grating']:
    OrderedDict([('title', 'Name of the grating element used'),
                 ('type', 'string'),
                 ('enum',
                  ['G140M',
                   'G235M',
                   'G395M',
                   'G140H',
                   'G235H',
                   'G395H',
                   'PRISM',
                   'MIRROR',
                   'N/A',
                   'ANY',
                   None]),
                 ('fits_keyword', 'GRATING'),
                 ('blend_table', True)])

On instance['meta']['instrument']['grating']:
    None
  warnings.warn(str(errmsg), ValidationWarning)

2018-05-08 15:20:36,281 - stpipe.Spec2Pipeline.extract_1d - INFO - Working on slit 1
2018-05-08 15:20:36,281 - stpipe.Spec2Pipeline.extract_1d - INFO - No data for slit 1, skipping ...
2018-05-08 15:20:36,281 - stpipe.Spec2Pipeline.extract_1d - INFO - Working on slit 1
2018-05-08 15:20:39,878 - stpipe.Spec2Pipeline.extract_1d - WARNING - Can't determine dispaxis from the WCS.
----------------------------------------------------------------------
ERROR RUNNING STEP 'Spec2Pipeline':
    'dispaxis'
Traceback (most recent call last):
  File "/Users/eisenham/Documents/ssbdev/jwst/jwst/stpipe/cmdline.py", line 297, in step_from_cmdline
    step.run(*positional)
  File "/Users/eisenham/Documents/ssbdev/jwst/jwst/stpipe/step.py", line 391, in run
    step_result = self.process(*args)
  File "/Users/eisenham/Documents/ssbdev/jwst/jwst/pipeline/calwebb_spec2.py", line 86, in process
    asn.filename
  File "/Users/eisenham/Documents/ssbdev/jwst/jwst/pipeline/calwebb_spec2.py", line 280, in process_exposure_product
    x1d_output = self.extract_1d(x1d_input)
  File "/Users/eisenham/Documents/ssbdev/jwst/jwst/stpipe/step.py", line 391, in run
    step_result = self.process(*args)
  File "/Users/eisenham/Documents/ssbdev/jwst/jwst/extract_1d/extract_1d_step.py", line 109, in process
    self.bkg_order)
  File "/Users/eisenham/Documents/ssbdev/jwst/jwst/extract_1d/extract.py", line 2026, in do_extract1d
    **extract_params)
  File "/Users/eisenham/Documents/ssbdev/jwst/jwst/extract_1d/extract.py", line 2505, in extract_one_slit
    log_initial_parameters(extract_params)
  File "/Users/eisenham/Documents/ssbdev/jwst/jwst/extract_1d/extract.py", line 367, in log_initial_parameters
    log.debug("dispaxis = %d", extract_params["dispaxis"])
KeyError: 'dispaxis'

Resources

An example dataset can be found in

/grp/jwst/ssb/eisenhamer/test_wfss/sdp_20180412/level2a

where the above example command can be run in.

stscieisenhamer commented 6 years ago

@philhodge There is now another sub-directory called spec2_partials with the assign_wcs and photom files saved.

philhodge commented 6 years ago

The input to the extract_1d step contained two "slits," one with shape (107, 0) and the other with shape (5, 14). extract_1d skipped the first one because it was empty, and then the step tried to process the second one. The function find_dispaxis determines the dispersion direction by comparing the wavelengths at three pixels, one near the middle of the image (see below for clarification), the next pixel to the right, and the next pixel up. If the wavelength changes more rapidly in one axis than the other, that axis is assumed to be the dispersion direction. If there is a wavelength attribute, the wavelengths are taken from that array. If not, the wavelengths are computed by using the WCS function.

The pixel "near the middle of the image" is taken to be the middle of wcs.bounding_box if that is not None. For this file, the bounding box was None, so the middle of the 2-D cutout was used as the starting pixel. Because the shape was (5, 14), that pixel was x = 7, y = 2 (zero indexed). The wavelengths computed by the WCS function only changed within the range x = 2 to 6, however; for x = 7 to 13 the values were all equal to 2.3.

s1 = model.slits[1]
wcs1 = s1.meta.wcs
wl = np.zeros((5, 14), dtype=np.float64)
for j in range(5):
    for i in range(14):
        _, _, wavelength, _ = wcs1(i, j, 2)
        wl[j, i] = wavelength

print(wl[:, 0:2])
[[ 0.75  0.75]
 [ 0.75  0.75]
 [ 0.75  0.75]
 [ 0.75  0.75]
 [ 0.75  0.75]]
print(wl[:, 2:7])
[[ 0.81711023  1.15033439  1.48355855  1.81678271  2.15000687]
 [ 0.81711023  1.15033439  1.48355855  1.81678271  2.15000687]
 [ 0.81711023  1.15033439  1.48355855  1.81678271  2.15000687]
 [ 0.81711023  1.15033439  1.48355855  1.81678271  2.15000687]
 [ 0.81711023  1.15033439  1.48355855  1.81678271  2.15000687]]
print(wl[:, 7:])
[[ 2.3  2.3  2.3  2.3  2.3  2.3  2.3]
 [ 2.3  2.3  2.3  2.3  2.3  2.3  2.3]
 [ 2.3  2.3  2.3  2.3  2.3  2.3  2.3]
 [ 2.3  2.3  2.3  2.3  2.3  2.3  2.3]
 [ 2.3  2.3  2.3  2.3  2.3  2.3  2.3]]

If pixels outside the range x = 2 to 6 were regarded as invalid and had been flagged as such by having wavelengths set to NaN, the find_dispaxis function would have evaluated wavelengths over the entire cutout region, and np.nanmean would have been used to determine which axis had the larger change in (non-NaN) wavelength:

dwlx = wl[:, 1:-1] - wl[:, 0:-2]
dwly = wl[1:-1, :] - wl[0:-2, :]
dwlx = np.nanmean(dwlx)
dwly = np.nanmean(dwly)
dwlx
0.12916666666666665
dwly
0.0
hbushouse commented 6 years ago

Does the second (non-empty) slit in this case correspond to the 0th-order and hence that might explain why there's no change in wavelength values?

hbushouse commented 6 years ago

Once #1801 gets merged, which will restrict 'extract_2d' to only extracting orders +1 and +2, this will result in no workable slits at all, because those two fall off the detector. I suspect that the direct image data in this test case, from which the source catalog is derived, may be garbage and hence we don't happen to get any useful sources/slits defined for extraction.

philhodge commented 6 years ago

That's true, the second slit does have 'SPORDER = 0'. I hadn't noticed that before. However, the wavelengths computed by the wcs function are the same, regardless of what spectral order is specified (it's the third argument) when wcs is called.

philhodge commented 6 years ago

The find_dispaxis function in extract_1d probably needs to check the DQ extension, in addition to filtering out NaNs in the wavelengths. In this case, however, DQ was 2 for almost half of the array, the "good" half, and the data sure don't look saturated to me.

stscieisenhamer commented 6 years ago

FYI: Looking at the i2d image, there is no real signal there that I can see, so no surprise that the catalog is odd.

stscieisenhamer commented 6 years ago

Can we create a catalog to put sources in specific places, just to ensure that everything else works? I know there is a regression test, but can this be done for this dataset?

philhodge commented 6 years ago

Sure, just edit the source catalog file.

hbushouse commented 6 years ago

Yes, editing the source catalog will do it. All that probably needs to happen is to just change the location (in either RA/Dec or pixel space) of the source(s) that are defined in it. The tricky bit is figuring out what new values to use to make it fall within the proper location of the grism image, but that can be done through a little trial and error (been there, done that).

stscieisenhamer commented 6 years ago

well, going with the center, both in pixel and sky, the same error occurs.

hbushouse commented 4 years ago

Fixed by https://github.com/spacetelescope/jwst/pull/3891