MUSE Pipeline Reference Manual  2.5.2
muse_create_sky.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 <string.h>
30 #include <math.h>
31 
32 #include <muse.h>
33 #include "muse_create_sky_z.h"
34 
35 /*---------------------------------------------------------------------------*
36  * Functions code *
37  *---------------------------------------------------------------------------*/
38 static muse_pixtable *
39 muse_create_sky_load_pixtable(muse_processing *aProcessing,
40  muse_create_sky_params_t *aParams)
41 {
42  muse_pixtable *pixtable = NULL;
43 
44  /* sort input pixel tables into different exposures */
45  cpl_table *exposures = muse_processing_sort_exposures(aProcessing);
46  if (!exposures) {
47  cpl_msg_error(__func__, "no science exposures found in input");
48  return NULL;
49  }
50  int nexposures = cpl_table_get_nrow(exposures);
51  if (nexposures != 1) {
52  cpl_msg_error(__func__, "More than one exposure (%i) in sky creation",
53  nexposures);
54  }
55 
56  /* now process all the pixel tables, do it separately for each exposure */
57  int i;
58  for (i = 0; i < nexposures; i++) {
59  cpl_table *thisexp = cpl_table_extract(exposures, i, 1);
61  aParams->lambdamin,
62  aParams->lambdamax);
63  cpl_table_delete(thisexp);
64  if (p == NULL) {
65  muse_pixtable_delete(pixtable);
66  pixtable = NULL;
67  break;
68  }
69  /* erase pre-existing QC parameters */
70  cpl_propertylist_erase_regexp(p->header, "ESO QC ", 0);
71  if (pixtable == NULL) {
72  pixtable = p;
73  } else {
74  cpl_table_insert(pixtable->table, p->table,
75  cpl_table_get_nrow(pixtable->table));
77  }
78  }
79  cpl_table_delete(exposures);
80 
81  if ((pixtable != NULL) && (muse_pixtable_is_skysub(pixtable) == CPL_TRUE)) {
82  cpl_msg_error(__func__, "Pixel table already sky subtracted");
83  muse_pixtable_delete(pixtable);
84  pixtable = NULL;
85  }
86 
87  muse_table *response = muse_processing_load_table(aProcessing,
88  MUSE_TAG_STD_RESPONSE, 0),
89  *telluric = muse_processing_load_table(aProcessing,
90  MUSE_TAG_STD_TELLURIC, 0);
91  cpl_table *extinction = muse_processing_load_ctable(aProcessing,
92  MUSE_TAG_EXTINCT_TABLE, 0);
93 
94  if ((pixtable != NULL) && (response != NULL)) {
95  if (muse_pixtable_is_fluxcal(pixtable) == CPL_TRUE) {
96  cpl_msg_error(__func__,
97  "Pixel table already flux calibrated. Dont specify %s, %s, %s",
98  MUSE_TAG_STD_RESPONSE, MUSE_TAG_EXTINCT_TABLE,
99  MUSE_TAG_STD_TELLURIC);
100  muse_pixtable_delete(pixtable);
101  pixtable = NULL;
102  } else {
103  /* See if we need to revert the flat-field spectrum correction, *
104  * to match pixel table and response curve, before applying it. */
105  muse_postproc_revert_ffspec_maybe(pixtable, response);
106  cpl_error_code rc = muse_flux_calibrate(pixtable, response, extinction,
107  telluric);
108  if (rc != CPL_ERROR_NONE) {
109  cpl_msg_error(__func__, "while muse_flux_calibrate");
110  muse_pixtable_delete(pixtable);
111  pixtable = NULL;
112  }
113  }
114  }
115 
116  muse_table_delete(response);
117  muse_table_delete(telluric);
118  cpl_table_delete(extinction);
119 
120  if ((pixtable != NULL) && (muse_pixtable_is_fluxcal(pixtable) != CPL_TRUE)) {
121  cpl_msg_error(__func__, "Pixel table not flux calibrated");
122  muse_pixtable_delete(pixtable);
123  pixtable = NULL;
124  }
125 
126  if (pixtable != NULL) {
127  cpl_table_and_selected_int(pixtable->table, MUSE_PIXTABLE_DQ,
128  CPL_NOT_EQUAL_TO, EURO3D_GOODPIXEL);
129  cpl_table_erase_selected(pixtable->table);
130 
131  /* do DAR correction for WFM data */
132  if (muse_pfits_get_mode(pixtable->header) < MUSE_MODE_NFM_AO_N) {
133  cpl_msg_debug(__func__, "WFM detected: starting DAR correction");
134  cpl_error_code rc = muse_dar_correct(pixtable, aParams->lambdaref);
135  cpl_msg_debug(__func__, "DAR correction returned rc=%d: %s", rc,
136  rc != CPL_ERROR_NONE ? cpl_error_get_message() : "");
137  } /* if WFM */
138  }
139 
140  return pixtable;
141 }
142 
143 /*----------------------------------------------------------------------------*/
150 /*----------------------------------------------------------------------------*/
151 int
152 muse_create_sky_compute(muse_processing *aProcessing,
153  muse_create_sky_params_t *aParams)
154 {
155  cpl_array *crarray = muse_cplarray_new_from_delimited_string(aParams->crsigma, ",");
156  double crsigmac = 15., /* for cube-based CR rejection */
157  crsigmas = 0.; /* spectrum-based CR rejection */
158  if (cpl_array_get_size(crarray) < 2) {
159  cpl_msg_warning(__func__, "Less than two values given by crsigma "
160  "parameter, using defaults (%.3f.,%.3f)!", crsigmac,
161  crsigmas);
162  } else {
163  crsigmac = cpl_array_get_string(crarray, 0)
164  ? atof(cpl_array_get_string(crarray, 0)) : 15.;
165  crsigmas = cpl_array_get_string(crarray, 1)
166  ? atof(cpl_array_get_string(crarray, 1)) : 0.;
167  }
168  cpl_array_delete(crarray);
169 
170  muse_pixtable *pixtable = muse_create_sky_load_pixtable(aProcessing, aParams);
171  if (pixtable == NULL) {
172  cpl_msg_error(__func__, "Could not load pixel table");
173  return -1;
174  }
175 
176  /*
177  First step: find the regions containing sky:
178  - create a whitelight image
179  - create the mask from it
180  - select pixtable rows containing sky
181  - create spectrum from selected rows
182  */
183  muse_mask *smask = muse_processing_load_mask(aProcessing, MUSE_TAG_SKY_MASK);
184  if (smask) { /* apply existing mask on input pixel table */
185  cpl_table_select_all(pixtable->table);
186  muse_pixtable_and_selected_mask(pixtable, smask, NULL, NULL);
187  muse_mask_delete(smask);
188  }
189 
190  muse_image *whitelight = muse_postproc_whitelight(pixtable, crsigmac);
191  if (whitelight == NULL) {
192  cpl_msg_error(__func__, "Could not create whitelight image");
193  return -1;
194  }
195  muse_processing_save_image(aProcessing, -1, whitelight, MUSE_TAG_SKY_IMAGE);
196 
197  muse_mask *sky_mask = muse_sky_create_skymask(whitelight, aParams->ignore,
198  aParams->fraction,
199  "ESO QC SKY");
200  muse_processing_save_mask(aProcessing, -1, sky_mask, MUSE_TAG_SKY_MASK);
201 
202  cpl_table_select_all(pixtable->table);
203  muse_pixtable_and_selected_mask(pixtable, sky_mask, NULL, NULL);
204  cpl_table_not_selected(pixtable->table);
205  cpl_table_erase_selected(pixtable->table);
206  muse_image_delete(whitelight);
207  muse_mask_delete(sky_mask);
208 
209  /* Apply mask and create spectrum, possibly with one CR-reject iteration */
210  cpl_table *spectrum = muse_resampling_spectrum_iterate(pixtable,
211  aParams->sampling,
212  0., crsigmas, 1);
213  if (spectrum == NULL) {
214  muse_pixtable_delete(pixtable);
215  return -1;
216  }
217  muse_processing_save_table(aProcessing, -1, spectrum, pixtable->header,
218  MUSE_TAG_SKY_SPECTRUM, MUSE_TABLE_TYPE_CPL);
219 
220  cpl_table *lines = muse_sky_lines_load(aProcessing);
221  if (lines != NULL) {
222  double lambda_low = cpl_table_get_column_min(spectrum, "lambda");
223  double lambda_high = cpl_table_get_column_max(spectrum, "lambda");
224  muse_sky_lines_set_range(lines, lambda_low-5, lambda_high+5);
225  }
226 
227  cpl_table *continuum = muse_sky_continuum_load(aProcessing);
228 
229  /*
230  Second step: create master sky, containing line list and
231  continuum. If a continuum was given as calibration frame, we
232  ignore the computed one and replace it by the external one.
233  */
234 
235  cpl_errorstate prestate = cpl_errorstate_get();
236  /*
237  Create the average LSF image for the spectrum of the pixels
238  selected as sky. This is done in the following steps:
239 
240  1. Average all cubes with the pixel count as weights into a single
241  image.
242 
243  2. Fold an additional rectangle to model the sky spectrum binning
244  */
245  muse_lsf_cube **lsfCube = muse_lsf_cube_load_all(aProcessing);
246  if (lsfCube != NULL) {
247  cpl_image *lsfImage = muse_lsf_average_cube_all(lsfCube, pixtable);
248  muse_wcs *lsfWCS = muse_lsf_cube_get_wcs_all(lsfCube);
249  muse_lsf_fold_rectangle(lsfImage, lsfWCS, aParams->sampling);
250 
251  cpl_msg_info(__func__, "Creating master sky spectrum using fits to lines "
252  "(fluxes) and residual continuum");
253 
254  // do the fit, ignoring possible errors
255  muse_sky_lines_fit(spectrum, lines, lsfImage, lsfWCS);
256  if (!cpl_errorstate_is_equal(prestate)) {
257  cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
258  cpl_errorstate_set(prestate);
259  }
260 
261  if (continuum == NULL) {
262  continuum = muse_sky_continuum_create(spectrum, lines, lsfImage, lsfWCS,
263  aParams->csampling);
264  }
265 
266  muse_lsf_cube_delete_all(lsfCube);
267  cpl_image_delete(lsfImage);
268 #ifdef USE_LSF_PARAMS
269  } else {
270  cpl_errorstate_set(prestate);
271  muse_lsf_params **lsfParams = muse_processing_lsf_params_load(aProcessing, 0);
272  if (lsfParams != NULL) { // Old LSF params code
273 
274  // do the fit, ignoring possible errors
275  muse_sky_lines_fit_old(spectrum, lines);
276  if (!cpl_errorstate_is_equal(prestate)) {
277  cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
278  cpl_errorstate_set(prestate);
279  }
280 
281  if (continuum == NULL) {
282  muse_sky_subtract_lines_old(pixtable, lines, lsfParams);
283  continuum = muse_resampling_spectrum(pixtable, aParams->csampling);
284  cpl_table_erase_column(continuum, "stat");
285  cpl_table_erase_column(continuum, "dq");
286  cpl_table_name_column(continuum, "data", "flux");
287  }
288 
289  muse_lsf_params_delete_all(lsfParams);
290  } else {
291  cpl_msg_error(__func__, "Could not load LSF. Continuum is not created.");
292  }
293 #endif
294  }
295  /* add QC parameters for both emission lines and continuum and save them */
296  cpl_propertylist *header = cpl_propertylist_new();
297  muse_sky_qc_lines(header, lines, "ESO QC SKY");
298  muse_sky_lines_save(aProcessing, lines, header);
299  cpl_propertylist_delete(header);
300 
301  header = cpl_propertylist_new();
302  muse_sky_qc_continuum(header, continuum, "ESO QC SKY");
303  muse_sky_save_continuum(aProcessing, continuum, header);
304  cpl_propertylist_delete(header);
305 
306  /* Clean up the local objects. */
307  cpl_table_delete(spectrum);
308  cpl_table_delete(lines);
309  cpl_table_delete(continuum);
310  muse_pixtable_delete(pixtable);
311  return cpl_errorstate_is_equal(prestate) ? 0 : -1;
312 }
313 
cpl_error_code muse_sky_lines_fit(cpl_table *, cpl_table *, cpl_image *, muse_wcs *)
Fit all entries of the pixel table to the master sky.
cpl_table * muse_sky_continuum_create(cpl_table *, cpl_table *, cpl_image *, muse_wcs *, double)
Create a continuum spectrum.
#define MUSE_PIXTABLE_DQ
Definition: muse_pixtable.h:49
void muse_image_delete(muse_image *aImage)
Deallocate memory associated to a muse_image object.
Definition: muse_image.c:85
int muse_processing_save_mask(muse_processing *aProcessing, int aIFU, muse_mask *aMask, const char *aTag)
Save a computed MUSE mask to disk.
A structure containing a spatial two-axis WCS.
Definition: muse_wcs.h:105
cpl_table * muse_resampling_spectrum_iterate(muse_pixtable *aPixtable, double aBinwidth, float aLo, float aHi, unsigned char aIter)
Iteratively resample selected pixels of a pixel table into spectrum.
cpl_error_code muse_sky_lines_save(muse_processing *, const cpl_table *, cpl_propertylist *)
Save sky lines table to file.
void muse_mask_delete(muse_mask *aMask)
Deallocate memory associated to a muse_mask object.
Definition: muse_mask.c:78
cpl_error_code muse_dar_correct(muse_pixtable *aPixtable, double aLambdaRef)
Correct the pixel coordinates of all pixels of a given pixel table for differential atmospheric refra...
Definition: muse_dar.c:139
cpl_error_code muse_postproc_revert_ffspec_maybe(muse_pixtable *aPt, const muse_table *aResponse)
Revert correction of on-sky data with the flat-field spectrum.
cpl_image * muse_lsf_average_cube_all(muse_lsf_cube **aLsfCube, muse_pixtable *aPixtable)
Create an average image from all LSF cubes.
cpl_table * muse_sky_lines_load(muse_processing *)
Load the sky data files.
double csampling
Spectral sampling of the continuum spectrum [Angstrom].
Data cube/stacked image list containing the LSF for one IFU.
Definition: muse_lsf.h:49
void muse_sky_qc_lines(cpl_propertylist *, cpl_table *, const char *)
Fill a header with the QC parameters for the sky lines.
Definition: muse_sky_qc.c:46
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:40
cpl_error_code muse_flux_calibrate(muse_pixtable *aPixtable, const muse_table *aResponse, const cpl_table *aExtinction, const muse_table *aTelluric)
Convert the input pixel table from counts to fluxes.
Definition: muse_flux.c:3094
cpl_table * table
The pixel table.
void muse_lsf_params_delete_all(muse_lsf_params **aParams)
Delete an allocated array of muse_lsf_params structure.
const char * crsigma
Sigma level clipping for cube-based and spectrum-based CR rejection. This has to be a string of two c...
void muse_sky_qc_continuum(cpl_propertylist *, cpl_table *, const char *)
Fill a header with the QC parameters for the sky continuum.
Definition: muse_sky_qc.c:102
cpl_boolean muse_pixtable_is_skysub(muse_pixtable *aPixtable)
Determine whether the pixel table is sky subtracted.
cpl_table * muse_sky_continuum_load(muse_processing *)
Load the sky continuum.
cpl_array * muse_cplarray_new_from_delimited_string(const char *aString, const char *aDelim)
Convert a delimited string into an array of strings.
Structure definition of MUSE pixel table.
cpl_error_code muse_sky_subtract_lines_old(muse_pixtable *aPixtable, cpl_table *aLines, muse_lsf_params **aLsfParams)
Subtract sky lines from a pixtable.
Definition: muse_sky_old.c:532
double ignore
Fraction of the image to be ignored. If an input sky mask is provided, the fraction is applied to the...
cpl_boolean muse_pixtable_is_fluxcal(muse_pixtable *aPixtable)
Determine whether the pixel table is flux calibrated.
muse_lsf_cube ** muse_lsf_cube_load_all(muse_processing *aProcessing)
Load all LSF cubes for all IFUs into an array.
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.
cpl_error_code muse_sky_lines_set_range(cpl_table *, double, double)
Limit the lines in the table to a wavelength range.
cpl_error_code muse_pixtable_and_selected_mask(muse_pixtable *aPixtable, const muse_mask *aMask, const cpl_propertylist *aWCS, const cpl_table *aOffsets)
Select all pixels where the spatial positions are enabled in the given mask.
Structure to store a table together with a property list.
Definition: muse_table.h:43
muse_image * muse_postproc_whitelight(muse_pixtable *aPixtable, double aCRSigma)
Create a white-light image of the field of view.
muse_mask * muse_processing_load_mask(muse_processing *aProcessing, const char *aTag)
Load a mask file and its FITS header.
void muse_table_delete(muse_table *aTable)
Deallocate memory associated to a muse_table object.
Definition: muse_table.c:80
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.
muse_pixtable * muse_pixtable_load_merge_channels(cpl_table *aExposureList, double aLambdaMin, double aLambdaMax)
Load and merge the pixel tables of the 24 MUSE sub-fields.
Handling of "mask" files.
Definition: muse_mask.h:43
void muse_lsf_cube_delete_all(muse_lsf_cube **aLsfCube)
Delete all LSF cubes.
muse_wcs * muse_lsf_cube_get_wcs_all(muse_lsf_cube **aLsfCubes)
Get the world coordinate representative of all LSF cubes.
double lambdamin
Cut off the data below this wavelength after loading the pixel table(s).
muse_mask * muse_sky_create_skymask(muse_image *, double, double, const char *)
Select spaxels to be considered as sky.
double sampling
Spectral sampling of the sky spectrum [Angstrom].
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.
double fraction
Fraction of the image (without the ignored part) to be considered as sky. If an input sky mask is pro...
double lambdamax
Cut off the data above this wavelength after loading the pixel table(s).
cpl_error_code muse_lsf_fold_rectangle(cpl_image *aLsfImage, const muse_wcs *aWCS, double aBinWidth)
Filter an LSF image with a rectangle to model spectrum binning.
double lambdaref
Reference wavelength used for correction of differential atmospheric refraction. The R-band (peak wav...
muse_lsf_params ** muse_processing_lsf_params_load(muse_processing *aProcessing, int aIFU)
Load slice LSF parameters.
cpl_table * muse_resampling_spectrum(muse_pixtable *aPixtable, double aBinwidth)
Resample the selected pixels of a pixel table into a spectrum.
cpl_error_code muse_sky_lines_fit_old(cpl_table *aSpectrum, cpl_table *aLines)
Fit all entries of the pixel table to the master sky.
Definition: muse_sky_old.c:436
cpl_table * muse_processing_sort_exposures(muse_processing *aProcessing)
Sort input frames (containing lists of pixel table filenames) into different exposures.
Structure definition of detector (slice) parameters.
void muse_pixtable_delete(muse_pixtable *aPixtable)
Deallocate memory associated to a pixel table object.
Structure to hold the parameters of the muse_create_sky recipe.
muse_ins_mode muse_pfits_get_mode(const cpl_propertylist *aHeaders)
find out the observation mode
Definition: muse_pfits.c:1352
cpl_propertylist * header
The FITS header.
cpl_error_code muse_sky_save_continuum(muse_processing *, const cpl_table *, cpl_propertylist *)
Save sky continuum table to file.