VISIR Pipeline Reference Manual  4.1.0
visir_spc_wcal.c
1 /* $Id: visir_spc_wcal.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 /*-----------------------------------------------------------------------------
42  Defines
43  -----------------------------------------------------------------------------*/
44 
45 #define RECIPE_STRING "visir_old_spc_wcal"
46 
47 /*-----------------------------------------------------------------------------
48  Private Functions prototypes
49  -----------------------------------------------------------------------------*/
50 static cpl_error_code visir_spc_wcal_save(cpl_frameset *,
51  const cpl_parameterlist *,
52  const cpl_propertylist *,
53  const cpl_propertylist *,
54  const cpl_table *);
55 
56 VISIR_RECIPE_DEFINE(visir_old_spc_wcal,
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",
63  "This recipe estimates the dispersion relation using the "
64  "atmospheric spectrum\n"
65  "in a long-slit spectroscopy half-cycle frame.\n"
66  "The files listed in the Set Of Frames (sof-file) "
67  "must be tagged:\n"
68  "VISIR-Long-Slit-Spectroscopy-file.fits "
69  VISIR_SPC_WCAL_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  double phi;
86  double ksi;
87  double eps;
88  double delta;
89 
90 } visir_spc_wcal_config;
91 
92 /*----------------------------------------------------------------------------*/
96 /*----------------------------------------------------------------------------*/
97 
98 /*-----------------------------------------------------------------------------
99  Functions code
100  -----------------------------------------------------------------------------*/
101 
102 /*----------------------------------------------------------------------------*/
109 /*----------------------------------------------------------------------------*/
110 static int visir_old_spc_wcal(cpl_frameset * framelist,
111  const cpl_parameterlist * parlist)
112 {
113  irplib_framelist * allframes = NULL;
114  irplib_framelist * rawframes = NULL;
115  cpl_propertylist * qclist = cpl_propertylist_new();
116  cpl_propertylist * paflist = cpl_propertylist_new();
117  const char * spc_cal_qeff;
118  const char * spc_cal_lines;
119  cpl_imagelist * hcycle = NULL;
120  cpl_image * imhcycle = NULL;
121  cpl_image * flipped = NULL;
122  cpl_image * imhcyclenarrow = NULL;
123  cpl_table * spc_table = NULL;
124  double wlen, slitw, temp, fwhm;
125  visir_spc_resol resol;
126  int icol1, icol2;
127  int jcol1, jcol2;
128 
129 
130  visir_spc_wcal_config.auto_bpm =
131  visir_parameterlist_get_bool(parlist, RECIPE_STRING, VISIR_PARAM_AUTOBPM);
132  visir_spc_wcal_config.plot = visir_parameterlist_get_int(parlist, RECIPE_STRING, VISIR_PARAM_PLOT);
133 
134  visir_spc_wcal_config.phi =
135  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_SLITSKEW);
136  visir_spc_wcal_config.ksi =
137  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_SPECSKEW);
138  visir_spc_wcal_config.eps =
139  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_VERTARC);
140  visir_spc_wcal_config.delta =
141  visir_parameterlist_get_double(parlist, RECIPE_STRING, VISIR_PARAM_HORIARC);
142 
143  jcol1 = visir_parameterlist_get_int(parlist, RECIPE_STRING,
144  VISIR_PARAM_REJLEFT);
145  jcol2 = visir_parameterlist_get_int(parlist, RECIPE_STRING,
146  VISIR_PARAM_REJRIGHT);
147 
148  skip_if (0);
149 
150  /* Identify the RAW and CALIB frames in the input frameset */
151  skip_if (visir_dfs_set_groups(framelist));
152 
153  /* Objects observation */
154  allframes = irplib_framelist_cast(framelist);
155  skip_if(allframes == NULL);
156  rawframes = irplib_framelist_extract(allframes, VISIR_SPC_WCAL_RAW);
157  skip_if (rawframes == NULL);
158 
159  irplib_framelist_empty(allframes);
160 
161  skip_if(irplib_framelist_load_propertylist_all(rawframes, 0, ".*", CPL_FALSE));
162 
163  skip_if(visir_dfs_check_framelist_tag(rawframes));
164 
165  /* Quantum efficiency file */
166  spc_cal_qeff = irplib_frameset_find_file(framelist, VISIR_CALIB_QEFF_SPC);
167 
168  /* Spectral lines calibration file */
169  spc_cal_lines = irplib_frameset_find_file(framelist, VISIR_CALIB_LINES_SPC);
170 
171  /* Get Resolution and Central Wavelength */
172  resol = visir_spc_get_res_wl(rawframes, &wlen, &slitw, &temp, &fwhm, 0);
173 
174  skip_if (0);
175 
176  if (resol == VISIR_SPC_R_GHR) {
177  cpl_msg_error(cpl_func, "This recipe cannot reduce HR Grism data");
178  visir_error_set(CPL_ERROR_TYPE_MISMATCH);
179  skip_if(1);
180  }
181 
182  skip_if(visir_qc_append_background(qclist, rawframes, 0, 0));
183 
184  /* Get the hcycle image from the reference file */
185  hcycle = visir_load_hcycle(rawframes, 0);
186  skip_if (0);
187 
188  imhcycle = cpl_imagelist_unset(hcycle, 0);
189 
190  /* Convert the combined image */
191  flipped = visir_spc_flip(imhcycle, wlen, resol, VISIR_DATA_CUBE2);
192  skip_if (0);
193  cpl_image_delete(imhcycle);
194  imhcycle = flipped;
195  flipped = NULL;
196 
197  skip_if (visir_spc_det_fix(&imhcycle, 1, CPL_FALSE,
198  wlen, resol,
199  visir_spc_wcal_config.phi,
200  visir_spc_wcal_config.ksi,
201  visir_spc_wcal_config.eps,
202  visir_spc_wcal_config.delta,
203  visir_spc_wcal_config.plot));
204 
205  icol1 = 1;
206  icol2 = cpl_image_get_size_x(imhcycle);
207 
208  if (jcol1 != 0) {
209  cpl_msg_info(cpl_func, "Ignoring %d leftmost columns", jcol1);
210  icol1 += jcol1;
211  }
212  if (jcol2 != 0) {
213  cpl_msg_info(cpl_func, "Ignoring %d rightmost columns", jcol2);
214  icol2 -= jcol2;
215  }
216 
217  if (jcol1 != 0 || jcol2 != 0) {
218  imhcyclenarrow = visir_spc_column_extract(imhcycle, icol1, icol2,
219  visir_spc_wcal_config.plot);
220  skip_if (0);
221 
222  } else {
223  imhcyclenarrow = imhcycle;
224  }
225 
226  skip_if (visir_spc_wavecal(imhcyclenarrow, qclist, wlen, slitw, temp, fwhm,
227  resol, 0,
228  spc_cal_lines, spc_cal_qeff,
229  &spc_table, visir_spc_wcal_config.plot, 0));
230 
231  /* This column is not part of the product */
232  skip_if (cpl_table_erase_column(spc_table, "SPC_EMISSIVITY"));
233 
234  bug_if (visir_spectro_qc(qclist, paflist, CPL_FALSE, rawframes, NULL,
235  "^(" VISIR_PFITS_REGEXP_SPC_WCAL_PAF ")$"));
236  irplib_framelist_empty(rawframes);
237 
238  /* Save the spectrum */
239  cpl_msg_info(cpl_func, "Saving the produced spectrum");
240 
241  /* PRO.CATG */
242  bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
243  VISIR_SPC_WCAL_TAB_PROCATG));
244 
245  skip_if (visir_spc_wcal_save(framelist, parlist, qclist, paflist, spc_table));
246 
247  end_skip;
248 
249  cpl_propertylist_delete(qclist);
250  cpl_propertylist_delete(paflist);
251  irplib_framelist_delete(allframes);
252  irplib_framelist_delete(rawframes);
253  cpl_table_delete(spc_table);
254  cpl_imagelist_delete(hcycle);
255  if (imhcyclenarrow != imhcycle) cpl_image_delete(imhcyclenarrow);
256  cpl_image_delete(imhcycle);
257  cpl_image_delete(flipped);
258 
259  return cpl_error_get_code();
260 }
261 
262 /*----------------------------------------------------------------------------*/
272 /*----------------------------------------------------------------------------*/
273 static cpl_error_code visir_spc_wcal_save(cpl_frameset * set,
274  const cpl_parameterlist * parlist,
275  const cpl_propertylist * qclist,
276  const cpl_propertylist * paflist,
277  const cpl_table * table)
278 {
279  bug_if (0);
280 
281  /* THE TABLE */
282  skip_if (irplib_dfs_save_table(set, parlist, set, table, NULL, RECIPE_STRING,
283  VISIR_SPC_WCAL_TAB_PROCATG,
284  qclist, NULL, visir_pipe_id,
285  RECIPE_STRING "_spectrum_tab" CPL_DFS_FITS));
286 
287 #ifdef VISIR_SAVE_PAF
288  /* THE PAF FILE FOR QC PARAMETERS */
289  skip_if (cpl_dfs_save_paf("VISIR", RECIPE_STRING, paflist,
290  RECIPE_STRING CPL_DFS_PAF));
291 #else
292  bug_if(paflist == NULL);
293 #endif
294 
295  end_skip;
296 
297  return cpl_error_get_code();
298 }
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.