CR not removed by median stacking
By default, the various recipes in MEGARA DRP use a median combination of three or more images to produce an average exposure and eliminate cosmic rays. However, when exposure times are long, it’s not uncommon for the same pixel to be hit by a cosmic ray in consecutive exposures.
The most straightforward solution is to visually inspect the resulting image
(e.g., the final_rss.fits file) and manually interpolate the affected pixels
using data from neighboring pixels. However, it can be more efficient to use an
automated procedure that detects and corrects pixels impacted by cosmic rays
across multiple exposures, eliminating the need for manual intervention.
Such an automatic method is available in the numina subpackage crmasks, and it can be easily activated by modifying the default image combination method used by MEGARA DRP.
This method should only be applied when three or more equivalent exposures are available. It works by identifying pixels where the signal has been affected by cosmic rays in more than half of the available exposures, such that the median combination still retains an erroneous signal.
Note
This method is likely to be useful primarily when reducing science images with long exposure times. In general, it is not recommended for the reduction of calibration images.
Overall procedure
The main workflow for applying the method is as follows:
Prepare the observation result file: Create the YAML file that lists the individual exposures and specifies additional requirements for the reduction recipe associated with the reduction recipe MegaraCrDetection.
Run the MEGARA DRP to generate the CR masks: The execution of the MEGARA DRP with this YAML file is interactive (see below), and the result is a FITS file named
crmasks.fits, which contains several masks that can later be used to remove the cosmic rays using different strategies. This file must be copied to the data/ subdirectory.Run the MEGARA DRP to generate the reduced scientific result: Once the file
crmasks.fitsis placed in the data/ subdirectory, the MEGARA DRP can be run again to generate the reduced scientific result, using for that purpose the corresponding YAML file. During this step, the user must chose one of the available strategies for combining exposures and removing cosmic rays. Instead of using the defaultmediancombination, one must specify one of the following methods in the requirements section:mediancr,meancrtormeancr.
Note
The three available combination methods, mediancr, meancrt and
meancr, produce different results:
mediancr: performs a median combination, replacing pixels suspected of being affected by cosmic rays in more than one exposure by the minimum value at each pixel across the available exposures.meancrt: generates the mean combination using of a single mask that stores all the cosmic rays detected in all the individual exposures, replacing each masked pixels by the value obtained when using themediancrmethod.meancr: also performs a mean combination, but uses the individual cosmic ray masks specifically computed for each available exposure.
Since the mean has a lower standard deviation than the median, the
meancrt and meancr methods are generally preferable. Among these
two, tests suggest that meancr tends to yield better
results. However, it is recommended to try all three methods and compare the
outputs to determine which works best for your specific dataset.
Preparing the observation result file
Let’s assume that we have three initially equivalent exposures. The process
begins with the creation of a YAML file, e.g. 8_generate_crmasks.yaml,
which lists the individual exposures to be used in the cosmic ray detection
step.
1id: 8_LR-R_crmasks
2mode: MegaraCrDetection
3instrument: MEGARA
4frames:
5 - 0003978527-20231115-MEGARA-MegaraLcbImage.fits
6 - 0003978528-20231115-MEGARA-MegaraLcbImage.fits
7 - 0003978529-20231115-MEGARA-MegaraLcbImage.fits
8requirements:
9 crmethod: mm_lacosmic # lacosmic | mmcosmic | mm_lacosmic
10 use_lamedian: False # use LAMEDIAN insteand of minimum value
11 interactive: True # display and pause between plots
12 flux_factor: none # none | auto | [list of values]
13 flux_factor_regions: # list of regions, FITS criterium
14 # - [xmin, xmax, ymin, ymax] # <-- format (no need to uncomment this)
15 apply_flux_factor_to: simulated # original | simulated (data)
16 dilation: 1 # dilation around suspected pixels (integer)
17 regions_to_be_skipped: # list of regions, FITS criterium
18 # - [xmin, xmax, ymin, ymax] # <-- format (no need to uncomment this)
19 pixels_to_be_flagged_as_cr: # FITS criterium, first pixel is [1, 1]
20 # - [X, Y] # <-- format (no need to uncomment this)
21 pixels_to_be_ignored_as_cr: # FITS criterium, first pixel is [1, 1]
22 # - [X, Y] # <-- format (no need to uncomment this)
23 pixels_to_be_replaced_by_local_median: # FITS criterium
24 # - [X, Y, X_width, Y_width] # <-- format (no need to uncomment this)
25 verify_cr: False # ask for confirmation
26 semiwindow: 15 # to display residual CR hits
27 color_scale: zscale # minmax | zscale
28 maxplots: -1 # -1: all CR detected are plotted
29 #-----------------------------------------------------------------------------
30 # Parameters for L.A. Cosmic (van Dokkum 2001), as implemented in ccdproc
31 # (based in https://github.com/astropy/astroscrappy).
32 # https://ccdproc.readthedocs.io/en/latest/api/ccdproc.cosmicray_lacosmic.html
33 la_gain_apply: True # Return gain-corrected data
34 la_sigclip: 5.0 # Laplacian-to-noise limit for cosmic ray detection
35 la_sigfrac: 0.3 # Fractional detection limit for neighboring pixels
36 la_objlim: 5.0 # Contrast between Laplacian and fine struct. images
37 la_satlevel: 65535.0 # Saturation level of the image (ADU)
38 la_niter: 4 # Number of iterations to perform
39 la_sepmed: True # Use the separable median filter (not the full filter)
40 la_cleantype: meanmask # median | medmask | meanmask | idw
41 la_fsmode: convolve # median | convolve
42 la_psfmodel: gaussxy # gauss | moffat | gaussx | gaussy | gaussxy
43 la_psffwhm_x: 2.5 # FWHM of the PSF (X direction) to generate the kernel
44 la_psffwhm_y: 2.5 # FWHM of the PSF (Y direction) to generate the kernel
45 la_psfsize: 11 # size of the kernel
46 la_psfbeta: 4.765 # Moffat beta parameter
47 la_verbose: True # Print to the screen or not
48 #-----------------------------------------------------------------------------
49 # Parameters for the Median-Minimum method (Cardiel et al. 2025)
50 # rectangular region to determine offsets between individual images:
51 mm_crosscorr_region: null # [xmin, xmax, ymin, ymax] FITS criterium | null
52 mm_boundary_fit: spline # piecewise | spline
53 mm_knots_splfit: 3 # used only for spline fit
54 mm_fixed_points_in_boundary:
55 # - [x, y, weight] # weights are optional(ignored) for spline(piecewise) fit
56 mm_nsimulations: 5 # simulations for each set of images
57 mm_niter_boundary_extension: 3 # iterations to extend the spline fit
58 mm_weight_boundary_extension: 10.0 # multiplicative increment in weigth
59 mm_threshold: 0.0 # minimum median2d - min2d value
60 mm_minimum_max2d_rnoise: 5.0 # minimum max2d (in readout units)
61 mm_seed: 123456 # random seed for reproducibility
62 #----------------------------------------------------------------------------
Note that in this file is important to specify:
mode: MegaraCrDetection, so the MEGARA DRP will know that the8_generate_crmasks.yamlfile is intended for generating the CR masks.frames:one should provide here the list of the individual equivalent exposures.requirements:this section contains a large set of parameters whose values define the strategy used for the cosmic-ray detection. This section is identical to therequirementssection of the configuration YAML file employed bynumina-crmasks, which is described in detail in this link.
Computing the CR masks
Coincident cosmic rays in the median combination
The recipe is run by doing:
(megara) $ numina run 8_generate_crmasks.yaml -r control.yaml
From this point, follow the description given in this example,
which helps understanding the impact of using different parameters in the
cosmic-ray pixel detection process. Important: in that example the
generation of the CR masks is carried out by executing the command-line script
numina-crmasks, which makes use of a slightly different input YAML file.
However, the code executed is the same as in the MegaraCrDetection recipe,
so the resulting output is identical.
Output file crmasks.fits
The output file crmasks.fits is generated in the results/ subdirectory,
and contains several extensions:
(megara) $ cd obsid8_LcbImage_LR-R_crmasks_results
(megara) $ fitsinfo crmasks.fits
The primary HDU does not contain image data but includes keywords that document the parameters used to generate the masks.
The next extensions store the actual masks computed by the program:
MEDIANCR: Mask for pixels suspected of being affected by two cosmic rays in the median combination.MEANCRT: Mask for pixels suspected of being affected by a cosmic ray in the mean combination.CRMASK1,CRMASK2,CRMASK3: Masks for pixels suspected of being affected by a cosmic ray in each of the individual exposures.
In all these masks, pixels suspected of being affected by a cosmic ray are set to 1, while the rest of the pixels are set to 0.
Finally, the extension LAMEDIAN contains the corrected image generated by
the L.A. Cosmic algorithm (van Dokkum 2001).
Do not forget to copy the file crmasks.fits to the data/ subdirectory
before running the MEGARA DRP to generate the reduced scientific result.
(megara) $ cp crmasks.fits ../data/
Applying the masks
Once the crmasks.fits file has been generated, it can be used to remove the
suspected cosmic rays from the combined image. Several options are available
for this step, depending on the value of method specified in the
requirements section of the YAML file used to produce the reduced scientific
result. Rather than assuming the combination method is median (the default
in MEGARA DRP), the user should explicitly specify one of the following
methods: mediancr, meancr or meancrt.
Method mediancr
For example, when using the MegaraLcbImage recipe, the corresponding
YAML file, 8_LcbImage.yaml should define the method and
crmasks parameters in the requirements section, as follows:
1id: 8_LcbImage_LR-R
2mode: MegaraLcbImage
3instrument: MEGARA
4frames:
5 - 0003978527-20231115-MEGARA-MegaraLcbImage.fits
6 - 0003978528-20231115-MEGARA-MegaraLcbImage.fits
7 - 0003978529-20231115-MEGARA-MegaraLcbImage.fits
8requirements:
9 method: mediancr
10 method_kwargs:
11 apply_flux_factor: False
12 use_lamedian: False
13 crmasks: ../data/crmasks.fits
14 extraction_offset: [+3]
15 ignored_sky_bundles: [96] #[93, 94, 96, 97, 99, 100]
16 reference_extinction: extinction_LP.txt
Note that the crmasks parameter must be explicitly specified, and it should
point to the crmasks.fits file generated in the previous step. This
requirement also gives users the flexibility to rename the file and test
different versions of it (each potentially created with varying detection
thresholds) to evaluate the impact of using different masks.
(megara) $ numina run 8_LcbImage.yaml -r control.yaml
When using method: mediancr, the MEGARA DRP reads the mask stored in the
MEDIANCR extension of the crmasks.fits file. The masked pixels (those
suspected of being affected by a cosmic ray in more than one exposure) are
replaced with the corresponding min2d value at each pixel. As a result, the
final image is identical to median2d, except for the corrected pixels.
Note that under the label method_kwargs: there are several parameters that
control how the information from the crmasks.fits file is used:
apply_flux_factor:When set toTrue, the factors listed in theFLUXF1,FLUXF2, etc. keywords of the primary HDU in thecrmasks.fitsfile are used to modify the signal of each individual exposure before combining them and removing cosmic rays. If this parameter is set toFalse, an effective factor of1.0is applied (i.e., no change is introduced), regardless of the values in the FITS header of thecrmasks.fitsfile.use_lamedian:: When set toTrue, the masked pixels are replaced with the corrected value obtained using the L.A. Cosmic algorithm, rather than using the minimum value in each pixel.
Method meancrt
1id: 8_LcbImage_LR-R
2mode: MegaraLcbImage
3instrument: MEGARA
4frames:
5 - 0003978527-20231115-MEGARA-MegaraLcbImage.fits
6 - 0003978528-20231115-MEGARA-MegaraLcbImage.fits
7 - 0003978529-20231115-MEGARA-MegaraLcbImage.fits
8requirements:
9 method: meancrt
10 method_kwargs:
11 apply_flux_factor: False
12 use_lamedian: False
13 crmasks: ../data/crmasks.fits
14 extraction_offset: [+3]
15 ignored_sky_bundles: [96] #[93, 94, 96, 97, 99, 100]
16 reference_extinction: extinction_LP.txt
When using method: meancrt, the MEGARA DRP reads the mask stored in
the MEANCRT extension of the file crmasks.fits. The masked pixels,
which are suspected of being affected by a cosmic ray, are replaced by
the corresponding values when using method: mediancr.
As a result, the final image is identical to mean2d, except for the
corrected pixels, where the data is sourced from the image produced by the
mediancr method.
The parameters under the method_kwargs: label play the same role as
described above for the mediancr combination method.
Method meancr
1id: 8_LcbImage_LR-R
2mode: MegaraLcbImage
3instrument: MEGARA
4frames:
5 - 0003978527-20231115-MEGARA-MegaraLcbImage.fits
6 - 0003978528-20231115-MEGARA-MegaraLcbImage.fits
7 - 0003978529-20231115-MEGARA-MegaraLcbImage.fits
8requirements:
9 method: meancr
10 method_kwargs:
11 apply_flux_factor: False
12 use_lamedian: False
13 crmasks: ../data/crmasks.fits
14 extraction_offset: [+3]
15 ignored_sky_bundles: [96] #[93, 94, 96, 97, 99, 100]
16 reference_extinction: extinction_LP.txt
When using method: meancr, the MEGARA DRP reads the masks stored in the
CRMASK1, CRMASK2 and CRMASK3 extensions of the file
crmasks.fits. The program uses NumPy masked arrays to construct a 3D stack
of the individual exposures, where each image is represented as a masked array
based on its corresponding mask. This allows the program to compute the mean
along the axis corresponding to the image number, excluding the masked pixels.
In cases where a pixel is masked in all individual exposures, the corresponding
pixel in the combined image is set to the min2d value at that location.
The parameters under the method_kwargs: label play the same role as
described above for the mediancr combination method.