MUSE Pipeline Reference Manual  2.5.2
muse_wavecal.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-2014 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 /*----------------------------------------------------------------------------*
27  * Includes *
28  *----------------------------------------------------------------------------*/
29 #include <stdio.h>
30 #include <float.h>
31 #include <math.h>
32 #include <string.h>
33 #include <cpl.h>
34 #include <muse.h>
35 #include "muse_wavecal_z.h"
36 
37 /*---------------------------------------------------------------------------*
38  * Functions code *
39  *---------------------------------------------------------------------------*/
40 
41 /*---------------------------------------------------------------------------*/
52 /*---------------------------------------------------------------------------*/
53 static muse_image *
54 muse_wavecal_sum_and_save(muse_imagelist *aImages, muse_processing *aProcessing,
55  int aIFU, cpl_boolean aSave)
56 {
57  cpl_ensure(aImages && aProcessing, CPL_ERROR_NULL_INPUT, NULL);
58 
59  /* do the final (sum!) image combination if requested */
60  char *pname = cpl_sprintf("muse.%s.combine", aProcessing->name);
61  cpl_parameter *param = cpl_parameterlist_find(aProcessing->parameters, pname);
62  char *porig = cpl_strdup(cpl_parameter_get_string(param));
63  cpl_parameter_set_string(param, "sum");
64  cpl_free(pname);
65  muse_combinepar *cpars = muse_combinepar_new(aProcessing->parameters,
66  "muse.muse_wavecal");
67  cpl_parameter_set_string(param, porig);
68  cpl_free(porig);
69  muse_image *image = muse_combine_images(cpars, aImages);
71  if (!image) {
72  cpl_error_set_message(__func__, CPL_ERROR_ILLEGAL_OUTPUT,
73  "summing per-lamp images did not work");
74  return NULL;
75  }
76 
77  /* transfer NSATURATION headers from lamp-combined images to final combined image */
78  /* loop over lamps (entries in the imagelist) */
79  unsigned int k;
80  for (k = 0; k < muse_imagelist_get_size(aImages); k++) {
81  cpl_errorstate state = cpl_errorstate_get();
82  unsigned int i = 1;
83  while (cpl_errorstate_is_equal(state)) {
84  char *kwget = cpl_sprintf(QC_WAVECAL_PREFIXi" "QC_BASIC_NSATURATED, i),
85  *kwout = cpl_sprintf(QC_WAVECAL_PREFIXli" "QC_BASIC_NSATURATED, k+1, i);
86  /* this will change the errorstate if the keyword doesn't exist: */
87  int nsaturated = cpl_propertylist_get_int(muse_imagelist_get(aImages, k)->header,
88  kwget);
89  if (cpl_errorstate_is_equal(state)) {
90  cpl_propertylist_update_int(image->header, kwout, nsaturated);
91  }
92  cpl_free(kwget);
93  cpl_free(kwout);
94  i++;
95  } /* while (errorstate is the same) */
96  /* reset the old errorstate, we don't want to leak *
97  * it outside, since it's not a real error */
98  cpl_errorstate_set(state);
99  } /* for k (all images in list) */
100  /* count saturated pixels in final combined image */
101  muse_basicproc_qc_saturated(image, QC_WAVECAL_PREFIX);
102 
103  /* save the pre-processed image to file *
104  * (this should be done, even if the wavelength calibration fails so *
105  * that the user can check what input files were used for the wavecal) */
106  if (aSave) {
107  muse_processing_save_image(aProcessing, aIFU, image, MUSE_TAG_ARC_RED);
108  }
109 
110  return image;
111 } /* muse_wavecal_sum_and_save() */
112 
113 /*----------------------------------------------------------------------------*/
125 /*----------------------------------------------------------------------------*/
126 static cpl_error_code
127 muse_wavecal_qc_nsaturated_single(cpl_propertylist *aHeader,
128  cpl_propertylist *aHin,
129  const char *aPrefix, unsigned int aIdx)
130 {
131  cpl_ensure_code(aHeader && aHin, CPL_ERROR_NULL_INPUT);
132 
133  /* set up lamp name(s) and number(s), depending on master status or header */
134  char *name = NULL;
135  cpl_array *numbers = NULL;
136  if (aIdx == 99) {
137  name = cpl_sprintf("combined");
138  numbers = cpl_array_new(1, CPL_TYPE_INT);
139  cpl_array_set_int(numbers, 0, 99);
140  } else {
141  name = muse_utils_header_get_lamp_names(aHin, ',');
142  numbers = muse_utils_header_get_lamp_numbers(aHin);
143  }
144 
145  /* use the info to actually propagate the nsaturated pixel info, *
146  * if only a single lamp was found active in the header */
147  if (cpl_array_get_size(numbers) != 1) {
148  cpl_msg_warning(__func__, "Image %u: %"CPL_SIZE_FORMAT" lamps active (%s)",
149  aIdx, cpl_array_get_size(numbers), name);
150  } else {
151  int nlamp = cpl_array_get_int(numbers, 0, NULL);
152  char *kw = NULL, /* output keyword */
153  *kwin = NULL;
154  if (aPrefix[strlen(aPrefix)-1] == ' ') {
155  kw = cpl_sprintf("%s%s LAMP%d", aPrefix, QC_BASIC_NSATURATED, nlamp);
156  kwin = cpl_sprintf("%s%s", aPrefix, QC_BASIC_NSATURATED);
157  } else {
158  kw = cpl_sprintf("%s %s LAMP%d", aPrefix, QC_BASIC_NSATURATED, nlamp);
159  kwin = cpl_sprintf("%s %s", aPrefix, QC_BASIC_NSATURATED);
160  }
161  int nsaturated = cpl_propertylist_get_int(aHin, kwin);
162  cpl_free(kwin);
163  cpl_msg_debug(__func__, "Lamp %d (%s, image %u): %d saturated pixels",
164  nlamp, name, aIdx, nsaturated);
165  cpl_propertylist_update_int(aHeader, kw, nsaturated);
166  cpl_free(kw);
167  } /* else: one single lamp */
168  cpl_array_delete(numbers);
169  cpl_free(name);
170 
171  return CPL_ERROR_NONE;
172 } /* muse_wavecal_qc_nsaturated_single() */
173 
174 /*----------------------------------------------------------------------------*/
186 /*----------------------------------------------------------------------------*/
187 static cpl_error_code
188 muse_wavecal_qc_nsaturated(cpl_propertylist *aHeader, muse_imagelist *aList,
189  muse_image *aMaster, const char *aPrefix)
190 {
191  cpl_ensure_code(aHeader, CPL_ERROR_NULL_INPUT);
192 
193  /* copy the saturated pixel counts from the fully *
194  * combined master image, if it exists */
195  if (aMaster && aMaster->header) {
196  muse_wavecal_qc_nsaturated_single(aHeader, aMaster->header, aPrefix, 99);
197  }
198 
199  /* now copy the saturated pixel counts from the *
200  * (lamp-combined) single images, if they exist */
201  unsigned int k;
202  for (k = 0; aList && k < aList->size; k++) {
203  cpl_propertylist *header = muse_imagelist_get(aList, k)->header;
204  muse_wavecal_qc_nsaturated_single(aHeader, header, aPrefix, k);
205  } /* for k (all images) */
206  return CPL_ERROR_NONE;
207 } /* muse_wavecal_qc_nsaturated() */
208 
209 /*---------------------------------------------------------------------------*/
216 /*---------------------------------------------------------------------------*/
217 int
218 muse_wavecal_compute(muse_processing *aProcessing,
219  muse_wavecal_params_t *aParams)
220 {
222  if (aParams->fitweighting == MUSE_WAVECAL_PARAM_FITWEIGHTING_CERR) {
223  fweight = MUSE_WAVE_WEIGHTING_CERR;
224  } else if (aParams->fitweighting == MUSE_WAVECAL_PARAM_FITWEIGHTING_SCATTER) {
225  fweight = MUSE_WAVE_WEIGHTING_SCATTER;
226  } else if (aParams->fitweighting == MUSE_WAVECAL_PARAM_FITWEIGHTING_CERRSCATTER) {
228  } else if (aParams->fitweighting != MUSE_WAVECAL_PARAM_FITWEIGHTING_UNIFORM) {
229  cpl_msg_error(__func__, "unknown fitweighting method \"%s\"",
230  aParams->fitweighting_s);
231  return -1;
232  }
233 
235  "muse.muse_wavecal");
236  cpl_frameset **labeledframes = NULL;
238  aParams->nifu,
239  bpars,
240  &labeledframes);
242  cpl_ensure(images, cpl_error_get_code(), -1);
243  /* save all resulting images, but only if there were different lamp *
244  * setups; otherwise we only save the combined image, see below */
245  cpl_frameset *used = aProcessing->usedframes;
246  unsigned int k, kimages = muse_imagelist_get_size(images);
247  cpl_boolean needsum = !aParams->lampwise || aParams->resample;
248  for (k = 0; k < kimages; k++) {
249  /* only save individual files if different from combined one */
250  if (!(kimages == 1 && needsum)) {
251  muse_image *image = muse_imagelist_get(images, k);
252  if (labeledframes) { /* replace usedframes with ones used for _this_ image */
253  aProcessing->usedframes = labeledframes[k];
254  }
255  muse_basicproc_qc_saturated(image, QC_WAVECAL_PREFIX);
256  cpl_propertylist_erase_regexp(image->header, MUSE_WCS_KEYS, 0);
257  if (aParams->saveimages) {
258  muse_processing_save_image(aProcessing, aParams->nifu, image,
259  MUSE_TAG_ARC_RED_LAMP);
260  }
261  }
262  /* delete the corresponding frameset in any case, if present */
263  if (labeledframes) {
264  cpl_frameset_delete(labeledframes[k]);
265  }
266  } /* for k (all images in list) */
267  cpl_free(labeledframes);
268  aProcessing->usedframes = used;
269  muse_image *masterimage = NULL;
270  if (needsum) {
271  masterimage = muse_wavecal_sum_and_save(images, aProcessing, aParams->nifu,
272  aParams->saveimages);
273  } /* if !lampwise */
274 
275  /* line catalog is needed in any case */
276  muse_table *linelist = muse_processing_load_table(aProcessing,
277  MUSE_TAG_LINE_CATALOG, 0);
278  int rc = muse_wave_lines_check(linelist) ? 0 : -1;
279  if (rc) {
280  muse_table_delete(linelist);
281  muse_image_delete(masterimage);
282  muse_imagelist_delete(images);
283  return rc;
284  }
285  /* trace table is needed in any case if we made it here */
286  cpl_table *tracetable = muse_processing_load_ctable(aProcessing, MUSE_TAG_TRACE_TABLE,
287  aParams->nifu);
288 
289  /* the main step: compute the wavelength solution, add QC parameters *
290  * to the FITS headers of the input exposure(s), since the output *
291  * table doesn't carry headers, and create an output header for saving *
292  * by copying most of the input headers. */
293  cpl_table *wavecaltable = NULL;
294  cpl_propertylist *header = NULL;
296  p->xorder = aParams->xorder;
297  p->yorder = aParams->yorder;
298  p->detsigma = aParams->sigma;
299  p->ddisp = aParams->dres;
300  p->tolerance = aParams->tolerance;
301  p->linesigma = aParams->linesigma;
302  p->rflag = aParams->residuals; /* the residuals component itself stays NULL */
303  p->fitsigma = aParams->fitsigma;
304  p->fitweighting = fweight;
305  if (aParams->lampwise) { /* separate image handling (new method) */
306  wavecaltable = muse_wave_calib_lampwise(images, tracetable, linelist->table,
307  p);
308  header = cpl_propertylist_duplicate(muse_imagelist_get(images, 0)->header);
309  } else { /* direct measurement and fit on single combined image (old method) */
310  wavecaltable = muse_wave_calib(masterimage, tracetable, linelist->table, p);
311  header = cpl_propertylist_duplicate(masterimage->header);
312  }
313  muse_table_delete(linelist);
314  if (!wavecaltable) { /* failure of the main step, return now */
315  muse_image_delete(masterimage);
316  muse_imagelist_delete(images);
317  cpl_table_delete(tracetable);
318  cpl_propertylist_delete(header);
320  return -1;
321  }
322 
323  /* propagate saturated pixel counts of (the) combined image(s), *
324  * but remove old ones */
325  muse_wavecal_qc_nsaturated(header, images, masterimage, QC_WAVECAL_PREFIX);
326  cpl_propertylist_erase_regexp(header, "^ESO QC.*"QC_BASIC_NSATURATED"$", 0);
327  /* create table header by copying most of the image header *
328  * and save the wavelength solution in this header */
329  cpl_propertylist_erase_regexp(header,
330  "^SIMPLE$|^BITPIX$|^NAXIS|^EXTEND$|^XTENSION$|"
331  "^DATASUM$|^DATAMIN$|^DATAMAX$|^DATAMD5$|"
332  "^PCOUNT$|^GCOUNT$|^HDUVERS$|^BLANK$|"
333  "^BZERO$|^BSCALE$|^BUNIT$|^CHECKSUM$|^INHERIT$|"
334  "^PIPEFILE$|^ESO PRO ", 0);
335  muse_processing_save_table(aProcessing, aParams->nifu, wavecaltable, header,
336  MUSE_TAG_WAVECAL_TABLE, MUSE_TABLE_TYPE_CPL);
337  if (p->residuals) {
338  /* don't want to save QC headers with the residuals table: */
339  cpl_propertylist_erase_regexp(header, QC_WAVECAL_PREFIX, 0);
340  muse_processing_save_table(aProcessing, aParams->nifu, p->residuals, header,
341  MUSE_TAG_WAVECAL_DEBUG, MUSE_TABLE_TYPE_CPL);
342  }
344  cpl_propertylist_delete(header);
345 
346  /* now the extra steps: resampling, and/or wavemap creation */
347  if (aParams->resample) {
348  /* to visualize wavelength calibration, use the *
349  * pixel table to resample the data onto a 2D image */
350  muse_pixtable *pixtable = muse_pixtable_create(masterimage, tracetable,
351  wavecaltable, NULL);
352  muse_image *resampled = muse_resampling_image(pixtable,
354  1.0, 1.25);
355  muse_pixtable_delete(pixtable);
356  if (resampled) {
357  /* don't want to save QC headers with the resampled image: */
358  cpl_propertylist_erase_regexp(resampled->header, QC_WAVECAL_PREFIX, 0);
359  muse_processing_save_cimage(aProcessing, aParams->nifu, resampled->data,
360  resampled->header, MUSE_TAG_ARC_RESAMP);
361  muse_image_delete(resampled);
362  }
363  } /* if resample */
364 
365  if (aParams->wavemap) {
366  muse_image *image = masterimage ? masterimage
367  : muse_imagelist_get(images, 0);
368  cpl_image *map = muse_wave_map(image, wavecaltable, tracetable);
369  /* simply use the header of the incoming image */
370  muse_processing_save_cimage(aProcessing, aParams->nifu, map, image->header,
371  MUSE_TAG_WAVE_MAP);
372  cpl_image_delete(map);
373  } /* if wavemap */
374 
375  /* clean up */
376  cpl_table_delete(wavecaltable);
377  muse_image_delete(masterimage);
378  muse_imagelist_delete(images);
379  cpl_table_delete(tracetable);
380 
381  return rc; /* can only be 0 or -1 */
382 } /* muse_wavecal_compute() */
int muse_processing_save_cimage(muse_processing *aProcessing, int aIFU, cpl_image *aImage, cpl_propertylist *aHeader, const char *aTag)
Save a computed FITS image to disk.
cpl_table * muse_wave_calib_lampwise(muse_imagelist *aImages, cpl_table *aTrace, cpl_table *aLinelist, muse_wave_params *aParams)
Find wavelength calibration solution using a list of arc images with different lamps.
cpl_table * muse_wave_calib(muse_image *aImage, cpl_table *aTrace, cpl_table *aLinelist, muse_wave_params *aParams)
Find wavelength calibration solution on an arc frame.
muse_image * muse_resampling_image(muse_pixtable *aPixtable, muse_resampling_type aMethod, double aDX, double aDLambda)
Resample a pixel table onto a two-dimensional regular grid.
muse_wave_params * muse_wave_params_new(cpl_propertylist *aHeader)
Allocate a wavelength parameters structure and fill it with defaults.
Structure definition for a collection of muse_images.
void muse_image_delete(muse_image *aImage)
Deallocate memory associated to a muse_image object.
Definition: muse_image.c:85
double fitsigma
Sigma level for iterative rejection of deviant datapoints during the final polynomial wavelength solu...
muse_wave_weighting_type fitweighting
void muse_wave_params_delete(muse_wave_params *aParams)
Deallocate memory associated to a wavelength parameters structure.
double linesigma
Sigma level for iterative rejection of deviant fits for each arc line within each slice...
const char * name
muse_imagelist * muse_basicproc_combine_images_lampwise(muse_processing *aProcessing, unsigned char aIFU, muse_basicproc_params *aBPars, cpl_frameset ***aLabeledFrames)
Combine several images into a lampwise image list.
cpl_image * data
the data extension
Definition: muse_image.h:46
cpl_boolean rflag
double dres
The allowed range of resolutions for pattern matching (of detected arc lines to line list) in fractio...
void muse_imagelist_delete(muse_imagelist *aList)
Free the memory of the MUSE image list.
cpl_table * residuals
cpl_boolean muse_wave_lines_check(muse_table *aTable)
Check that a LINE_CATALOG has the expected format.
muse_basicproc_params * muse_basicproc_params_new(cpl_parameterlist *aParameters, const char *aPrefix)
Create a new structure of basic processing parameters.
muse_image * muse_combine_images(muse_combinepar *aCPars, muse_imagelist *aImages)
Combine several images into one.
Definition: muse_combine.c:741
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:40
muse_wave_weighting_type
Type of weighting to use in the wavelength calibration fit.
void muse_basicproc_params_delete(muse_basicproc_params *aBPars)
Free a structure of basic processing parameters.
cpl_propertylist * header
the FITS header
Definition: muse_image.h:72
unsigned int muse_imagelist_get_size(muse_imagelist *aList)
Return the number of stored images.
cpl_frameset * usedframes
void muse_combinepar_delete(muse_combinepar *aCPars)
Clear the combination parameters.
Definition: muse_combine.c:715
Structure definition of MUSE pixel table.
#define MUSE_WCS_KEYS
regular expression for WCS properties
Definition: muse_wcs.h:48
muse_image * muse_imagelist_get(muse_imagelist *aList, unsigned int aIdx)
Get the muse_image of given list index.
int nifu
IFU to handle. If set to 0, all IFUs are processed serially. If set to -1, all IFUs are processed in ...
Structure containing wavelength calibration parameters.
unsigned short xorder
cpl_table * table
The table.
Definition: muse_table.h:49
muse_table * muse_processing_load_table(muse_processing *aProcessing, const char *aTag, unsigned char aIFU)
Load a MUSE table according to its tag and IFU/channel number.
muse_combinepar * muse_combinepar_new(cpl_parameterlist *aParameters, const char *aPrefix)
Create a new set of combination parameters.
Definition: muse_combine.c:672
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...
cpl_image * muse_wave_map(muse_image *aImage, const cpl_table *aWave, const cpl_table *aTrace)
Write out a wavelength map for visual checks.
int resample
Resample the input arc images onto 2D images for a visual check using tracing and wavelength calibrat...
Structure to store a table together with a property list.
Definition: muse_table.h:43
muse_pixtable * muse_pixtable_create(muse_image *aImage, cpl_table *aTrace, cpl_table *aWave, cpl_table *aGeoTable)
Create the pixel table for one CCD.
void muse_table_delete(muse_table *aTable)
Deallocate memory associated to a muse_table object.
Definition: muse_table.c:80
char * muse_utils_header_get_lamp_names(cpl_propertylist *aHeader, char aSep)
Concatenate names of all active calibration lamps.
Definition: muse_utils.c:989
int yorder
Order of the polynomial used to fit the dispersion relation.
cpl_array * muse_utils_header_get_lamp_numbers(cpl_propertylist *aHeader)
List numbers of all active calibration lamps.
Definition: muse_utils.c:1056
int muse_processing_save_image(muse_processing *aProcessing, int aIFU, muse_image *aImage, const char *aTag)
Save a computed MUSE image to disk.
cpl_table * muse_processing_load_ctable(muse_processing *aProcessing, const char *aTag, unsigned char aIFU)
Load a CPL table according to its tag and IFU/channel number.
int fitweighting
Type of weighting to use in the final polynomial wavelength solution fit, using centroiding error est...
cpl_error_code muse_processing_save_table(muse_processing *aProcessing, int aIFU, void *aTable, cpl_propertylist *aHeader, const char *aTag, muse_table_type aType)
Save a computed table to disk.
Structure of basic processing parameters.
unsigned short yorder
Structure to hold the parameters of the muse_wavecal recipe.
double tolerance
Tolerance for pattern matching (of detected arc lines to line list)
unsigned int size
void muse_pixtable_delete(muse_pixtable *aPixtable)
Deallocate memory associated to a pixel table object.
const char * fitweighting_s
Type of weighting to use in the final polynomial wavelength solution fit, using centroiding error est...
double sigma
Sigma level used to detect arc emission lines above the median background level in the S/N image of t...
cpl_parameterlist * parameters
int xorder
Order of the polynomial for the horizontal curvature within each slice.
cpl_error_code muse_basicproc_qc_saturated(muse_image *aImage, const char *aPrefix)
Add QC parameter about saturated pixels to a muse_image.