|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from gempy.gemini import gemini_tools as gt |
| 4 | + |
| 5 | +from ..gemini.primitives_gemini import Gemini |
| 6 | +from . import parameters_irdc |
| 7 | + |
| 8 | +from recipe_system.utils.decorators import parameter_override, capture_provenance |
| 9 | + |
| 10 | + |
| 11 | +@parameter_override |
| 12 | +@capture_provenance |
| 13 | +class IRDC(Gemini): |
| 14 | + """ |
| 15 | + This is the class containing all of the preprocessing primitives |
| 16 | + for instruments that use the Gemini Infrared Detector Controller (IRDC). |
| 17 | + """ |
| 18 | + tagset = {"GEMINI"} |
| 19 | + |
| 20 | + def _initialize(self, adinputs, **kwargs): |
| 21 | + super()._initialize(adinputs, **kwargs) |
| 22 | + self._param_update(parameters_irdc) |
| 23 | + |
| 24 | + def nonlinearityCorrect(self, adinputs=None, suffix=None): |
| 25 | + """ |
| 26 | + Run on raw or nprepared Gemini NIRI data, this script calculates and |
| 27 | + applies a per-pixel linearity correction based on the counts in the |
| 28 | + pixel, the exposure time, the read mode, the bias level and the ROI. |
| 29 | + Pixels over the maximum correctable value are set to BADVAL unless |
| 30 | + given the force flag. Note that you may use glob expansion in infile, |
| 31 | + however, any pattern matching characters (*,?) must be either quoted |
| 32 | + or escaped with a backslash. Do we need a badval parameter that defines |
| 33 | + a value to assign to uncorrectable pixels, or do we want to just add |
| 34 | + those pixels to the DQ plane with a specific value? |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + suffix: str |
| 39 | + suffix to be added to output files |
| 40 | + """ |
| 41 | + # Instantiate the log |
| 42 | + log = self.log |
| 43 | + log.debug(gt.log_message("primitive", self.myself(), "starting")) |
| 44 | + timestamp_key = self.timestamp_keys[self.myself()] |
| 45 | + |
| 46 | + def linearize(counts, coeffs): |
| 47 | + """Return a linearized version of the counts in electrons per coadd""" |
| 48 | + # The coefficients are in the form of a {region: coeffs} dictionary. |
| 49 | + corrected_counts = np.empty_like(counts) |
| 50 | + for _slice, coeff in coeffs.items(): |
| 51 | + log.debug("Coefficients for {} rows = {:.6f} " |
| 52 | + "{:.9e} {:.9e}".format( |
| 53 | + _slice, coeff.time_delta, coeff.gamma, coeff.eta)) |
| 54 | + corrected_counts[_slice] = ( |
| 55 | + counts[_slice] * (1 + counts[_slice] * |
| 56 | + (np.float32(coeff.gamma) + |
| 57 | + counts[_slice] * np.float32(coeff.eta)))) |
| 58 | + return corrected_counts |
| 59 | + |
| 60 | + for ad in adinputs: |
| 61 | + if ad.phu.get(timestamp_key): |
| 62 | + log.warning("No changes will be made to {}, since it has " |
| 63 | + "already been processed by nonlinearityCorrect". |
| 64 | + format(ad.filename)) |
| 65 | + continue |
| 66 | + |
| 67 | + total_exptime = ad.exposure_time() |
| 68 | + coadds = ad.coadds() |
| 69 | + # Check the raw exposure time (i.e., per coadd). First, convert |
| 70 | + # the total exposure time returned by the descriptor back to |
| 71 | + # the raw exposure time |
| 72 | + exptime = np.float32(total_exptime / |
| 73 | + (coadds if ad.is_coadds_summed() else 1)) |
| 74 | + if exptime > 600: |
| 75 | + log.warning(f"Exposure time {exptime} for {ad.filename} is " |
| 76 | + "outside the range used to derive correction.") |
| 77 | + |
| 78 | + for ext, gain, coeffs in zip(ad, ad.gain(), self._nonlinearity_coeffs(ad)): |
| 79 | + if coeffs is None: |
| 80 | + log.warning("No nonlinearity coefficients found for " |
| 81 | + f"{ad.filename} extension {ext.id} - " |
| 82 | + "no correction applied") |
| 83 | + continue |
| 84 | + elif not isinstance(coeffs, dict): |
| 85 | + # coeffs apply to entire array |
| 86 | + coeffs = {None: coeffs} |
| 87 | + |
| 88 | + raw_mean_value = np.mean(ext.data) / coadds |
| 89 | + log.fullinfo("The mean value of the raw pixel data in " \ |
| 90 | + "{} is {:.8f}".format(ext.filename, raw_mean_value)) |
| 91 | + |
| 92 | + # Create a new array that contains the corrected pixel data. |
| 93 | + # Remember that gain() returns 1.0 after ADUToElectrons |
| 94 | + raw_pixel_data = ext.data * np.float32(gain / coadds) |
| 95 | + corrected_pixel_data = (linearize(raw_pixel_data, coeffs) * |
| 96 | + np.float32(coadds / gain)) |
| 97 | + |
| 98 | + # Try to do something useful with the VAR plane, if it exists |
| 99 | + # Since the data are fairly pristine, VAR will simply be the |
| 100 | + # Poisson noise (divided by gain if in ADU), possibly plus RN**2 |
| 101 | + # So making an additive correction will sort this out, |
| 102 | + # irrespective of whether there's read noise |
| 103 | + if ext.variance is not None: |
| 104 | + ext.variance += (corrected_pixel_data - ext.data) / np.float32(gain) |
| 105 | + # Now update the SCI extension |
| 106 | + ext.data = corrected_pixel_data |
| 107 | + |
| 108 | + # Correct for the exposure time issue by scaling the counts |
| 109 | + # to the nominal exposure time |
| 110 | + time_delta = np.float32(np.mean([v.time_delta for v in coeffs.values()])) |
| 111 | + ext.multiply(exptime / (exptime + time_delta)) |
| 112 | + |
| 113 | + # Determine the mean of the corrected pixel data |
| 114 | + corrected_mean_value = np.mean(ext.data) / coadds |
| 115 | + log.fullinfo("The mean value of the corrected pixel data in " |
| 116 | + "{} is {:.8f}".format(ext.filename, corrected_mean_value)) |
| 117 | + |
| 118 | + # Correct the exposure time by adding coeff1 * coadds |
| 119 | + total_exptime = total_exptime + time_delta * coadds |
| 120 | + |
| 121 | + # Update descriptors for saturation and nonlinear thresholds |
| 122 | + log.fullinfo(f"The true total exposure time = {total_exptime}") |
| 123 | + for desc in ('saturation_level', 'non_linear_level'): |
| 124 | + current_value = getattr(ext, desc)() |
| 125 | + new_value = linearize( |
| 126 | + np.array([current_value * gain / coadds]), |
| 127 | + coeffs)[0] * coadds / gain |
| 128 | + ext.hdr[ad._keyword_for(desc)] = np.round(new_value, 3) |
| 129 | + |
| 130 | + # Timestamp and update filename |
| 131 | + gt.mark_history(ad, primname=self.myself(), keyword=timestamp_key) |
| 132 | + ad.update_filename(suffix=suffix, strip=True) |
| 133 | + return adinputs |
0 commit comments