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 cpl_error_code didfail = CPL_ERROR_NONE;
00174 irplib_framelist * allframes = NULL;
00175 irplib_framelist * rawframes = NULL;
00176 const cpl_frame * bpmframe = NULL;
00177 int i, n;
00178
00179
00180
00181 skip_if (visir_dfs_set_groups(framelist));
00182 cpl_fits_set_mode(CPL_FITS_START_CACHING);
00183
00184
00185 allframes = irplib_framelist_cast(framelist);
00186 skip_if(allframes == NULL);
00187 rawframes = irplib_framelist_extract_regexp(allframes, "^("
00188 VISIR_UTIL_WEIGHT_MAP "|"
00189 VISIR_UTIL_VARIANCE_MAP "|"
00190 VISIR_UTIL_ERROR_MAP ")$",
00191 CPL_FALSE);
00192 skip_if (rawframes == NULL);
00193 bpmframe = cpl_frameset_find_const(framelist, VISIR_CALIB_BPM);
00194
00195 any_if("Propagating error");
00196
00197 n = irplib_framelist_get_size(rawframes);
00198 for (i = 0; i < n; i++) {
00199
00200 skip_if (visir_util_convert_weight_one(framelist, rawframes, bpmframe,
00201 i, parlist));
00202 cpl_fits_set_mode(CPL_FITS_RESTART_CACHING);
00203 }
00204
00205 error_if(didfail, didfail, "Failed to convert data in %d frame(s)", n);
00206
00207 end_skip;
00208
00209 irplib_framelist_delete(allframes);
00210 irplib_framelist_delete(rawframes);
00211
00212 return cpl_error_get_code();
00213 }
00214
00215
00216
00226
00227 static
00228 cpl_error_code visir_util_convert_weight_one(cpl_frameset * framelist,
00229 irplib_framelist * rawframes,
00230 const cpl_frame * bpmframe, int i,
00231 const cpl_parameterlist * parlist)
00232 {
00233
00234 const int n = irplib_framelist_get_size(rawframes);
00235 const cpl_boolean do_w = irplib_parameterlist_get_bool(parlist, PACKAGE,
00236 RECIPE_STRING,
00237 "weight");
00238 const cpl_boolean do_e = irplib_parameterlist_get_bool(parlist, PACKAGE,
00239 RECIPE_STRING,
00240 "error");
00241 const cpl_boolean do_v = irplib_parameterlist_get_bool(parlist, PACKAGE,
00242 RECIPE_STRING,
00243 "variance");
00244 const double bad_val = irplib_parameterlist_get_double(parlist, PACKAGE,
00245 RECIPE_STRING,
00246 "bad_value");
00247
00248 const cpl_boolean bad_flag = irplib_parameterlist_get_bool(parlist, PACKAGE,
00249 RECIPE_STRING,
00250 "bad_flag");
00251
00252 cpl_errorstate prestate = cpl_errorstate_get();
00253 cpl_frameset * products = cpl_frameset_new();
00254 cpl_frameset * usedframes = cpl_frameset_new();
00255 const cpl_frame * frame = irplib_framelist_get_const(rawframes, i);
00256 const char * tag = cpl_frame_get_tag(frame);
00257 const int frametype =
00258 tag != NULL && !strcmp(tag, VISIR_UTIL_WEIGHT_MAP)
00259 ? VISIR_FRAME_WEIGHT : (tag != NULL && !strcmp(tag, VISIR_UTIL_ERROR_MAP)
00260 ? VISIR_FRAME_ERROR : VISIR_FRAME_VARIANCE);
00261 const char * filename = cpl_frame_get_filename(frame);
00262 const int next = cpl_fits_count_extensions(filename);
00263 cpl_imagelist * map = NULL;
00264 cpl_imagelist * emap = NULL;
00265 cpl_imagelist * wmap = NULL;
00266 cpl_imagelist * vmap = NULL;
00267 const char * bpmname = bpmframe ? cpl_frame_get_filename(bpmframe)
00268 : NULL;
00269 const int mext = bpmname ? cpl_fits_count_extensions(bpmname)
00270 : -1;
00271 cpl_image * ibpm = NULL;
00272 cpl_image * isqr = NULL;
00273 cpl_image * ione = NULL;
00274 cpl_image * idiv = NULL;
00275 cpl_mask * bpm = NULL;
00276 cpl_mask * bpmi = NULL;
00277 cpl_propertylist * plist = NULL;
00278
00279 char * eproname = do_e == CPL_FALSE ? NULL
00280 : cpl_sprintf(RECIPE_STRING "_%03d_error" CPL_DFS_FITS, 1+i);
00281 char * wproname = do_w == CPL_FALSE ? NULL
00282 : cpl_sprintf(RECIPE_STRING "_%03d_weight" CPL_DFS_FITS, 1+i);
00283 char * vproname = do_v == CPL_FALSE ? NULL
00284 : cpl_sprintf(RECIPE_STRING "_%03d_variance" CPL_DFS_FITS, 1+i);
00285 int iext;
00286
00287 cpl_msg_info(cpl_func, "Converting %d Data unit(s) in frame %d/%d",
00288 1+next, 1+i, n);
00289
00290 any_if("Propagating error");
00291
00292 if (bpmname != NULL) {
00293 error_if(mext != 1 && mext != next, CPL_ERROR_INCOMPATIBLE_INPUT,
00294 "Raw file %s has %d extension(s) <=> %d extension(s) of bpm-"
00295 "file %s", filename, next, mext, bpmname);
00296 }
00297
00298 for (iext = 0; iext <= next; iext++) {
00299
00300
00301 cpl_propertylist_delete(plist);
00302 plist = cpl_propertylist_load_regexp(filename, iext, CPL_DFS_PRO_CATG,
00303 CPL_TRUE);
00304
00305 skip_if(plist == NULL);
00306
00307 if (emap != map) cpl_imagelist_delete(emap);
00308 emap = NULL;
00309 if (wmap != map) cpl_imagelist_delete(wmap);
00310 wmap = NULL;
00311 if (vmap != map) cpl_imagelist_delete(vmap);
00312 vmap = NULL;
00313
00314 cpl_imagelist_delete(map);
00315 map = cpl_imagelist_load(filename, CPL_TYPE_FLOAT, iext);
00316 if (!cpl_errorstate_is_equal(prestate)) {
00317 cpl_errorstate_set(prestate);
00318 } else {
00319
00320
00321 int j;
00322 const int m = cpl_imagelist_get_size(map);
00323
00324 if (iext <= mext) {
00325
00326 cpl_image_delete(ibpm);
00327 ibpm = cpl_image_load(bpmname, CPL_TYPE_UNSPECIFIED, 0, iext);
00328 cpl_mask_delete(bpm);
00329 bpm = cpl_mask_threshold_image_create(ibpm, 0.5, DBL_MAX);
00330 if (!bad_flag) cpl_mask_not(bpm);
00331 skip_if(bpm == NULL);
00332 }
00333
00334 for (j = 0; j < m; j++) {
00335 cpl_image * imap = cpl_imagelist_get(map, j);
00336
00337
00338 cpl_mask_delete(bpmi);
00339 bpmi = cpl_mask_threshold_image_create(imap, 0.0, DBL_MAX);
00340 bug_if(cpl_mask_not(bpmi));
00341
00342
00343 if (bpm)
00344 skip_if(cpl_mask_or(bpmi, bpm));
00345
00346 bug_if(cpl_image_reject_from_mask(imap, bpmi));
00347
00348
00349 bug_if(cpl_image_fill_rejected(imap, 1.0));
00350
00351 if (frametype == VISIR_FRAME_WEIGHT) {
00352
00353 if (do_w) {
00354 if (wmap == NULL) wmap = map;
00355 }
00356
00357 if (do_v || do_e) {
00358
00359
00360 if (ione == NULL) {
00361 ione = cpl_image_new(cpl_image_get_size_x(imap),
00362 cpl_image_get_size_y(imap),
00363 CPL_TYPE_FLOAT);
00364 cpl_image_add_scalar(ione, 1.0);
00365
00366 }
00367
00368 idiv = cpl_image_divide_create(ione, imap);
00369
00370
00371
00372 if (cpl_image_get_bpm_const(idiv))
00373 bug_if(cpl_mask_or(bpmi,
00374 cpl_image_get_bpm_const(idiv)));
00375
00376 bug_if(cpl_image_reject_from_mask(idiv, bpmi));
00377 }
00378
00379 if (do_v) {
00380 if (vmap == NULL)
00381 vmap = do_w ? cpl_imagelist_new() : map;
00382
00383
00384 bug_if(cpl_imagelist_set(vmap, idiv, j));
00385 idiv = NULL;
00386 }
00387
00388 if (do_e) {
00389 if (emap == NULL)
00390 emap = do_w || do_v ? cpl_imagelist_new() : map;
00391
00392 if (do_v) {
00393 idiv = cpl_image_power_create(cpl_imagelist_get_const
00394 (vmap, j), 0.5);
00395 } else {
00396 bug_if(cpl_image_power(idiv, 0.5));
00397 }
00398
00399
00400 bug_if(cpl_imagelist_set(emap, idiv, j));
00401 idiv = NULL;
00402 }
00403 } else if (frametype == VISIR_FRAME_ERROR) {
00404
00405 if (do_e) {
00406 if (emap == NULL) emap = map;
00407 }
00408
00409 if (do_v) {
00410 if (vmap == NULL)
00411 vmap = do_e ? cpl_imagelist_new() : map;
00412
00413 if (do_e) {
00414 isqr = cpl_image_power_create(imap, 2.0);
00415
00416
00417 bug_if(cpl_imagelist_set(vmap, isqr, j));
00418 isqr = NULL;
00419 } else {
00420 bug_if(cpl_image_power(imap, 2.0));
00421 }
00422 }
00423
00424 if (do_w) {
00425 if (wmap == NULL)
00426 wmap = do_e || do_v ? cpl_imagelist_new() : map;
00427
00428 if (ione == NULL) {
00429 ione = cpl_image_new(cpl_image_get_size_x(imap),
00430 cpl_image_get_size_y(imap),
00431 CPL_TYPE_FLOAT);
00432 cpl_image_add_scalar(ione, 1.0);
00433
00434 }
00435
00436 if (do_v) {
00437 idiv = cpl_image_divide_create
00438 (ione, cpl_imagelist_get(vmap, j));
00439 } else if (do_e) {
00440 isqr = cpl_image_power_create(imap, 2.0);
00441 idiv = cpl_image_divide_create(ione, isqr);
00442 } else {
00443 bug_if(cpl_image_power(imap, 2.0));
00444 idiv = cpl_image_divide_create(ione, imap);
00445 }
00446 cpl_image_delete(isqr);
00447 isqr = NULL;
00448
00449
00450 bug_if(cpl_imagelist_set(wmap, idiv, j));
00451 idiv = NULL;
00452 }
00453
00454 } else if (frametype == VISIR_FRAME_VARIANCE) {
00455 if (do_v) {
00456 if (vmap == NULL) vmap = map;
00457 }
00458
00459 if (do_w) {
00460 if (wmap == NULL)
00461 wmap = do_v ? cpl_imagelist_new() : map;
00462
00463 if (ione == NULL) {
00464 ione = cpl_image_new(cpl_image_get_size_x(imap),
00465 cpl_image_get_size_y(imap),
00466 CPL_TYPE_FLOAT);
00467 cpl_image_add_scalar(ione, 1.0);
00468
00469 }
00470
00471 idiv = cpl_image_divide_create(ione, imap);
00472
00473
00474
00475 if (cpl_image_get_bpm_const(idiv))
00476 bug_if(cpl_mask_or(bpmi,
00477 cpl_image_get_bpm_const(idiv)));
00478
00479 bug_if(cpl_image_reject_from_mask(idiv, bpmi));
00480
00481
00482 bug_if(cpl_imagelist_set(wmap, idiv, j));
00483 idiv = NULL;
00484 }
00485
00486 if (do_e) {
00487 if (emap == NULL)
00488 emap = do_v || do_w ? cpl_imagelist_new() : map;
00489
00490 if (do_v || do_w) {
00491 isqr = cpl_image_power_create(imap, 0.5);
00492
00493 bug_if(cpl_imagelist_set(emap, isqr, j));
00494 isqr = NULL;
00495
00496 } else {
00497 bug_if(cpl_image_power(imap, 0.5));
00498 }
00499 }
00500 } else {
00501 bug_if(1);
00502 }
00503
00504
00505 if (do_w) {
00506 bug_if(cpl_image_fill_rejected(cpl_imagelist_get
00507 (wmap, j), 0.0));
00508 }
00509 if (do_e) {
00510 bug_if(cpl_image_fill_rejected(cpl_imagelist_get
00511 (emap, j), bad_val));
00512 }
00513 if (do_v) {
00514
00515 bug_if(cpl_image_fill_rejected(cpl_imagelist_get
00516 (vmap, j), bad_val));
00517 }
00518 }
00519
00520 }
00521
00522 if (iext == 0) {
00523
00524 bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
00525 (frame)));
00526 if (bpmframe != NULL)
00527 bug_if(cpl_frameset_insert(usedframes, cpl_frame_duplicate
00528 (bpmframe)));
00529
00530 if (do_w) {
00531 if (wmap == NULL) {
00532 skip_if(irplib_dfs_save_propertylist(products, parlist,
00533 usedframes,
00534 RECIPE_STRING,
00535 VISIR_UTIL_WEIGHT_MAP_PROCATG,
00536 plist, NULL,
00537 visir_pipe_id,
00538 wproname));
00539 } else {
00540 skip_if(irplib_dfs_save_imagelist(products, parlist,
00541 usedframes, wmap,
00542 CPL_BPP_IEEE_FLOAT,
00543 RECIPE_STRING,
00544 VISIR_UTIL_WEIGHT_MAP_PROCATG,
00545 plist, NULL,
00546 visir_pipe_id, wproname));
00547 }
00548 }
00549 if (do_e) {
00550 if (emap == NULL) {
00551 skip_if(irplib_dfs_save_propertylist(products, parlist,
00552 usedframes,
00553 RECIPE_STRING,
00554 VISIR_UTIL_ERROR_MAP_PROCATG,
00555 plist, NULL,
00556 visir_pipe_id,
00557 eproname));
00558 } else {
00559 skip_if(irplib_dfs_save_imagelist(products, parlist,
00560 usedframes, emap,
00561 CPL_BPP_IEEE_FLOAT,
00562 RECIPE_STRING,
00563 VISIR_UTIL_ERROR_MAP_PROCATG,
00564 plist, NULL,
00565 visir_pipe_id, eproname));
00566 }
00567 }
00568 if (do_v) {
00569 if (vmap == NULL) {
00570 skip_if(irplib_dfs_save_propertylist(products, parlist,
00571 usedframes,
00572 RECIPE_STRING,
00573 VISIR_UTIL_VARIANCE_MAP_PROCATG,
00574 plist, NULL,
00575 visir_pipe_id,
00576 vproname));
00577 } else {
00578 skip_if(irplib_dfs_save_imagelist(products, parlist,
00579 usedframes, vmap,
00580 CPL_BPP_IEEE_FLOAT,
00581 RECIPE_STRING,
00582 VISIR_UTIL_VARIANCE_MAP_PROCATG,
00583 plist, NULL,
00584 visir_pipe_id, vproname));
00585 }
00586 }
00587 } else {
00588 if (do_w) {
00589 if (wmap == NULL) {
00590 skip_if(cpl_propertylist_save(plist, wproname,
00591 CPL_IO_EXTEND));
00592 } else {
00593 skip_if(cpl_imagelist_save(wmap, wproname,
00594 CPL_BPP_IEEE_FLOAT, plist,
00595 CPL_IO_EXTEND));
00596 }
00597 }
00598 if (do_e) {
00599 if (emap == NULL) {
00600 skip_if(cpl_propertylist_save(plist, eproname,
00601 CPL_IO_EXTEND));
00602 } else {
00603 skip_if(cpl_imagelist_save(emap, eproname,
00604 CPL_BPP_IEEE_FLOAT, plist,
00605 CPL_IO_EXTEND));
00606 }
00607 }
00608 if (do_v) {
00609 if (vmap == NULL) {
00610 skip_if(cpl_propertylist_save(plist, vproname,
00611 CPL_IO_EXTEND));
00612 } else {
00613 skip_if(cpl_imagelist_save(vmap, vproname,
00614 CPL_BPP_IEEE_FLOAT, plist,
00615 CPL_IO_EXTEND));
00616 }
00617 }
00618 }
00619 }
00620
00621 FOR_EACH_FRAMESET_C(frm, products) {
00622 cpl_frame * copy = cpl_frame_duplicate(frm);
00623 cpl_error_code error = cpl_frameset_insert(framelist, copy);
00624
00625 if (error) break;
00626 }
00627 bug_if(frame != NULL);
00628
00629 end_skip;
00630
00631 if (emap != map) cpl_imagelist_delete(emap);
00632 if (wmap != map) cpl_imagelist_delete(wmap);
00633 if (vmap != map) cpl_imagelist_delete(vmap);
00634 cpl_imagelist_delete(map);
00635 cpl_free(eproname);
00636 cpl_free(wproname);
00637 cpl_free(vproname);
00638 cpl_image_delete(ione);
00639 cpl_image_delete(idiv);
00640 cpl_image_delete(isqr);
00641 cpl_image_delete(ibpm);
00642 cpl_mask_delete(bpm);
00643 cpl_mask_delete(bpmi);
00644 cpl_propertylist_delete(plist);
00645 cpl_frameset_delete(usedframes);
00646 cpl_frameset_delete(products);
00647
00648 return cpl_error_get_code();
00649
00650 }
00651
00652