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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 #ifdef HAVE_CONFIG_H
00126 # include <config.h>
00127 #endif
00128
00129
00136
00137
00138
00139
00140
00141 #include <uves_mdark_impl.h>
00142
00143 #include <uves_parameters.h>
00144 #include <uves_utils.h>
00145 #include <uves.h>
00146 #include <uves_dfs.h>
00147 #include <uves_pfits.h>
00148 #include <uves_qclog.h>
00149 #include <uves_recipe.h>
00150 #include <uves_utils_wrappers.h>
00151 #include <uves_error.h>
00152
00153 #include <irplib_access.h>
00154 #include <cpl.h>
00155 #include <float.h>
00156
00157
00158
00159
00160
00161 static void uves_mdark_qclog(cpl_imagelist* raw_images,
00162 cpl_table* qclog);
00163
00164
00165
00166 #define cpl_plugin_get_info uves_mdark_get_info
00167 UVES_RECIPE_DEFINE(
00168 UVES_MDARK_ID, UVES_MDARK_DOM,
00169
00170
00171 uves_define_global_parameters,
00172 "Jonas M. Larsen", "cpl@eso.org",
00173 "Creates the master dark frame",
00174 "This recipe creates a master dark frame by taking the median of all\n"
00175 "input frames which should have identical exposure times. Symbolically,\n"
00176 " masterdark = median( dark_i ) - masterbias\n"
00177 "\n"
00178 "The input dark frames must have same tag and size and must be either\n"
00179 "(P)DARK_BLUE or (P)DARK_RED. Also, a master bias (MASTER_BIAS_xxxx) must\n"
00180 "be provided for each chip (xxxx = BLUE, REDL, REDU).\n"
00181 "\n"
00182 "On blue input the recipe computes one master dark frame; on red input the\n"
00183 "recipe produces a master dark frame for each chip (MASTER_(P)DARK_xxxx).\n");
00184
00186
00187
00188
00189
00209
00210 static cpl_image *
00211 uves_mdark_process_chip(const cpl_imagelist *raw_images, uves_propertylist **raw_headers,
00212 const cpl_image *master_bias,
00213 uves_propertylist *mdark_header)
00214 {
00215 cpl_image *master_dark = NULL;
00216 cpl_image *current_dark = NULL;
00217 cpl_imagelist *preproc_images = NULL;
00218 double min_exptime = 0;
00219 double max_exptime = 0;
00220 int i;
00221
00222
00223
00224 preproc_images = cpl_imagelist_new();
00225 for (i = 0; i < cpl_imagelist_get_size(raw_images); i++)
00226 {
00227 double exposure_time = 0.0;
00228 const uves_propertylist *current_header;
00229
00230 current_dark = cpl_image_duplicate(irplib_imagelist_get_const(raw_images, i));
00231 current_header = raw_headers[i];
00232
00233
00234 if (master_bias != NULL)
00235 {
00236 uves_msg("Subtracting master bias");
00237 check( uves_subtract_bias(current_dark, master_bias),
00238 "Error subtracting master bias");
00239
00240 if (false) {
00241 uves_msg_debug("Thresholding to non-negative values");
00242 check( cpl_image_threshold(current_dark,
00243 0, DBL_MAX,
00244 0, DBL_MAX),
00245 "Error thresholding image");
00246 }
00247 }
00248 else
00249 {
00250 uves_msg("Skipping bias subtraction");
00251 }
00252
00253 check( exposure_time = uves_pfits_get_exptime(current_header),
00254 "Error reading exposure time");
00255
00256
00257 if (i == 0 || exposure_time < min_exptime)
00258 {
00259 min_exptime = exposure_time;
00260 }
00261 if (i == 0 || exposure_time > max_exptime)
00262 {
00263 max_exptime = exposure_time;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 check( cpl_imagelist_set(preproc_images,
00275 current_dark,
00276 i),
00277 "Could not insert image into image list");
00278
00279
00280
00281 current_dark = NULL;
00282 }
00283
00284
00285 uves_msg("Exposure times range from %e s to %e s (%e %% variation)",
00286 min_exptime,
00287 max_exptime,
00288 100 * (max_exptime - min_exptime) / min_exptime);
00289
00290 if ((max_exptime - min_exptime) / min_exptime > .001)
00291 {
00292 uves_msg_warning("Exposure times differ by %e %%",
00293 100 * (max_exptime - min_exptime) / min_exptime);
00294 }
00295
00296
00297 uves_msg("Calculating stack median");
00298 check( master_dark = cpl_imagelist_collapse_median_create(preproc_images),
00299 "Error computing median");
00300
00301 check( uves_pfits_set_exptime(mdark_header, (max_exptime + min_exptime)/2),
00302 "Error setting master dark exposure time");
00303
00304 cleanup:
00305 uves_free_image(¤t_dark);
00306 uves_free_imagelist(&preproc_images);
00307 if (cpl_error_get_code() != CPL_ERROR_NONE)
00308 {
00309 uves_free_image(&master_dark);
00310 }
00311
00312 return master_dark;
00313 }
00314
00315
00322
00323 static void
00324 IRPLIB_CONCAT2X(UVES_MDARK_ID,exe)(cpl_frameset *frames,
00325 const cpl_parameterlist *parameters,
00326 const char *starttime)
00327 {
00328 uves_mdark_exe_body(frames, parameters, starttime, make_str(UVES_MDARK_ID));
00329 return;
00330 }
00331
00332
00344
00345 void
00346 uves_mdark_exe_body(cpl_frameset *frames,
00347 const cpl_parameterlist *parameters,
00348 const char *starttime,
00349 const char *recipe_id)
00350 {
00351
00352
00353
00354
00355
00356
00357
00358
00359 cpl_imagelist *raw_images[2] = {NULL, NULL};
00360 uves_propertylist **raw_headers[2] = {NULL, NULL};
00361
00362
00363 cpl_image *master_bias = NULL;
00364 uves_propertylist *master_bias_header = NULL;
00365
00366
00367 cpl_table* qclog[2] = {NULL, NULL};
00368 cpl_image *master_dark = NULL;
00369 uves_propertylist *product_header[2] = {NULL, NULL};
00370
00371
00372 char *product_filename = NULL;
00373 const char *product_tag[2] = {NULL, NULL};
00374 bool blue;
00375 enum uves_chip chip;
00376
00377
00378
00379 if (irplib_frameset_find(frames, UVES_DARK(true )) != NULL ||
00380 irplib_frameset_find(frames, UVES_DARK(false)) != NULL)
00381 {
00382 check( uves_load_raw_imagelist(frames,
00383 false,
00384 UVES_DARK(true), UVES_DARK(false),
00385 CPL_TYPE_DOUBLE,
00386 raw_images, raw_headers, product_header,
00387 &blue), "Error loading raw dark frames");
00388
00389 for (chip = uves_chip_get_first(blue);
00390 chip != UVES_CHIP_INVALID;
00391 chip = uves_chip_get_next(chip))
00392 {
00393 product_tag[uves_chip_get_index(chip)] = UVES_MASTER_DARK(chip);
00394 }
00395 }
00396 else if (irplib_frameset_find(frames, UVES_PDARK(true )) != NULL ||
00397 irplib_frameset_find(frames, UVES_PDARK(false)) != NULL)
00398 {
00399 check( uves_load_raw_imagelist(frames,
00400 false,
00401 UVES_PDARK(true), UVES_PDARK(false),
00402 CPL_TYPE_DOUBLE,
00403 raw_images, raw_headers, product_header,
00404 &blue), "Error loading raw dark frames");
00405
00406 for (chip = uves_chip_get_first(blue);
00407 chip != UVES_CHIP_INVALID;
00408 chip = uves_chip_get_next(chip))
00409 {
00410 product_tag[uves_chip_get_index(chip)] = UVES_MASTER_PDARK(chip);
00411 }
00412 }
00413 else
00414 {
00415 assure(false, CPL_ERROR_DATA_NOT_FOUND,
00416 "Missing input dark frame: %s, %s, %s or %s expected",
00417 UVES_DARK(true) , UVES_DARK(false),
00418 UVES_PDARK(true), UVES_PDARK(false));
00419 }
00420
00421
00422 for (chip = uves_chip_get_first(blue);
00423 chip != UVES_CHIP_INVALID;
00424 chip = uves_chip_get_next(chip))
00425 {
00426 const char *master_bias_filename = "";
00427 const char *chip_name = "";
00428
00429 int raw_index = uves_chip_get_index(chip);
00430
00431 uves_msg("Processing %s chip",
00432 uves_chip_tostring_upper(chip));
00433
00434
00435 check_nomsg( chip_name = uves_pfits_get_chipid(raw_headers[raw_index][0], chip));
00436
00437
00438 uves_free_image(&master_bias);
00439 uves_free_propertylist(&master_bias_header);
00440 if (irplib_frameset_find(frames, UVES_MASTER_BIAS(chip)) != NULL)
00441 {
00442 check( uves_load_mbias(frames, chip_name,
00443 &master_bias_filename, &master_bias,
00444 &master_bias_header, chip),
00445 "Error loading master bias");
00446
00447 uves_msg_low("Using master bias in '%s'", master_bias_filename);
00448 }
00449 else
00450 {
00451 uves_msg_low("No master bias in SOF. Bias subtraction not done");
00452 }
00453
00454
00455 uves_free_image(&master_dark);
00456 check( master_dark = uves_mdark_process_chip(raw_images[raw_index],
00457 raw_headers[raw_index],
00458 master_bias,
00459 product_header[raw_index]),
00460 "Error processing chip");
00461
00462
00463
00464 uves_msg("Calculating QC parameters");
00465 uves_qclog_delete(&qclog[0]);
00466 qclog[0] = uves_qclog_init(raw_headers[raw_index][0], chip);
00467 check(uves_mdark_qclog(raw_images[raw_index],
00468 qclog[0]),"error computing qclog");
00469
00470 uves_msg("Saving product");
00471
00472 cpl_free(product_filename);
00473 check( product_filename = uves_masterdark_filename(chip), "Error getting filename");
00474
00475
00476 check( uves_frameset_insert(frames,
00477 master_dark,
00478 CPL_FRAME_GROUP_PRODUCT,
00479 CPL_FRAME_TYPE_IMAGE,
00480 CPL_FRAME_LEVEL_INTERMEDIATE,
00481 product_filename,
00482 product_tag[raw_index],
00483 raw_headers[raw_index][0],
00484 product_header[raw_index],
00485 NULL,
00486 parameters,
00487 recipe_id,
00488 PACKAGE "/" PACKAGE_VERSION,qclog,
00489 starttime, true,
00490 UVES_ALL_STATS),
00491 "Could not add master dark %s to frameset", product_filename);
00492 uves_msg("Master dark %s added to frameset", product_filename);
00493 uves_qclog_delete(&qclog[0]);
00494
00495 }
00496
00497 cleanup:
00498
00499 if (raw_images[0] != NULL)
00500 {
00501 int i;
00502 for (i = 0; i < cpl_imagelist_get_size(raw_images[0]); i++)
00503 {
00504 if (raw_headers[0] != NULL) uves_free_propertylist(&raw_headers[0][i]);
00505 if (raw_headers[1] != NULL) uves_free_propertylist(&raw_headers[1][i]);
00506 }
00507
00508 cpl_free(raw_headers[0]); raw_headers[0] = NULL;
00509 cpl_free(raw_headers[1]); raw_headers[1] = NULL;
00510 }
00511
00512 uves_free_imagelist(&raw_images[0]);
00513 uves_free_imagelist(&raw_images[1]);
00514
00515
00516 uves_free_image(&master_bias);
00517 uves_free_propertylist(&master_bias_header);
00518
00519
00520 uves_qclog_delete(&qclog[0]);
00521 uves_free_image(&master_dark);
00522 uves_free_propertylist(&product_header[0]);
00523 uves_free_propertylist(&product_header[1]);
00524 cpl_free(product_filename);
00525
00526 return;
00527 }
00528
00529
00530 static void uves_mdark_qclog(cpl_imagelist* raw_images,
00531 cpl_table* qclog)
00532 {
00533 int nraw=0;
00534
00535
00536
00537
00538
00539
00540
00541 check_nomsg(nraw=cpl_imagelist_get_size(raw_images));
00542 check_nomsg(uves_qclog_add_int(qclog,
00543 "PRO DATANCOM",
00544 nraw,
00545 "Number of frames combined",
00546 "%d"));
00547 cleanup:
00548 return;
00549 }
00550
00551