36#include "visir_recipe.h"
44#define RECIPE_STRING "visir_util_spc_txt2fits"
54static cpl_error_code visir_util_spc_txt2fits_save(cpl_frameset *,
55 const cpl_parameterlist *,
56 const cpl_propertylist *,
60static cpl_error_code visir_propertylist_fill_comments(cpl_propertylist *,
63VISIR_RECIPE_DEFINE(visir_util_spc_txt2fits, VISIR_PARAM_QEFF,
64 "Generate spectrum calibration FITS tables",
65 "This recipe shall be used to generate spectrum "
66 "calibration tables.\n"
67 "The sof file shall consist of 1 line with the name of an "
69 "currently tagged with either " VISIR_SPC_LINES_ASCII
" or "
70 VISIR_SPC_QEFF_ASCII
".\n"
71 "The file must comprise two columns:\n"
72 "1st: Must be wavelengths in increasing order in units "
74 "2nd: For " VISIR_SPC_LINES_ASCII
"-files must be the "
75 "atmospheric emission, "
77 " for " VISIR_SPC_QEFF_ASCII
"-files must be the "
78 "quantum efficiency of "
80 "A " VISIR_SPC_LINES_ASCII
"-file will generate a "
83 "a " VISIR_SPC_QEFF_ASCII
"-file will generate a "
86 "The current " VISIR_CALIB_LINES_SPC
"- and "
89 "generated using the ASCII-files in the catalogs/ "
90 "directory of the VISIR\n"
91 "source-code distribution.");
114static int visir_util_spc_txt2fits(cpl_frameset * framelist,
115 const cpl_parameterlist * parlist)
117 const char * label2 = NULL;
119 cpl_propertylist * qclist = cpl_propertylist_new();
120 irplib_framelist * allframes = NULL;
121 irplib_framelist * rawframes = NULL;
122 cpl_bivector * spectrum = NULL;
123 cpl_table * tab = NULL;
124 FILE * stream = NULL;
132 allframes = irplib_framelist_cast(framelist);
133 skip_if(allframes == NULL);
134 rawframes = irplib_framelist_extract_regexp(allframes,
"^("
135 VISIR_SPC_LINES_ASCII
"|"
136 VISIR_SPC_QEFF_ASCII
")$",
138 skip_if (rawframes == NULL);
140 nframes = irplib_framelist_get_size(rawframes);
143 for (i = 0; i < nframes; i++) {
144 const cpl_frame * frame = irplib_framelist_get_const(rawframes, i);
145 const char * filename = cpl_frame_get_filename(frame);
146 const char * basepos;
148 char extname[FLEN_VALUE];
151 skip_if(filename == NULL);
152 cpl_propertylist_empty(qclist);
154 basepos = strrchr(filename,
'/');
155 basepos = basepos == NULL ? filename : basepos + 1;
156 dotpos = strrchr(basepos,
'.');
157 baselen = dotpos == NULL ? strlen(basepos) : (size_t)(dotpos - basepos);
159 strncpy(extname, basepos, CX_MIN(FLEN_VALUE, baselen));
160 extname[CX_MIN(FLEN_VALUE - 1, baselen)] =
'\0';
162 skip_if(cpl_propertylist_append_string(qclist,
"EXTNAME", extname));
165 if (stream != NULL) fclose(stream);
166 stream = fopen(filename,
"r");
168 skip_if (stream == NULL);
170 cpl_bivector_delete(spectrum);
171 spectrum = cpl_bivector_new(2481);
172 skip_if( visir_bivector_load(spectrum, stream));
174 if (strcmp(cpl_frame_get_tag(frame), VISIR_SPC_LINES_ASCII)) {
177 procat = VISIR_SPEC_CAL_QEFF_PROCATG;
178 label2 =
"Efficiency";
180 skip_if(cpl_propertylist_set_comment(qclist,
"EXTNAME",
"Match "
187 procat = VISIR_SPEC_CAL_LINES_PROCATG;
192 nvals = cpl_bivector_get_size(spectrum);
196 cpl_table_unwrap(tab,
"Wavelength");
197 cpl_table_unwrap(tab, label2);
198 cpl_table_delete(tab);
200 tab = cpl_table_new(nvals);
202 skip_if( tab == NULL );
204 skip_if (cpl_table_wrap_double(tab, cpl_bivector_get_x_data(spectrum),
206 skip_if (cpl_table_set_column_unit(tab,
"Wavelength",
"m"));
208 skip_if (cpl_table_wrap_double(tab, cpl_bivector_get_y_data(spectrum),
211 skip_if (cpl_table_set_column_unit(tab, label2,
"[0;1] (Unitless)"));
213 skip_if(visir_propertylist_fill_comments(qclist, stream));
216 cpl_msg_info(cpl_func,
"Saving the table with %d rows", nvals);
219 skip_if (visir_util_spc_txt2fits_save(framelist, parlist, qclist,
222 skip_if (cpl_table_save(tab, NULL, qclist, RECIPE_STRING
223 CPL_DFS_FITS, CPL_IO_EXTEND));
229 if (stream != NULL) fclose(stream);
230 cpl_bivector_delete(spectrum);
232 cpl_table_unwrap(tab,
"Wavelength");
233 cpl_table_unwrap(tab, label2);
234 cpl_table_delete(tab);
236 irplib_framelist_delete(allframes);
237 irplib_framelist_delete(rawframes);
238 cpl_propertylist_delete(qclist);
240 return cpl_error_get_code();
257cpl_error_code visir_util_spc_txt2fits_save(cpl_frameset * set,
258 const cpl_parameterlist * parlist,
259 const cpl_propertylist * qclist,
261 const cpl_table * tab)
264 cpl_propertylist * applist = cpl_propertylist_new();
267 bug_if(cpl_propertylist_append_string(applist,
"INSTRUME",
"VISIR"));
269 skip_if (irplib_dfs_save_table(set, parlist, set, tab, qclist, RECIPE_STRING,
270 procat, applist, NULL, visir_pipe_id,
271 RECIPE_STRING CPL_DFS_FITS));
275 cpl_propertylist_delete(applist);
277 return cpl_error_get_code();
292static cpl_error_code visir_propertylist_fill_comments(cpl_propertylist * self,
296 bug_if(self == NULL);
297 bug_if(stream == NULL);
299 if (!fseek(stream, 0, SEEK_SET)) {
303 while (fgets(line, 1024, stream) != NULL) {
305 if (line[0] ==
'#') {
306 char value[FLEN_VALUE];
307 strncpy(value, line+1, FLEN_VALUE);
308 value[FLEN_VALUE - 1] =
'\0';
309 cpl_propertylist_append_string(self,
"COMMENT", value);
316 return cpl_error_get_code();
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.