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 #include <string.h>
00036
00037
00038 #include <cpl.h>
00039
00040
00041 #include <irplib_utils.h>
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #include <uves_dfs.h>
00053 #include <uves_msg.h>
00054 #include <uves_error.h>
00055 #include <uves_utils_wrappers.h>
00056 #include <uves_remove_crh_single.h>
00057 #include <uves_cpl_size.h>
00058
00059
00060
00061
00062
00063 static int uves_utl_remove_crh_single_create(cpl_plugin *) ;
00064 static int uves_utl_remove_crh_single_exec(cpl_plugin *) ;
00065 static int uves_utl_remove_crh_single_destroy(cpl_plugin *) ;
00066 static int uves_utl_remove_crh_single(cpl_parameterlist *, cpl_frameset *) ;
00067
00068
00069
00070
00071
00072 static char uves_utl_remove_crh_single_description[] =
00073 "This recipe performs image computation.\n"
00074 "The input files is one image\n"
00075 "their associated tags should be IMA.\n"
00076 "The output is the image cleaned from CRHs\n"
00077 "Information on relevant parameters can be found with\n"
00078 "esorex --params uves_utl_remove_crh_single\n"
00079 "esorex --help uves_utl_remove_crh_single\n"
00080 "\n";
00081
00082
00083
00084
00085
00089
00090
00092
00100
00101 int cpl_plugin_get_info(cpl_pluginlist * list)
00102 {
00103 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00104 cpl_plugin * plugin = &recipe->interface ;
00105
00106 cpl_plugin_init(plugin,
00107 CPL_PLUGIN_API,
00108 UVES_BINARY_VERSION,
00109 CPL_PLUGIN_TYPE_RECIPE,
00110 "uves_utl_remove_crh_single",
00111 "Remove CRHs from an image",
00112 uves_utl_remove_crh_single_description,
00113 "Andrea Modigliani",
00114 "Andrea.Modigliani@eso.org",
00115 uves_get_license(),
00116 uves_utl_remove_crh_single_create,
00117 uves_utl_remove_crh_single_exec,
00118 uves_utl_remove_crh_single_destroy) ;
00119
00120 cpl_pluginlist_append(list, plugin) ;
00121
00122 return 0;
00123 }
00124
00125
00134
00135 static int uves_utl_remove_crh_single_create(cpl_plugin * plugin)
00136 {
00137 cpl_recipe * recipe ;
00138 cpl_parameter * p ;
00139
00140
00141 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00142 recipe = (cpl_recipe *)plugin ;
00143 else return -1 ;
00144 cpl_error_reset();
00145 irplib_reset();
00146
00147
00148 recipe->parameters = cpl_parameterlist_new() ;
00149
00150
00151
00152 p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.crh_frac_max",
00153 CPL_TYPE_DOUBLE,
00154 "Maximum fraction of allowed CRHs",
00155 "uves.uves_utl_remove_crh_single",0.7);
00156 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "crh_frac_max") ;
00157 cpl_parameterlist_append(recipe->parameters, p) ;
00158
00159
00160 p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.sigma_lim",
00161 CPL_TYPE_DOUBLE,
00162 "Maximum sigma in kappa-sigma clip",
00163 "uves.uves_utl_remove_crh_single", 25.) ;
00164 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sigma_lim") ;
00165 cpl_parameterlist_append(recipe->parameters, p) ;
00166
00167
00168
00169 p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.f_lim",
00170 CPL_TYPE_DOUBLE,
00171 "Max fraction of bad pixels allowed",
00172 "uves.uves_utl_remove_crh_single", 0.7) ;
00173 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "f_lim") ;
00174 cpl_parameterlist_append(recipe->parameters, p) ;
00175
00176
00177 p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.max_iter",
00178 CPL_TYPE_INT,
00179 "Max fraction of bad pixels allowed",
00180 "uves.uves_utl_remove_crh_single",5) ;
00181 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "max_iter") ;
00182 cpl_parameterlist_append(recipe->parameters, p) ;
00183
00184
00185
00186 p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.gain",
00187 CPL_TYPE_DOUBLE,
00188 "Detector's gain",
00189 "uves.uves_utl_remove_crh_single",2.42) ;
00190 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "gain") ;
00191 cpl_parameterlist_append(recipe->parameters, p) ;
00192
00193
00194 p = cpl_parameter_new_value("uves.uves_utl_remove_crh_single.ron",
00195 CPL_TYPE_DOUBLE,
00196 "Detector's ron",
00197 "uves.uves_utl_remove_crh_single",1.) ;
00198 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ron") ;
00199 cpl_parameterlist_append(recipe->parameters, p) ;
00200
00201
00202
00203
00204
00205 return 0;
00206 }
00207
00208
00214
00215 static int uves_utl_remove_crh_single_exec(cpl_plugin * plugin)
00216 {
00217 cpl_recipe * recipe ;
00218 int code=0;
00219 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00220
00221
00222 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00223 recipe = (cpl_recipe *)plugin ;
00224 else return -1 ;
00225 cpl_error_reset();
00226 irplib_reset();
00227 code = uves_utl_remove_crh_single(recipe->parameters, recipe->frames) ;
00228
00229
00230 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00231
00232
00233 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00234 }
00235
00236 return code ;
00237 }
00238
00239
00245
00246 static int uves_utl_remove_crh_single_destroy(cpl_plugin * plugin)
00247 {
00248 cpl_recipe * recipe ;
00249
00250
00251 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00252 recipe = (cpl_recipe *)plugin ;
00253 else return -1 ;
00254
00255 cpl_parameterlist_delete(recipe->parameters) ;
00256 return 0 ;
00257 }
00258
00259
00266
00267 static int
00268 uves_utl_remove_crh_single( cpl_parameterlist * parlist,
00269 cpl_frameset * framelist)
00270 {
00271 cpl_parameter * param= NULL ;
00272 cpl_frameset * raw_on=NULL;
00273 cpl_frameset * raw_off=NULL;
00274 double crh_frac_max=0;
00275 double sigma_lim=0;
00276 double f_lim=0;
00277 int max_iter=0;
00278 double gain=0;
00279 double ron=0;
00280 int nraw=0;
00281
00282 cpl_image* ima_res=NULL;
00283 cpl_image* ima_on=NULL;
00284 cpl_image* ima_off=NULL;
00285
00286
00287 cpl_propertylist* plist1=NULL;
00288 cpl_propertylist* plist2=NULL;
00289 cpl_propertylist* pliste=NULL;
00290
00291 const char* name_r="ima_res.fits";
00292
00293 cpl_frame* product_frame=NULL;
00294 cpl_frame* frame_on=NULL;
00295 cpl_frame* frame_off=NULL;
00296 cpl_size next1=0;
00297 const char* name1=NULL;
00298
00299 cpl_size next2=0;
00300 const char* name2=NULL;
00301
00302 int noff=0;
00303 int chips=0;
00304 int nfrm=0;
00305 int i=0;
00306
00307
00308 uves_msg("Welcome to UVES Pipeline release %d.%d.%d",
00309 UVES_MAJOR_VERSION,UVES_MINOR_VERSION,UVES_MICRO_VERSION);
00310
00311
00312 check_nomsg(param=cpl_parameterlist_find(parlist,
00313 "uves.uves_utl_remove_crh_single.crh_frac_max"));
00314 check_nomsg(crh_frac_max=cpl_parameter_get_double(param));
00315
00316 check_nomsg(param=cpl_parameterlist_find(parlist,
00317 "uves.uves_utl_remove_crh_single.sigma_lim"));
00318 check_nomsg(sigma_lim = cpl_parameter_get_double(param)) ;
00319
00320
00321 check_nomsg(param=cpl_parameterlist_find(parlist,
00322 "uves.uves_utl_remove_crh_single.f_lim"));
00323 check_nomsg(f_lim = cpl_parameter_get_double(param)) ;
00324
00325 check_nomsg(param=cpl_parameterlist_find(parlist,
00326 "uves.uves_utl_remove_crh_single.max_iter"));
00327 check_nomsg(max_iter = cpl_parameter_get_int(param)) ;
00328
00329 check_nomsg(param=cpl_parameterlist_find(parlist,
00330 "uves.uves_utl_remove_crh_single.gain"));
00331 check_nomsg(gain = cpl_parameter_get_double(param)) ;
00332
00333
00334 check_nomsg(param=cpl_parameterlist_find(parlist,
00335 "uves.uves_utl_remove_crh_single.ron"));
00336 check_nomsg(ron = cpl_parameter_get_double(param)) ;
00337
00338
00339
00340
00341
00342 check(uves_dfs_set_groups(framelist),
00343 "Cannot identify RAW and CALIB frames") ;
00344
00345
00346
00347 nfrm=cpl_frameset_get_size(framelist);
00348 if(nfrm<1) {
00349 uves_msg_error("Empty input frame list!");
00350 goto cleanup ;
00351 }
00352
00353
00354 check_nomsg(raw_on=cpl_frameset_new());
00355
00356 check(uves_contains_frames_kind(framelist,raw_on,"RAW_IMA"),
00357 "Found no input frames with tag %s","RAW_IMA");
00358 check_nomsg(nraw=cpl_frameset_get_size(raw_on));
00359 if (nraw<1) {
00360 uves_msg_error("Found no input frames with tag %s","RAW_IMA");
00361 goto cleanup;
00362 }
00363
00364 uves_msg("nraw=%d",nraw);
00365
00366 check_nomsg(frame_on=cpl_frameset_get_first(raw_on));
00367 check_nomsg(next1=cpl_frame_get_nextensions(frame_on));
00368 check_nomsg(name1=cpl_frame_get_filename(frame_on));
00369 check_nomsg(plist1=cpl_propertylist_load(name1,0));
00370 uves_msg("CRH affected file name =%s",name1);
00371
00372
00373
00374 if (nfrm>1) {
00375
00376 check_nomsg(raw_off=cpl_frameset_new());
00377
00378 chips=cpl_propertylist_get_int(plist1,"ESO DET CHIPS");
00379
00380
00381 if(chips==2) {
00382 check(uves_contains_frames_kind(framelist,raw_off,"BIAS_RED"),
00383 "Found no input frames with tag %s","BIAS_RED");
00384 } else {
00385 check(uves_contains_frames_kind(framelist,raw_off,"BIAS_BLUE"),
00386 "Found no input frames with tag %s","BIAS_BLUE");
00387 }
00388
00389 check_nomsg(noff=cpl_frameset_get_size(raw_off));
00390 if (noff<1) {
00391 uves_msg_error("Found no input bias frames");
00392
00393 } else {
00394
00395 frame_off=cpl_frameset_get_first(raw_off);
00396 next2=cpl_frame_get_nextensions(frame_off);
00397
00398 if(next2 != next1) {
00399 uves_msg_error("Raw frames with different number of extensions");
00400 uves_msg_error("Something wrong! Exit");
00401 goto cleanup;
00402 }
00403 name2=cpl_frame_get_filename(frame_off);
00404 uves_msg("Bias file name =%s",name2);
00405 check_nomsg(cpl_image_save(NULL, name_r,CPL_BPP_IEEE_FLOAT,
00406 plist1,CPL_IO_DEFAULT));
00407
00408
00409 if(next1==0) {
00410
00411
00412
00413
00414
00415
00416 check_nomsg(ima_on=cpl_image_load(name1,CPL_TYPE_FLOAT,0,0));
00417 check_nomsg(ima_off=cpl_image_load(name2,CPL_TYPE_FLOAT,0,0));
00418 check_nomsg(cpl_image_subtract(ima_on,ima_off));
00419
00420
00421 cpl_image_save(ima_on,"image_with_crh.fits",CPL_BPP_IEEE_FLOAT,
00422 NULL,CPL_IO_DEFAULT);
00423
00424
00425 check(ima_res=uves_remove_crh_single(ima_on,crh_frac_max,
00426 sigma_lim,f_lim,
00427 max_iter,gain,ron),
00428 "fail to remove CRHs");
00429
00430 check_nomsg(cpl_image_add(ima_res,ima_off));
00431
00432 check_nomsg(cpl_image_save(ima_res, name_r,CPL_BPP_IEEE_FLOAT,
00433 plist1,CPL_IO_DEFAULT));
00434 } else {
00435 uves_msg("next=%" CPL_SIZE_FORMAT "",next1);
00436
00437
00438
00439
00440
00441
00442
00443
00444 for(i=1;i<=next1;i++) {
00445 uves_msg("name1=%s",name1);
00446 uves_msg("name2=%s",name2);
00447 check_nomsg(ima_on=cpl_image_load(name1,CPL_TYPE_FLOAT,0,i));
00448
00449
00450 check_nomsg(pliste=cpl_propertylist_load(name1,i));
00451
00452 if(next2==0) {
00453 check_nomsg(ima_off=cpl_image_load(name2,CPL_TYPE_FLOAT,0,0));
00454
00455 } else {
00456 check_nomsg(ima_off=cpl_image_load(name2,CPL_TYPE_FLOAT,0,i));
00457
00458 }
00459 uves_msg("ima_on=%p ima_off=%p",ima_on,ima_off);
00460 check_nomsg(cpl_image_subtract(ima_on,ima_off));
00461
00462 cpl_image_save(ima_on,"image_with_crh.fits",CPL_BPP_IEEE_FLOAT,
00463 NULL,CPL_IO_DEFAULT);
00464
00465 check(ima_res=uves_remove_crh_single(ima_on,crh_frac_max,
00466 sigma_lim,f_lim,
00467 max_iter,gain,ron),
00468 "fail to remove CRHs");
00469
00470 check_nomsg(cpl_image_add(ima_res,ima_off));
00471
00472
00473 if(i>0) {
00474 check_nomsg(cpl_image_save(ima_res, name_r,CPL_BPP_IEEE_FLOAT,
00475 pliste,CPL_IO_EXTEND));
00476 }
00477 uves_free_image(&ima_on);
00478 uves_free_image(&ima_off);
00479 uves_free_image(&ima_res);
00480 cpl_propertylist_delete(pliste); pliste=NULL;
00481
00482
00483 }
00484
00485
00486 }
00487 }
00488
00489 uves_free_frameset(&raw_off);
00490 uves_free_frameset(&raw_on);
00491
00492
00493 } else {
00494 uves_msg("Please, provide a bias frame. Exit.");
00495 goto cleanup;
00496 }
00497
00498
00499
00500
00501
00502
00503
00504
00505 check_nomsg(product_frame = cpl_frame_new());
00506 check_nomsg(cpl_frame_set_filename(product_frame, name_r)) ;
00507 check_nomsg(cpl_frame_set_tag(product_frame, "PRODUCT")) ;
00508 check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00509 check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00510 check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00511 "Error while initialising the product frame") ;
00512
00513
00514 check_nomsg(cpl_propertylist_erase_regexp(plist1, "^ESO PRO CATG",0));
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533 cpl_propertylist_delete(plist1) ; plist1=NULL;
00534
00535
00536 check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00537
00538
00539 cleanup:
00540 uves_free_frameset(&raw_off);
00541 uves_free_image(&ima_on);
00542 uves_free_image(&ima_off);
00543 uves_free_image(&ima_res);
00544
00545 if(pliste!=NULL) cpl_propertylist_delete(pliste); pliste=NULL;
00546
00547 if (plist1!=NULL) cpl_propertylist_delete(plist1);plist1=NULL;
00548 if (plist2!=NULL) cpl_propertylist_delete(plist2);plist2=NULL;
00549
00550
00551 if (cpl_error_get_code()) {
00552 return -1 ;
00553 } else {
00554 return 0 ;
00555 }
00556
00557 }