Determination of initial slice boundaries

Warning

Please note that the code shown below is under development and may undergo modifications. If problems arise, try first installing the development versions of numina and fridadrp.

General procedure

To determine the boundaries of each of the 30 slices obtained on the H2RG detector when using FRIDA in IFS mode, we start with a QTH lamp exposure. As a test, we use the images:

  • 0000000096-20260505-FRIDA-FridaSuccess-raw.fits: QTH (Quartz-Tungsten-Halogen) lamp

  • 0000000132-20260506-FRIDA-FridaSuccess-raw.fits: spectral traces

Both images are available in the subdirectory Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent, which contains some of the first calibration images obtained.

The general procedure uses the following scripts:

  • fridadrp-find_slice_boundary_borders_from_flat: determination of the boundaries of each slice.

  • fridadrp-fit_slice_boundary_borders_with_polynomials: fitting of the boundaries using polynomials.

  • fridadrp-overplot_slice_boundary_polynomials: overlay of the detected boundaries for each slice, as well as the resulting polynomial fits, on arbitrary images. This script also allows overlaying the polynomial fit on the spectral traces.

  • fridadrp-predict_polynoimal_slice_borders: extrapolation of the boundaries of the first and last slices when either of these two falls outside the detector.

  • fridadrp-find_traces_within_slice_boundary_polynomials: polynomial fitting of the spectral traces contained within each slice.

  • fridadrp--interpolate_traces_within_slices: this script allows the interpolation of incorrect spectral traces using information from the same traces in slices of the same family.

Polynomial boundaries of the slices

Slice borders at a single column

After testing different approaches, a method that seems to work well consists of taking vertical cuts of the image and using the positions of the first and second derivatives. The first derivatives indicate approximately where the signal rises and falls occur. The second derivative (searched for in the neighborhood of each first derivative) allows us to approximate the region where the signal has maximum concavity (which provides a more generous slice width than the first derivative). Finally, we opted to fit a straight line between the first and second derivative of each signal rise (or fall), and extrapolate that line until it intersects a value coinciding with the minimum signal between each pair of slices (after adding 1% of the 90th-percentile signal of the slice whose edge we want to estimate).

(venv_numina) $ fridadrp-find_slice_boundary_borders_from_flat --help
Usage: fridadrp-find_slice_boundary_borders_from_flat [-h]
                                                      [--flatfile FLATFILE]
                                                      [--output OUTPUT]
                                                      [--overwrite]
                                                      [--slice-ini SLICE_INI]
                                                      [--slice-end SLICE_END]
                                                      [--row-ini ROW_INI]
                                                      [--row-end ROW_END]
                                                      [--colrange MIN MAX]
                                                      [--xmedian XMEDIAN]
                                                      [--savgol-ywindow SAVGOL_YWINDOW]
                                                      [--savgol-polyorder SAVGOL_POLYORDER]
                                                      [--plots]
                                                      [--output-dir OUTPUT_DIR]
                                                      [--record] [--echo]
                                                      [--version]
                                                      [--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]

Find the slice boundaries from flat image

Options:
  -h, --help            show this help message and exit
  --flatfile FLATFILE   Path to the flat file
  --output OUTPUT       Output FITS file name
  --overwrite           Overwrite output file if it exists
  --slice-ini SLICE_INI
                        Initial slice number (1-based index)
  --slice-end SLICE_END
                        Final slice number (1-based index)
  --row-ini ROW_INI     Initial row number (1-based index) along NAXIS2
  --row-end ROW_END     Final row number (1-based index) along NAXIS2
  --colrange MIN MAX    Column range to analyze (1-based index) along NAXIS1.
                        This option can be specified multiple times
  --xmedian XMEDIAN     Size of the median filter along NAXIS1 axis (odd;
                        default: 21)
  --savgol-ywindow SAVGOL_YWINDOW
                        Savitzky-Golay filter window size along NAXIS2 axis
                        (default: 5)
  --savgol-polyorder SAVGOL_POLYORDER
                        Savitzky-Golay filter polynomial order along NAXIS2
                        axis (default: 2)
  --plots               Display plots
  --output-dir OUTPUT_DIR
                        Output directory (default: .)
  --record              Record terminal output
  --echo                Display full command line
  --version             Display version
  --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        Set the logging level

We start by running the script using a single column --colrange 501 501. This way, the code shows us several figures that allow us to understand how the calculations are being performed.

(venv_frida) $ fridadrp-find_slice_boundary_borders_from_flat \
  --flatfile 0000000096-20260505-FRIDA-FridaSuccess-raw.fits \
  --colrange 501 501 \
  --plots
────────── Welcome to fridadrp-find_slice_boundary_borders_from_flat ───────────
Using fridadrp.tools.find_slice_boundary_borders_from_flat version              
0.1.dev248+g830b3980e.d20260804                                                 
Columns to analyze: from 501 to 501 (total 1 columns) - Mode: continuous        
Output file name is None. No output file will be created.                       
------------------------------------------------------------------------------- 
'?': show this help message                                                     
'a': toggle imshow aspect='equal' / aspect='auto'                               
'v': set vmin and vmax manually                                                 
',': set vmin and vmax to min and max of the zoomed region                      
'/': set vmin and vmax using zscale of the zoomed region                        
'q': quit                                                                       
------------------------------------------------------------------------------- 
mosaic with original and processed input exposure

Fig. 1 Mosaic showing the initial processing of the QTH image. Top left, initial image. Top right, image smoothed with a median filter along the horizontal axis. The bottom images show the first (left) and second (right) derivatives when moving vertically along the image. The derivatives are computed using a Savitzky-Golay filter. The default parameters for this filter are --savgol-ywindow 5 and --savgol-polyorder 2.

When using the zoom option on any of the 4 images, that operation is applied identically to the other three.

zoom mosaic with original and processed input exposure

Fig. 2 Result of applying zoom to the previous figure (Fig. 1). This allows us to better visualize how both the first derivatives (with positive and negative sign) and the second derivatives (with positive sign) can be used as a first estimate of the slice boundaries.

After pressing q to close the previous figure, the code shows us, in a new figure, how the signal changes along the selected column (501 in this example).

TBD

Fig. 3 Vertical cut along column 501. Gray shows the original image. Blue shows the image after applying the median filter along the X axis (--xmedian 21 by default). The red filled circles show the positions where the first derivative has a maximum (large circle) and a minimum (small circle). The green filled circles indicate the positions where the second derivative has a maximum (i.e., has concave curvature seen from above); the large circle corresponds to the left edge and the small circle to the right edge. The black ‘x’ symbols mark the minimum between each pair of slices. The black filled circle indicates the 90th percentile of the smoothed image and is placed in the central region of each slice.

The previous figure clearly shows that, although the second derivatives seem to bound the slice edges fairly well, they underestimate their dimensions. After several tests, a good alternative consists of performing a linear fit between the positions of the first and second derivatives on each side of each slice, and extrapolating that value until it reaches a value close to the minimum signal reached in the gap between slices. The following figure shows what happens in the gaps between all the slices.

TBD

Fig. 4 Mosaic showing what happens in the gaps between consecutive slices. The red and green filled circles again correspond to the first and second derivatives. On both sides of each slice, a linear fit has been performed to these points. The extrapolation outward from each slice is carried out until reaching a signal that exceeds the minimum value in the corresponding gap by an amount equal to 1% of the difference between the median signal within the slice and the minimum value in the gap. The black filled circles indicate that extrapolated positions.

For the left edge of the first slice and the right edge of the last slice, the extrapolation thresholds corresponding to the right and left edges of the respective slices are used.

Left border Left border
Right border Right border

Fig. 5 Determination of the extrapolation of the left edge for the first slice (ID 30) and the right edge for the last slice (ID 15).

Once the boundaries of all slices have been determined, the script shows us a summary mosaic with the result.

TBD

Fig. 6 Final determination of the boundaries of all slices for column 501 of the image. The black dashed lines indicate the limits obtained. The curves corresponding to the first derivative (solid red line) and second derivative (solid green line) are overplotted.

Since we have only selected one column using --colrange 501 501, after pressing q, the script ends execution without generating any output file.

Working on columns...                                                           
0%                                                                              
0% 10%_____________________________________ (elap=0:22|left=3:19|exp=3:41)      
0% 10% 20%_________________________________ (elap=0:22|left=1:28|exp=1:51)      
0% 10% 20% 30%_____________________________ (elap=0:22|left=0:52|exp=1:14)      
0% 10% 20% 30% 40%_________________________ (elap=0:22|left=0:33|exp=0:55)      
0% 10% 20% 30% 40% 50%_____________________ (elap=0:22|left=0:22|exp=0:44)      
0% 10% 20% 30% 40% 50% 60%_________________ (elap=0:22|left=0:15|exp=0:37)      
0% 10% 20% 30% 40% 50% 60% 70%_____________ (elap=0:22|left=0:09|exp=0:32)      
0% 10% 20% 30% 40% 50% 60% 70% 80%_________ (elap=0:22|left=0:06|exp=0:28)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90%_____ (elap=0:22|left=0:02|exp=0:25)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% (elap=0:22|left=0:00|exp=0:22)      
Number of useful columns processed: 1 out of 1                                  
Slice boundary borders computed for column 501. Not saved to FITS file since a  
single column is specified.                                                     
Total time elapsed: 0:00:26.822534                                              
──────────────────────────────────  Goodbye!  ──────────────────────────────────

