32#include "cr2res_utils.h"
33#include "cr2res_dfs.h"
40#define RECIPE_STRING "cr2res_util_genstd"
46int cpl_plugin_get_info(cpl_pluginlist * list);
52static int cr2res_util_genstd_create(cpl_plugin *);
53static int cr2res_util_genstd_exec(cpl_plugin *);
54static int cr2res_util_genstd_destroy(cpl_plugin *);
55static int cr2res_util_genstd(cpl_frameset *,
const cpl_parameterlist *);
61static char cr2res_util_genstd_description[] =
"\
62Photospheric flux table generation \n\
64 This utility is used to generate the photospheric flux table \n\
67 raw.txt " CR2RES_PHOTO_FLUX_TXT_RAW
" [1 to n] \n\
68 The specified files are named after the standard star they represent\n\
69 (e.g. HIP61007.txt). \n\
70 The first line of the file must contain the RA and DEC (hh mm ss). \n\
71 (e.g. # 13 20 35.818 -36 42 44.26). \n\
72 The rest of the file must contain two columns: \n\
73 1st: Wavelengths in increasing order \n\
74 2nd: The atmospheric emission \n\
77 cr2res_util_genstd.fits " CR2RES_PHOTO_FLUX_PROCATG
" \n\
81 Library functions used \n\
99int cpl_plugin_get_info(cpl_pluginlist * list)
101 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
102 cpl_plugin * plugin = &recipe->interface;
104 if (cpl_plugin_init(plugin,
106 CR2RES_BINARY_VERSION,
107 CPL_PLUGIN_TYPE_RECIPE,
109 "Generate standard star FITS tables",
110 cr2res_util_genstd_description,
111 CR2RES_PIPELINE_AUTHORS,
114 cr2res_util_genstd_create,
115 cr2res_util_genstd_exec,
116 cr2res_util_genstd_destroy)) {
117 cpl_msg_error(cpl_func,
"Plugin initialization failed");
118 (void)cpl_error_set_where(cpl_func);
122 if (cpl_pluginlist_append(list, plugin)) {
123 cpl_msg_error(cpl_func,
"Error adding plugin to list");
124 (void)cpl_error_set_where(cpl_func);
139static int cr2res_util_genstd_create(cpl_plugin * plugin)
141 cpl_recipe * recipe ;
145 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
146 recipe = (cpl_recipe *)plugin;
151 recipe->parameters = cpl_parameterlist_new();
154 p = cpl_parameter_new_value(
"cr2res_util_genstd.display",
155 CPL_TYPE_BOOL,
"Flag to plot", RECIPE_STRING, FALSE);
156 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"display");
157 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
158 cpl_parameterlist_append(recipe->parameters, p);
169static int cr2res_util_genstd_exec(cpl_plugin * plugin)
174 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
175 recipe = (cpl_recipe *)plugin;
178 return cr2res_util_genstd(recipe->frames, recipe->parameters);
188static int cr2res_util_genstd_destroy(cpl_plugin * plugin)
193 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
194 recipe = (cpl_recipe *)plugin;
197 cpl_parameterlist_delete(recipe->parameters);
211static int cr2res_util_genstd(
212 cpl_frameset * framelist,
213 const cpl_parameterlist * parlist)
215 const cpl_parameter * par ;
217 cpl_bivector * bivec_ref ;
218 int ra1, ra2, dec1, dec2;
221 cpl_frame * cur_frame ;
222 const char * cur_fname ;
224 int nframes, nvals_ref, display ;
230 setlocale(LC_NUMERIC,
"C");
233 par=cpl_parameterlist_find_const(parlist,
"cr2res_util_genstd.display");
234 display = cpl_parameter_get_bool(par);
238 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
241 nframes = cpl_frameset_get_size(framelist) ;
244 cur_frame = cpl_frameset_get_position(framelist, 0) ;
245 cur_fname = cpl_frame_get_filename(cur_frame) ;
246 if ((bivec_ref = cpl_bivector_read(cur_fname))==NULL){
247 cpl_msg_error(__func__,
"Cannot load the file in the bivector") ;
250 pwave_ref = cpl_bivector_get_x_data(bivec_ref) ;
251 nvals_ref = cpl_bivector_get_size(bivec_ref) ;
254 tab = cpl_table_new(nframes+1) ;
255 cpl_table_new_column(tab, CR2RES_COL_STDNAME, CPL_TYPE_STRING) ;
256 cpl_table_new_column(tab, CR2RES_COL_RA, CPL_TYPE_DOUBLE) ;
257 cpl_table_new_column(tab, CR2RES_COL_DEC, CPL_TYPE_DOUBLE) ;
258 cpl_table_new_column_array(tab, CR2RES_COL_PHOTOFLUX, CPL_TYPE_DOUBLE,
262 cpl_table_set_string(tab, CR2RES_COL_STDNAME, 0,
"WAVE") ;
263 cpl_table_set_double(tab, CR2RES_COL_RA, 0, -1.0) ;
264 cpl_table_set_double(tab, CR2RES_COL_DEC, 0, -1.0) ;
265 array = cpl_array_wrap_double(pwave_ref, nvals_ref) ;
266 cpl_table_set_array(tab, CR2RES_COL_PHOTOFLUX, 0, array) ;
267 cpl_array_unwrap(array) ;
270 for (i = 0; i < nframes; i++) {
279 cur_frame = cpl_frameset_get_position(framelist, i) ;
280 cur_fname = cpl_frame_get_filename(cur_frame) ;
283 if ((in = fopen(cur_fname,
"r")) == NULL) {
284 cpl_msg_error(__func__,
"Could not open %s", cur_fname) ;
285 cpl_table_delete(tab) ;
286 cpl_bivector_delete(bivec_ref) ;
289 if (fgets(line, 1024, in) == NULL) {
291 cpl_table_delete(tab) ;
292 cpl_bivector_delete(bivec_ref) ;
295 if (sscanf(line,
"#%d %d %lg %c%d %d %lg ", &ra1, &ra2, &ra3, &isign,
296 &dec1, &dec2, &dec3) != 7) {
297 cpl_msg_error(__func__,
"Invalid first line in file %s", cur_fname);
299 cpl_table_delete(tab) ;
300 cpl_bivector_delete(bivec_ref) ;
306 if (isign ==
'-') dec *= -1.0 ;
309 if ((bivec = cpl_bivector_read(cur_fname))==NULL){
310 cpl_msg_error(__func__,
"Cannot load the file in the bivector") ;
311 cpl_bivector_delete(bivec_ref) ;
312 cpl_table_delete(tab) ;
315 pwave = cpl_bivector_get_x_data(bivec) ;
316 pemiss = cpl_bivector_get_y_data(bivec) ;
317 nvals = cpl_bivector_get_size(bivec) ;
320 if (nvals != nvals_ref) {
321 cpl_msg_error(__func__,
"Invalid file size: %s", cur_fname) ;
322 cpl_bivector_delete(bivec_ref) ;
323 cpl_bivector_delete(bivec) ;
324 cpl_table_delete(tab) ;
329 for (j=0 ; j<nvals ; j++) {
330 if (pwave[j] != pwave_ref[j]) {
331 cpl_msg_error(__func__,
"Invalid bins in %s", cur_fname) ;
332 cpl_bivector_delete(bivec_ref) ;
333 cpl_bivector_delete(bivec) ;
334 cpl_table_delete(tab) ;
342 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Flux (jy)';",
343 "t 'Photospheric flux' w lines",
"", bivec);
347 cpl_table_set_string(tab, CR2RES_COL_STDNAME, i+1,
351 cpl_table_set_double(tab, CR2RES_COL_RA, i+1, ra) ;
352 cpl_table_set_double(tab, CR2RES_COL_DEC, i+1, dec) ;
355 array = cpl_array_wrap_double(pemiss, nvals) ;
356 cpl_table_set_array(tab, CR2RES_COL_PHOTOFLUX, i+1, array) ;
357 cpl_array_unwrap(array) ;
359 cpl_bivector_delete(bivec) ;
361 cpl_bivector_delete(bivec_ref) ;
364 cpl_msg_info(__func__,
"Save the table") ;
367 parlist, framelist, RECIPE_STRING) == -1) {
368 cpl_msg_error(__func__,
"Cannot write the table") ;
369 cpl_table_delete(tab) ;
372 cpl_table_delete(tab) ;
cpl_error_code cr2res_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
int cr2res_io_save_PHOTO_FLUX(const char *filename, cpl_table *out_table, const cpl_parameterlist *parlist, cpl_frameset *set, const char *recipe)
Save PHOTO_FLUX file.
double cr2res_ra_hms2deg(int hh, int mm, double ss)
Convert the RA from hh mm ss to degrees.
double cr2res_dec_hms2deg(int dd, int mm, double ss)
Convert the DEC from dd mm ss to degrees.
char * cr2res_get_root_name(const char *filename)
Find out the root part of a basename (name without extension).
char * cr2res_get_base_name(const char *filename)
Find out the base name of a file (i.e. without prefix path)
const char * cr2res_get_license(void)
Get the pipeline copyright and license.