CRIRES Pipeline Reference Manual 2.3.17
crires_util_wlinit.c
1/* $Id: crires_util_wlinit.c,v 1.39 2012-09-20 12:06:34 yjung Exp $
2 *
3 * This file is part of the crires 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * $Author: yjung $
23 * $Date: 2012-09-20 12:06:34 $
24 * $Revision: 1.39 $
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 <locale.h>
37#include "crires_recipe.h"
38
39#include "crires_wlcalib.h"
40#include "crires_wlestimate.h"
41#include "irplib_utils.h"
42
43/*-----------------------------------------------------------------------------
44 Define
45 -----------------------------------------------------------------------------*/
46
47#define RECIPE_STRING "crires_util_wlinit"
48
49/*-----------------------------------------------------------------------------
50 Functions prototypes
51 -----------------------------------------------------------------------------*/
52
53static int crires_util_wlinit_save(const cpl_imagelist *, const cpl_table **,
54 const cpl_parameterlist *, cpl_frameset *) ;
55
56static char crires_util_wlinit_description[] =
57"This recipe accepts 1 parameters:\n"
58"First parameter: Any FITS file containing the WLEN settings in its header.\n"
59"\n"
60"This recipe produces 2 files:\n"
61"First product: the image with the wavelength values.\n"
62" (PRO TYPE = "CRIRES_PROTYPE_WL_MAP")\n"
63"Second product: the table with the wavelength polynomial coefficients.\n"
64" (PRO TYPE = "CRIRES_PROTYPE_WL_POLY")\n" ;
65
66CRIRES_RECIPE_DEFINE(crires_util_wlinit,
67 CRIRES_PARAM_WAVES,
68 "Wavelength calibration from the initial guess",
69 crires_util_wlinit_description) ;
70
71/*-----------------------------------------------------------------------------
72 Static variables
73 -----------------------------------------------------------------------------*/
74
75static struct {
76 /* Inputs */
77 double wmin[CRIRES_NB_DETECTORS] ;
78 double wmax[CRIRES_NB_DETECTORS] ;
79 /* Outputs */
80 crires_illum_period period ;
81 double qc_wlcent[CRIRES_NB_DETECTORS] ;
82 double qc_wldisp[CRIRES_NB_DETECTORS] ;
83} crires_util_wlinit_config ;
84
85/*-----------------------------------------------------------------------------
86 Functions code
87 -----------------------------------------------------------------------------*/
88
89/*----------------------------------------------------------------------------*/
96/*----------------------------------------------------------------------------*/
97static int crires_util_wlinit(
98 cpl_frameset * frameset,
99 const cpl_parameterlist * parlist)
100{
101 const char * sval ;
102 cpl_imagelist * wl_map ;
103 cpl_table * wl_tab[CRIRES_NB_DETECTORS] ;
104 cpl_polynomial * pol[CRIRES_NB_DETECTORS] ;
105 int pix ;
106 int i, j ;
107
108 /* Needed for sscanf() */
109 setlocale(LC_NUMERIC, "C");
110
111 /* Retrieve input parameters */
112 sval = crires_parameterlist_get_string(parlist, RECIPE_STRING,
113 CRIRES_PARAM_WAVES) ;
114 if (sscanf(sval, "%lg,%lg,%lg,%lg,%lg,%lg,%lg,%lg",
115 &crires_util_wlinit_config.wmin[0],
116 &crires_util_wlinit_config.wmax[0],
117 &crires_util_wlinit_config.wmin[1],
118 &crires_util_wlinit_config.wmax[1],
119 &crires_util_wlinit_config.wmin[2],
120 &crires_util_wlinit_config.wmax[2],
121 &crires_util_wlinit_config.wmin[3],
122 &crires_util_wlinit_config.wmax[3])!=2*CRIRES_NB_DETECTORS){
123 return -1 ;
124 }
125
126 /* Identify the RAW and CALIB frames in the input frameset */
127 if (crires_dfs_set_groups(frameset, NULL)) {
128 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
129 return -1 ;
130 }
131
132 /* Get the detector illumination period */
133 crires_util_wlinit_config.period =
134 crires_get_detector_illum_period(
135 cpl_frame_get_filename(cpl_frameset_get_position(frameset, 0)));
136 if (crires_util_wlinit_config.period == CRIRES_ILLUM_UNKNOWN) {
137 cpl_msg_error(__func__,
138 "Cannot determine the detector illumination period") ;
139 return -1 ;
140 } else {
141 crires_display_detector_illum(crires_util_wlinit_config.period) ;
142 }
143
144 /* Loop on the spectra to calibrate */
145 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
146 cpl_msg_info(__func__, "Wavelength calibration for chip %d", i+1) ;
147
148 /* Initialise */
149 crires_util_wlinit_config.qc_wlcent[i] = -1.0 ;
150 crires_util_wlinit_config.qc_wldisp[i] = -1.0 ;
151
152 /* Get the wavelength estimate */
153 if ((pol[i] = crires_wlestimate_compute(
154 crires_util_wlinit_config.wmin[i],
155 crires_util_wlinit_config.wmax[i])) == NULL) {
156 if ((pol[i] = crires_wlestimate_get(cpl_frame_get_filename(
157 cpl_frameset_get_position(frameset,0)), i+1)) == NULL) {
158 cpl_msg_error(__func__, "Cannot get the calibration") ;
159 cpl_msg_indent_less() ;
160 for (j=0 ; j<i ; j++) cpl_polynomial_delete(pol[j]) ;
161 return -1 ;
162 }
163 }
164
165 /* Create the table */
166 wl_tab[i] = crires_wlcalib_gen_wltab_one(
167 (const cpl_polynomial *)pol[i], -1, -1.0) ;
168 if (pol[i] != NULL) cpl_polynomial_delete(pol[i]) ;
169 }
170 cpl_msg_indent_less() ;
171
172 /* Create the map */
173 wl_map = crires_wlcalib_gen_wlmap((const cpl_table **)wl_tab) ;
174
175 /* Compute the QC parameters */
176 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
177 crires_util_wlinit_config.qc_wlcent[i] =
178 cpl_image_get(cpl_imagelist_get(wl_map, i),
179 512, 256, &pix) ;
180 crires_util_wlinit_config.qc_wldisp[i] =
181 ((cpl_image_get(cpl_imagelist_get(wl_map, i), 1024, 256, &pix)) -
182 (cpl_image_get(cpl_imagelist_get(wl_map, i), 1, 256, &pix)))
183 / 1023 ;
184 }
185
186 /* Save the products */
187 cpl_msg_info(__func__, "Save the products") ;
188 cpl_msg_indent_more() ;
189 if (crires_util_wlinit_save(wl_map, (const cpl_table **)wl_tab, parlist,
190 frameset)) {
191 cpl_msg_error(__func__, "Cannot save the products") ;
192 cpl_msg_indent_less() ;
193 cpl_imagelist_delete(wl_map) ;
194 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
195 if (wl_tab[i] != NULL) cpl_table_delete(wl_tab[i]) ;
196 }
197 return -1 ;
198 }
199 cpl_msg_indent_less() ;
200 cpl_imagelist_delete(wl_map) ;
201 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
202 if (wl_tab[i] != NULL) cpl_table_delete(wl_tab[i]) ;
203 }
204
205 /* Return */
206 if (cpl_error_get_code()) return -1 ;
207 else return 0 ;
208}
209
210/*----------------------------------------------------------------------------*/
219/*----------------------------------------------------------------------------*/
220static int crires_util_wlinit_save(
221 const cpl_imagelist * ilist,
222 const cpl_table ** tab,
223 const cpl_parameterlist * parlist,
224 cpl_frameset * set)
225{
226 cpl_propertylist ** qclists ;
227 const cpl_frame * ref_frame ;
228 cpl_propertylist * inputlist ;
229 const char * recipe_name = "crires_util_wlinit" ;
230 int i ;
231
232 /* Get the reference frame */
233 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
234
235 /* Create the QC lists */
236 qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
237 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
238 qclists[i] = cpl_propertylist_new() ;
239 cpl_propertylist_append_double(qclists[i], "ESO QC CENTWL",
240 crires_util_wlinit_config.qc_wlcent[i]) ;
241 cpl_propertylist_append_double(qclists[i], "ESO QC DISPWL",
242 crires_util_wlinit_config.qc_wldisp[i]) ;
243
244 /* Propagate some keywords from input raw frame extensions */
245 inputlist = cpl_propertylist_load_regexp(
246 cpl_frame_get_filename(ref_frame), i+1,
247 CRIRES_HEADER_EXT_FORWARD, 0) ;
248 cpl_propertylist_copy_property_regexp(qclists[i], inputlist,
249 CRIRES_HEADER_EXT_FORWARD, 0) ;
250 cpl_propertylist_delete(inputlist) ;
251 }
252
253 /* Write the image */
254 crires_image_save(set,
255 parlist,
256 set,
257 ilist,
258 recipe_name,
259 CRIRES_WL_MAP_IMA,
260 CRIRES_PROTYPE_WL_MAP,
261 crires_util_wlinit_config.period,
262 NULL,
263 (const cpl_propertylist **)qclists,
264 PACKAGE "/" PACKAGE_VERSION,
265 "crires_util_wlinit_ima.fits") ;
266
267 /* Write the table */
268 crires_table_save(set,
269 parlist,
270 set,
271 tab,
272 recipe_name,
273 CRIRES_CALPRO_WAVE,
274 CRIRES_PROTYPE_WL_POLY,
275 NULL,
276 (const cpl_propertylist **)qclists,
277 PACKAGE "/" PACKAGE_VERSION,
278 "crires_util_wlinit_tab.fits") ;
279
280 /* Free and return */
281 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
282 cpl_propertylist_delete(qclists[i]) ;
283 }
284 cpl_free(qclists) ;
285 return 0;
286}
287