VISIR Pipeline Reference Manual  4.1.7
visir_spc_phot_ech.c
1 /* $Id: visir_spc_phot_ech.c,v 1.77 2012-08-21 09:56:14 jtaylor Exp $
2  *
3  * This file is part of the VISIR Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: jtaylor $
23  * $Date: 2012-08-21 09:56:14 $
24  * $Revision: 1.77 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 
33 /*-----------------------------------------------------------------------------
34  Includes
35  -----------------------------------------------------------------------------*/
36 
37 #include "visir_recipe.h"
38 #include "visir_spectro.h"
39 #include "visir_spc_distortion.h"
40 #include "visir_spc_photom.h"
41 
42 /*-----------------------------------------------------------------------------
43  Defines
44  -----------------------------------------------------------------------------*/
45 
46 #define RECIPE_STRING "visir_old_spc_phot_ech"
47 
48 /* FITS keys to be loaded for all raw files */
49 #define RECIPE_KEYS_REGEXP_ALL \
50  VISIR_PFITS_REGEXP_IMG_RECOMBINE \
51  "|" VISIR_PFITS_REGEXP_SPC_GET_RES_WL \
52  "|" VISIR_PFITS_REGEXP_SPC_SENSIT
53 
54 /* FITS keys to be loaded for first raw file */
55 #define RECIPE_KEYS_REGEXP \
56  RECIPE_KEYS_REGEXP_ALL \
57  "|" VISIR_PFITS_SPC_PHOT_COPY \
58  "|" VISIR_PFITS_REGEXP_CAPA \
59  "|" VISIR_PFITS_REGEXP_SPC_PHOT_PAF
60 
61 /* FITS keys to be loaded for first raw file, in case WCS is used */
62 #define RECIPE_KEYS_REGEXP_WCS \
63  RECIPE_KEYS_REGEXP \
64  "|" IRPLIB_PFITS_WCS_REGEXP
65 
66 /*-----------------------------------------------------------------------------
67  Private Functions prototypes
68  -----------------------------------------------------------------------------*/
69 
70 static cpl_error_code visir_spc_phot_ech_save(cpl_frameset *,
71  const cpl_parameterlist *,
72  const cpl_propertylist *,
73  const cpl_propertylist *,
74  const cpl_image *,
75  const cpl_image *,
76  const cpl_table *);
77 
78 VISIR_RECIPE_DEFINE(visir_old_spc_phot_ech,
79  VISIR_PARAM_EMIS_TOL |
80  VISIR_PARAM_ZERODIST | VISIR_PARAM_ORDEROFF |
81  VISIR_PARAM_REFINE | VISIR_PARAM_XCORR |
82  VISIR_PARAM_OFFSETS | VISIR_PARAM_OBJECTS |
83  VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
84  VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |
85  VISIR_PARAM_UNION | VISIR_PARAM_REJECT |
86  VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
87  VISIR_PARAM_PLOT | VISIR_PARAM_SLITSKEW |
88  VISIR_PARAM_SPECSKEW | VISIR_PARAM_VERTARC |
89  VISIR_PARAM_REJLEFT | VISIR_PARAM_REJRIGHT |
90  VISIR_PARAM_HORIARC | VISIR_PARAM_FIXCOMBI,
91  "Old DRS detector: Sensitivity computation in echelle spectroscopy",
92  "This recipe estimates the dispersion relation using the "
93  "atmospheric spectrum\n"
94  "in a echelle spectroscopy half-cycle frame.\n"
95  "It also extracts the spectrum of an observed object "
96  "using a combined frame.\n"
97  "Lastly, it computes the spectral photometry of an "
98  "observed standard star.\n"
99  "The files listed in the Set Of Frames (sof-file) "
100  "must be tagged:\n"
101  "VISIR-Echelle-Spectroscopy-file.fits "
102  VISIR_SPC_PHOT_ECH_RAW "\n"
103  "VISIR-Quantum-Efficiency-Calibration-file.fits "
104  VISIR_CALIB_QEFF_SPC "\n"
105  "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits "
106  VISIR_CALIB_LINES_SPC
107  "\n"
108  "VISIR-Spectroscopic-Standard-Star-Catalog.fits "
109  VISIR_CALIB_STDSTAR_SPC "\n"
110  MAN_VISIR_CALIB_BPM_SPC);
111 
112 /*----------------------------------------------------------------------------*/
116 /*----------------------------------------------------------------------------*/
117 
118 /*-----------------------------------------------------------------------------
119  Functions code
120  -----------------------------------------------------------------------------*/
121 
122 /*----------------------------------------------------------------------------*/
129 /*----------------------------------------------------------------------------*/
130 static int visir_old_spc_phot_ech(cpl_frameset * framelist,
131  const cpl_parameterlist * parlist)
132 {
133  irplib_framelist * allframes = NULL;
134  irplib_framelist * rawframes = NULL;
135  cpl_propertylist * qclist = cpl_propertylist_new();
136  cpl_propertylist * paflist = cpl_propertylist_new();
137  const char * badpix;
138  const char * star_cat;
139  const char * spc_cal_qeff;
140  const char * spc_cal_lines;
141  const char * flat;
142  cpl_image ** combinedpair = NULL;
143  cpl_image * combined = NULL;
144  cpl_table * tab = NULL;
145  cpl_image * weight2d = NULL;
146  double wlen, slitw, temp, fwhm;
147  visir_spc_resol resol;
148  visir_spc_config config;
149  cpl_boolean drop_wcs;
150  const char * keys_regexp = "^(" RECIPE_KEYS_REGEXP_WCS
151  "|" VISIR_PFITS_REGEXP_DIT
152  ")$";
153  const char * dit_key = VISIR_PFITS_DOUBLE_DIT;
154 
155  config.recipename = RECIPE_STRING;
156  config.parlist = parlist;
157 
158  /* Retrieve input parameters */
159  config.do_fixcombi =
160  visir_parameterlist_get_bool(parlist, RECIPE_STRING, VISIR_PARAM_FIXCOMBI);
161 
162  config.plot =
163  visir_parameterlist_get_int(parlist, RECIPE_STRING, VISIR_PARAM_PLOT);
164 
165  config.phi =
166  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_SLITSKEW);
167  config.ksi =
168  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_SPECSKEW);
169  config.eps =
170  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_VERTARC);
171  config.delta =
172  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_HORIARC);
173 
174  config.orderoffset =
175  visir_parameterlist_get_int(parlist, RECIPE_STRING, VISIR_PARAM_ORDEROFF);
176 
177  config.phot_emis_tol =
178  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_EMIS_TOL);
179 
180  skip_if (0);
181 
182  /* Identify the RAW and CALIB frames in the input frameset */
183  skip_if (visir_dfs_set_groups(framelist));
184 
185  /* Objects observation */
186  allframes = irplib_framelist_cast(framelist);
187  skip_if(allframes == NULL);
188  rawframes = irplib_framelist_extract(allframes, VISIR_SPC_PHOT_ECH_RAW);
189  skip_if (rawframes == NULL);
190 
191  irplib_framelist_empty(allframes);
192 
193  skip_if(irplib_framelist_load_propertylist(rawframes, 0, 0, keys_regexp,
194  CPL_FALSE));
195 
196  skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, ".*", CPL_FALSE));
197 
198  skip_if(visir_dfs_check_framelist_tag(rawframes));
199 
200  if (cpl_propertylist_has(irplib_framelist_get_propertylist(rawframes, 0),
201  VISIR_PFITS_DOUBLE_SEQ1_DIT))
202  dit_key = VISIR_PFITS_DOUBLE_SEQ1_DIT;
203  skip_if(0);
204 
205  /* Standard star catalog */
206  star_cat = irplib_frameset_find_file(framelist, VISIR_CALIB_STDSTAR_SPC);
207  if (star_cat == NULL) {
208  cpl_msg_error(cpl_func, "The file with the star catalog is missing");
209  skip_if(1);
210  }
211 
212  /* Quantum efficiency file */
213  spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
214 
215  /* Spectral lines calibration file */
216  spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
217 
218  /* Bad pixels calibration file */
219  badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
220 
221  /* Flatfield calibration file */
222  flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
223 
224  /* Get the resolution and central wavelength */
225  resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm, 0);
226 
227  skip_if (0);
228 
229  if (resol != VISIR_SPC_R_GHR) {
230  if (config.orderoffset == 0) {
231  cpl_msg_warning(cpl_func,"Reducing non-HR Grism data as main order");
232  } else {
233  cpl_msg_error(cpl_func, "This recipe cannot reduce non-HR Grism "
234  "data with an order-offset of %d",
235  config.orderoffset);
236  visir_error_set(CPL_ERROR_TYPE_MISMATCH);
237  skip_if(1);
238  }
239  }
240 
241  /* Combine the frames */
242  combinedpair = visir_img_recombine(RECIPE_STRING, parlist, rawframes, badpix,
243  flat, CPL_GEOM_FIRST, &drop_wcs,
244  !config.do_fixcombi, wlen, resol);
245 
246  if (combinedpair == NULL) {
247  cpl_msg_error(cpl_func, "Could not combine the input frames");
248  skip_if (1);
249  }
250 
251  cpl_image_delete(combinedpair[1]);
252  combined = cpl_image_cast(combinedpair[0], CPL_TYPE_DOUBLE);
253  cpl_image_delete(combinedpair[0]);
254 
255  skip_if (0);
256 
257  /* Compute here the sensitivity */
258  cpl_msg_info(cpl_func, "Compute the sensitivity");
259  tab = visir_spc_phot_sensit_from_image(&combined, rawframes, &config,
260  star_cat, spc_cal_lines,
261  spc_cal_qeff, &weight2d, qclist,
262  CPL_TRUE, wlen, slitw, temp, fwhm,
263  resol, dit_key);
264 
265  if (tab == NULL) {
266  cpl_msg_error(cpl_func, "Cannot compute sensitivity: '%s' in %s",
267  cpl_error_get_message(), cpl_error_get_where());
268  skip_if(1);
269  }
270 
271  bug_if (visir_spectro_qc(qclist, paflist, drop_wcs, rawframes,
272  "^(" VISIR_PFITS_SPC_PHOT_COPY ")$",
273  "^(" VISIR_PFITS_REGEXP_SPC_PHOT_PAF ")$"));
274  irplib_framelist_empty(rawframes);
275 
276  /* Save the results */
277  cpl_msg_info(cpl_func, "Save the results");
278 
279  /* PRO.CATG */
280  bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
281  VISIR_SPC_PHOT_ECH_COMBINED_PROCATG));
282 
283  skip_if (visir_spc_phot_ech_save(framelist, parlist, qclist, paflist,
284  combined, weight2d, tab));
285 
286  end_skip;
287 
288  cpl_propertylist_delete(qclist);
289  cpl_propertylist_delete(paflist);
290  irplib_framelist_delete(allframes);
291  irplib_framelist_delete(rawframes);
292  cpl_image_delete(combined);
293  cpl_table_delete(tab);
294  cpl_image_delete(weight2d);
295 
296  cpl_free(combinedpair);
297 
298  return cpl_error_get_code();
299 }
300 
301 /*----------------------------------------------------------------------------*/
313 /*----------------------------------------------------------------------------*/
314 static cpl_error_code visir_spc_phot_ech_save(cpl_frameset * set,
315  const cpl_parameterlist * parlist,
316  const cpl_propertylist * qclist,
317  const cpl_propertylist * paflist,
318  const cpl_image *combined,
319  const cpl_image *weight2d,
320  const cpl_table * table)
321 {
322  bug_if (0);
323 
324  /* THE TABLE */
325  skip_if (irplib_dfs_save_table(set, parlist, set, table, NULL, RECIPE_STRING,
326  VISIR_SPC_PHOT_ECH_TAB_PROCATG,
327  qclist, NULL, visir_pipe_id,
328  RECIPE_STRING "_tab" CPL_DFS_FITS));
329 
330  /* THE COMBINED IMAGE */
331  skip_if (irplib_dfs_save_image(set, parlist, set, combined, CPL_BPP_IEEE_FLOAT,
332  RECIPE_STRING, VISIR_SPC_PHOT_ECH_COMBINED_PROCATG,
333  qclist, NULL, visir_pipe_id,
334  RECIPE_STRING CPL_DFS_FITS));
335 
336  /* THE WEIGHT IMAGE */
337  skip_if (irplib_dfs_save_image(set, parlist, set, weight2d, CPL_BPP_IEEE_FLOAT,
338  RECIPE_STRING, VISIR_SPC_PHOT_ECH_WEIGHT_PROCATG,
339  qclist, NULL, visir_pipe_id,
340  RECIPE_STRING "_weight" CPL_DFS_FITS));
341 
342 #ifdef VISIR_SAVE_PAF
343  /* THE PAF FILE FOR QC PARAMETERS */
344  skip_if (cpl_dfs_save_paf("VISIR", RECIPE_STRING, paflist,
345  RECIPE_STRING CPL_DFS_PAF));
346 #else
347  bug_if(paflist == NULL);
348 #endif
349 
350  end_skip;
351 
352  return cpl_error_get_code();
353 
354 }
double visir_parameterlist_get_double(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR parameter of type double.
cpl_error_code visir_dfs_check_framelist_tag(const irplib_framelist *self)
Check the tags in a frameset (group raw only)
Definition: visir_dfs.c:218
cpl_error_code irplib_dfs_save_table(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_table *table, const cpl_propertylist *tablelist, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save a table as a DFS-compliant pipeline product.
Definition: irplib_utils.c:335
int visir_parameterlist_get_int(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR integer parameter.
cpl_boolean visir_parameterlist_get_bool(const cpl_parameterlist *self, const char *recipe, visir_parameter bitmask)
Retrieve the value of a VISIR boolean parameter.
void irplib_framelist_empty(irplib_framelist *self)
Erase all frames from a framelist.
cpl_error_code irplib_dfs_save_image(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_image *image, cpl_type_bpp bpp, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save an image as a DFS-compliant pipeline product.
Definition: irplib_utils.c:192
cpl_error_code irplib_framelist_load_propertylist_all(irplib_framelist *self, int ind, const char *regexp, cpl_boolean invert)
Load the propertylists of all frames in the framelist.
irplib_framelist * irplib_framelist_extract(const irplib_framelist *self, const char *tag)
Extract the frames with the given tag from a framelist.
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: visir_dfs.c:72
cpl_table * visir_spc_phot_sensit_from_image(cpl_image **combined, const irplib_framelist *rawframes, const visir_spc_config *pconfig, const char *star_cat, const char *spc_cal_lines, const char *spc_cal_qeff, cpl_image **pweight2d, cpl_propertylist *qclist, cpl_boolean do_ech, double wlen, double slitw, double temp, double fwhm, const visir_spc_resol resol, const char *dit_key)
Extract spectrum from image and compute sensitivity.
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
const char * irplib_frameset_find_file(const cpl_frameset *self, const char *tag)
Find the filename with the given tag in a frame set.
cpl_propertylist * irplib_framelist_get_propertylist(irplib_framelist *self, int pos)
Get the propertylist of the specified frame in the framelist.
cpl_error_code irplib_framelist_load_propertylist(irplib_framelist *self, int pos, int ind, const char *regexp, cpl_boolean invert)
Load the propertylist of the specified frame in the framelist.
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.
cpl_image ** visir_img_recombine(const char *recipename, const cpl_parameterlist *parlist, const irplib_framelist *rawframes, const char *badpix, const char *flat, cpl_geom_combine combine_mode, cpl_boolean *pdid_resize, cpl_boolean do_spc_fix, double wlen, visir_spc_resol resol)
The VISIR imaging combination using cross correlation.