We can compare the slice widths as a function of the slice ID.

TBD

Fig. 7 Comparison of the widths of the different slices, grouped into the two ID families, with the slices centered around its mid point.

TBD

Fig. 8 Comparison of the widths of the different slices, grouped into the two ID families, with the slices aligned to the left border.

TBD

Fig. 9 Comparison of the widths of the different slices, grouped into the two ID families, with the slices aligned to the right border.

Another way of representing the same information consists of plotting the slice width as a function of ID number.

TBD

Fig. 10 Comparison of the widths of the different slices, grouped into the two ID families. As will be discussed below, slice ID 15 shows a narrower width than expected. This is because it is partially located outside the useful region of the detector.

The most important aspect to highlight from the previous figure is that the variation in slice width appears to fit reasonably well to a 2nd degree polynomial. This will be useful later on, when we need to predict the location of the boundaries of a slice that may not appear complete on the detector.

Slice borders at multiple columns

The --colrange parameter (column number along NAXIS1, starting at 1) allows one or more ranges to be defined. If there are several ranges, --colrange N1 N2 must be repeated. For example, we could use: --colrange 10 800 --colrange 1500 2000. If --colrange is not specified, the script uses all initially valid columns of the H2RG detector (from 5 to 2044; the detector has an outer border of 4 pixels that contains no useful signal).

Now that we have shown how the boundary of each slice is calculated for a particular column, we can run the script for all columns.

(venv_frida) $ fridadrp-find_slice_boundary_borders_from_flat \
  --flatfile 0000000096-20260505-FRIDA-FridaSuccess-raw.fits \
  --overwrite
────────── Welcome to fridadrp-find_slice_boundary_borders_from_flat ───────────
Using fridadrp.tools.find_slice_boundary_borders_from_flat version              
0.1.dev248+g830b3980e.d20260804                                                 
Column ranges not specified. Using default range: 5 to 2044.                    
Columns to analyze: from 5 to 2044 (total 2040 columns) - Mode: continuous      
Working on columns...                                                           
0%                                                                              
0% 10%_____________________________________ (elap=0:06|left=0:51|exp=0:57)      
0% 10% 20%_________________________________ (elap=0:11|left=0:45|exp=0:57)      
0% 10% 20% 30%_____________________________ (elap=0:17|left=0:40|exp=0:57)      
0% 10% 20% 30% 40%_________________________ (elap=0:23|left=0:34|exp=0:57)      
0% 10% 20% 30% 40% 50%_____________________ (elap=0:29|left=0:29|exp=0:57)      
0% 10% 20% 30% 40% 50% 60%_________________ (elap=0:35|left=0:23|exp=0:58)      
0% 10% 20% 30% 40% 50% 60% 70%_____________ (elap=0:41|left=0:17|exp=0:58)      
0% 10% 20% 30% 40% 50% 60% 70% 80%_________ (elap=0:46|left=0:12|exp=0:58)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90%_____ (elap=0:52|left=0:06|exp=0:58)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% (elap=0:58|left=0:00|exp=0:58)      
Number of useful columns processed: 1988 out of 2040                            
Slice boundary borders saved to: slice_boundary_borders_from_flat_1-30.fits     
Total time elapsed: 0:00:59.488915                                              
──────────────────────────────────  Goodbye!  ──────────────────────────────────

Since in the last run we did not use the --colrange argument, the script makes use of all initially valid columns of the H2RG detector (columns 5 to 2044 along NAXIS1).

The result is stored in a FITS file, whose default name is slice_boundary_borders_from_flat_1-30.fits. The suffix 1-30 before the extension indicates that the script has fitted all 30 slices (see below for other cases). The output file name can be modified using --output <filename>. If the file already exists, the program raises an error unless --overwrite is used.

The output file contains 3 extensions.

(venv_frida) $ fitsinfo slice_boundary_borders_from_flat_1-30.fits 
Filename: slice_boundary_borders_from_flat_1-30.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU      63   ()      
  1  L-BORDER      1 ImageHDU         8   (2048, 30)   float64   
  2  R-BORDER      1 ImageHDU         8   (2048, 30)   float64   
  3  SLIWIDTH      1 ImageHDU         8   (2048, 30)   float64

The L-BORDER and R-BORDER extensions contain the coordinates (position along NAXIS2, using array indices, i.e., ranging from 0 to NAXIS2-1) of the points that could be computed. For some columns of the detector (positions along NAXIS1) these points could not be computed and their value is NaN. This happens when the peaks of the first and second derivatives do not maintain the expected order. Normally this is only a subset of the columns, so the boundaries should still be fittable. We leave this task for the script fridadrp-fit_slice_boundary_borders_with_polynomials. The SLIWIDTH extension contains the width of the slices as a function of column number (it inherits the NaNs from the two previous extensions).

We can visualize the detected boundary borders using the script fridadrp-overplot_slice_boundary_polynomials.

(venv_numina) $ fridadrp-overplot_slice_boundary_polynomials --help
Usage: fridadrp-overplot_slice_boundary_polynomials [-h] [--poly POLY]
                                                    [--borders BORDERS]
                                                    [--traces TRACES]
                                                    --image IMAGE
                                                    [--voffset VOFFSET]
                                                    [--sliceid] [--traceid]
                                                    [--xlim XLIM XLIM]
                                                    [--ylim YLIM YLIM]
                                                    [--pdf-mosaic PDF_MOSAIC]
                                                    [--montage {png,pdf}]
                                                    [--output-dir OUTPUT_DIR]
                                                    [--record] [--echo]
                                                    [--version]
                                                    [--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]

Overplot the slice boundaries (borders and/or polynomials) and/or traces on
image

