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 irplib_ensure(!naco_img_strehl_save(framelist, parlist, qclist,
00213 paflist, img, framepair, i+1),
00214 CPL_ERROR_FILE_IO,
00215 "Could not save the products of pair %d", i+1);
00216
00217 cpl_image_delete(img);
00218 img = NULL;
00219 cpl_propertylist_empty(qclist);
00220 cpl_propertylist_empty(paflist);
00221
00222 nb_good++;
00223 }
00224
00225
00226 irplib_framelist_empty(framepair);
00227
00228 bug_if(irplib_framelist_get_size(framepair) != 0);
00229
00230 }
00231
00232 bug_if(irplib_framelist_get_size(rawframes) != 0);
00233
00234 irplib_ensure(nb_good > 0, CPL_ERROR_DATA_NOT_FOUND,
00235 "None of the %d pairs of frames could be reduced", npairs);
00236
00237 end_skip;
00238
00239 cpl_image_delete(img);
00240 irplib_framelist_delete(allframes);
00241 irplib_framelist_delete(rawframes);
00242 irplib_framelist_delete(framepair);
00243 cpl_propertylist_delete(qclist);
00244 cpl_propertylist_delete(paflist);
00245
00246 return cpl_error_get_code();
00247 }
00248
00249
00256
00257 static cpl_image * naco_img_strehl_reduce(const cpl_parameterlist * parlist,
00258 const irplib_framelist * framepair)
00259 {
00260
00261 const cpl_frame * frame1 = irplib_framelist_get_const(framepair, 0);
00262 const cpl_frame * frame2 = irplib_framelist_get_const(framepair, 1);
00263 const cpl_propertylist * plist1
00264 = irplib_framelist_get_propertylist_const(framepair, 0);
00265 const cpl_propertylist * plist2
00266 = irplib_framelist_get_propertylist_const(framepair, 1);
00267 const char * sval1;
00268 const char * sval2;
00269 cpl_image * im1 = NULL;
00270 cpl_image * im2 = NULL;
00271
00272
00273 double psigmas[] = {5.0, 2.0, 1.0, 0.5};
00274 const int nsigmas = (int)(sizeof(psigmas)/sizeof(double));
00275 int isigma;
00276 cpl_vector * sigmas = NULL;
00277 cpl_apertures * apert = NULL;
00278 double dval;
00279 double pixscale;
00280 double lam, dlam;
00281 int iflux;
00282 int ndit;
00283
00284
00285 bug_if(cpl_error_get_code());
00286
00287 naco_img_strehl_config.pscale = -1.0;
00288 naco_img_strehl_config.pos_x = -1.0;
00289 naco_img_strehl_config.pos_y = -1.0;
00290 naco_img_strehl_config.strehl = -1.0;
00291 naco_img_strehl_config.strehl_err = -1.0;
00292 naco_img_strehl_config.star_bg = -1.0;
00293 naco_img_strehl_config.star_peak = -1.0;
00294 naco_img_strehl_config.star_flux = -1.0;
00295 naco_img_strehl_config.psf_peak = -1.0;
00296 naco_img_strehl_config.psf_flux = -1.0;
00297 naco_img_strehl_config.bg_noise = -1.0;
00298
00299
00300 irplib_check(sval1 = naco_pfits_get_filter(plist1);
00301 sval2 = naco_pfits_get_filter(plist2);
00302 pixscale = naco_pfits_get_pixscale(plist1);
00303 dval = naco_pfits_get_pixscale(plist2),
00304 "Could not load the filter names and Pixel-scales");
00305
00306 irplib_ensure(!strcmp(sval1, sval2), CPL_ERROR_INCOMPATIBLE_INPUT,
00307 "The filters in the pair are different: %s <=> %s", sval1, sval2);
00308
00309 irplib_ensure(fabs(pixscale-dval) <= 1e-3, CPL_ERROR_INCOMPATIBLE_INPUT,
00310 "The pixel-scales in the pair are different: %g <=> %g",
00311 pixscale, dval);
00312
00313 naco_img_strehl_config.pscale = pixscale;
00314 naco_img_strehl_config.filter = sval1;
00315
00316 cpl_msg_info(cpl_func, "Filter: [%s]", naco_img_strehl_config.filter);
00317 cpl_msg_info(cpl_func, "Pixel scale: [%g]", naco_img_strehl_config.pscale);
00318
00319
00320 cpl_msg_info(cpl_func, "---> Loading the pair of input images");
00321 im1 = cpl_image_load(cpl_frame_get_filename(frame1), CPL_TYPE_FLOAT, 0, 0);
00322 skip_if(0);
00323 im2 = cpl_image_load(cpl_frame_get_filename(frame2), CPL_TYPE_FLOAT, 0, 0);
00324 skip_if(0);
00325
00326
00327 cpl_msg_info(cpl_func, "---> Subtracting input images");
00328 skip_if(cpl_image_subtract(im1, im2));
00329 cpl_image_delete(im2);
00330 im2 = NULL;
00331
00332
00333 cpl_msg_info(cpl_func, "---> Detecting a bright star using %d sigma-levels "
00334 "ranging from %g down to %g", nsigmas, psigmas[0],
00335 psigmas[nsigmas-1]);
00336 sigmas = cpl_vector_wrap(nsigmas, psigmas);
00337 irplib_check(apert = cpl_apertures_extract_window(im1, sigmas,
00338 (int)(cpl_image_get_size_x(im1)-STREHL_DEF_LOCATE_SX)/2,
00339 (int)(cpl_image_get_size_y(im1)-STREHL_DEF_LOCATE_SY)/2,
00340 (int)(cpl_image_get_size_x(im1)+STREHL_DEF_LOCATE_SX)/2,
00341 (int)(cpl_image_get_size_y(im1)+STREHL_DEF_LOCATE_SY)/2,
00342 &isigma),
00343 "Could not detect the star");
00344
00345 skip_if(irplib_apertures_find_max_flux(apert, &iflux, 1));
00346
00347 naco_img_strehl_config.pos_x = cpl_apertures_get_centroid_x(apert, iflux);
00348 naco_img_strehl_config.pos_y = cpl_apertures_get_centroid_y(apert, iflux);
00349 cpl_apertures_delete(apert);
00350 apert = NULL;
00351 cpl_msg_info(cpl_func, "Star detected at sigma=%g, at position %g %g",
00352 psigmas[isigma],
00353 naco_img_strehl_config.pos_x,
00354 naco_img_strehl_config.pos_y);
00355 skip_if(0);
00356
00357
00358 irplib_check(naco_get_filter_infos(naco_img_strehl_config.filter, &lam, &dlam),
00359 "Cannot get filter infos [%s]", naco_img_strehl_config.filter);
00360
00361
00362 cpl_msg_info(cpl_func, "---> Computing the strehl ratio");
00363 irplib_check(irplib_strehl_compute(im1,
00364 STREHL_M1, STREHL_M2, lam, dlam,
00365 naco_img_strehl_config.pscale,
00366 STREHL_BOX_SIZE,
00367 naco_img_strehl_config.pos_x,
00368 naco_img_strehl_config.pos_y,
00369 naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_STAR_R),
00370 naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_BG_RINT),
00371 naco_parameterlist_get_double(parlist, RECIPE_STRING, NACO_PARAM_BG_REXT),
00372 -1, -1,
00373 &(naco_img_strehl_config.strehl),
00374 &(naco_img_strehl_config.strehl_err),
00375 &(naco_img_strehl_config.star_bg),
00376 &(naco_img_strehl_config.star_peak),
00377 &(naco_img_strehl_config.star_flux),
00378 &(naco_img_strehl_config.psf_peak),
00379 &(naco_img_strehl_config.psf_flux),
00380 &(naco_img_strehl_config.bg_noise)),
00381 "Could not compute the Strehl ratio");
00382
00383
00384 ndit = naco_pfits_get_ndit(plist1);
00385 skip_if(cpl_error_get_code());
00386
00387 cpl_msg_info(cpl_func, "Strehl: %g", naco_img_strehl_config.strehl);
00388 cpl_msg_info(cpl_func, "Strehl error: %g", naco_img_strehl_config.strehl_err);
00389 cpl_msg_info(cpl_func, "Star bg: %g", naco_img_strehl_config.star_bg);
00390 cpl_msg_info(cpl_func, "Star peak: %g", naco_img_strehl_config.star_peak);
00391 cpl_msg_info(cpl_func, "Star flux: %g", naco_img_strehl_config.star_flux);
00392 cpl_msg_info(cpl_func, "PSF peak: %g", naco_img_strehl_config.psf_peak);
00393 cpl_msg_info(cpl_func, "PSF flux: %g", naco_img_strehl_config.psf_flux);
00394 cpl_msg_info(cpl_func, "Bg noise: %g", naco_img_strehl_config.bg_noise);
00395
00396 if (ndit*naco_img_strehl_config.star_flux < STREHL_MINIMUM_FLUX)
00397 cpl_msg_warning(cpl_func, "The Strehl may be unreliable, due to a too "
00398 "low star flux: %g < %g/%d",
00399 naco_img_strehl_config.star_flux,
00400 (double)STREHL_MINIMUM_FLUX, ndit);
00401
00402 if (naco_img_strehl_config.star_peak > STREHL_MAXIMUM_PEAK)
00403 cpl_msg_warning(cpl_func, "The Strehl may be unreliable, due to a too "
00404 "high star peak: %g > %g",
00405 naco_img_strehl_config.star_peak,
00406 (double)STREHL_MAXIMUM_PEAK);
00407
00408 end_skip;
00409
00410 cpl_image_delete(im2);
00411 cpl_apertures_delete(apert);
00412 cpl_vector_unwrap(sigmas);
00413
00414 if (cpl_error_get_code()) {
00415 cpl_image_delete(im1);
00416 im1 = NULL;
00417 }
00418
00419 return im1;
00420 }
00421
00422
00423
00431
00432 static cpl_error_code naco_img_strehl_qc(cpl_propertylist * qclist,
00433 cpl_propertylist * paflist,
00434 const irplib_framelist * framepair)
00435 {
00436
00437 cpl_errorstate cleanstate = cpl_errorstate_get();
00438 const cpl_propertylist * plist1
00439 = irplib_framelist_get_propertylist_const(framepair, 0);
00440 const cpl_propertylist * plist2
00441 = irplib_framelist_get_propertylist_const(framepair, 1);
00442 const char * optiname;
00443 double dval1, dval2;
00444 double mean;
00445 const double almost_zero = 1e-3;
00446
00447
00448
00449 bug_if (0);
00450
00451
00452 skip_if (cpl_propertylist_copy_property_regexp(paflist, plist1, "^("
00453 NACO_PFITS_REGEXP_STREHL_PAF
00454 ")$", 0));
00455
00456
00457 dval1 = naco_pfits_get_l0mean(plist1);
00458 dval2 = naco_pfits_get_l0mean(plist2);
00459 mean = 0.0;
00460 if (cpl_error_get_code())
00461 naco_error_reset("Could not get FITS key:");
00462 else {
00463 if (fabs(dval1) <= almost_zero) mean = dval2;
00464 else if (fabs(dval2) <= almost_zero) mean = dval1;
00465 else mean = (dval1+dval2)/2.0;
00466 }
00467 skip_if(cpl_propertylist_append_double(paflist,
00468 "ESO AOS RTC DET DST L0MEAN", mean));
00469
00470
00471 dval1 = naco_pfits_get_t0mean(plist1);
00472 dval2 = naco_pfits_get_t0mean(plist2);
00473 mean = 0.0;
00474 if (cpl_error_get_code())
00475 naco_error_reset("Could not get FITS key:");
00476 else {
00477 if (fabs(dval1) <= almost_zero) mean = dval2;
00478 else if (fabs(dval2) <= almost_zero) mean = dval1;
00479 else mean = (dval1+dval2)/2.0;
00480 }
00481 skip_if(cpl_propertylist_append_double(paflist, "ESO AOS RTC DET DST T0MEAN",
00482 mean));
00483
00484
00485 dval1 = naco_pfits_get_r0mean(plist1);
00486 dval2 = naco_pfits_get_r0mean(plist2);
00487 mean = 0.0;
00488 if (cpl_error_get_code())
00489 naco_error_reset("Could not get FITS key:");
00490 else {
00491 if (fabs(dval1) <= almost_zero) mean = dval2;
00492 else if (fabs(dval2) <= almost_zero) mean = dval1;
00493 else mean = (dval1+dval2)/2.0;
00494 }
00495 skip_if(cpl_propertylist_append_double(paflist, "ESO AOS RTC DET DST R0MEAN",
00496 mean));
00497
00498
00499 dval1 = naco_pfits_get_ecmean(plist1);
00500 dval2 = naco_pfits_get_ecmean(plist2);
00501 mean = 0.0;
00502 if (cpl_error_get_code())
00503 naco_error_reset("Could not get FITS key:");
00504 else {
00505 if (fabs(dval1) <= almost_zero) mean = dval2;
00506 else if (fabs(dval2) <= almost_zero) mean = dval1;
00507 else mean = (dval1+dval2)/2.0;
00508 }
00509 skip_if(cpl_propertylist_append_double(paflist, "ESO AOS RTC DET DST ECMEAN",
00510 mean));
00511
00512
00513 dval1 = naco_pfits_get_fluxmean(plist1);
00514 dval2 = naco_pfits_get_fluxmean(plist2);
00515 mean = 0.0;
00516 if (cpl_error_get_code())
00517 naco_error_reset("Could not get FITS key:");
00518 else {
00519 if (fabs(dval1) <= almost_zero) mean = dval2;
00520 else if (fabs(dval2) <= almost_zero) mean = dval1;
00521 else mean = (dval1+dval2)/2.0;
00522 }
00523 skip_if(cpl_propertylist_append_double(paflist,"ESO AOS RTC DET DST FLUXMEAN",
00524 mean));
00525
00526
00527 skip_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER OBS",
00528 naco_img_strehl_config.filter));
00529
00530 optiname = naco_pfits_get_opti3_name(plist1);
00531 if (cpl_error_get_code())
00532 naco_error_reset("Could not get FITS key:");
00533 else
00534 skip_if(cpl_propertylist_append_string(qclist, "ESO QC FILTER NDENS",
00535 optiname));
00536 optiname = naco_pfits_get_opti4_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 POL",
00541 optiname));
00542
00543
00544 mean = 0.5*(naco_pfits_get_airmass_start(plist1) +
00545 naco_pfits_get_airmass_end(plist2));
00546 if (cpl_error_get_code()) {
00547 mean = 0.0;
00548 naco_error_reset("Could not get FITS key:");
00549 }
00550 skip_if(cpl_propertylist_append_double(qclist, "ESO QC AIRMASS", mean));
00551
00552
00553 cpl_propertylist_append_double(qclist, "ESO QC STREHL",
00554 naco_img_strehl_config.strehl);
00555 cpl_propertylist_append_double(qclist, "ESO QC STREHL FLUX",
00556 naco_img_strehl_config.star_flux);
00557 cpl_propertylist_append_double(qclist, "ESO QC STREHL PEAK",
00558 naco_img_strehl_config.star_peak);
00559 cpl_propertylist_append_double(qclist, "ESO QC STREHL ERROR",
00560 naco_img_strehl_config.strehl_err);
00561 cpl_propertylist_append_double(qclist, "ESO QC STREHL RMS",
00562 naco_img_strehl_config.bg_noise);
00563 cpl_propertylist_append_double(qclist, "ESO QC STREHL POSX",
00564 naco_img_strehl_config.pos_x);
00565 cpl_propertylist_append_double(qclist, "ESO QC STREHL POSY",
00566 naco_img_strehl_config.pos_y);
00567
00568 bug_if(0);
00569
00570 bug_if (cpl_propertylist_append(paflist, qclist));
00571
00572 bug_if (cpl_propertylist_copy_property_regexp(qclist, plist1, "^("
00573 IRPLIB_PFITS_REGEXP_RECAL
00574 ")$", 0));
00575
00576 bug_if (irplib_pfits_set_airmass(qclist, framepair));
00577
00578 end_skip;
00579
00580 return cpl_error_get_code();
00581 }
00582
00583
00595
00596 static cpl_error_code naco_img_strehl_save(cpl_frameset * set_tot,
00597 const cpl_parameterlist * parlist,
00598 const cpl_propertylist * qclist,
00599 const cpl_propertylist * paflist,
00600 const cpl_image * ima,
00601 const irplib_framelist * framepair,
00602 int pair_id)
00603 {
00604 cpl_frameset * rawframes = irplib_frameset_cast(framepair);
00605 char * filename = NULL;
00606
00607
00608 bug_if(0);
00609
00610
00611 filename = cpl_sprintf(RECIPE_STRING "%02d" CPL_DFS_FITS, pair_id);
00612 skip_if (irplib_dfs_save_image(set_tot, parlist, rawframes, ima,
00613 CPL_BPP_IEEE_FLOAT, RECIPE_STRING,
00614 naco_img_strehl_config.mode == 1
00615 ? NACO_IMG_STREHL_RESCAL
00616 : NACO_IMG_STREHL_RESTECH, qclist, NULL,
00617 naco_pipe_id, filename));
00618
00619
00620 cpl_free(filename);
00621 filename = cpl_sprintf(RECIPE_STRING "%02d" CPL_DFS_PAF, pair_id);
00622 skip_if (cpl_dfs_save_paf("NACO", RECIPE_STRING, paflist, filename));
00623
00624 end_skip;
00625
00626 cpl_free(filename);
00627 cpl_frameset_delete(rawframes);
00628
00629 return cpl_error_get_code();
00630 }