MUSE Pipeline Reference Manual  2.5.2
muse_wavecal_z.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set sw=2 sts=2 et cin: */
3 /*
4  * This file is part of the MUSE Instrument Pipeline
5  * Copyright (C) 2005-2015 European Southern Observatory
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 /* This file was automatically generated */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 /*----------------------------------------------------------------------------*
29  * Includes *
30  *----------------------------------------------------------------------------*/
31 #include <string.h> /* strcmp(), strstr() */
32 #include <strings.h> /* strcasecmp() */
33 #include <cpl.h>
34 
35 #include "muse_wavecal_z.h" /* in turn includes muse.h */
36 
37 /*----------------------------------------------------------------------------*/
76 /*----------------------------------------------------------------------------*/
79 /*----------------------------------------------------------------------------*
80  * Static variables *
81  *----------------------------------------------------------------------------*/
82 static const char *muse_wavecal_help =
83  "This recipe detects arc emission lines and fits a wavelength solution to each slice of the instrument. The wavelength calibration table contains polynomials defining the wavelength solution of the slices on the CCD. Processing trims the raw data and records the overscan statistics, subtracts the bias (taking account of the overscan, if --overscan is not \"none\") and converts them from adu to count. Optionally, the dark can be subtracted and the data can be divided by the flat-field, but this is not recommended. The data is then combined using input parameters, into separate images for each lamp. To compute the wavelength solution, arc lines are detected at the center of each slice (using threshold detection on a S/N image) and subsequently assigned wavelengths, using pattern matching to identify lines from the input line catalog. Each line is then traced to the edges of the slice, using Gaussian centering in each CCD column. The Gaussians not only yield center, but also centering error, and line properties (e.g. FWHM). Deviant fits are detected using polynomial fits to each arc line (using the xorder parameter) and rejected. These analysis and measuring steps are carried out separately on images exposed by the different arc lamps, reducing the amount of blending, that can otherwise influence line identification and Gaussian centering. The final two-dimensional fit uses all positions (of all lamps), their wavelengths, and the given polynomial orders to compute the final wavelength solution for each slice, iteratively rejecting outliers. This final fit can be either unweighted (fitweighting=\"uniform\", for fastest processing) or weighted (other values of fitweighting, for higher accuracy).";
84 
85 static const char *muse_wavecal_help_esorex =
86  "\n\nInput frames for raw frame tag \"ARC\":\n"
87  "\n Frame tag Type Req #Fr Description"
88  "\n -------------------- ---- --- --- ------------"
89  "\n ARC raw Y Raw arc lamp exposures"
90  "\n MASTER_BIAS calib Y 1 Master bias"
91  "\n MASTER_DARK calib . 1 Master dark"
92  "\n MASTER_FLAT calib . 1 Master flat"
93  "\n TRACE_TABLE calib Y 1 Trace table"
94  "\n LINE_CATALOG calib Y 1 Arc line catalog"
95  "\n BADPIX_TABLE calib . Known bad pixels"
96  "\n\nProduct frames for raw frame tag \"ARC\":\n"
97  "\n Frame tag Level Description"
98  "\n -------------------- -------- ------------"
99  "\n WAVECAL_TABLE final Wavelength calibration table"
100  "\n WAVECAL_RESIDUALS final Fit residuals of all arc lines (if --residuals=true)"
101  "\n ARC_RED_LAMP final Reduced ARC image, per lamp (if --saveimages=true)";
102 
103 /*----------------------------------------------------------------------------*/
111 /*----------------------------------------------------------------------------*/
112 static cpl_recipeconfig *
113 muse_wavecal_new_recipeconfig(void)
114 {
115  cpl_recipeconfig *recipeconfig = cpl_recipeconfig_new();
116 
117  cpl_recipeconfig_set_tag(recipeconfig, "ARC", 1, -1);
118  cpl_recipeconfig_set_input(recipeconfig, "ARC", "MASTER_BIAS", 1, 1);
119  cpl_recipeconfig_set_input(recipeconfig, "ARC", "MASTER_DARK", -1, 1);
120  cpl_recipeconfig_set_input(recipeconfig, "ARC", "MASTER_FLAT", -1, 1);
121  cpl_recipeconfig_set_input(recipeconfig, "ARC", "TRACE_TABLE", 1, 1);
122  cpl_recipeconfig_set_input(recipeconfig, "ARC", "LINE_CATALOG", 1, 1);
123  cpl_recipeconfig_set_input(recipeconfig, "ARC", "BADPIX_TABLE", -1, -1);
124  cpl_recipeconfig_set_output(recipeconfig, "ARC", "WAVECAL_TABLE");
125  cpl_recipeconfig_set_output(recipeconfig, "ARC", "WAVECAL_RESIDUALS");
126  cpl_recipeconfig_set_output(recipeconfig, "ARC", "ARC_RED_LAMP");
127 
128  return recipeconfig;
129 } /* muse_wavecal_new_recipeconfig() */
130 
131 /*----------------------------------------------------------------------------*/
141 /*----------------------------------------------------------------------------*/
142 static cpl_error_code
143 muse_wavecal_prepare_header(const char *aFrametag, cpl_propertylist *aHeader)
144 {
145  cpl_ensure_code(aFrametag, CPL_ERROR_NULL_INPUT);
146  cpl_ensure_code(aHeader, CPL_ERROR_NULL_INPUT);
147  if (!strcmp(aFrametag, "WAVECAL_TABLE")) {
148  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LINES NDET",
149  CPL_TYPE_INT,
150  "Number of detected arc lines in slice j");
151  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LINES NID",
152  CPL_TYPE_INT,
153  "Number of identified arc lines in slice j");
154  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LINES PEAK MEAN",
155  CPL_TYPE_FLOAT,
156  "[count] Mean peak count level above background of detected arc lines in slice j");
157  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LINES PEAK STDEV",
158  CPL_TYPE_FLOAT,
159  "[count] Standard deviation of peak count level above background of detected arc lines in slice j");
160  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LINES PEAK MIN",
161  CPL_TYPE_FLOAT,
162  "[count] Peak count level above background of the faintest line in slice j");
163  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LINES PEAK MAX",
164  CPL_TYPE_FLOAT,
165  "[count] Peak count level above background of the brightest line in slice j");
166  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LAMP[0-9] LINES PEAK MEAN",
167  CPL_TYPE_FLOAT,
168  "[count] Mean peak count level of lines of lamp l above background of detected arc lines in slice j.");
169  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LAMP[0-9] LINES PEAK STDEV",
170  CPL_TYPE_FLOAT,
171  "[count] Standard deviation of peak count level of lines of lamp l above background of detected arc lines in slice j.");
172  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LAMP[0-9] LINES PEAK MAX",
173  CPL_TYPE_FLOAT,
174  "[count] Peak count level above background of the brightest line of lamp l in slice j.");
175  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LINES FWHM MEAN",
176  CPL_TYPE_FLOAT,
177  "Mean FWHM of detected arc lines in slice j");
178  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LINES FWHM STDEV",
179  CPL_TYPE_FLOAT,
180  "Standard deviation of FWHM of detected arc lines in slice j");
181  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LINES FWHM MIN",
182  CPL_TYPE_FLOAT,
183  "Minimum FWHM of detected arc lines in slice j");
184  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ LINES FWHM MAX",
185  CPL_TYPE_FLOAT,
186  "Maximum FWHM of detected arc lines in slice j");
187  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ RESOL",
188  CPL_TYPE_FLOAT,
189  "Mean spectral resolution R determined in slice j");
190  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ FIT NLINES",
191  CPL_TYPE_INT,
192  "Number of arc lines used in calibration solution fit in slice j");
193  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ FIT RMS",
194  CPL_TYPE_FLOAT,
195  "[Angstrom] RMS of the wavelength calibration fit in slice j");
196  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ DWLEN BOTTOM",
197  CPL_TYPE_FLOAT,
198  "[Angstrom] Difference in wavelength computed for the bottom left and bottom right corners of the slice on the CCD");
199  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ DWLEN TOP",
200  CPL_TYPE_FLOAT,
201  "[Angstrom] Difference in wavelength computed for the top left and top right corners of the slice on the CCD");
202  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ WLPOS",
203  CPL_TYPE_FLOAT,
204  "[pix] Position of wavelength given in WLEN in slice j");
205  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL SLICE[0-9]+ WLEN",
206  CPL_TYPE_FLOAT,
207  "[Angstrom] Wavelength associated to WLPOS in slice j");
208  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL NSATURATED LAMP[0-9]",
209  CPL_TYPE_INT,
210  "Number of saturated pixels in output data of lamp l");
211  } else if (!strcmp(aFrametag, "WAVECAL_RESIDUALS")) {
212  } else if (!strcmp(aFrametag, "ARC_RED_LAMP")) {
213  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL INPUT[0-9]+ NSATURATED",
214  CPL_TYPE_INT,
215  "Number of saturated pixels in raw arc i in input list");
216  muse_processing_prepare_property(aHeader, "ESO QC WAVECAL NSATURATED",
217  CPL_TYPE_INT,
218  "Number of saturated pixels in output data");
219  } else {
220  cpl_msg_warning(__func__, "Frame tag %s is not defined", aFrametag);
221  return CPL_ERROR_ILLEGAL_INPUT;
222  }
223  return CPL_ERROR_NONE;
224 } /* muse_wavecal_prepare_header() */
225 
226 /*----------------------------------------------------------------------------*/
235 /*----------------------------------------------------------------------------*/
236 static cpl_frame_level
237 muse_wavecal_get_frame_level(const char *aFrametag)
238 {
239  if (!aFrametag) {
240  return CPL_FRAME_LEVEL_NONE;
241  }
242  if (!strcmp(aFrametag, "WAVECAL_TABLE")) {
243  return CPL_FRAME_LEVEL_FINAL;
244  }
245  if (!strcmp(aFrametag, "WAVECAL_RESIDUALS")) {
246  return CPL_FRAME_LEVEL_FINAL;
247  }
248  if (!strcmp(aFrametag, "ARC_RED_LAMP")) {
249  return CPL_FRAME_LEVEL_FINAL;
250  }
251  return CPL_FRAME_LEVEL_NONE;
252 } /* muse_wavecal_get_frame_level() */
253 
254 /*----------------------------------------------------------------------------*/
263 /*----------------------------------------------------------------------------*/
264 static muse_frame_mode
265 muse_wavecal_get_frame_mode(const char *aFrametag)
266 {
267  if (!aFrametag) {
268  return MUSE_FRAME_MODE_ALL;
269  }
270  if (!strcmp(aFrametag, "WAVECAL_TABLE")) {
271  return MUSE_FRAME_MODE_MASTER;
272  }
273  if (!strcmp(aFrametag, "WAVECAL_RESIDUALS")) {
274  return MUSE_FRAME_MODE_MASTER;
275  }
276  if (!strcmp(aFrametag, "ARC_RED_LAMP")) {
277  return MUSE_FRAME_MODE_SUBSET;
278  }
279  return MUSE_FRAME_MODE_ALL;
280 } /* muse_wavecal_get_frame_mode() */
281 
282 /*----------------------------------------------------------------------------*/
292 /*----------------------------------------------------------------------------*/
293 static int
294 muse_wavecal_create(cpl_plugin *aPlugin)
295 {
296  /* Check that the plugin is part of a valid recipe */
297  cpl_recipe *recipe;
298  if (cpl_plugin_get_type(aPlugin) == CPL_PLUGIN_TYPE_RECIPE) {
299  recipe = (cpl_recipe *)aPlugin;
300  } else {
301  return -1;
302  }
303 
304  /* register the extended processing information (new FITS header creation, *
305  * getting of the frame level for a certain tag) */
307  muse_wavecal_new_recipeconfig(),
308  muse_wavecal_prepare_header,
309  muse_wavecal_get_frame_level,
310  muse_wavecal_get_frame_mode);
311 
312  /* XXX initialize timing in messages *
313  * since at least esorex is too stupid to turn it on, we have to do it */
315  cpl_msg_set_time_on();
316  }
317 
318  /* Create the parameter list in the cpl_recipe object */
319  recipe->parameters = cpl_parameterlist_new();
320  /* Fill the parameters list */
321  cpl_parameter *p;
322 
323  /* --nifu: IFU to handle. If set to 0, all IFUs are processed serially. If set to -1, all IFUs are processed in parallel. */
324  p = cpl_parameter_new_range("muse.muse_wavecal.nifu",
325  CPL_TYPE_INT,
326  "IFU to handle. If set to 0, all IFUs are processed serially. If set to -1, all IFUs are processed in parallel.",
327  "muse.muse_wavecal",
328  (int)0,
329  (int)-1,
330  (int)24);
331  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "nifu");
332  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "nifu");
333 
334  cpl_parameterlist_append(recipe->parameters, p);
335 
336  /* --overscan: If this is "none", stop when detecting discrepant overscan levels (see ovscsigma), for "offset" it assumes that the mean overscan level represents the real offset in the bias levels of the exposures involved, and adjusts the data accordingly; for "vpoly", a polynomial is fit to the vertical overscan and subtracted from the whole quadrant. */
337  p = cpl_parameter_new_value("muse.muse_wavecal.overscan",
338  CPL_TYPE_STRING,
339  "If this is \"none\", stop when detecting discrepant overscan levels (see ovscsigma), for \"offset\" it assumes that the mean overscan level represents the real offset in the bias levels of the exposures involved, and adjusts the data accordingly; for \"vpoly\", a polynomial is fit to the vertical overscan and subtracted from the whole quadrant.",
340  "muse.muse_wavecal",
341  (const char *)"vpoly");
342  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "overscan");
343  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "overscan");
344 
345  cpl_parameterlist_append(recipe->parameters, p);
346 
347  /* --ovscreject: This influences how values are rejected when computing overscan statistics. Either no rejection at all ("none"), rejection using the DCR algorithm ("dcr"), or rejection using an iterative constant fit ("fit"). */
348  p = cpl_parameter_new_value("muse.muse_wavecal.ovscreject",
349  CPL_TYPE_STRING,
350  "This influences how values are rejected when computing overscan statistics. Either no rejection at all (\"none\"), rejection using the DCR algorithm (\"dcr\"), or rejection using an iterative constant fit (\"fit\").",
351  "muse.muse_wavecal",
352  (const char *)"dcr");
353  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "ovscreject");
354  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ovscreject");
355 
356  cpl_parameterlist_append(recipe->parameters, p);
357 
358  /* --ovscsigma: If the deviation of mean overscan levels between a raw input image and the reference image is higher than |ovscsigma x stdev|, stop the processing. If overscan="vpoly", this is used as sigma rejection level for the iterative polynomial fit (the level comparison is then done afterwards with |100 x stdev| to guard against incompatible settings). Has no effect for overscan="offset". */
359  p = cpl_parameter_new_value("muse.muse_wavecal.ovscsigma",
360  CPL_TYPE_DOUBLE,
361  "If the deviation of mean overscan levels between a raw input image and the reference image is higher than |ovscsigma x stdev|, stop the processing. If overscan=\"vpoly\", this is used as sigma rejection level for the iterative polynomial fit (the level comparison is then done afterwards with |100 x stdev| to guard against incompatible settings). Has no effect for overscan=\"offset\".",
362  "muse.muse_wavecal",
363  (double)30.);
364  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "ovscsigma");
365  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ovscsigma");
366 
367  cpl_parameterlist_append(recipe->parameters, p);
368 
369  /* --ovscignore: The number of pixels of the overscan adjacent to the data section of the CCD that are ignored when computing statistics or fits. */
370  p = cpl_parameter_new_value("muse.muse_wavecal.ovscignore",
371  CPL_TYPE_INT,
372  "The number of pixels of the overscan adjacent to the data section of the CCD that are ignored when computing statistics or fits.",
373  "muse.muse_wavecal",
374  (int)3);
375  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "ovscignore");
376  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ovscignore");
377 
378  cpl_parameterlist_append(recipe->parameters, p);
379 
380  /* --combine: Type of lampwise image combination to use. */
381  p = cpl_parameter_new_enum("muse.muse_wavecal.combine",
382  CPL_TYPE_STRING,
383  "Type of lampwise image combination to use.",
384  "muse.muse_wavecal",
385  (const char *)"sigclip",
386  4,
387  (const char *)"average",
388  (const char *)"median",
389  (const char *)"minmax",
390  (const char *)"sigclip");
391  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "combine");
392  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "combine");
393 
394  cpl_parameterlist_append(recipe->parameters, p);
395 
396  /* --lampwise: Identify and measure the arc emission lines on images separately for each lamp setup. */
397  p = cpl_parameter_new_value("muse.muse_wavecal.lampwise",
398  CPL_TYPE_BOOL,
399  "Identify and measure the arc emission lines on images separately for each lamp setup.",
400  "muse.muse_wavecal",
401  (int)TRUE);
402  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "lampwise");
403  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "lampwise");
404  if (!getenv("MUSE_EXPERT_USER")) {
405  cpl_parameter_disable(p, CPL_PARAMETER_MODE_CLI);
406  }
407 
408  cpl_parameterlist_append(recipe->parameters, p);
409 
410  /* --sigma: Sigma level used to detect arc emission lines above the median background level in the S/N image of the central column of each slice */
411  p = cpl_parameter_new_value("muse.muse_wavecal.sigma",
412  CPL_TYPE_DOUBLE,
413  "Sigma level used to detect arc emission lines above the median background level in the S/N image of the central column of each slice",
414  "muse.muse_wavecal",
415  (double)1.0);
416  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "sigma");
417  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sigma");
418 
419  cpl_parameterlist_append(recipe->parameters, p);
420 
421  /* --dres: The allowed range of resolutions for pattern matching (of detected arc lines to line list) in fractions relative to the expected value */
422  p = cpl_parameter_new_value("muse.muse_wavecal.dres",
423  CPL_TYPE_DOUBLE,
424  "The allowed range of resolutions for pattern matching (of detected arc lines to line list) in fractions relative to the expected value",
425  "muse.muse_wavecal",
426  (double)0.05);
427  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "dres");
428  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "dres");
429 
430  cpl_parameterlist_append(recipe->parameters, p);
431 
432  /* --tolerance: Tolerance for pattern matching (of detected arc lines to line list) */
433  p = cpl_parameter_new_value("muse.muse_wavecal.tolerance",
434  CPL_TYPE_DOUBLE,
435  "Tolerance for pattern matching (of detected arc lines to line list)",
436  "muse.muse_wavecal",
437  (double)0.1);
438  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "tolerance");
439  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "tolerance");
440 
441  cpl_parameterlist_append(recipe->parameters, p);
442 
443  /* --xorder: Order of the polynomial for the horizontal curvature within each slice */
444  p = cpl_parameter_new_value("muse.muse_wavecal.xorder",
445  CPL_TYPE_INT,
446  "Order of the polynomial for the horizontal curvature within each slice",
447  "muse.muse_wavecal",
448  (int)2);
449  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "xorder");
450  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xorder");
451 
452  cpl_parameterlist_append(recipe->parameters, p);
453 
454  /* --yorder: Order of the polynomial used to fit the dispersion relation */
455  p = cpl_parameter_new_value("muse.muse_wavecal.yorder",
456  CPL_TYPE_INT,
457  "Order of the polynomial used to fit the dispersion relation",
458  "muse.muse_wavecal",
459  (int)6);
460  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "yorder");
461  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "yorder");
462 
463  cpl_parameterlist_append(recipe->parameters, p);
464 
465  /* --linesigma: Sigma level for iterative rejection of deviant fits for each arc line within each slice, a negative value means to use the default (2.5). */
466  p = cpl_parameter_new_value("muse.muse_wavecal.linesigma",
467  CPL_TYPE_DOUBLE,
468  "Sigma level for iterative rejection of deviant fits for each arc line within each slice, a negative value means to use the default (2.5).",
469  "muse.muse_wavecal",
470  (double)-1.0);
471  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "linesigma");
472  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "linesigma");
473 
474  cpl_parameterlist_append(recipe->parameters, p);
475 
476  /* --residuals: Create a table containing residuals of the fits to the data of all arc lines. This is useful to assess the quality of the wavelength solution in detail. */
477  p = cpl_parameter_new_value("muse.muse_wavecal.residuals",
478  CPL_TYPE_BOOL,
479  "Create a table containing residuals of the fits to the data of all arc lines. This is useful to assess the quality of the wavelength solution in detail.",
480  "muse.muse_wavecal",
481  (int)FALSE);
482  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "residuals");
483  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "residuals");
484 
485  cpl_parameterlist_append(recipe->parameters, p);
486 
487  /* --fitsigma: Sigma level for iterative rejection of deviant datapoints during the final polynomial wavelength solution within each slice, a negative value means to use the default (3.0). */
488  p = cpl_parameter_new_value("muse.muse_wavecal.fitsigma",
489  CPL_TYPE_DOUBLE,
490  "Sigma level for iterative rejection of deviant datapoints during the final polynomial wavelength solution within each slice, a negative value means to use the default (3.0).",
491  "muse.muse_wavecal",
492  (double)-1.0);
493  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "fitsigma");
494  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fitsigma");
495 
496  cpl_parameterlist_append(recipe->parameters, p);
497 
498  /* --fitweighting: Type of weighting to use in the final polynomial wavelength solution fit, using centroiding error estimate and/or scatter of each single line as estimates of its accuracy. */
499  p = cpl_parameter_new_enum("muse.muse_wavecal.fitweighting",
500  CPL_TYPE_STRING,
501  "Type of weighting to use in the final polynomial wavelength solution fit, using centroiding error estimate and/or scatter of each single line as estimates of its accuracy.",
502  "muse.muse_wavecal",
503  (const char *)"cerrscatter",
504  4,
505  (const char *)"uniform",
506  (const char *)"cerr",
507  (const char *)"scatter",
508  (const char *)"cerrscatter");
509  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "fitweighting");
510  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "fitweighting");
511 
512  cpl_parameterlist_append(recipe->parameters, p);
513 
514  /* --saveimages: Save */
515  p = cpl_parameter_new_value("muse.muse_wavecal.saveimages",
516  CPL_TYPE_BOOL,
517  "Save",
518  "muse.muse_wavecal",
519  (int)FALSE);
520  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "saveimages");
521  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "saveimages");
522 
523  cpl_parameterlist_append(recipe->parameters, p);
524 
525  /* --resample: Resample the input arc images onto 2D images for a visual check using tracing and wavelength calibration solutions. Note that the image produced will show small wiggles even when the calibration was successful! */
526  p = cpl_parameter_new_value("muse.muse_wavecal.resample",
527  CPL_TYPE_BOOL,
528  "Resample the input arc images onto 2D images for a visual check using tracing and wavelength calibration solutions. Note that the image produced will show small wiggles even when the calibration was successful!",
529  "muse.muse_wavecal",
530  (int)FALSE);
531  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "resample");
532  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "resample");
533  if (!getenv("MUSE_EXPERT_USER")) {
534  cpl_parameter_disable(p, CPL_PARAMETER_MODE_CLI);
535  }
536 
537  cpl_parameterlist_append(recipe->parameters, p);
538 
539  /* --wavemap: Create a wavelength map of the input images */
540  p = cpl_parameter_new_value("muse.muse_wavecal.wavemap",
541  CPL_TYPE_BOOL,
542  "Create a wavelength map of the input images",
543  "muse.muse_wavecal",
544  (int)FALSE);
545  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "wavemap");
546  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "wavemap");
547  if (!getenv("MUSE_EXPERT_USER")) {
548  cpl_parameter_disable(p, CPL_PARAMETER_MODE_CLI);
549  }
550 
551  cpl_parameterlist_append(recipe->parameters, p);
552 
553  /* --merge: Merge output products from different IFUs into a common file. */
554  p = cpl_parameter_new_value("muse.muse_wavecal.merge",
555  CPL_TYPE_BOOL,
556  "Merge output products from different IFUs into a common file.",
557  "muse.muse_wavecal",
558  (int)FALSE);
559  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CFG, "merge");
560  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "merge");
561 
562  cpl_parameterlist_append(recipe->parameters, p);
563 
564  return 0;
565 } /* muse_wavecal_create() */
566 
567 /*----------------------------------------------------------------------------*/
578 /*----------------------------------------------------------------------------*/
579 static int
580 muse_wavecal_params_fill(muse_wavecal_params_t *aParams, cpl_parameterlist *aParameters)
581 {
582  cpl_ensure_code(aParams, CPL_ERROR_NULL_INPUT);
583  cpl_ensure_code(aParameters, CPL_ERROR_NULL_INPUT);
584  cpl_parameter *p;
585 
586  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.nifu");
587  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
588  aParams->nifu = cpl_parameter_get_int(p);
589 
590  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.overscan");
591  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
592  aParams->overscan = cpl_parameter_get_string(p);
593 
594  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.ovscreject");
595  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
596  aParams->ovscreject = cpl_parameter_get_string(p);
597 
598  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.ovscsigma");
599  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
600  aParams->ovscsigma = cpl_parameter_get_double(p);
601 
602  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.ovscignore");
603  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
604  aParams->ovscignore = cpl_parameter_get_int(p);
605 
606  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.combine");
607  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
608  aParams->combine_s = cpl_parameter_get_string(p);
609  aParams->combine =
610  (!strcasecmp(aParams->combine_s, "average")) ? MUSE_WAVECAL_PARAM_COMBINE_AVERAGE :
611  (!strcasecmp(aParams->combine_s, "median")) ? MUSE_WAVECAL_PARAM_COMBINE_MEDIAN :
612  (!strcasecmp(aParams->combine_s, "minmax")) ? MUSE_WAVECAL_PARAM_COMBINE_MINMAX :
613  (!strcasecmp(aParams->combine_s, "sigclip")) ? MUSE_WAVECAL_PARAM_COMBINE_SIGCLIP :
614  MUSE_WAVECAL_PARAM_COMBINE_INVALID_VALUE;
615  cpl_ensure_code(aParams->combine != MUSE_WAVECAL_PARAM_COMBINE_INVALID_VALUE,
616  CPL_ERROR_ILLEGAL_INPUT);
617 
618  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.lampwise");
619  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
620  aParams->lampwise = cpl_parameter_get_bool(p);
621 
622  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.sigma");
623  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
624  aParams->sigma = cpl_parameter_get_double(p);
625 
626  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.dres");
627  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
628  aParams->dres = cpl_parameter_get_double(p);
629 
630  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.tolerance");
631  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
632  aParams->tolerance = cpl_parameter_get_double(p);
633 
634  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.xorder");
635  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
636  aParams->xorder = cpl_parameter_get_int(p);
637 
638  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.yorder");
639  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
640  aParams->yorder = cpl_parameter_get_int(p);
641 
642  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.linesigma");
643  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
644  aParams->linesigma = cpl_parameter_get_double(p);
645 
646  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.residuals");
647  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
648  aParams->residuals = cpl_parameter_get_bool(p);
649 
650  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.fitsigma");
651  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
652  aParams->fitsigma = cpl_parameter_get_double(p);
653 
654  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.fitweighting");
655  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
656  aParams->fitweighting_s = cpl_parameter_get_string(p);
657  aParams->fitweighting =
658  (!strcasecmp(aParams->fitweighting_s, "uniform")) ? MUSE_WAVECAL_PARAM_FITWEIGHTING_UNIFORM :
659  (!strcasecmp(aParams->fitweighting_s, "cerr")) ? MUSE_WAVECAL_PARAM_FITWEIGHTING_CERR :
660  (!strcasecmp(aParams->fitweighting_s, "scatter")) ? MUSE_WAVECAL_PARAM_FITWEIGHTING_SCATTER :
661  (!strcasecmp(aParams->fitweighting_s, "cerrscatter")) ? MUSE_WAVECAL_PARAM_FITWEIGHTING_CERRSCATTER :
662  MUSE_WAVECAL_PARAM_FITWEIGHTING_INVALID_VALUE;
663  cpl_ensure_code(aParams->fitweighting != MUSE_WAVECAL_PARAM_FITWEIGHTING_INVALID_VALUE,
664  CPL_ERROR_ILLEGAL_INPUT);
665 
666  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.saveimages");
667  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
668  aParams->saveimages = cpl_parameter_get_bool(p);
669 
670  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.resample");
671  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
672  aParams->resample = cpl_parameter_get_bool(p);
673 
674  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.wavemap");
675  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
676  aParams->wavemap = cpl_parameter_get_bool(p);
677 
678  p = cpl_parameterlist_find(aParameters, "muse.muse_wavecal.merge");
679  cpl_ensure_code(p, CPL_ERROR_DATA_NOT_FOUND);
680  aParams->merge = cpl_parameter_get_bool(p);
681 
682  return 0;
683 } /* muse_wavecal_params_fill() */
684 
685 /*----------------------------------------------------------------------------*/
692 /*----------------------------------------------------------------------------*/
693 static int
694 muse_wavecal_exec(cpl_plugin *aPlugin)
695 {
696  if (cpl_plugin_get_type(aPlugin) != CPL_PLUGIN_TYPE_RECIPE) {
697  return -1;
698  }
700  cpl_recipe *recipe = (cpl_recipe *)aPlugin;
701  cpl_msg_set_threadid_on();
702 
703  cpl_frameset *usedframes = cpl_frameset_new(),
704  *outframes = cpl_frameset_new();
705  muse_wavecal_params_t params;
706  muse_wavecal_params_fill(&params, recipe->parameters);
707 
708  cpl_errorstate prestate = cpl_errorstate_get();
709 
710  if (params.nifu < -1 || params.nifu > kMuseNumIFUs) {
711  cpl_msg_error(__func__, "Please specify a valid IFU number (between 1 and "
712  "%d), 0 (to process all IFUs consecutively), or -1 (to "
713  "process all IFUs in parallel) using --nifu.", kMuseNumIFUs);
714  return -1;
715  } /* if invalid params.nifu */
716 
717  cpl_boolean donotmerge = CPL_FALSE; /* depending on nifu we may not merge */
718  int rc = 0;
719  if (params.nifu > 0) {
720  muse_processing *proc = muse_processing_new("muse_wavecal",
721  recipe);
722  rc = muse_wavecal_compute(proc, &params);
723  cpl_frameset_join(usedframes, proc->usedframes);
724  cpl_frameset_join(outframes, proc->outframes);
726  donotmerge = CPL_TRUE; /* after processing one IFU, merging cannot work */
727  } else if (params.nifu < 0) { /* parallel processing */
728  int *rcs = cpl_calloc(kMuseNumIFUs, sizeof(int));
729  int nifu;
730  #pragma omp parallel for default(none) \
731  shared(outframes, params, rcs, recipe, usedframes)
732  for (nifu = 1; nifu <= kMuseNumIFUs; nifu++) {
733  muse_processing *proc = muse_processing_new("muse_wavecal",
734  recipe);
735  muse_wavecal_params_t *pars = cpl_malloc(sizeof(muse_wavecal_params_t));
736  memcpy(pars, &params, sizeof(muse_wavecal_params_t));
737  pars->nifu = nifu;
738  int *rci = rcs + (nifu - 1);
739  *rci = muse_wavecal_compute(proc, pars);
740  if (rci && (int)cpl_error_get_code() == MUSE_ERROR_CHIP_NOT_LIVE) {
741  *rci = 0;
742  }
743  cpl_free(pars);
744  #pragma omp critical(muse_processing_used_frames)
745  cpl_frameset_join(usedframes, proc->usedframes);
746  #pragma omp critical(muse_processing_output_frames)
747  cpl_frameset_join(outframes, proc->outframes);
749  } /* for nifu */
750  /* non-parallel loop to propagate the "worst" return code; *
751  * since we only ever return -1, any non-zero code is propagated */
752  for (nifu = 1; nifu <= kMuseNumIFUs; nifu++) {
753  if (rcs[nifu-1] != 0) {
754  rc = rcs[nifu-1];
755  } /* if */
756  } /* for nifu */
757  cpl_free(rcs);
758  } else { /* serial processing */
759  for (params.nifu = 1; params.nifu <= kMuseNumIFUs && !rc; params.nifu++) {
760  muse_processing *proc = muse_processing_new("muse_wavecal",
761  recipe);
762  rc = muse_wavecal_compute(proc, &params);
763  if (rc && (int)cpl_error_get_code() == MUSE_ERROR_CHIP_NOT_LIVE) {
764  rc = 0;
765  }
766  cpl_frameset_join(usedframes, proc->usedframes);
767  cpl_frameset_join(outframes, proc->outframes);
769  } /* for nifu */
770  } /* else */
771  UNUSED_ARGUMENT(donotmerge); /* maybe this is not going to be used below */
772 
773  if (!cpl_errorstate_is_equal(prestate)) {
774  /* dump all errors from this recipe in chronological order */
775  cpl_errorstate_dump(prestate, CPL_FALSE, muse_cplerrorstate_dump_some);
776  /* reset message level to not get the same errors displayed again by esorex */
777  cpl_msg_set_level(CPL_MSG_INFO);
778  }
779  /* clean up duplicates in framesets of used and output frames */
782 
783  /* merge output products from the up to 24 IFUs */
784  if (params.merge && !donotmerge) {
785  muse_utils_frameset_merge_frames(outframes, CPL_TRUE);
786  }
787 
788  /* to get esorex to see our classification (frame groups etc.), *
789  * replace the original frameset with the list of used frames *
790  * before appending product output frames */
791  /* keep the same pointer, so just erase all frames, not delete the frameset */
792  muse_cplframeset_erase_all(recipe->frames);
793  cpl_frameset_join(recipe->frames, usedframes);
794  cpl_frameset_join(recipe->frames, outframes);
795  cpl_frameset_delete(usedframes);
796  cpl_frameset_delete(outframes);
797  return rc;
798 } /* muse_wavecal_exec() */
799 
800 /*----------------------------------------------------------------------------*/
807 /*----------------------------------------------------------------------------*/
808 static int
809 muse_wavecal_destroy(cpl_plugin *aPlugin)
810 {
811  /* Get the recipe from the plugin */
812  cpl_recipe *recipe;
813  if (cpl_plugin_get_type(aPlugin) == CPL_PLUGIN_TYPE_RECIPE) {
814  recipe = (cpl_recipe *)aPlugin;
815  } else {
816  return -1;
817  }
818 
819  /* Clean up */
820  cpl_parameterlist_delete(recipe->parameters);
822  return 0;
823 } /* muse_wavecal_destroy() */
824 
825 /*----------------------------------------------------------------------------*/
835 /*----------------------------------------------------------------------------*/
836 int
837 cpl_plugin_get_info(cpl_pluginlist *aList)
838 {
839  cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
840  cpl_plugin *plugin = &recipe->interface;
841 
842  char *helptext;
844  helptext = cpl_sprintf("%s%s", muse_wavecal_help,
845  muse_wavecal_help_esorex);
846  } else {
847  helptext = cpl_sprintf("%s", muse_wavecal_help);
848  }
849 
850  /* Initialize the CPL plugin stuff for this module */
851  cpl_plugin_init(plugin, CPL_PLUGIN_API, MUSE_BINARY_VERSION,
852  CPL_PLUGIN_TYPE_RECIPE,
853  "muse_wavecal",
854  "Detect arc emission lines and determine the wavelength solution for each slice.",
855  helptext,
856  "Peter Weilbacher",
857  "usd-help@eso.org",
859  muse_wavecal_create,
860  muse_wavecal_exec,
861  muse_wavecal_destroy);
862  cpl_pluginlist_append(aList, plugin);
863  cpl_free(helptext);
864 
865  return 0;
866 } /* cpl_plugin_get_info() */
867 
void muse_processing_delete(muse_processing *aProcessing)
Free the muse_processing structure.
double fitsigma
Sigma level for iterative rejection of deviant datapoints during the final polynomial wavelength solu...
double linesigma
Sigma level for iterative rejection of deviant fits for each arc line within each slice...
const char * ovscreject
This influences how values are rejected when computing overscan statistics. Either no rejection at al...
double ovscsigma
If the deviation of mean overscan levels between a raw input image and the reference image is higher ...
muse_cplframework_type muse_cplframework(void)
Return the CPL framework the recipe is run under.
int ovscignore
The number of pixels of the overscan adjacent to the data section of the CCD that are ignored when co...
double dres
The allowed range of resolutions for pattern matching (of detected arc lines to line list) in fractio...
cpl_frameset * usedframes
muse_processing * muse_processing_new(const char *aName, cpl_recipe *aRecipe)
Create a new processing structure.
const char * muse_get_license(void)
Get the pipeline copyright and license.
Definition: muse_utils.c:83
muse_frame_mode
int nifu
IFU to handle. If set to 0, all IFUs are processed serially. If set to -1, all IFUs are processed in ...
cpl_frameset * outframes
const char * overscan
If this is "none", stop when detecting discrepant overscan levels (see ovscsigma), for "offset" it assumes that the mean overscan level represents the real offset in the bias levels of the exposures involved, and adjusts the data accordingly; for "vpoly", a polynomial is fit to the vertical overscan and subtracted from the whole quadrant.
void muse_cplerrorstate_dump_some(unsigned aCurrent, unsigned aFirst, unsigned aLast)
Dump some CPL errors.
int wavemap
Create a wavelength map of the input images.
int lampwise
Identify and measure the arc emission lines on images separately for each lamp setup.
int residuals
Create a table containing residuals of the fits to the data of all arc lines. This is useful to asses...
void muse_processinginfo_delete(cpl_recipe *)
Clear all information from the processing info and from the recipe config.
int resample
Resample the input arc images onto 2D images for a visual check using tracing and wavelength calibrat...
int yorder
Order of the polynomial used to fit the dispersion relation.
cpl_error_code muse_cplframeset_erase_duplicate(cpl_frameset *aFrames)
Erase all duplicate frames from a frameset.
cpl_error_code muse_cplframeset_erase_all(cpl_frameset *aFrames)
Erase all frames in a frameset.
int fitweighting
Type of weighting to use in the final polynomial wavelength solution fit, using centroiding error est...
const char * combine_s
Type of lampwise image combination to use. (as string)
void muse_processinginfo_register(cpl_recipe *, cpl_recipeconfig *, muse_processing_prepare_header_func *, muse_processing_get_frame_level_func *, muse_processing_get_frame_mode_func *)
Register extended functionalities for MUSE recipes.
int combine
Type of lampwise image combination to use.
Structure to hold the parameters of the muse_wavecal recipe.
double tolerance
Tolerance for pattern matching (of detected arc lines to line list)
void muse_processing_recipeinfo(cpl_plugin *)
Output main pipeline configuration, inputs, and parameters.
const char * fitweighting_s
Type of weighting to use in the final polynomial wavelength solution fit, using centroiding error est...
cpl_error_code muse_utils_frameset_merge_frames(cpl_frameset *aFrames, cpl_boolean aDelete)
Merge IFU-specific files from a frameset to create multi-IFU outputs.
Definition: muse_utils.c:589
double sigma
Sigma level used to detect arc emission lines above the median background level in the S/N image of t...
int merge
Merge output products from different IFUs into a common file.
int xorder
Order of the polynomial for the horizontal curvature within each slice.
cpl_error_code muse_processing_prepare_property(cpl_propertylist *, const char *, cpl_type, const char *)
Prepare and check the specified property.