MUSE Pipeline Reference Manual  2.5.2
muse_scipost_raman.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) 2017-2018 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 <muse.h>
30 #include "muse_scipost_raman_z.h"
31 #include <string.h>
32 
33 
34 /*----------------------------------------------------------------------------*/
42 /* copied & slightly modified from muse_create_sky.c */
43 /*----------------------------------------------------------------------------*/
44 static muse_pixtable *
45 muse_raman_load_pixtable(muse_processing *aProcessing,
47 {
48  muse_pixtable *pixtable = NULL;
49 
50  /* sort input pixel tables into different exposures */
51  cpl_table *exposures = muse_processing_sort_exposures(aProcessing);
52  if (!exposures) {
53  cpl_msg_error(__func__, "no science exposures found in input");
54  return NULL;
55  }
56  int nexposures = cpl_table_get_nrow(exposures);
57  if (nexposures != 1) {
58  cpl_msg_error(__func__, "More than one exposure (%i) in raman",
59  nexposures);
60  }
61 
62  /* now process all the pixel tables, do it separately for each exposure */
63  int i;
64  for (i = 0; i < nexposures; i++) {
65  cpl_table *thisexp = cpl_table_extract(exposures, i, 1);
67  aParams->lambdamin,
68  aParams->lambdamax);
69  cpl_table_delete(thisexp);
70  if (p == NULL) {
71  muse_pixtable_delete(pixtable);
72  pixtable = NULL;
73  break;
74  }
75  /* erase pre-existing QC parameters */
76  cpl_propertylist_erase_regexp(p->header, "ESO QC ", 0);
77  if (pixtable == NULL) {
78  pixtable = p;
79  } else {
80  cpl_table_insert(pixtable->table, p->table,
81  cpl_table_get_nrow(pixtable->table));
83  }
84  }
85  cpl_table_delete(exposures);
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) {
121  cpl_table_and_selected_int(pixtable->table, MUSE_PIXTABLE_DQ,
122  CPL_NOT_EQUAL_TO, EURO3D_GOODPIXEL);
123  cpl_table_erase_selected(pixtable->table);
124 
125  /* do DAR correction for WFM data */
126  if (muse_pfits_get_mode(pixtable->header) < MUSE_MODE_NFM_AO_N) {
127  cpl_msg_debug(__func__, "WFM detected: starting DAR correction");
128  cpl_error_code rc = muse_dar_correct(pixtable, aParams->lambdaref);
129  cpl_msg_debug(__func__, "DAR correction returned rc=%d: %s", rc,
130  rc != CPL_ERROR_NONE ? cpl_error_get_message() : "");
131  } /* if WFM */
132  }
133 
134  return pixtable;
135 }
136 
137 /*----------------------------------------------------------------------------*/
145 /*----------------------------------------------------------------------------*/
146 int
147 muse_scipost_raman_compute(muse_processing *aProcessing,
149 {
150  muse_pixtable *pixtable = muse_raman_load_pixtable(aProcessing, aParams);
151  if (pixtable == NULL) {
152  cpl_msg_error(__func__, "Could not load pixel table");
153  return -1;
154  }
155 
156  /* load all necessary calibrations and optional inputs *
157  * and store them in a post-processing structure */
159  = muse_postproc_properties_new(MUSE_POSTPROC_SCIPOST);
160  prop->raman_lines = muse_raman_lines_load(aProcessing);
161  prop->lsf_cube = muse_lsf_cube_load_all(aProcessing);
162  prop->sky_mask = muse_processing_load_mask(aProcessing, MUSE_TAG_SKY_MASK);
163  /* parameters relevant for Raman processing */
164  prop->skymodel_params.fraction = aParams->fraction;
165  prop->skymodel_params.ignore = aParams->ignore;
166  prop->skymodel_params.crsigmac = aParams->crsigma;
167  prop->raman_width = aParams->width;
168 
169  muse_datacube *ramancube = NULL;
170  muse_postproc_correct_raman(pixtable, prop, NULL/*XXX*/, &ramancube);
172 
173  cpl_msg_info(__func__, "Write output pixel table");
174  muse_processing_save_table(aProcessing, -1, pixtable, NULL,
175  MUSE_TAG_PIXTABLE_REDUCED,
177  muse_pixtable_delete(pixtable);
178 
179  cpl_msg_info(__func__, "Write other output file");
180  muse_processing_save_cube(aProcessing, -1, ramancube,
181  MUSE_TAG_RAMAN_IMAGES, MUSE_CUBE_TYPE_FITS);
182  muse_datacube_delete(ramancube);
183 
184  return cpl_error_get_code();
185 }
#define MUSE_PIXTABLE_DQ
Definition: muse_pixtable.h:49
Structure definition of a MUSE datacube.
Definition: muse_datacube.h:48
muse_postproc_properties * muse_postproc_properties_new(muse_postproc_type aType)
Create a post-processing properties object.
Definition: muse_postproc.c:76
double width
Wavelength range around Raman lines [Angstrom].
double lambdaref
Reference wavelength used for correction of differential atmospheric refraction. The R-band (peak wav...
cpl_error_code muse_postproc_correct_raman(muse_pixtable *aPT, muse_postproc_properties *aProp, const cpl_table *aOffsets, muse_datacube **aRamanCube)
Set up and run Raman-light removal.
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_table * muse_raman_lines_load(muse_processing *aProcessing)
Load the raman lines file.
Definition: muse_raman.c:48
double crsigma
Sigma level clipping for cube-based CR rejection (using "median", see muse_scipost). It can be switched off, by passing zero or a negative value.
void muse_datacube_delete(muse_datacube *aCube)
Deallocate memory associated to a muse_datacube object.
double fraction
Fraction of the image (without the ignored part) to be considered as sky. If an input sky mask is pro...
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.
Structure definition of MUSE pixel table.
cpl_error_code muse_processing_save_cube(muse_processing *aProcessing, int aIFU, void *aCube, const char *aTag, muse_cube_type aType)
Save a MUSE datacube to disk.
double ignore
Lowest fraction of the image to be ignored. If an input sky mask is provided, the fraction is applied...
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.
Structure definition of the post-processing properties.
Definition: muse_postproc.h:92
void muse_postproc_properties_delete(muse_postproc_properties *aProp)
Free memory taken by a post-processing properties object and all its components.
double lambdamin
Cut off the data below this wavelength after loading the pixel table(s).
Structure to store a table together with a property list.
Definition: muse_table.h:43
muse_mask * muse_processing_load_mask(muse_processing *aProcessing, const char *aTag)
Load a mask file and its FITS header.
Structure to hold the parameters of the muse_scipost_raman recipe.
void muse_table_delete(muse_table *aTable)
Deallocate memory associated to a muse_table object.
Definition: muse_table.c:80
muse_lsf_cube ** lsf_cube
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.
double lambdamax
Cut off the data above this wavelength after loading the pixel table(s).
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.
muse_sky_params skymodel_params
cpl_table * muse_processing_sort_exposures(muse_processing *aProcessing)
Sort input frames (containing lists of pixel table filenames) into different exposures.
void muse_pixtable_delete(muse_pixtable *aPixtable)
Deallocate memory associated to a pixel table object.
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.