8000 JP-1663: NIRSpec MOS flat field DQ handling by jemorrison · Pull Request #5284 · spacetelescope/jwst · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

JP-1663: NIRSpec MOS flat field DQ handling #5284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 11, 2020

Conversation

jemorrison
Copy link
Collaborator
@jemorrison jemorrison commented Aug 27, 2020

This PR set the various flat components (d_flat, f_flat, s_flat) to 1 if the input data is a NAN or the DQ flags are set to DO_NOT_USE + NO_FLAT_FIELD.

Only the DQ flag of DO_NOT_USE is used to determine if the the flat data should be used to create the interpolated NIRSpec flat.

Resolves #5293 / JP-1663

@codecov
Copy link
codecov bot commented Aug 27, 2020

Codecov Report

Merging #5284 into master will decrease coverage by 0.08%.
The diff coverage is 21.27%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5284      +/-   ##
==========================================
- Coverage   52.76%   52.67%   -0.09%     
==========================================
  Files         406      407       +1     
  Lines       36788    36898     +110     
  Branches     5709     5724      +15     
==========================================
+ Hits        19411    19436      +25     
- Misses      16139    16224      +85     
  Partials     1238     1238              
Flag Coverage Δ
#unit 52.67% <21.27%> (-0.09%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
jwst/flatfield/flat_field.py 19.78% <21.27%> (-0.42%) ⬇️
jwst/master_background/nirspec_utils.py 31.14% <0.00%> (-3.40%) ⬇️
jwst/stpipe/step.py 79.96% <0.00%> (-1.79%) ⬇️
jwst/master_background/master_background_step.py 35.07% <0.00%> (-1.44%) ⬇️
jwst/lib/suffix.py 95.18% <0.00%> (ø)
jwst/master_background/__init__.py 100.00% <0.00%> (ø)
...ter_background/master_background_nrs_slits_step.py 20.48% <0.00%> (ø)
jwst/pipeline/calwebb_spec2.py 33.83% <0.00%> (+4.72%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4055a1b...2c9178c. Read the comment docs.

@jemorrison jemorrison changed the title JP-1071 NIRSPec MOS part JP-1663 NIRSPec MOS flat fielding Aug 31, 2020
# their DQ to NO_FLAT_FIELD
# Find pixels in the flat that have a value of NaN and add to
# DQ to DO_NOT_USE + NO_FLAT_FIELD
bad_flag = dqflags.pixel['DO_NOT_USE'] + dqflags.pixel['NO_FLAT_FIELD']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are only supposed reject pixels where the DQ flag is set to DO_NOT_USE, right? Why are we doing the work in code here that should be done by the creators of the reference file?

I.e. if a pixel has NaN in the reference file, it should also have DO_NOT_USE set for that pixel. All other bits set are informational only.

Same applies to the many instances of this below in the code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we can take this out once the flat field reference files are made correctly. There is no flagging of DO_NOT_USE. But it does not harm - if the flat field is a 0 or a NaN it should not be use by the code - this is just a second level of checking.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes you just gotta do what you gotta do, in order to cover all bases, just in case someone else didn't do what they gotta do.

Copy link
Collaborator
@jdavies-st jdavies-st Sep 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only worry is that because the code does this "fixing" of the reffile, it won't be obvious if incorrect reference files keep getting delivered. We won't know. And given the complexity and the shear number of instances of this in the flatfield code, since we are addressing all of these here and now, we will have to go back and change every instance of this a 2nd time if we want to verify that we have correct reffiles.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See discussion here

#4157 (comment)

#4157 (comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I put the 2nd level of checks for that exact reason. Actually the checks were already there but used a different DQ flag. As someone who will be making reference files during commissioning I think it is going to be a stressful and crazy time. I could see some essential flagging not being set correctly. But it would only add to the stress if the pipeline failed - The pipeline should be smart enough to know that a flat value of 0 of NAN should not be used. The NIRSpec team is going to redeliver reference files but that might take a while. Having a common sense checks in the pipeline seems ok to me. - That is my two cents

@jdavies-st jdavies-st changed the title JP-1663 NIRSPec MOS flat fielding JP-1663: NIRSPec MOS flat fielding Sep 2, 2020
@jdavies-st jdavies-st changed the title JP-1663: NIRSPec MOS flat fielding JP-1663: NIRSpec MOS flat field DQ handling Sep 2, 2020
@hbushouse hbushouse added this to the Build 7.6 milestone Sep 2, 2020
flat_zero = np.where(flat_data == 0.)
flat_dq[flat_zero] = np.bitwise_or(flat_dq[flat_zero],
dqflags.pixel['NO_FLAT_FIELD'])
flat_dq[flat_zero] = np.bitwise_or(flat_dq[flat_zero],bad_flag)

# Find all pixels in the flat that have a DQ value of NO_FLAT_FIELD
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to update the comment here to stay in sync with code change.

flat_zero = np.where(f_flat == 0.)
f_flat_dq[flat_zero] = np.bitwise_or(f_flat_dq[flat_zero],bad_flag)

# Find all pixels in the flat that have a DQ value of NO_FLAT_FIELD
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update comment

flat_zero = np.where(flat_2d == 0.)
s_flat_dq[flat_zero] = np.bitwise_or(s_flat_dq[flat_zero],bad_flag)

# Find all pixels in the flat that have a DQ value of NO_FLAT_FIELD
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update comment

flat_zero = np.where(flat_2d == 0.)
d_flat_dq[flat_zero] = np.bitwise_or(d_flat_dq[flat_zero],bad_flag)

# Find all pixels in the flat that have a DQ value of NO_FLAT_FIELD
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update comment

bad2 = np.bitwise_and(dq_list[1], dqflags.pixel['DO_NOT_USE'])
iflag = np.where( (bad1==1) & (bad2 ==1))

# locate places that flat_dq is set to DO_NOT_USE and reset to UNRELIABLE FLAT
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we unsetting DO_NOT_USE values? And if we do, why not reset them to NO_FLAT_FIELD, as opposed to UNRELIABLE_FLAT? If they were originally marked as DO_NOT_USE, they must be really bad, got reset to 1, and hence no flat-field value was applied to the science data.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussions with James Muzerolle on how to combine the s-flat, d-flat and f-flat (see JP-1663 Aug 31 - Sept 1)
the 3 flats are independent of each other. Just because 1 flat has an DO_NOT_USE value it does not mean that the combined flat should be flagged as DO_NOT_USE. It should only be DO_NOT_USE if all 3 flats are do not use. So if one of the flats was flagged as do_not_use but the others were not we changed the final DQ value to be UNRELIABLE_FLAT

bad3 = np.bitwise_and(dq_list[2], dqflags.pixel['DO_NOT_USE'])
iflag = np.where( (bad1==1) & (bad2 ==1) & (bad3 ==1))

# locate places that flat_dq is set to DO_NOT_USE and reset to UNRELIABLE FLAT
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as above.

iloc = np.where(np.bitwise_and(flat_dq, dqflags.pixel['DO_NOT_USE']))
flat_dq[iloc] = dqflags.pixel['UNRELIABLE_FLAT']
# now only set DO_NOT_USE to pixels that are set in both flats as DO_NOT_USE
flat_dq[iflag] = np.bitwise_or(flat_dq[iflag],dqflags.pixel['DO_NOT_USE'])
elif n_dq == 3:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there some more elegant way to do all of this checking of dq values from any of 1, 2, or 3 flats, like just a single loop that has a range of n_dq and combines values from all the individual dq arrays that exist? As opposed to having hardwired code blocks to handle each instance of 0, 1, 2, or 3 dq arrays.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can look into this. To me there is a balance between elegance and knowing quickly what the code is doing to tracking down errors. I went cautious and tried to make it very clear what the code was doing.

@jemorrison
Copy link
Collaborator Author

@hbushouse
I made code changes after the review. Could you look at the updates and see it they answer all your questions

Copy link
Collaborator
@hbushouse hbushouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just needs a change log entry.

@jemorrison jemorrison merged commit cbc763e into spacetelescope:master Sep 11, 2020
@jemorrison jemorrison deleted the JP-1071_Part2_MOS branch January 7, 2021 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NIRSPEC: flat field calspec2 failing validation for MOS data
3 participants
0