Skip to content
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

Nonlinearity correction + datacube spec1d extractions + datacube fluxing improvements #1770

Merged
merged 128 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 66 commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
2bae506
improved grating correction
rcooke-ast Jan 20, 2024
b4a6a14
updated grating correction docs
rcooke-ast Jan 20, 2024
d61c527
grating correction fix
rcooke-ast Jan 20, 2024
8494164
begin extract spec1d
rcooke-ast Jan 21, 2024
5072c72
switch to sensfunc
rcooke-ast Jan 21, 2024
4008504
switch to sensfunc in docs
rcooke-ast Jan 21, 2024
10511f9
add extract_datacube
rcooke-ast Jan 21, 2024
04ccac3
correct nonlinear
rcooke-ast Jan 22, 2024
c59f4d0
correct nonlinear
rcooke-ast Jan 22, 2024
397e35c
add extract to config
rcooke-ast Jan 22, 2024
30c8d6b
extractions working
rcooke-ast Jan 22, 2024
de88b2b
extract to specobjs
rcooke-ast Jan 23, 2024
284c33a
code cleanup
rcooke-ast Jan 23, 2024
a48b180
optimal extract
rcooke-ast Jan 23, 2024
2c04bc4
revert
rcooke-ast Jan 23, 2024
308f134
reorganise imports
rcooke-ast Jan 23, 2024
52c1102
extraction working
rcooke-ast Jan 23, 2024
6403e02
extraction working
rcooke-ast Jan 23, 2024
52b4c2d
add sensfunc option for coadd3d file
rcooke-ast Jan 24, 2024
4b210bf
add sensfunc option
rcooke-ast Jan 24, 2024
d2983e1
deprecate
rcooke-ast Jan 24, 2024
bd694d3
sensfunc applied
rcooke-ast Jan 24, 2024
832552e
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Jan 24, 2024
1eff610
typo
rcooke-ast Jan 25, 2024
bcfce86
fix sensfile
rcooke-ast Jan 25, 2024
eb6da38
fix units
rcooke-ast Jan 25, 2024
a40832a
fix sensfile input
rcooke-ast Jan 25, 2024
c409efe
allow delta_wave as input
rcooke-ast Jan 25, 2024
e3186c0
support extraction of fluxed cubes
rcooke-ast Jan 25, 2024
de5fd64
typo
rcooke-ast Jan 25, 2024
71ca014
minor fix
rcooke-ast Jan 25, 2024
2e77668
Merge branch 'kcrm_dec_2023' into kcwi_cube_updates
rcooke-ast Feb 3, 2024
5f9751d
grating_corr fix
rcooke-ast Feb 3, 2024
9a6b0c3
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Feb 8, 2024
f091e5b
Merge branch 'release' into kcwi_cube_updates
rcooke-ast Feb 8, 2024
40cebd1
docs
rcooke-ast Feb 12, 2024
075fdec
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Feb 12, 2024
29aa617
add extractfile class
rcooke-ast Feb 12, 2024
390afae
typo
rcooke-ast Feb 12, 2024
acfa855
apply nonlinear
rcooke-ast Feb 12, 2024
fcc9805
add extraction pars
rcooke-ast Feb 12, 2024
2243b1b
rm embed
rcooke-ast Feb 12, 2024
c830ef4
extfile
rcooke-ast Feb 12, 2024
b71d38b
support wl range
rcooke-ast Feb 12, 2024
0be7fbe
fix nonlinear keys
rcooke-ast Feb 12, 2024
a447465
1s exptime
rcooke-ast Feb 12, 2024
a3ad55a
1s exptime
rcooke-ast Feb 12, 2024
daa1739
1s exptime
rcooke-ast Feb 12, 2024
45781cc
rm duplicate WL creation
rcooke-ast Feb 13, 2024
6a6d71f
mask optimal kernel
rcooke-ast Feb 13, 2024
3bc3fe4
add todo about dar fix
rcooke-ast Feb 13, 2024
d6db0ee
add output filename
rcooke-ast Feb 14, 2024
c0b1a34
logging msg
rcooke-ast Feb 14, 2024
cc0d737
TYPE=DC
rcooke-ast Feb 14, 2024
988692f
even object profile
rcooke-ast Feb 14, 2024
6edf897
update datacube docs
rcooke-ast Feb 14, 2024
a2de445
fix tests
rcooke-ast Feb 15, 2024
f90c6fa
add docs
rcooke-ast Feb 15, 2024
b23aba2
begin tweak_method
rcooke-ast Feb 15, 2024
b564837
tweaked by gradient
rcooke-ast Feb 15, 2024
a7ced6c
cleanup
rcooke-ast Feb 16, 2024
98df661
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Feb 21, 2024
53734ec
code cleanup after PR comments
rcooke-ast Feb 22, 2024
2f38d6a
doc combination update
rcooke-ast Feb 22, 2024
b16aef2
extinction correction
rcooke-ast Feb 22, 2024
0c8e925
added non-linear documentation
rcooke-ast Feb 22, 2024
35814fc
added non-linear documentation
rcooke-ast Feb 22, 2024
5004f95
added non-linear documentation
rcooke-ast Feb 23, 2024
2b201d6
slice_subpixel clarification docs
rcooke-ast Feb 23, 2024
9e7b30b
Merge branch 'kcwi_cube_updates' into cube_tweakedge
rcooke-ast Feb 25, 2024
252c87a
Merge branch 'hotfix_cube_sig' into cube_tweakedge
rcooke-ast Feb 25, 2024
b652be6
tweak slits method=gradient
rcooke-ast Feb 26, 2024
fda945c
SlicerIFU -> tweak slits
rcooke-ast Feb 26, 2024
d796825
no padding
rcooke-ast Feb 27, 2024
48fb279
no padding
rcooke-ast Feb 27, 2024
d40325c
rename init
rcooke-ast Feb 27, 2024
916b629
todo add
rcooke-ast Feb 28, 2024
a214ed4
rm tweak bias
rcooke-ast Feb 28, 2024
9ce48e4
rm todo
rcooke-ast Feb 29, 2024
f483ecb
rm padding
rcooke-ast Mar 2, 2024
d59990e
docs
rcooke-ast Mar 3, 2024
97bbf6b
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Mar 3, 2024
381cfd3
Merge branch 'kcwi_cube_updates' into cube_tweakedge
rcooke-ast Mar 3, 2024
43f1d68
fix wcs
rcooke-ast Mar 7, 2024
8db670a
add debug
rcooke-ast Mar 9, 2024
4be7d2f
Merge branch 'kcwi_cube_updates' into cube_tweakedge
rcooke-ast Mar 9, 2024
caaa00e
Merge remote-tracking branch 'origin/flexure_ifu' into cube_tweakedge
rcooke-ast Mar 21, 2024
bc19ae9
Merge branch 'flexure_ifu' into cube_tweakedge
rcooke-ast Mar 26, 2024
4704775
add bpm for white light
rcooke-ast Mar 26, 2024
2f3ad9e
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Mar 27, 2024
cacae29
flatfield improvements
rcooke-ast Mar 27, 2024
a482d55
Merge branch 'release' into kcwi_cube_updates
rcooke-ast Mar 27, 2024
7372b71
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Mar 27, 2024
b1759d8
Merge branch 'kcwi_cube_updates' into cube_tweakedge
rcooke-ast Mar 27, 2024
98e3dca
Apply suggestions from code review
rcooke-ast Mar 27, 2024
a897d2b
update flatfield
rcooke-ast Mar 28, 2024
fdcea2d
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Apr 2, 2024
6f2f60c
Merge branch 'develop' into cube_tweakedge
rcooke-ast Apr 5, 2024
95a4421
Merge branch 'kcwi_cube_updates' into cube_tweakedge
rcooke-ast Apr 5, 2024
5ef9602
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Apr 5, 2024
7fbb0a4
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Apr 6, 2024
db2fe46
rm copy
rcooke-ast Apr 12, 2024
18c8eb7
rm initialise
rcooke-ast Apr 12, 2024
b98d758
fix patterns
rcooke-ast Apr 12, 2024
f4bee40
doc hotfix
rcooke-ast Apr 12, 2024
fe28589
scipy convolution module
rcooke-ast Apr 12, 2024
c1c1360
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Apr 12, 2024
dc2b581
Merge branch 'kcwi_cube_updates' into cube_tweakedge
rcooke-ast Apr 12, 2024
f87e8ec
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Apr 20, 2024
c94ded6
Merge branch 'develop' into cube_tweakedge
rcooke-ast Apr 20, 2024
9193696
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Apr 23, 2024
297c6d9
Merge branch 'kcwi_cube_updates' into cube_tweakedge
rcooke-ast Apr 23, 2024
5cec044
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Apr 24, 2024
b90cb71
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Apr 30, 2024
a5a7526
Merge branch 'kcwi_cube_updates' into cube_tweakedge
rcooke-ast Apr 30, 2024
e94e037
add maxfrac
rcooke-ast Apr 30, 2024
14d810f
Merge branch 'develop' into kcwi_cube_updates
kbwestfall Jun 12, 2024
510d2b1
Merge branch 'kcwi_cube_updates' into cube_tweakedge
kbwestfall Jun 12, 2024
3117c08
Merge pull request #1796 from pypeit/cube_tweakedge
rcooke-ast Jul 17, 2024
9ab0689
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Jul 17, 2024
aa4f782
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Jul 22, 2024
9c5bd24
no error returned
rcooke-ast Jul 23, 2024
35f0a7d
no error returned
rcooke-ast Jul 23, 2024
7e9c193
Merge branch 'kcwi_cube_updates' into kcwi_updates2
rcooke-ast Jul 23, 2024
b5811ef
phase shift with no errors
rcooke-ast Jul 23, 2024
76062b4
fix scikit-image argument update
rcooke-ast Jul 24, 2024
87527fa
fixed missing argument
rcooke-ast Jul 27, 2024
abe1090
Merge branch 'develop' into kcwi_cube_updates
rcooke-ast Jul 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions deprecated/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,71 @@ def make_whitelight_frompixels(all_ra, all_dec, all_wave, all_sci, all_wghts, al
whitelight_ivar[:, :, ff] = ivar_img.copy()
return whitelight_Imgs, whitelight_ivar, whitelightWCS


def make_sensfunc(ss_file, senspar, blaze_wave=None, blaze_spline=None, grating_corr=False):
"""
Generate the sensitivity function from a standard star DataCube.

Args:
ss_file (:obj:`str`):
The relative path and filename of the standard star datacube. It
should be fits format, and for full functionality, should ideally of
the form :class:`~pypeit.coadd3d.DataCube`.
senspar (:class:`~pypeit.par.pypeitpar.SensFuncPar`):
The parameters required for the sensitivity function computation.
blaze_wave (`numpy.ndarray`_, optional):
Wavelength array used to construct blaze_spline
blaze_spline (`scipy.interpolate.interp1d`_, optional):
Spline representation of the reference blaze function (based on the illumflat).
grating_corr (:obj:`bool`, optional):
If a grating correction should be performed, set this variable to True.

Returns:
`numpy.ndarray`_: A mask of the good sky pixels (True = good)
"""
# TODO :: This routine has not been updated to the new spec1d plan of passing in a sensfunc object
# :: Probably, this routine should be removed and the functionality moved to the sensfunc object
msgs.error("coding error - make_sensfunc is not currently supported. Please contact the developers")
# Check if the standard star datacube exists
if not os.path.exists(ss_file):
msgs.error("Standard cube does not exist:" + msgs.newline() + ss_file)
msgs.info(f"Loading standard star cube: {ss_file:s}")
# Load the standard star cube and retrieve its RA + DEC
stdcube = fits.open(ss_file)
star_ra, star_dec = stdcube[1].header['CRVAL1'], stdcube[1].header['CRVAL2']

# Extract a spectrum of the standard star
wave, Nlam_star, Nlam_ivar_star, gpm_star = extract_standard_spec(stdcube)

# Extract the information about the blaze
if grating_corr:
blaze_wave_curr, blaze_spec_curr = stdcube['BLAZE_WAVE'].data, stdcube['BLAZE_SPEC'].data
blaze_spline_curr = interp1d(blaze_wave_curr, blaze_spec_curr,
kind='linear', bounds_error=False, fill_value="extrapolate")
# Perform a grating correction
grat_corr = correct_grating_shift(wave, blaze_wave_curr, blaze_spline_curr, blaze_wave, blaze_spline)
# Apply the grating correction to the standard star spectrum
Nlam_star /= grat_corr
Nlam_ivar_star *= grat_corr ** 2

# Read in some information above the standard star
std_dict = flux_calib.get_standard_spectrum(star_type=senspar['star_type'],
star_mag=senspar['star_mag'],
ra=star_ra, dec=star_dec)
# Calculate the sensitivity curve
# TODO :: This needs to be addressed... unify flux calibration into the main PypeIt routines.
msgs.warn("Datacubes are currently flux-calibrated using the UVIS algorithm... this will be deprecated soon")
zeropoint_data, zeropoint_data_gpm, zeropoint_fit, zeropoint_fit_gpm = \
flux_calib.fit_zeropoint(wave, Nlam_star, Nlam_ivar_star, gpm_star, std_dict,
mask_hydrogen_lines=senspar['mask_hydrogen_lines'],
mask_helium_lines=senspar['mask_helium_lines'],
hydrogen_mask_wid=senspar['hydrogen_mask_wid'],
nresln=senspar['UVIS']['nresln'],
resolution=senspar['UVIS']['resolution'],
trans_thresh=senspar['UVIS']['trans_thresh'],
polyorder=senspar['polyorder'],
polycorrect=senspar['UVIS']['polycorrect'],
polyfunc=senspar['UVIS']['polyfunc'])
wgd = np.where(zeropoint_fit_gpm)
sens = np.power(10.0, -0.4 * (zeropoint_fit[wgd] - flux_calib.ZP_UNIT_CONST)) / np.square(wave[wgd])
return interp1d(wave[wgd], sens, kind='linear', bounds_error=False, fill_value="extrapolate")
1 change: 1 addition & 0 deletions doc/calibrations/calibrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ The primary calibration procedures are, in the order they're performed:
flexure
wave_calib
Slit Alignment (IFU only) <alignment>
non-linear correction <nonlinear>
flat_fielding
scattlight

Expand Down
93 changes: 93 additions & 0 deletions doc/calibrations/nonlinear.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

=====================
Non-Linear correction
=====================

The non-linear correction is a simple way to correct the non-linear behavior of the
sensor. Imagine a perfect light source that emits photons with a constant count rate.
An ideal detector would be able to measure this count rate independently of the exposure
time. However, in practice, detectors have a non-linear response to the incoming photons.
This means that the count rate measured by the detector is not proportional to the count
rate of the light source. The non-linear correction is a simple way to correct this
non-linear behavior. Most modern CCD cameras have a linear response to about 1% over
most of the dynamic range, so this correction is not necessary for most applications.
The correction parameters can be measured by the user, but care should be taken with
how you collect the calibration data, since the internal continuum light source used
for most flat-field calibrations is not a perfect light source.

At present, the non-linear correction is only implemented for KCWI, which is already very
close to linear over most of the dynamic range of the CCD. If you have collected non-linear
calibration data with another instrument, please contact the developers to see if we can
implement this correction for your instrument.

Calibrations required
---------------------

The non-linear correction requires a set of dedicated calibration data that can be used to measure
the non-linear response of the detector. This calibration data should be collected with
a continuum light source (usually internal to the instrument). We currently recommend that
you turn on the internal lamp, and leave it for 30 minutes to warm up and settle. The lamp
count rate will likely decay over time, so we recommend that you collect a series of exposures
that are bracketed by `reference` exposures of constant exposure time (also known as the
`bracketed repeat exposure` technique). The reference exposures are used to monitor the
temporal decay of the lamp count rate. Here is a screenshot to show the lamp decay as a
function of time. The points are the `reference exposures` and are colour-coded by the
wavelength of the light, and the solid lines are the best-fit exponential + 2D polynomial
decay curves. The bottom panel shows a residual of the fit. Thus, both the relative shape
and the intensity of the lamp decay can be monitored as a function of time.

.. image:: ../figures/nonlinear_lamp_decay.png

Between a set of reference exposures, you should
acquire a series of exposures with increasing exposure time. The exposure time should be
increased by a factor of 2 between each exposure. The exposure time should be chosen so that
the count rate is not too high, but also not too low. The counts should extend to at least 10%
of the maximum counts of the detector. The `reference` exposures should have an exposure time
that is close to the middle of the range of exposure times used for the other exposures. It is
often good practice to collect an even number of exposures at each exposure time; some shutters
move in opposite directions for adjacent exposures, and the true exposure time may depend on
the direction that the shutter is moving.

To determine the non-linearity, we assume that the correction is quadratic, such that the
quadratic term is a small perturbation from linear response. The functional form of the
true count rate is then:

.. math::

C_T \cdot t = C_M \cdot t (1 + b \cdot C_M \cdot t)

where :math:`C_T` is the true count rate, :math:`C_M` is the measured count rate, :math:`t`
is the exposure time, and :math:`b` is the non-linearity coefficient.
The non-linearity coefficient can be determined by fitting
the measured count rate as a function of the exposure time. That that as the true count rate
tends to zero, the measured count rate tends to zero. The non-linearity coefficient can be
measured for each pixel on the detector, and it is a good idea to consider the non-linearity
coefficient for different amplifiers separately. The above equation can be written as a matrix
equation:

.. math::

M \cdot x = e
M = \begin{bmatrix}
C_{M,1}t_1 & (C_{M,1}t_1)^2 \\
C_{M,2}t_2 & (C_{M,2}t_2)^2 \\
\vdots & \vdots \\
C_{M,n}t_n & (C_{M,n}t_n)^2 \\
\end{bmatrix}
x = \begin{bmatrix}
1/C_T \\
b/C_T \\
\end{bmatrix}
e = \begin{bmatrix}
t_1 \\
t_2 \\
\vdots \\
t_n \\
\end{bmatrix}

where :math:`M` is a matrix of measured count rates, :math:`x` is a vector of the non-linearity
coefficient and the true count rate, and :math:`e` is a vector of exposure times. The non-linearity
coefficient can be determined by inverting the matrix :math:`M`, and solving for :math:`x`. The
non-linearity coefficient can be determined for each pixel on the detector. The central value of
the distribution of non-linearity coefficients can be used as a fixed non-linearity coefficient
for each amplifier of the detector.
Loading
Loading