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 #include <string.h>
00036
00037 #include "naco_recipe.h"
00038 #include "irplib_strehl.h"
00039
00040
00041
00042
00043
00044 #define STREHL_MINIMUM_FLUX 100000
00045 #define STREHL_MAXIMUM_PEAK 4000
00046 #define STREHL_DEF_LOCATE_SX 512
00047 #define STREHL_DEF_LOCATE_SY 512
00048
00049 #define RECIPE_STRING "naco_img_strehl"
00050
00051
00052
00053
00054
00055 static cpl_image * naco_img_strehl_reduce(const cpl_parameterlist *,
00056 const irplib_framelist *);
00057
00058 static cpl_error_code naco_img_strehl_qc(cpl_propertylist *,
00059 cpl_propertylist *,
00060 const irplib_framelist *);
00061
00062 static cpl_error_code naco_img_strehl_save(cpl_frameset *,
00063 const cpl_parameterlist *,
00064 const cpl_propertylist *,
00065 const cpl_propertylist *,
00066 const cpl_image *,
00067 const irplib_framelist *,
00068 int);
00069
00070 NACO_RECIPE_DEFINE(naco_img_strehl,
00071 NACO_PARAM_STAR_R |
00072 NACO_PARAM_BG_RINT |
00073 NACO_PARAM_BG_REXT,
00074 "Strehl computation recipe",
00075 RECIPE_STRING " -- NACO Strehl Ratio recipe.\n"
00076 "This recipe estimates the strehl ratio and its error "
00077 "bound.\nThe Set Of Frames (sof-file) must specify at "
00078 "least one pair of files and they must be tagged either\n"
00079 "NACO-raw-file.fits " NACO_IMG_STREHL_CAL " or\n"
00080 "NACO-raw-file.fits " NACO_IMG_STREHL_TECH "\n");
00081
00082
00083
00084
00085
00086 static struct {
00087
00088 int mode;
00089
00090 double pscale;
00091 const char * filter;
00092 double pos_x;
00093 double pos_y;
00094 double strehl;
00095 double strehl_err;
00096 double star_bg;
00097 double star_peak;
00098 double star_flux;
00099 double psf_peak;
00100 double psf_flux;
00101 double bg_noise;
00102 } naco_img_strehl_config;
00103
00104
00108
00109
00110
00111
00112
00113
00114
00115
00122
00123 static int naco_img_strehl(cpl_frameset * framelist,
00124 const cpl_parameterlist * parlist)
00125 {
00126 cpl_errorstate cleanstate = cpl_errorstate_get();
00127 irplib_framelist * allframes = NULL;
00128 irplib_framelist * rawframes = NULL;
00129 irplib_framelist * framepair = NULL;
00130 cpl_frame * frame1 = NULL;
00131 cpl_frame * frame2 = NULL;
00132 cpl_image * img = NULL;
00133 cpl_propertylist * qclist = cpl_propertylist_new();
00134 cpl_propertylist * paflist = cpl_propertylist_new();
00135 int nframes, npairs;
00136 int nb_good = 0;
00137 int i;
00138
00139
00140 irplib_check(naco_dfs_set_groups(framelist),
00141 "Could not group the input frames");
00142
00143 allframes = irplib_framelist_cast(framelist);
00144 skip_if(allframes == NULL);
00145 irplib_check(rawframes
00146 = irplib_framelist_extract_regexp(allframes,
00147 "^(" NACO_IMG_STREHL_CAL
00148 "|" NACO_IMG_STREHL_TECH
00149 ")$", CPL_FALSE),
00150 "Could not load the raw frames");
00151 irplib_framelist_empty(allframes);
00152
00153 naco_img_strehl_config.mode = 0;
00154 if (cpl_frameset_find(framelist, NACO_IMG_STREHL_CAL))
00155 naco_img_strehl_config.mode = 1;
00156
00157 if (cpl_frameset_find(framelist, NACO_IMG_STREHL_TECH)) {
00158 irplib_ensure(!naco_img_strehl_config.mode, CPL_ERROR_INCOMPATIBLE_INPUT,
00159 "Mixing of DO categories " NACO_IMG_STREHL_CAL " and "
00160 NACO_IMG_STREHL_TECH "is not supported");
00161 naco_img_strehl_config.mode = 2;
00162 }
00163
00164
00165 nframes = irplib_framelist_get_size(rawframes);
00166 skip_if_lt(nframes, 2, "frames");
00167
00168 if (nframes % 2) {
00169 cpl_msg_warning(cpl_func, "The SOF comprises an odd number (%d) of raw "
00170 "frames, ignoring the last", nframes);
00171 skip_if(irplib_framelist_erase(rawframes, nframes - 1));
00172 }
00173 npairs = nframes/2;
00174
00175 framepair = irplib_framelist_new();
00176
00177
00178 for (i=0; i < npairs; i++) {
00179 cpl_error_code error;
00180
00181
00182 cpl_msg_info(cpl_func, "Reducing pair %d out of %d", i+1, npairs);
00183
00184 frame1 = irplib_framelist_unset(rawframes, 0, NULL);
00185 error = irplib_framelist_set(framepair, frame1, 0);
00186 bug_if(error);
00187
00188 skip_if(irplib_framelist_load_propertylist(framepair, 0, 0, "^("
00189 IRPLIB_PFITS_REGEXP_RECAL "|"
00190 NACO_PFITS_REGEXP_STREHL "|"
00191 NACO_PFITS_REGEXP_STREHL_PAF
00192 ")$", CPL_FALSE));
00193
00194 frame2 = irplib_framelist_unset(rawframes, 0, NULL);
00195 error = irplib_framelist_set(framepair, frame2, 1);
00196 bug_if(error);
00197
00198 skip_if(irplib_framelist_load_propertylist_all(framepair, 0, "^("
00199 NACO_PFITS_REGEXP_STREHL
00200 ")$", CPL_FALSE));
00201
00202 img = naco_img_strehl_reduce(parlist, framepair);
00203
00204 if (img == NULL) {
00205 if (npairs > 1)
00206 irplib_error_recover(cleanstate, "Could not reduce pair number "
00207 "%d:", i+1);
00208 } else {
00209
00210 skip_if(naco_img_strehl_qc(qclist, paflist, framepair));
00211
00212
00213 bug_if (cpl_propertylist_append_string(paflist, CPL_DFS_PRO_CATG,
00214 naco_img_strehl_config.mode
00215 == 1 ? NACO_IMG_STREHL_RESCAL
00216 : NACO_IMG_STREHL_RESTECH));
00217
00218 irplib_ensure(!naco_img_strehl_save(framelist, parlist, qclist,
00219 paflist, img, framepair, i+1),
00220 CPL_ERROR_FILE_IO,
00221 "Could not save the products of pair %d", i+1);
00222
00223 cpl_image_delete(img);
00224 img = NULL;
00225 cpl_propertylist_empty(qclist);
00226 cpl_propertylist_empty(paflist);
00227
00228 nb_good++;
00229 }
00230
00231
00232 irplib_framelist_empty(framepair);
00233
00234 bug_if(irplib_framelist_get_size(framepair) != 0);
00235
00236 }
00237
00238 bug_if(irplib_framelist_get_size(rawframes) != 0);
00239
00240 irplib_ensure(nb_good > 0, CPL_ERROR_DATA_NOT_FOUND,
00241 "None of the %d pairs of frames could be reduced", npairs);
00242
00243 end_skip;
00244
00245 cpl_image_delete(img);
00246 irplib_framelist_delete(allframes);
00247 irplib_framelist_delete(rawframes);
00248 irplib_framelist_delete(framepair);
00249 cpl_propertylist_delete(qclist);
00250 cpl_propertylist_delete(paflist);
00251
00252 return cpl_error_get_code();
00253 }
00254
00255
00262
00263 static cpl_image * naco_img_strehl_reduce(const cpl_parameterlist * parlist,
00264 const irplib_framelist * framepair)
00265 {
00266
00267 const cpl_frame * frame1 = irplib_framelist_get_const(framepair, 0);
00268 const cpl_frame * frame2 = irplib_framelist_get_const(framepair, 1);
00269 const cpl_propertylist * plist1
00270 = irplib_framelist_get_propertylist_const(framepair, 0);
00271 const cpl_propertylist * plist2
00272 = irplib_framelist_get_propertylist_const(framepair, 1);
00273 const char * sval1;
00274 const char * sval2;
00275 cpl_image * im1 = NULL;
00276 cpl_image * im2 = NULL;
00277
00278
00279 double psigmas[] = {5.0, 2.0, 1.0, 0.5};
00280 const int nsigmas = (int)(sizeof(psigmas)/sizeof(double));
00281 cpl_size isigma;
00282 cpl_vector * sigmas = NULL;
00283 cpl_apertures * apert = NULL;
00284 double dval;
00285 double pixscale;
00286 double lam, dlam;
00287 int iflux;
00288 int ndit;
00289
00290
00291 bug_if(cpl_error_get_code());
00292
00293 naco_img_strehl_config.pscale = -1.0;
00294 naco_img_strehl_config.pos_x = -1.0;
00295 naco_img_strehl_config.pos_y = -1.0;
00296 naco_img_strehl_config.strehl = -1.0;
00297 naco_img_strehl_config.strehl_err = -1.0;
00298 naco_img_strehl_config.star_bg = -1.0;
00299 naco_img_strehl_config.star_peak = -1.0;
00300 naco_img_strehl_config.star_flux = -1.0;
00301 naco_img_strehl_config.psf_peak = -1.0;
00302 naco_img_strehl_config.psf_flux = -1.0;
00303 naco_img_strehl_config.bg_noise = -1.0;
00304
00305
00306 irplib_check(sval1 = naco_pfits_get_filter(plist1);
00307 sval2 = naco_pfits_get_filter(plist2);
00308 pixscale = naco_pfits_get_pixscale(plist1);
00309 dval = naco_pfits_get_pixscale(plist2),
00310 "Could not load the filter names and Pixel-scales");
00311
00312 irplib_ensure(!strcmp(sval1, sval2), CPL_ERROR_INCOMPATIBLE_INPUT,
00313 "The filters in the pair are different: %s <=> %s", sval1, sval2);
00314
00315 irplib_ensure(fabs(pixscale-dval) <= 1e-3, CPL_ERROR_INCOMPATIBLE_INPUT,
00316 "The pixel-scales in the pair are different: %g <=> %g",
00317 pixscale, dval);
00318
00319 naco_img_strehl_config.pscale = pixscale;
00320 naco_img_strehl_config.filter = sval1;
00321
00322 cpl_msg_info(cpl_func, "Filter: [%s]", naco_img_strehl_config.filter);
00323 cpl_msg_info(cpl_func, "Pixel scale: [%g]", naco_img_strehl_config.pscale);
00324
00325
00326 cpl_msg_info(cpl_func, "---> Loading the pair of input images");
00327 im1 = cpl_image_load(cpl_frame_get_filename(frame1), CPL_TYPE_FLOAT, 0, 0);
00328 skip_if(0);
00329 im2 = cpl_image_load(cpl_frame_get_filename(frame2), CPL_TYPE_FLOAT, 0, 0);
00330 skip_if(0);
00331
00332
00333 cpl_msg_info(cpl_func, "---> Subtracting input images");
00334 skip_if(cpl_image_subtract(im1, im2));
00335 cpl_image_delete(im2);
00336 im2 = NULL;
00337
00338
00339 cpl_msg_info(cpl_func, "---> Detecting a bright star using %d sigma-levels "
00340 "ranging from %g down to %g", nsigmas, psigmas[0],
00341 psigmas[nsigmas-1]);
00342 sigmas = cpl_vector_wrap(nsigmas, psigmas);
00343 irplib_check(apert = cpl_apertures_extract_window(im1, sigmas,
00344 (int)(cpl_image_get_size_x(im1)-STREHL_DEF_LOCATE_SX)/2,
00345 (int)(cpl_image_get_size_y(im1)-STREHL_DEF_LOCATE_SY)/2,
00346 (int)(cpl_image_get_size_x(im1)+STREHL_DEF_LOCATE_SX)/2,
00347 (int)(cpl_image_get_size_y(im1)+STREHL_DEF_LOCATE_SY)/2,
00348 &isigma),
00349 "Could not detect the star");
00350
00351 skip_if(irplib_apertures_find_max_flux(apert, &iflux, 1));
00352
00353 naco_img_strehl_config.pos_x = cpl_apertures_get_centroid_x(apert, iflux);
00354 naco_img_strehl_config.pos_y = cpl_apertures_get_centroid_y(apert, iflux);
00355 cpl_apertures_delete(apert);
00356 apert = NULL;
00357 cpl_msg_info(cpl_func, "Star detected at sigma=%g, at position %g %g",
00358 psigmas[isigma],
00359 naco_img_strehl_config.pos_x,
00360 naco_img_strehl_config.pos_y);
00361 skip_if(0);
00362
00363
00364 irplib_check(naco_get_filter_infos(naco_img_strehl_config.filter, &lam, &dlam),
00365 "Cannot get filter infos [%s]", naco_img_strehl_config.filter);
00366
00367
00368 cpl_msg_info(cpl_func, "---> Computing the strehl ratio");
00369 irplib_check(irplib_strehl_compute(im1,
00370 STREHL_M1, STREHL_M2, lam, dlam,
00371 naco_img_strehl_config.pscale,
00372 STREHL_BOX_SIZE,
00373 naco_img_strehl_config.pos_x,
00374 naco_img_strehl_config.pos_y,
00375 naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_STAR_R),
00376 naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_BG_RINT),
00377 naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_BG_REXT),
00378 -1, -1,
00379 &(naco_img_strehl_config.strehl),
00380 &(naco_img_strehl_config.strehl_err),
00381 &(naco_img_strehl_config.star_bg),
00382 &(naco_img_strehl_config.star_peak),
00383 &(naco_img_strehl_config.star_flux),
00384 &(naco_img_strehl_config.psf_peak),
00385 &(naco_img_strehl_config.psf_flux),
00386 &(naco_img_strehl_config.bg_noise)),
00387 "Could not compute the Strehl ratio");
00388
00389
00390 ndit = naco_pfits_get_ndit(plist1);
00391 skip_if(cpl_error_get_code());
00392
00393 cpl_msg_info(cpl_func, "Strehl: %g", naco_img_strehl_config.strehl);
00394 cpl_msg_info(cpl_func, "Strehl error: %g", naco_img_strehl_config.strehl_err);
00395 cpl_msg_info(cpl_func, "Star bg: %g", naco_img_strehl_config.star_bg);
00396 cpl_msg_info(cpl_func, "Star peak: %g", naco_img_strehl_config.star_peak);
00397 cpl_msg_info(cpl_func, "Star flux: %g", naco_img_strehl_config.star_flux);
00398 cpl_msg_info(cpl_func, "PSF peak: %g", naco_img_strehl_config.psf_peak);
00399 cpl_msg_info(cpl_func, "PSF flux: %g", naco_img_strehl_config.psf_flux);
00400 cpl_msg_info(cpl_func, "Bg noise: %g", naco_img_strehl_config.bg_noise);
00401
00402 if (ndit*naco_img_strehl_config.star_flux < STREHL_MINIMUM_FLUX)
00403 cpl_msg_warning(cpl_func, "The Strehl may be unreliable, due to a too "
00404 "low star flux: %g < %g/%d",
00405 naco_img_strehl_config.star_flux,
00406 (double)STREHL_MINIMUM_FLUX, ndit);
00407
00408 if (naco_img_strehl_config.star_peak > STREHL_MAXIMUM_PEAK)
00409 cpl_msg_warning(cpl_func, "The Strehl may be unreliable, due to a too "
00410 "high star peak: %g > %g",
00411 naco_img_strehl_config.star_peak,
00412 (double)STREHL_MAXIMUM_PEAK);
00413
00414 end_skip;
00415
00416 cpl_image_delete(im2);
00417 cpl_apertures_delete(apert);
00418 cpl_vector_unwrap(sigmas);
00419
00420 if (cpl_error_get_code()) {
00421 cpl_image_delete(im1);
00422 im1 = NULL;
00423 }
00424
00425 return im1;
00426 }
00427
00428
00429
00437
00438 static cpl_error_code naco_img_strehl_qc(cpl_propertylist * qclist,
00439 cpl_propertylist * paflist,
00440 const irplib_framelist * framepair)
00441 {
00442
00443 cpl_errorstate cleanstate = cpl_errorstate_get();
00444 const cpl_propertylist * plist1
00445 = irplib_framelist_get_propertylist_const(framepair, 0);
00446 const cpl_propertylist * plist2
00447 = irplib_framelist_get_propertylist_const(framepair, 1);
00448 const char * optiname;
00449 double dval1, dval2;
00450 double mean;
00451 const double almost_zero = 1e-3;
00452
00453
00454
00455 bug_if (0);
00456
00457
00458 skip_if (cpl_propertylist_copy_property_regexp(paflist, plist1, "^("
00459 NACO_PFITS_REGEXP_STREHL_PAF
00460 ")$", 0));
00461
00462
00463 dval1 = naco_pfits_get_l0mean(plist1);
00464 dval2 = naco_pfits_get_l0mean(plist2);
00465 mean = 0.0;
00466 if (cpl_error_get_code())
00467 naco_error_reset("Could not get FITS key:");
00468 else {
00469 if (fabs(dval1) <= almost_zero) mean = dval2;
00470 else if (fabs(dval2) <= almost_zero) mean = dval1;
00471 else mean = (dval1+dval2)/2.0;
00472 }
00473 skip_if(cpl_propertylist_append_double(paflist,
00474 "ESO AOS RTC DET DST L0MEAN", mean));
00475
00476
00477 dval1 = naco_pfits_get_t0mean(plist1);
00478 dval2 = naco_pfits_get_t0mean(plist2);
00479 mean = 0.0;
00480 if (cpl_error_get_code())
00481 naco_error_reset("Could not get FITS key:");
00482 else {
00483 if (fabs(dval1) <= almost_zero) mean = dval2;
00484 else if (fabs(dval2) <= almost_zero) mean = dval1;
00485 else mean = (dval1+dval2)/2.0;
00486 }
00487 skip_if(cpl_propertylist_append_double(paflist, "ESO AOS RTC DET DST T0MEAN",
00488 mean));
00489
00490
00491 dval1 = naco_pfits_get_r0mean(plist1);
00492 dval2 = naco_pfits_get_r0mean(plist2);
00493 mean = 0.0;
00494 if (cpl_error_get_code())
00495 naco_error_reset("Could not get FITS key:");
00496 else {
00497 if (fabs(dval1) <= almost_zero) mean = dval2;
00498 else if (fabs(dval2) <= almost_zero) mean = dval1;
00499 else mean = (dval1+dval2)/2.0;
00500 }
00501 skip_if(cpl_propertylist_append_double(paflist, "ESO AOS RTC DET DST R0MEAN",
00502 mean));
00503
00504
00505 dval1 = naco_pfits_get_ecmean(plist1);
00506 dval2 = naco_pfits_get_ecmean(plist2);
00507 mean = 0.0;
00508 if (cpl_error_get_code())
00509 naco_error_reset("Could not get FITS key:");
00510 else {
00511 if (fabs(dval1) <= almost_zero) mean = dval2;
00512 else if (fabs(dval2) <= almost_zero) mean = dval1;
00513 else mean = (dval1+dval2)/2.0;
00514 }
00515 skip_if(cpl_propertylist_append_double(paflist, "ESO AOS RTC DET DST ECMEAN",
00516 mean));
00517
00518
00519 dval1 = naco_pfits_get_fluxmean(plist1);
00520 dval2 = naco_pfits_get_fluxmean(plist2);
00521 mean = 0.0;
00522 if (cpl_error_get_code())
00523 naco_error_reset("Could not get FITS key:");
00524 else {
00525 if (fabs(dval1) <= almost_zero) mean = dval2;
00526 else if (fabs(dval2) <= almost_zero) mean = dval1;
00527 else mean = (dval1+dval2)/2.0;
00528 }
00529 skip_if(cpl_propertylist_append_double(paflist,"ESO AOS RTC DET DST FLUXMEAN",
00530 mean));
00531
00532
00533 skip_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS",
00534 naco_img_strehl_config.filter));
00535
00536 optiname = naco_pfits_get_opti3_name(plist1);
00537 if (cpl_error_get_code())
00538 naco_error_reset("Could not get FITS key:");
00539 else
00540 skip_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER NDENS",
00541 optiname));
00542 optiname = naco_pfits_get_opti4_name(plist1);
00543 if (cpl_error_get_code())
00544 naco_error_reset("Could not get FITS key:");
00545 else
00546 skip_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER POL",
00547 optiname));
00548
00549
00550 mean = 0.5*(naco_pfits_get_airmass_start(plist1) +
00551 naco_pfits_get_airmass_end(plist2));
00552 if (cpl_error_get_code()) {
00553 mean = 0.0;
00554 naco_error_reset("Could not get FITS key:");
00555 }
00556 skip_if(cpl_propertylist_append_double(qclist, "ESO QC AIRMASS", mean));
00557
00558
00559 cpl_propertylist_append_double(qclist, "ESO QC STREHL",
00560 naco_img_strehl_config.strehl);
00561 cpl_propertylist_append_double(qclist, "ESO QC STREHL FLUX",
00562 naco_img_strehl_config.star_flux);
00563 cpl_propertylist_append_double(qclist, "ESO QC STREHL PEAK",
00564 naco_img_strehl_config.star_peak);
00565 cpl_propertylist_append_double(qclist, "ESO QC STREHL ERROR",
00566 naco_img_strehl_config.strehl_err);
00567 cpl_propertylist_append_double(qclist, "ESO QC STREHL RMS",
00568 naco_img_strehl_config.bg_noise);
00569 cpl_propertylist_append_double(qclist, "ESO QC STREHL POSX",
00570 naco_img_strehl_config.pos_x);
00571 cpl_propertylist_append_double(qclist, "ESO QC STREHL POSY",
00572 naco_img_strehl_config.pos_y);
00573
00574 bug_if(0);
00575
00576 bug_if (cpl_propertylist_append(paflist, qclist));
00577
00578 bug_if (cpl_propertylist_copy_property_regexp(qclist, plist1, "^("
00579 IRPLIB_PFITS_REGEXP_RECAL
00580 ")$", 0));
00581
00582 bug_if (irplib_pfits_set_airmass(qclist, framepair));
00583
00584 end_skip;
00585
00586 return cpl_error_get_code();
00587 }
00588
00589
00601
00602 static cpl_error_code naco_img_strehl_save(cpl_frameset * set_tot,
00603 const cpl_parameterlist * parlist,
00604 const cpl_propertylist * qclist,
00605 const cpl_propertylist * paflist,
00606 const cpl_image * ima,
00607 const irplib_framelist * framepair,
00608 int pair_id)
00609 {
00610 cpl_frameset * rawframes = irplib_frameset_cast(framepair);
00611 char * filename = NULL;
00612
00613
00614 bug_if(0);
00615
00616
00617 filename = cpl_sprintf(RECIPE_STRING "%02d" CPL_DFS_FITS, pair_id);
00618 skip_if (irplib_dfs_save_image(set_tot, parlist, rawframes, ima,
00619 CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
00620 naco_img_strehl_config.mode == 1
00621 ? NACO_IMG_STREHL_RESCAL
00622 : NACO_IMG_STREHL_RESTECH, qclist, NULL,
00623 naco_pipe_id, filename));
00624
00625
00626 cpl_free(filename);
00627 filename = cpl_sprintf(RECIPE_STRING "%02d" CPL_DFS_PAF, pair_id);
00628 skip_if (cpl_dfs_save_paf("NACO", RECIPE_STRING, paflist, filename));
00629
00630 end_skip;
00631
00632 cpl_free(filename);
00633 cpl_frameset_delete(rawframes);
00634
00635 return cpl_error_get_code();
00636 }