VISIR Pipeline Reference Manual  4.1.0
visir_spc_wcal_ech.c
1 /* $Id: visir_spc_wcal_ech.c,v 1.73 2012-09-27 15:45:07 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-09-27 15:45:07 $
24  * $Revision: 1.73 $
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 
40 /*-----------------------------------------------------------------------------
41  Defines
42  -----------------------------------------------------------------------------*/
43 
44 #define RECIPE_STRING "visir_old_spc_wcal_ech"
45 
46 /*-----------------------------------------------------------------------------
47  Private Functions prototypes
48  -----------------------------------------------------------------------------*/
49 static cpl_error_code visir_spc_wcal_ech_save(cpl_frameset *,
50  const cpl_parameterlist *,
51  const cpl_propertylist *,
52  const cpl_propertylist *,
53  const cpl_table *);
54 
55 VISIR_RECIPE_DEFINE(visir_old_spc_wcal_ech,
56  VISIR_PARAM_ZERODIST | VISIR_PARAM_ORDEROFF |
57  VISIR_PARAM_AUTOBPM |
58  VISIR_PARAM_PLOT | VISIR_PARAM_SLITSKEW |
59  VISIR_PARAM_SPECSKEW | VISIR_PARAM_VERTARC |
60  VISIR_PARAM_REJLEFT | VISIR_PARAM_REJRIGHT |
61  VISIR_PARAM_HORIARC,
62  "Old DRS detector: Spectroscopic wavelength calibration recipe in Echelle",
63  "This recipe estimates the dispersion relation using the "
64  "atmospheric spectrum\n"
65  "in a grism spectroscopy half-cycle frame.\n"
66  "The files listed in the Set Of Frames (sof-file) "
67  "must be tagged:\n"
68  "VISIR-Echelle-Spectroscopy-file.fits "
69  VISIR_SPC_WCAL_ECH_RAW "\n"
70  "VISIR-Quantum-Efficiency-Calibration-file.fits "
71  VISIR_CALIB_QEFF_SPC "\n"
72  "VISIR-Atmospheric-Emission-Lines-Calibration-file.fits "
73  VISIR_CALIB_LINES_SPC
74  "\n"
75  MAN_VISIR_CALIB_BPM_SPC);
76 
77 /*-----------------------------------------------------------------------------
78  Static variables
79  -----------------------------------------------------------------------------*/
80 
81 static struct {
82  /* Inputs */
83  int auto_bpm;
84  int plot;
85  int orderoffset;
86  double phi;
87  double ksi;
88  double eps;
89  double delta;
90 
91 } visir_spc_wcal_ech_config;
92 
93 
94 /*----------------------------------------------------------------------------*/
98 /*----------------------------------------------------------------------------*/
99 
100 /*-----------------------------------------------------------------------------
101  Functions code
102  -----------------------------------------------------------------------------*/
103 
104 /*----------------------------------------------------------------------------*/
111 /*----------------------------------------------------------------------------*/
112 static int visir_old_spc_wcal_ech(cpl_frameset * framelist,
113  const cpl_parameterlist * parlist)
114 {
115  irplib_framelist * allframes = NULL;
116  irplib_framelist * rawframes = NULL;
117  cpl_propertylist * qclist = cpl_propertylist_new();
118  cpl_propertylist * paflist = cpl_propertylist_new();
119  const char * spc_cal_qeff;
120  const char * spc_cal_lines;
121  cpl_imagelist * hcycle = NULL;
122  cpl_image * imhcycle = NULL;
123  cpl_image * flipped = NULL;
124  cpl_table * spc_table = NULL;
125  cpl_image * order = NULL;
126  double wlen, slitw, temp, fwhm;
127  int icol1, icol2;
128  int jcol1, jcol2;
129  visir_spc_resol resol;
130 
131 
132  visir_spc_wcal_ech_config.auto_bpm =
133  visir_parameterlist_get_bool(parlist, RECIPE_STRING, VISIR_PARAM_AUTOBPM);
134  visir_spc_wcal_ech_config.plot =
135  visir_parameterlist_get_int(parlist, RECIPE_STRING, VISIR_PARAM_PLOT);
136 
137  visir_spc_wcal_ech_config.phi =
138  visir_parameterlist_get_double(parlist,RECIPE_STRING,VISIR_PARAM_SLITSKEW);
139  visir_spc_wcal_ech_config.ksi =
140  visir_parameterlist_get_double(parlist,RECIPE_STRING,VISIR_PARAM_SPECSKEW);
141  visir_spc_wcal_ech_config.eps =
142  visir_parameterlist_get_double(parlist,RECIPE_STRING, VISIR_PARAM_VERTARC);
143  visir_spc_wcal_ech_config.delta =
144  visir_parameterlist_get_double(parlist,RECIPE_STRING, VISIR_PARAM_HORIARC);
145 
146  visir_spc_wcal_ech_config.orderoffset =
147  visir_parameterlist_get_int(parlist, RECIPE_STRING, VISIR_PARAM_ORDEROFF);
148 
149  jcol1 = visir_parameterlist_get_int(parlist, RECIPE_STRING,
150  VISIR_PARAM_REJLEFT);
151  jcol2 = visir_parameterlist_get_int(parlist, RECIPE_STRING,
152  VISIR_PARAM_REJRIGHT);
153 
154  skip_if (0);
155 
156  /* Identify the RAW and CALIB frames in the input frameset */
157  skip_if (visir_dfs_set_groups(framelist));
158 
159  /* Objects observation */
160  allframes = irplib_framelist_cast(framelist);
161  skip_if(allframes == NULL);
162  rawframes = irplib_framelist_extract(allframes, VISIR_SPC_WCAL_ECH_RAW);
163  skip_if (rawframes == NULL);
164 
165  irplib_framelist_empty(allframes);
166 
167  skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, ".*", CPL_FALSE));
168 
169  skip_if(visir_dfs_check_framelist_tag(rawframes));
170 
171  /* Quantum efficiency file */
172  spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
173 
174  /* Spectral lines calibration file */
175  spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
176 
177  /* Get Resolution and Central Wavelength */
178  resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm, 0);
179 
180  skip_if (0);
181 
182  if (resol != VISIR_SPC_R_GHR) {
183  if (visir_spc_wcal_ech_config.orderoffset == 0) {
184  cpl_msg_warning(cpl_func,"Reducing non-HR Grism data as main order");
185  } else {
186  cpl_msg_error(cpl_func, "This recipe cannot reduce non-HR Grism "
187  "data with an order-offset of %d",
188  visir_spc_wcal_ech_config.orderoffset);
189  visir_error_set(CPL_ERROR_TYPE_MISMATCH);
190  skip_if(1);
191  }
192  }
193 
194  /* Get the hcycle image from the reference file */
195  hcycle = visir_load_hcycle(rawframes, 0);
196  skip_if (0);
197 
198  imhcycle = cpl_imagelist_unset(hcycle, 0);
199 
200  /* Convert the combined image
201  * recipe only supported with drs data */
202  flipped = visir_spc_flip(imhcycle, wlen, resol, VISIR_DATA_CUBE2);
203  skip_if (0);
204  cpl_image_delete(imhcycle);
205  imhcycle = flipped;
206  flipped = NULL;
207 
208  skip_if (visir_spc_det_fix(&imhcycle, 1, CPL_FALSE,
209  wlen, resol,
210  visir_spc_wcal_ech_config.phi,
211  visir_spc_wcal_ech_config.ksi,
212  visir_spc_wcal_ech_config.eps,
213  visir_spc_wcal_ech_config.delta,
214  visir_spc_wcal_ech_config.plot));
215 
216  skip_if (visir_spc_echelle_limit(&icol1, &icol2, wlen,
217  visir_spc_wcal_ech_config.orderoffset, 1,
218  cpl_image_get_size_y(imhcycle), 0));
219 
220  skip_if(visir_qc_append_background(qclist, rawframes, icol1, icol2));
221 
222  if (jcol1 != 0) {
223  cpl_msg_info(cpl_func, "Ignoring %d leftmost columns from %d to %d",
224  jcol1, icol1, icol1 + jcol1);
225  icol1 += jcol1;
226  }
227  if (jcol2 != 0) {
228  cpl_msg_info(cpl_func, "Ignoring %d rightmost columns from %d to %d",
229  jcol2, icol2 - jcol2, icol2);
230  icol2 -= jcol2;
231  }
232 
233  if (icol1 != 1 || icol2 != cpl_image_get_size_x(imhcycle)) {
234 
235  order = visir_spc_column_extract(imhcycle, icol1, icol2,
236  visir_spc_wcal_ech_config.plot);
237  skip_if (0);
238  } else {
239  /* Cannot really succeed from here... */
240  order = imhcycle;
241  }
242 
243  skip_if (visir_spc_wavecal(order, qclist, wlen, slitw, temp, fwhm, resol,
244  visir_spc_wcal_ech_config.orderoffset,
245  spc_cal_lines, spc_cal_qeff,
246  &spc_table, visir_spc_wcal_ech_config.plot, 0));
247 
248  /* This column is not part of the product */
249  skip_if (cpl_table_erase_column(spc_table, "SPC_EMISSIVITY"));
250 
251  bug_if (visir_spectro_qc(qclist, paflist, CPL_FALSE, rawframes, NULL,
252  "^(" VISIR_PFITS_REGEXP_SPC_WCAL_PAF ")$"));
253  irplib_framelist_empty(rawframes);
254 
255  /* Save the spectrum */
256  cpl_msg_info(cpl_func, "Saving the produced spectrum");
257 
258  /* PRO.CATG */
259  bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
260  VISIR_SPC_WCAL_ECH_TAB_PROCATG));
261 
262  skip_if (visir_spc_wcal_ech_save(framelist, parlist, qclist, paflist,
263  spc_table));
264 
265  end_skip;
266 
267  cpl_propertylist_delete(qclist);
268  cpl_propertylist_delete(paflist);
269  irplib_framelist_delete(allframes);
270  irplib_framelist_delete(rawframes);
271  cpl_table_delete(spc_table);
272  if (order != imhcycle) cpl_image_delete(order);
273  cpl_imagelist_delete(hcycle);
274  cpl_image_delete(imhcycle);
275  cpl_image_delete(flipped);
276 
277  return cpl_error_get_code();
278 }
279 
280 /*----------------------------------------------------------------------------*/
290 /*----------------------------------------------------------------------------*/
291 static cpl_error_code visir_spc_wcal_ech_save(cpl_frameset * set,
292  const cpl_parameterlist * parlist,
293  const cpl_propertylist * qclist,
294  const cpl_propertylist * paflist,
295  const cpl_table * table)
296 {
297  bug_if (0);
298 
299  /* THE TABLE */
300  skip_if (irplib_dfs_save_table(set, parlist, set, table, NULL, RECIPE_STRING,
301  VISIR_SPC_WCAL_ECH_TAB_PROCATG,
302  qclist, NULL, visir_pipe_id,
303  RECIPE_STRING "_spectrum_tab" CPL_DFS_FITS));
304 
305 #ifdef VISIR_SAVE_PAF
306  /* THE PAF FILE FOR QC PARAMETERS */
307  skip_if (cpl_dfs_save_paf("VISIR", RECIPE_STRING, paflist,
308  RECIPE_STRING CPL_DFS_PAF));
309 #else
310  bug_if(paflist == NULL);
311 #endif
312 
313  end_skip;
314 
315  return cpl_error_get_code();
316 }
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_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
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_imagelist * visir_load_hcycle(const irplib_framelist *rawframes, int pos)
Load the HCYCLE images from a VISIR file.
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.