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 "visir_recipe.h"
00037
00038
00039 #include <string.h>
00040
00041
00042
00043
00044
00045 #define RECIPE_STRING "visir_util_convert_weight"
00046
00047 #define VISIR_FRAME_WEIGHT 0
00048 #define VISIR_FRAME_ERROR 1
00049 #define VISIR_FRAME_VARIANCE 2
00050
00051
00052
00053
00054
00055
00056 static cpl_error_code visir_util_convert_weight_one(cpl_frameset *,
00057 irplib_framelist *,
00058 const cpl_frame *, int,
00059 const cpl_parameterlist *);
00060
00061 cpl_recipe_define(visir_util_convert_weight, VISIR_BINARY_VERSION,
00062 "Lars Lundin", PACKAGE_BUGREPORT, "2011",
00063 "Conversion between different weight maps",
00064 "The files listed in the Set Of Frames (sof-file) "
00065 "must be tagged:\n"
00066 "VISIR-weight-map.fits " VISIR_UTIL_WEIGHT_MAP " or\n"
00067 "VISIR-error-map.fits " VISIR_UTIL_ERROR_MAP " or\n"
00068 "VISIR-variance-map.fits " VISIR_UTIL_VARIANCE_MAP "\n"
00069 "VISIR-bpm-file.fits " VISIR_CALIB_BPM " (optional)\n"
00070 "\nThe relation between an error e, a weight w and a "
00071 "variance v is:\n"
00072 "e = sqrt(v) = 1/sqrt(w).\n"
00073 "\n"
00074 "Depending on the setting of the boolean recipe parameters "
00075 "each input file will cause the following product(s) to be "
00076 "created (each with a FITS card 'HIERARCH " CPL_DFS_PRO_CATG
00077 "' with the specified value)\n"
00078 VISIR_UTIL_ERROR_MAP_PROCATG
00079 ": An error map, requires input v >= 0, w > 0\n"
00080 VISIR_UTIL_WEIGHT_MAP_PROCATG
00081 ": A weight map, requires input e > 0, v > 0\n"
00082 VISIR_UTIL_VARIANCE_MAP_PROCATG
00083 ": A variance map, requires input e >= 0, w > 0\n"
00084 "If for a given input the identical output is specified, "
00085 "the data unit(s) may still change according to the provided "
00086 "bad pixel map\n"
00087 "In a weight map product any bad pixel is set to 0.\n"
00088 "In a variance or error map product any bad pixel is set to "
00089 "the value specified by the relevant recipe option.");
00090
00091
00095
00096
00097
00098
00099
00100
00101
00102
00110
00111 static cpl_error_code
00112 visir_util_convert_weight_fill_parameterlist(cpl_parameterlist * self)
00113 {
00114 const char * context = PACKAGE "." RECIPE_STRING;
00115 cpl_error_code err;
00116
00117 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
00118
00119
00120
00121
00122 err = irplib_parameterlist_set_bool(self, PACKAGE, RECIPE_STRING, "error",
00123 CPL_TRUE, NULL, context, "Convert to "
00124 "error");
00125 cpl_ensure_code(!err, err);
00126
00127
00128
00129 err = irplib_parameterlist_set_bool(self, PACKAGE, RECIPE_STRING, "weight",
00130 CPL_FALSE, NULL, context, "Convert to "
00131 "weight. Bad pixels are seto to 0");
00132 cpl_ensure_code(!err, err);
00133
00134
00135
00136 err = irplib_parameterlist_set_bool(self, PACKAGE, RECIPE_STRING, "variance",
00137 CPL_FALSE, NULL, context, "Convert to "
00138 "variance. Bad pixels are seto to 0");
00139 cpl_ensure_code(!err, err);
00140
00141
00142
00143 err = irplib_parameterlist_set_double(self, PACKAGE, RECIPE_STRING,
00144 "bad_value", FLT_MAX, NULL, context,
00145 "The value to use for any bad pixel "
00146 "in an error or variance map");
00147 cpl_ensure_code(!err, err);
00148
00149
00150 err = irplib_parameterlist_set_bool(self, PACKAGE, RECIPE_STRING,
00151 "bad_flag", CPL_TRUE, NULL, context,
00152 "The value used to flag a bad pixel "
00153 "in an input bad pixel map. True: 1. "
00154 "False: 0.");
00155 cpl_ensure_code(!err, err);
00156
00157 return CPL_ERROR_NONE;
00158 }
00159
00160
00161
00162
00169
00170 static int visir_util_convert_weight(cpl_frameset * framelist,
00171 const cpl_parameterlist * parlist)
00172 {
00173 #ifdef _OPENMP
00174 cpl_errorstate cleanstate = cpl_errorstate_get();
00175 #endif
00176 cpl_error_code didfail = CPL_ERROR_NONE;
00177 irplib_framelist * allframes = NULL;
00178 irplib_framelist * rawframes = NULL;
00179 const cpl_frame * bpmframe = NULL;
00180 int i, n;
00181
00182
00183
00184 skip_if (visir_dfs_set_groups(framelist));
00185
00186
00187 allframes = irplib_framelist_cast(framelist);
00188 skip_if(allframes == NULL);
00189 rawframes = irplib_framelist_extract_regexp(allframes, "^("
00190 VISIR_UTIL_WEIGHT_MAP "|"
00191 VISIR_UTIL_VARIANCE_MAP "|"
00192 VISIR_UTIL_ERROR_MAP ")$",
00193 CPL_FALSE);
00194 skip_if (rawframes == NULL);
00195 bpmframe = cpl_frameset_find_const(framelist, VISIR_CALIB_BPM);
00196
00197 any_if("Propagating error");
00198
00199 n = irplib_framelist_get_size(rawframes);
00200 #ifdef _OPENMP
00201 #pragma omp parallel for private(i)
00202 #endif
00203 for (i = 0; i < n; i++) {
00204 if (!didfail) {
00205
00206
00207
00208
00209
00210
00211
00212
00213 if (visir_util_convert_weight_one(framelist, rawframes, bpmframe,
00214 i, parlist)) {
00215 const cpl_error_code errori = cpl_error_set_where(cpl_func);
00216 #ifdef _OPENMP
00217
00218
00219 cpl_errorstate_dump(cleanstate, CPL_FALSE, NULL);
00220 cpl_errorstate_set(cleanstate);
00221 #pragma omp critical(visir_util_convert_weight)
00222 #endif
00223 didfail = errori;
00224 }
00225 }
00226 }
00227
00228 error_if(didfail, didfail, "Failed to convert data in %d frame(s)", n);
00229
00230 end_skip;
00231
00232 irplib_framelist_delete(allframes);
00233 irplib_framelist_delete(rawframes);
00234
00235 return cpl_error_get_code();
00236 }
00237
00238
00239
00249
00250 static
00251 cpl_error_code visir_util_convert_weight_one(cpl_frameset * framelist,
00252 irplib_framelist * rawframes,
00253 const cpl_frame * bpmframe, int i,
00254 const cpl_parameterlist * parlist)
00255 {
00256
00257 const int n = irplib_framelist_get_size(rawframes);
00258 const cpl_boolean do_w = irplib_parameterlist_get_bool(parlist, PACKAGE,
00259 RECIPE_STRING,
00260 "weight");
00261 const cpl_boolean do_e = irplib_parameterlist_get_bool(parlist, PACKAGE,
00262 RECIPE_STRING,
00263 "error");
00264 const cpl_boolean do_v = irplib_parameterlist_get_bool(parlist, PACKAGE,
00265 RECIPE_STRING,
00266 "variance");
00267 const double bad_val = irplib_parameterlist_get_double(parlist, PACKAGE,
00268 RECIPE_STRING,
00269 "bad_value");
00270
00271 const cpl_boolean bad_flag = irplib_parameterlist_get_bool(parlist, PACKAGE,
00272 RECIPE_STRING,
00273 "bad_flag");
00274
00275 cpl_errorstate prestate = cpl_errorstate_get();
00276 cpl_frameset * products = cpl_frameset_new();
00277 cpl_frameset * usedframes = cpl_frameset_new();
00278 const cpl_frame * frame = irplib_framelist_get_const(rawframes, i);
00279 const char * tag = cpl_frame_get_tag(frame);
00280 const int frametype =
00281 tag != NULL && !strcmp(tag, VISIR_UTIL_WEIGHT_MAP)
00282 ? VISIR_FRAME_WEIGHT : (tag != NULL && !strcmp(tag, VISIR_UTIL_ERROR_MAP)
00283 ? VISIR_FRAME_ERROR : VISIR_FRAME_VARIANCE);
00284 const char * filename = cpl_frame_get_filename(frame);
00285 const int next = cpl_fits_count_extensions(filename);
00286 cpl_imagelist * map = NULL;
00287 cpl_imagelist * emap = NULL;
00288 cpl_imagelist * wmap = NULL;
00289 cpl_imagelist * vmap = NULL;
00290 const char * bpmname = bpmframe ? cpl_frame_get_filename(bpmframe)
00291 : NULL;
00292 const int mext = bpmname ? cpl_fits_count_extensions(bpmname)
00293 : -1;
00294 cpl_image * ibpm = NULL;
00295 cpl_image * isqr = NULL;
00296 cpl_image * ione = NULL;
00297 cpl_image * idiv = NULL;
00298 cpl_mask * bpm = NULL;
00299 cpl_mask * bpmi = NULL;
00300 cpl_propertylist * plist = NULL;
00301
00302 char * eproname = do_e == CPL_FALSE ? NULL
00303 : cpl_sprintf(RECIPE_STRING "_%03d_error" CPL_DFS_FITS, 1+i);
00304 char * wproname = do_w == CPL_FALSE ? NULL
00305 : cpl_sprintf(RECIPE_STRING "_%03d_weight" CPL_DFS_FITS, 1+i);
00306 char * vproname = do_v == CPL_FALSE ? NULL
00307 : cpl_sprintf(RECIPE_STRING "_%03d_variance" CPL_DFS_FITS, 1+i);
00308 int iext;
00309
00310 cpl_msg_info(cpl_func, "Converting %d Data unit(s) in frame %d/%d",
00311 1+next, 1+i, n);
00312
00313 any_if("Propagating error");
00314
00315 if (bpmname != NULL) {
00316 error_if(mext != 1 && mext != next, CPL_ERROR_INCOMPATIBLE_INPUT,
00317 "Raw file %s has %d extension(s) <=> %d extension(s) of bpm-"
00318 "file %s", filename, next, mext, bpmname);
00319 }
00320
00321 for (iext = 0; iext <= next; iext++) {
00322
00323
00324 cpl_propertylist_delete(plist);
00325 plist = cpl_propertylist_load_regexp(filename, iext, CPL_DFS_PRO_CATG,
00326 CPL_TRUE);
00327
00328 skip_if(plist == NULL);
00329
00330 if (emap != map) cpl_imagelist_delete(emap);
00331 emap = NULL;
00332 if (wmap != map) cpl_imagelist_delete(wmap);
00333 wmap = NULL;
00334 if (vmap != map) cpl_imagelist_delete(vmap);
00335 vmap = NULL;
00336
00337 cpl_imagelist_delete(map);
00338 map = cpl_imagelist_load(filename, CPL_TYPE_FLOAT, iext);
00339 if (!cpl_errorstate_is_equal(prestate)) {
00340 cpl_errorstate_set(prestate);
00341 } else {
00342
00343
00344 int j;
00345 const int m = cpl_imagelist_get_size(map);
00346
00347 if (iext <= mext) {
00348
00349 cpl_image_delete(ibpm);
00350 ibpm = cpl_image_load(bpmname, CPL_TYPE_UNSPECIFIED, 0, iext);
00351 cpl_mask_delete(bpm);
00352 bpm = cpl_mask_threshold_image_create(ibpm, 0.5, DBL_MAX);
00353 if (!bad_flag) cpl_mask_not(bpm);
00354 skip_if(bpm == NULL);
00355 }
00356
00357 for (j = 0; j < m; j++) {
00358 cpl_image * imap = cpl_imagelist_get(map, j);
00359
00360
00361 cpl_mask_delete(bpmi);
00362 bpmi = cpl_mask_threshold_image_create(imap, 0.0, DBL_MAX);
00363 bug_if(cpl_mask_not(bpmi));
00364
00365
00366 if (bpm)
00367 skip_if(cpl_mask_or(bpmi, bpm));
00368
00369 bug_if(cpl_image_reject_from_mask(imap, bpmi));
00370
00371
00372 bug_if(cpl_image_fill_rejected(imap, 1.0));
00373
00374 if (frametype == VISIR_FRAME_WEIGHT) {
00375
00376 if (do_w) {
00377 if (wmap == NULL) wmap = map;
00378 }
00379
00380 if (do_v || do_e) {
00381
00382
00383 if (ione == NULL) {
00384 ione = cpl_image_new(cpl_image_get_size_x(imap),
00385 cpl_image_get_size_y(imap),
00386 CPL_TYPE_FLOAT);
00387 cpl_image_add_scalar(ione, 1.0);
00388
00389 }
00390
00391 idiv = cpl_image_divide_create(ione, imap);
00392
00393
00394
00395 if (cpl_image_get_bpm_const(idiv))
00396 bug_if(cpl_mask_or(bpmi,
00397 cpl_image_get_bpm_const(idiv)));
00398
00399 bug_if(cpl_image_reject_from_mask(idiv, bpmi));
00400 }
00401
00402 if (do_v) {
00403 if (vmap == NULL)
00404 vmap = do_w ? cpl_imagelist_new() : map;
00405
00406
00407 bug_if(cpl_imagelist_set(vmap, idiv, j));
00408 idiv = NULL;
00409 }
00410
00411 if (do_e) {
00412 if (emap == NULL)
00413 emap = do_w || do_v ? cpl_imagelist_new() : map;
00414
00415 if (do_v) {
00416 idiv = cpl_image_power_create(cpl_imagelist_get_const
00417 (vmap, j), 0.5);
00418 } else {
00419 bug_if(cpl_image_power(idiv, 0.5));
00420 }
00421
00422
00423 bug_if(cpl_imagelist_set(emap, idiv, j));
00424 idiv = NULL;
00425 }
00426 } else if (frametype == VISIR_FRAME_ERROR) {
00427
00428 if (do_e) {
00429 if (emap == NULL) emap = map;
00430 }
00431
00432 if (do_v) {
00433 if (vmap == NULL)
00434 vmap = do_e ? cpl_imagelist_new() : map;
00435
00436 if (do_e) {
00437 isqr = cpl_image_power_create(imap, 2.0);
00438
00439
00440 bug_if(cpl_imagelist_set(vmap, isqr, j));
00441 isqr = NULL;
00442 } else {
00443 bug_if(cpl_image_power(imap, 2.0));
00444 }
00445 }
00446
00447 if (do_w) {
00448 if (wmap == NULL)
00449 wmap = do_e || do_v ? cpl_imagelist_new() : map;
00450
00451 if (ione == NULL) {
00452 ione = cpl_image_new(cpl_image_get_size_x(imap),
00453 cpl_image_get_size_y(imap),
00454 CPL_TYPE_FLOAT);
00455 cpl_image_add_scalar(ione, 1.0);
00456
00457 }
00458
00459 if (do_v) {
00460 idiv = cpl_image_divide_create
00461 (ione, cpl_imagelist_get(vmap, j));
00462 } else if (do_e) {
00463 isqr = cpl_image_power_create(imap, 2.0);
00464 idiv = cpl_image_divide_create(ione, isqr);
00465 } else {
00466 bug_if(cpl_image_power(imap, 2.0));
00467 idiv = cpl_image_divide_create(ione, imap);
00468 }
00469 cpl_image_delete(isqr);
00470 isqr = NULL;
00471
00472
00473 bug_if(cpl_imagelist_set(wmap, idiv, j));
00474 idiv = NULL;
00475 }
00476
00477 } else if (frametype == VISIR_FRAME_VARIANCE) {
00478 if (do_v) {
00479 if (vmap == NULL) vmap = map;
00480 }
00481
00482 if (do_w) {
00483 if (wmap == NULL)
00484 wmap = do_v ? cpl_imagelist_new() : map;
00485
00486 if (ione == NULL) {
00487 ione = cpl_image_new(cpl_image_get_size_x(imap),
00488 cpl_image_get_size_y(imap),
00489 CPL_TYPE_FLOAT);
00490 cpl_image_add_scalar(ione, 1.0);
00491
00492 }
00493
00494 idiv = cpl_image_divide_create(ione, imap);
00495
00496
00497
00498 if (cpl_image_get_bpm_const(idiv))
00499 bug_if(cpl_mask_or(bpmi,
00500 cpl_image_get_bpm_const(idiv)));
00501
00502 bug_if(cpl_image_reject_from_mask(idiv, bpmi));
00503
00504
00505 bug_if(cpl_imagelist_set(wmap, idiv, j));
00506 idiv = NULL;
00507 }
00508
00509 if (do_e) {
00510 if (emap == NULL)
00511 emap = do_v || do_w ? cpl_imagelist_new() : map;
00512
00513 if (do_v || do_w) {
00514 isqr = cpl_image_power_create(imap, 0.5);
00515
00516 bug_if(cpl_imagelist_set(emap, isqr, j));
00517 isqr = NULL;
00518
00519 } else {
00520 bug_if(cpl_image_power(imap, 0.5));
00521 }
00522 }
00523 } else {
00524 bug_if(1);
00525 }
00526
00527
00528 if (do_w) {
00529 bug_if(cpl_image_fill_rejected(cpl_imagelist_get
00530 (wmap, j), 0.0));
00531 }
00532 if (do_e) {
00533 bug_if(cpl_image_fill_rejected(cpl_imagelist_get
00534 (emap, j), bad_val));
00535 }
00536 if (do_v) {
00537
00538 bug_if(cpl_image_fill_rejected(cpl_imagelist_get
00539 (vmap, j), bad_val));
00540 }
00541 }
00542
00543 }
00544
00545 if (iext == 0) {
00546
00547 bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
00548 (frame)));
00549 if (bpmframe != NULL)
00550 bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
00551 (bpmframe)));
00552
00553 if (do_w) {
00554 if (wmap == NULL) {
00555 skip_if(irplib_dfs_save_propertylist(products, parlist,
00556 usedframes,
00557 RECIPE_STRING,
00558 VISIR_UTIL_WEIGHT_MAP_PROCATG,
00559 plist, NULL,
00560 visir_pipe_id,
00561 wproname));
00562 } else {
00563 skip_if(irplib_dfs_save_imagelist(products, parlist,
00564 usedframes, wmap,
00565 CPL_BPP_IEEE_FLOAT,
00566 RECIPE_STRING,
00567 VISIR_UTIL_WEIGHT_MAP_PROCATG,
00568 plist, NULL,
00569 visir_pipe_id, wproname));
00570 }
00571 }
00572 if (do_e) {
00573 if (emap == NULL) {
00574 skip_if(irplib_dfs_save_propertylist(products, parlist,
00575 usedframes,
00576 RECIPE_STRING,
00577 VISIR_UTIL_ERROR_MAP_PROCATG,
00578 plist, NULL,
00579 visir_pipe_id,
00580 eproname));
00581 } else {
00582 skip_if(irplib_dfs_save_imagelist(products, parlist,
00583 usedframes, emap,
00584 CPL_BPP_IEEE_FLOAT,
00585 RECIPE_STRING,
00586 VISIR_UTIL_ERROR_MAP_PROCATG,
00587 plist, NULL,
00588 visir_pipe_id, eproname));
00589 }
00590 }
00591 if (do_v) {
00592 if (vmap == NULL) {
00593 skip_if(irplib_dfs_save_propertylist(products, parlist,
00594 usedframes,
00595 RECIPE_STRING,
00596 VISIR_UTIL_VARIANCE_MAP_PROCATG,
00597 plist, NULL,
00598 visir_pipe_id,
00599 vproname));
00600 } else {
00601 skip_if(irplib_dfs_save_imagelist(products, parlist,
00602 usedframes, vmap,
00603 CPL_BPP_IEEE_FLOAT,
00604 RECIPE_STRING,
00605 VISIR_UTIL_VARIANCE_MAP_PROCATG,
00606 plist, NULL,
00607 visir_pipe_id, vproname));
00608 }
00609 }
00610 } else {
00611 if (do_w) {
00612 if (wmap == NULL) {
00613 skip_if(cpl_propertylist_save(plist, wproname,
00614 CPL_IO_EXTEND));
00615 } else {
00616 skip_if(cpl_imagelist_save(wmap, wproname,
00617 CPL_BPP_IEEE_FLOAT, plist,
00618 CPL_IO_EXTEND));
00619 }
00620 }
00621 if (do_e) {
00622 if (emap == NULL) {
00623 skip_if(cpl_propertylist_save(plist, eproname,
00624 CPL_IO_EXTEND));
00625 } else {
00626 skip_if(cpl_imagelist_save(emap, eproname,
00627 CPL_BPP_IEEE_FLOAT, plist,
00628 CPL_IO_EXTEND));
00629 }
00630 }
00631 if (do_v) {
00632 if (vmap == NULL) {
00633 skip_if(cpl_propertylist_save(plist, vproname,
00634 CPL_IO_EXTEND));
00635 } else {
00636 skip_if(cpl_imagelist_save(vmap, vproname,
00637 CPL_BPP_IEEE_FLOAT, plist,
00638 CPL_IO_EXTEND));
00639 }
00640 }
00641 }
00642 }
00643
00644 for (frame = cpl_frameset_get_first_const(products);
00645 frame != NULL;
00646 frame = cpl_frameset_get_next_const(products)) {
00647 cpl_frame * copy = cpl_frame_duplicate(frame);
00648 cpl_error_code error;
00649
00650 #ifdef _OPENMP
00651 #pragma omp critical(visir_util_weight2error_one)
00652 #endif
00653 error = cpl_frameset_insert(framelist, copy);
00654
00655 if (error) break;
00656 }
00657 bug_if(frame != NULL);
00658
00659 end_skip;
00660
00661 if (emap != map) cpl_imagelist_delete(emap);
00662 if (wmap != map) cpl_imagelist_delete(wmap);
00663 if (vmap != map) cpl_imagelist_delete(vmap);
00664 cpl_imagelist_delete(map);
00665 cpl_free(eproname);
00666 cpl_free(wproname);
00667 cpl_free(vproname);
00668 cpl_image_delete(ione);
00669 cpl_image_delete(idiv);
00670 cpl_image_delete(isqr);
00671 cpl_image_delete(ibpm);
00672 cpl_mask_delete(bpm);
00673 cpl_mask_delete(bpmi);
00674 cpl_propertylist_delete(plist);
00675 cpl_frameset_delete(usedframes);
00676 cpl_frameset_delete(products);
00677
00678 return cpl_error_get_code();
00679
00680 }
00681
00682