40 #include "irplib_utils.h"
41 #include "irplib_calib.h"
42 #include "irplib_strehl.h"
43 #include "irplib_stdstar.h"
44 #include "irplib_cat.h"
45 #include "irplib_wcs.h"
47 #include "hawki_image_stats.h"
48 #include "hawki_utils.h"
49 #include "hawki_calib.h"
50 #include "hawki_load.h"
51 #include "hawki_save.h"
52 #include "hawki_pfits.h"
53 #include "hawki_dfs.h"
54 #include "hawki_alloc.h"
63 int cpl_plugin_get_info(cpl_pluginlist * list);
65 static int hawki_cal_zpoint_create(cpl_plugin *) ;
66 static int hawki_cal_zpoint_exec(cpl_plugin *) ;
67 static int hawki_cal_zpoint_destroy(cpl_plugin *) ;
68 static int hawki_cal_zpoint(cpl_parameterlist *, cpl_frameset *) ;
70 static void hawki_cal_zpoint_output_init(
void);
71 static int hawki_cal_zpoint_retrieve_input_param
72 (cpl_parameterlist * parlist);
73 static cpl_table ** hawki_cal_zpoint_reduce
75 const char * stdstars,
78 cpl_table ** raw_zpoint_stats,
80 cpl_imagelist ** images);
81 static int hawki_cal_zpoint_save
82 (cpl_table ** zpoint_tables,
84 cpl_imagelist * images,
85 cpl_table ** raw_zpoint_stats,
86 cpl_frameset * zpoint_frames,
87 cpl_frameset * calib_frames,
88 const cpl_frame * stars_frame,
89 cpl_parameterlist * parlist,
91 static int hawki_cal_zpoint_compute_qc
92 (cpl_propertylist * qcmainparams,
93 cpl_propertylist ** qcextparams,
95 static cpl_table ** hawki_cal_zpoint_photom
96 (cpl_imagelist * ilist,
99 static int hawki_cal_zpoint_get_mag(
const char *,
double,
double, hawki_band) ;
100 static int hawki_cal_zpoint_compute_keywords
103 static cpl_error_code hawki_cal_zpoint_get_expected_pos
106 int hawki_cal_zpoint_check_epoch_equinox(cpl_propertylist * plist);
114 double xcoord[HAWKI_NB_DETECTORS] ;
115 double ycoord[HAWKI_NB_DETECTORS] ;
118 double stdstar_given_magnitude;
119 double detect_sigma ;
122 double phot_star_radius ;
125 } hawki_cal_zpoint_config;
137 double airmass[HAWKI_NB_DETECTORS];
138 double zpoint[HAWKI_NB_DETECTORS];
139 double atx0[HAWKI_NB_DETECTORS];
140 double posx[HAWKI_NB_DETECTORS];
141 double posy[HAWKI_NB_DETECTORS];
142 double flux[HAWKI_NB_DETECTORS];
143 double instrmag[HAWKI_NB_DETECTORS];
144 double peak[HAWKI_NB_DETECTORS];
145 double bgd[HAWKI_NB_DETECTORS];
146 double fwhmx[HAWKI_NB_DETECTORS];
147 double fwhmy[HAWKI_NB_DETECTORS];
148 double fwhm[HAWKI_NB_DETECTORS];
149 double fwhmx_as[HAWKI_NB_DETECTORS];
150 double fwhmy_as[HAWKI_NB_DETECTORS];
151 double fwhm_as[HAWKI_NB_DETECTORS];
158 double stdstar_mag_filter;
159 double stdstar_mag_H;
160 double stdstar_mag_J;
161 double stdstar_mag_K;
162 double stdstar_mag_Y;
163 int stdstar_incat_found;
164 int stdstar_mag_available;
165 int stdstar_image_detected[HAWKI_NB_DETECTORS];
166 int zpoint_computable[HAWKI_NB_DETECTORS];
167 int zpoint_mean_computable;
168 } hawki_cal_zpoint_outputs;
170 static char hawki_cal_zpoint_description[] =
171 "hawki_cal_zpoint -- Zero point recipe\n"
172 "The input of the recipe files listed in the Set Of Frames (sof-file)\n"
173 "must be tagged as:\n"
174 "raw-file.fits "HAWKI_CAL_ZPOINT_RAW
" or\n"
175 "stdstars-file.fits "HAWKI_CALPRO_STDSTARS
" or\n"
176 "flat-file.fits "HAWKI_CALPRO_FLAT
" or\n"
177 "bpm-file.fits "HAWKI_CALPRO_BPM
"\n"
178 "The recipe creates as an output:\n"
179 "hawki_cal_zpoint.fits ("HAWKI_CALPRO_ZPOINT_TAB
"): Zero point solution table\n"
180 "hawki_cal_zpoint_check.fits ("HAWKI_CALPRO_ZPOINT_IMA
"): Standard star images corrected (for checking purposes)\n"
181 "hawki_cal_zpoint_stats.fits ("HAWKI_CALPRO_ZPOINT_STATS
"): Statistics of the raw standard star images\n"
183 "esorex exits with an error code of 0 if the recipe completes successfully\n"
199 int cpl_plugin_get_info(cpl_pluginlist * list)
201 cpl_recipe * recipe = cpl_calloc(1,
sizeof(*recipe));
202 cpl_plugin * plugin = &recipe->interface;
204 cpl_plugin_init(plugin,
206 HAWKI_BINARY_VERSION,
207 CPL_PLUGIN_TYPE_RECIPE,
209 "Zero point computation recipe",
210 hawki_cal_zpoint_description,
211 "Cesar Enrique Garcia Dabo",
214 hawki_cal_zpoint_create,
215 hawki_cal_zpoint_exec,
216 hawki_cal_zpoint_destroy) ;
218 cpl_pluginlist_append(list, plugin) ;
233 static int hawki_cal_zpoint_create(cpl_plugin * plugin)
235 cpl_recipe * recipe ;
239 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
240 recipe = (cpl_recipe *)plugin ;
244 recipe->parameters = cpl_parameterlist_new();
245 if (recipe->parameters == NULL)
250 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.detect_sigma",
251 CPL_TYPE_DOUBLE,
"the sigma value for object detection",
252 "hawki.hawki_cal_zpoint", 7.0);
253 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"detect_sigma") ;
254 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
255 cpl_parameterlist_append(recipe->parameters, p) ;
257 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.star_r",CPL_TYPE_DOUBLE,
258 "the star radius",
"hawki.hawki_cal_zpoint", -1.0) ;
259 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"star_r") ;
260 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
261 cpl_parameterlist_append(recipe->parameters, p) ;
263 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.bg_r1", CPL_TYPE_DOUBLE,
264 "the internal background radius",
"hawki.hawki_cal_zpoint", -1.0) ;
265 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"bg_r1") ;
266 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
267 cpl_parameterlist_append(recipe->parameters, p) ;
269 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.bg_r2", CPL_TYPE_DOUBLE,
270 "the external background radius",
"hawki.hawki_cal_zpoint", -1.0) ;
271 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"bg_r2") ;
272 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
273 cpl_parameterlist_append(recipe->parameters, p) ;
275 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.ra", CPL_TYPE_DOUBLE,
276 "RA in degrees",
"hawki.hawki_cal_zpoint", 999.0) ;
277 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"ra") ;
278 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
279 cpl_parameterlist_append(recipe->parameters, p) ;
281 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.dec", CPL_TYPE_DOUBLE,
282 "DEC in degrees",
"hawki.hawki_cal_zpoint", 999.0) ;
283 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"dec") ;
284 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
285 cpl_parameterlist_append(recipe->parameters, p) ;
287 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.mag", CPL_TYPE_DOUBLE,
288 "magnitude",
"hawki.hawki_cal_zpoint", 99.0) ;
289 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"mag") ;
290 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
291 cpl_parameterlist_append(recipe->parameters, p) ;
293 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.sx", CPL_TYPE_INT,
294 "x half-size of the search box",
"hawki.hawki_cal_zpoint", 100) ;
295 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sx") ;
296 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
297 cpl_parameterlist_append(recipe->parameters, p) ;
299 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.sy", CPL_TYPE_INT,
300 "y half-size of the search box",
"hawki.hawki_cal_zpoint", 100) ;
301 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"sy") ;
302 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
303 cpl_parameterlist_append(recipe->parameters, p) ;
305 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.xcoord", CPL_TYPE_STRING,
306 "Coordinates in X where the standard star is located. If -1 use WCS",
307 "hawki.hawki_cal_zpoint",
"-1., -1., -1., -1.");
308 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"xcoord") ;
309 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
310 cpl_parameterlist_append(recipe->parameters, p) ;
312 p = cpl_parameter_new_value(
"hawki.hawki_cal_zpoint.ycoord", CPL_TYPE_STRING,
313 "Coordinates in Y where the standard star is located. If -1 use WCS",
314 "hawki.hawki_cal_zpoint",
"-1., -1., -1., -1.") ;
315 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"ycoord") ;
316 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV) ;
317 cpl_parameterlist_append(recipe->parameters, p) ;
330 static int hawki_cal_zpoint_exec(cpl_plugin * plugin)
332 cpl_recipe * recipe ;
335 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
336 recipe = (cpl_recipe *)plugin ;
342 return hawki_cal_zpoint(recipe->parameters, recipe->frames) ;
352 static int hawki_cal_zpoint_destroy(cpl_plugin * plugin)
354 cpl_recipe * recipe ;
357 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
358 recipe = (cpl_recipe *)plugin ;
361 cpl_parameterlist_delete(recipe->parameters) ;
373 static int hawki_cal_zpoint(
374 cpl_parameterlist * parlist,
375 cpl_frameset * framelist)
377 cpl_parameter * par ;
380 const char * stdstars ;
381 cpl_frameset * zpoint_frames ;
382 cpl_frameset * calib_frames ;
383 const cpl_frame * stars_frame;
384 cpl_table ** raw_zpoint_stats;
385 cpl_table ** zpoint_tables;
386 cpl_imagelist * std_star_images ;
391 hawki_cal_zpoint_output_init();
392 zpoint_frames = NULL ;
396 if(hawki_cal_zpoint_retrieve_input_param(parlist))
398 cpl_msg_error(__func__,
"Wrong parameters");
404 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
409 calib_frames = cpl_frameset_new();
412 cpl_frameset_insert(calib_frames, cpl_frame_duplicate(
413 cpl_frameset_find_const(framelist, HAWKI_CALPRO_FLAT)));
416 cpl_frameset_insert(calib_frames, cpl_frame_duplicate(
417 cpl_frameset_find_const(framelist, HAWKI_CALPRO_BPM)));
420 stars_frame = cpl_frameset_find_const(framelist, HAWKI_CALPRO_STDSTARS);
421 if (stars_frame == NULL)
423 cpl_msg_error(__func__,
"Cannot find the catalog in the input list (%s)",
424 HAWKI_CALPRO_STDSTARS);
425 cpl_frameset_delete(calib_frames);
428 stdstars = cpl_frame_get_filename(stars_frame);
432 HAWKI_CAL_ZPOINT_RAW)) != NULL) {
434 cpl_msg_error(__func__,
"Cannot find raw frames in the input list (%s)",
435 HAWKI_CAL_ZPOINT_RAW);
436 cpl_frameset_delete(calib_frames);
441 if (cpl_frameset_get_size(zpoint_frames) != 4) {
442 cpl_msg_error(__func__,
443 "4 input raw frames are expected, not %"CPL_SIZE_FORMAT,
444 cpl_frameset_get_size(zpoint_frames)) ;
445 cpl_frameset_delete(zpoint_frames) ;
446 cpl_frameset_delete(calib_frames);
451 raw_zpoint_stats = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_table *));
452 for(idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
454 raw_zpoint_stats[idet] = cpl_table_new(
455 cpl_frameset_get_size(zpoint_frames));
460 cpl_msg_info(__func__,
"Reduce the data") ;
461 cpl_msg_indent_more() ;
462 labels = cpl_calloc(cpl_frameset_get_size(zpoint_frames),
sizeof(
int)) ;
463 if ((zpoint_tables = hawki_cal_zpoint_reduce(zpoint_frames, stdstars,
464 bpm, flat, raw_zpoint_stats, labels, &std_star_images))==NULL)
466 cpl_msg_error(__func__,
"Cannot reduce the data") ;
467 cpl_frameset_delete(zpoint_frames) ;
468 cpl_frameset_delete(calib_frames);
471 cpl_msg_indent_less() ;
474 cpl_msg_indent_less() ;
477 cpl_msg_info(__func__,
"Save the products") ;
478 cpl_msg_indent_more() ;
479 if (hawki_cal_zpoint_save
480 (zpoint_tables, labels, std_star_images, raw_zpoint_stats,
481 zpoint_frames, calib_frames, stars_frame,
482 parlist, framelist) == -1)
484 cpl_msg_warning(__func__,
"Data could not be saved. "
485 "Check permisions or disk space") ;
486 cpl_frameset_delete(zpoint_frames);
488 cpl_imagelist_delete(std_star_images) ;
489 cpl_frameset_delete(calib_frames);
492 cpl_msg_indent_less() ;
495 cpl_msg_indent_less() ;
498 cpl_frameset_delete(zpoint_frames);
499 cpl_frameset_delete(calib_frames);
500 cpl_imagelist_delete(std_star_images);
506 if (cpl_error_get_code())
508 cpl_msg_error(__func__,
509 "HAWK-I pipeline could not recover from previous errors");
526 static cpl_table ** hawki_cal_zpoint_reduce
528 const char * stdstars,
531 cpl_table ** raw_zpoint_stats,
533 cpl_imagelist ** star_images)
535 cpl_frame * cur_frame ;
536 cpl_propertylist * plist ;
538 cpl_imagelist * star_images_frame_order ;
540 cpl_bivector * positions ;
541 cpl_image * filt_ima ;
544 double pos_x, pos_y, pos_x_cen, pos_y_cen, dist, min_dist ;
545 cpl_apertures * aperts ;
546 cpl_table ** zpoint_tables;
547 cpl_image * tmp_ima ;
551 int iframe_star = -1;
555 cpl_errorstate error_prevstate;
559 if (
set == NULL)
return NULL ;
560 if (stdstars == NULL)
return NULL ;
561 if (star_images == NULL)
return NULL ;
564 error_prevstate = cpl_errorstate_get();
565 cur_frame = cpl_frameset_get_frame(
set, 0) ;
566 plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0) ;
568 else sprintf(hawki_cal_zpoint_outputs.filter, sval) ;
569 if (hawki_cal_zpoint_config.target_ra > 998.0)
574 if(hawki_cal_zpoint_check_epoch_equinox(plist) == -1)
576 cpl_propertylist_delete(plist);
580 if (hawki_cal_zpoint_config.target_dec > 998.0)
585 if(hawki_cal_zpoint_check_epoch_equinox(plist) == -1)
587 cpl_propertylist_delete(plist);
593 cpl_propertylist_delete(plist) ;
594 if(!cpl_errorstate_is_equal(error_prevstate))
596 cpl_msg_error(__func__,
"Cannot get keywords from main header:");
597 cpl_msg_indent_more();
598 cpl_msg_error(__func__,
"%s",cpl_error_get_message());
599 cpl_msg_indent_less();
602 cpl_msg_info(__func__,
"Searching catalog stars closest to target:");
603 cpl_msg_indent_more();
606 cpl_msg_info(__func__,
"RA = %g (%s); DEC = %g (%s)",
607 hawki_cal_zpoint_config.target_ra, rastr,
608 hawki_cal_zpoint_config.target_dec, decstr);
609 cpl_msg_info(__func__,
"HAWK-I Filter: %s", hawki_cal_zpoint_outputs.filter);
610 cpl_msg_indent_less();
613 if ((hawki_cal_zpoint_outputs.band =
615 HAWKI_BAND_UNKNOWN) {
616 cpl_msg_error(__func__,
"Cannot associate the filter %s to a band",
617 hawki_cal_zpoint_outputs.filter) ;
622 cpl_msg_indent_more();
623 return_code = hawki_cal_zpoint_get_mag(stdstars,
624 hawki_cal_zpoint_config.target_ra,
625 hawki_cal_zpoint_config.target_dec,
626 hawki_cal_zpoint_outputs.band);
627 if (return_code == -1)
629 cpl_msg_error(__func__,
"Could not open star database");
632 else if(return_code == 1)
634 cpl_msg_warning(__func__,
"No suitable star found in catalog");
635 cpl_msg_warning(__func__,
"Using the target coordinates "
636 "as the standard star coordinates: ");
637 hawki_cal_zpoint_outputs.stdstar_ra =
638 hawki_cal_zpoint_config.target_ra;
639 hawki_cal_zpoint_outputs.stdstar_dec =
640 hawki_cal_zpoint_config.target_dec;
643 cpl_msg_info(__func__,
" RA = %g (%s) ; DEC = %g (%s)",
644 hawki_cal_zpoint_outputs.stdstar_ra, rastr,
645 hawki_cal_zpoint_outputs.stdstar_dec, decstr);
649 cpl_msg_info(__func__,
"Catalog where the star was found: %s",
650 hawki_cal_zpoint_outputs.catalog);
651 cpl_msg_info(__func__,
"Star name: %s",
652 hawki_cal_zpoint_outputs.starname);
653 if(hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
655 cpl_msg_info(__func__,
"Star magnitude in filter %s : %g [mag]",
656 hawki_cal_zpoint_outputs.filter,
657 hawki_cal_zpoint_outputs.stdstar_mag_filter);
661 cpl_msg_info(__func__,
"Star coordinates: RA = %g (%s) ; DEC = %g (%s)",
662 hawki_cal_zpoint_outputs.stdstar_ra, rastr,
663 hawki_cal_zpoint_outputs.stdstar_dec, decstr);
665 if (hawki_cal_zpoint_config.stdstar_given_magnitude < 98.0)
667 hawki_cal_zpoint_outputs.stdstar_mag_available = 1;
668 hawki_cal_zpoint_outputs.stdstar_mag_filter =
669 hawki_cal_zpoint_config.stdstar_given_magnitude;
670 cpl_msg_info(__func__,
"Using user defined "
671 "star magnitude in filter %s : %g [mag]",
672 hawki_cal_zpoint_outputs.filter,
673 hawki_cal_zpoint_outputs.stdstar_mag_filter);
675 cpl_msg_indent_less();
678 cpl_msg_info(__func__,
"Guessing which frame the STD is in for each chip");
680 (
set, hawki_cal_zpoint_outputs.stdstar_ra,
681 hawki_cal_zpoint_outputs.stdstar_dec, labels);
684 cpl_msg_error(__func__,
"Cannot determine which frame the STD is on") ;
690 cpl_msg_indent_more();
691 if(hawki_cal_zpoint_get_expected_pos(
set, labels) != CPL_ERROR_NONE)
693 cpl_msg_error(__func__,
"Could not determine where the star is located");
694 cpl_msg_indent_less();
697 cpl_msg_indent_less();
700 hawki_cal_zpoint_compute_keywords(
set, labels);
703 nima = cpl_frameset_get_size(
set) ;
704 positions = cpl_bivector_new(nima) ;
707 *star_images = cpl_imagelist_new();
710 nframes = cpl_frameset_get_size(
set) ;
711 cpl_msg_info(__func__,
"Loop on the chips");
712 cpl_msg_indent_more() ;
713 for(idet = 0; idet < HAWKI_NB_DETECTORS; ++idet)
715 cpl_imagelist * sky_images;
716 cpl_image * star_ima = NULL;
721 cpl_msg_info(__func__,
"Loading the chip %d", idet+1);
722 cpl_msg_indent_more() ;
725 sky_images = cpl_imagelist_new();
727 cpl_msg_indent_more() ;
728 for (iframe=0 ; iframe<nframes ; iframe++)
736 cpl_bivector_delete(positions) ;
737 cpl_imagelist_delete(*star_images);
738 cpl_imagelist_delete(sky_images) ;
739 cpl_msg_error(__func__,
"Error reading image");
743 size_x = cpl_image_get_size_x(ima_cur) ;
744 size_y = cpl_image_get_size_y(ima_cur) ;
755 cpl_msg_error(__func__,
"Cannot compute stats on ima %d det %d",
757 cpl_bivector_delete(positions) ;
758 cpl_imagelist_delete(*star_images);
759 cpl_imagelist_delete(sky_images) ;
760 cpl_image_delete(ima_cur);
761 cpl_msg_indent_less() ;
766 if(labels[iframe] == idet + 1)
769 iframe_star = iframe;
772 cpl_imagelist_set(sky_images, ima_cur,
773 cpl_imagelist_get_size(sky_images));
775 cpl_msg_indent_less();
778 cpl_msg_info(__func__,
"Correct for the sky");
779 sky = cpl_imagelist_collapse_median_create(sky_images);
780 cpl_imagelist_delete(sky_images) ;
783 cpl_image_subtract(star_ima, sky) ;
784 cpl_image_delete(sky) ;
788 cpl_msg_info(__func__,
"Correct for the flat") ;
793 cpl_msg_error(__func__,
"Cannot get the extension with chip %d",
795 cpl_imagelist_delete(*star_images) ;
796 cpl_bivector_delete(positions) ;
800 flat_im = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, ext_nb) ;
801 cpl_image_divide(star_ima, flat_im) ;
802 cpl_image_delete(flat_im) ;
807 cpl_msg_info(__func__,
"Correct for the bad pixels") ;
810 cpl_msg_error(__func__,
"Cannot correct the BPM for chip %d",
812 cpl_imagelist_delete(*star_images) ;
813 cpl_bivector_delete(positions) ;
819 cpl_imagelist_set(*star_images, star_ima,
820 cpl_imagelist_get_size(*star_images)) ;
823 cpl_msg_info(__func__,
"For chip %d the STD should be on frame %d",
824 idet + 1, iframe_star+1);
825 pos_x_cen = pos_y_cen = -1.0 ;
826 size_x = cpl_image_get_size_x(star_ima) ;
827 size_y = cpl_image_get_size_y(star_ima) ;
830 kernel = cpl_mask_new(3, 3);
831 cpl_mask_not(kernel);
832 filt_ima = cpl_image_new(cpl_image_get_size_x(star_ima),
833 cpl_image_get_size_y(star_ima),
834 cpl_image_get_type(star_ima));
835 cpl_image_filter_mask(filt_ima, star_ima, kernel, CPL_FILTER_MEDIAN,
837 cpl_mask_delete(kernel);
840 aperts = cpl_apertures_extract_sigma(filt_ima,
841 hawki_cal_zpoint_config.detect_sigma) ;
842 cpl_image_delete(filt_ima) ;
845 cpl_msg_error(__func__,
"Cannot find the central object") ;
846 cpl_imagelist_delete(*star_images) ;
847 cpl_bivector_delete(positions) ;
850 min_dist = size_x * size_x + size_y * size_y ;
851 for (iaper=0 ; iaper<cpl_apertures_get_size(aperts) ; iaper++) {
852 pos_x = cpl_apertures_get_centroid_x(aperts, iaper+1) ;
853 pos_y = cpl_apertures_get_centroid_y(aperts, iaper+1) ;
854 dist = (pos_x-hawki_cal_zpoint_config.xcoord[idet])*
855 (pos_x-hawki_cal_zpoint_config.xcoord[idet]) +
856 (pos_y-hawki_cal_zpoint_config.ycoord[idet])*
857 (pos_y-hawki_cal_zpoint_config.ycoord[idet]);
864 cpl_apertures_delete(aperts) ;
866 cpl_vector_set(cpl_bivector_get_x(positions), iframe_star, pos_x_cen) ;
867 cpl_vector_set(cpl_bivector_get_y(positions), iframe_star, pos_y_cen) ;
868 cpl_msg_info(__func__,
"Expected star position: %g %g",
869 hawki_cal_zpoint_config.xcoord[idet],
870 hawki_cal_zpoint_config.ycoord[idet]);
871 cpl_msg_info(__func__,
"Bright object position: %g %g",
872 pos_x_cen, pos_y_cen) ;
875 if(fabs(pos_x_cen - hawki_cal_zpoint_config.xcoord[idet]) >
876 hawki_cal_zpoint_config.sx ||
877 fabs(pos_y_cen - hawki_cal_zpoint_config.ycoord[idet]) >
878 hawki_cal_zpoint_config.sy)
880 hawki_cal_zpoint_outputs.stdstar_image_detected[idet] = 0;
881 cpl_msg_warning(cpl_func,
"No object has been found within the box"
882 "limits [%d, %d] around the expected position",
883 hawki_cal_zpoint_config.sx,
884 hawki_cal_zpoint_config.sy);
888 hawki_cal_zpoint_outputs.stdstar_image_detected[idet] = 1;
892 cpl_msg_indent_less() ;
894 cpl_msg_indent_less() ;
897 star_images_frame_order = cpl_imagelist_new() ;
898 for (iframe=0 ; iframe< nframes; iframe++)
900 tmp_ima = cpl_image_duplicate
901 (cpl_imagelist_get(*star_images, labels[iframe] -1 ));
902 cpl_imagelist_set(star_images_frame_order, tmp_ima, iframe);
906 cpl_msg_info(__func__,
"Compute the photometry") ;
907 cpl_msg_indent_more() ;
908 if ((zpoint_tables = hawki_cal_zpoint_photom
909 (star_images_frame_order, positions, labels))==NULL) {
910 cpl_msg_error(__func__,
"Cannot reduce") ;
911 cpl_bivector_delete(positions) ;
912 cpl_imagelist_delete(star_images_frame_order) ;
913 cpl_msg_indent_less() ;
919 cpl_msg_indent_less() ;
920 cpl_bivector_delete(positions) ;
921 cpl_imagelist_delete(star_images_frame_order) ;
923 return zpoint_tables;
937 static cpl_table ** hawki_cal_zpoint_photom(
938 cpl_imagelist * ilist,
952 double bgd, fl, zp, peak, fwhm_x, fwhm_y ;
953 cpl_bivector * iqe_res ;
958 if (ilist == NULL)
return NULL ;
959 if (pos == NULL)
return NULL ;
962 nframes = cpl_imagelist_get_size(ilist) ;
963 stdstar_mag = hawki_cal_zpoint_outputs.stdstar_mag_filter;
964 dit = hawki_cal_zpoint_outputs.dit ;
965 pixscale = hawki_cal_zpoint_outputs.pixscale ;
968 switch (hawki_cal_zpoint_outputs.band) {
969 case HAWKI_BAND_J: extinction = 0.098 ; break ;
970 case HAWKI_BAND_H: extinction = 0.039 ; break ;
971 case HAWKI_BAND_K: extinction = 0.065 ; break ;
972 case HAWKI_BAND_Y: extinction = 0.00 ; break ;
973 default: extinction = 0.00 ; break ;
975 hawki_cal_zpoint_outputs.ext_coeff = extinction;
976 cpl_msg_info(__func__,
"Using tabulated extinction for band %s: %f",
977 hawki_cal_zpoint_outputs.filter, extinction);
980 for (iframe=0 ; iframe<nframes ; iframe++) {
981 idet = labels[iframe]-1;
982 if(hawki_cal_zpoint_outputs.stdstar_image_detected[idet] == 1)
985 ima = cpl_imagelist_get(ilist, iframe) ;
988 pos_x = cpl_bivector_get_x_data(pos) ;
989 pos_y = cpl_bivector_get_y_data(pos) ;
992 iqe_res = cpl_image_iqe
993 (ima, (
int)(pos_x[iframe]-10.0), (
int)(pos_y[iframe]-10.0),
994 (
int)(pos_x[iframe]+10.0), (
int)(pos_y[iframe]+10.0));
997 cpl_msg_debug(__func__,
"Cannot compute FWHM for chip %d",
999 fwhm_x = fwhm_y = -1.0 ;
1002 fwhm_x = cpl_vector_get(cpl_bivector_get_x(iqe_res), 2) ;
1003 fwhm_y = cpl_vector_get(cpl_bivector_get_x(iqe_res), 3) ;
1004 cpl_bivector_delete(iqe_res) ;
1008 r = hawki_cal_zpoint_config.phot_star_radius ;
1010 if (fwhm_x>0 && fwhm_y>0) r = 5*(fwhm_x+fwhm_y)/2.0 ;
1011 else r = HAWKI_PHOT_STAR_RADIUS ;
1013 r1 = hawki_cal_zpoint_config.phot_bg_r1 ;
1014 r2 = hawki_cal_zpoint_config.phot_bg_r2 ;
1015 if (r1 < 0) r1 = r + 10.0 ;
1016 if (r2 < 0) r2 = r1 + 20.0 ;
1022 bgd = irplib_strehl_ring_background(ima, (
int)(pos_x[iframe]),
1023 (
int)(pos_y[iframe]), (
int)r1, (
int)r2, IRPLIB_BG_METHOD_MEDIAN);
1025 fl = irplib_strehl_disk_flux(ima,
1026 (
int)(pos_x[iframe]), (
int)(pos_y[iframe]), (
int)r, bgd);
1030 peak = cpl_image_get_max_window(ima,
1031 (
int)(pos_x[iframe]-5), (
int)(pos_y[iframe]-5),
1032 (
int)(pos_x[iframe]+5), (
int)(pos_y[iframe]+5));
1035 if (hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
1037 if (fl > 0 && dit > 0)
1039 hawki_cal_zpoint_outputs.zpoint_computable[idet] = 1;
1040 zp = stdstar_mag + 2.5 * log10(fl) - 2.5 * log10(dit);
1042 hawki_cal_zpoint_outputs.zpoint[idet] = zp;
1043 hawki_cal_zpoint_outputs.atx0[idet] = zp +
1044 extinction * hawki_cal_zpoint_outputs.airmass[idet];
1047 hawki_cal_zpoint_outputs.zpoint_computable[idet] = 0;
1049 hawki_cal_zpoint_outputs.posx[idet] = pos_x[iframe];
1050 hawki_cal_zpoint_outputs.posy[idet] = pos_y[iframe];
1051 hawki_cal_zpoint_outputs.flux[idet] = fl;
1052 hawki_cal_zpoint_outputs.instrmag[idet] = 2.5 * log10(fl/dit);
1053 hawki_cal_zpoint_outputs.peak[idet] = peak;
1054 hawki_cal_zpoint_outputs.bgd[idet] = bgd;
1055 hawki_cal_zpoint_outputs.fwhmx[idet] = fwhm_x;
1056 hawki_cal_zpoint_outputs.fwhmy[idet] = fwhm_y;
1057 if (fwhm_x > 0 && fwhm_y > 0)
1058 hawki_cal_zpoint_outputs.fwhm[idet] = sqrt(fwhm_x*fwhm_y);
1060 hawki_cal_zpoint_outputs.fwhm[idet] = -1.0;
1061 hawki_cal_zpoint_outputs.fwhmx_as[idet] = fwhm_x * pixscale;
1062 hawki_cal_zpoint_outputs.fwhmy_as[idet] = fwhm_y * pixscale;
1063 if (fwhm_x > 0 && fwhm_y > 0)
1064 hawki_cal_zpoint_outputs.fwhm_as[idet] =
1065 sqrt(fwhm_x*fwhm_y*pixscale*pixscale);
1067 hawki_cal_zpoint_outputs.fwhm_as[labels[iframe]-1] = -1.0;
1071 cpl_msg_warning(cpl_func,
"Standard star not detected in chip %d. "
1072 "No zeropoint computed.", idet + 1);
1080 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
1081 cpl_table_new_column(zpoint[idet],
1082 HAWKI_COL_ZPOINT_CHIP, CPL_TYPE_INT);
1083 cpl_table_new_column(zpoint[idet],
1084 HAWKI_COL_ZPOINT_STARNAME, CPL_TYPE_STRING);
1085 cpl_table_new_column(zpoint[idet],
1086 HAWKI_COL_ZPOINT_POSX, CPL_TYPE_DOUBLE);
1087 cpl_table_set_column_unit(zpoint[idet],
1088 HAWKI_COL_ZPOINT_POSX,
"pix");
1089 cpl_table_new_column(zpoint[idet],
1090 HAWKI_COL_ZPOINT_POSY, CPL_TYPE_DOUBLE);
1091 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_POSY,
"pix");
1092 cpl_table_new_column(zpoint[idet],
1093 HAWKI_COL_ZPOINT_ZPOINT, CPL_TYPE_DOUBLE);
1094 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_ZPOINT,
"mag");
1095 cpl_table_new_column(zpoint[idet],
1096 HAWKI_COL_ZPOINT_ATX0, CPL_TYPE_DOUBLE);
1097 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_ATX0,
"mag");
1098 cpl_table_new_column(zpoint[idet],
1099 HAWKI_COL_ZPOINT_AIRMASS, CPL_TYPE_DOUBLE);
1100 cpl_table_new_column(zpoint[idet],
1101 HAWKI_COL_ZPOINT_FLUX, CPL_TYPE_DOUBLE);
1102 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FLUX,
"ADU");
1103 cpl_table_new_column(zpoint[idet],
1104 HAWKI_COL_ZPOINT_INSTRMAG, CPL_TYPE_DOUBLE);
1105 cpl_table_set_column_unit(zpoint[idet],
1106 HAWKI_COL_ZPOINT_INSTRMAG,
"log(ADU/s)");
1107 cpl_table_new_column(zpoint[idet],
1108 HAWKI_COL_ZPOINT_FILTER, CPL_TYPE_STRING);
1109 cpl_table_new_column(zpoint[idet],
1110 HAWKI_COL_ZPOINT_PEAK, CPL_TYPE_DOUBLE);
1111 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_PEAK,
"ADU");
1112 cpl_table_new_column(zpoint[idet],
1113 HAWKI_COL_ZPOINT_BGD, CPL_TYPE_DOUBLE);
1114 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_BGD,
"ADU");
1115 cpl_table_new_column(zpoint[idet],
1116 HAWKI_COL_ZPOINT_FWHMX, CPL_TYPE_DOUBLE);
1117 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMX,
"pix");
1118 cpl_table_new_column(zpoint[idet],
1119 HAWKI_COL_ZPOINT_FWHMY, CPL_TYPE_DOUBLE);
1120 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMY,
"pix");
1121 cpl_table_new_column(zpoint[idet],
1122 HAWKI_COL_ZPOINT_FWHM, CPL_TYPE_DOUBLE);
1123 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHM,
"pix");
1124 cpl_table_new_column(zpoint[idet],
1125 HAWKI_COL_ZPOINT_FWHMX_AS, CPL_TYPE_DOUBLE);
1126 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMX_AS,
"arcsec");
1127 cpl_table_new_column(zpoint[idet],
1128 HAWKI_COL_ZPOINT_FWHMY_AS, CPL_TYPE_DOUBLE);
1129 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHMY_AS,
"arcsec");
1130 cpl_table_new_column(zpoint[idet],
1131 HAWKI_COL_ZPOINT_FWHM_AS, CPL_TYPE_DOUBLE);
1132 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_FWHM_AS,
"arcsec");
1133 cpl_table_new_column(zpoint[idet],
1134 HAWKI_COL_ZPOINT_STARMAG, CPL_TYPE_DOUBLE);
1135 cpl_table_set_column_unit(zpoint[idet],HAWKI_COL_ZPOINT_STARMAG,
"mag");
1137 cpl_table_set_int(zpoint[idet], HAWKI_COL_ZPOINT_CHIP, 0, idet+1) ;
1138 cpl_table_set_string(zpoint[idet], HAWKI_COL_ZPOINT_FILTER, 0,
1139 hawki_cal_zpoint_outputs.filter);
1140 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_AIRMASS, 0,
1141 hawki_cal_zpoint_outputs.airmass[idet]) ;
1142 if(hawki_cal_zpoint_outputs.stdstar_incat_found== 1)
1144 cpl_table_set_string(zpoint[idet], HAWKI_COL_ZPOINT_STARNAME, 0,
1145 hawki_cal_zpoint_outputs.starname) ;
1146 if(hawki_cal_zpoint_outputs.stdstar_mag_available== 1)
1148 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_STARMAG, 0,
1149 hawki_cal_zpoint_outputs.stdstar_mag_filter);
1152 if(hawki_cal_zpoint_outputs.stdstar_image_detected[idet] == 1)
1154 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_POSX, 0,
1155 hawki_cal_zpoint_outputs.posx[idet]) ;
1156 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_POSY, 0,
1157 hawki_cal_zpoint_outputs.posy[idet]) ;
1158 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FLUX, 0,
1159 hawki_cal_zpoint_outputs.flux[idet]) ;
1160 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_INSTRMAG, 0,
1161 hawki_cal_zpoint_outputs.instrmag[idet]) ;
1162 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_PEAK, 0,
1163 hawki_cal_zpoint_outputs.peak[idet]) ;
1164 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_BGD, 0,
1165 hawki_cal_zpoint_outputs.bgd[idet]) ;
1166 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX, 0,
1167 hawki_cal_zpoint_outputs.fwhmx[idet]) ;
1168 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY, 0,
1169 hawki_cal_zpoint_outputs.fwhmy[idet]) ;
1170 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHM, 0,
1171 hawki_cal_zpoint_outputs.fwhm[idet]) ;
1172 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMX_AS, 0,
1173 hawki_cal_zpoint_outputs.fwhmx_as[idet]) ;
1174 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHMY_AS, 0,
1175 hawki_cal_zpoint_outputs.fwhmy_as[idet]) ;
1176 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_FWHM_AS, 0,
1177 hawki_cal_zpoint_outputs.fwhm_as[idet]) ;
1178 if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
1180 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_ZPOINT, 0,
1181 hawki_cal_zpoint_outputs.zpoint[idet]);
1182 cpl_table_set_double(zpoint[idet], HAWKI_COL_ZPOINT_ATX0, 0,
1183 hawki_cal_zpoint_outputs.atx0[idet]);
1189 if(hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
1192 hawki_cal_zpoint_outputs.mean_zpoint = 0.0 ;
1193 hawki_cal_zpoint_outputs.mean_atx0 = 0.0 ;
1194 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
1195 if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
1198 hawki_cal_zpoint_outputs.mean_zpoint +=
1199 hawki_cal_zpoint_outputs.zpoint[idet] ;
1200 hawki_cal_zpoint_outputs.mean_atx0 +=
1201 hawki_cal_zpoint_outputs.atx0[idet] ;
1207 hawki_cal_zpoint_outputs.zpoint_mean_computable = 1;
1208 hawki_cal_zpoint_outputs.mean_zpoint /= nzpoint ;
1209 hawki_cal_zpoint_outputs.mean_atx0 /= nzpoint ;
1214 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1216 if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
1218 cpl_msg_info(__func__,
"Zero point [at airmass=1] in chip %d:",
1220 cpl_msg_indent_more();
1221 cpl_msg_info(__func__,
" ZP: %g [%g]",
1222 hawki_cal_zpoint_outputs.zpoint[idet],
1223 hawki_cal_zpoint_outputs.atx0[idet]);
1224 cpl_msg_info(__func__,
" Flux of star: %f",
1225 hawki_cal_zpoint_outputs.flux[idet]);
1226 cpl_msg_indent_less();
1229 cpl_msg_info(__func__,
"Zero point not available for chip %d",
1246 static int hawki_cal_zpoint_get_mag(
1247 const char * std_file,
1249 double pointing_dec,
1252 cpl_table * catalogue;
1254 char * used_catname;
1266 hawki_cal_zpoint_outputs.stdstar_incat_found = 0;
1269 if ((catalogue = irplib_stdstar_load_catalog(std_file,
"all")) == NULL) {
1270 cpl_msg_error(cpl_func,
"Cannot read catalogue file %s",std_file);
1275 if (irplib_stdstar_check_columns_exist(catalogue) != CPL_ERROR_NONE) {
1276 cpl_msg_error(cpl_func,
"Note all the columns are present"
1277 " in the catalogue file %s",std_file);
1282 if (irplib_stdstar_select_stars_dist(catalogue,
1283 pointing_ra, pointing_dec, 5.0) == -1) {
1284 cpl_table_delete(catalogue) ;
1289 if ((star_ind=irplib_stdstar_find_closest(catalogue,
1290 pointing_ra, pointing_dec)) < 0) {
1291 cpl_table_delete(catalogue) ;
1296 hawki_cal_zpoint_outputs.stdstar_incat_found = 1;
1297 star_name = cpl_strdup(cpl_table_get_string(catalogue,
1298 IRPLIB_STDSTAR_STAR_COL, star_ind));
1299 used_catname = cpl_strdup(cpl_table_get_string
1300 (catalogue, IRPLIB_STDSTAR_CAT_COL, star_ind));
1301 star_type = cpl_strdup(cpl_table_get_string(catalogue,
1302 IRPLIB_STDSTAR_TYPE_COL, star_ind));
1303 stdstar_ra = cpl_table_get_double(catalogue,
1304 IRPLIB_STDSTAR_RA_COL, star_ind, NULL);
1305 stdstar_dec = cpl_table_get_double(catalogue,
1306 IRPLIB_STDSTAR_DEC_COL, star_ind, NULL);
1308 star_ind, &mag_invalid);
1319 strncpy(hawki_cal_zpoint_outputs.starname, star_name, 510);
1320 strncpy(hawki_cal_zpoint_outputs.sptype, star_type, 510);
1321 strncpy(hawki_cal_zpoint_outputs.catalog, used_catname, 510);
1322 hawki_cal_zpoint_outputs.stdstar_ra = stdstar_ra;
1323 hawki_cal_zpoint_outputs.stdstar_dec = stdstar_dec;
1324 if(mag_invalid == 0)
1326 hawki_cal_zpoint_outputs.stdstar_mag_available = 1;
1327 hawki_cal_zpoint_outputs.stdstar_mag_filter = mag_filter;
1330 hawki_cal_zpoint_outputs.stdstar_mag_available = 0;
1331 hawki_cal_zpoint_outputs.stdstar_mag_H = mag_H;
1332 hawki_cal_zpoint_outputs.stdstar_mag_J = mag_J;
1333 hawki_cal_zpoint_outputs.stdstar_mag_K = mag_K;
1334 hawki_cal_zpoint_outputs.stdstar_mag_Y = mag_Y;
1335 cpl_free(star_name);
1336 cpl_free(star_type);
1337 cpl_free(used_catname);
1338 cpl_table_delete(catalogue);
1353 static int hawki_cal_zpoint_save
1354 (cpl_table ** zpoint_tables,
1356 cpl_imagelist * images,
1357 cpl_table ** raw_zpoint_stats,
1358 cpl_frameset * zpoint_frames,
1359 cpl_frameset * calib_frames,
1360 const cpl_frame * stars_frame,
1361 cpl_parameterlist * parlist,
1364 cpl_propertylist * qcmainparams;
1365 cpl_propertylist ** qcextparams;
1366 cpl_propertylist * mainheader;
1367 cpl_propertylist ** extheaders;
1368 cpl_propertylist ** statsqcextparams;
1369 cpl_propertylist ** statsextheaders;
1370 cpl_frameset * used_frames;
1371 const char * ref_filename;
1372 const char * recipe_name =
"hawki_cal_zpoint" ;
1375 cpl_errorstate error_prevstate = cpl_errorstate_get();
1382 qcmainparams = cpl_propertylist_new();
1383 qcextparams = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_propertylist*)) ;
1384 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1385 qcextparams[idet] = cpl_propertylist_new();
1387 hawki_cal_zpoint_compute_qc(qcmainparams, qcextparams,
set);
1390 statsqcextparams = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_propertylist*)) ;
1391 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1392 statsqcextparams[idet] = cpl_propertylist_new();
1396 mainheader = cpl_propertylist_new();
1397 extheaders = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_propertylist*)) ;
1398 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1399 extheaders[idet] = cpl_propertylist_new();
1401 cpl_propertylist_append(mainheader, qcmainparams);
1402 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1403 cpl_propertylist_append(extheaders[idet], qcextparams[idet]) ;
1406 statsextheaders = cpl_malloc(HAWKI_NB_DETECTORS *
sizeof(cpl_propertylist*)) ;
1407 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1408 statsextheaders[idet] = cpl_propertylist_duplicate(statsqcextparams[idet]);
1411 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1413 cpl_propertylist * inputlist;
1414 cpl_propertylist * wcslist;
1415 int this_iframe = -1;
1420 inputlist = cpl_propertylist_load_regexp(ref_filename, ext_nb,
1421 HAWKI_HEADER_EXT_FORWARD, 0);
1422 cpl_propertylist_append(extheaders[idet], inputlist);
1423 cpl_propertylist_append(statsextheaders[idet], inputlist);
1424 cpl_propertylist_delete(inputlist);
1427 for(iframe=0; iframe<cpl_frameset_get_size(zpoint_frames); iframe++)
1428 if(labels[iframe] == idet + 1)
1429 this_iframe = iframe;
1430 wcslist = cpl_propertylist_load_regexp
1431 (cpl_frame_get_filename(cpl_frameset_get_frame(zpoint_frames, this_iframe)),
1432 ext_nb, HAWKI_HEADER_WCS, 0);
1433 cpl_propertylist_copy_property_regexp
1434 (extheaders[idet], wcslist, HAWKI_HEADER_WCS, 0);
1435 cpl_propertylist_delete(wcslist);
1439 used_frames = cpl_frameset_duplicate(zpoint_frames);
1440 for(iframe = 0; iframe< cpl_frameset_get_size(calib_frames); ++iframe)
1441 cpl_frameset_insert(used_frames, cpl_frame_duplicate(
1442 cpl_frameset_get_frame(calib_frames, iframe)));
1443 cpl_frameset_insert(used_frames, cpl_frame_duplicate(stars_frame));
1447 (
const cpl_table **)zpoint_tables,
1449 HAWKI_CALPRO_ZPOINT_TAB,
1450 HAWKI_PROTYPE_ZPOINT_TAB,
1452 (
const cpl_propertylist **)extheaders,
1453 "hawki_cal_zpoint.fits");
1454 cpl_frameset_delete(used_frames);
1457 used_frames = cpl_frameset_duplicate(zpoint_frames);
1461 (
const cpl_table **)raw_zpoint_stats,
1463 HAWKI_CALPRO_ZPOINT_STATS,
1464 HAWKI_PROTYPE_ZPOINT_STATS,
1466 (
const cpl_propertylist **)statsextheaders,
1467 "hawki_cal_zpoint_stats.fits");
1470 for(iframe = 0; iframe< cpl_frameset_get_size(calib_frames); ++iframe)
1471 cpl_frameset_insert(used_frames, cpl_frame_duplicate(
1472 cpl_frameset_get_frame(calib_frames, iframe)));
1478 HAWKI_CALPRO_ZPOINT_IMA,
1479 HAWKI_PROTYPE_ZPOINT_IMA,
1482 "hawki_cal_zpoint_check.fits") ;
1483 cpl_frameset_delete(used_frames);
1486 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++) {
1487 cpl_propertylist_delete(extheaders[idet]) ;
1488 cpl_propertylist_delete(qcextparams[idet]) ;
1489 cpl_propertylist_delete(statsqcextparams[idet]);
1490 cpl_propertylist_delete(statsextheaders[idet]);
1492 cpl_propertylist_delete(mainheader);
1493 cpl_propertylist_delete(qcmainparams);
1494 cpl_free(extheaders);
1495 cpl_free(qcextparams);
1496 cpl_free(statsqcextparams);
1497 cpl_free(statsextheaders);
1500 if(!cpl_errorstate_is_equal(error_prevstate))
1502 cpl_errorstate_set(CPL_ERROR_NONE);
1516 static int hawki_cal_zpoint_compute_qc
1517 (cpl_propertylist * qcmainparams,
1518 cpl_propertylist ** qcextparams,
1525 nframes = cpl_frameset_get_size(
set) ;
1528 if (qcmainparams == NULL)
return -1;
1529 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1530 if (qcextparams[idet] == NULL)
return -1;
1533 cpl_propertylist_append_string(qcmainparams,
"ESO QC FILTER OBS",
1534 hawki_cal_zpoint_outputs.filter);
1535 cpl_propertylist_set_comment(qcmainparams,
"ESO QC FILTER OBS",
1536 "Observation filter");
1537 cpl_propertylist_append_string(qcmainparams,
"ESO QC FILTER REF",
1539 cpl_propertylist_set_comment(qcmainparams,
"ESO QC FILTER REF",
1540 "Reference filter");
1541 cpl_propertylist_append_double(qcmainparams,
"ESO QC AMBI RHUM AVG",
1542 hawki_cal_zpoint_outputs.humidity);
1543 cpl_propertylist_set_comment(qcmainparams,
"ESO QC AMBI RHUM AVG",
1544 "(percent) ambient relative humidity @ 30/2 m");
1545 cpl_propertylist_append_double(qcmainparams,
"ESO QC AIRMASS MEAN",
1546 hawki_cal_zpoint_outputs.mean_airmass) ;
1547 cpl_propertylist_set_comment(qcmainparams,
"ESO QC AIRMASS MEAN",
1549 cpl_propertylist_append_double(qcmainparams,
"ESO QC DATANCOM",
1551 cpl_propertylist_set_comment(qcmainparams,
"ESO QC DATANCOM",
1552 "Number of files used for the reduction");
1553 if(hawki_cal_zpoint_outputs.stdstar_incat_found == 1)
1555 cpl_propertylist_append_string(qcmainparams,
"ESO QC STDNAME",
1556 hawki_cal_zpoint_outputs.starname) ;
1557 cpl_propertylist_set_comment(qcmainparams,
"ESO QC STDNAME",
1558 "Standard star name");
1559 cpl_propertylist_append_string(qcmainparams,
"ESO QC SPECTYPE",
1560 hawki_cal_zpoint_outputs.sptype) ;
1561 cpl_propertylist_set_comment(qcmainparams,
"ESO QC SPECTYPE",
1562 "Standard star spectral type");
1563 cpl_propertylist_append_double(qcmainparams,
"ESO QC STARMAG H",
1564 hawki_cal_zpoint_outputs.stdstar_mag_H) ;
1565 cpl_propertylist_set_comment(qcmainparams,
"ESO QC STARMAG H",
1566 "Standard star magnitude in H");
1567 cpl_propertylist_append_double(qcmainparams,
"ESO QC STARMAG J",
1568 hawki_cal_zpoint_outputs.stdstar_mag_J) ;
1569 cpl_propertylist_set_comment(qcmainparams,
"ESO QC STARMAG J",
1570 "Standard star magnitude in J");
1571 cpl_propertylist_append_double(qcmainparams,
"ESO QC STARMAG K",
1572 hawki_cal_zpoint_outputs.stdstar_mag_K) ;
1573 cpl_propertylist_set_comment(qcmainparams,
"ESO QC STARMAG K",
1574 "Standard star magnitude in K");
1575 cpl_propertylist_append_double(qcmainparams,
"ESO QC STARMAG Y",
1576 hawki_cal_zpoint_outputs.stdstar_mag_Y) ;
1577 cpl_propertylist_set_comment(qcmainparams,
"ESO QC STARMAG Y",
1578 "Standard star magnitude in Y");
1579 cpl_propertylist_append_string(qcmainparams,
"ESO QC CATNAME",
1580 hawki_cal_zpoint_outputs.catalog) ;
1581 cpl_propertylist_set_comment(qcmainparams,
"ESO QC CATNAME",
1582 "Standard star catalogue name");
1584 if(hawki_cal_zpoint_outputs.stdstar_mag_available == 1)
1586 cpl_propertylist_append_double(qcmainparams,
"ESO QC STARMAG",
1587 hawki_cal_zpoint_outputs.stdstar_mag_filter) ;
1588 cpl_propertylist_set_comment(qcmainparams,
"ESO QC STARMAG",
1589 "Standard star magnitude in observed filter");
1591 if(hawki_cal_zpoint_outputs.zpoint_mean_computable == 1)
1593 cpl_propertylist_append_double(qcmainparams,
"ESO QC ZPOINT MEAN",
1594 hawki_cal_zpoint_outputs.mean_zpoint) ;
1595 cpl_propertylist_set_comment(qcmainparams,
"ESO QC ZPOINT MEAN",
1596 "Mean measured zero-point for all the chips [mag]");
1597 cpl_propertylist_append_double(qcmainparams,
"ESO QC ATX0 MEAN",
1598 hawki_cal_zpoint_outputs.mean_atx0);
1599 cpl_propertylist_set_comment(qcmainparams,
"ESO QC ATX0 MEAN",
1600 "Mean extinction corrected zero-point for all the chips [mag]");
1601 cpl_propertylist_append_double(qcmainparams,
"ESO QC ZPOINT EXT COEFF",
1602 hawki_cal_zpoint_outputs.ext_coeff);
1603 cpl_propertylist_set_comment(qcmainparams,
"ESO QC ZPOINT EXT COEFF",
1604 "Extinction coefficient used in the computation of ATX0");
1608 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1610 if(hawki_cal_zpoint_outputs.zpoint_computable[idet] == 1)
1612 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT",
1613 hawki_cal_zpoint_outputs.zpoint[idet]) ;
1614 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT",
1615 "Measured zero-point for a given chip [mag]");
1616 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ATX0",
1617 hawki_cal_zpoint_outputs.atx0[idet]);
1618 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ATX0",
1619 "Extinction corrected zero-point for a given chip [mag]");
1621 if(hawki_cal_zpoint_outputs.stdstar_image_detected[idet] == 1)
1623 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT POSX",
1624 hawki_cal_zpoint_outputs.posx[idet]);
1625 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT POSX",
1626 "X position of the standard star [pixel]");
1627 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT POSY",
1628 hawki_cal_zpoint_outputs.posy[idet]);
1629 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT POSY",
1630 "Y position of the standard star [pixel]");
1631 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT FLUX",
1632 hawki_cal_zpoint_outputs.flux[idet]);
1633 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT FLUX",
1634 "Flux of the standard star [ADU]");
1635 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT PEAK",
1636 hawki_cal_zpoint_outputs.peak[idet]) ;
1637 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT PEAK",
1638 "Peak of the standard star [ADU]");
1639 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT BGD",
1640 hawki_cal_zpoint_outputs.bgd[idet]) ;
1641 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT BGD",
1642 "Background around the standard star [ADU]");
1643 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT FWHMX",
1644 hawki_cal_zpoint_outputs.fwhmx[idet]);
1645 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT FWHMX",
1646 "X FWHM of the standard star [pixel]");
1647 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT FWHMY",
1648 hawki_cal_zpoint_outputs.fwhmy[idet]);
1649 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT FWHMY",
1650 "Y FWHM of the standard star [pixel]");
1651 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT FWHM",
1652 hawki_cal_zpoint_outputs.fwhm[idet]);
1653 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT FWHM",
1654 "FWHM of the standard star [pixel]");
1655 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT FWHMX_AS",
1656 hawki_cal_zpoint_outputs.fwhmx_as[idet]);
1657 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT FWHMX_AS",
1658 "X FWHM of the standard star [arcsec]");
1659 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT FWHMY_AS",
1660 hawki_cal_zpoint_outputs.fwhmy_as[idet]);
1661 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT FWHMY_AS",
1662 "Y FWHM of the standard star [arcsec]");
1663 cpl_propertylist_append_double(qcextparams[idet],
"ESO QC ZPOINT FWHM_AS",
1664 hawki_cal_zpoint_outputs.fwhm_as[idet]);
1665 cpl_propertylist_set_comment(qcextparams[idet],
"ESO QC ZPOINT FWHM_AS",
1666 "FWHM of the standard star [arcsec]");
1679 static int hawki_cal_zpoint_compute_keywords(
1684 cpl_vector * hum_vec ;
1685 cpl_frame * cur_frame ;
1686 cpl_propertylist * plist ;
1690 if (
set == NULL)
return -1 ;
1693 nframes = cpl_frameset_get_size(
set) ;
1694 hawki_cal_zpoint_outputs.mean_airmass = 0.0 ;
1696 hum_vec = cpl_vector_new(nframes) ;
1698 for (iframe=0 ; iframe<nframes ; iframe++) {
1699 if (cpl_error_get_code()) {
1700 cpl_vector_delete(hum_vec) ;
1703 cur_frame = cpl_frameset_get_frame(
set, iframe) ;
1704 plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0) ;
1706 hawki_cal_zpoint_outputs.mean_airmass +=
1708 if (iframe==nframes-1)
1709 hawki_cal_zpoint_outputs.mean_airmass +=
1711 hawki_cal_zpoint_outputs.airmass[labels[iframe] - 1] =
1715 cpl_propertylist_delete(plist) ;
1716 if (cpl_error_get_code()) {
1717 cpl_vector_delete(hum_vec) ;
1722 hawki_cal_zpoint_outputs.humidity = cpl_vector_get_mean(hum_vec) ;
1723 hawki_cal_zpoint_outputs.mean_airmass /= 2 ;
1726 cpl_vector_delete(hum_vec) ;
1727 if (cpl_error_get_code())
return -1 ;
1731 static cpl_error_code hawki_cal_zpoint_get_expected_pos
1732 (cpl_frameset *
set,
1735 const char * filename;
1738 for(iframe=0 ; iframe<HAWKI_NB_DETECTORS ; iframe++)
1740 cpl_propertylist * wcs_plist;
1744 idet = labels[iframe];
1745 if(hawki_cal_zpoint_config.xcoord[idet - 1] == -1 ||
1746 hawki_cal_zpoint_config.ycoord[idet - 1] == -1)
1748 filename = cpl_frame_get_filename
1749 (cpl_frameset_get_frame_const(
set, iframe));
1750 wcs_plist = cpl_propertylist_load
1752 wcs = cpl_wcs_new_from_propertylist(wcs_plist);
1753 cpl_propertylist_delete(wcs_plist);
1756 cpl_msg_error(__func__,
"Could not get WCS info");
1757 cpl_wcs_delete(wcs);
1758 return CPL_ERROR_ILLEGAL_INPUT;
1760 if(irplib_wcs_radectoxy(wcs,
1761 hawki_cal_zpoint_outputs.stdstar_ra,
1762 hawki_cal_zpoint_outputs.stdstar_dec,
1763 &(hawki_cal_zpoint_config.xcoord[idet - 1]),
1764 &(hawki_cal_zpoint_config.ycoord[idet - 1]))
1767 cpl_msg_error(__func__,
"Could not get the expected position of star");
1768 cpl_wcs_delete(wcs);
1769 return CPL_ERROR_UNSPECIFIED;
1771 cpl_msg_info(cpl_func,
1772 "Star expected position in detector %d is X=%f Y=%f",
1773 idet, hawki_cal_zpoint_config.xcoord[idet - 1],
1774 hawki_cal_zpoint_config.ycoord[idet - 1]);
1777 cpl_wcs_delete(wcs);
1781 cpl_msg_info(cpl_func,
1782 "Using given star position in detector %d: X=%f Y=%f",
1784 hawki_cal_zpoint_config.xcoord[idet - 1],
1785 hawki_cal_zpoint_config.ycoord[idet - 1]);
1794 static void hawki_cal_zpoint_output_init(
void)
1798 hawki_cal_zpoint_outputs.starname[0] = (char)0 ;
1799 hawki_cal_zpoint_outputs.sptype[0] = (char)0 ;
1800 hawki_cal_zpoint_outputs.filter[0] = (char)0 ;
1801 hawki_cal_zpoint_outputs.catalog[0] = (char)0 ;
1802 hawki_cal_zpoint_outputs.pixscale = -1.0 ;
1803 hawki_cal_zpoint_outputs.dit = -1.0 ;
1804 hawki_cal_zpoint_outputs.humidity = -1.0 ;
1805 hawki_cal_zpoint_outputs.mean_airmass = -1.0 ;
1806 for (idet=0 ; idet<HAWKI_NB_DETECTORS ; idet++)
1808 hawki_cal_zpoint_outputs.airmass[idet] = -1.0 ;
1809 hawki_cal_zpoint_outputs.zpoint[idet] = -1.0 ;
1810 hawki_cal_zpoint_outputs.atx0[idet] = -1.0 ;
1811 hawki_cal_zpoint_outputs.posx[idet] = -1.0 ;
1812 hawki_cal_zpoint_outputs.posy[idet] = -1.0 ;
1813 hawki_cal_zpoint_outputs.flux[idet] = -1.0 ;
1814 hawki_cal_zpoint_outputs.peak[idet] = -1.0 ;
1815 hawki_cal_zpoint_outputs.bgd[idet] = -1.0 ;
1816 hawki_cal_zpoint_outputs.fwhmx[idet] = -1.0 ;
1817 hawki_cal_zpoint_outputs.fwhmy[idet] = -1.0 ;
1818 hawki_cal_zpoint_outputs.fwhm[idet] = -1.0 ;
1819 hawki_cal_zpoint_outputs.fwhmx_as[idet] = -1.0 ;
1820 hawki_cal_zpoint_outputs.fwhmy_as[idet] = -1.0 ;
1821 hawki_cal_zpoint_outputs .fwhm_as[idet] = -1.0 ;
1825 int hawki_cal_zpoint_retrieve_input_param
1826 (cpl_parameterlist * parlist)
1828 cpl_parameter * par ;
1834 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.ra") ;
1835 hawki_cal_zpoint_config.target_ra = cpl_parameter_get_double(par) ;
1837 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.dec") ;
1838 hawki_cal_zpoint_config.target_dec = cpl_parameter_get_double(par) ;
1840 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.mag") ;
1841 hawki_cal_zpoint_config.stdstar_given_magnitude =
1842 cpl_parameter_get_double(par) ;
1844 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.detect_sigma");
1845 hawki_cal_zpoint_config.detect_sigma = cpl_parameter_get_double(par) ;
1847 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.sx") ;
1848 hawki_cal_zpoint_config.sx = cpl_parameter_get_int(par) ;
1850 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.sy") ;
1851 hawki_cal_zpoint_config.sy = cpl_parameter_get_int(par) ;
1853 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.star_r") ;
1854 hawki_cal_zpoint_config.phot_star_radius = cpl_parameter_get_double(par) ;
1856 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.bg_r1") ;
1857 hawki_cal_zpoint_config.phot_bg_r1 = cpl_parameter_get_double(par) ;
1859 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.bg_r2") ;
1860 hawki_cal_zpoint_config.phot_bg_r2 = cpl_parameter_get_double(par) ;
1862 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.xcoord");
1863 sval = cpl_parameter_get_string(par);
1864 if (sscanf(sval,
"%lf,%lf,%lf,%lf",
1865 hawki_cal_zpoint_config.xcoord,
1866 hawki_cal_zpoint_config.xcoord+1,
1867 hawki_cal_zpoint_config.xcoord+2,
1868 hawki_cal_zpoint_config.xcoord+3)!=4)
1873 par = cpl_parameterlist_find(parlist,
"hawki.hawki_cal_zpoint.ycoord");
1874 sval = cpl_parameter_get_string(par);
1875 if (sscanf(sval,
"%lf,%lf,%lf,%lf",
1876 hawki_cal_zpoint_config.ycoord,
1877 hawki_cal_zpoint_config.ycoord+1,
1878 hawki_cal_zpoint_config.ycoord+2,
1879 hawki_cal_zpoint_config.ycoord+3)!=4)
1887 int hawki_cal_zpoint_check_epoch_equinox(cpl_propertylist * plist)
1892 cpl_msg_error(__func__,
"Epoch and equinox must be 2000.");