MUSE Pipeline Reference Manual  1.0.2
muse_sky_common.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) 2008-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 <math.h>
30 
31 #include "muse_sky.h"
32 
33 #include "muse_cplwrappers.h"
34 #include "muse_data_format_z.h"
35 #include "muse_quality.h"
36 #include "muse_mask.h"
37 #include "muse_pfits.h"
38 
39 /*----------------------------------------------------------------------------*
40  * Special variable types *
41  *----------------------------------------------------------------------------*/
45 /*----------------------------------------------------------------------------*/
54 /*----------------------------------------------------------------------------*/
56  {"lambda", CPL_TYPE_DOUBLE, "Angstrom", "%7.2f", "wavelength", CPL_TRUE},
57  {"flux", CPL_TYPE_DOUBLE, "erg/(s cm^2 arcsec^2)", "%e", "Flux", CPL_TRUE},
58  { NULL, 0, NULL, NULL, NULL, CPL_FALSE }
59 };
60 
61 /*----------------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------------*/
69 cpl_table *
70 muse_sky_spectrum_from_cube(muse_datacube *aCube, const cpl_mask *aMask) {
71  unsigned count = cpl_imagelist_get_size(aCube->data);
72  cpl_table *spectrum = muse_cpltable_new(muse_dataspectrum_def, count);
73  double crval3 = cpl_propertylist_get_float(aCube->header, "CRVAL3");
74  double crpix3 = cpl_propertylist_get_float(aCube->header, "CRPIX3");
75  double cdelt3 = cpl_propertylist_get_float(aCube->header, "CD3_3");
76  unsigned i;
77  cpl_mask *nmask = cpl_mask_duplicate(aMask);
78  cpl_mask_not(nmask);
79  for (i = 0; i < count; i++) {
80  cpl_table_set(spectrum, "lambda", i, crval3 + (i + 1 - crpix3) * cdelt3);
81 
82  cpl_image *img = cpl_imagelist_get(aCube->data, i);
83  cpl_mask *bpm = cpl_image_get_bpm(img);
84  cpl_mask_or(bpm, nmask);
85  if (aCube->dq != NULL) {
86  cpl_image *dq = cpl_imagelist_get(aCube->dq, i);
87  cpl_mask *dq_mask = cpl_mask_threshold_image_create(dq, -0.5, 0.5);
88  cpl_mask_not(dq_mask);
89  cpl_mask_or(bpm, dq_mask);
90  cpl_mask_delete(dq_mask);
91  }
92  double dev = cpl_image_get_stdev(img);
93  double mean = cpl_image_get_mean(img);
94  cpl_table_set(spectrum, "data", i, mean);
95  cpl_table_set(spectrum, "stat", i, dev / sqrt(cpl_mask_count(bpm)));
96  cpl_table_set(spectrum, "dq", i, (cpl_mask_count(bpm) < 3));
97  }
98  cpl_mask_delete(nmask);
99 
100  return spectrum;
101 }
102 
103 /*----------------------------------------------------------------------------*/
117 /*----------------------------------------------------------------------------*/
118 muse_mask *
119 muse_sky_create_skymask(muse_image *aImage, double aFraction,
120  const char *aQCPrefix)
121 {
122  cpl_ensure(aImage, CPL_ERROR_NULL_INPUT, NULL);
123 
124  /* do statistics */
126  double t0 = cpl_image_get_min(aImage->data);
127  double t1 = muse_cplimage_get_percentile(aImage->data, aFraction);
128  cpl_msg_info(__func__, "Creating sky mask for pixels between minimum (%g) and"
129  " threshold (%g)", t0, t1);
130 
131  /* create output mask with pixels between min and threshold */
132  muse_mask *selectedRegions = muse_mask_new();
133  selectedRegions->mask = cpl_mask_threshold_image_create(aImage->data, t0, t1);
134  cpl_mask_not(selectedRegions->mask);
135  cpl_mask_or(selectedRegions->mask, cpl_image_get_bpm(aImage->data));
136  cpl_mask_not(selectedRegions->mask);
137 
138  /* add threshold as QC parameter */
139  selectedRegions->header = cpl_propertylist_duplicate(aImage->header);
140  char keyword[KEYWORD_LENGTH];
141  snprintf(keyword, KEYWORD_LENGTH, "%s THRESHOLD", aQCPrefix);
142  cpl_propertylist_append_double(selectedRegions->header, keyword, t1);
143 
144  return selectedRegions;
145 }
146 
147 /*----------------------------------------------------------------------------*/
166 /*----------------------------------------------------------------------------*/
167 cpl_array *
168 muse_sky_apply_lsf(const cpl_array *aLambda, const cpl_table *aLines,
169  const muse_lsf_params *aLsf)
170 {
171  cpl_ensure(aLambda, CPL_ERROR_NULL_INPUT, NULL);
172  cpl_ensure(aLsf, CPL_ERROR_NULL_INPUT, NULL);
173 
174  cpl_array *linesLambda = muse_cpltable_extract_column((cpl_table *)aLines,
175  "lambda");
176  cpl_array *linesFlux = muse_cpltable_extract_column((cpl_table *)aLines,
177  "flux");
178  cpl_array *spectrum
179  = muse_lsf_spectrum_get_lines(aLambda, linesLambda, linesFlux, aLsf);
180  cpl_array_unwrap(linesLambda);
181  cpl_array_unwrap(linesFlux);
182 
183  cpl_array *sensitivity = cpl_array_duplicate(aLambda);
184  cpl_array_subtract_scalar(sensitivity, aLsf->lambda_ref);
185  muse_cplarray_poly1d(sensitivity, aLsf->sensitivity);
186 
187  cpl_array_multiply(spectrum, sensitivity);
188 
189  cpl_array_delete(sensitivity);
190 
191  return spectrum;
192 }
193 
194 /*----------------------------------------------------------------------------*/
206 /*----------------------------------------------------------------------------*/
207 void
208 muse_sky_mark_cosmic(cpl_table *aSpectrum, muse_pixtable *aPixtable) {
209  float *lbda = cpl_table_get_data_float(aPixtable->table, MUSE_PIXTABLE_LAMBDA);
210  float *data = cpl_table_get_data_float(aPixtable->table, MUSE_PIXTABLE_DATA);
211  int *dq = cpl_table_get_data_int(aPixtable->table, MUSE_PIXTABLE_DQ);
212 
213  cpl_size spec_nrows = cpl_table_get_nrow(aSpectrum);
214  double *spec_data = cpl_table_get_data_double(aSpectrum, "data");
215  double *spec_stat = cpl_table_get_data_double(aSpectrum, "stat");
216  double *spec_std = cpl_malloc(spec_nrows * sizeof(double));
217  cpl_size i_spec;
218  for (i_spec = 0; i_spec < spec_nrows; i_spec++) {
219  spec_std[i_spec] = sqrt(spec_stat[i_spec]);
220  }
221 
222  /* loop through all selected pixel table rows */
223  cpl_array *asel = cpl_table_where_selected(aPixtable->table);
224  const cpl_size *sel = cpl_array_get_data_cplsize_const(asel);
225  cpl_size isel, nsel = cpl_array_get_size(asel);
226  cpl_size n_cosmic = 0;
227  for (isel = 0; isel < nsel; isel++) {
228  cpl_size i_row = sel[isel];
229  if ((dq[i_row] != EURO3D_GOODPIXEL)) {
230  continue;
231  }
232  double l = lbda[i_row];
233  i_spec = muse_cpltable_find_sorted(aSpectrum, "lambda", l);
234  if ((i_spec < spec_nrows - 1) && (spec_data[i_spec] < spec_data[i_spec+1])) {
235  i_spec++;
236  }
237  if (spec_data[i_spec] + 5 * spec_std[i_spec] < data[i_row]) {
238  dq[i_row] = EURO3D_COSMICRAY;
239  n_cosmic++;
240  }
241  }
242  cpl_msg_info(__func__, "Marked %"CPL_SIZE_FORMAT" pixels of %"CPL_SIZE_FORMAT
243  " as cosmic", n_cosmic, nsel);
244 
245  cpl_array_delete(asel);
246  cpl_free(spec_std);
247 }
248 
249 /*----------------------------------------------------------------------------*/
262 /*----------------------------------------------------------------------------*/
263 cpl_error_code
265  const cpl_table *aContinuum, cpl_propertylist *aHeader)
266 {
267  cpl_ensure_code(aProcessing && aContinuum && aHeader, CPL_ERROR_NULL_INPUT);
268  cpl_frame *frame = muse_processing_new_frame(aProcessing, -1, aHeader,
269  MUSE_TAG_SKY_CONT,
270  CPL_FRAME_TYPE_TABLE);
271  cpl_ensure_code(frame, CPL_ERROR_ILLEGAL_INPUT);
272  const char *filename = cpl_frame_get_filename(frame);
273  cpl_error_code rc = cpl_propertylist_save(aHeader, filename, CPL_IO_CREATE);
274  rc = muse_cpltable_append_file(aContinuum, filename,
275  "CONTINUUM", muse_fluxspectrum_def);
276  if (rc == CPL_ERROR_NONE) {
277  cpl_frameset_insert(aProcessing->outframes, frame);
278  } else {
279  cpl_frame_delete(frame);
280  }
281  return rc;
282 } /* muse_sky_save_continuum() */
283 
Structure definition of a MUSE datacube.
Definition: muse_datacube.h:47
cpl_image * data
the data extension
Definition: muse_image.h:46
cpl_table * muse_sky_spectrum_from_cube(muse_datacube *aCube, const cpl_mask *aMask)
Create a spectrum out of a cube by applying a mask.
const muse_cpltable_def muse_dataspectrum_def[]
muse_mask * muse_mask_new(void)
Allocate memory for a new muse object.
Definition: muse_mask.c:51
muse_mask * muse_sky_create_skymask(muse_image *aImage, double aFraction, const char *aQCPrefix)
Select spaxels to be considered as sky.
cpl_error_code muse_cplarray_poly1d(cpl_array *aArray, const cpl_array *aCoeff)
Apply a polynomial to an array.
Structure definition of MUSE three extension FITS file.
Definition: muse_image.h:40
cpl_table * table
The pixel table.
cpl_propertylist * header
the FITS header
Definition: muse_image.h:72
cpl_array * muse_lsf_spectrum_get_lines(const cpl_array *aLambda, const cpl_array *aLinesLambda, const cpl_array *aLinesFlux, const muse_lsf_params *aLsf)
Set the lines spectrum from the lines table and the detector LSF.
void muse_sky_mark_cosmic(cpl_table *aSpectrum, muse_pixtable *aPixtable)
Mark all pixel above a certain limit as COSMIC.
cpl_table * muse_cpltable_new(const muse_cpltable_def *aDef, cpl_size aLength)
Create an empty table according to the specified definition.
cpl_array * sensitivity
Relative detector sensitivity parametrization.
Definition: muse_lsf.h:58
Structure definition of MUSE pixel table.
cpl_frame * muse_processing_new_frame(muse_processing *aProcessing, int aIFU, cpl_propertylist *aHeader, const char *aTag, cpl_frame_type aType)
Create a new frame for a result file.
cpl_array * muse_cpltable_extract_column(cpl_table *aTable, const char *aColumn)
Create an array from a section of a column.
cpl_frameset * outframes
double muse_cplimage_get_percentile(const cpl_image *aImage, double aFraction)
Get the percentile of an image.
cpl_imagelist * data
the cube containing the actual data values
Definition: muse_datacube.h:75
const muse_cpltable_def muse_fluxspectrum_def[]
Definition of the flux spectrum table structure.
cpl_imagelist * dq
the optional cube containing the bad pixel status
Definition: muse_datacube.h:80
cpl_size muse_cpltable_find_sorted(const cpl_table *aTable, const char *aColumn, double aValue)
Find a row in a table.
cpl_propertylist * header
the FITS header
Definition: muse_datacube.h:56
double lambda_ref
Reference wavelength for polynomial parametrizations.
Definition: muse_lsf.h:56
Handling of "mask" files.
Definition: muse_mask.h:42
cpl_error_code muse_image_reject_from_dq(muse_image *aImage)
Reject pixels of a muse_image depending on its DQ data.
Definition: muse_image.c:856
cpl_propertylist * header
the FITS header
Definition: muse_mask.h:55
Definition of a cpl table structure.
cpl_array * muse_sky_apply_lsf(const cpl_array *aLambda, const cpl_table *aLines, const muse_lsf_params *aLsf)
Apply the LSF parameters to a spectrum.
cpl_mask * mask
The mask data.
Definition: muse_mask.h:48
Structure definition of detector (slice) parameters.
Definition: muse_lsf.h:50
cpl_error_code muse_cpltable_append_file(const cpl_table *aTable, const char *aFile, const char *aExtension, const muse_cpltable_def aDefinition[])
Save a table to disk (into a FITS extension)
cpl_error_code muse_sky_save_continuum(muse_processing *aProcessing, const cpl_table *aContinuum, cpl_propertylist *aHeader)
Save sky continuum table to file.