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 #include "crires_recipe.h"
00037
00038 #include "crires_combine.h"
00039
00040
00041
00042
00043
00044 #define RECIPE_STRING "crires_win_flat"
00045
00046
00047
00048
00049
00050 static cpl_imagelist * crires_win_flat_reduce(cpl_frameset *, const char *) ;
00051 static cpl_imagelist * crires_win_flat_bpm(cpl_imagelist *, double,
00052 double, double) ;
00053 static int crires_win_flat_save(const cpl_imagelist *, const cpl_imagelist *,
00054 int, cpl_frameset *, const cpl_parameterlist *, cpl_frameset *) ;
00055 static int crires_win_flat_compare(const cpl_frame *, const cpl_frame *) ;
00056
00057 static char crires_win_flat_description[] =
00058 "crires_win_flat -- Flat-field recipe in Windowing mode\n"
00059 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
00060 "raw-file.fits "CRIRES_WIN_FLAT_RAW" or\n"
00061 "dark-file.fits "CRIRES_CALPRO_DARK".\n" ;
00062
00063 CRIRES_RECIPE_DEFINE(crires_win_flat,
00064 CRIRES_PARAM_THRESHOLDS |
00065 CRIRES_PARAM_BPM_RATE |
00066 CRIRES_PARAM_REPLACE |
00067 CRIRES_PARAM_KAPPA_SIGCLIP |
00068 CRIRES_PARAM_COLLAPSE_METH,
00069 "Flatfield recipe in Windowing mode",
00070 crires_win_flat_description) ;
00071
00072
00073
00074
00075
00076 static struct {
00077
00078 double bpm_low ;
00079 double bpm_high ;
00080 double bpm_lines_ratio ;
00081 int replace_flag ;
00082 double kappa_sigclip ;
00083 crires_collapse_method coll_meth ;
00084
00085 int bpm_nb[CRIRES_NB_DETECTORS] ;
00086 double flat_mean[CRIRES_NB_DETECTORS] ;
00087 double flat_stdev[CRIRES_NB_DETECTORS] ;
00088 double flat_flux[CRIRES_NB_DETECTORS] ;
00089 double flat_master_rms[CRIRES_NB_DETECTORS] ;
00090 } crires_win_flat_config ;
00091
00092
00093
00094
00095
00096
00103
00104 static int crires_win_flat(
00105 cpl_frameset * frameset,
00106 const cpl_parameterlist * parlist)
00107 {
00108 const char * sval ;
00109 int * labels ;
00110 int nlabels ;
00111 cpl_frameset * rawframes ;
00112 const char * dark ;
00113 cpl_imagelist * flat ;
00114 cpl_frameset * flat_one ;
00115 cpl_imagelist * bpm ;
00116 int i ;
00117
00118
00119 rawframes = NULL ;
00120 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00121 crires_win_flat_config.bpm_nb[i] = -1 ;
00122 crires_win_flat_config.flat_mean[i] = -1.0 ;
00123 crires_win_flat_config.flat_stdev[i] = -1.0 ;
00124 crires_win_flat_config.flat_flux[i] = -1.0 ;
00125 crires_win_flat_config.flat_master_rms[i] = -1.0 ;
00126 }
00127
00128
00129 sval = crires_parameterlist_get_string(parlist, RECIPE_STRING,
00130 CRIRES_PARAM_THRESHOLDS) ;
00131 if (sscanf(sval, "%lg,%lg",
00132 &crires_win_flat_config.bpm_low,
00133 &crires_win_flat_config.bpm_high)!=2) {
00134 return -1 ;
00135 }
00136 crires_win_flat_config.replace_flag = crires_parameterlist_get_bool(
00137 parlist, RECIPE_STRING, CRIRES_PARAM_REPLACE) ;
00138 crires_win_flat_config.bpm_lines_ratio = crires_parameterlist_get_double(
00139 parlist, RECIPE_STRING, CRIRES_PARAM_BPM_RATE) ;
00140 crires_win_flat_config.kappa_sigclip = crires_parameterlist_get_double(
00141 parlist, RECIPE_STRING, CRIRES_PARAM_KAPPA_SIGCLIP) ;
00142 sval = crires_parameterlist_get_string(parlist, RECIPE_STRING,
00143 CRIRES_PARAM_COLLAPSE_METH) ;
00144 if (!strcmp(sval, "avg"))
00145 crires_win_flat_config.coll_meth = CRIRES_COLLAPSE_AVG ;
00146 else if (!strcmp(sval, "med"))
00147 crires_win_flat_config.coll_meth = CRIRES_COLLAPSE_MED ;
00148 else if (!strcmp(sval, "sig"))
00149 crires_win_flat_config.coll_meth = CRIRES_COLLAPSE_SIG ;
00150 else {
00151 cpl_msg_error(__func__, "Invalid collapse method specified");
00152 return -1;
00153 }
00154
00155
00156 if (crires_dfs_set_groups(frameset, "crires_win_flat")) {
00157 cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
00158 return -1 ;
00159 }
00160
00161
00162 dark = crires_extract_filename(frameset, CRIRES_CALPRO_DARK_WIN) ;
00163
00164
00165 if ((rawframes = crires_extract_frameset(frameset,
00166 CRIRES_WIN_FLAT_RAW)) == NULL) {
00167 cpl_msg_error(__func__, "No raw frame in input") ;
00168 return -1 ;
00169 }
00170
00171
00172 if ((labels = cpl_frameset_labelise(rawframes, crires_win_flat_compare,
00173 &nlabels)) == NULL) {
00174 cpl_msg_error(__func__, "Cannot labelise input frames") ;
00175 cpl_frameset_delete(rawframes) ;
00176 return -1 ;
00177 }
00178
00179
00180 for (i=0 ; i<nlabels ; i++) {
00181
00182 cpl_msg_info(__func__, "Reduce data set %d / %d", i+1, nlabels);
00183 cpl_msg_indent_more() ;
00184 flat_one = cpl_frameset_extract(rawframes, labels, i) ;
00185 flat = crires_win_flat_reduce(flat_one, dark) ;
00186 cpl_msg_indent_less() ;
00187
00188
00189 cpl_msg_info(__func__, "Save the products") ;
00190 cpl_msg_indent_more() ;
00191 if (flat == NULL) {
00192 cpl_msg_warning(__func__, "Cannot reduce set nb %d", i+1) ;
00193 } else {
00194 if ((bpm = crires_win_flat_bpm(flat,
00195 crires_win_flat_config.bpm_low,
00196 crires_win_flat_config.bpm_high,
00197 crires_win_flat_config.bpm_lines_ratio)) == NULL) {
00198 cpl_msg_warning(__func__, "Cannot create bad pixels map") ;
00199 }
00200 crires_win_flat_save(flat, bpm, i+1, flat_one, parlist, frameset) ;
00201 cpl_imagelist_delete(flat) ;
00202 cpl_imagelist_delete(bpm) ;
00203 }
00204 cpl_msg_indent_less() ;
00205 cpl_frameset_delete(flat_one) ;
00206 }
00207 cpl_frameset_delete(rawframes) ;
00208 cpl_free(labels) ;
00209
00210
00211 if (cpl_error_get_code()) return -1 ;
00212 else return 0 ;
00213 }
00214
00215
00222
00223 static cpl_imagelist * crires_win_flat_reduce(
00224 cpl_frameset * flatframes,
00225 const char * dark)
00226 {
00227 cpl_propertylist * plist ;
00228 cpl_frame * ref_frame ;
00229 const char * fname ;
00230 int nframes ;
00231 double dit_frame, dit_dark ;
00232 cpl_imagelist * in ;
00233 cpl_imagelist * out ;
00234 cpl_vector * medians ;
00235 double median ;
00236 cpl_image * ima ;
00237 int i, j ;
00238
00239
00240 if (flatframes == NULL) return NULL ;
00241
00242
00243 nframes = cpl_frameset_get_size(flatframes) ;
00244
00245
00246 ref_frame = cpl_frameset_get_frame(flatframes, 0) ;
00247 fname = cpl_frame_get_filename(ref_frame) ;
00248 if ((plist=cpl_propertylist_load(fname, 0)) == NULL) {
00249 cpl_msg_error(__func__, "Getting header from RAW file");
00250 cpl_msg_indent_less() ;
00251 return NULL ;
00252 }
00253 dit_frame = crires_pfits_get_dit(plist) ;
00254 cpl_propertylist_delete(plist) ;
00255 if (cpl_error_get_code()) {
00256 cpl_msg_error(__func__, "Cannot get the DIT from RAW file") ;
00257 cpl_msg_indent_less() ;
00258 return NULL ;
00259 }
00260 cpl_msg_info(__func__, "DIT value: %g sec.", dit_frame) ;
00261
00262
00263 if (dark != NULL) {
00264 cpl_msg_info(__func__, "Verify the dark DIT") ;
00265 cpl_msg_indent_more() ;
00266 if ((plist=cpl_propertylist_load(dark, 0)) == NULL) {
00267 cpl_msg_error(__func__, "Getting header from DARK");
00268 cpl_msg_indent_less() ;
00269 return NULL ;
00270 }
00271 dit_dark = crires_pfits_get_dit(plist) ;
00272 cpl_propertylist_delete(plist) ;
00273 if (cpl_error_get_code()) {
00274 cpl_msg_error(__func__, "Cannot get the DIT from DARK") ;
00275 cpl_msg_indent_less() ;
00276 return NULL ;
00277 }
00278 if (fabs(dit_dark-dit_frame) > 1e-5) {
00279 cpl_msg_error(__func__, "Mismatch RAW DIT (%g) / DARK DIT (%g)",
00280 dit_frame, dit_dark) ;
00281 cpl_msg_indent_less() ;
00282 return NULL ;
00283 }
00284 cpl_msg_indent_less() ;
00285
00286
00287 if (crire_stripe_keys_mismatch(fname, dark)) {
00288 cpl_msg_error(__func__,
00289 "Mismatch of STRIPE keys with the dark frame") ;
00290 return NULL ;
00291 }
00292 }
00293
00294
00295 out = cpl_imagelist_new() ;
00296
00297
00298 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00299 cpl_msg_info(__func__, "Compute the MASTER FLAT for chip nb %d", i+1) ;
00300 cpl_msg_indent_more() ;
00301
00302
00303 in = crires_load_frameset(flatframes, CRIRES_ILLUM_FULL_DETECTOR,
00304 i+1, CPL_TYPE_FLOAT) ;
00305
00306
00307 if (crires_calib_chip_list(in, CRIRES_ILLUM_FULL_DETECTOR, i+1,
00308 NULL, dark, NULL, NULL, dit_frame)) {
00309 cpl_msg_error(__func__, "Cannot apply the calibrations") ;
00310 cpl_imagelist_delete(in) ;
00311 return NULL ;
00312 }
00313
00314
00315 medians = cpl_vector_new(nframes) ;
00316
00317
00318 cpl_msg_info(__func__, "Normalise with the median") ;
00319 for (j=0 ; j<nframes ; j++) {
00320 median = cpl_image_get_median(cpl_imagelist_get(in, j)) ;
00321 if (cpl_error_get_code()) {
00322 cpl_msg_error(__func__, "Cannot compute the median") ;
00323 cpl_imagelist_delete(in) ;
00324 cpl_vector_delete(medians) ;
00325 return NULL ;
00326 }
00327 cpl_vector_set(medians, j, median) ;
00328 if (fabs(median) > 1e-3)
00329 cpl_image_divide_scalar(cpl_imagelist_get(in, j), median) ;
00330 }
00331
00332
00333 crires_win_flat_config.flat_mean[i] = cpl_vector_get_mean(medians) ;
00334 if (cpl_vector_get_size(medians) > 1)
00335 crires_win_flat_config.flat_stdev[i]=cpl_vector_get_stdev(medians);
00336 else
00337 crires_win_flat_config.flat_stdev[i] = -1.0 ;
00338 crires_win_flat_config.flat_flux[i] =
00339 crires_win_flat_config.flat_mean[i] / dit_frame ;
00340
00341
00342 if ((ima = crires_combine_collapse_imagelist(in, medians,
00343 crires_win_flat_config.kappa_sigclip,
00344 i+1,
00345 crires_win_flat_config.coll_meth)) == NULL) {
00346 cpl_msg_error(__func__, "Cannot average the flats") ;
00347 cpl_imagelist_delete(in) ;
00348 cpl_vector_delete(medians) ;
00349 return NULL ;
00350 }
00351 cpl_vector_delete(medians) ;
00352 cpl_imagelist_delete(in) ;
00353
00354
00355 crires_win_flat_config.flat_master_rms[i] =
00356 cpl_image_get_stdev(ima) ;
00357
00358
00359 cpl_imagelist_set(out, ima, i) ;
00360 cpl_msg_indent_less() ;
00361 }
00362
00363 return out ;
00364 }
00365
00366
00375
00376 static cpl_imagelist * crires_win_flat_bpm(
00377 cpl_imagelist * flat,
00378 double low,
00379 double high,
00380 double bad_per_line_limit)
00381 {
00382 cpl_imagelist * bpm ;
00383 int nima ;
00384 cpl_image * bpm_cur ;
00385 cpl_mask * mask_cur ;
00386 cpl_binary * pmask_cur ;
00387 int nx, ny, cur_bp_nb ;
00388 int i, j, k ;
00389
00390
00391 if (flat == NULL) return NULL ;
00392
00393
00394 nima = cpl_imagelist_get_size(flat) ;
00395
00396
00397 bpm = cpl_imagelist_new() ;
00398
00399
00400 for (i=0 ; i<nima ; i++) {
00401
00402 if ((mask_cur = cpl_mask_threshold_image_create(
00403 cpl_imagelist_get(flat, i), low, high)) == NULL) {
00404 cpl_msg_error(__func__, "Cannot create bad pixels map") ;
00405 cpl_imagelist_delete(bpm) ;
00406 return NULL ;
00407 }
00408 cpl_mask_not(mask_cur) ;
00409
00410
00411
00412
00413
00414
00415
00416 nx = cpl_mask_get_size_x(mask_cur) ;
00417 ny = cpl_mask_get_size_y(mask_cur) ;
00418 pmask_cur = cpl_mask_get_data(mask_cur) ;
00419 for (j=0 ; j<ny ; j++) {
00420 cur_bp_nb = cpl_mask_count_window(mask_cur, 1, j+1, nx, j+1) ;
00421
00422 if (cur_bp_nb > bad_per_line_limit * nx) {
00423
00424 for (k=0 ; k<nx ; k++) {
00425 pmask_cur[k+j*nx] = CPL_BINARY_0 ;
00426 }
00427 }
00428 }
00429
00430
00431 bpm_cur = cpl_image_new_from_mask(mask_cur) ;
00432 crires_win_flat_config.bpm_nb[i] = cpl_mask_count(mask_cur) ;
00433 cpl_mask_delete(mask_cur) ;
00434 cpl_imagelist_set(bpm, bpm_cur, i) ;
00435
00436
00437 if (crires_win_flat_config.replace_flag) {
00438 cpl_image_threshold(cpl_imagelist_get(flat, i),
00439 low, high, 1.0, 1.0) ;
00440 }
00441 }
00442 return bpm ;
00443 }
00444
00445
00455
00456 static int crires_win_flat_save(
00457 const cpl_imagelist * flat,
00458 const cpl_imagelist * bpm,
00459 int set_nb,
00460 cpl_frameset * set,
00461 const cpl_parameterlist * parlist,
00462 cpl_frameset * set_tot)
00463 {
00464 cpl_propertylist ** qclists ;
00465 const cpl_frame * ref_frame ;
00466 char * filename ;
00467 cpl_propertylist * inputlist ;
00468 const char * recipe_name = "crires_win_flat" ;
00469 int i ;
00470
00471
00472 ref_frame = irplib_frameset_get_first_from_group(set, CPL_FRAME_GROUP_RAW) ;
00473
00474
00475 qclists = cpl_malloc(CRIRES_NB_DETECTORS * sizeof(cpl_propertylist*)) ;
00476 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00477 qclists[i] = cpl_propertylist_new() ;
00478 cpl_propertylist_append_int(qclists[i], "ESO QC NBBAD",
00479 crires_win_flat_config.bpm_nb[i]) ;
00480 cpl_propertylist_append_double(qclists[i], "ESO QC FLAT MEAN",
00481 crires_win_flat_config.flat_mean[i]) ;
00482 cpl_propertylist_append_double(qclists[i], "ESO QC FLAT STDEV",
00483 crires_win_flat_config.flat_stdev[i]) ;
00484 cpl_propertylist_append_double(qclists[i], "ESO QC FLAT FLUX",
00485 crires_win_flat_config.flat_flux[i]) ;
00486 cpl_propertylist_append_double(qclists[i], "ESO QC FLAT MASTER RMS",
00487 crires_win_flat_config.flat_master_rms[i]) ;
00488
00489
00490 inputlist = cpl_propertylist_load_regexp(
00491 cpl_frame_get_filename(ref_frame), i+1,
00492 CRIRES_HEADER_EXT_FORWARD, 0) ;
00493 cpl_propertylist_copy_property_regexp(qclists[i], inputlist,
00494 CRIRES_HEADER_EXT_FORWARD, 0) ;
00495 cpl_propertylist_delete(inputlist) ;
00496 }
00497
00498
00499 filename = cpl_sprintf("%s_set%02d.fits", recipe_name, set_nb) ;
00500 crires_image_save(set_tot,
00501 parlist,
00502 set,
00503 flat,
00504 recipe_name,
00505 CRIRES_CALPRO_FLAT_WIN,
00506 CRIRES_PROTYPE_FLAT,
00507 CRIRES_ILLUM_FULL_DETECTOR,
00508 NULL,
00509 (const cpl_propertylist**)qclists,
00510 PACKAGE "/" PACKAGE_VERSION,
00511 filename) ;
00512 cpl_free(filename) ;
00513
00514
00515 if (bpm != NULL) {
00516 filename = cpl_sprintf("%s_set%02d_bpm.fits", recipe_name, set_nb) ;
00517 crires_image_save(set_tot,
00518 parlist,
00519 set,
00520 bpm,
00521 recipe_name,
00522 CRIRES_CALPRO_BPM_WIN,
00523 CRIRES_PROTYPE_BPM,
00524 CRIRES_ILLUM_FULL_DETECTOR,
00525 NULL,
00526 (const cpl_propertylist**)qclists,
00527 PACKAGE "/" PACKAGE_VERSION,
00528 filename) ;
00529 cpl_free(filename) ;
00530 }
00531
00532
00533 for (i=0 ; i<CRIRES_NB_DETECTORS ; i++) {
00534 cpl_propertylist_delete(qclists[i]) ;
00535 }
00536 cpl_free(qclists) ;
00537 return 0;
00538 }
00539
00540
00547
00548 static int crires_win_flat_compare(
00549 const cpl_frame * frame1,
00550 const cpl_frame * frame2)
00551 {
00552 int comparison ;
00553 cpl_propertylist * plist1 ;
00554 cpl_propertylist * plist2 ;
00555 double dval1, dval2 ;
00556
00557
00558 if (frame1==NULL || frame2==NULL) return -1 ;
00559
00560
00561 if ((plist1=cpl_propertylist_load(cpl_frame_get_filename(frame1),0))==NULL){
00562 cpl_msg_error(__func__, "getting header from reference frame");
00563 return -1 ;
00564 }
00565 if ((plist2=cpl_propertylist_load(cpl_frame_get_filename(frame2),0))==NULL){
00566 cpl_msg_error(__func__, "getting header from reference frame");
00567 cpl_propertylist_delete(plist1) ;
00568 return -1 ;
00569 }
00570
00571
00572 if (cpl_error_get_code()) {
00573 cpl_propertylist_delete(plist1) ;
00574 cpl_propertylist_delete(plist2) ;
00575 return -1 ;
00576 }
00577
00578 comparison = 1 ;
00579
00580
00581 dval1 = crires_pfits_get_dit(plist1) ;
00582 dval2 = crires_pfits_get_dit(plist2) ;
00583 if (cpl_error_get_code()) {
00584 cpl_msg_error(__func__, "Cannot get the DIT");
00585 cpl_propertylist_delete(plist1) ;
00586 cpl_propertylist_delete(plist2) ;
00587 return -1 ;
00588 }
00589 if (fabs(dval1-dval2) > 1e-3) comparison = 0 ;
00590
00591
00592 dval1 = crires_pfits_get_refwlen(plist1) ;
00593 dval2 = crires_pfits_get_refwlen(plist2) ;
00594 if (cpl_error_get_code()) {
00595 cpl_msg_error(__func__, "Cannot get the reference wavelength");
00596 cpl_propertylist_delete(plist1) ;
00597 cpl_propertylist_delete(plist2) ;
00598 return -1 ;
00599 }
00600 if (fabs(dval1-dval2) > 1e-3) comparison = 0 ;
00601
00602
00603 dval1 = crires_pfits_get_bafflepos(plist1) ;
00604 dval2 = crires_pfits_get_bafflepos(plist2) ;
00605 if (cpl_error_get_code()) {
00606 cpl_msg_error(__func__, "Cannot get the baffle position");
00607 cpl_propertylist_delete(plist1) ;
00608 cpl_propertylist_delete(plist2) ;
00609 return -1 ;
00610 }
00611 if (fabs(dval1-dval2) > 1e-3) comparison = 0 ;
00612
00613 cpl_propertylist_delete(plist1) ;
00614 cpl_propertylist_delete(plist2) ;
00615 return comparison ;
00616 }
00617