38 #include "irplib_plugin.h"
39 #include "irplib_utils.h"
40 #include "irplib_calib.h"
41 #include "irplib_strehl.h"
42 #include "irplib_stdstar.h"
44 #include "isaac_utils.h"
45 #include "isaac_pfits.h"
46 #include "isaac_dfs.h"
56 #define RECIPE_STRING "isaac_img_zpoint"
58 #define PHOT_STAR_RADIUS 30.0
59 #define PHOT_BACKGROUND_R1 40.0
60 #define PHOT_BACKGROUND_R2 60.0
62 #define DEF_LOCATE_SX 10
63 #define DEF_LOCATE_SY 10
69 static cpl_table * isaac_img_zpoint_reduce(cpl_frameset *,
const char *,
70 const char *,
const char *,
const char *,
const char *, cpl_image **);
71 static cpl_imagelist * isaac_img_zpoint_load(cpl_frameset *,
const char *,
72 const char *,
const char *,
const char *);
73 static int isaac_img_zpoint_save(cpl_table *, cpl_image *, cpl_frameset *,
74 const cpl_parameterlist *, cpl_frameset *);
75 static cpl_table * isaac_img_zpoint_photom(cpl_imagelist *, cpl_bivector *);
76 static cpl_error_code isaac_img_zpoint_get_mag(
const char *,
double,
double,
78 static int isaac_img_zpoint_gradients(cpl_vector *, cpl_bivector *,
double *,
79 double *,
double *,
double *);
80 static cpl_bivector * isaac_img_zpoint_get_offsets(cpl_frameset *);
81 static cpl_error_code isaac_img_zpoint_compute_keywords(
const cpl_frameset *,
83 static cpl_image * isaac_img_zpoint_check_im(cpl_imagelist *,
84 cpl_bivector *,
double,
double,
double);
86 cpl_recipe_define(isaac_img_zpoint, ISAAC_BINARY_VERSION,
87 "Lars Lundin", PACKAGE_BUGREPORT,
"2008",
88 "ISAAC Zero point computation recipe",
89 RECIPE_STRING
" -- ISAAC Zero point recipe\n"
90 "The files listed in the Set Of Frames (sof-file) must "
92 "raw-file.fits "ISAAC_IMG_ZPOINT_RAW
" or\n"
93 "raw-file.fits "ISAAC_IMG_ZPOINT_CHOPPING_RAW
" or\n"
94 "stdstars.fits "ISAAC_CALIB_STDSTARS
" or\n"
95 "flat-file.fits "ISAAC_CALIB_FLAT
" or\n"
96 "detlin-a-file.fits "ISAAC_CALIB_DETLIN_A
" or\n"
97 "detlin-b-file.fits "ISAAC_CALIB_DETLIN_B
" or\n"
98 "detlin-c-file.fits "ISAAC_CALIB_DETLIN_C
"\n");
111 double phot_star_radius;
131 } isaac_img_zpoint_config;
148 cpl_error_code isaac_img_zpoint_fill_parameterlist(cpl_parameterlist *
self)
151 const char * context = PACKAGE
"." RECIPE_STRING;
154 cpl_ensure_code(
self, CPL_ERROR_NULL_INPUT);
160 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
161 "star_r", PHOT_STAR_RADIUS, NULL,
162 context,
"The star radius");
163 cpl_ensure_code(!err, err);
166 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
167 "bg_r1", PHOT_BACKGROUND_R1, NULL,
168 context,
"The internal background "
170 cpl_ensure_code(!err, err);
173 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
174 "bg_r2", PHOT_BACKGROUND_R2, NULL,
175 context,
"The external background "
177 cpl_ensure_code(!err, err);
180 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
181 "ra", 999.0, NULL, context,
182 "Right Ascension [degrees]");
183 cpl_ensure_code(!err, err);
186 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
187 "dec", 999.0, NULL, context,
188 "Declination [degrees]");
189 cpl_ensure_code(!err, err);
192 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
193 "mag", 99.0, NULL, context,
194 "Standard Star Magnitude to use "
195 "instead of catalogue value");
196 cpl_ensure_code(!err, err);
199 err = irplib_parameterlist_set_int(
self, PACKAGE, RECIPE_STRING,
200 "sx", DEF_LOCATE_SX, NULL, context,
201 "X-size of the search window");
202 cpl_ensure_code(!err, err);
205 err = irplib_parameterlist_set_int(
self, PACKAGE, RECIPE_STRING,
206 "sy", DEF_LOCATE_SY, NULL, context,
207 "Y-size of the search window");
208 cpl_ensure_code(!err, err);
211 err = irplib_parameterlist_set_bool(
self, PACKAGE, RECIPE_STRING,
212 "check_im", CPL_FALSE, NULL, context,
213 "Flag to create the check image");
214 cpl_ensure_code(!err, err);
216 return CPL_ERROR_NONE;
228 static int isaac_img_zpoint(cpl_frameset * framelist,
229 const cpl_parameterlist * parlist)
232 const char * stdstars;
234 const char * detlin_a;
235 const char * detlin_b;
236 const char * detlin_c;
237 cpl_frameset * rawframes = NULL;
238 cpl_table * tab = NULL;
239 cpl_image * check_im = NULL;
242 isaac_img_zpoint_config.starname = NULL;
243 isaac_img_zpoint_config.sptype = NULL;
244 isaac_img_zpoint_config.filter[0] = (char)0;
245 isaac_img_zpoint_config.catalog = NULL;
246 isaac_img_zpoint_config.gradx = -1.0;
247 isaac_img_zpoint_config.grady = -1.0;
248 isaac_img_zpoint_config.graddx = -1.0;
249 isaac_img_zpoint_config.graddy = -1.0;
250 isaac_img_zpoint_config.fwhm_mean = -1.0;
254 isaac_img_zpoint_config.ra =
255 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
"ra");
258 isaac_img_zpoint_config.dec =
259 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
"dec");
262 isaac_img_zpoint_config.magnitude =
263 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
"mag");
266 isaac_img_zpoint_config.sx =
267 irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING,
"sx");
270 isaac_img_zpoint_config.sy =
271 irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING,
"sy");
274 isaac_img_zpoint_config.phot_star_radius =
275 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
278 isaac_img_zpoint_config.phot_bg_r1 =
279 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
283 isaac_img_zpoint_config.phot_bg_r2 =
284 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
288 isaac_img_zpoint_config.check_im =
289 irplib_parameterlist_get_bool(parlist, PACKAGE, RECIPE_STRING,
304 ISAAC_IMG_ZPOINT_RAW)) != NULL) {
305 isaac_img_zpoint_config.chopping = 0;
307 ISAAC_IMG_ZPOINT_CHOPPING_RAW)) != NULL) {
308 isaac_img_zpoint_config.chopping = 1;
310 error_if(1, CPL_ERROR_DATA_NOT_FOUND,
"Cannot find raw frames in the "
315 cpl_msg_info(cpl_func,
"Reduce the data");
316 cpl_msg_indent_more();
317 tab = isaac_img_zpoint_reduce(rawframes, stdstars, flat, detlin_a,
318 detlin_b, detlin_c, &check_im);
319 cpl_msg_indent_less();
320 skip_if(tab == NULL);
323 cpl_msg_info(cpl_func,
"Save the paf file");
324 cpl_msg_indent_more();
325 isaac_img_zpoint_save(tab, check_im, rawframes, parlist, framelist);
326 cpl_msg_indent_less();
331 cpl_image_delete(check_im);
332 cpl_frameset_delete(rawframes);
333 cpl_table_delete(tab);
334 cpl_free(isaac_img_zpoint_config.starname);
335 cpl_free(isaac_img_zpoint_config.sptype);
336 cpl_free(isaac_img_zpoint_config.catalog);
338 return cpl_error_get_code();
354 static cpl_table * isaac_img_zpoint_reduce(
356 const char * stdstars,
358 const char * detlin_a,
359 const char * detlin_b,
360 const char * detlin_c,
361 cpl_image ** check_im)
363 cpl_frame * cur_frame;
364 cpl_propertylist * plist;
366 cpl_imagelist * imlist;
371 double pos_x, pos_y, pos_x_cen, pos_y_cen, dist, min_dist;
372 cpl_apertures * aperts;
373 cpl_bivector * offsets,
376 int llx, lly, urx, ury;
377 double val, sqsum, zprms, avg_zp;
379 cpl_vector * tmp_vec;
385 pos_x_cen = pos_y_cen = -1.0;
388 cur_frame = cpl_frameset_get_position(set, 0);
389 plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
391 cpl_propertylist_delete(plist);
392 cpl_ensure(0, cpl_error_get_code(), NULL);
394 (void)strncpy(isaac_img_zpoint_config.filter, sval, 512);
395 isaac_img_zpoint_config.filter[511] = (char)0;
396 if (isaac_img_zpoint_config.ra > 998.0)
398 if (isaac_img_zpoint_config.dec > 998.0)
401 cpl_propertylist_delete(plist);
402 if (cpl_error_get_code()) {
403 cpl_msg_error(cpl_func,
"Cannot get some header informations");
406 cpl_msg_info(cpl_func,
"Using star at position: RA = %g; DEC = %g",
407 isaac_img_zpoint_config.ra, isaac_img_zpoint_config.dec);
410 if ((isaac_img_zpoint_config.band =
412 ISAAC_BAND_UNKNOWN) {
413 cpl_msg_error(cpl_func,
"Cannot associate the filter to a BB one");
418 if (isaac_img_zpoint_config.magnitude > 98.0) {
419 cpl_msg_info(cpl_func,
"Get the star magnitude");
420 if (isaac_img_zpoint_get_mag(stdstars, isaac_img_zpoint_config.ra,
421 isaac_img_zpoint_config.dec,
422 isaac_img_zpoint_config.band)) {
423 cpl_msg_error(cpl_func,
"Cannot get the magnitude from the catalog");
427 cpl_msg_info(cpl_func,
"Star magnitude with filter %s : %g",
428 isaac_img_zpoint_config.filter,
429 isaac_img_zpoint_config.magnitude);
432 cpl_msg_info(cpl_func,
"Load images");
433 if ((imlist = isaac_img_zpoint_load(set, flat, detlin_a, detlin_b,
434 detlin_c)) == NULL) {
435 cpl_msg_error(cpl_func,
"Cannot load the images");
440 cpl_msg_info(cpl_func,
"Detect a bright object in the first frame");
441 size_x = cpl_image_get_size_x(cpl_imagelist_get(imlist, 0));
442 size_y = cpl_image_get_size_y(cpl_imagelist_get(imlist, 0));
443 kernel = cpl_mask_new(3, 3);
444 cpl_mask_not(kernel);
445 ima = cpl_image_new(size_x, size_y,
446 cpl_image_get_type(cpl_imagelist_get(imlist, 0)));
447 cpl_image_filter_mask(ima, cpl_imagelist_get(imlist, 0), kernel,
448 CPL_FILTER_MEDIAN, CPL_BORDER_FILTER);
449 cpl_mask_delete(kernel);
450 aperts = cpl_apertures_extract_sigma(ima, 5.0);
451 cpl_image_delete(ima);
452 min_dist = size_x * size_x + size_y * size_y;
453 for (i=0; i<cpl_apertures_get_size(aperts); i++) {
454 pos_x = cpl_apertures_get_centroid_x(aperts, i+1);
455 pos_y = cpl_apertures_get_centroid_y(aperts, i+1);
456 dist = (pos_x-size_x/2)*(pos_x-size_x/2) +
457 (pos_y-size_y/2)*(pos_y-size_y/2);
464 cpl_apertures_delete(aperts);
465 if (cpl_error_get_code()) {
466 cpl_msg_error(cpl_func,
"Cannot find the central object");
467 cpl_imagelist_delete(imlist);
470 cpl_msg_info(cpl_func,
"Bright object position: %g %g",
471 pos_x_cen, pos_y_cen);
474 cpl_msg_info(cpl_func,
"Read the offsets in the header");
475 offsets = isaac_img_zpoint_get_offsets(set);
478 niter = cpl_imagelist_get_size(imlist);
479 positions = cpl_bivector_new(niter);
480 for (i=0; i<niter; i++) {
481 off_x = cpl_vector_get(cpl_bivector_get_x(offsets), i) -
482 cpl_vector_get(cpl_bivector_get_x(offsets), 0);
483 off_y = cpl_vector_get(cpl_bivector_get_y(offsets), i) -
484 cpl_vector_get(cpl_bivector_get_y(offsets), 0);
485 pos_x = pos_x_cen + off_x;
486 pos_y = pos_y_cen + off_y;
487 cpl_vector_set(cpl_bivector_get_x(positions), i, pos_x);
488 cpl_vector_set(cpl_bivector_get_y(positions), i, pos_y);
490 cpl_bivector_delete(offsets);
493 cpl_msg_info(cpl_func,
"Refine the star positions");
494 for (i=1; i<niter; i++) {
495 pos_x = cpl_vector_get(cpl_bivector_get_x(positions), i);
496 pos_y = cpl_vector_get(cpl_bivector_get_y(positions), i);
497 llx = pos_x - isaac_img_zpoint_config.sx;
498 urx = pos_x + isaac_img_zpoint_config.sx;
499 lly = pos_y - isaac_img_zpoint_config.sy;
500 ury = pos_y + isaac_img_zpoint_config.sy;
501 ima = cpl_imagelist_get(imlist, i);
502 pos_x = cpl_image_get_centroid_x_window(ima, llx, lly, urx, ury);
503 pos_y = cpl_image_get_centroid_y_window(ima, llx, lly, urx, ury);
504 cpl_vector_set(cpl_bivector_get_x(positions), i, pos_x);
505 cpl_vector_set(cpl_bivector_get_y(positions), i, pos_y);
507 if (cpl_error_get_code()) {
508 cpl_msg_error(cpl_func,
"Cannot refine the positions");
509 cpl_imagelist_delete(imlist);
510 cpl_bivector_delete(positions);
515 if (isaac_img_zpoint_config.check_im) {
516 *check_im = isaac_img_zpoint_check_im(imlist, positions,
517 isaac_img_zpoint_config.phot_star_radius,
518 isaac_img_zpoint_config.phot_bg_r1,
519 isaac_img_zpoint_config.phot_bg_r2);
523 cpl_msg_info(cpl_func,
"Compute the photometry");
524 cpl_msg_indent_more();
525 if ((tab_res = isaac_img_zpoint_photom(imlist, positions)) == NULL) {
526 cpl_msg_error(cpl_func,
"Cannot reduce");
527 cpl_bivector_delete(positions);
528 cpl_imagelist_delete(imlist);
529 cpl_image_delete(*check_im);
531 cpl_msg_indent_less();
534 cpl_msg_indent_less();
535 cpl_imagelist_delete(imlist);
538 isaac_img_zpoint_config.flux_med =
539 cpl_table_get_column_median(tab_res,
"FLUX");
542 isaac_img_zpoint_config.fwhm_mean =
543 cpl_table_get_column_mean(tab_res,
"FWHMX");
544 isaac_img_zpoint_config.fwhm_mean +=
545 cpl_table_get_column_mean(tab_res,
"FWHMY");
546 isaac_img_zpoint_config.fwhm_mean /= 2.0;
549 tmp_vec = cpl_vector_new(niter);
550 for (i=0; i<niter; i++) {
551 cpl_vector_set(tmp_vec, i,
552 cpl_table_get_double(tab_res,
"FLUX", i, NULL));
554 isaac_img_zpoint_gradients(tmp_vec, positions,
555 &(isaac_img_zpoint_config.gradx),
556 &(isaac_img_zpoint_config.grady),
557 &(isaac_img_zpoint_config.graddx),
558 &(isaac_img_zpoint_config.graddy));
559 cpl_vector_delete(tmp_vec);
560 cpl_bivector_delete(positions);
563 tmp_vec = cpl_vector_new(niter);
564 for (i=0; i<niter; i++) {
565 cpl_vector_set(tmp_vec, i,
566 cpl_table_get_double(tab_res,
"ZPOINT", i, NULL));
569 avg_zp = sqsum = 0.0;
570 if (isaac_img_zpoint_config.chopping == 0) {
571 cpl_vector_sort(tmp_vec, 1);
573 for (i=1; i<niter-1; i++) {
574 val = cpl_vector_get(tmp_vec, i);
581 }
else if (isaac_img_zpoint_config.chopping == 1) {
582 for (i=0; i<niter; i++) {
583 val = cpl_vector_get(tmp_vec, i);
591 cpl_msg_error(cpl_func,
"unsupported mode");
592 cpl_table_delete(tab_res);
593 cpl_vector_delete(tmp_vec);
594 cpl_image_delete(*check_im);
596 cpl_msg_indent_less();
599 cpl_vector_delete(tmp_vec);
601 cpl_msg_error(cpl_func,
"no valid zpoint measurement: cannot compute");
602 isaac_img_zpoint_config.zpoint = -1.0;
603 isaac_img_zpoint_config.zpointrms = -1.0;
605 avg_zp /= (double)nb_ok;
606 sqsum /= (double)nb_ok;
607 zprms = sqsum - avg_zp * avg_zp;
608 zprms = zprms > 0 ? sqrt(zprms) : 0;
609 isaac_img_zpoint_config.zpoint = avg_zp;
610 isaac_img_zpoint_config.zpointrms = zprms;
614 cpl_msg_info(cpl_func,
"***** FINAL RESULTS *****");
615 cpl_msg_info(cpl_func,
"Zero point : %g", isaac_img_zpoint_config.zpoint);
616 cpl_msg_info(cpl_func,
"Zero p. RMS: %g", isaac_img_zpoint_config.zpointrms);
637 static cpl_image * isaac_img_zpoint_check_im(
638 cpl_imagelist * imlist,
639 cpl_bivector * positions,
644 int nima, in_nx, in_ny, nx, ny, box_sz, llx, lly;
656 if (imlist == NULL)
return NULL;
657 if (positions == NULL)
return NULL;
658 nima = cpl_imagelist_get_size(imlist);
659 if (cpl_bivector_get_size(positions) != nima)
return NULL;
662 in_ima = cpl_imagelist_get(imlist, 0);
663 in_nx = cpl_image_get_size_x(in_ima);
664 in_ny = cpl_image_get_size_y(in_ima);
665 pos_x = cpl_bivector_get_x_data(positions);
666 pos_y = cpl_bivector_get_y_data(positions);
669 box_sz = 2 * (int)r3 + 1;
672 out_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
673 pout_ima = cpl_image_get_data_float(out_ima);
676 for (k=0; k<nima; k++) {
678 in_ima = cpl_imagelist_get(imlist, k);
679 pin_ima = cpl_image_get_data_float(in_ima);
682 llx = (int)(pos_x[k] - r3);
683 lly = (int)(pos_y[k] - r3);
685 for (i=0; i<box_sz; i++) {
686 for (j=0; j<box_sz; j++) {
687 out_pos = (box_sz * k) + i + j * nx;
688 in_pos = llx + i + (lly+j) * in_nx;
689 if (in_pos >= 0 && in_pos < in_nx*in_ny)
690 pout_ima[out_pos] = pin_ima[in_pos];
696 for (i=0; i<box_sz; i++) {
697 for (j=0; j<box_sz; j++) {
698 dist = sqrt((i - (box_sz/2)) * (i - (box_sz/2)) +
699 (j - (box_sz/2)) * (j - (box_sz/2)));
700 if ((fabs(dist-sqrt(r1*r1))) < 0.5) pout_ima[i + j * nx] = 10000;
701 if ((fabs(dist-sqrt(r2*r2))) < 0.5) pout_ima[i + j * nx] = 10000;
702 if ((fabs(dist-sqrt(r3*r3))) < 0.5) pout_ima[i + j * nx] = 10000;
717 static cpl_imagelist * isaac_img_zpoint_load(
720 const char * detlin_a,
721 const char * detlin_b,
722 const char * detlin_c)
724 cpl_imagelist * imlist;
725 cpl_imagelist * imlist2;
726 cpl_imagelist * diffs;
731 if (isaac_img_zpoint_config.chopping == 0) {
732 if ((imlist = cpl_imagelist_load_frameset(set, CPL_TYPE_FLOAT, 1,
734 cpl_msg_error(cpl_func,
"Cannot load the images");
737 }
else if (isaac_img_zpoint_config.chopping == 1) {
738 if ((imlist = cpl_imagelist_load_frameset(set, CPL_TYPE_FLOAT, 1,
740 cpl_msg_error(cpl_func,
"Cannot load the images");
743 if ((imlist2 = cpl_imagelist_load_frameset(set, CPL_TYPE_FLOAT, 2,
745 cpl_msg_error(cpl_func,
"Cannot load the images");
746 cpl_imagelist_delete(imlist);
749 cpl_imagelist_subtract(imlist, imlist2);
750 cpl_imagelist_delete(imlist2);
752 cpl_msg_error(cpl_func,
"Unsupported mode");
757 if (detlin_a && detlin_b && detlin_c) {
758 cpl_msg_info(cpl_func,
"Correct for non-linearity");
759 if (irplib_detlin_correct(imlist, detlin_a, detlin_b, detlin_c) == -1) {
760 cpl_msg_error(cpl_func,
"Cannot correct for non-linearity");
766 cpl_image * flat_im = cpl_image_load(flat, CPL_TYPE_UNSPECIFIED, 0, 0);
767 cpl_error_code error = flat_im ? CPL_ERROR_NONE : cpl_error_get_code();
769 cpl_msg_info(cpl_func,
"Divide by the flat field");
773 error |= cpl_image_threshold(flat_im, sqrt(FLT_MIN), sqrt(FLT_MAX),
775 error |= cpl_imagelist_divide_image(imlist, flat_im);
776 cpl_image_delete(flat_im);
779 cpl_imagelist_delete(imlist);
780 (void)cpl_error_set_message(cpl_func, cpl_error_get_code(),
781 "Flat field correction failed");
787 if (isaac_img_zpoint_config.chopping == 1) {
790 }
else if (isaac_img_zpoint_config.chopping == 0) {
792 diffs = cpl_imagelist_new();
793 for (i=0; i<cpl_imagelist_get_size(imlist)-1; i++) {
794 ima = cpl_image_subtract_create(
795 cpl_imagelist_get(imlist, i),
796 cpl_imagelist_get(imlist, i+1));
797 cpl_imagelist_set(diffs, ima, 2*i);
798 ima = cpl_image_subtract_create(
799 cpl_imagelist_get(imlist, i+1),
800 cpl_imagelist_get(imlist, i));
801 cpl_imagelist_set(diffs, ima, 2*i+1);
803 cpl_imagelist_delete(imlist);
804 if (cpl_error_get_code()) {
805 cpl_msg_error(cpl_func,
"Cannot build the difference images");
806 cpl_imagelist_delete(diffs);
810 cpl_msg_error(cpl_func,
"Unsupported mode - abort");
811 cpl_imagelist_delete(imlist);
827 static cpl_table * isaac_img_zpoint_photom(
828 cpl_imagelist * ilist,
833 double r, r1, r2, mag, dit;
837 double bgd, fl, zp, peak, fwhm_x, fwhm_y;
841 if (ilist == NULL)
return NULL;
842 if (pos == NULL)
return NULL;
845 nbima = cpl_imagelist_get_size(ilist);
846 mag = isaac_img_zpoint_config.magnitude;
847 dit = isaac_img_zpoint_config.dit;
848 r = isaac_img_zpoint_config.phot_star_radius;
849 r1 = isaac_img_zpoint_config.phot_bg_r1;
850 r2 = isaac_img_zpoint_config.phot_bg_r2;
853 tab = cpl_table_new(nbima);
854 cpl_table_new_column(tab,
"POSX", CPL_TYPE_DOUBLE);
855 cpl_table_new_column(tab,
"POSY", CPL_TYPE_DOUBLE);
856 cpl_table_new_column(tab,
"ZPOINT", CPL_TYPE_DOUBLE);
857 cpl_table_new_column(tab,
"FLUX", CPL_TYPE_DOUBLE);
858 cpl_table_new_column(tab,
"PEAK", CPL_TYPE_DOUBLE);
859 cpl_table_new_column(tab,
"BGD", CPL_TYPE_DOUBLE);
860 cpl_table_new_column(tab,
"FWHMX", CPL_TYPE_DOUBLE);
861 cpl_table_new_column(tab,
"FWHMY", CPL_TYPE_DOUBLE);
864 pos_x = cpl_bivector_get_x_data(pos);
865 pos_y = cpl_bivector_get_y_data(pos);
866 for (i=0; i<nbima; i++) {
868 ima = cpl_imagelist_get(ilist, i);
871 bgd = irplib_strehl_ring_background(ima, (
int)(pos_x[i]),
872 (
int)(pos_y[i]), (
int)r1, (
int)r2, IRPLIB_BG_METHOD_MEDIAN);
874 fl = irplib_strehl_disk_flux(ima,
875 (
int)(pos_x[i]), (
int)(pos_y[i]), (
int)r, bgd);
877 zp = mag + 2.5 * log10(fl) - 2.5 * log10(dit);
878 cpl_msg_info(cpl_func,
"Zero point nb %d: %g", i+1, zp);
880 peak = cpl_image_get_max_window(ima,
881 (
int)(pos_x[i]-5), (
int)(pos_y[i]-5),
882 (
int)(pos_x[i]+5), (
int)(pos_y[i]+5));
885 if (cpl_image_get_fwhm(ima, (
int)(pos_x[i]), (
int)(pos_y[i]),
886 &fwhm_x, &fwhm_y) != CPL_ERROR_NONE) {
887 cpl_msg_debug(cpl_func,
"Cannot compute FWHM for image %d", i+1);
892 cpl_table_set_double(tab,
"POSX", i, pos_x[i]);
893 cpl_table_set_double(tab,
"POSY", i, pos_y[i]);
894 cpl_table_set_double(tab,
"ZPOINT", i, zp);
895 cpl_table_set_double(tab,
"FLUX", i, fl);
896 cpl_table_set_double(tab,
"PEAK", i, peak);
897 cpl_table_set_double(tab,
"BGD", i, bgd);
898 cpl_table_set_double(tab,
"FWHMX", i, fwhm_x);
899 cpl_table_set_double(tab,
"FWHMY", i, fwhm_y);
919 static int isaac_img_zpoint_gradients(
921 cpl_bivector * positions,
928 double two, three, four, five, mean;
929 double two_pos_x, three_pos_x, four_pos_x, five_pos_x,
930 two_pos_y, three_pos_y, four_pos_y, five_pos_y;
936 if (flux == NULL)
return CPL_ERROR_UNSPECIFIED;
939 nflux = cpl_vector_get_size(flux);
947 mean = cpl_vector_get_mean(flux);
950 pflux = cpl_vector_get_data(flux);
951 ppos_x = cpl_bivector_get_x_data(positions);
952 ppos_y = cpl_bivector_get_y_data(positions);
954 two = (pflux[1] + pflux[2]) / 2.0;
955 three = (pflux[3] + pflux[4]) / 2.0;
956 four = (pflux[5] + pflux[6]) / 2.0;
959 two_pos_x = (ppos_x[1] + ppos_x[2]) / 2.0;
960 two_pos_y = (ppos_y[1] + ppos_y[2]) / 2.0;
961 three_pos_x = (ppos_x[3] + ppos_x[4]) / 2.0;
962 three_pos_y = (ppos_y[3] + ppos_y[4]) / 2.0;
963 four_pos_x = (ppos_x[5] + ppos_x[6]) / 2.0;
964 four_pos_y = (ppos_y[5] + ppos_y[6]) / 2.0;
965 five_pos_x = ppos_x[7];
966 five_pos_y = ppos_y[7];
969 *gradx = (two + five - three - four) / 2;
970 *grady = (three + two - four - five) / 2;
973 *gradx *= (512 / ((two_pos_x + five_pos_x - three_pos_x - four_pos_x)/2));
974 *grady *= (512 / ((three_pos_y + two_pos_y - four_pos_y - five_pos_y)/2));
977 *graddx = sqrt((two-five)*(two-five) + (three-four)*(three-four))/2.0;
978 *graddy = sqrt((three-two)*(three-two) + (four-five)*(four-five))/2.0;
996 static cpl_bivector * isaac_img_zpoint_get_offsets(cpl_frameset * set)
1000 cpl_bivector * offs;
1001 cpl_frame * cur_frame;
1002 cpl_propertylist * plist;
1003 double off_x, off_y;
1007 if (set == NULL)
return NULL;
1010 nframes = cpl_frameset_get_size(set);
1013 if (isaac_img_zpoint_config.chopping == 1) {
1015 }
else if (isaac_img_zpoint_config.chopping == 0) {
1016 noffsets = 2 * (nframes-1);
1019 if (cpl_error_get_code())
return NULL;
1022 offs = cpl_bivector_new(noffsets);
1025 for (i=0; i<nframes; i++) {
1026 cur_frame = cpl_frameset_get_position(set, i);
1027 plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame),0);
1030 cpl_propertylist_delete(plist);
1032 cpl_vector_set(cpl_bivector_get_x(offs), 2*i, off_x);
1033 cpl_vector_set(cpl_bivector_get_y(offs), 2*i, off_y);
1034 }
else if (i==nframes-1) {
1035 cpl_vector_set(cpl_bivector_get_x(offs), 2*i-1, off_x);
1036 cpl_vector_set(cpl_bivector_get_y(offs), 2*i-1, off_y);
1038 cpl_vector_set(cpl_bivector_get_x(offs), 2*i, off_x);
1039 cpl_vector_set(cpl_bivector_get_y(offs), 2*i, off_y);
1040 cpl_vector_set(cpl_bivector_get_x(offs), 2*i-1, off_x);
1041 cpl_vector_set(cpl_bivector_get_y(offs), 2*i-1, off_y);
1045 cpl_msg_error(cpl_func,
"Unsupported mode - abort");
1050 if (cpl_error_get_code()) {
1051 cpl_bivector_delete(offs);
1052 cpl_msg_error(cpl_func,
"Cannot find offsets in headers");
1067 static cpl_error_code isaac_img_zpoint_get_mag(
const char * stdstars,
1072 cpl_errorstate prestate = cpl_errorstate_get();
1075 bug_if(stdstars == NULL);
1083 if (!irplib_stdstar_find_star(stdstars, ra, dec,
1086 &isaac_img_zpoint_config.magnitude,
1087 &isaac_img_zpoint_config.starname,
1088 &isaac_img_zpoint_config.sptype,
1089 &isaac_img_zpoint_config.catalog,
1091 !irplib_stdstar_find_star(stdstars, ra, dec,
1093 "LCO-Palomar-NICMOS-Red-Stars.txt",
1094 &isaac_img_zpoint_config.magnitude,
1095 &isaac_img_zpoint_config.starname,
1096 &isaac_img_zpoint_config.sptype,
1097 &isaac_img_zpoint_config.catalog,
1099 !irplib_stdstar_find_star(stdstars, ra, dec,
1102 &isaac_img_zpoint_config.magnitude,
1103 &isaac_img_zpoint_config.starname,
1104 &isaac_img_zpoint_config.sptype,
1105 &isaac_img_zpoint_config.catalog,
1108 (band == ISAAC_BAND_K &&
1109 !irplib_stdstar_find_star(stdstars, ra, dec,
1112 &isaac_img_zpoint_config.magnitude,
1113 &isaac_img_zpoint_config.starname,
1114 &isaac_img_zpoint_config.sptype,
1115 &isaac_img_zpoint_config.catalog,
1116 NULL, NULL, 2.0)) ||
1117 (band == ISAAC_BAND_KS &&
1118 !irplib_stdstar_find_star(stdstars, ra, dec,
1121 &isaac_img_zpoint_config.magnitude,
1122 &isaac_img_zpoint_config.starname,
1123 &isaac_img_zpoint_config.sptype,
1124 &isaac_img_zpoint_config.catalog,
1125 NULL, NULL, 2.0))) {
1126 cpl_errorstate_set(prestate);
1134 if (!irplib_stdstar_find_star(stdstars, ra, dec,
1136 "ESO-VanDerBliek.txt",
1137 &isaac_img_zpoint_config.magnitude,
1138 &isaac_img_zpoint_config.starname,
1139 &isaac_img_zpoint_config.sptype,
1140 &isaac_img_zpoint_config.catalog,
1142 !irplib_stdstar_find_star(stdstars, ra, dec,
1144 "MSSSO-Photometric.txt",
1145 &isaac_img_zpoint_config.magnitude,
1146 &isaac_img_zpoint_config.starname,
1147 &isaac_img_zpoint_config.sptype,
1148 &isaac_img_zpoint_config.catalog,
1150 !irplib_stdstar_find_star(stdstars, ra, dec,
1152 "MSSSO-Spectroscopic.txt",
1153 &isaac_img_zpoint_config.magnitude,
1154 &isaac_img_zpoint_config.starname,
1155 &isaac_img_zpoint_config.sptype,
1156 &isaac_img_zpoint_config.catalog,
1158 !irplib_stdstar_find_star(stdstars, ra, dec,
1160 &isaac_img_zpoint_config.magnitude,
1161 &isaac_img_zpoint_config.starname,
1162 &isaac_img_zpoint_config.sptype,
1163 &isaac_img_zpoint_config.catalog,
1165 cpl_errorstate_set(prestate);
1171 cpl_msg_error(cpl_func,
"cannot determine associated filter");
1177 return cpl_error_get_code();
1191 static int isaac_img_zpoint_save(
1193 cpl_image * check_im,
1195 const cpl_parameterlist * parlist,
1198 cpl_propertylist * plist;
1199 cpl_propertylist * paflist;
1200 cpl_propertylist * qclist;
1201 const cpl_frame * ref_frame;
1203 double hum, airm, extinction;
1209 qclist = cpl_propertylist_new();
1212 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
1213 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
1215 cpl_msg_error(cpl_func,
"getting header from reference frame");
1216 cpl_propertylist_delete(qclist);
1217 return CPL_ERROR_UNSPECIFIED;
1220 if (cpl_error_get_code()) {
1221 cpl_propertylist_delete(qclist);
1222 cpl_propertylist_delete(plist);
1223 return CPL_ERROR_UNSPECIFIED;
1226 if (cpl_error_get_code()) cpl_error_reset();
1227 else cpl_propertylist_append_string(qclist,
"ESO QC FILTER OBS", sval);
1228 cpl_propertylist_append_string(qclist,
"ESO QC FILTER REF",
1230 if (isaac_img_zpoint_compute_keywords(raw, &hum, &airm)) {
1231 cpl_propertylist_append_double(qclist,
"ESO QC AMBI RHUM AVG", hum);
1232 cpl_propertylist_append_double(qclist,
"ESO QC AIRMASS", airm);
1234 cpl_propertylist_delete(plist);
1236 switch (isaac_img_zpoint_config.band) {
1237 case ISAAC_BAND_J: extinction = 0.09;
break;
1238 case ISAAC_BAND_JS: extinction = 0.05;
break;
1239 case ISAAC_BAND_H: extinction = 0.04;
break;
1240 case ISAAC_BAND_KS: extinction = 0.06;
break;
1241 default: extinction = 0.00;
break;
1243 cpl_propertylist_append_double(qclist,
"ESO QC ZPOINT",
1244 isaac_img_zpoint_config.zpoint);
1245 cpl_propertylist_append_double(qclist,
"ESO QC ZPOINT ATX0",
1246 isaac_img_zpoint_config.zpoint + airm * extinction);
1247 cpl_propertylist_append_double(qclist,
"ESO QC ZPOINTRMS",
1248 isaac_img_zpoint_config.zpointrms);
1249 cpl_propertylist_append_double(qclist,
"ESO QC FLUX MED",
1250 isaac_img_zpoint_config.flux_med);
1251 cpl_propertylist_append_string(qclist,
"ESO QC STDNAME",
1252 isaac_img_zpoint_config.starname ?
1253 isaac_img_zpoint_config.starname :
"");
1254 cpl_propertylist_append_string(qclist,
"ESO QC SPECTYPE",
1255 isaac_img_zpoint_config.sptype ?
1256 isaac_img_zpoint_config.sptype :
"");
1257 cpl_propertylist_append_double(qclist,
"ESO QC STARMAG",
1258 isaac_img_zpoint_config.magnitude);
1259 cpl_propertylist_append_string(qclist,
"ESO QC CATNAME",
1260 isaac_img_zpoint_config.catalog ?
1261 isaac_img_zpoint_config.catalog :
"");
1262 cpl_propertylist_append_double(qclist,
"ESO QC GRADX",
1263 isaac_img_zpoint_config.gradx);
1264 cpl_propertylist_append_double(qclist,
"ESO QC GRADY",
1265 isaac_img_zpoint_config.grady);
1266 cpl_propertylist_append_double(qclist,
"ESO QC GRADDX",
1267 isaac_img_zpoint_config.graddx);
1268 cpl_propertylist_append_double(qclist,
"ESO QC GRADDY",
1269 isaac_img_zpoint_config.graddy);
1270 cpl_propertylist_append_double(qclist,
"ESO QC FWHM MEAN",
1271 isaac_img_zpoint_config.fwhm_mean);
1274 irplib_dfs_save_table(set,
1280 ISAAC_IMG_ZPOINT_TAB,
1283 PACKAGE
"/" PACKAGE_VERSION,
1284 "isaac_img_zpoint.fits");
1287 irplib_dfs_save_image(set,
1293 ISAAC_IMG_ZPOINT_CHECK,
1296 PACKAGE
"/" PACKAGE_VERSION,
1297 "isaac_img_zpoint_check.fits");
1301 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
1304 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
1306 cpl_msg_error(cpl_func,
"getting header from reference frame");
1307 cpl_propertylist_delete(qclist);
1308 return CPL_ERROR_UNSPECIFIED;
1312 paflist = cpl_propertylist_new();
1313 cpl_propertylist_copy_property_regexp(paflist, plist,
1314 "^(ARCFILE|ESO TPL ID|DATE-OBS|MJD-OBS|ESO INS MODE|"
1315 "ESO OBS ID|ESO DET DIT|ESO INS PIXSCALE|RA|DEC)$", 0);
1316 cpl_propertylist_delete(plist);
1319 cpl_propertylist_copy_property_regexp(paflist, qclist,
"ESO QC", 0);
1320 cpl_propertylist_delete(qclist);
1323 cpl_propertylist_update_string(paflist, CPL_DFS_PRO_CATG,
1324 ISAAC_IMG_ZPOINT_TAB);
1327 cpl_dfs_save_paf(
"ISAAC",
1330 "isaac_img_zpoint.paf");
1331 cpl_propertylist_delete(paflist);
1345 cpl_error_code isaac_img_zpoint_compute_keywords(
const cpl_frameset * set,
1349 const cpl_size nframes = cpl_frameset_get_size(set);
1350 double meanhum = 0.0;
1353 if (airm != NULL) *airm = 0.0;
1354 if (hum != NULL) *hum = 0.0;
1356 cpl_ensure_code(set != NULL, CPL_ERROR_NULL_INPUT);
1357 cpl_ensure_code(hum != NULL, CPL_ERROR_NULL_INPUT);
1358 cpl_ensure_code(airm != NULL, CPL_ERROR_NULL_INPUT);
1359 cpl_ensure_code(nframes > 0, CPL_ERROR_DATA_NOT_FOUND);
1361 for (i = 0; i < nframes; i++) {
1362 const cpl_frame * cur_frame = cpl_frameset_get_position_const(set, i);
1363 const char * filename = cpl_frame_get_filename(cur_frame);
1364 cpl_propertylist * plist = cpl_propertylist_load(filename, 0);
1370 cpl_propertylist_delete(plist);
1372 meanhum += (humi - meanhum) / (
double)(i + 1);
1374 if (cpl_error_get_code())
break;
1378 if (cpl_error_get_code())
return cpl_error_set_where(cpl_func);
1380 assert(i == nframes);
1385 return CPL_ERROR_NONE;
int isaac_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
double isaac_pfits_get_ra(const cpl_propertylist *plist)
find out the RA
double isaac_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
double isaac_pfits_get_cumoffsety(const cpl_propertylist *plist)
find out the cumulative offset in Y
double isaac_pfits_get_airmass_end(const cpl_propertylist *plist)
find out airmass end
double isaac_pfits_get_dec(const cpl_propertylist *plist)
find out the DEC
const char * isaac_pfits_get_filter(const cpl_propertylist *plist)
find out the filter
double isaac_pfits_get_humidity_level(const cpl_propertylist *plist)
find out the humidity level
double isaac_pfits_get_cumoffsetx(const cpl_propertylist *plist)
find out the cumulative offset in X
double isaac_pfits_get_airmass_start(const cpl_propertylist *plist)
find out airmass start
const char * isaac_extract_filename(const cpl_frameset *self, const char *tag)
Extract the filename of the first frame of the given tag.
const char * isaac_std_band_name(isaac_band band)
Return a band name.
isaac_band isaac_get_bbfilter(const char *f)
Get the broad band filter.
cpl_bivector * isaac_get_offsets(const cpl_frameset *fset)
Get the offsets from a set of frames.
cpl_frameset * isaac_extract_frameset(const cpl_frameset *self, const char *tag)
Extract the frames with the given tag from a frameset.