00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036
00037
00038 #include "irplib_plugin.h"
00039 #include "irplib_utils.h"
00040 #include "irplib_calib.h"
00041 #include "irplib_stdstar.h"
00042 #include "irplib_strehl.h"
00043
00044 #include "sofi_utils.h"
00045 #include "sofi_pfits.h"
00046 #include "sofi_dfs.h"
00047
00048 #include <string.h>
00049 #include <math.h>
00050 #include <cpl.h>
00051
00052
00053
00054
00055
00056
00057 #define RECIPE_STRING "sofi_img_zpoint"
00058
00059 #define PHOT_STAR_RADIUS 30.0
00060 #define PHOT_BACKGROUND_R1 40.0
00061 #define PHOT_BACKGROUND_R2 60.0
00062
00063 #define DEF_LOCATE_SX 10
00064 #define DEF_LOCATE_SY 10
00065
00066
00067
00068
00069
00070 static cpl_table * sofi_img_zpoint_reduce(cpl_frameset *, const char *,
00071 const char *, const char *, const char *, const char *, cpl_image **);
00072 static cpl_imagelist * sofi_img_zpoint_load(cpl_frameset *, const char *,
00073 const char *, const char *, const char *);
00074 static int sofi_img_zpoint_save(cpl_table *, cpl_image *, cpl_frameset *,
00075 const cpl_parameterlist *, cpl_frameset *);
00076 static cpl_table * sofi_img_zpoint_photom(cpl_imagelist *, cpl_bivector *);
00077 static cpl_error_code sofi_img_zpoint_get_mag(const char *, double, double,
00078 sofi_band);
00079 static int sofi_img_zpoint_gradients(cpl_vector *, double *, double *,
00080 double *, double *);
00081 static cpl_bivector * sofi_img_zpoint_get_offsets(cpl_frameset *);
00082 static int sofi_img_zpoint_compute_keywords(cpl_frameset *, double *,double *);
00083 static cpl_image * sofi_img_zpoint_check_im(cpl_imagelist *,
00084 cpl_bivector *, double, double, double);
00085
00086 cpl_recipe_define(sofi_img_zpoint, SOFI_BINARY_VERSION,
00087 "Lars Lundin", PACKAGE_BUGREPORT, "2008",
00088 "SOFI Zero point computation recipe",
00089 RECIPE_STRING " -- SOFI Zero point recipe\n"
00090 "The files listed in the Set Of Frames (sof-file) must "
00091 "be tagged:\n"
00092 "raw-file.fits "SOFI_IMG_ZPOINT_RAW" or\n"
00093 "stdstars.fits "SOFI_CALIB_STDSTARS" or\n"
00094 "flat-file.fits "SOFI_CALIB_FLAT" or\n"
00095 "detlin-a-file.fits "SOFI_CALIB_DETLIN_A" or\n"
00096 "detlin-b-file.fits "SOFI_CALIB_DETLIN_B" or\n"
00097 "detlin-c-file.fits "SOFI_CALIB_DETLIN_C"\n");
00098
00099
00100
00101
00102
00103 static struct {
00104
00105 double ra;
00106 double dec;
00107 double magnitude;
00108 int sx;
00109 int sy;
00110 double phot_star_radius;
00111 double phot_bg_r1;
00112 double phot_bg_r2;
00113 int check_im;
00114
00115 double dit;
00116 char filter[512];
00117 sofi_band band;
00118 char * starname;
00119 char * sptype;
00120 char * catalog;
00121 double zpoint;
00122 double zpointrms;
00123 double flux_med;
00124 double gradx;
00125 double grady;
00126 double graddx;
00127 double graddy;
00128 double fwhm_mean;
00129 } sofi_img_zpoint_config;
00130
00131
00132
00133
00134
00135
00143
00144 static
00145 cpl_error_code sofi_img_zpoint_fill_parameterlist(cpl_parameterlist * self)
00146 {
00147
00148 const char * context = PACKAGE "." RECIPE_STRING;
00149 cpl_error_code err;
00150
00151 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
00152
00153
00154
00155
00156 err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
00157 "star_r", PHOT_STAR_RADIUS, NULL,
00158 context, "The star radius");
00159 cpl_ensure_code(!err, err);
00160
00161
00162 err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
00163 "bg_r1", PHOT_BACKGROUND_R1, NULL,
00164 context, "The internal background "
00165 "radius");
00166 cpl_ensure_code(!err, err);
00167
00168
00169 err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
00170 "bg_r2", PHOT_BACKGROUND_R2, NULL,
00171 context, "The external background "
00172 "radius");
00173 cpl_ensure_code(!err, err);
00174
00175
00176 err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
00177 "ra", 999.0, NULL, context,
00178 "Right Ascension [degrees]");
00179 cpl_ensure_code(!err, err);
00180
00181
00182 err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
00183 "dec", 999.0, NULL, context,
00184 "Declination [degrees]");
00185 cpl_ensure_code(!err, err);
00186
00187
00188 err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
00189 "mag", 99.0, NULL, context,
00190 "Standard Star Magnitude to use "
00191 "instead of catalogue value");
00192 cpl_ensure_code(!err, err);
00193
00194
00195 err = irplib_parameterlist_set_int(self, PACKAGE, RECIPE_STRING,
00196 "sx", DEF_LOCATE_SX, NULL, context,
00197 "X-size of the search window");
00198 cpl_ensure_code(!err, err);
00199
00200
00201 err = irplib_parameterlist_set_int(self, PACKAGE, RECIPE_STRING,
00202 "sy", DEF_LOCATE_SY, NULL, context,
00203 "Y-size of the search window");
00204 cpl_ensure_code(!err, err);
00205
00206
00207 err = irplib_parameterlist_set_bool(self, PACKAGE, RECIPE_STRING,
00208 "check_im", CPL_FALSE, NULL, context,
00209 "Flag to create the check image");
00210 cpl_ensure_code(!err, err);
00211
00212 return CPL_ERROR_NONE;
00213 }
00214
00215
00216
00217
00224
00225 static int sofi_img_zpoint(cpl_frameset * framelist,
00226 const cpl_parameterlist * parlist)
00227 {
00228 const char * stdstars;
00229 const char * flat;
00230 const char * detlin_a;
00231 const char * detlin_b;
00232 const char * detlin_c;
00233 cpl_frameset * rawframes = NULL;
00234 cpl_table * tab = NULL;
00235 cpl_image * check_im = NULL;
00236
00237
00238 sofi_img_zpoint_config.starname = NULL;
00239 sofi_img_zpoint_config.sptype = NULL;
00240 sofi_img_zpoint_config.filter[0] = (char)0;
00241 sofi_img_zpoint_config.catalog = NULL;
00242 sofi_img_zpoint_config.gradx = -1.0;
00243 sofi_img_zpoint_config.grady = -1.0;
00244 sofi_img_zpoint_config.graddx = -1.0;
00245 sofi_img_zpoint_config.graddy = -1.0;
00246 sofi_img_zpoint_config.fwhm_mean = -1.0;
00247
00248
00249
00250 sofi_img_zpoint_config.ra =
00251 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING, "ra");
00252
00253
00254 sofi_img_zpoint_config.dec =
00255 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING, "dec");
00256
00257
00258 sofi_img_zpoint_config.magnitude =
00259 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING, "mag");
00260
00261
00262 sofi_img_zpoint_config.sx =
00263 irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING, "sx");
00264
00265
00266 sofi_img_zpoint_config.sy =
00267 irplib_parameterlist_get_int(parlist, PACKAGE, RECIPE_STRING, "sy");
00268
00269
00270 sofi_img_zpoint_config.phot_star_radius =
00271 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
00272 "star_r");
00273
00274 sofi_img_zpoint_config.phot_bg_r1 =
00275 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
00276 "bg_r1");
00277
00278
00279 sofi_img_zpoint_config.phot_bg_r2 =
00280 irplib_parameterlist_get_double(parlist, PACKAGE, RECIPE_STRING,
00281 "bg_r2");
00282
00283
00284 sofi_img_zpoint_config.check_im =
00285 irplib_parameterlist_get_bool(parlist, PACKAGE, RECIPE_STRING,
00286 "check_im");
00287
00288
00289 skip_if (sofi_dfs_set_groups(framelist));
00290
00291
00292
00293 detlin_a = sofi_extract_filename(framelist, SOFI_CALIB_DETLIN_A);
00294 detlin_b = sofi_extract_filename(framelist, SOFI_CALIB_DETLIN_B);
00295 detlin_c = sofi_extract_filename(framelist, SOFI_CALIB_DETLIN_C);
00296 flat = sofi_extract_filename(framelist, SOFI_CALIB_FLAT);
00297 stdstars = sofi_extract_filename(framelist, SOFI_CALIB_STDSTARS);
00298
00299
00300 rawframes = sofi_extract_frameset(framelist, SOFI_IMG_ZPOINT_RAW);
00301 error_if(rawframes == NULL, CPL_ERROR_DATA_NOT_FOUND, "Cannot find "
00302 "raw frames in the input list");
00303
00304
00305 cpl_msg_info(cpl_func, "Reduce the data");
00306 cpl_msg_indent_more();
00307 tab = sofi_img_zpoint_reduce(rawframes, stdstars, flat, detlin_a,
00308 detlin_b, detlin_c, &check_im);
00309 cpl_msg_indent_less();
00310 skip_if (tab == NULL);
00311
00312
00313 cpl_msg_info(cpl_func, "Save the paf file");
00314 cpl_msg_indent_more();
00315 sofi_img_zpoint_save(tab, check_im, rawframes, parlist, framelist);
00316 cpl_msg_indent_less();
00317 skip_if (0);
00318
00319 end_skip;
00320
00321 cpl_image_delete(check_im);
00322 cpl_frameset_delete(rawframes);
00323 cpl_table_delete(tab);
00324
00325 cpl_free(sofi_img_zpoint_config.starname);
00326 cpl_free(sofi_img_zpoint_config.sptype);
00327 cpl_free(sofi_img_zpoint_config.catalog);
00328
00329 return cpl_error_get_code();
00330 }
00331
00332
00344
00345 static cpl_table * sofi_img_zpoint_reduce(
00346 cpl_frameset * set,
00347 const char * stdstars,
00348 const char * flat,
00349 const char * detlin_a,
00350 const char * detlin_b,
00351 const char * detlin_c,
00352 cpl_image ** check_im)
00353 {
00354 cpl_frame * cur_frame;
00355 cpl_propertylist * plist;
00356 const char * sval;
00357 cpl_imagelist * imlist;
00358 cpl_image * ima;
00359 int niter;
00360 int size_x, size_y;
00361 double pos_x, pos_y, pos_x_cen, pos_y_cen, dist, min_dist;
00362 cpl_apertures * aperts;
00363 cpl_bivector * offsets,
00364 * positions;
00365 double off_x, off_y;
00366 int llx, lly, urx, ury;
00367 double val, sqsum, zprms, avg_zp;
00368 cpl_table * tab_res;
00369 cpl_vector * tmp_vec;
00370 int nb_ok;
00371 int i;
00372
00373
00374 *check_im = NULL;
00375 pos_x_cen = pos_y_cen = -1.0;
00376
00377
00378 cur_frame = cpl_frameset_get_frame(set, 0);
00379 plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
00380 if ((sval = sofi_pfits_get_filter(plist)) == NULL) {
00381 cpl_propertylist_delete(plist);
00382 return NULL;
00383 } else sprintf(sofi_img_zpoint_config.filter, sval);
00384 if (sofi_img_zpoint_config.ra > 998.0)
00385 sofi_img_zpoint_config.ra = sofi_pfits_get_ra(plist);
00386 if (sofi_img_zpoint_config.dec > 998.0)
00387 sofi_img_zpoint_config.dec = sofi_pfits_get_dec(plist);
00388 sofi_img_zpoint_config.dit = sofi_pfits_get_dit(plist);
00389 cpl_propertylist_delete(plist);
00390 if (cpl_error_get_code()) {
00391 cpl_msg_error(cpl_func, "Cannot get some header informations");
00392 return NULL;
00393 }
00394 cpl_msg_info(cpl_func, "Using star at position: RA = %g; DEC = %g",
00395 sofi_img_zpoint_config.ra, sofi_img_zpoint_config.dec);
00396
00397
00398 if ((sofi_img_zpoint_config.band =
00399 sofi_get_bbfilter(sofi_img_zpoint_config.filter)) ==
00400 SOFI_BAND_UNKNOWN) {
00401 cpl_msg_error(cpl_func, "Cannot associate the filter to a BB one");
00402 return NULL;
00403 }
00404
00405
00406 if (sofi_img_zpoint_config.magnitude > 98.0) {
00407 cpl_msg_info(cpl_func, "Get the star magnitude");
00408 if (sofi_img_zpoint_get_mag(stdstars, sofi_img_zpoint_config.ra,
00409 sofi_img_zpoint_config.dec,
00410 sofi_img_zpoint_config.band)) {
00411 cpl_msg_error(cpl_func, "Cannot get the magnitude from the catalog");
00412 return NULL;
00413 }
00414 }
00415 cpl_msg_info(cpl_func, "Star magnitude with filter %s : %g",
00416 sofi_img_zpoint_config.filter,
00417 sofi_img_zpoint_config.magnitude);
00418
00419
00420 cpl_msg_info(cpl_func, "Load images");
00421 if ((imlist = sofi_img_zpoint_load(set, flat, detlin_a, detlin_b,
00422 detlin_c)) == NULL) {
00423 cpl_msg_error(cpl_func, "Cannot load the images");
00424 return NULL;
00425 }
00426
00427
00428 cpl_msg_info(cpl_func, "Detect a bright object in the first frame");
00429 size_x = cpl_image_get_size_x(cpl_imagelist_get(imlist, 0));
00430 size_y = cpl_image_get_size_y(cpl_imagelist_get(imlist, 0));
00431 aperts = cpl_apertures_extract_sigma(cpl_imagelist_get(imlist, 0), 5.0);
00432 min_dist = size_x * size_x + size_y * size_y;
00433 for (i=0; i<cpl_apertures_get_size(aperts); i++) {
00434 pos_x = cpl_apertures_get_centroid_x(aperts, i+1);
00435 pos_y = cpl_apertures_get_centroid_y(aperts, i+1);
00436 dist = (pos_x-size_x/2)*(pos_x-size_x/2) +
00437 (pos_y-size_y/2)*(pos_y-size_y/2);
00438 if (dist<min_dist) {
00439 min_dist = dist;
00440 pos_x_cen = pos_x;
00441 pos_y_cen = pos_y;
00442 }
00443 }
00444 cpl_apertures_delete(aperts);
00445 if (cpl_error_get_code()) {
00446 cpl_msg_error(cpl_func, "Cannot find the central object");
00447 cpl_imagelist_delete(imlist);
00448 return NULL;
00449 }
00450 cpl_msg_info(cpl_func,
00451 "Bright object position: %g %g", pos_x_cen, pos_y_cen);
00452
00453
00454 cpl_msg_info(cpl_func, "Read the offsets in the header");
00455 offsets = sofi_img_zpoint_get_offsets(set);
00456
00457
00458 niter = cpl_imagelist_get_size(imlist);
00459 positions = cpl_bivector_new(niter);
00460 for (i=0; i<niter; i++) {
00461 off_x = cpl_vector_get(cpl_bivector_get_x(offsets), i) -
00462 cpl_vector_get(cpl_bivector_get_x(offsets), 0);
00463 off_y = cpl_vector_get(cpl_bivector_get_y(offsets), i) -
00464 cpl_vector_get(cpl_bivector_get_y(offsets), 0);
00465 pos_x = pos_x_cen + off_x;
00466 pos_y = pos_y_cen + off_y;
00467 cpl_vector_set(cpl_bivector_get_x(positions), i, pos_x);
00468 cpl_vector_set(cpl_bivector_get_y(positions), i, pos_y);
00469 }
00470 cpl_bivector_delete(offsets);
00471
00472
00473 cpl_msg_info(cpl_func, "Refine the star positions");
00474 for (i=1; i<niter; i++) {
00475 pos_x = cpl_vector_get(cpl_bivector_get_x(positions), i);
00476 pos_y = cpl_vector_get(cpl_bivector_get_y(positions), i);
00477 llx = pos_x - sofi_img_zpoint_config.sx;
00478 urx = pos_x + sofi_img_zpoint_config.sx;
00479 lly = pos_y - sofi_img_zpoint_config.sy;
00480 ury = pos_y + sofi_img_zpoint_config.sy;
00481 ima = cpl_imagelist_get(imlist, i);
00482 pos_x = cpl_image_get_centroid_x_window(ima, llx, lly, urx, ury);
00483 pos_y = cpl_image_get_centroid_y_window(ima, llx, lly, urx, ury);
00484 cpl_vector_set(cpl_bivector_get_x(positions), i, pos_x);
00485 cpl_vector_set(cpl_bivector_get_y(positions), i, pos_y);
00486 }
00487 if (cpl_error_get_code()) {
00488 cpl_msg_error(cpl_func, "Cannot refine the positions");
00489 cpl_imagelist_delete(imlist);
00490 cpl_bivector_delete(positions);
00491 return NULL;
00492 }
00493
00494
00495 if (sofi_img_zpoint_config.check_im) {
00496 *check_im = sofi_img_zpoint_check_im(imlist, positions,
00497 sofi_img_zpoint_config.phot_star_radius,
00498 sofi_img_zpoint_config.phot_bg_r1,
00499 sofi_img_zpoint_config.phot_bg_r2);
00500 }
00501
00502
00503 cpl_msg_info(cpl_func, "Compute the photometry");
00504 cpl_msg_indent_more();
00505 if ((tab_res = sofi_img_zpoint_photom(imlist, positions)) == NULL) {
00506 cpl_msg_error(cpl_func, "Cannot reduce");
00507 cpl_bivector_delete(positions);
00508 cpl_imagelist_delete(imlist);
00509 cpl_image_delete(*check_im);
00510 *check_im = NULL;
00511 cpl_msg_indent_less();
00512 return NULL;
00513 }
00514 cpl_msg_indent_less();
00515 cpl_imagelist_delete(imlist);
00516 cpl_bivector_delete(positions);
00517
00518
00519 sofi_img_zpoint_config.flux_med =
00520 cpl_table_get_column_median(tab_res, "FLUX");
00521
00522
00523 sofi_img_zpoint_config.fwhm_mean =
00524 cpl_table_get_column_mean(tab_res, "FWHMX");
00525 sofi_img_zpoint_config.fwhm_mean +=
00526 cpl_table_get_column_mean(tab_res, "FWHMY");
00527 sofi_img_zpoint_config.fwhm_mean /= 2.0;
00528
00529
00530 tmp_vec = cpl_vector_new(niter);
00531 for (i=0; i<niter; i++) {
00532 cpl_vector_set(tmp_vec, i,
00533 cpl_table_get_double(tab_res, "FLUX", i, NULL));
00534 }
00535 sofi_img_zpoint_gradients(tmp_vec,
00536 &(sofi_img_zpoint_config.gradx),
00537 &(sofi_img_zpoint_config.grady),
00538 &(sofi_img_zpoint_config.graddx),
00539 &(sofi_img_zpoint_config.graddy));
00540 cpl_vector_delete(tmp_vec);
00541
00542
00543 tmp_vec = cpl_vector_new(niter);
00544 for (i=0; i<niter; i++) {
00545 cpl_vector_set(tmp_vec, i,
00546 cpl_table_get_double(tab_res, "ZPOINT", i, NULL));
00547 }
00548 nb_ok = 0;
00549 avg_zp = sqsum = 0.0;
00550 cpl_vector_sort(tmp_vec, 1);
00551
00552 for (i=1; i<niter-1; i++) {
00553 val = cpl_vector_get(tmp_vec, i);
00554 if (val > 0.0) {
00555 avg_zp += val;
00556 sqsum += val * val;
00557 nb_ok ++;
00558 }
00559 }
00560 cpl_vector_delete(tmp_vec);
00561 if (nb_ok < 1) {
00562 cpl_msg_error(cpl_func, "no valid zpoint measurement: cannot compute");
00563 sofi_img_zpoint_config.zpoint = -1.0;
00564 sofi_img_zpoint_config.zpointrms = -1.0;
00565 } else {
00566 avg_zp /= (double)nb_ok;
00567 sqsum /= (double)nb_ok;
00568 zprms = sqsum - avg_zp * avg_zp;
00569 zprms = zprms > 0 ? sqrt(zprms) : 0;
00570 sofi_img_zpoint_config.zpoint = avg_zp;
00571 sofi_img_zpoint_config.zpointrms = zprms;
00572 }
00573
00574
00575 cpl_msg_info(cpl_func, "***** FINAL RESULTS *****");
00576 cpl_msg_info(cpl_func, "Zero point : %g", sofi_img_zpoint_config.zpoint);
00577 cpl_msg_info(cpl_func, "Zero p. RMS: %g", sofi_img_zpoint_config.zpointrms);
00578
00579 return tab_res;
00580 }
00581
00582
00597
00598 static cpl_image * sofi_img_zpoint_check_im(
00599 cpl_imagelist * imlist,
00600 cpl_bivector * positions,
00601 double r1,
00602 double r2,
00603 double r3)
00604 {
00605 int nima, in_nx, in_ny, nx, ny, box_sz, llx, lly;
00606 cpl_image * in_ima;
00607 float * pin_ima;
00608 cpl_image * out_ima;
00609 float * pout_ima;
00610 int out_pos, in_pos;
00611 double * pos_x;
00612 double * pos_y;
00613 double dist;
00614 int i, j, k;
00615
00616
00617 if (imlist == NULL) return NULL;
00618 if (positions == NULL) return NULL;
00619 nima = cpl_imagelist_get_size(imlist);
00620 if (cpl_bivector_get_size(positions) != nima) return NULL;
00621
00622
00623 in_ima = cpl_imagelist_get(imlist, 0);
00624 in_nx = cpl_image_get_size_x(in_ima);
00625 in_ny = cpl_image_get_size_y(in_ima);
00626 pos_x = cpl_bivector_get_x_data(positions);
00627 pos_y = cpl_bivector_get_y_data(positions);
00628
00629
00630 box_sz = 2 * (int)r3 + 1;
00631 nx = nima * box_sz;
00632 ny = box_sz;
00633 out_ima = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
00634 pout_ima = cpl_image_get_data_float(out_ima);
00635
00636
00637 for (k=0; k<nima; k++) {
00638
00639 in_ima = cpl_imagelist_get(imlist, k);
00640 pin_ima = cpl_image_get_data_float(in_ima);
00641
00642
00643 llx = (int)(pos_x[k] - r3);
00644 lly = (int)(pos_y[k] - r3);
00645
00646 for (i=0; i<box_sz; i++) {
00647 for (j=0; j<box_sz; j++) {
00648 out_pos = (box_sz * k) + i + j * nx;
00649 in_pos = llx + i + (lly+j) * in_nx;
00650 if (in_pos >= 0 && in_pos < in_nx*in_ny)
00651 pout_ima[out_pos] = pin_ima[in_pos];
00652 }
00653 }
00654 }
00655
00656
00657 for (i=0; i<box_sz; i++) {
00658 for (j=0; j<box_sz; j++) {
00659 dist = sqrt((i - (box_sz/2)) * (i - (box_sz/2)) +
00660 (j - (box_sz/2)) * (j - (box_sz/2)));
00661 if ((fabs(dist-sqrt(r1*r1))) < 0.5) pout_ima[i + j * nx] = 10000;
00662 if ((fabs(dist-sqrt(r2*r2))) < 0.5) pout_ima[i + j * nx] = 10000;
00663 if ((fabs(dist-sqrt(r3*r3))) < 0.5) pout_ima[i + j * nx] = 10000;
00664 }
00665 }
00666
00667
00668 return out_ima;
00669 }
00670
00671
00677
00678 static cpl_imagelist * sofi_img_zpoint_load(
00679 cpl_frameset * set,
00680 const char * flat,
00681 const char * detlin_a,
00682 const char * detlin_b,
00683 const char * detlin_c)
00684
00685 {
00686 cpl_imagelist * imlist;
00687 cpl_imagelist * diffs;
00688 cpl_image * flat_im;
00689 cpl_image * ima;
00690 int i;
00691
00692
00693 if ((imlist = cpl_imagelist_load_frameset(set, CPL_TYPE_FLOAT, 1,
00694 0)) == NULL) {
00695 cpl_msg_error(cpl_func, "Cannot load the images");
00696 return NULL;
00697 }
00698
00699
00700 if (detlin_a && detlin_b && detlin_c) {
00701 cpl_msg_info(cpl_func, "Correct for non-linearity");
00702 if (sofi_detlin_correct(imlist, detlin_a, detlin_b, detlin_c) == -1) {
00703 cpl_msg_error(cpl_func, "Cannot correct for non-linearity");
00704 }
00705 }
00706
00707
00708 if (flat) {
00709 cpl_msg_info(cpl_func, "Divide by the flat field");
00710 flat_im = cpl_image_load(flat, CPL_TYPE_FLOAT, 0, 0);
00711 if (cpl_imagelist_divide_image(imlist, flat_im) != CPL_ERROR_NONE) {
00712 cpl_msg_error(cpl_func, "Cannot divide by flat field");
00713 if (flat_im) cpl_image_delete(flat_im);
00714 cpl_imagelist_delete(imlist);
00715 return NULL;
00716 }
00717 cpl_image_delete(flat_im);
00718 }
00719
00720
00721 diffs = cpl_imagelist_new();
00722 for (i=0; i<cpl_imagelist_get_size(imlist)-1; i++) {
00723 ima = cpl_image_subtract_create(
00724 cpl_imagelist_get(imlist, i),
00725 cpl_imagelist_get(imlist, i+1));
00726 cpl_imagelist_set(diffs, ima, 2*i);
00727 ima = cpl_image_subtract_create(
00728 cpl_imagelist_get(imlist, i+1),
00729 cpl_imagelist_get(imlist, i));
00730 cpl_imagelist_set(diffs, ima, 2*i+1);
00731 }
00732 cpl_imagelist_delete(imlist);
00733 if (cpl_error_get_code()) {
00734 cpl_msg_error(cpl_func, "Cannot build the difference images");
00735 cpl_imagelist_delete(diffs);
00736 return NULL;
00737 }
00738 return diffs;
00739 }
00740
00741
00750
00751 static cpl_table * sofi_img_zpoint_photom(
00752 cpl_imagelist * ilist,
00753 cpl_bivector * pos)
00754 {
00755 cpl_table * tab;
00756 int nbima;
00757 double r, r1, r2, mag, dit;
00758 cpl_image * ima;
00759 double * pos_x;
00760 double * pos_y;
00761 double bgd, fl, zp, peak, fwhm_x, fwhm_y;
00762 int i;
00763
00764
00765 if (ilist == NULL) return NULL;
00766 if (pos == NULL) return NULL;
00767
00768
00769 nbima = cpl_imagelist_get_size(ilist);
00770 mag = sofi_img_zpoint_config.magnitude;
00771 dit = sofi_img_zpoint_config.dit;
00772 r = sofi_img_zpoint_config.phot_star_radius;
00773 r1 = sofi_img_zpoint_config.phot_bg_r1;
00774 r2 = sofi_img_zpoint_config.phot_bg_r2;
00775
00776
00777 tab = cpl_table_new(nbima);
00778 cpl_table_new_column(tab, "POSX", CPL_TYPE_DOUBLE);
00779 cpl_table_new_column(tab, "POSY", CPL_TYPE_DOUBLE);
00780 cpl_table_new_column(tab, "ZPOINT", CPL_TYPE_DOUBLE);
00781 cpl_table_new_column(tab, "FLUX", CPL_TYPE_DOUBLE);
00782 cpl_table_new_column(tab, "PEAK", CPL_TYPE_DOUBLE);
00783 cpl_table_new_column(tab, "BGD", CPL_TYPE_DOUBLE);
00784 cpl_table_new_column(tab, "FWHMX", CPL_TYPE_DOUBLE);
00785 cpl_table_new_column(tab, "FWHMY", CPL_TYPE_DOUBLE);
00786
00787
00788 pos_x = cpl_bivector_get_x_data(pos);
00789 pos_y = cpl_bivector_get_y_data(pos);
00790 for (i=0; i<nbima; i++) {
00791
00792 ima = cpl_imagelist_get(ilist, i);
00793
00794
00795 bgd = irplib_strehl_ring_background(ima, (int)(pos_x[i]),
00796 (int)(pos_y[i]), (int)r1, (int)r2, IRPLIB_BG_METHOD_MEDIAN);
00797
00798 fl = irplib_strehl_disk_flux(ima,
00799 (int)(pos_x[i]), (int)(pos_y[i]), (int)r, bgd);
00800
00801 zp = mag + 2.5 * log10(fl) - 2.5 * log10(dit);
00802 cpl_msg_info(cpl_func, "Zero point nb %d: %g", i+1, zp);
00803
00804 peak = cpl_image_get_max_window(ima,
00805 (int)(pos_x[i]-5), (int)(pos_y[i]-5),
00806 (int)(pos_x[i]+5), (int)(pos_y[i]+5));
00807
00808
00809 if (cpl_image_get_fwhm(ima, (int)(pos_x[i]), (int)(pos_y[i]),
00810 &fwhm_x, &fwhm_y) != CPL_ERROR_NONE) {
00811 cpl_msg_debug(cpl_func, "Cannot compute FWHM for image %d", i+1);
00812 cpl_error_reset();
00813 }
00814
00815
00816 cpl_table_set_double(tab, "POSX", i, pos_x[i]);
00817 cpl_table_set_double(tab, "POSY", i, pos_y[i]);
00818 cpl_table_set_double(tab, "ZPOINT", i, zp);
00819 cpl_table_set_double(tab, "FLUX", i, fl);
00820 cpl_table_set_double(tab, "PEAK", i, peak);
00821 cpl_table_set_double(tab, "BGD", i, bgd);
00822 cpl_table_set_double(tab, "FWHMX", i, fwhm_x);
00823 cpl_table_set_double(tab, "FWHMY", i, fwhm_y);
00824 }
00825 return tab;
00826 }
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842 static int sofi_img_zpoint_gradients(
00843 cpl_vector * flux,
00844 double * gradx,
00845 double * grady,
00846 double * graddx,
00847 double * graddy)
00848 {
00849 int nflux;
00850 double two, three, four, five, mean;
00851 double * pflux;
00852
00853
00854 if (flux == NULL) return -1;
00855
00856
00857 nflux = cpl_vector_get_size(flux);
00858 if (nflux != 8) {
00859 *gradx = -1.0;
00860 *grady = -1.0;
00861 *graddx = -1.0;
00862 *graddy = -1.0;
00863 return 0;
00864 }
00865 mean = cpl_vector_get_mean(flux);
00866
00867
00868 pflux = cpl_vector_get_data(flux);
00869 two = (pflux[1] + pflux[2]) / 2.0;
00870 three = (pflux[3] + pflux[4]) / 2.0;
00871 four = (pflux[5] + pflux[6]) / 2.0;
00872 five = pflux[7];
00873
00874 *gradx = (two + five - three - four) / 2;
00875 *grady = (three + two - four - five) / 2;
00876 *graddx = sqrt((two-five)*(two-five) + (three-four)*(three-four))/2.0;
00877 *graddy = sqrt((three-two)*(three-two) + (four-five)*(four-five))/2.0;
00878
00879 *gradx /= mean;
00880 *grady /= mean;
00881 *graddx /= mean;
00882 *graddy /= mean;
00883
00884 return 0;
00885 }
00886
00887
00893
00894 static cpl_bivector * sofi_img_zpoint_get_offsets(cpl_frameset * set)
00895 {
00896 int nframes;
00897 int noffsets;
00898 cpl_bivector * offs;
00899 cpl_frame * cur_frame;
00900 cpl_propertylist * plist;
00901 double off_x, off_y;
00902 int i;
00903
00904
00905 if (set == NULL) return NULL;
00906
00907
00908 nframes = cpl_frameset_get_size(set);
00909
00910 noffsets = 2 * (nframes-1);
00911
00912
00913 if (cpl_error_get_code()) return NULL;
00914
00915
00916 offs = cpl_bivector_new(noffsets);
00917
00918
00919 for (i=0; i<nframes; i++) {
00920 cur_frame = cpl_frameset_get_frame(set, i);
00921 plist=cpl_propertylist_load(cpl_frame_get_filename(cur_frame),0);
00922 off_x = sofi_pfits_get_cumoffsetx(plist);
00923 off_y = sofi_pfits_get_cumoffsety(plist);
00924 cpl_propertylist_delete(plist);
00925 if (i==0) {
00926 cpl_vector_set(cpl_bivector_get_x(offs), 2*i, off_x);
00927 cpl_vector_set(cpl_bivector_get_y(offs), 2*i, off_y);
00928 } else if (i==nframes-1) {
00929 cpl_vector_set(cpl_bivector_get_x(offs), 2*i-1, off_x);
00930 cpl_vector_set(cpl_bivector_get_y(offs), 2*i-1, off_y);
00931 } else {
00932 cpl_vector_set(cpl_bivector_get_x(offs), 2*i, off_x);
00933 cpl_vector_set(cpl_bivector_get_y(offs), 2*i, off_y);
00934 cpl_vector_set(cpl_bivector_get_x(offs), 2*i-1, off_x);
00935 cpl_vector_set(cpl_bivector_get_y(offs), 2*i-1, off_y);
00936 }
00937 }
00938
00939
00940 if (cpl_error_get_code()) {
00941 cpl_bivector_delete(offs);
00942 cpl_msg_error(cpl_func, "Cannot find offsets in headers");
00943 return NULL;
00944 }
00945 return offs;
00946 }
00947
00948
00949
00957
00958 static cpl_error_code sofi_img_zpoint_get_mag(const char * stdstars,
00959 double ra,
00960 double dec,
00961 sofi_band band)
00962 {
00963 cpl_errorstate prestate = cpl_errorstate_get();
00964
00965 bug_if(0);
00966 bug_if(stdstars == NULL);
00967
00968 switch (band) {
00969
00970 case SOFI_BAND_J:
00971 case SOFI_BAND_H:
00972 case SOFI_BAND_K:
00973 case SOFI_BAND_KS:
00974 if (!irplib_stdstar_find_star(stdstars, ra, dec,
00975 sofi_std_band_name(band),
00976 "LCO-Palomar.txt",
00977 &sofi_img_zpoint_config.magnitude,
00978 &sofi_img_zpoint_config.starname,
00979 &sofi_img_zpoint_config.sptype,
00980 &sofi_img_zpoint_config.catalog,
00981 NULL, NULL, 2.0) ||
00982 !irplib_stdstar_find_star(stdstars, ra, dec,
00983 sofi_std_band_name(band),
00984 "LCO-Palomar-NICMOS-Red-Stars.txt",
00985 &sofi_img_zpoint_config.magnitude,
00986 &sofi_img_zpoint_config.starname,
00987 &sofi_img_zpoint_config.sptype,
00988 &sofi_img_zpoint_config.catalog,
00989 NULL, NULL, 2.0) ||
00990 !irplib_stdstar_find_star(stdstars, ra, dec,
00991 sofi_std_band_name(band),
00992 "all",
00993 &sofi_img_zpoint_config.magnitude,
00994 &sofi_img_zpoint_config.starname,
00995 &sofi_img_zpoint_config.sptype,
00996 &sofi_img_zpoint_config.catalog,
00997 NULL, NULL, 2.0) ||
00998
00999 (band == SOFI_BAND_K &&
01000 !irplib_stdstar_find_star(stdstars, ra, dec,
01001 sofi_std_band_name(SOFI_BAND_KS),
01002 "all",
01003 &sofi_img_zpoint_config.magnitude,
01004 &sofi_img_zpoint_config.starname,
01005 &sofi_img_zpoint_config.sptype,
01006 &sofi_img_zpoint_config.catalog,
01007 NULL, NULL, 2.0)) ||
01008 (band == SOFI_BAND_KS &&
01009 !irplib_stdstar_find_star(stdstars, ra, dec,
01010 sofi_std_band_name(SOFI_BAND_K),
01011 "all",
01012 &sofi_img_zpoint_config.magnitude,
01013 &sofi_img_zpoint_config.starname,
01014 &sofi_img_zpoint_config.sptype,
01015 &sofi_img_zpoint_config.catalog,
01016 NULL, NULL, 2.0))) {
01017 cpl_errorstate_set(prestate);
01018 } else {
01019 skip_if(1);
01020 }
01021 break;
01022
01023 case SOFI_BAND_L:
01024 case SOFI_BAND_M:
01025 if (!irplib_stdstar_find_star(stdstars, ra, dec,
01026 sofi_std_band_name(band),
01027 "ESO-VanDerBliek.txt",
01028 &sofi_img_zpoint_config.magnitude,
01029 &sofi_img_zpoint_config.starname,
01030 &sofi_img_zpoint_config.sptype,
01031 &sofi_img_zpoint_config.catalog,
01032 NULL, NULL, 2.0) ||
01033 !irplib_stdstar_find_star(stdstars, ra, dec,
01034 sofi_std_band_name(band),
01035 "MSSSO-Photometric.txt",
01036 &sofi_img_zpoint_config.magnitude,
01037 &sofi_img_zpoint_config.starname,
01038 &sofi_img_zpoint_config.sptype,
01039 &sofi_img_zpoint_config.catalog,
01040 NULL, NULL, 2.0) ||
01041 !irplib_stdstar_find_star(stdstars, ra, dec,
01042 sofi_std_band_name(band),
01043 "MSSSO-Spectroscopic.txt",
01044 &sofi_img_zpoint_config.magnitude,
01045 &sofi_img_zpoint_config.starname,
01046 &sofi_img_zpoint_config.sptype,
01047 &sofi_img_zpoint_config.catalog,
01048 NULL, NULL, 2.0) ||
01049 !irplib_stdstar_find_star(stdstars, ra, dec,
01050 sofi_std_band_name(band), "all",
01051 &sofi_img_zpoint_config.magnitude,
01052 &sofi_img_zpoint_config.starname,
01053 &sofi_img_zpoint_config.sptype,
01054 &sofi_img_zpoint_config.catalog,
01055 NULL, NULL, 2.0)) {
01056 cpl_errorstate_set(prestate);
01057 } else {
01058 skip_if(1);
01059 }
01060 break;
01061 default:
01062 cpl_msg_error(cpl_func, "cannot determine associated filter");
01063 skip_if(1);
01064 }
01065
01066 end_skip;
01067
01068 return cpl_error_get_code();
01069 }
01070
01071
01081
01082 static int sofi_img_zpoint_save(
01083 cpl_table * tab,
01084 cpl_image * check_im,
01085 cpl_frameset * raw,
01086 const cpl_parameterlist * parlist,
01087 cpl_frameset * set)
01088 {
01089 cpl_propertylist * plist;
01090 cpl_propertylist * paflist;
01091 cpl_propertylist * qclist;
01092 const cpl_frame * ref_frame;
01093 const char * sval;
01094 double hum, airm;
01095
01096
01097 qclist = cpl_propertylist_new();
01098
01099
01100 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
01101 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
01102 0)) == NULL) {
01103 cpl_msg_error(cpl_func, "getting header from reference frame");
01104 cpl_propertylist_delete(qclist);
01105 return -1;
01106 }
01107
01108 if (cpl_error_get_code()) {
01109 cpl_propertylist_delete(qclist);
01110 cpl_propertylist_delete(plist);
01111 return -1;
01112 }
01113 sval = sofi_pfits_get_filter(plist);
01114 if (cpl_error_get_code()) cpl_error_reset();
01115 else cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS", sval);
01116 cpl_propertylist_append_string(qclist, "ESO QC FILTER REF",
01117 sofi_std_band_name(sofi_img_zpoint_config.band));
01118 if (sofi_img_zpoint_compute_keywords(raw, &hum, &airm) != -1) {
01119 cpl_propertylist_append_double(qclist, "ESO QC AMBI RHUM AVG", hum);
01120 cpl_propertylist_append_double(qclist, "ESO QC AIRMASS", airm);
01121 }
01122 cpl_propertylist_delete(plist);
01123 cpl_propertylist_append_double(qclist, "ESO QC ZPOINT",
01124 sofi_img_zpoint_config.zpoint);
01125 cpl_propertylist_append_double(qclist, "ESO QC ZPOINTRMS",
01126 sofi_img_zpoint_config.zpointrms);
01127 cpl_propertylist_append_double(qclist, "ESO QC FLUX MED",
01128 sofi_img_zpoint_config.flux_med);
01129 cpl_propertylist_append_string(qclist, "ESO QC STDNAME",
01130 sofi_img_zpoint_config.starname);
01131 cpl_propertylist_append_string(qclist, "ESO QC SPECTYPE",
01132 sofi_img_zpoint_config.sptype);
01133 cpl_propertylist_append_double(qclist, "ESO QC STARMAG",
01134 sofi_img_zpoint_config.magnitude);
01135 cpl_propertylist_append_string(qclist, "ESO QC CATNAME",
01136 sofi_img_zpoint_config.catalog);
01137 cpl_propertylist_append_double(qclist, "ESO QC GRADX",
01138 sofi_img_zpoint_config.gradx);
01139 cpl_propertylist_append_double(qclist, "ESO QC GRADY",
01140 sofi_img_zpoint_config.grady);
01141 cpl_propertylist_append_double(qclist, "ESO QC GRADDX",
01142 sofi_img_zpoint_config.graddx);
01143 cpl_propertylist_append_double(qclist, "ESO QC GRADDY",
01144 sofi_img_zpoint_config.graddy);
01145 cpl_propertylist_append_double(qclist, "ESO QC FWHM MEAN",
01146 sofi_img_zpoint_config.fwhm_mean);
01147
01148
01149 irplib_dfs_save_table(set,
01150 parlist,
01151 set,
01152 tab,
01153 NULL,
01154 "sofi_img_zpoint",
01155 SOFI_IMG_ZPOINT_TAB,
01156 qclist,
01157 NULL,
01158 PACKAGE "/" PACKAGE_VERSION,
01159 "sofi_img_zpoint.fits");
01160
01161
01162 if (check_im) {
01163 irplib_dfs_save_image(set,
01164 parlist,
01165 set,
01166 check_im,
01167 CPL_BPP_IEEE_FLOAT,
01168 "sofi_img_zpoint",
01169 SOFI_IMG_ZPOINT_CHECK,
01170 qclist,
01171 NULL,
01172 PACKAGE "/" PACKAGE_VERSION,
01173 "sofi_img_zpoint_check.fits");
01174 }
01175
01176
01177 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW);
01178
01179
01180 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(ref_frame),
01181 0)) == NULL) {
01182 cpl_msg_error(cpl_func, "getting header from reference frame");
01183 cpl_propertylist_delete(qclist);
01184 return -1;
01185 }
01186
01187
01188 paflist = cpl_propertylist_new();
01189 cpl_propertylist_copy_property_regexp(paflist, plist,
01190 "^(ARCFILE|ESO TPL ID|DATE-OBS|MJD-OBS|ESO TEL AIRM START|ESO INS MODE|"
01191 "ESO OBS ID|ESO DET DIT|ESO INS PIXSCALE|RA|DEC)$", 0);
01192 cpl_propertylist_delete(plist);
01193
01194
01195 cpl_propertylist_copy_property_regexp(paflist, qclist, "ESO QC", 0);
01196 cpl_propertylist_delete(qclist);
01197
01198
01199 cpl_dfs_save_paf("SOFI",
01200 "sofi_img_zpoint",
01201 paflist,
01202 "sofi_img_zpoint.paf");
01203 cpl_propertylist_delete(paflist);
01204 return 0;
01205 }
01206
01207
01213
01214 static int sofi_img_zpoint_compute_keywords(
01215 cpl_frameset * set,
01216 double * hum,
01217 double * airm)
01218 {
01219 int nframes;
01220 cpl_vector * hum_vec;
01221 cpl_frame * cur_frame;
01222 cpl_propertylist * plist;
01223 int i;
01224
01225
01226 if (set == NULL) return -1;
01227
01228
01229 nframes = cpl_frameset_get_size(set);
01230 *airm = 0.0;
01231
01232 hum_vec = cpl_vector_new(nframes);
01233
01234 for (i=0; i<nframes; i++) {
01235 if (cpl_error_get_code()) {
01236 cpl_vector_delete(hum_vec);
01237 return -1;
01238 }
01239 cur_frame = cpl_frameset_get_frame(set, i);
01240 plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
01241 if (i==0) *airm += sofi_pfits_get_airmass_start(plist);
01242 if (i==nframes-1) *airm += sofi_pfits_get_airmass_end(plist);
01243 cpl_vector_set(hum_vec, i, sofi_pfits_get_humidity_level(plist));
01244 cpl_propertylist_delete(plist);
01245 if (cpl_error_get_code()) {
01246 cpl_vector_delete(hum_vec);
01247 cpl_error_reset();
01248 return -1;
01249 }
01250 }
01251 *hum = cpl_vector_get_mean(hum_vec);
01252 *airm /= 2;
01253
01254
01255 cpl_vector_delete(hum_vec);
01256 if (cpl_error_get_code()) return -1;
01257 return 0;
01258 }
01259
01260