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 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030
00035
00040
00041
00042
00043 #include <uves_reduce_mflat_combine.h>
00044
00045 #include <uves.h>
00046 #include <uves_backsub.h>
00047 #include <uves_chip.h>
00048 #include <uves_dfs.h>
00049 #include <uves_pfits.h>
00050 #include <uves_parameters.h>
00051 #include <uves_utils.h>
00052 #include <uves_utils_wrappers.h>
00053 #include <uves_qclog.h>
00054 #include <uves_error.h>
00055 #include <uves_msg.h>
00056
00057 #include <cpl.h>
00058 #include <float.h>
00059 #include <string.h>
00060
00061
00062
00063
00064 static void
00065 uves_reduce_mflat_combine(cpl_frameset *frames,
00066 const cpl_parameterlist *parameters,
00067 bool flames,
00068 const char *recipe_id,
00069 const char *starttime);
00070
00071
00072
00073
00074 const char * const uves_mflat_combine_desc =
00075 "This recipe combined a MASTER_FLAT_xxxx with a MASTER_DFLAT_xxxx\n"
00076 "Input are:\n"
00077 "a master flat (MASTER_FLAT_xxxx)\n"
00078 "a master dflat (MASTER_DFLAT_xxxx)\n"
00079 "an order table (ORDER_TABLE_xxxx)\n"
00080 "provided for each chip (xxxx = BLUE, REDL, REDU). \n"
00081 "Output is a MASTER_FLAT_xxxx\n";
00082
00083
00090
00091 int
00092 uves_mflat_combine_define_parameters_body(cpl_parameterlist *parameters,
00093 const char *recipe_id)
00094 {
00095
00096
00097
00098
00099 if (uves_define_global_parameters(parameters) != CPL_ERROR_NONE)
00100 {
00101 return -1;
00102 }
00103
00104 return (cpl_error_get_code() != CPL_ERROR_NONE);
00105 }
00106
00107
00108
00109
00110
00122
00123 void
00124 uves_mflat_combine_exe_body(cpl_frameset *frames,
00125 const cpl_parameterlist *parameters,
00126 const char *starttime,
00127 const char *recipe_id)
00128 {
00129 bool flames = false;
00130 check_nomsg(uves_reduce_mflat_combine(frames, parameters,flames,recipe_id,
00131 starttime));
00132
00133 cleanup:
00134 return;
00135 }
00136
00137
00153 cpl_image*
00154 uves_combine_flats(const cpl_frame* frm_flat,
00155 const cpl_frame* frm_dflat,
00156 cpl_frameset* frames,
00157 bool flames,
00158 const int ORDER_THRESHOLD)
00159 {
00160
00161
00162 const char * name_flat=NULL ;
00163 const char * name_dflat=NULL ;
00164 const char *ordertable_filename = "";
00165
00166 cpl_image* ima_flat=NULL;
00167 cpl_image* ima_dflat=NULL;
00168 cpl_image* ima_cflat=NULL;
00169 cpl_image* ima_mask=NULL;
00170
00171 int sx=0;
00172 int sy=0;
00173 int j=0;
00174 int i=0;
00175
00176 double* point_mask=NULL;
00177
00178 double xpos=0;
00179 double ypos=0;
00180
00181 double order_ref=(double)ORDER_THRESHOLD;
00182 int ypos_min=0;
00183 int ypos_max=0;
00184 int ypos_cen=0;
00185 int xpos_cen=0;
00186 int xrad=5;
00187 int yrad=5;
00188 int llx=0;
00189 int lly=0;
00190 int urx=0;
00191 int ury=0;
00192 double dflux=0;
00193 double fflux=0;
00194 double scale=0;
00195
00196
00197
00198 cpl_table *ordertable = NULL;
00199 uves_propertylist *ordertable_header = NULL;
00200 polynomial *order_locations = NULL;
00201 cpl_table *traces = NULL;
00202 const char *chip_name = "";
00203 enum uves_chip chip=UVES_CHIP_BLUE;
00204
00205
00206
00207 name_flat=cpl_frame_get_filename(frm_flat);
00208 name_dflat=cpl_frame_get_filename(frm_dflat);
00209 check_nomsg(ima_flat=cpl_image_load(name_flat,CPL_TYPE_DOUBLE,0,0));
00210 check_nomsg(ima_dflat=cpl_image_load(name_dflat,CPL_TYPE_DOUBLE,0,0));
00211
00212
00213 sx=cpl_image_get_size_x(ima_flat);
00214 sy=cpl_image_get_size_y(ima_flat);
00215 assure(sx==cpl_image_get_size_x(ima_dflat),CPL_ERROR_ILLEGAL_INPUT,
00216 "illagal x size");
00217 assure(sy==cpl_image_get_size_y(ima_dflat),CPL_ERROR_ILLEGAL_INPUT,
00218 "illagal y size");
00219
00220
00221
00222 check_nomsg( chip_name = UVES_CHIP_ID(chip));
00223 uves_msg("Combining %s chip", uves_chip_tostring_upper(chip));
00224
00225 check( uves_load_ordertable(frames,
00226 flames,
00227 chip_name,
00228 &ordertable_filename,
00229 &ordertable,
00230 &ordertable_header,
00231 NULL,
00232 &order_locations,
00233 &traces, NULL, NULL,
00234 NULL, NULL,
00235 chip,
00236 false),
00237 "Could not load order table");
00238 uves_msg("Using order table in '%s'", ordertable_filename);
00239
00240
00241 ypos=uves_polynomial_evaluate_2d(order_locations,0,order_ref);
00242 ypos+=uves_polynomial_evaluate_2d(order_locations,0,order_ref+1);
00243 ypos/=2.;
00244 ypos_min=(int)ypos;
00245
00246
00247 ypos=uves_polynomial_evaluate_2d(order_locations,(double)sx,order_ref);
00248 ypos+=uves_polynomial_evaluate_2d(order_locations,(double)sx,order_ref+1);
00249 ypos/=2.;
00250 ypos_max=(int)ypos;
00251
00252 uves_msg_debug("ypos min=%d max=%d",ypos_min,ypos_max);
00253 ima_mask = cpl_image_new(sx,sy,CPL_TYPE_DOUBLE);
00254 point_mask=cpl_image_get_data_double(ima_mask);
00255
00256 for(j=(int)ypos_max;j<sy;j++) {
00257 for(i=0;i<sx;i++) {
00258 point_mask[j*sx+i]=1.;
00259 }
00260 }
00261
00262
00263 for(j=ypos_min;j<ypos_max;j++) {
00264 for(i=0;i<sx;i++) {
00265 xpos=(double)i;
00266
00267
00268
00269
00270
00271 ypos= uves_polynomial_evaluate_2d(order_locations,xpos,order_ref);
00272 ypos+=uves_polynomial_evaluate_2d(order_locations,xpos,order_ref+1);
00273 ypos/=2.;
00274 if(j > ypos) {
00275 point_mask[j*sx+i] = 1.;
00276 }
00277 }
00278 }
00279
00280
00281
00282
00283
00284
00285
00286 xpos_cen=sx/2;
00287 llx=xpos_cen-xrad;
00288 urx=xpos_cen+xrad;
00289 ypos= uves_polynomial_evaluate_2d(order_locations,(double)xpos_cen,
00290 order_ref+1);
00291 ypos_cen=(int)ypos;
00292 lly=ypos_cen-yrad;
00293 ury=ypos_cen+yrad;
00294 fflux=cpl_image_get_median_window(ima_flat,llx,lly,urx,ury);
00295
00296
00297
00298
00299 ypos=uves_polynomial_evaluate_2d(order_locations,(double)xpos_cen,order_ref);
00300 ypos_cen=(int)ypos;
00301 lly=ypos_cen-yrad;
00302 ury=ypos_cen+yrad;
00303 dflux=cpl_image_get_median_window(ima_dflat,llx,lly,urx,ury);
00304
00305
00306 scale=fflux/dflux;
00307
00308 uves_msg_debug("flux: n=%g d=%g s=%g",fflux,dflux,scale);
00309
00310
00311 ima_cflat=cpl_image_duplicate(ima_flat);
00312 cpl_image_multiply(ima_cflat,ima_mask);
00313 cpl_image_multiply_scalar(ima_mask,-1.);
00314 cpl_image_add_scalar(ima_mask,1.);
00315 cpl_image_multiply(ima_dflat,ima_mask);
00316 cpl_image_multiply_scalar(ima_dflat,scale);
00317 cpl_image_add(ima_cflat,ima_dflat);
00318
00319
00320
00321
00322
00323
00324 cleanup:
00325
00326 uves_free_table(&ordertable);
00327 uves_free_propertylist(&ordertable_header);
00328 uves_polynomial_delete(&order_locations);
00329 uves_free_table(&traces);
00330 uves_free_image(&ima_flat);
00331 uves_free_image(&ima_dflat);
00332 uves_free_image(&ima_mask);
00333
00334 return ima_cflat;
00335 }
00336
00346
00347 static void
00348 uves_reduce_mflat_combine(cpl_frameset *frames,
00349 const cpl_parameterlist *parameters,
00350 bool flames,
00351 const char *recipe_id,
00352 const char *starttime)
00353 {
00354 bool DEBUG;
00355 cpl_frame * frm_dflat=NULL ;
00356 cpl_frame * frm_flat=NULL ;
00357 const char * name_flat=NULL ;
00358 const char * name_dflat=NULL ;
00359 uves_propertylist* hflat=NULL;
00360
00361
00362 cpl_table *qclog[] = {NULL, NULL};
00363 uves_propertylist *product_header=NULL;
00364 cpl_image* ima_cflat=NULL;
00365
00366
00367
00368 char *product_filename = NULL;
00369 const char *product_tag[2] = {NULL, NULL};
00370 char pro_filename[255];
00371
00372 const char* PROCESS_CHIP=NULL;
00373 int ORDER_THRESHOLD=7;
00374
00375 enum uves_chip chip;
00376 int raw_index=0;
00377 bool blue=true;
00378
00379
00380 {
00381
00382 check( uves_get_parameter(parameters, NULL, "uves", "debug",
00383 CPL_TYPE_BOOL , &DEBUG ),
00384 "Could not read parameter");
00385 }
00386 check( uves_get_parameter(parameters, NULL, "uves", "process_chip",
00387 CPL_TYPE_STRING, &PROCESS_CHIP),
00388 "Could not read parameter");
00389
00390 uves_string_toupper((char*)PROCESS_CHIP);
00391
00392 check( uves_get_parameter(parameters, NULL, "uves", "order_threshold",
00393 CPL_TYPE_INT, &ORDER_THRESHOLD),
00394 "Could not read parameter");
00395
00396 for (chip = uves_chip_get_first(blue);
00397 chip != UVES_CHIP_INVALID;
00398 chip = uves_chip_get_next(chip))
00399 {
00400 if (
00401 cpl_frameset_find(frames,UVES_MASTER_FLAT(chip)) != NULL ||
00402 cpl_frameset_find(frames,UVES_MASTER_DFLAT(chip)) != NULL
00403 )
00404 {
00405
00406 if(chip == UVES_CHIP_BLUE) {
00407 blue = true;
00408 }
00409 else {
00410 blue = false;
00411 }
00412 } else {
00413 blue = false;
00414 }
00415 }
00416
00417
00418
00419 for (chip = uves_chip_get_first(blue);
00420 chip != UVES_CHIP_INVALID;
00421 chip = uves_chip_get_next(chip))
00422 {
00423
00424 if(strcmp(PROCESS_CHIP,"REDU") == 0) {
00425 chip = uves_chip_get_next(chip);
00426 }
00427
00428 raw_index = uves_chip_get_index(chip);
00429
00430 uves_msg("Processing %s chip", uves_chip_tostring_upper(chip));
00431
00432
00433 frm_flat=cpl_frameset_find(frames,UVES_MASTER_FLAT(chip));
00434 frm_dflat=cpl_frameset_find(frames,UVES_MASTER_DFLAT(chip));
00435
00436 if(frm_flat == NULL && frm_dflat == NULL) {
00437 uves_msg_error("A %s or %s frame or both must be provided in input",
00438 UVES_MASTER_FLAT(chip),UVES_MASTER_DFLAT(chip));
00439 goto cleanup;
00440 }
00441 else if(frm_flat == NULL && frm_dflat != NULL) {
00442 name_dflat=cpl_frame_get_filename(frm_dflat);
00443 hflat=uves_propertylist_load(name_dflat,0);
00444 ima_cflat=cpl_image_load(name_dflat,CPL_TYPE_DOUBLE,0,0);
00445 } else if (frm_flat != NULL && frm_dflat == NULL) {
00446 name_flat=cpl_frame_get_filename(frm_flat);
00447 hflat=uves_propertylist_load(name_flat,0);
00448 ima_cflat=cpl_image_load(name_flat,CPL_TYPE_DOUBLE,0,0);
00449 } else {
00450 check_nomsg(name_dflat=cpl_frame_get_filename(frm_dflat));
00451 check_nomsg(hflat=uves_propertylist_load(name_dflat,0));
00452
00453 check_nomsg(ima_cflat=uves_combine_flats(frm_flat,frm_dflat,frames,
00454 flames,ORDER_THRESHOLD));
00455
00456 }
00457
00458 cpl_free(product_filename);
00459 check( product_filename = uves_masterflat_filename(chip),
00460 "Error getting filename");
00461
00462
00463 sprintf(pro_filename,product_filename);
00464 product_header=uves_propertylist_duplicate(hflat);
00465 product_tag[uves_chip_get_index(chip)] = UVES_MASTER_FLAT(chip);
00466
00467 check( uves_frameset_insert(
00468 frames,
00469 ima_cflat,
00470 CPL_FRAME_GROUP_PRODUCT,
00471 CPL_FRAME_TYPE_IMAGE,
00472 CPL_FRAME_LEVEL_INTERMEDIATE,
00473 pro_filename,
00474 product_tag[raw_index],
00475 hflat,
00476 product_header,
00477 NULL,
00478 parameters,
00479 recipe_id,
00480 PACKAGE "/" PACKAGE_VERSION, qclog,
00481 starttime, true, UVES_ALL_STATS),
00482 "Could not add master flat %s %s to frameset",
00483 pro_filename, product_tag[raw_index]);
00484
00485 uves_msg("Master flat %s %s added to frameset",
00486 pro_filename, product_tag[raw_index]);
00487
00488
00489 if(strcmp(PROCESS_CHIP,"REDL") == 0) {
00490 chip = uves_chip_get_next(chip);
00491 }
00492
00493
00494 uves_free_image(&ima_cflat);
00495 uves_qclog_delete(&qclog[0]);
00496 uves_free_propertylist(&product_header);
00497 uves_free_propertylist(&hflat);
00498
00499 }
00500
00501
00502 cleanup:
00503
00504 uves_free_image(&ima_cflat);
00505 uves_qclog_delete(&qclog[0]);
00506 uves_free_propertylist(&product_header);
00507 uves_free_propertylist(&hflat);
00508 cpl_free(product_filename);
00509
00510 return;
00511 }
00512
00513