VISIR Pipeline Reference Manual  4.1.0
visir_spc_phot.c
1 /* $Id: visir_spc_phot.c,v 1.149 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.149 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include "visir_recipe.h"
37 #include "visir_spectro.h"
38 #include "visir_spc_distortion.h"
39 #include "visir_spc_photom.h"
40 
41 
42 
43 /*-----------------------------------------------------------------------------
44  Defines
45  -----------------------------------------------------------------------------*/
46 
47 #define RECIPE_STRING "visir_old_spc_phot"
48 
49 /* FITS keys to be loaded for all raw files */
50 #define RECIPE_KEYS_REGEXP_ALL \
51  VISIR_PFITS_REGEXP_IMG_RECOMBINE \
52  "|" VISIR_PFITS_REGEXP_SPC_GET_RES_WL \
53  "|" VISIR_PFITS_REGEXP_SPC_SENSIT
54 
55 /* FITS keys to be loaded for first raw file */
56 #define RECIPE_KEYS_REGEXP \
57  RECIPE_KEYS_REGEXP_ALL \
58  "|" VISIR_PFITS_SPC_PHOT_COPY \
59  "|" VISIR_PFITS_REGEXP_CAPA \
60  "|" VISIR_PFITS_REGEXP_SPC_PHOT_PAF
61 
62 /* FITS keys to be loaded for first raw file, in case WCS is used */
63 #define RECIPE_KEYS_REGEXP_WCS \
64  RECIPE_KEYS_REGEXP \
65  "|" IRPLIB_PFITS_WCS_REGEXP
66 
67 /*-----------------------------------------------------------------------------
68  Private Functions prototypes
69  -----------------------------------------------------------------------------*/
70 static cpl_error_code visir_spc_phot_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,
79  VISIR_PARAM_EMIS_TOL |
80  VISIR_PARAM_REFINE | VISIR_PARAM_XCORR |
81  VISIR_PARAM_OFFSETS | VISIR_PARAM_OBJECTS |
82  VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
83  VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE |
84  VISIR_PARAM_UNION | VISIR_PARAM_REJECT |
85  VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
86  VISIR_PARAM_PLOT | VISIR_PARAM_SLITSKEW |
87  VISIR_PARAM_SPECSKEW | VISIR_PARAM_VERTARC |
88  VISIR_PARAM_REJLEFT | VISIR_PARAM_REJRIGHT |
89  VISIR_PARAM_HORIARC | VISIR_PARAM_FIXCOMBI,
90  "Old DRS detector: Sensitivity computation in spectroscopy",
91  "This recipe estimates the dispersion relation using the "
92  "atmospheric spectrum\n"
93  "in a long-slit spectroscopy half-cycle frame.\n"
94  "It also extracts the spectrum of an observed object "
95  "using a combined frame.\n"
96  "Lastly, it computes the spectral photometry of an "
97  "observed standard star.\n"
98  "The files listed in the Set Of Frames (sof-file) "
99  "must be tagged:\n"
100  "VISIR-Long-Slit-Spectroscopy-file.fits "
101  VISIR_SPC_PHOT_RAW "\n"
102  "VISIR-Quantum-Efficiency-Calibration-file.fits "
103  VISIR_CALIB_QEFF_SPC "\n"
104  "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits "
105  VISIR_CALIB_LINES_SPC
106  "\n"
107  "VISIR-Spectroscopic-Standard-Star-Catalog.fits "
108  VISIR_CALIB_STDSTAR_SPC "\n"
109  MAN_VISIR_CALIB_BPM_SPC);
110 
111 /*----------------------------------------------------------------------------*/
115 /*----------------------------------------------------------------------------*/
116 
117 /*-----------------------------------------------------------------------------
118  Functions code
119  -----------------------------------------------------------------------------*/
120 
121 /*----------------------------------------------------------------------------*/
128 /*----------------------------------------------------------------------------*/
129 static int visir_old_spc_phot(cpl_frameset * framelist,
130  const cpl_parameterlist * parlist)
131 {
132  irplib_framelist * allframes = NULL;
133  irplib_framelist * rawframes = NULL;
134  cpl_propertylist * qclist = cpl_propertylist_new();
135  cpl_propertylist * paflist = cpl_propertylist_new();
136  const char * badpix;
137  const char * star_cat;
138  const char * spc_cal_qeff;
139  const char * spc_cal_lines;
140  const char * flat;
141  cpl_image ** combinedpair = NULL;
142  cpl_image * combined = NULL;
143  cpl_table * tab = NULL;
144  cpl_image * weight2d = NULL;
145  double wlen, slitw, temp, fwhm;
146  visir_spc_resol resol;
147  visir_spc_config config;
148  cpl_boolean drop_wcs;
149  const char * keys_regexp = "^(" RECIPE_KEYS_REGEXP_WCS
150  "|" VISIR_PFITS_REGEXP_DIT
151  ")$";
152  const char * dit_key = VISIR_PFITS_DOUBLE_DIT;
153 
154 
155  config.recipename = RECIPE_STRING;
156  config.parlist = parlist;
157  config.orderoffset = 0;
158 
159  /* Retrieve input parameters */
160  config.do_fixcombi =
161  visir_parameterlist_get_bool(parlist, RECIPE_STRING, VISIR_PARAM_FIXCOMBI);
162 
163  config.plot =
164  visir_parameterlist_get_int(parlist, RECIPE_STRING, VISIR_PARAM_PLOT);
165 
166  config.phi =
167  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_SLITSKEW);
168  config.ksi =
169  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_SPECSKEW);
170  config.eps =
171  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_VERTARC);
172  config.delta =
173  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_HORIARC);
174 
175  config.phot_emis_tol =
176  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_EMIS_TOL);
177 
178  skip_if (0);
179 
180  /* Identify the RAW and CALIB frames in the input frameset */
181  skip_if (visir_dfs_set_groups(framelist));
182 
183  /* Objects observation */
184  allframes = irplib_framelist_cast(framelist);
185  skip_if(allframes == NULL);
186  rawframes = irplib_framelist_extract(allframes, VISIR_SPC_PHOT_RAW);
187  skip_if (rawframes == NULL);
188 
189  irplib_framelist_empty(allframes);
190 
191  skip_if(irplib_framelist_load_propertylist(rawframes, 0, 0, keys_regexp,
192  CPL_FALSE));
193 
194  skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, "^("
195  RECIPE_KEYS_REGEXP_ALL
196  "|" VISIR_PFITS_REGEXP_DIT
197  "|ESO DRS .*"
198  ")$", CPL_FALSE));
199 
200  skip_if(visir_dfs_check_framelist_tag(rawframes));
201 
202  if (cpl_propertylist_has(irplib_framelist_get_propertylist(rawframes, 0),
203  VISIR_PFITS_DOUBLE_SEQ1_DIT))
204  dit_key = VISIR_PFITS_DOUBLE_SEQ1_DIT;
205  skip_if(0);
206 
207  /* Standard star catalog */
208  star_cat = irplib_frameset_find_file(framelist, VISIR_CALIB_STDSTAR_SPC);
209  if (star_cat == NULL) {
210  cpl_msg_error(cpl_func, "The file with the star catalog is missing");
211  skip_if(1);
212  }
213 
214  visir_data_type data_type;
215  const cpl_frame * frm = irplib_framelist_get_const(rawframes, 0);
216  cpl_propertylist * plist = irplib_framelist_get_propertylist(rawframes, 0);
217  skip_if(visir_get_data_type(frm, plist, &data_type, NULL));
218 
219  /* Quantum efficiency file */
220  spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
221 
222  /* Spectral lines calibration file */
223  spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
224 
225  /* Bad pixels calibration file */
226  badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
227 
228  /* Flatfield calibration file */
229  flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
230 
231  /* Get the resolution and central wavelength */
232  resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm,
233  visir_data_is_aqu(data_type));
234 
235  skip_if (0);
236 
237  if (resol == VISIR_SPC_R_GHR) {
238  cpl_msg_error(cpl_func, "This recipe cannot reduce HR Grism data");
239  visir_error_set(CPL_ERROR_TYPE_MISMATCH);
240  skip_if(1);
241  }
242 
243  /* Combine the frames */
244  combinedpair = visir_img_recombine(RECIPE_STRING, parlist, rawframes, badpix,
245  flat, CPL_GEOM_FIRST, &drop_wcs,
246  !config.do_fixcombi, wlen, resol);
247 
248  if (combinedpair == NULL) {
249  cpl_msg_error(cpl_func, "Could not combine the input frames");
250  skip_if (1);
251  }
252 
253  cpl_image_delete(combinedpair[1]);
254  combined = cpl_image_cast(combinedpair[0], CPL_TYPE_DOUBLE);
255  cpl_image_delete(combinedpair[0]);
256 
257  skip_if (0);
258 
259  /* Compute here the sensitivity */
260  cpl_msg_info(cpl_func, "Compute the sensitivity");
261  tab = visir_spc_phot_sensit_from_image(&combined, rawframes, &config,
262  star_cat, spc_cal_lines,
263  spc_cal_qeff, &weight2d, qclist,
264  CPL_FALSE, wlen, slitw, temp, fwhm,
265  resol, dit_key);
266 
267  if (tab == NULL) {
268  cpl_msg_error(cpl_func, "Cannot compute sensitivity: '%s' in %s",
269  cpl_error_get_message(), cpl_error_get_where());
270  skip_if(1);
271  }
272 
273  bug_if (visir_spectro_qc(qclist, paflist, drop_wcs, rawframes,
274  "^(" VISIR_PFITS_SPC_PHOT_COPY ")$",
275  "^(" VISIR_PFITS_REGEXP_SPC_PHOT_PAF ")$"));
276  irplib_framelist_empty(rawframes);
277 
278  /* Save the results */
279  cpl_msg_info(cpl_func, "Save the results");
280 
281  /* PRO.CATG */
282  bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
283  VISIR_SPC_PHOT_COMBINED_PROCATG));
284 
285  skip_if (visir_spc_phot_save(framelist, parlist, qclist, paflist,
286  combined, weight2d, tab));
287 
288  end_skip;
289 
290  cpl_propertylist_delete(qclist);
291  cpl_propertylist_delete(paflist);
292  irplib_framelist_delete(allframes);
293  irplib_framelist_delete(rawframes);
294  cpl_image_delete(combined);
295  cpl_table_delete(tab);
296  cpl_image_delete(weight2d);
297 
298  cpl_free(combinedpair);
299 
300  return cpl_error_get_code();
301 }
302 
303 /*----------------------------------------------------------------------------*/
315 /*----------------------------------------------------------------------------*/
316 static cpl_error_code visir_spc_phot_save(cpl_frameset * set,
317  const cpl_parameterlist * parlist,
318  const cpl_propertylist * qclist,
319  const cpl_propertylist * paflist,
320  const cpl_image * combined,
321  const cpl_image * weight2d,
322  const cpl_table * table)
323 {
324 
325  bug_if (0);
326 
327  /* THE TABLE */
328  skip_if (irplib_dfs_save_table(set, parlist, set, table, NULL, RECIPE_STRING,
329  VISIR_SPC_PHOT_TAB_PROCATG,
330  qclist, NULL, visir_pipe_id,
331  RECIPE_STRING "_tab" CPL_DFS_FITS));
332 
333  /* THE COMBINED IMAGE */
334  skip_if (irplib_dfs_save_image(set, parlist, set, combined, CPL_BPP_IEEE_FLOAT,
335  RECIPE_STRING, VISIR_SPC_PHOT_COMBINED_PROCATG,
336  qclist, NULL, visir_pipe_id,
337  RECIPE_STRING CPL_DFS_FITS));
338 
339  /* THE WEIGHT IMAGE */
340  skip_if (irplib_dfs_save_image(set, parlist, set, weight2d, CPL_BPP_IEEE_FLOAT,
341  RECIPE_STRING, VISIR_SPC_PHOT_WEIGHT_PROCATG,
342  qclist, NULL, visir_pipe_id,
343  RECIPE_STRING "_weight" CPL_DFS_FITS));
344 
345 #ifdef VISIR_SAVE_PAF
346  /* THE PAF FILE FOR QC PARAMETERS */
347 
348  skip_if (cpl_dfs_save_paf("VISIR", RECIPE_STRING, paflist,
349  RECIPE_STRING CPL_DFS_PAF));
350 #else
351  bug_if(paflist == NULL);
352 #endif
353 
354  end_skip;
355 
356  return cpl_error_get_code();
357 
358 }
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:173
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
const cpl_frame * irplib_framelist_get_const(const irplib_framelist *self, int pos)
Get the specified frame from the framelist.
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.