38 #include "irplib_plugin.h"
39 #include "irplib_utils.h"
40 #include "irplib_calib.h"
41 #include "irplib_stdstar.h"
42 #include "irplib_strehl.h"
44 #include "sofi_utils.h"
45 #include "sofi_pfits.h"
57 #define RECIPE_STRING "sofi_img_zpoint"
59 #define PHOT_STAR_RADIUS 30.0
60 #define PHOT_BACKGROUND_R1 40.0
61 #define PHOT_BACKGROUND_R2 60.0
63 #define DEF_LOCATE_SX 10
64 #define DEF_LOCATE_SY 10
70 static cpl_table * sofi_img_zpoint_reduce(cpl_frameset *,
const char *,
71 const char *,
const char *,
const char *,
const char *, cpl_image **);
72 static cpl_imagelist * sofi_img_zpoint_load(cpl_frameset *,
const char *,
73 const char *,
const char *,
const char *);
74 static int sofi_img_zpoint_save(cpl_table *, cpl_image *, cpl_frameset *,
75 const cpl_parameterlist *, cpl_frameset *);
76 static cpl_table * sofi_img_zpoint_photom(cpl_imagelist *, cpl_bivector *);
77 static cpl_error_code sofi_img_zpoint_get_mag(
const char *,
double,
double,
79 static int sofi_img_zpoint_gradients(cpl_vector *,
double *,
double *,
81 static cpl_bivector * sofi_img_zpoint_get_offsets(cpl_frameset *);
82 static int sofi_img_zpoint_compute_keywords(cpl_frameset *,
double *,
double *);
83 static cpl_image * sofi_img_zpoint_check_im(cpl_imagelist *,
84 cpl_bivector *,
double,
double,
double);
86 cpl_recipe_define(sofi_img_zpoint, SOFI_BINARY_VERSION,
87 "Lars Lundin", PACKAGE_BUGREPORT,
"2008",
88 "SOFI Zero point computation recipe",
89 RECIPE_STRING
" -- SOFI Zero point recipe\n"
90 "The files listed in the Set Of Frames (sof-file) must "
92 "raw-file.fits "SOFI_IMG_ZPOINT_RAW
" or\n"
93 "stdstars.fits "SOFI_CALIB_STDSTARS
" or\n"
94 "flat-file.fits "SOFI_CALIB_FLAT
" or\n"
95 "detlin-a-file.fits "SOFI_CALIB_DETLIN_A
" or\n"
96 "detlin-b-file.fits "SOFI_CALIB_DETLIN_B
" or\n"
97 "detlin-c-file.fits "SOFI_CALIB_DETLIN_C
"\n");
110 double phot_star_radius;
129 } sofi_img_zpoint_config;
145 cpl_error_code sofi_img_zpoint_fill_parameterlist(cpl_parameterlist *
self)
148 const char * context = PACKAGE
"." RECIPE_STRING;
151 cpl_ensure_code(
self, CPL_ERROR_NULL_INPUT);
156 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
157 "star_r", PHOT_STAR_RADIUS, NULL,
158 context,
"The star radius");
159 cpl_ensure_code(!err, err);
162 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
163 "bg_r1", PHOT_BACKGROUND_R1, NULL,
164 context,
"The internal background "
166 cpl_ensure_code(!err, err);
169 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
170 "bg_r2", PHOT_BACKGROUND_R2, NULL,
171 context,
"The external background "
173 cpl_ensure_code(!err, err);
176 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
177 "ra", 999.0, NULL, context,
178 "Right Ascension [degrees]");
179 cpl_ensure_code(!err, err);
182 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
183 "dec", 999.0, NULL, context,
184 "Declination [degrees]");
185 cpl_ensure_code(!err, err);
188 err = irplib_parameterlist_set_double(
self, PACKAGE, RECIPE_STRING,
189 "mag", 99.0, NULL, context,
190 "Standard Star Magnitude to use "
191 "instead of catalogue value");
192 cpl_ensure_code(!err, err);
195 err = irplib_parameterlist_set_int(
self, PACKAGE, RECIPE_STRING,
196 "sx", DEF_LOCATE_SX, NULL, context,
197 "X-size of the search window");
198 cpl_ensure_code(!err, err);
201 err = irplib_parameterlist_set_int(
self, PACKAGE, RECIPE_STRING,
202 "sy", DEF_LOCATE_SY, NULL, context,
203 "Y-size of the search window");
204 cpl_ensure_code(!err, err);
207 err = irplib_parameterlist_set_bool(
self, PACKAGE, RECIPE_STRING,
208 "check_im", CPL_FALSE, NULL, context,
209 "Flag to create the check image");
210 cpl_ensure_code(!err, err);
212 return CPL_ERROR_NONE;
225 static int sofi_img_zpoint(cpl_frameset * framelist,
226 const cpl_parameterlist * parlist)
228 const char * stdstars;
230 const char * detlin_a;
231 const char * detlin_b;
232 const char * detlin_c;
233 cpl_frameset * rawframes = NULL;
234 cpl_table * tab = NULL;
235 cpl_image * check_im = NULL;
238 sofi_img_zpoint_config.starname = NULL;
239 sofi_img_zpoint_config.sptype = NULL;
240 sofi_img_zpoint_config.filter[0] = (char)0;
241 sofi_img_zpoint_config.catalog = NULL;
242 sofi_img_zpoint_config.gradx = -1.0;
243 sofi_img_zpoint_config.grady = -1.0;
244 sofi_img_zpoint_config.graddx = -1.0;
245 sofi_img_zpoint_config.graddy = -1.0;
246 sofi_img_zpoint_config.fwhm_mean = -1.0;
250 sofi_img_zpoint_config.ra =
251 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
"ra");
254 sofi_img_zpoint_config.dec =
255 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
"dec");
258 sofi_img_zpoint_config.magnitude =
259 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
"mag");
262 sofi_img_zpoint_config.sx =
263 irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING,
"sx");
266 sofi_img_zpoint_config.sy =
267 irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING,
"sy");
270 sofi_img_zpoint_config.phot_star_radius =
271 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
274 sofi_img_zpoint_config.phot_bg_r1 =
275 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
279 sofi_img_zpoint_config.phot_bg_r2 =
280 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
284 sofi_img_zpoint_config.check_im =
285 irplib_parameterlist_get_bool(parlist, PACKAGE, RECIPE_STRING,
301 error_if(rawframes == NULL, CPL_ERROR_DATA_NOT_FOUND,
"Cannot find "
302 "raw frames in the input list");
305 cpl_msg_info(cpl_func,
"Reduce the data");
306 cpl_msg_indent_more();
307 tab = sofi_img_zpoint_reduce(rawframes, stdstars, flat, detlin_a,
308 detlin_b, detlin_c, &check_im);
309 cpl_msg_indent_less();
310 skip_if (tab == NULL);
313 cpl_msg_info(cpl_func,
"Save the paf file");
314 cpl_msg_indent_more();
315 sofi_img_zpoint_save(tab, check_im, rawframes, parlist, framelist);
316 cpl_msg_indent_less();
321 cpl_image_delete(check_im);
322 cpl_frameset_delete(rawframes);
323 cpl_table_delete(tab);
325 cpl_free(sofi_img_zpoint_config.starname);
326 cpl_free(sofi_img_zpoint_config.sptype);
327 cpl_free(sofi_img_zpoint_config.catalog);
329 return cpl_error_get_code();
345 static cpl_table * sofi_img_zpoint_reduce(
347 const char * stdstars,
349 const char * detlin_a,
350 const char * detlin_b,
351 const char * detlin_c,
352 cpl_image ** check_im)
354 cpl_frame * cur_frame;
355 cpl_propertylist * plist;
357 cpl_imagelist * imlist;
361 double pos_x, pos_y, pos_x_cen, pos_y_cen, dist, min_dist;
362 cpl_apertures * aperts;
363 cpl_bivector * offsets,
366 int llx, lly, urx, ury;
367 double val, sqsum, zprms, avg_zp;
369 cpl_vector * tmp_vec;
375 pos_x_cen = pos_y_cen = -1.0;
378 cur_frame = cpl_frameset_get_position(set, 0);
379 plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
381 cpl_propertylist_delete(plist);
383 }
else strcpy(sofi_img_zpoint_config.filter, sval);
384 if (sofi_img_zpoint_config.ra > 998.0)
386 if (sofi_img_zpoint_config.dec > 998.0)
389 cpl_propertylist_delete(plist);
390 if (cpl_error_get_code()) {
391 cpl_msg_error(cpl_func,
"Cannot get some header informations");
394 cpl_msg_info(cpl_func,
"Using star at position: RA = %g; DEC = %g",
395 sofi_img_zpoint_config.ra, sofi_img_zpoint_config.dec);
398 if ((sofi_img_zpoint_config.band =
401 cpl_msg_error(cpl_func,
"Cannot associate the filter to a BB one");
406 if (sofi_img_zpoint_config.magnitude > 98.0) {
407 cpl_msg_info(cpl_func,
"Get the star magnitude");
408 if (sofi_img_zpoint_get_mag(stdstars, sofi_img_zpoint_config.ra,
409 sofi_img_zpoint_config.dec,
410 sofi_img_zpoint_config.band)) {
411 cpl_msg_error(cpl_func,
"Cannot get the magnitude from the catalog");
415 cpl_msg_info(cpl_func,
"Star magnitude with filter %s : %g",
416 sofi_img_zpoint_config.filter,
417 sofi_img_zpoint_config.magnitude);
420 cpl_msg_info(cpl_func,
"Load images");
421 if ((imlist = sofi_img_zpoint_load(set, flat, detlin_a, detlin_b,
422 detlin_c)) == NULL) {
423 cpl_msg_error(cpl_func,
"Cannot load the images");
428 cpl_msg_info(cpl_func,
"Detect a bright object in the first frame");
429 size_x = cpl_image_get_size_x(cpl_imagelist_get(imlist, 0));
430 size_y = cpl_image_get_size_y(cpl_imagelist_get(imlist, 0));
431 aperts = cpl_apertures_extract_sigma(cpl_imagelist_get(imlist, 0), 5.0);
432 min_dist = size_x * size_x + size_y * size_y;
433 for (i=0; i<cpl_apertures_get_size(aperts); i++) {
434 pos_x = cpl_apertures_get_centroid_x(aperts, i+1);
435 pos_y = cpl_apertures_get_centroid_y(aperts, i+1);
436 dist = (pos_x-size_x/2)*(pos_x-size_x/2) +
437 (pos_y-size_y/2)*(pos_y-size_y/2);
444 cpl_apertures_delete(aperts);
445 if (cpl_error_get_code()) {
446 cpl_msg_error(cpl_func,
"Cannot find the central object");
447 cpl_imagelist_delete(imlist);
450 cpl_msg_info(cpl_func,
451 "Bright object position: %g %g", pos_x_cen, pos_y_cen);
454 cpl_msg_info(cpl_func,
"Read the offsets in the header");
455 offsets = sofi_img_zpoint_get_offsets(set);
458 niter = cpl_imagelist_get_size(imlist);
459 positions = cpl_bivector_new(niter);
460 for (i=0; i<niter; i++) {
461 off_x = cpl_vector_get(cpl_bivector_get_x(offsets), i) -
462 cpl_vector_get(cpl_bivector_get_x(offsets), 0);
463 off_y = cpl_vector_get(cpl_bivector_get_y(offsets), i) -
464 cpl_vector_get(cpl_bivector_get_y(offsets), 0);
465 pos_x = pos_x_cen + off_x;
466 pos_y = pos_y_cen + off_y;
467 cpl_vector_set(cpl_bivector_get_x(positions), i, pos_x);
468 cpl_vector_set(cpl_bivector_get_y(positions), i, pos_y);
470 cpl_bivector_delete(offsets);
473 cpl_msg_info(cpl_func,
"Refine the star positions");
474 for (i=1; i<niter; i++) {
475 pos_x = cpl_vector_get(cpl_bivector_get_x(positions), i);
476 pos_y = cpl_vector_get(cpl_bivector_get_y(positions), i);
477 llx = pos_x - sofi_img_zpoint_config.sx;
478 urx = pos_x + sofi_img_zpoint_config.sx;
479 lly = pos_y - sofi_img_zpoint_config.sy;
480 ury = pos_y + sofi_img_zpoint_config.sy;
481 ima = cpl_imagelist_get(imlist, i);
482 pos_x = cpl_image_get_centroid_x_window(ima, llx, lly, urx, ury);
483 pos_y = cpl_image_get_centroid_y_window(ima, llx, lly, urx, ury);
484 cpl_vector_set(cpl_bivector_get_x(positions), i, pos_x);
485 cpl_vector_set(cpl_bivector_get_y(positions), i, pos_y);
487 if (cpl_error_get_code()) {
488 cpl_msg_error(cpl_func,
"Cannot refine the positions");
489 cpl_imagelist_delete(imlist);
490 cpl_bivector_delete(positions);
495 if (sofi_img_zpoint_config.check_im) {
496 *check_im = sofi_img_zpoint_check_im(imlist, positions,
497 sofi_img_zpoint_config.phot_star_radius,
498 sofi_img_zpoint_config.phot_bg_r1,
499 sofi_img_zpoint_config.phot_bg_r2);
503 cpl_msg_info(cpl_func,
"Compute the photometry");
504 cpl_msg_indent_more();
505 if ((tab_res = sofi_img_zpoint_photom(imlist, positions)) == NULL) {
506 cpl_msg_error(cpl_func,
"Cannot reduce");
507 cpl_bivector_delete(positions);
508 cpl_imagelist_delete(imlist);
509 cpl_image_delete(*check_im);
511 cpl_msg_indent_less();
514 cpl_msg_indent_less();
515 cpl_imagelist_delete(imlist);
516 cpl_bivector_delete(positions);
519 sofi_img_zpoint_config.flux_med =
520 cpl_table_get_column_median(tab_res,
"FLUX");
523 sofi_img_zpoint_config.fwhm_mean =
524 cpl_table_get_column_mean(tab_res,
"FWHMX");
525 sofi_img_zpoint_config.fwhm_mean +=
526 cpl_table_get_column_mean(tab_res,
"FWHMY");
527 sofi_img_zpoint_config.fwhm_mean /= 2.0;
530 tmp_vec = cpl_vector_new(niter);
531 for (i=0; i<niter; i++) {
532 cpl_vector_set(tmp_vec, i,
533 cpl_table_get_double(tab_res,
"FLUX", i, NULL));
535 sofi_img_zpoint_gradients(tmp_vec,
536 &(sofi_img_zpoint_config.gradx),
537 &(sofi_img_zpoint_config.grady),
538 &(sofi_img_zpoint_config.graddx),
539 &(sofi_img_zpoint_config.graddy));
540 cpl_vector_delete(tmp_vec);
543 tmp_vec = cpl_vector_new(niter);
544 for (i=0; i<niter; i++) {
545 cpl_vector_set(tmp_vec, i,
546 cpl_table_get_double(tab_res,
"ZPOINT", i, NULL));
549 avg_zp = sqsum = 0.0;
550 cpl_vector_sort(tmp_vec, 1);
552 for (i=1; i<niter-1; i++) {
553 val = cpl_vector_get(tmp_vec, i);
560 cpl_vector_delete(tmp_vec);
562 cpl_msg_error(cpl_func,
"no valid zpoint measurement: cannot compute");
563 sofi_img_zpoint_config.zpoint = -1.0;
564 sofi_img_zpoint_config.zpointrms = -1.0;
566 avg_zp /= (double)nb_ok;
567 sqsum /= (double)nb_ok;
568 zprms = sqsum - avg_zp * avg_zp;
569 zprms = zprms > 0 ? sqrt(zprms) : 0;
570 sofi_img_zpoint_config.zpoint = avg_zp;
571 sofi_img_zpoint_config.zpointrms = zprms;
575 cpl_msg_info(cpl_func,
"***** FINAL RESULTS *****");
576 cpl_msg_info(cpl_func,
"Zero point : %g", sofi_img_zpoint_config.zpoint);
577 cpl_msg_info(cpl_func,
"Zero p. RMS: %g", sofi_img_zpoint_config.zpointrms);
598 static cpl_image * sofi_img_zpoint_check_im(
599 cpl_imagelist * imlist,
600 cpl_bivector * positions,
605 int nima, in_nx, in_ny, nx, ny, box_sz, llx, lly;
617 if (imlist == NULL)
return NULL;
618 if (positions == NULL)
return NULL;
619 nima = cpl_imagelist_get_size(imlist);
620 if (cpl_bivector_get_size(positions) != nima)
return NULL;
623 in_ima = cpl_imagelist_get(imlist, 0);
624 in_nx = cpl_image_get_size_x(in_ima);
625 in_ny = cpl_image_get_size_y(in_ima);
626 pos_x = cpl_bivector_get_x_data(positions);
627 pos_y = cpl_bivector_get_y_data(positions);
630 box_sz = 2 * (int)r3 + 1;
633 out_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
634 pout_ima = cpl_image_get_data_float(out_ima);
637 for (k=0; k<nima; k++) {
639 in_ima = cpl_imagelist_get(imlist, k);
640 pin_ima = cpl_image_get_data_float(in_ima);
643 llx = (int)(pos_x[k] - r3);
644 lly = (int)(pos_y[k] - r3);
646 for (i=0; i<box_sz; i++) {
647 for (j=0; j<box_sz; j++) {
648 out_pos = (box_sz * k) + i + j * nx;
649 in_pos = llx + i + (lly+j) * in_nx;
650 if (in_pos >= 0 && in_pos < in_nx*in_ny)
651 pout_ima[out_pos] = pin_ima[in_pos];
657 for (i=0; i<box_sz; i++) {
658 for (j=0; j<box_sz; j++) {
659 dist = sqrt((i - (box_sz/2)) * (i - (box_sz/2)) +
660 (j - (box_sz/2)) * (j - (box_sz/2)));
661 if ((fabs(dist-sqrt(r1*r1))) < 0.5) pout_ima[i + j * nx] = 10000;
662 if ((fabs(dist-sqrt(r2*r2))) < 0.5) pout_ima[i + j * nx] = 10000;
663 if ((fabs(dist-sqrt(r3*r3))) < 0.5) pout_ima[i + j * nx] = 10000;
678 static cpl_imagelist * sofi_img_zpoint_load(
681 const char * detlin_a,
682 const char * detlin_b,
683 const char * detlin_c)
686 cpl_imagelist * imlist;
687 cpl_imagelist * diffs;
693 if ((imlist = cpl_imagelist_load_frameset(set, CPL_TYPE_FLOAT, 1,
695 cpl_msg_error(cpl_func,
"Cannot load the images");
700 if (detlin_a && detlin_b && detlin_c) {
701 cpl_msg_info(cpl_func,
"Correct for non-linearity");
703 cpl_msg_error(cpl_func,
"Cannot correct for non-linearity");
709 cpl_msg_info(cpl_func,
"Divide by the flat field");
710 flat_im = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, 0);
711 if (cpl_imagelist_divide_image(imlist, flat_im) != CPL_ERROR_NONE) {
712 cpl_msg_error(cpl_func,
"Cannot divide by flat field");
713 if (flat_im) cpl_image_delete(flat_im);
714 cpl_imagelist_delete(imlist);
717 cpl_image_delete(flat_im);
721 diffs = cpl_imagelist_new();
722 for (i=0; i<cpl_imagelist_get_size(imlist)-1; i++) {
723 ima = cpl_image_subtract_create(
724 cpl_imagelist_get(imlist, i),
725 cpl_imagelist_get(imlist, i+1));
726 cpl_imagelist_set(diffs, ima, 2*i);
727 ima = cpl_image_subtract_create(
728 cpl_imagelist_get(imlist, i+1),
729 cpl_imagelist_get(imlist, i));
730 cpl_imagelist_set(diffs, ima, 2*i+1);
732 cpl_imagelist_delete(imlist);
733 if (cpl_error_get_code()) {
734 cpl_msg_error(cpl_func,
"Cannot build the difference images");
735 cpl_imagelist_delete(diffs);
751 static cpl_table * sofi_img_zpoint_photom(
752 cpl_imagelist * ilist,
757 double r, r1, r2, mag, dit;
761 double bgd, fl, zp, peak, fwhm_x, fwhm_y;
765 if (ilist == NULL)
return NULL;
766 if (pos == NULL)
return NULL;
769 nbima = cpl_imagelist_get_size(ilist);
770 mag = sofi_img_zpoint_config.magnitude;
771 dit = sofi_img_zpoint_config.dit;
772 r = sofi_img_zpoint_config.phot_star_radius;
773 r1 = sofi_img_zpoint_config.phot_bg_r1;
774 r2 = sofi_img_zpoint_config.phot_bg_r2;
777 tab = cpl_table_new(nbima);
778 cpl_table_new_column(tab,
"POSX", CPL_TYPE_DOUBLE);
779 cpl_table_new_column(tab,
"POSY", CPL_TYPE_DOUBLE);
780 cpl_table_new_column(tab,
"ZPOINT", CPL_TYPE_DOUBLE);
781 cpl_table_new_column(tab,
"FLUX", CPL_TYPE_DOUBLE);
782 cpl_table_new_column(tab,
"PEAK", CPL_TYPE_DOUBLE);
783 cpl_table_new_column(tab,
"BGD", CPL_TYPE_DOUBLE);
784 cpl_table_new_column(tab,
"FWHMX", CPL_TYPE_DOUBLE);
785 cpl_table_new_column(tab,
"FWHMY", CPL_TYPE_DOUBLE);
788 pos_x = cpl_bivector_get_x_data(pos);
789 pos_y = cpl_bivector_get_y_data(pos);
790 for (i=0; i<nbima; i++) {
792 ima = cpl_imagelist_get(ilist, i);
795 bgd = irplib_strehl_ring_background(ima, (
int)(pos_x[i]),
796 (
int)(pos_y[i]), (
int)r1, (
int)r2, IRPLIB_BG_METHOD_MEDIAN);
798 fl = irplib_strehl_disk_flux(ima,
799 (
int)(pos_x[i]), (
int)(pos_y[i]), (
int)r, bgd);
801 zp = mag + 2.5 * log10(fl) - 2.5 * log10(dit);
802 cpl_msg_info(cpl_func,
"Zero point nb %d: %g", i+1, zp);
804 peak = cpl_image_get_max_window(ima,
805 (
int)(pos_x[i]-5), (
int)(pos_y[i]-5),
806 (
int)(pos_x[i]+5), (
int)(pos_y[i]+5));
809 if (cpl_image_get_fwhm(ima, (
int)(pos_x[i]), (
int)(pos_y[i]),
810 &fwhm_x, &fwhm_y) != CPL_ERROR_NONE) {
811 cpl_msg_debug(cpl_func,
"Cannot compute FWHM for image %d", i+1);
816 cpl_table_set_double(tab,
"POSX", i, pos_x[i]);
817 cpl_table_set_double(tab,
"POSY", i, pos_y[i]);
818 cpl_table_set_double(tab,
"ZPOINT", i, zp);
819 cpl_table_set_double(tab,
"FLUX", i, fl);
820 cpl_table_set_double(tab,
"PEAK", i, peak);
821 cpl_table_set_double(tab,
"BGD", i, bgd);
822 cpl_table_set_double(tab,
"FWHMX", i, fwhm_x);
823 cpl_table_set_double(tab,
"FWHMY", i, fwhm_y);
842 static int sofi_img_zpoint_gradients(
850 double two, three, four, five, mean;
854 if (flux == NULL)
return CPL_ERROR_UNSPECIFIED;
857 nflux = cpl_vector_get_size(flux);
865 mean = cpl_vector_get_mean(flux);
868 pflux = cpl_vector_get_data(flux);
869 two = (pflux[1] + pflux[2]) / 2.0;
870 three = (pflux[3] + pflux[4]) / 2.0;
871 four = (pflux[5] + pflux[6]) / 2.0;
874 *gradx = (two + five - three - four) / 2;
875 *grady = (three + two - four - five) / 2;
876 *graddx = sqrt((two-five)*(two-five) + (three-four)*(three-four))/2.0;
877 *graddy = sqrt((three-two)*(three-two) + (four-five)*(four-five))/2.0;
894 static cpl_bivector * sofi_img_zpoint_get_offsets(cpl_frameset * set)
899 cpl_frame * cur_frame;
900 cpl_propertylist * plist;
905 if (set == NULL)
return NULL;
908 nframes = cpl_frameset_get_size(set);
910 noffsets = 2 * (nframes-1);
913 if (cpl_error_get_code())
return NULL;
916 offs = cpl_bivector_new(noffsets);
919 for (i=0; i<nframes; i++) {
920 cur_frame = cpl_frameset_get_position(set, i);
921 plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame),0);
924 cpl_propertylist_delete(plist);
926 cpl_vector_set(cpl_bivector_get_x(offs), 2*i, off_x);
927 cpl_vector_set(cpl_bivector_get_y(offs), 2*i, off_y);
928 }
else if (i==nframes-1) {
929 cpl_vector_set(cpl_bivector_get_x(offs), 2*i-1, off_x);
930 cpl_vector_set(cpl_bivector_get_y(offs), 2*i-1, off_y);
932 cpl_vector_set(cpl_bivector_get_x(offs), 2*i, off_x);
933 cpl_vector_set(cpl_bivector_get_y(offs), 2*i, off_y);
934 cpl_vector_set(cpl_bivector_get_x(offs), 2*i-1, off_x);
935 cpl_vector_set(cpl_bivector_get_y(offs), 2*i-1, off_y);
940 if (cpl_error_get_code()) {
941 cpl_bivector_delete(offs);
942 cpl_msg_error(cpl_func,
"Cannot find offsets in headers");
958 static cpl_error_code sofi_img_zpoint_get_mag(
const char * stdstars,
963 cpl_errorstate prestate = cpl_errorstate_get();
966 bug_if(stdstars == NULL);
974 if (!irplib_stdstar_find_star(stdstars, ra, dec,
977 &sofi_img_zpoint_config.magnitude,
978 &sofi_img_zpoint_config.starname,
979 &sofi_img_zpoint_config.sptype,
980 &sofi_img_zpoint_config.catalog,
982 !irplib_stdstar_find_star(stdstars, ra, dec,
984 "LCO-Palomar-NICMOS-Red-Stars.txt",
985 &sofi_img_zpoint_config.magnitude,
986 &sofi_img_zpoint_config.starname,
987 &sofi_img_zpoint_config.sptype,
988 &sofi_img_zpoint_config.catalog,
990 !irplib_stdstar_find_star(stdstars, ra, dec,
993 &sofi_img_zpoint_config.magnitude,
994 &sofi_img_zpoint_config.starname,
995 &sofi_img_zpoint_config.sptype,
996 &sofi_img_zpoint_config.catalog,
999 (band == SOFI_BAND_K &&
1000 !irplib_stdstar_find_star(stdstars, ra, dec,
1003 &sofi_img_zpoint_config.magnitude,
1004 &sofi_img_zpoint_config.starname,
1005 &sofi_img_zpoint_config.sptype,
1006 &sofi_img_zpoint_config.catalog,
1007 NULL, NULL, 2.0)) ||
1008 (band == SOFI_BAND_KS &&
1009 !irplib_stdstar_find_star(stdstars, ra, dec,
1012 &sofi_img_zpoint_config.magnitude,
1013 &sofi_img_zpoint_config.starname,
1014 &sofi_img_zpoint_config.sptype,
1015 &sofi_img_zpoint_config.catalog,
1016 NULL, NULL, 2.0))) {
1017 cpl_errorstate_set(prestate);
1025 if (!irplib_stdstar_find_star(stdstars, ra, dec,
1027 "ESO-VanDerBliek.txt",
1028 &sofi_img_zpoint_config.magnitude,
1029 &sofi_img_zpoint_config.starname,
1030 &sofi_img_zpoint_config.sptype,
1031 &sofi_img_zpoint_config.catalog,
1033 !irplib_stdstar_find_star(stdstars, ra, dec,
1035 "MSSSO-Photometric.txt",
1036 &sofi_img_zpoint_config.magnitude,
1037 &sofi_img_zpoint_config.starname,
1038 &sofi_img_zpoint_config.sptype,
1039 &sofi_img_zpoint_config.catalog,
1041 !irplib_stdstar_find_star(stdstars, ra, dec,
1043 "MSSSO-Spectroscopic.txt",
1044 &sofi_img_zpoint_config.magnitude,
1045 &sofi_img_zpoint_config.starname,
1046 &sofi_img_zpoint_config.sptype,
1047 &sofi_img_zpoint_config.catalog,
1049 !irplib_stdstar_find_star(stdstars, ra, dec,
1051 &sofi_img_zpoint_config.magnitude,
1052 &sofi_img_zpoint_config.starname,
1053 &sofi_img_zpoint_config.sptype,
1054 &sofi_img_zpoint_config.catalog,
1056 cpl_errorstate_set(prestate);
1062 cpl_msg_error(cpl_func,
"cannot determine associated filter");
1068 return cpl_error_get_code();
1082 static int sofi_img_zpoint_save(
1084 cpl_image * check_im,
1086 const cpl_parameterlist * parlist,
1089 cpl_propertylist * plist;
1090 cpl_propertylist * paflist;
1091 cpl_propertylist * qclist;
1092 const cpl_frame * ref_frame;
1094 double hum = 0.0, airm;
1097 qclist = cpl_propertylist_new();
1100 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
1101 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
1103 cpl_msg_error(cpl_func,
"getting header from reference frame");
1104 cpl_propertylist_delete(qclist);
1105 return CPL_ERROR_UNSPECIFIED;
1108 if (cpl_error_get_code()) {
1109 cpl_propertylist_delete(qclist);
1110 cpl_propertylist_delete(plist);
1111 return CPL_ERROR_UNSPECIFIED;
1114 if (cpl_error_get_code()) cpl_error_reset();
1115 else cpl_propertylist_append_string(qclist,
"ESO QC FILTER OBS", sval);
1116 cpl_propertylist_append_string(qclist,
"ESO QC FILTER REF",
1118 if (sofi_img_zpoint_compute_keywords(raw, &hum, &airm) != -1) {
1119 cpl_propertylist_append_double(qclist,
"ESO QC AMBI RHUM AVG", hum);
1120 cpl_propertylist_append_double(qclist,
"ESO QC AIRMASS", airm);
1122 cpl_propertylist_delete(plist);
1123 cpl_propertylist_append_double(qclist,
"ESO QC ZPOINT",
1124 sofi_img_zpoint_config.zpoint);
1125 cpl_propertylist_append_double(qclist,
"ESO QC ZPOINTRMS",
1126 sofi_img_zpoint_config.zpointrms);
1127 cpl_propertylist_append_double(qclist,
"ESO QC FLUX MED",
1128 sofi_img_zpoint_config.flux_med);
1129 cpl_propertylist_append_string(qclist,
"ESO QC STDNAME",
1130 sofi_img_zpoint_config.starname);
1131 cpl_propertylist_append_string(qclist,
"ESO QC SPECTYPE",
1132 sofi_img_zpoint_config.sptype);
1133 cpl_propertylist_append_double(qclist,
"ESO QC STARMAG",
1134 sofi_img_zpoint_config.magnitude);
1135 cpl_propertylist_append_string(qclist,
"ESO QC CATNAME",
1136 sofi_img_zpoint_config.catalog);
1137 cpl_propertylist_append_double(qclist,
"ESO QC GRADX",
1138 sofi_img_zpoint_config.gradx);
1139 cpl_propertylist_append_double(qclist,
"ESO QC GRADY",
1140 sofi_img_zpoint_config.grady);
1141 cpl_propertylist_append_double(qclist,
"ESO QC GRADDX",
1142 sofi_img_zpoint_config.graddx);
1143 cpl_propertylist_append_double(qclist,
"ESO QC GRADDY",
1144 sofi_img_zpoint_config.graddy);
1145 cpl_propertylist_append_double(qclist,
"ESO QC FWHM MEAN",
1146 sofi_img_zpoint_config.fwhm_mean);
1149 irplib_dfs_save_table(set,
1155 SOFI_IMG_ZPOINT_TAB,
1158 PACKAGE
"/" PACKAGE_VERSION,
1159 "sofi_img_zpoint.fits");
1163 irplib_dfs_save_image(set,
1169 SOFI_IMG_ZPOINT_CHECK,
1172 PACKAGE
"/" PACKAGE_VERSION,
1173 "sofi_img_zpoint_check.fits");
1177 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
1180 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
1182 cpl_msg_error(cpl_func,
"getting header from reference frame");
1183 cpl_propertylist_delete(qclist);
1184 return CPL_ERROR_UNSPECIFIED;
1188 paflist = cpl_propertylist_new();
1189 cpl_propertylist_copy_property_regexp(paflist, plist,
1190 "^(ARCFILE|ESO TPL ID|DATE-OBS|MJD-OBS|ESO TEL AIRM START|ESO INS MODE|"
1191 "ESO OBS ID|ESO DET DIT|ESO INS PIXSCALE|RA|DEC)$", 0);
1192 cpl_propertylist_delete(plist);
1195 cpl_propertylist_copy_property_regexp(paflist, qclist,
"ESO QC", 0);
1196 cpl_propertylist_delete(qclist);
1199 cpl_dfs_save_paf(
"SOFI",
1202 "sofi_img_zpoint.paf");
1203 cpl_propertylist_delete(paflist);
1214 static int sofi_img_zpoint_compute_keywords(
1220 cpl_vector * hum_vec;
1221 cpl_frame * cur_frame;
1222 cpl_propertylist * plist;
1226 if (set == NULL)
return CPL_ERROR_UNSPECIFIED;
1229 nframes = cpl_frameset_get_size(set);
1232 hum_vec = cpl_vector_new(nframes);
1234 for (i=0; i<nframes; i++) {
1235 if (cpl_error_get_code()) {
1236 cpl_vector_delete(hum_vec);
1237 return CPL_ERROR_UNSPECIFIED;
1239 cur_frame = cpl_frameset_get_position(set, i);
1240 plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
1244 cpl_propertylist_delete(plist);
1245 if (cpl_error_get_code()) {
1246 cpl_vector_delete(hum_vec);
1248 return CPL_ERROR_UNSPECIFIED;
1251 *hum = cpl_vector_get_mean(hum_vec);
1255 cpl_vector_delete(hum_vec);
1256 if (cpl_error_get_code())
return CPL_ERROR_UNSPECIFIED;