Options:
  -h, --help            show this help message and exit
  --poly POLY           Path to the file with the boundary polynomials
                        (optional)
  --borders BORDERS     Path to the file with the boundary borders (optional)
  --traces TRACES       Path to the file with the slice trace polynomials
                        (optional)
  --image IMAGE         Image to display boundaries on
  --voffset VOFFSET     Vertical constant offset (pixels) to apply
  --sliceid             Overplot slice ID
  --traceid             Overplot trace ID
  --xlim XLIM XLIM      X-axis limits for the plot (xmin, xmax); array indices
                        (1-based along NAXIS1 axis)
  --ylim YLIM YLIM      Y-axis limits for the plot (ymin, ymax); array indices
                        (1-based along NAXIS2 axis)
  --pdf-mosaic PDF_MOSAIC
                        Output PDF file to save zoomed images of all the
                        slices
  --montage {png,pdf}   Display a montage of the final plots with traces for
                        every slice (requires ImageMagick's montage)
  --output-dir OUTPUT_DIR
                        Output directory (default: .)
  --record              Record terminal output
  --echo                Display full command line
  --version             Display version
  --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        Set the logging level

We run this script using as inputs both the QTH image and the FITS file generated by the script fridadrp-find_slice_boundary_borders_from_flat.

(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --borders slice_boundary_borders_from_flat_1-30.fits \
  --image 0000000096-20260505-FRIDA-FridaSuccess-raw.fits \
  --sliceid
─────────── Welcome to fridadrp-overplot_slice_boundary_polynomials ────────────
Using fridadrp.tools.overplot_slice_boundary_polynomials version                
0.1.dev248+g830b3980e.d20260804                                                 
Reading slice boundary borders from                                             
slice_boundary_borders_from_flat_1-30.fits                                      
Number of slices defined: SLCNUMT=30                                            
Number of NaN values in collapsed left border : 60                              
Number of NaN values in collapsed right border: 60                              
Read 30 left borders and 30 right borders from                                  
slice_boundary_borders_from_flat_1-30.fits.                                     
------------------------------------------------------------------------------- 
'?': show this help message                                                     
'a': toggle imshow aspect='equal' / aspect='auto'                               
'h': reset zoom to initial limits                                               
'v': set vmin and vmax manually                                                 
',': set vmin and vmax to min and max of the zoomed region                      
'/': set vmin and vmax using zscale of the zoomed region                        
'q': quit                                                                       
------------------------------------------------------------------------------- 
Total time elapsed: 0:00:07.709395                                              
──────────────────────────────────  Goodbye!  ──────────────────────────────────
mosaic with original and processed input exposure

Fig. 11 Full image with the detected boundaries. Note that for some columns these boundaries do not exist because the detection process did not work correctly. This effect is not very significant because, as we can verify in the terminal output, the number of columns with NaNs is small.

We can zoom in to better appreciate what happens with different slices. It is worth paying attention to what happens at the bottom and top of the detector.

Lower border Lower border
Upper border Upper border

Fig. 12 Zoomed view of what happens in the first (left panel) and last (right panel) slices.

In this case it is worth noting that the slice appearing at the top edge (slice ID 15) falls partially outside the detector. The upper boundaries of that slice are underestimated because the useful detector information ends at pixel 2044.

Lower border Lower border
Upper border Upper border

Fig. 13 Detail of the last slice (ID 15), using different cuts. It can be seen how the signal of this slice is truncated at the top because part of the slice falls outside the useful region of the detector.

We can easily visualize how the width of each slice varies as we move along NAXIS1, as well as the variation between different slices. It is enough to plot the 2D image stored in the SLIWIDTH extension of the file slice_boundary_borders_from_flat_1-30.fits.

(venv_frida) $ numina-ximshow slice_boundary_borders_from_flat_1-30.fits \
  --extname sliwidth \
  --cbar_orientation vertical \
  --cbar_label "Slice width (pixels)" \
  --ylabel "Slice number" \
  --cmap viridis --z1z2 45,70
TBD

Fig. 14 Slice width value for each detector column along NAXIS1. Note the clear difference between the even and odd slices, corresponding to the two ID families: 1 to 15 (even slices, bottom to top) and 16 to 30 (odd slices, top to bottom). Columns shown in white correspond to NaNs.

In this particular case, although slice ID 15 falls partially outside the useful region of the detector, the script fridadrp-find_slice_boundary_borders_from_flat has nonetheless been able to detect both boundaries of that slice. Since there can be cases (see below) in which the entire lower boundary of the first slice (ID 30) or the entire upper boundary of the last slice (ID 15) fall outside the detector, it is useful to run the boundary borders detection script again while avoiding attempts to fit non-existent boundaries.

Avoiding missing borders

The script fridadrp-find_slice_boundary_borders_from_flat allows the use of the following arguments:

  • --slice-ini: default 1, numbering from 1 (ID 30, at the bottom of the detector) to 30 (ID 15, at the top).

  • --slice-end: default 30 (ID 15, at the top of the detector), following the same numbering.

  • --row-ini: lower row number (1-based index) along NAXIS2. The default is 5 (first valid pixel after skipping the 4 initial border pixels). Rows below --row-ini are set equal to zero in the image when searching for slice boundaries.

  • --row-end: upper row number (1-based index) along NAXIS2. The default is 2044 (last valid pixel to avoid the 4 final border pixels). Rows above --row-end are set equal to zero in the image when searching for slice boundaries.

The idea is to use --slice-ini 2 to avoid fitting the first slice (ID 30, at the bottom) or --slice-end 29 to avoid fitting the last slice (ID 15, at the top).

If we use --slice-ini 2, we must also use an appropriate value for --row-ini, so that we can ignore the first portion of the image at the bottom that partially contains slice ID 30. Similarly, if we use --slice-end 29, we must use an appropriate value of --row-end to avoid using the upper part of the image where slice ID 15 may appear only partially.

In principle this should also work with --slice-ini greater than 2 and --slice-end less than 29. We could even compute the boundaries of a single slice by making --slice-ini and --slice-end equal. In these cases, appropriate values of --row-ini and --row-end would need to be used.

In the case of the image 0000000096-20260505-FRIDA-FridaSuccess-raw.fits, the top slice, number 30 (slice ID 15), has its upper boundary truncated (it extends beyond row 2044, the last valid row on the detector along NAXIS2). In this case we can fit slices 1 to 29, without using the last one.

(venv_frida) $ fridadrp-find_slice_boundary_borders_from_flat \
  --flatfile 0000000096-20260505-FRIDA-FridaSuccess-raw.fits \
  --slice-end 29 \
  --row-end 1986 \
  --overwrite
────────── Welcome to fridadrp-find_slice_boundary_borders_from_flat ───────────
Using fridadrp.tools.find_slice_boundary_borders_from_flat version              
0.1.dev248+g830b3980e.d20260804                                                 
Column ranges not specified. Using default range: 5 to 2044.                    
Columns to analyze: from 5 to 2044 (total 2040 columns) - Mode: continuous      
Working on columns...                                                           
0%                                                                              
0% 10%_____________________________________ (elap=0:05|left=0:49|exp=0:54)      
0% 10% 20%_________________________________ (elap=0:11|left=0:43|exp=0:54)      
0% 10% 20% 30%_____________________________ (elap=0:16|left=0:38|exp=0:55)      
0% 10% 20% 30% 40%_________________________ (elap=0:22|left=0:33|exp=0:55)      
0% 10% 20% 30% 40% 50%_____________________ (elap=0:28|left=0:28|exp=0:55)      
0% 10% 20% 30% 40% 50% 60%_________________ (elap=0:33|left=0:22|exp=0:55)      
0% 10% 20% 30% 40% 50% 60% 70%_____________ (elap=0:39|left=0:17|exp=0:56)      
0% 10% 20% 30% 40% 50% 60% 70% 80%_________ (elap=0:45|left=0:11|exp=0:56)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90%_____ (elap=0:50|left=0:06|exp=0:56)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% (elap=0:56|left=0:00|exp=0:56)      
Number of useful columns processed: 1988 out of 2040                            
Slice boundary borders saved to: slice_boundary_borders_from_flat_1-29.fits     
Total time elapsed: 0:00:57.355541                                              
──────────────────────────────────  Goodbye!  ──────────────────────────────────

In this case the generated output file is appropriately named slice_boundary_borders_from_flat_1-29.fits by default.

We visualize the new result.

(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --borders slice_boundary_borders_from_flat_1-29.fits \
  --image 0000000096-20260505-FRIDA-FridaSuccess-raw.fits \
  --sliceid
Full image Full image
Zoomed region Zoomed region

Fig. 15 We can see that in this case the last slice (ID 15) has not been fitted.

Polynomial fit of the slice borders

The next step consists of performing a polynomial fit to the boundaries detected in the previous procedure. This task is carried out with the help of the script fridadrp-fit_slice_boundary_borders_with_polynomials.

(venv_numina) $ fridadrp-fit_slice_boundary_borders_with_polynomials --help
Usage: fridadrp-fit_slice_boundary_borders_with_polynomials
       [-h] --input INPUT [--colrange MIN MAX] --deg DEG [--output OUTPUT]
       [--overwrite] [--plots] [--output-dir OUTPUT_DIR] [--record] [--echo]
       [--version] [--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]

Fit the slice boundaries determined from flat image

Options:
  -h, --help            show this help message and exit
  --input INPUT         Path to the FITS file with border data
  --colrange MIN MAX    Column range to fit (1-based index). This option can
                        be specified multiple times
  --deg DEG             Degree of the polynomial to fit
  --output OUTPUT       Output FITS file name
  --overwrite           Overwrite output file if it exists
  --plots               Display plots
  --output-dir OUTPUT_DIR
                        Output directory (default: .)
  --record              Record terminal output
  --echo                Display full command line
  --version             Display version
  --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        Set the logging level
(venv_frida) $ fridadrp-fit_slice_boundary_borders_with_polynomials \
  --input slice_boundary_borders_from_flat_1-30.fits \
  --output slice_boundary_polynomials_1-30.fits \
  --deg 3 \
  --overwrite

We start by fitting all 30 slices, even though we already know that slice ID 15 is incorrect.

─────── Welcome to fridadrp-fit_slice_boundary_borders_with_polynomials ────────
Using fridadrp.tools.fit_slice_boundary_borders_with_polynomials version        
0.1.dev248+g830b3980e.d20260804                                                 
Column ranges not specified. Using default range: 5 to 2044.                    
Columns to analyze: from 5 to 2044 (total 2040 columns) - Mode: continuous      
Reading slice boundary borders from                                             
slice_boundary_borders_from_flat_1-30.fits                                      
Number of slices defined: SLCNUMT=30                                            
Number of NaN values in collapsed left border : 60                              
Number of NaN values in collapsed right border: 60                              
Number of initial valid columns to fit each boundary: 1988                      
Number of valid columns after applying columns_to_analyze: 1988                 
Polynomial degree: 3                                                            
Fitting slice boundaries with polynomials...                                    
0%                                                                              
0% 10%_____________________________________ (elap=0:01|left=0:07|exp=0:08)      
0% 10% 20%_________________________________ (elap=0:02|left=0:10|exp=0:12)      
0% 10% 20% 30%_____________________________ (elap=0:03|left=0:08|exp=0:12)      
0% 10% 20% 30% 40%_________________________ (elap=0:05|left=0:07|exp=0:12)      
0% 10% 20% 30% 40% 50%_____________________ (elap=0:07|left=0:07|exp=0:13)      
0% 10% 20% 30% 40% 50% 60%_________________ (elap=0:07|left=0:05|exp=0:12)      
0% 10% 20% 30% 40% 50% 60% 70%_____________ (elap=0:08|left=0:03|exp=0:11)      
0% 10% 20% 30% 40% 50% 60% 70% 80%_________ (elap=0:08|left=0:02|exp=0:10)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90%_____ (elap=0:08|left=0:01|exp=0:09)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% (elap=0:09|left=0:00|exp=0:09)      
coefs_left shape....: (30, 4)                                                   
coefs_right shape...: (30, 4)                                                   
Slice boundary polynomials saved to: slice_boundary_polynomials_1-30.fits       
Total time elapsed: 0:00:08.778110                                              
──────────────────────────────────  Goodbye!  ──────────────────────────────────

The previous script saves the results in a FITS file whose name in this example is slice_boundary_polynomials_1-30.fits (we have preserved the suffix 1-30 before the extension, to follow the same convention used to name the FITS file employed as input).

The output file contains 3 extensions:

(venv_frida) $ fitsinfo slice_boundary_polynomials_1-30.fits
Filename: slice_boundary_polynomials_1-30.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU      59   ()      
  1  L-BORDER      1 ImageHDU         8   (4, 30)   float64   
  2  R-BORDER      1 ImageHDU         8   (4, 30)   float64   
  3  SLIWIDTH      1 ImageHDU         8   (2048, 30)   float64 

The L-BORDER and R-BORDER extensions contain the coefficients of the polynomials fitted to each boundary (NAXIS1=4 coefficients, NAXIS2=30 slices). The extension SLIWIDTH contains the width of the slices as a function of the column number along NAXIS1 (for the NAXIS2=30 slices). It is interesting to compare the image stored in this last SLIWIDTH extension with the one obtained earlier using the values of the points detected as boundaries.

Full image Full image
Zoomed region Zoomed region

Fig. 16 Comparison of the slice widths stored in the SLIWIDTH extension of the files slice_boundary_borders_from_flat_1-30.fits (left panel) and slice_boundary_polynomials_1-30.fits (right panel). The polynomial fit generates a smooth variation with column number along NAXIS1.

We can check the polynomial fits graphically by overlaying the points previously determined as slice boundaries (which are the points used in the polynomial fits) together with the resulting polynomials.

(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --borders slice_boundary_borders_from_flat_1-30.fits \
  --poly slice_boundary_polynomials_1-30.fits \
  --image 0000000096-20260505-FRIDA-FridaSuccess-raw.fits \
  --sliceid
TBD

Fig. 17 Full image with the detected boundaries and the fitted polynomials.

We can zoom in to better appreciate what happens with different slices. It is worth paying attention to what happens at the bottom and top of the detector.

Lower border Lower border
Upper border Upper border

Fig. 18 Zoomed view of what happens in the first (left panel) and last (right panel) slices.

As we already saw earlier when determining the slice boundaries, the upper boundary of slice ID 15 extends beyond the useful region of the detector.

Lower border Lower border
Upper border Upper border

Fig. 19 Detail of the last slice (ID 15), using different cuts. It can be seen how the signal of this slice is truncated at the top because part of the slice falls outside the useful region of the detector. The polynomial fit of the upper boundary of that slice does not perform well, because a large fraction of the points have underestimated positions along the NAXIS2 axis.

We will now repeat the work carried out with the script fridadrp-fit_slice_boundary_borders_with_polynomials, using as input the file slice_boundary_borders_from_flat_1-29.fits (which does not contain boundary values for slice ID 15).

(venv_frida) $ fridadrp-fit_slice_boundary_borders_with_polynomials \
  --input slice_boundary_borders_from_flat_1-29.fits \
  --output slice_boundary_polynomials_1-29.fits \
  --deg 3 \
  --overwrite
─────── Welcome to fridadrp-fit_slice_boundary_borders_with_polynomials ────────
Using fridadrp.tools.fit_slice_boundary_borders_with_polynomials version        
0.1.dev248+g830b3980e.d20260804                                                 
Column ranges not specified. Using default range: 5 to 2044.                    
Columns to analyze: from 5 to 2044 (total 2040 columns) - Mode: continuous      
Reading slice boundary borders from                                             
slice_boundary_borders_from_flat_1-29.fits                                      
Number of slices defined: SLCNUMT=29                                            
Number of NaN values in collapsed left border : 60                              
Number of NaN values in collapsed right border: 60                              
Number of initial valid columns to fit each boundary: 1988                      
Number of valid columns after applying columns_to_analyze: 1988                 
Polynomial degree: 3                                                            
Fitting slice boundaries with polynomials...                                    
0%                                                                              
0% 10%_____________________________________ (elap=0:01|left=0:07|exp=0:08)      
0% 10% 20%_________________________________ (elap=0:02|left=0:10|exp=0:12)      
0% 10% 20% 30%_____________________________ (elap=0:04|left=0:08|exp=0:12)      
0% 10% 20% 30% 40%_________________________ (elap=0:05|left=0:07|exp=0:12)      
0% 10% 20% 30% 40% 50%_____________________ (elap=0:07|left=0:07|exp=0:13)      
0% 10% 20% 30% 40% 50% 60%_________________ (elap=0:07|left=0:05|exp=0:12)      
0% 10% 20% 30% 40% 50% 60% 70%_____________ (elap=0:08|left=0:03|exp=0:11)      
0% 10% 20% 30% 40% 50% 60% 70% 80%_________ (elap=0:08|left=0:02|exp=0:10)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90%_____ (elap=0:08|left=0:01|exp=0:09)      
Skipped slices (id): #15                                                        
coefs_left shape....: (30, 4)                                                   
coefs_right shape...: (30, 4)                                                   
Slice boundary polynomials saved to: slice_boundary_polynomials_1-29.fits       
Total time elapsed: 0:00:08.551443                                              
──────────────────────────────────  Goodbye!  ──────────────────────────────────
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --borders slice_boundary_borders_from_flat_1-29.fits \
  --poly slice_boundary_polynomials_1-29.fits \
  --image 0000000096-20260505-FRIDA-FridaSuccess-raw.fits \
  --sliceid
TBD

Fig. 20 Full image with the detected boundaries and the polynomial fits, excluding slice ID 15.

Predicting missing polynomial borders

As we have seen above, there appears to be a relatively continuous variation in slice width when we consider them grouped into two families: IDs 1 to 15 on one hand and IDs 16 to 30 on the other. The script fridadrp-predict_polynomial_slice_borders makes use of this fact to try to predict the polynomial boundaries of slices that may be partially outside the detector. We have seen that this happens with several calibration images when changing grating, and it can result in the boundaries of the bottom slice (ID 30) or the top slice (ID 15) not being determinable.

(venv_numina) $ fridadrp-predict_polynomial_slice_borders --help
Usage: fridadrp-predict_polynomial_slice_borders [-h] --poly POLY
                                                 --slicenum SLICE_INDEX
                                                 [--degslice DEGSLICE]
                                                 --output OUTPUT [--overwrite]
                                                 [--force] [--plots]
                                                 [--output-dir OUTPUT_DIR]
                                                 [--record] [--echo]
                                                 [--version]
                                                 [--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]

Predict slice boundary polynomials

Options:
  -h, --help            show this help message and exit
  --poly POLY           Path to the input file with the boundary polynomials
  --slicenum SLICE_INDEX
                        Slice number to be predicted (1 to 30, where 1 is the
                        bottom slice and 30 is the top slice)
  --degslice DEGSLICE   Degree of the polynomial to be fitted (slice border
                        vs. slice index; default 2)
  --output OUTPUT       Output file name for the predicted polynomials
  --overwrite           Overwrite existing output file
  --force               Force recomputation of existing polynomials in chosen
                        slice
  --plots               Display plots of the polynomial fitting
  --output-dir OUTPUT_DIR
                        Output directory (default: .)
  --record              Record terminal output
  --echo                Display full command line
  --version             Display version
  --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        Set the logging level

In the example at hand, we are going to predict the polynomial boundaries of slice ID 15 (number 30 counting from bottom to top on the detector).

(venv_frida) $ fridadrp-predict_polynomial_slice_borders \
  --poly slice_boundary_polynomials_1-29.fits \
  --output slice_boundary_polynomials_1-30_fixed.fits \
  --slicenum 30 \
  --overwrite \
  --plots
───────────── Welcome to fridadrp-predict_polynomial_slice_borders ─────────────
Using fridadrp.tools.predict_polynomial_slice_borders version                   
0.1.dev248+g830b3980e.d20260804                                                 
Reading slice boundary polynomials from                                         
slice_boundary_polynomials_1-29.fits                                            
Number of slices defined: SLCNUMT=29                                            
Reading 30 slices with polynomial degree 3 (left borders).                      
Reading 30 slices with polynomial degree 3 (right borders).                     
Predicting polynomial borders for slice number 30 (slice ID 15)                 
Slice ID 15 belongs to the group of slices with ID:                             
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].                            
Using the polynomials of the following slice IDs to predict the new borders:    
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]                                 
Fitting along NAXIS1...                                                         
0%                                                                              
0% 10%_____________________________________ (elap=0:08|left=1:15|exp=1:23)      
0% 10% 20%_________________________________ (elap=0:08|left=0:33|exp=0:42)      
0% 10% 20% 30%_____________________________ (elap=0:08|left=0:20|exp=0:28)      
0% 10% 20% 30% 40%_________________________ (elap=0:08|left=0:13|exp=0:21)      
0% 10% 20% 30% 40% 50%_____________________ (elap=0:09|left=0:09|exp=0:17)      
0% 10% 20% 30% 40% 50% 60%_________________ (elap=0:19|left=0:12|exp=0:31)      
0% 10% 20% 30% 40% 50% 60% 70%_____________ (elap=0:19|left=0:08|exp=0:27)      
0% 10% 20% 30% 40% 50% 60% 70% 80%_________ (elap=0:19|left=0:05|exp=0:23)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90%_____ (elap=0:19|left=0:02|exp=0:21)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% (elap=0:20|left=0:00|exp=0:20)      
Copied input polynomials file slice_boundary_polynomials_1-29.fits to output    
file find_borders7/slice_boundary_polynomials_1-30_fixed.fits.                  
Updating the output FITS file with the predicted polynomials for slice number 30
(slice ID 15).                                                                  
Total time elapsed: 0:00:23.972502                                              
──────────────────────────────────  Goodbye!  ──────────────────────────────────

The script determines that the slice to be predicted (ID 15) belongs to the family of slices with ID between 1 and 15. For each column along NAXIS1, the code performs a polynomial fit of degree --degslice 2 (default) to the position of each boundary as a function of slice number. This polynomial then allows the boundary position to be predicted for the slice indicated in --slicenum.

Since we used the --plots argument, several plots are shown during the execution of this last script. First, the positions of the right and left boundaries (along the NAXIS2 axis) of the slices used in the fit are shown, for columns 1, 1025, and 2048 (along the NAXIS1 axis).

Left border polynomial Left border polynomial
Right border polynomial Right border polynomial

Fig. 21 Location of the polynomial boundaries for the slices used in the fit, for column 1 along the NAXIS1 axis.

Left border polynomial Left border polynomial
Right border polynomial Right border polynomial

Fig. 22 Location of the polynomial boundaries for the slices used in the fit, for column 1025 along the NAXIS1 axis.

Left border polynomial Left border polynomial
Right border polynomial Right border polynomial

Fig. 23 Location of the polynomial boundaries for the slices used in the fit, for column 2048 along the NAXIS1 axis.

Once the fits have been performed for all detector columns along NAXIS1, the resulting polynomials are used to predict the expected location of the polynomial boundaries of the target slice at each column. These positions are then fitted with a polynomial of the same degree as the one used for the polynomial fits already stored in the input file.

Left border polynomial Left border polynomial
Right border polynomial Right border polynomial

Fig. 24 Predicted left and right border polynomials for slice ID 15.

The result is stored in the output file specified by the --output argument. In this case the output file is called slice_boundary_polynomials_1-30_fixed.fits and has the same format as the input file slice_boundary_polynomials_1-29_fixed.fits.

We can visualize the new result graphically.

(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --borders slice_boundary_borders_from_flat_1-30.fits \
  --poly slice_boundary_polynomials_1-30_fixed.fits \
  --image 0000000096-20260505-FRIDA-FridaSuccess-raw.fits \
  --sliceid \
  --ylim 1900 2060
TBD

Fig. 25 Graphical comparison between the boundaries (white points) obtained by the script fridadrp-find_slice_boundary_borders_from_flat and the prediction of the polynomial boundaries computed with the script fridadrp-predict_polynomial_slice_borders for slice ID 15. Note that the right boundary (upper in the image) of that slice clearly falls outside the useful region of the detector.

Polynomial fit of spectral traces

Initial example

Once we have obtained the boundaries of each slice and fitted them with polynomials, we can move on to the next phase, which consists of obtaining polynomial fits to the spectral traces. In this case we will use the script fridadrp-find_traces_within_slice_boundary_polynomials.

(venv_numina) $ fridadrp-find_traces_within_slice_boundary_polynomials --help
Usage: fridadrp-find_traces_within_slice_boundary_polynomials
       [-h] --image IMAGE --poly POLY [--voffset VOFFSET [VOFFSET ...]]
       --ntraces NTRACES [--nextend NEXTEND]
       [--nclean-around-peaks NCLEAN_AROUND_PEAKS] --deg DEG
       [--colrange MIN MAX] [--ymedian YMEDIAN] [--xmedian XMEDIAN]
       [--theilsen] [--degslice DEGSLICE] [--degrefine DEGREFINE] [--norefine]
       --output OUTPUT [--overwrite] [--plotsliceid PLOTSLICEID]
       [--pdf-out PDF_OUT] [--output-dir OUTPUT_DIR] [--record] [--echo]
       [--version] [--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]

Find traces within slice boundary polynomials

Options:
  -h, --help            show this help message and exit
  --image IMAGE         Path to the input image file (FITS format)
  --poly POLY           Path to the input file with the boundary polynomials
  --voffset VOFFSET [VOFFSET ...]
                        Vertical offset to apply to the slice boundaries
                        (default: 0.0 pixels; + upward, - downward). If more
                        than one value is provided, a polynomial of degree
                        len(voffset)-1 will be fitted to the offsets and
                        applied to the slice boundaries.
  --ntraces NTRACES     Number of traces per slice to find
  --nextend NEXTEND     Number of pixels to extend the slice boundaries for
                        trace finding (default: 2)
  --nclean-around-peaks NCLEAN_AROUND_PEAKS
                        Number of pixels to clean at both sides of each peak
                        (default: -1; set to zero pixels moving outward from
                        the peak while the signal keeps decreasing)
  --deg DEG             Degree of the polynomial to fit each trace
  --colrange MIN MAX    Column range to analyze (1-based index) along NAXIS1.
                        This option can be specified multiple times
  --ymedian YMEDIAN     Size of the median filter along NAXIS2 axis to be
                        subtracted (odd; default: 0 -> no filtering)
  --xmedian XMEDIAN     Size of the median filter along NAXIS1 axis (odd;
                        default: 21)
  --theilsen            Use initial Theil-Sen regression to reject outliers in
                        the polynomial fitting
  --degslice DEGSLICE   Degree of the polynomial to fit traces across slices
                        (default: 2)
  --degrefine DEGREFINE
                        Degree of the polynomial to refine the trace positions
                        (default: 2)
  --norefine            Do not refine the extrapolated traces
  --output OUTPUT       Output file name for the predicted polynomials
  --overwrite           Overwrite existing output file
  --plotsliceid PLOTSLICEID
                        Display plots for slice id (this option can be
                        specified multiple times)
  --pdf-out PDF_OUT     Output PDF file for final plots with traces for every
                        slice (default: None, no output)
  --output-dir OUTPUT_DIR
                        Output directory (default: .)
  --record              Record terminal output
  --echo                Display full command line
  --version             Display version
  --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        Set the logging level

To illustrate the procedure, as input image we will use 0000000132-20260506-FRIDA-FridaSuccess-raw.fits, which shows 5 spectral traces in each slice.

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image 0000000132-20260506-FRIDA-FridaSuccess-raw.fits \
  --poly slice_boundary_polynomials_1-30_fixed.fits \
  --voffset 0.0 \
  --ntraces 5 \
  --deg 3 \
  --output traces_within_slice_polynomials.fits \
  --pdf-out traces_within_slice_polynomials.pdf \
  --plotsliceid 15 \
  --overwrite
────── Welcome to fridadrp-find_traces_within_slice_boundary_polynomials ───────
Using fridadrp.tools.find_traces_within_slice_boundary_polynomials version      
0.1.dev248+g830b3980e.d20260804                                                 
Column ranges not specified. Using default range: 5 to 2044.                    
Columns to analyze: from 5 to 2044 (total 2040 columns) - Mode: continuous      
No median filtering along NAXIS2 will be applied (ymedian=0).                   
Applying median filtering along NAXIS1 with size 21.                            
Reading slice boundary polynomials from                                         
slice_boundary_polynomials_1-30_fixed.fits                                      
Number of slices defined: SLCNUMT=30                                            
Reading 30 slices with polynomial degree 3 (left borders).                      
Reading 30 slices with polynomial degree 3 (right borders).                     
Applying constant vertical offset of 0.0 pixels to slice boundary polynomials.  
Press 'q' to close the plot or 'x' to close the plot and exit the program.      

The first thing the script does is show us the image with the spectral traces, overlaying on it the location of the polynomial boundaries. It is possible to shift these boundaries in the vertical direction using --voffset. If this argument takes a single value, the offset is assumed to be constant in the vertical direction (positive: shifts the boundaries upward; negative: shifts the boundaries downward). If more than one number is provided, the shift is polynomial: the polynomial is computed using as its degree the number of points minus one, so that the polynomial is exact. In this latter case, the numbers entered are used as vertical offsets distributed uniformly along the NAXIS1 axis. In this example we are simply using --voffset 0.0.

It will be important to zoom in on the displayed image to better assess whether the polynomial boundaries are correct.

Full image Full image
Zoomed region Zoomed region

Fig. 26 Full image (left panel) and zoom of the first slices (right panel), with the polynomial boundaries overlaid.

Finding and fitting traces within slice boundary polynomials...                 
0%                                                                              
0% 10%_____________________________________ (elap=0:06|left=0:52|exp=0:58)      
0% 10% 20%_________________________________ (elap=0:11|left=0:45|exp=0:56)      
0% 10% 20% 30%_____________________________ (elap=0:18|left=0:41|exp=0:59)      
0% 10% 20% 30% 40%_________________________ (elap=0:24|left=0:36|exp=0:59)      
0% 10% 20% 30% 40% 50%_____________________ (elap=0:30|left=0:30|exp=0:59)      
0% 10% 20% 30% 40% 50% 60%_________________ (elap=0:35|left=0:24|exp=0:59)      
0% 10% 20% 30% 40% 50% 60% 70%_____________ (elap=0:42|left=0:18|exp=1:00)      
0% 10% 20% 30% 40% 50% 60% 70% 80%_________ (elap=0:48|left=0:12|exp=1:00)      
0% 10% 20% 30% 40% 50% 60% 70% 80% 90%_____ (elap=0:54|left=0:06|exp=1:01)      
Slice #30 (ID 15) right boundary is out of the useful pixel range.              
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% (elap=0:58|left=0:00|exp=0:58)      
Skipped slice: #30 (ID 15)                                                      
Extrapolating traces for the last slice (ID 15)                                 
Slice ID 15 belongs to the group of slices with ID:                             
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].                            
Fitting traces for the following slices (ID):                                   
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]                                 
Fitting trace 1/5 of slice ID 15 along NAXIS1...                                

Since we used --plotsliceid 15, the script will show us this problematic slice in detail when it is reached (recall that it is partially outside the useful region of the detector). As we saw earlier for the case of the boundaries, it is also possible to predict the expected position of each of the 5 traces by making use of the fits to the same trace in other slices of the same family. In this case the code tells us in the terminal that slice ID 15 belongs to the same family as the slices with ID between 1 and 15.

For each of the 5 traces, the polynomial fit is shown at 3 detector columns (5, 1025, and 2044), as well as the resulting polynomial fit of the trace as a function of column number (note that the fit is performed over all the columns specified in --colrange).

Column 5 Column 5
Colum 1025 Colum 1025
Colum 2044 Colum 2044
Trace fit Trace fit

Fig. 27 Polynomial fits of the position of trace 1 as a function of slice number for columns 5, 1025, and 2044 (first three panels), and final polynomial fit of the trace as a function of column number along NAXIS1 (right panel). Although in the last plot it may seem that many points are being rejected in the fit (shown in red), note that the scale on the vertical axis indicates that we are fitting a small fraction of a pixel.

Fitting trace 2/5 of slice ID 15 along NAXIS1...                                
Column 5 Column 5
Colum 1025 Colum 1025
Colum 2044 Colum 2044
Trace fit Trace fit

Fig. 28 Same as the previous figure, for the case of trace number 2.

Fitting trace 3/5 of slice ID 15 along NAXIS1...                                
Column 5 Column 5
Colum 1025 Colum 1025
Colum 2044 Colum 2044
Trace fit Trace fit

Fig. 29 Same as the previous figures, for the case of trace number 3.

Fitting trace 4/5 of slice ID 15 along NAXIS1...                                
Column 5 Column 5
Colum 1025 Colum 1025
Colum 2044 Colum 2044
Trace fit Trace fit

Fig. 30 Same as the previous figures, for the case of trace number 4.

Fitting trace 5/5 of slice ID 15 along NAXIS1...                                
Column 5 Column 5
Colum 1025 Colum 1025
Colum 2044 Colum 2044
Trace fit Trace fit

Fig. 31 Same as the previous figures, for the case of trace number 5.

Refining the fitted traces for slice ID 15 using the smoothed image data...     
Refining trace 1/5 of slice ID 15...                                            

Once the expected positions for the 5 traces have been estimated, the code attempts to refine the fit using the information in the image provided via --image, provided that the entire trace lies within the useful region of the detector. In this case this occurs for the first 4 traces (out of the total of 5).

TBD

Fig. 32 Difference between the predicted position of trace number 1 and the fit to the signal in the input image. The correction is modeled by means of a polynomial fit.

Refining trace 2/5 of slice ID 15...                                            
TBD

Fig. 33 Same as the previous figure, for the case of trace number 2.

Refining trace 3/5 of slice ID 15...                                            
TBD

Fig. 34 Same as the previous figures, for the case of trace number 3.

Refining trace 4/5 of slice ID 15...                                            
TBD

Fig. 35 Same as the previous figures, for the case of trace number 4.

Trace 5/5 of slice ID 15 is out of bounds. Skipping refinement.                 

Since trace number 5 falls outside the useful region of the detector, it is not possible to correct its position using the information in the input image.

Applying average refinement to Trace 5/5 of slice ID 15.                        

Instead, an average of the corrections made to the other four traces is used.

TBD

Fig. 36 Average correction computed using the corrections deduced for the 4 traces within the useful region of the detector.

Once all traces have been computed for the problematic slice, the program displays the result graphically.

TBD

Fig. 37 Graphical comparison of the extrapolated traces for slice ID 15. The magenta (dashed line) shows the direct extrapolation using the information from the same traces in the slices of the same family. The cyan (solid line) represents the refined fit using the information from the input image.

Saving final plots of traces for every slice in PDF file:                       
traces_within_slice_polynomials.pdf                                             
Traces within slice boundary polynomials saved to                               
traces_within_slice_polynomials.fits                                            
Total time elapsed: 0:01:31.152357                                              
──────────────────────────────────  Goodbye!  ──────────────────────────────────

Two output files are generated:

  • traces_within_slice_polynomials.fits: FITS file containing the polynomial boundaries and the polynomial traces. If --voffset was different from zero, the saved polynomial boundaries take this shift into account, so they will not be identical to those in the file specified via --poly.

  • traces_within_slice_polynomials.pdf: PDF file showing, for each slice, the polynomial boundaries and the polynomial traces, overlaid on the image specified via --image.

The information in the file traces_within_slice_polynomials.fits is distributed across several extensions:

(venv_fridat) $ fitsinfo traces_within_slice_polynomials.fits
Filename: traces_within_slice_polynomials.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU      74   ()      
  1  L-BORDER      1 ImageHDU         8   (4, 30)   float64   
  2  R-BORDER      1 ImageHDU         8   (4, 30)   float64   
  3  SLIWIDTH      1 ImageHDU         8   (2048, 30)   float64   
  4  SLCNUM01      1 ImageHDU        11   (4, 5)   float64   
  5  SLCNUM02      1 ImageHDU        11   (4, 5)   float64   
  6  SLCNUM03      1 ImageHDU        11   (4, 5)   float64   
  7  SLCNUM04      1 ImageHDU        11   (4, 5)   float64   
  8  SLCNUM05      1 ImageHDU        11   (4, 5)   float64   
  9  SLCNUM06      1 ImageHDU        11   (4, 5)   float64   
 10  SLCNUM07      1 ImageHDU        11   (4, 5)   float64   
 11  SLCNUM08      1 ImageHDU        11   (4, 5)   float64   
 12  SLCNUM09      1 ImageHDU        11   (4, 5)   float64   
 13  SLCNUM10      1 ImageHDU        11   (4, 5)   float64   
 14  SLCNUM11      1 ImageHDU        11   (4, 5)   float64   
 15  SLCNUM12      1 ImageHDU        11   (4, 5)   float64   
 16  SLCNUM13      1 ImageHDU        11   (4, 5)   float64   
 17  SLCNUM14      1 ImageHDU        11   (4, 5)   float64   
 18  SLCNUM15      1 ImageHDU        11   (4, 5)   float64   
 19  SLCNUM16      1 ImageHDU        11   (4, 5)   float64   
 20  SLCNUM17      1 ImageHDU        11   (4, 5)   float64   
 21  SLCNUM18      1 ImageHDU        11   (4, 5)   float64   
 22  SLCNUM19      1 ImageHDU        11   (4, 5)   float64   
 23  SLCNUM20      1 ImageHDU        11   (4, 5)   float64   
 24  SLCNUM21      1 ImageHDU        11   (4, 5)   float64   
 25  SLCNUM22      1 ImageHDU        11   (4, 5)   float64   
 26  SLCNUM23      1 ImageHDU        11   (4, 5)   float64   
 27  SLCNUM24      1 ImageHDU        11   (4, 5)   float64   
 28  SLCNUM25      1 ImageHDU        11   (4, 5)   float64   
 29  SLCNUM26      1 ImageHDU        11   (4, 5)   float64   
 30  SLCNUM27      1 ImageHDU        11   (4, 5)   float64   
 31  SLCNUM28      1 ImageHDU        11   (4, 5)   float64   
 32  SLCNUM29      1 ImageHDU        11   (4, 5)   float64   
 33  SLCNUM30      1 ImageHDU        11   (4, 5)   float64

The first extensions, L-BORDER, R-BORDER, and SLIWIDTH, are analogous to those explained above for the file slice_boundary_borders_from_flat_1-30.fits. There are 30 additional extensions, SLCNUM??, one per slice, containing the arrays that store the polynomial fit coefficients of the traces, where in this case NAXIS1=4 is the number of coefficients (degree-3 polynomial) and NAXIS2=5 is the number of traces.

It is possible to use the script fridadrp-overplot_slice_boundary_polynomials again to display the result graphically. On this occasion, instead of using the --poly argument, --traces must be used (note that --poly is not necessary because the file containing the traces also contains the polynomials for the slice boundaries).

(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
--traces traces_within_slice_polynomials.fits \
--image 0000000132-20260506-FRIDA-FridaSuccess-raw.fits \
--sliceid
TBD

Fig. 38 Display of the image specified via --image, with the polynomial boundaries and traces stored in the file indicated via --traces. It is possible to zoom in and navigate the image to check the quality of the fits performed.

First slices First slices
Last slices Last slices

Fig. 39 Zoomed-in view of the previous image, showing the first slices (left panel) and the last slices (right panel) in more detail.

The script fridadrp-overplot_slice_boundary_polynomials also has a --pdf-mosaic argument that allows generating a PDF file with the polynomial boundaries and traces plotted on the selected image, showing one slice per page of the file.

(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials.fits \
  --image 0000000132-20260506-FRIDA-FridaSuccess-raw.fits \
  --traceid \
  --pdf-mosaic traces_within_slice_polynomials_final.pdf
─────────── Welcome to fridadrp-overplot_slice_boundary_polynomials ────────────
Using fridadrp.tools.overplot_slice_boundary_polynomials version                
0.1.dev248+g830b3980e.d20260804                                                 
Reading slice boundary polynomials from                                         
traces_within_slice_polynomials.fits                                            
Number of slices defined: SLCNUMT=30                                            
Reading 30 slices with polynomial degree 3 (left borders).                      
Reading 30 slices with polynomial degree 3 (right borders).                     
Read 30 slices with 5 traces per slice from                                     
traces_within_slice_polynomials.fits.                                           
Saving final plots of traces for every slice in PDF file:                       
traces_within_slice_polynomials_final.pdf                                       
Total time elapsed: 0:00:04.190234                                              
──────────────────────────────────  Goodbye!  ──────────────────────────────────

In this case the PDF file containing all the traces is called traces_within_slice_polynomials_final.pdf.

We can also generate, from the PDF file just created, a mosaic (either in PDF or PNG format) containing all 30 plots in the same figure. For this, it is useful to use ImageMagick’s montage command. For example:

(venv_frida) $ montage \
  -density 300 \
  traces_within_slice_polynomials_final.pdf \
  -tile 6x5 \
  -geometry 600x375+5+5 \
  -background white \
  traces_within_slice_polynomials_final_montage.pdf

An even more convenient way is to use --montage png or --montage pdf as an argument of fridadrp-overplot_slice_boundary_polynomials.

TBD

Fig. 40 Image resulting from using --montage png when running fridadrp-overplot_slice_boundary_polynomials in the last command shown above.

Warning

The work that follows has been carried out using only the polynomial boundaries obtained above. It appears that these boundaries, obtained for a single case, seem to work well even when changing grating.

In the following sections, the fit to the spectral traces for the initial calibration sequence, available in the directory Configuraciones-calibracion_de_distorsion-IFS, is shown.

Grating zJ Low, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config13/0000007804-20260512-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset 6 \
  --colrange 480 1660 \
  --ntraces 6 \
  --deg 2 \
  --theilsen \
  --output traces_within_slice_polynomials_007804.fits \
  --pdf-out traces_within_slice_polynomials_007804.pdf \
  --plotsliceid 15 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_007804.fits \
  --image config13/0000007804-20260512-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_007804_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_007804_fixed_montage.png

Grating HK Low, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config14/0000007810-20260512-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset 6 \
  --colrange 335 1215 --colrange 1775 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_007810.fits \
  --pdf-out traces_within_slice_polynomials_007810.pdf \
  --plotsliceid 15 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_007810.fits \
  --image config14/0000007810-20260512-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_007810_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_007810_fixed_montage.png

Grating z Medium, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config02/0000000150-20260510-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset -27 \
  --colrange 400 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_000150.fits \
  --pdf-out traces_within_slice_polynomials_000150.pdf \
  --plotsliceid 30 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000150.fits \
  --image config02/0000000150-20260510-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000510_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000510_fixed_montage.png

Grating J Medium, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config01/0000000147-20260510-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset 9 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_000147.fits \
  --pdf-out traces_within_slice_polynomials_000147.pdf \
  --plotsliceid 15 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000147.fits \
  --image config01/0000000147-20260510-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000147_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000147_fixed_montage.png

Grating H Medium, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config03/0000000153-20260511-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset 4 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_000153.fits \
  --pdf-out traces_within_slice_polynomials_000153.pdf \
  --plotsliceid 15 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000153.fits \
  --image config03/0000000153-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000153_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000153_fixed_montage.png

Grating K Medium, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config04/0000000156-20260511-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset 0 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_000156.fits \
  --pdf-out traces_within_slice_polynomials_000156.pdf \
  --plotsliceid 15 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000156.fits \
  --image config04/0000000156-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000156_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000156_fixed_montage.png

Grating H High1, Medium Camera

In this case we need to use --voffset 3 22 to apply a different vertical shift on the left side than on the right side.

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config09/0000000171-20260511-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset 3 22 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_000171.fits \
  --pdf-out traces_within_slice_polynomials_000171.pdf \
  --plotsliceid 15 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000171.fits \
  --image config09/0000000171-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000171_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000171_fixed_montage.png

In this case the traces of the slices with ID 16 and 17 are incorrect. To fix them we can use the script fridadrp-interpolate_traces_within_slices.

(venv_numina) $ fridadrp-interpolate_traces_within_slices --help
Usage: fridadrp-interpolate_traces_within_slices [-h] --image IMAGE
                                                 --traces TRACES
                                                 --sliceid SLICEID
                                                 [--skip-sliceid SKIP_SLICEID]
                                                 [--colrange MIN MAX]
                                                 [--xmedian XMEDIAN]
                                                 [--degslice DEGSLICE]
                                                 [--norefine]
                                                 [--degrefine DEGREFINE]
                                                 [--plots] --output OUTPUT
                                                 [--overwrite]
                                                 [--output-dir OUTPUT_DIR]
                                                 [--record] [--echo]
                                                 [--version]
                                                 [--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]

Interpolate/extrapolate traces within slices

Options:
  -h, --help            show this help message and exit
  --image IMAGE         Path to the input image file (FITS format)
  --traces TRACES       Path to the file with the slice trace polynomials
  --sliceid SLICEID     Slice ID to process. This option can be specified
                        multiple times
  --skip-sliceid SKIP_SLICEID
                        Slice ID to ignore in the interpolation/extrapolation.
                        This option can be specified multiple times
  --colrange MIN MAX    Column range to analyze (1-based index) along NAXIS1.
                        This option can be specified multiple times
  --xmedian XMEDIAN     Size of the median filter along NAXIS1 axis (odd;
                        default: 21)
  --degslice DEGSLICE   Degree of the polynomial to fit traces across slices
                        (default: 2)
  --norefine            Do not refine the interpolated/extrapolated traces
                        using the smoothed image data
  --degrefine DEGREFINE
                        Degree of the polynomial to refine the trace positions
                        (default: 2)
  --plots               Display plots of the interpolated/extrapolated traces
  --output OUTPUT       Output file name for the predicted polynomials
  --overwrite           Overwrite existing output file
  --output-dir OUTPUT_DIR
                        Output directory (default: .)
  --record              Record terminal output
  --echo                Display full command line
  --version             Display version
  --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        Set the logging level

If in this script we specify more than one slice using --sliceid several times, the order matters. The traces of each slice are interpolated/extrapolated in the order given. Once the traces of a particular slice have already been interpolated/extrapolated, they are then reused for the next slice (if it belongs to the same group as the new slice).

(venv_frida) $ fridadrp-interpolate_traces_within_slices \
  --image config09/0000000171-20260511-FRIDA-FridaSuccess-raw.fits \
  --traces traces_within_slice_polynomials_000171.fits \
  --sliceid 17 --sliceid 16 \
  --output traces_within_slice_polynomials_000171_fixed.fits \
  --norefine \
  --plots \
  --overwrite

If we do not use --plots, nothing is plotted.

If we do not use --norefine, the code attempts to refine the traces using information from the image (smoothed with --xmedian 21 by default). The refinement does not always work well, especially when there is a very weak trace at the edges. In this particular case it is better not to refine.

Important: there is a --skip-sliceid argument (which can be used multiple times) to indicate slice IDs corresponding to slices that we do not want to use as input in the interpolation/extrapolation. By default it is an empty list.

We check the new traces:

(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000171_fixed.fits \
  --image config09/0000000171-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000171_fixed2.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000171_fixed2_montage.png

The result is better, but still not fully satisfactory: trace number 1 of the slices with ID 15, 16, 14, and 17 remains slightly elevated on the left side.

Grating H High2, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config10/0000000174-20260511-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset -2 17 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_000174.fits \
  --pdf-out traces_within_slice_polynomials_000174.pdf \
  --plotsliceid 15 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000174.fits \
  --image config10/0000000174-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000174_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000174_fixed_montage.png

There are some problems with the traces of slices with ID 15, 16, and 17.

(venv_frida) $ fridadrp-interpolate_traces_within_slices \
  --image config10/0000000174-20260511-FRIDA-FridaSuccess-raw.fits \
  --traces traces_within_slice_polynomials_000174.fits \
  --sliceid 17 --sliceid 16 --sliceid 15 \
  --output traces_within_slice_polynomials_000174_fixed.fits \
  --norefine \
  --plots \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000174_fixed.fits \
  --image config10/0000000174-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000174_fixed2.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000174_fixed2_montage.png

Grating H High3, Medium Camera

Here the signal is weak, and it is noticeable that the DARK has not been properly subtracted. To fix this, we use --ymedian 101 (the default is --ymedian 0, in which case no median filter is applied in the vertical direction and its result is not subtracted). If --ymedian is different from zero, this filtering and subtraction is performed before the one corresponding to --xmedian.

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config11/0000007797-20260511-FRIDA-FridaSuccess_raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset 13 15 \
  --ymedian 101 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_007797.fits \
  --pdf-out traces_within_slice_polynomials_007797.pdf \
  --plotsliceid 15 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_007797.fits \
  --image config11/0000007797-20260511-FRIDA-FridaSuccess_raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_007797_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_007797_fixed_montage.png

Only the traces of the slices with ID 30, 29, 28, …, 16 are well fitted (half of the 30 total slices). Of the sequence with ID 1, 2, …, 15, only slice ID 3 turns out well. The following therefore fail: slices ID 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15. We need an image with a longer exposure time.

Grating H High4, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config12/0000007801-20260512-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset 12 15 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_007801.fits \
  --pdf-out traces_within_slice_polynomials_007801.pdf \
  --plotsliceid 15 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_007801.fits \
  --image config12/0000007801-20260512-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_007801_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_007801_fixed_montage.png

Grating K High1, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config05/0000000159-20260511-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset -16 -8 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_000159.fits \
  --pdf-out traces_within_slice_polynomials_000159.pdf \
  --plotsliceid 30 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000159.fits \
  --image config05/0000000159-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000159_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000159_fixed_montage.png

Here the slices with ID 15, 16, 17, 18, 19, and 30 are incorrect (the latter partially outside the detector). Slice ID 15 can be well predicted from group 1..15. It is then of interest to process slices ID 19, 18, 17, and 16 (in that order, since this way we move progressively further away from the extrapolation of the slices with ID 29..20). Finally, we can predict slice 30 from the slices with ID 29..16 (having already corrected slices ID 16, 17, 18, and 19).

(venv_frida) $ fridadrp-interpolate_traces_within_slices \
  --image config05/0000000159-20260511-FRIDA-FridaSuccess-raw.fits \
  --traces traces_within_slice_polynomials_000159.fits \
  --sliceid 15 --sliceid 19 --sliceid 18 --sliceid 17 --sliceid 16 \
  --sliceid 30 \
  --output traces_within_slice_polynomials_000159_fixed.fits \
  --norefine \
  --plots \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000159_fixed.fits \
  --image config05/0000000159-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000159_fixed2.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000159_fixed2_montage.png

Grating K High2, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config06/0000000162-20260511-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset -26 -16 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_000162.fits \
  --pdf-out traces_within_slice_polynomials_000162.pdf \
  --plotsliceid 30 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000162.fits \
  --image config06/0000000162-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000162_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000162_fixed_montage.png

Here the slices with ID 15, 16, 14, 17, 18, 19, and 30 are incorrect (the latter partially outside the detector).

(venv_frida) $ fridadrp-interpolate_traces_within_slices \
  --image config06/0000000162-20260511-FRIDA-FridaSuccess-raw.fits \
  --traces traces_within_slice_polynomials_000162.fits \
  --sliceid 14 --sliceid 15 \
  --sliceid 19 --sliceid 18 --sliceid 17 --sliceid 16 --sliceid 30 \
  --output traces_within_slice_polynomials_000162_fixed.fits \
  --norefine \
  --plots \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000162_fixed.fits \
  --image config06/0000000162-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000162_fixed2.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000162_fixed2_montage.png

Traces number 1 of slices with ID 15, 16, 14, 17, 13, 18, and 19 remain slightly elevated on the left side. It would be worth trying with a better image.

Grating K High3, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config07/0000000165-20260511-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset -46 -36 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_000165.fits \
  --pdf-out traces_within_slice_polynomials_000165.pdf \
  --plotsliceid 30 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000165.fits \
  --image config07/0000000165-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000165_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000165_fixed_montage.png

Here the slices with ID 19, 18, 13, 17, 14, 16, 15, and 30 are incorrect (the latter partially outside the detector).

(venv_frida) $ fridadrp-interpolate_traces_within_slices \
  --image config07/0000000165-20260511-FRIDA-FridaSuccess-raw.fits \
  --traces traces_within_slice_polynomials_000165.fits \
  --sliceid 13 --sliceid 14 --sliceid 15 \
  --sliceid 19 --sliceid 18 --sliceid 17 --sliceid 16 --sliceid 30 \
  --output traces_within_slice_polynomials_000165_fixed.fits \
  --norefine \
  --plots \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000165_fixed.fits \
  --image config07/0000000165-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000165_fixed2.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000165_fixed2_montage.png

Traces number 1 of slices ID 16, 17, and 18 remain slightly elevated on the left side. It would be worth trying with a better image.

Grating K High4, Medium Camera

(venv_frida) $ fridadrp-find_traces_within_slice_boundary_polynomials \
  --image config08/0000000168-20260511-FRIDA-FridaSuccess-raw.fits \
  --poly ../Comfiguraciones-IM-IFS-pruebas_con_el_DFAgent/slice_boundary_polynomials_1-30_fixed.fits \
  --voffset -56 -46 \
  --colrange 5 2044 \
  --ntraces 6 \
  --deg 3 \
  --theilsen \
  --output traces_within_slice_polynomials_000168.fits \
  --pdf-out traces_within_slice_polynomials_000168.pdf \
  --plotsliceid 30 \
  --overwrite
(venv_frida) $ fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000168.fits \
  --image config08/0000000168-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000168_fixed.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000168_fixed_montage.png

Here the slices with ID 15, 16, 14, 17, 13, 18, and 30 are incorrect (the latter partially outside the detector).

(venv_frida) $ fridadrp-interpolate_traces_within_slices \
  --image config08/0000000168-20260511-FRIDA-FridaSuccess-raw.fits \
  --traces traces_within_slice_polynomials_000168.fits \
  --sliceid 13 --sliceid 14 --sliceid 15 \
  --sliceid 18 --sliceid 17 --sliceid 16 --sliceid 30 \
  --output traces_within_slice_polynomials_000168_fixed.fits \
  --norefine \
  --plots \
  --overwrite
(venv_frida) $  fridadrp-overplot_slice_boundary_polynomials \
  --traces traces_within_slice_polynomials_000168_fixed.fits \
  --image config08/0000000168-20260511-FRIDA-FridaSuccess-raw.fits \
  --pdf-mosaic traces_within_slice_polynomials_000168_fixed2.pdf \
  --montage png \
  --traceid
../../_images/traces_within_slice_polynomials_000168_fixed2_montage.png

Traces number 1 of slices ID 15, 16, 14, 17, 13, and 18 remain slightly elevated on the left side. It would be worth trying with a better image.