CRIRES Pipeline Reference Manual 2.3.18
crires_model_fix.c
1/* $Id: crires_model_fix.c,v 1.22 2011-03-22 09:17:12 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: 2011-03-22 09:17:12 $
24 * $Revision: 1.22 $
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_model_refining.h"
40#include "crires_model_kernel.h"
41
42/*-----------------------------------------------------------------------------
43 Define
44 -----------------------------------------------------------------------------*/
45
46#define RECIPE_STRING "crires_model_fix"
47
48/*-----------------------------------------------------------------------------
49 Functions prototypes
50 -----------------------------------------------------------------------------*/
51
52static int crires_model_fix_parse(const char *, cpl_vector **, cpl_vector **,
53 int **, cpl_vector **) ;
54static int crires_model_fix_save(cpl_table *, const cpl_parameterlist *,
55 cpl_frameset *) ;
56
57static char crires_model_fix_description[] =
58"crires_model_fix -- Model Fixing recipe\n"
59"The files listed in the Set Of Frames (sof-file) must be tagged:\n"
60"raw-file.fits "CRIRES_MODEL_FIX_RAW" or\n"
61"WLS-file.txt "CRIRES_CALPRO_MODEL_FIX_TAB" like:\n"
62" The TXT file is something like:\n"
63" # pos_x pos_y chip wls\n"
64" 500 100 3 1025.0\n"
65" 600 100 3 1035.0\n"
66" 700 100 3 1045.0\n"
67" 900 100 3 1065.0\n"
68"config-file.fits "CRIRES_CALPRO_MODEL_CONFIG" or\n"
69"config-file.fits "CRIRES_CALPRO_MODEL_REFINE_CONF".\n" ;
70
71CRIRES_RECIPE_DEFINE(crires_model_fix,
72 0,
73 "Model Fixing recipe",
74 crires_model_fix_description) ;
75
76/*-----------------------------------------------------------------------------
77 Functions code
78 -----------------------------------------------------------------------------*/
79
80/*----------------------------------------------------------------------------*/
87/*----------------------------------------------------------------------------*/
88static int crires_model_fix(
89 cpl_frameset * frameset,
90 const cpl_parameterlist * parlist)
91{
92 cpl_frameset * rawframes ;
93 const char * config ;
94 const char * user_wls ;
95 cpl_frame * ref_frame ;
96 const char * ref_fname ;
97 cpl_vector * pos_x ;
98 cpl_vector * pos_y ;
99 int * chip ;
100 cpl_vector * wls ;
101 cpl_table * new_conf ;
102
103 /* Needed for sscanf() */
104 setlocale(LC_NUMERIC, "C");
105
106 /* The Model is switched off */
107 if (crires_model_off()) {
108 return 0 ;
109 }
110
111 /* Initialise */
112 rawframes = NULL ;
113
114 /* Retrieve input parameters */
115
116 /* Identify the RAW and CALIB frames in the input frameset */
117 if (crires_dfs_set_groups(frameset, "crires_model_fix")) {
118 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
119 return -1 ;
120 }
121
122 /* Retrieve calibration data */
123 config = crires_extract_filename(frameset, CRIRES_CALPRO_MODEL_CONFIG) ;
124 if (config == NULL) {
125 config = crires_extract_filename(frameset,
126 CRIRES_CALPRO_MODEL_REFINE_CONF) ;
127 }
128 if (config == NULL) {
129 cpl_msg_error(__func__, "Model configuration file is missing") ;
130 return -1 ;
131 }
132 user_wls = crires_extract_filename(frameset, CRIRES_CALPRO_MODEL_FIX_TAB) ;
133 if (user_wls == NULL) {
134 cpl_msg_error(__func__, "User specified wavelengths are missing") ;
135 return -1 ;
136 }
137
138 /* Retrieve raw frames */
139 if ((rawframes = crires_extract_frameset(frameset,
140 CRIRES_MODEL_FIX_RAW)) == NULL) {
141 cpl_msg_error(__func__, "No raw frame in input") ;
142 return -1 ;
143 }
144 ref_frame = cpl_frameset_get_position(rawframes, 0) ;
145 ref_fname = cpl_frame_get_filename(ref_frame) ;
146
147 /* Check the model config file validity vs. the current data */
148 if (crires_model_config_check(config, ref_fname) != 0) {
149 cpl_msg_error(__func__,
150 "The model configuration file version is wrong") ;
151 cpl_frameset_delete(rawframes) ;
152 return -1 ;
153 }
154
155 /* Read the input TXT file */
156 cpl_msg_info(__func__, "Parse the passed TXT file") ;
157 if (crires_model_fix_parse(user_wls, &pos_x, &pos_y, &chip, &wls)) {
158 cpl_msg_error(__func__, "Cannot parse the input txt file") ;
159 cpl_frameset_delete(rawframes) ;
160 return -1 ;
161 }
162
163 /* Compute the new configuration file */
164 cpl_msg_info(__func__, "Compute the new configuration file") ;
165 cpl_msg_indent_more() ;
166 if ((new_conf = crires_model_refining_fix(pos_x, pos_y, chip, wls,
167 ref_fname, config)) == NULL) {
168 cpl_msg_error(__func__, "Cannot compute the new configuration file");
169 cpl_frameset_delete(rawframes) ;
170 cpl_vector_delete(pos_x) ;
171 cpl_vector_delete(pos_y) ;
172 cpl_free(chip) ;
173 cpl_vector_delete(wls) ;
174 cpl_msg_indent_less() ;
175 return -1;
176 }
177 cpl_msg_indent_less() ;
178
179 /* Clean */
180 cpl_vector_delete(pos_x) ;
181 cpl_vector_delete(pos_y) ;
182 cpl_free(chip) ;
183 cpl_vector_delete(wls) ;
184 cpl_frameset_delete(rawframes) ;
185
186 /* Save the new configuration file */
187 cpl_msg_info(__func__, "Save the new configuration file") ;
188 if (crires_model_fix_save(new_conf, parlist, frameset) == -1) {
189 cpl_msg_error(__func__, "Cannot save the product");
190 cpl_table_delete(new_conf) ;
191 return -1 ;
192 }
193 cpl_table_delete(new_conf) ;
194
195 /* Return */
196 if (cpl_error_get_code()) return -1 ;
197 else return 0 ;
198}
199
200/*----------------------------------------------------------------------------*/
208/*----------------------------------------------------------------------------*/
209static int crires_model_fix_save(
210 cpl_table * out_table,
211 const cpl_parameterlist * parlist,
212 cpl_frameset * set)
213{
214 cpl_propertylist * plist ;
215
216 plist = cpl_propertylist_new();
217 cpl_propertylist_append_string(plist, "INSTRUME", "CRIRES") ;
218 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
219 CRIRES_CALPRO_MODEL_CONFIG) ;
220 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
221 CRIRES_PROTYPE_MOD_CONF) ;
222
223 /* Use standard saving function */
224 if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
225 NULL, "crires_model_fix", plist, NULL,
226 PACKAGE "/" PACKAGE_VERSION,
227 "crires_model_fix.fits") != CPL_ERROR_NONE) {
228 cpl_msg_error(__func__, "Cannot save the table") ;
229 return -1 ;
230 }
231 cpl_propertylist_delete(plist) ;
232
233 /* Return */
234 return 0 ;
235}
236
237/*----------------------------------------------------------------------------*/
255/*----------------------------------------------------------------------------*/
256static int crires_model_fix_parse(
257 const char * txt_file,
258 cpl_vector ** pos_x,
259 cpl_vector ** pos_y,
260 int ** chip,
261 cpl_vector ** wls)
262{
263 FILE * in;
264 int size ;
265 char line[1024];
266 double x, y, wl;
267 int ch ;
268
269 /* Test entries */
270 if (txt_file == NULL) return -1 ;
271 if (pos_x == NULL) return -1 ;
272 if (pos_y == NULL) return -1 ;
273 if (chip == NULL) return -1 ;
274 if (wls == NULL) return -1 ;
275
276 /* Open the file */
277 in = fopen(txt_file, "r");
278 if (in == NULL) return -1 ;
279
280 /* Get the size */
281 size = 0 ;
282 while (fgets(line, 1024, in) != NULL) {
283 if (line[0] != '#' && sscanf(line, "%lg %lg %d %lg", &x, &y,
284 &ch, &wl) == 4) {
285 size ++ ;
286 }
287 }
288
289 /* Create and fill the vectors */
290 *pos_x = cpl_vector_new(size);
291 *pos_y = cpl_vector_new(size);
292 *chip = cpl_malloc(size*sizeof(int)) ;
293 *wls = cpl_vector_new(size);
294
295 size = 0 ;
296 rewind(in) ;
297 while (fgets(line, 1024, in) != NULL) {
298 if (line[0] != '#' && sscanf(line, "%lg %lg %d %lg", &x, &y,
299 &ch, &wl) == 4) {
300 cpl_vector_set(*pos_x, size, x);
301 cpl_vector_set(*pos_y, size, y);
302 (*chip)[size] = ch ;
303 cpl_vector_set(*wls, size, wl);
304 size ++ ;
305 }
306 }
307 fclose(in);
308
309 return 0 ;
310}