37 #include "crires_recipe.h"
39 #include "crires_model_refining.h"
40 #include "crires_model_kernel.h"
46 #define RECIPE_STRING "crires_model_fix"
52 static int crires_model_fix_parse(
const char *, cpl_vector **, cpl_vector **,
53 int **, cpl_vector **) ;
54 static int crires_model_fix_save(cpl_table *,
const cpl_parameterlist *,
57 static 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"
68 "config-file.fits "CRIRES_CALPRO_MODEL_CONFIG
" or\n"
69 "config-file.fits "CRIRES_CALPRO_MODEL_REFINE_CONF
".\n" ;
71 CRIRES_RECIPE_DEFINE(crires_model_fix,
73 "Model Fixing recipe",
74 crires_model_fix_description) ;
88 static int crires_model_fix(
89 cpl_frameset * frameset,
90 const cpl_parameterlist * parlist)
92 cpl_frameset * rawframes ;
94 const char * user_wls ;
95 cpl_frame * ref_frame ;
96 const char * ref_fname ;
101 cpl_table * new_conf ;
104 setlocale(LC_NUMERIC,
"C");
107 if (crires_model_off()) {
117 if (crires_dfs_set_groups(frameset,
"crires_model_fix")) {
118 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
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) ;
128 if (config == NULL) {
129 cpl_msg_error(__func__,
"Model configuration file is missing") ;
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") ;
139 if ((rawframes = crires_extract_frameset(frameset,
140 CRIRES_MODEL_FIX_RAW)) == NULL) {
141 cpl_msg_error(__func__,
"No raw frame in input") ;
144 ref_frame = cpl_frameset_get_position(rawframes, 0) ;
145 ref_fname = cpl_frame_get_filename(ref_frame) ;
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) ;
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) ;
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) ;
173 cpl_vector_delete(wls) ;
174 cpl_msg_indent_less() ;
177 cpl_msg_indent_less() ;
180 cpl_vector_delete(pos_x) ;
181 cpl_vector_delete(pos_y) ;
183 cpl_vector_delete(wls) ;
184 cpl_frameset_delete(rawframes) ;
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) ;
193 cpl_table_delete(new_conf) ;
196 if (cpl_error_get_code())
return -1 ;
209 static int crires_model_fix_save(
210 cpl_table * out_table,
211 const cpl_parameterlist * parlist,
214 cpl_propertylist * plist ;
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) ;
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") ;
231 cpl_propertylist_delete(plist) ;
256 static int crires_model_fix_parse(
257 const char * txt_file,
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 ;
277 in = fopen(txt_file,
"r");
278 if (in == NULL)
return -1 ;
282 while (fgets(line, 1024, in) != NULL) {
283 if (line[0] !=
'#' && sscanf(line,
"%lg %lg %d %lg", &x, &y,
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);
297 while (fgets(line, 1024, in) != NULL) {
298 if (line[0] !=
'#' && sscanf(line,
"%lg %lg %d %lg", &x, &y,
300 cpl_vector_set(*pos_x, size, x);
301 cpl_vector_set(*pos_y, size, y);
303 cpl_vector_set(*wls, size, wl);