38 #include "irplib_utils.h"
40 #include "sofi_utils.h"
41 #include "sofi_pfits.h"
48 static int sofi_util_genlines_create(cpl_plugin *);
49 static int sofi_util_genlines_exec(cpl_plugin *);
50 static int sofi_util_genlines_destroy(cpl_plugin *);
51 static int sofi_util_genlines(cpl_parameterlist *, cpl_frameset *);
52 static int sofi_util_genlines_save(cpl_table *, cpl_parameterlist *,
66 } sofi_util_genlines_config;
68 static char sofi_util_genlines_description[] =
69 "This recipe is used to generate spectrum calibration tables.\n"
70 "The sof file contains the names of the input ASCII file\n"
71 "tagged with "SOFI_UTIL_GENLINES_RAW
".\n"
72 "The ASCII file must contain two columns:\n"
73 "1st: Wavelengths in increasing order (the unit is corrected by\n"
74 " the factor option to obtain nanometers).\n"
75 "2nd: The atmospheric emission.\n"
76 "The ASCII files are in the catalogs/ directory of the SOFI distribution.\n"
77 "This recipe produces 1 file:\n"
78 "First product: the table with the lines.\n"
79 " (PRO CATG = "SOFI_UTIL_GENLINES_OH_CAT
") or\n"
80 " (PRO CATG = "SOFI_UTIL_GENLINES_XE_CAT
") or\n"
81 " (PRO CATG = "SOFI_UTIL_GENLINES_NE_CAT
")\n";
96 int cpl_plugin_get_info(cpl_pluginlist * list)
98 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe));
99 cpl_plugin * plugin = &recipe->interface;
101 cpl_plugin_init(plugin,
104 CPL_PLUGIN_TYPE_RECIPE,
105 "sofi_util_genlines",
106 "Generate spectrum calibration FITS tables",
107 sofi_util_genlines_description,
111 sofi_util_genlines_create,
112 sofi_util_genlines_exec,
113 sofi_util_genlines_destroy);
115 cpl_pluginlist_append(list, plugin);
130 static int sofi_util_genlines_create(cpl_plugin * plugin)
136 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
137 recipe = (cpl_recipe *)plugin;
138 else return CPL_ERROR_UNSPECIFIED;
141 recipe->parameters = cpl_parameterlist_new();
145 p = cpl_parameter_new_value(
"sofi.sofi_util_genlines.fill_blanks",
146 CPL_TYPE_BOOL,
"flag to fill blanks",
"sofi.sofi_util_genlines",
148 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"fill_blanks");
149 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
150 cpl_parameterlist_append(recipe->parameters, p);
152 p = cpl_parameter_new_value(
"sofi.sofi_util_genlines.display",
153 CPL_TYPE_BOOL,
"flag to plot",
"sofi.sofi_util_genlines",
155 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"display");
156 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
157 cpl_parameterlist_append(recipe->parameters, p);
159 p = cpl_parameter_new_value(
"sofi.sofi_util_genlines.mode",
160 CPL_TYPE_INT,
"1-OH, 2-XE, 3-NE",
161 "sofi.sofi_util_genlines", 1);
162 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"mode");
163 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
164 cpl_parameterlist_append(recipe->parameters, p);
166 p = cpl_parameter_new_value(
"sofi.sofi_util_genlines.wl_factor",
167 CPL_TYPE_DOUBLE,
"The factor used to multiply the wl",
168 "sofi.sofi_util_genlines", 1.0);
169 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"wl_factor");
170 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
171 cpl_parameterlist_append(recipe->parameters, p);
183 static int sofi_util_genlines_exec(cpl_plugin * plugin)
188 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
189 recipe = (cpl_recipe *)plugin;
190 else return CPL_ERROR_UNSPECIFIED;
192 return sofi_util_genlines(recipe->parameters, recipe->frames);
202 static int sofi_util_genlines_destroy(cpl_plugin * plugin)
207 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
208 recipe = (cpl_recipe *)plugin;
209 else return CPL_ERROR_UNSPECIFIED;
211 cpl_parameterlist_delete(recipe->parameters);
225 static int sofi_util_genlines(
226 cpl_parameterlist * parlist,
227 cpl_frameset * framelist)
229 cpl_bivector * bivec;
232 cpl_bivector * bivec_fill;
233 double * pbivec_fill_x;
234 double * pbivec_fill_y;
235 cpl_frame * cur_frame;
236 int nvals, nb_new_vals;
242 cpl_ensure_code(!cpl_error_get_code(), cpl_error_get_code());
249 par = cpl_parameterlist_find(parlist,
250 "sofi.sofi_util_genlines.fill_blanks");
251 sofi_util_genlines_config.fill_blanks = cpl_parameter_get_bool(par);
253 par = cpl_parameterlist_find(parlist,
"sofi.sofi_util_genlines.display");
254 sofi_util_genlines_config.display = cpl_parameter_get_bool(par);
256 par = cpl_parameterlist_find(parlist,
"sofi.sofi_util_genlines.mode");
257 sofi_util_genlines_config.mode = cpl_parameter_get_int(par);
259 par=cpl_parameterlist_find(parlist,
"sofi.sofi_util_genlines.wl_factor");
260 sofi_util_genlines_config.wl_factor = cpl_parameter_get_double(par);
264 cpl_msg_error(cpl_func,
"Cannot identify RAW and CALIB frames");
265 return CPL_ERROR_UNSPECIFIED;
269 cur_frame = cpl_frameset_get_position(framelist, 0);
270 if ((bivec=cpl_bivector_read(cpl_frame_get_filename(cur_frame)))==NULL) {
271 cpl_msg_error(cpl_func,
"Cannot load the file in the bivector");
272 return CPL_ERROR_UNSPECIFIED;
274 nvals = cpl_bivector_get_size(bivec);
277 if (sofi_util_genlines_config.fill_blanks) {
278 nb_new_vals = 3 * nvals;
279 bivec_fill = cpl_bivector_new(nb_new_vals);
280 pbivec_fill_x = cpl_bivector_get_x_data(bivec_fill);
281 pbivec_fill_y = cpl_bivector_get_y_data(bivec_fill);
282 pbivec_x = cpl_bivector_get_x_data(bivec);
283 pbivec_y = cpl_bivector_get_y_data(bivec);
284 for (i=0 ; i<nvals ; i++) {
285 wavel = pbivec_x[i] * sofi_util_genlines_config.wl_factor;
286 pbivec_fill_x[3*i] = wavel - 0.01;
287 pbivec_fill_y[3*i] = 0.0;
288 pbivec_fill_x[3*i+1] = wavel;
289 pbivec_fill_y[3*i+1] = pbivec_y[i];
290 pbivec_fill_x[3*i+2] = wavel + 0.01;
291 pbivec_fill_y[3*i+2] = 0.0;
293 cpl_bivector_delete(bivec);
296 nvals = cpl_bivector_get_size(bivec);
298 cpl_vector_multiply_scalar(cpl_bivector_get_x(bivec),
299 sofi_util_genlines_config.wl_factor);
303 if (sofi_util_genlines_config.display) {
305 "set grid;set xlabel 'Wavelength (A)';set ylabel 'Emission';",
306 "t 'Catalog lines' w lines",
"", bivec);
310 tab = cpl_table_new(nvals);
311 cpl_table_wrap_double(tab, cpl_bivector_get_x_data(bivec),
312 SOFI_COL_WAVELENGTH);
313 cpl_table_wrap_double(tab, cpl_bivector_get_y_data(bivec),
317 cpl_msg_info(cpl_func,
"Saving the table with %d rows", nvals);
318 if (sofi_util_genlines_save(tab, parlist, framelist) == -1) {
319 cpl_msg_error(cpl_func,
"Cannot write the table");
320 cpl_bivector_delete(bivec);
321 cpl_table_unwrap(tab, SOFI_COL_WAVELENGTH);
322 cpl_table_unwrap(tab, SOFI_COL_EMISSION);
323 cpl_table_delete(tab);
324 return CPL_ERROR_UNSPECIFIED;
326 cpl_bivector_delete(bivec);
327 cpl_table_unwrap(tab, SOFI_COL_WAVELENGTH);
328 cpl_table_unwrap(tab, SOFI_COL_EMISSION);
329 cpl_table_delete(tab);
342 static int sofi_util_genlines_save(
343 cpl_table * out_table,
344 cpl_parameterlist * parlist,
348 cpl_propertylist * plist;
349 cpl_frame * product_frame;
352 sprintf(name_o,
"sofi_util_genlines_save.fits");
353 cpl_msg_info(cpl_func,
"Writing %s" , name_o);
356 plist = cpl_propertylist_new();
357 cpl_propertylist_append_string(plist,
"INSTRUME",
"SOFI");
360 product_frame = cpl_frame_new();
361 cpl_frame_set_filename(product_frame, name_o);
362 if (sofi_util_genlines_config.mode == 1)
363 cpl_frame_set_tag(product_frame, SOFI_UTIL_GENLINES_OH_CAT);
364 else if (sofi_util_genlines_config.mode == 2)
365 cpl_frame_set_tag(product_frame, SOFI_UTIL_GENLINES_XE_CAT);
366 else if (sofi_util_genlines_config.mode == 3)
367 cpl_frame_set_tag(product_frame, SOFI_UTIL_GENLINES_NE_CAT);
369 cpl_frame_set_tag(product_frame,
"UNKNOWN");
371 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_TABLE);
372 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
373 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL);
376 if (cpl_dfs_setup_product_header(plist, product_frame, set, parlist,
377 "sofi_util_genlines", PACKAGE
"/" PACKAGE_VERSION,
378 "PRO-1.15", NULL)!=CPL_ERROR_NONE) {
379 cpl_msg_warning(cpl_func,
"Problem in the product DFS-compliance");
384 if (cpl_table_save(out_table, plist, NULL, name_o,
385 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
386 cpl_msg_error(cpl_func,
"Cannot save the product");
387 cpl_frame_delete(product_frame);
388 cpl_propertylist_delete(plist);
389 return CPL_ERROR_UNSPECIFIED;
391 cpl_propertylist_delete(plist);
394 cpl_frameset_insert(set, product_frame);