40 #include "irplib_utils.h"
41 #include "irplib_strehl.h"
42 #include "irplib_stdstar.h"
43 #include "irplib_cat.h"
45 #include "hawki_utils.h"
46 #include "hawki_calib.h"
47 #include "hawki_load.h"
48 #include "hawki_save.h"
49 #include "hawki_pfits.h"
50 #include "hawki_dfs.h"
51 #include "hawki_alloc.h"
60 int cpl_plugin_get_info(cpl_pluginlist * list);
62 static int hawki_util_extinction_create(cpl_plugin *) ;
63 static int hawki_util_extinction_exec(cpl_plugin *) ;
64 static int hawki_util_extinction_destroy(cpl_plugin *) ;
65 static int hawki_util_extinction(cpl_parameterlist *, cpl_frameset *) ;
67 static cpl_table ** hawki_util_extinction_reduce
69 const char * stdstars,
72 cpl_table ** raw_zpoint_stats,
75 cpl_imagelist ** images);
76 static int hawki_util_extinction_save_photsol
77 (cpl_table ** photsol_table,
78 cpl_frameset * zpoint_frames,
79 cpl_parameterlist * parlist,
81 static cpl_table ** hawki_util_extinction_photom
82 (cpl_frameset * std_stars_photom);
83 static cpl_table * hawki_util_extinction_get_photomsol
84 (cpl_table * std_stars_photom);
85 static int hawki_util_extinction_compute_keywords
94 static char hawki_util_extinction_description[] =
95 "hawki_util_extinction -- Zero point recipe with extinction\n"
96 "The input of the recipe files listed in the Set Of Frames (sof-file)\n"
97 "must be tagged as:\n"
98 "hawki_util_extinction.fits ("HAWKI_CALPRO_ZPOINT_TAB
"): Zero point solution table\n"
99 "The recipe creates as an output:\n"
100 "hawki_cal_photom.fits ("HAWKI_CALPRO_PHOT_TAB
"): Photometric solution\n"
102 "esorex exits with an error code of 0 if the recipe completes successfully\n"
118 int cpl_plugin_get_info(cpl_pluginlist * list)
120 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe)) ;
121 cpl_plugin * plugin = &recipe->interface ;
123 cpl_plugin_init(plugin,
125 HAWKI_BINARY_VERSION,
126 CPL_PLUGIN_TYPE_RECIPE,
127 "hawki_util_extinction",
128 "Zero point with extinction computation recipe",
129 hawki_util_extinction_description,
130 "Cesar Enrique Garcia Dabo",
133 hawki_util_extinction_create,
134 hawki_util_extinction_exec,
135 hawki_util_extinction_destroy) ;
137 cpl_pluginlist_append(list, plugin) ;
152 static int hawki_util_extinction_create(cpl_plugin * plugin)
154 cpl_recipe * recipe ;
158 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
159 recipe = (cpl_recipe *)plugin ;
163 recipe->parameters = cpl_parameterlist_new() ;
164 if (recipe->parameters == NULL)
180 static int hawki_util_extinction_exec(cpl_plugin * plugin)
182 cpl_recipe * recipe ;
185 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
186 recipe = (cpl_recipe *)plugin ;
192 return hawki_util_extinction(recipe->parameters, recipe->frames) ;
202 static int hawki_util_extinction_destroy(cpl_plugin * plugin)
204 cpl_recipe * recipe ;
207 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
208 recipe = (cpl_recipe *)plugin ;
211 cpl_parameterlist_delete(recipe->parameters) ;
223 static int hawki_util_extinction(
224 cpl_parameterlist * parlist,
225 cpl_frameset * framelist)
227 cpl_frameset * std_stars_photom ;
228 cpl_table ** zpoint_tables;
231 std_stars_photom = NULL ;
235 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
241 (framelist, HAWKI_CALPRO_ZPOINT_TAB)) == NULL)
243 cpl_msg_error(__func__,
"Cannot find std stars info (%s)",
244 HAWKI_CALPRO_ZPOINT_TAB);
249 cpl_msg_info(__func__,
"Reduce the data") ;
250 cpl_msg_indent_more() ;
251 if ((zpoint_tables = hawki_util_extinction_photom(std_stars_photom))==NULL)
253 cpl_msg_error(__func__,
"Cannot compute photometric solution") ;
254 cpl_frameset_delete(std_stars_photom) ;
255 cpl_msg_indent_less() ;
258 cpl_msg_indent_less() ;
261 cpl_msg_info(__func__,
"Save the products");
262 cpl_msg_indent_more() ;
263 if (hawki_util_extinction_save_photsol
264 (zpoint_tables, std_stars_photom,
265 parlist, framelist) == -1)
267 cpl_msg_warning(__func__,
"Data could not be saved. "
268 "Check permisions or disk space") ;
270 cpl_frameset_delete(std_stars_photom);
271 cpl_msg_indent_less() ;
274 cpl_msg_indent_less() ;
277 cpl_frameset_delete(std_stars_photom);
281 if (cpl_error_get_code())
return -1 ;
296 static cpl_table ** hawki_util_extinction_photom
297 (cpl_frameset * std_stars_photom)
300 cpl_table ** photsol;
303 cpl_bivector * iqe_res ;
309 cpl_frame * ref_frame;
312 if (std_stars_photom == NULL)
return NULL ;
315 photsol = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_table*));
316 nframes = cpl_frameset_get_size(std_stars_photom) ;
319 ref_frame = cpl_frameset_get_first(std_stars_photom);
320 nframes = cpl_frameset_get_size(std_stars_photom);
323 for (idet=0 ; idet<HAWKI_NB_DETECTORS; idet++)
325 cpl_table * merged_std_stars_photom;
327 cpl_msg_info(cpl_func,
"Working on detector %d", idet +1);
328 cpl_msg_indent_more();
333 for( iframe = 0 ; iframe < nframes ; ++iframe)
335 cpl_frame * this_frame;
336 cpl_table * this_table;
338 this_frame = cpl_frameset_get_frame(std_stars_photom, iframe);
340 merged_std_stars_photom = cpl_table_load
341 (cpl_frame_get_filename(this_frame),ext_nb, 1);
344 cpl_table * this_table =
345 cpl_table_load(cpl_frame_get_filename(this_frame),ext_nb, 1);
346 cpl_table_insert(merged_std_stars_photom, this_table,
347 cpl_table_get_nrow(merged_std_stars_photom));
348 cpl_table_delete(this_table);
352 if(cpl_table_get_nrow(merged_std_stars_photom) < 2)
354 cpl_msg_error(__func__,
"%d stars found. At least 2 stars must be present",
355 cpl_table_get_nrow(merged_std_stars_photom));
356 cpl_table_delete(merged_std_stars_photom);
359 cpl_msg_info(__func__,
"Number of star measurements %d",
360 cpl_table_get_nrow(merged_std_stars_photom));
362 if((photsol[idet] = hawki_util_extinction_get_photomsol
363 (merged_std_stars_photom)) == NULL)
365 cpl_table_delete(merged_std_stars_photom);
366 for(jdet=0; jdet < idet; jdet++)
367 cpl_table_delete(photsol[jdet]);
372 cpl_table_delete(merged_std_stars_photom);
373 cpl_msg_indent_less();
378 static cpl_table * hawki_util_extinction_get_photomsol
379 (cpl_table * std_stars_photom)
381 const double * instrmag;
382 const double * airmass;
383 const double * catmag;
389 cpl_polynomial * phot_coeff;
392 const cpl_boolean sampsym = CPL_TRUE;
393 const cpl_size mindeg1d = 0;
394 const cpl_size maxdeg1d = 1;
398 nstars = cpl_table_get_nrow(std_stars_photom);
400 photsol = cpl_table_new(1);
401 instrmag = cpl_table_get_data_double_const
402 (std_stars_photom, HAWKI_COL_ZPOINT_INSTRMAG);
403 airmass = cpl_table_get_data_double_const
404 (std_stars_photom, HAWKI_COL_ZPOINT_AIRMASS);
405 catmag = cpl_table_get_data_double_const
406 (std_stars_photom, HAWKI_COL_ZPOINT_STARMAG);
408 if(instrmag == NULL || airmass == NULL || catmag == NULL)
410 cpl_msg_error(cpl_func,
"Some of the following columns not found "
411 "in table: %s, %s, %s", HAWKI_COL_ZPOINT_INSTRMAG,
412 HAWKI_COL_ZPOINT_AIRMASS, HAWKI_COL_ZPOINT_STARMAG);
413 cpl_table_delete(photsol);
417 cpl_table_new_column(photsol, HAWKI_COL_PHOT_FILTER, CPL_TYPE_STRING);
418 cpl_table_new_column(photsol, HAWKI_COL_PHOT_ZEROPOINT, CPL_TYPE_DOUBLE);
419 cpl_table_set_column_unit(photsol,HAWKI_COL_PHOT_ZEROPOINT,
"mag");
420 cpl_table_new_column(photsol, HAWKI_COL_PHOT_EXTCOEFF, CPL_TYPE_DOUBLE);
421 cpl_table_set_column_unit(photsol,HAWKI_COL_PHOT_EXTCOEFF,
"mag/airmass");
424 xpos = cpl_matrix_new(1, nstars);
425 ypos = cpl_vector_new(nstars);
426 for(istar = 0; istar < nstars; ++istar)
429 cpl_matrix_set(xpos, 0, istar, airmass[istar]);
430 y = catmag[istar] + instrmag[istar];
431 cpl_vector_set(ypos, istar, y);
436 phot_coeff = cpl_polynomial_new(1);
437 if(cpl_polynomial_fit(phot_coeff, xpos, NULL, ypos, NULL,
438 CPL_FALSE, &mindeg1d, &maxdeg1d) != CPL_ERROR_NONE)
440 cpl_msg_error(cpl_func,
"Cannot get the photometric solution");
441 cpl_table_delete(photsol);
444 cpl_matrix_delete(xpos);
445 cpl_vector_delete(ypos);
447 zeropoint = cpl_polynomial_get_coeff(phot_coeff, &pows);
449 extcoeff = -cpl_polynomial_get_coeff(phot_coeff, &pows);
450 cpl_polynomial_delete(phot_coeff);
452 cpl_table_set_double(photsol, HAWKI_COL_PHOT_ZEROPOINT, 0,
454 cpl_table_set_double(photsol, HAWKI_COL_PHOT_EXTCOEFF, 0,
458 cpl_msg_indent_more();
459 cpl_msg_info(__func__,
"Zero point: %f", zeropoint);
460 cpl_msg_info(__func__,
"Extinction coefficient: %f", extcoeff);
461 cpl_msg_indent_less();
476 static int hawki_util_extinction_save_photsol
477 (cpl_table ** photsol_table,
478 cpl_frameset * zpoint_frames,
479 cpl_parameterlist * parlist,
482 cpl_propertylist ** qclists ;
483 const char * ref_filename;
484 cpl_propertylist * inputlist ;
485 int ext_nb, nframes ;
486 const char * recipe_name =
"hawki_util_extinction" ;
489 cpl_errorstate error_prevstate = cpl_errorstate_get();
493 nframes = cpl_frameset_get_size(
set) ;
499 qclists = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_propertylist*)) ;
500 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
502 int this_iframe = -1;
504 qclists[idet] = cpl_propertylist_new() ;
510 inputlist = cpl_propertylist_load_regexp(ref_filename, ext_nb,
511 HAWKI_HEADER_EXT_FORWARD, 0) ;
512 cpl_propertylist_append(qclists[idet], inputlist) ;
513 cpl_propertylist_delete(inputlist) ;
521 (
const cpl_table **)photsol_table,
523 HAWKI_CALPRO_PHOT_TAB,
524 HAWKI_PROTYPE_PHOT_TAB,
526 (
const cpl_propertylist **)qclists,
527 "hawki_util_extinction.fits");
530 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
531 cpl_propertylist_delete(qclists[idet]) ;
536 if(!cpl_errorstate_is_equal(error_prevstate))
538 cpl_errorstate_set(CPL_ERROR_NONE);