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 #include <sinfo_tpl_utils.h>
00044 #include <sinfo_pfits.h>
00045 #include <sinfo_tpl_dfs.h>
00046 #include <sinfo_key_names.h>
00047 #include <sinfo_raw_types.h>
00048 #include <sinfo_pro_types.h>
00049 #include <sinfo_functions.h>
00050 #include <sinfo_msg.h>
00051 #include <sinfo_error.h>
00052 #include <sinfo_utils_wrappers.h>
00053 #include <sinfo_remove_crh_single.h>
00054
00055
00056
00057
00058
00059 static int sinfo_utl_remove_crh_single_create(cpl_plugin *) ;
00060 static int sinfo_utl_remove_crh_single_exec(cpl_plugin *) ;
00061 static int sinfo_utl_remove_crh_single_destroy(cpl_plugin *) ;
00062 static int sinfo_utl_remove_crh_single(cpl_parameterlist *, cpl_frameset *) ;
00063
00064
00065
00066
00067
00068 static char sinfo_utl_remove_crh_single_description[] =
00069 "This recipe performs image computation.\n"
00070 "The input files is one image\n"
00071 "their associated tags should be IMA.\n"
00072 "The output is the image cleaned from CRHs\n"
00073 "Information on relevant parameters can be found with\n"
00074 "esorex --params sinfo_utl_remove_crh_single\n"
00075 "esorex --help sinfo_utl_remove_crh_single\n"
00076 "\n";
00077
00078
00079
00080
00081
00085
00086
00088
00096
00097 int cpl_plugin_get_info(cpl_pluginlist * list)
00098 {
00099 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00100 cpl_plugin * plugin = &recipe->interface ;
00101
00102 cpl_plugin_init(plugin,
00103 CPL_PLUGIN_API,
00104 SINFONI_BINARY_VERSION,
00105 CPL_PLUGIN_TYPE_RECIPE,
00106 "sinfo_utl_remove_crh_single",
00107 "Remove CRHs from an image",
00108 sinfo_utl_remove_crh_single_description,
00109 "Andrea Modigliani",
00110 "Andrea.Modigliani@eso.org",
00111 sinfo_get_license(),
00112 sinfo_utl_remove_crh_single_create,
00113 sinfo_utl_remove_crh_single_exec,
00114 sinfo_utl_remove_crh_single_destroy) ;
00115
00116 cpl_pluginlist_append(list, plugin) ;
00117
00118 return 0;
00119 }
00120
00121
00130
00131 static int sinfo_utl_remove_crh_single_create(cpl_plugin * plugin)
00132 {
00133 cpl_recipe * recipe ;
00134 cpl_parameter * p ;
00135
00136
00137 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00138 recipe = (cpl_recipe *)plugin ;
00139 else return -1 ;
00140 cpl_error_reset();
00141 irplib_reset();
00142
00143
00144 recipe->parameters = cpl_parameterlist_new() ;
00145
00146
00147
00148 p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.crh_frac_max",
00149 CPL_TYPE_DOUBLE,
00150 "Maximum fraction of allowed CRHs",
00151 "sinfoni.sinfo_utl_remove_crh_single",0.7);
00152 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "crh_frac_max") ;
00153 cpl_parameterlist_append(recipe->parameters, p) ;
00154
00155
00156 p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.sigma_lim",
00157 CPL_TYPE_DOUBLE,
00158 "Maximum sigma in kappa-sigma clip",
00159 "sinfoni.sinfo_utl_remove_crh_single", 25.) ;
00160 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sigma_lim") ;
00161 cpl_parameterlist_append(recipe->parameters, p) ;
00162
00163
00164
00165 p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.f_lim",
00166 CPL_TYPE_DOUBLE,
00167 "Max fraction of bad pixels allowed",
00168 "sinfoni.sinfo_utl_remove_crh_single", 0.7) ;
00169 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "f_lim") ;
00170 cpl_parameterlist_append(recipe->parameters, p) ;
00171
00172
00173 p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.max_iter",
00174 CPL_TYPE_INT,
00175 "Max fraction of bad pixels allowed",
00176 "sinfoni.sinfo_utl_remove_crh_single",5) ;
00177 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "max_iter") ;
00178 cpl_parameterlist_append(recipe->parameters, p) ;
00179
00180
00181
00182 p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.gain",
00183 CPL_TYPE_DOUBLE,
00184 "Detector's gain",
00185 "sinfoni.sinfo_utl_remove_crh_single",2.42) ;
00186 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "gain") ;
00187 cpl_parameterlist_append(recipe->parameters, p) ;
00188
00189
00190 p = cpl_parameter_new_value("sinfoni.sinfo_utl_remove_crh_single.ron",
00191 CPL_TYPE_DOUBLE,
00192 "Detector's ron",
00193 "sinfoni.sinfo_utl_remove_crh_single",1.) ;
00194 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ron") ;
00195 cpl_parameterlist_append(recipe->parameters, p) ;
00196
00197
00198
00199
00200
00201 return 0;
00202 }
00203
00204
00210
00211 static int sinfo_utl_remove_crh_single_exec(cpl_plugin * plugin)
00212 {
00213 cpl_recipe * recipe ;
00214 int code=0;
00215 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00216
00217
00218 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00219 recipe = (cpl_recipe *)plugin ;
00220 else return -1 ;
00221 cpl_error_reset();
00222 irplib_reset();
00223 code = sinfo_utl_remove_crh_single(recipe->parameters, recipe->frames) ;
00224
00225
00226 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00227
00228
00229 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00230 }
00231
00232 return code ;
00233 }
00234
00235
00241
00242 static int sinfo_utl_remove_crh_single_destroy(cpl_plugin * plugin)
00243 {
00244 cpl_recipe * recipe ;
00245
00246
00247 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00248 recipe = (cpl_recipe *)plugin ;
00249 else return -1 ;
00250
00251 cpl_parameterlist_delete(recipe->parameters) ;
00252 return 0 ;
00253 }
00254
00255
00262
00263 static int
00264 sinfo_utl_remove_crh_single( cpl_parameterlist * parlist,
00265 cpl_frameset * framelist)
00266 {
00267 cpl_parameter * param= NULL ;
00268 cpl_frameset * raw_on=NULL;
00269 cpl_frameset * raw_off=NULL;
00270 double crh_frac_max=0;
00271 double sigma_lim=0;
00272 double f_lim=0;
00273 int max_iter=0;
00274 double gain=0;
00275 double ron=0;
00276 int nraw=0;
00277 int n=0;
00278 cpl_image* ima_res=NULL;
00279 char name[256];
00280 cpl_image* image=NULL;
00281 cpl_image* img_on=NULL;
00282 cpl_image* img_off=NULL;
00283 cpl_propertylist* plist=NULL;
00284 const char* name_o=NULL;
00285 cpl_frame* product_frame=NULL;
00286 cpl_frame* frame_on=NULL;
00287 cpl_frame* frame_off=NULL;
00288
00289 sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00290 SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00291
00292
00293 check_nomsg(param=cpl_parameterlist_find(parlist,
00294 "sinfoni.sinfo_utl_remove_crh_single.crh_frac_max"));
00295 check_nomsg(crh_frac_max=cpl_parameter_get_double(param));
00296
00297 check_nomsg(param=cpl_parameterlist_find(parlist,
00298 "sinfoni.sinfo_utl_remove_crh_single.sigma_lim"));
00299 check_nomsg(sigma_lim = cpl_parameter_get_double(param)) ;
00300
00301
00302 check_nomsg(param=cpl_parameterlist_find(parlist,
00303 "sinfoni.sinfo_utl_remove_crh_single.f_lim"));
00304 check_nomsg(f_lim = cpl_parameter_get_double(param)) ;
00305
00306 check_nomsg(param=cpl_parameterlist_find(parlist,
00307 "sinfoni.sinfo_utl_remove_crh_single.max_iter"));
00308 check_nomsg(max_iter = cpl_parameter_get_int(param)) ;
00309
00310 check_nomsg(param=cpl_parameterlist_find(parlist,
00311 "sinfoni.sinfo_utl_remove_crh_single.gain"));
00312 check_nomsg(gain = cpl_parameter_get_double(param)) ;
00313
00314
00315 check_nomsg(param=cpl_parameterlist_find(parlist,
00316 "sinfoni.sinfo_utl_remove_crh_single.ron"));
00317 check_nomsg(ron = cpl_parameter_get_double(param)) ;
00318
00319
00320 check(sinfo_dfs_set_groups(framelist),
00321 "Cannot identify RAW and CALIB frames") ;
00322
00323
00324
00325 n=cpl_frameset_get_size(framelist);
00326 if(n<1) {
00327 sinfo_msg_error("Empty input frame list!");
00328 goto cleanup ;
00329 }
00330
00331
00332 check_nomsg(raw_on=cpl_frameset_new());
00333
00334 check(sinfo_contains_frames_kind(framelist,raw_on,RAW_ON),
00335 "Found no input frames with tag %s",RAW_ON);
00336 check_nomsg(nraw=cpl_frameset_get_size(raw_on));
00337 if (nraw<1) {
00338 sinfo_msg_error("Found no input frames with tag %s",RAW_ON);
00339 goto cleanup;
00340 }
00341
00342
00343 check_nomsg(raw_off=cpl_frameset_new());
00344
00345 check(sinfo_contains_frames_kind(framelist,raw_off,RAW_OFF),
00346 "Found no input frames with tag %s",RAW_OFF);
00347 check_nomsg(nraw=cpl_frameset_get_size(raw_off));
00348 if (nraw<1) {
00349 sinfo_msg_error("Found no input frames with tag %s",RAW_OFF);
00350 goto cleanup;
00351 }
00352
00353
00354 check_nomsg(frame_on=cpl_frameset_get_first(raw_on));
00355 check_nomsg(strcpy(name,cpl_frame_get_filename(frame_on)));
00356 check_nomsg(sinfo_free_frameset(&raw_on));
00357 sinfo_msg("name=%s",name);
00358 check_nomsg(img_on=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00359 check(plist=cpl_propertylist_load(name,0),
00360 "Cannot read the FITS header") ;
00361
00362
00363 frame_off=cpl_frameset_get_first(raw_off);
00364 strcpy(name,cpl_frame_get_filename(frame_off));
00365 sinfo_free_frameset(&raw_off);
00366
00367 img_off=cpl_image_load(name,CPL_TYPE_FLOAT,0,0);
00368
00369 image=cpl_image_duplicate(img_on);
00370 sinfo_free_image(&img_on);
00371
00372 cpl_image_subtract(image,img_off);
00373 cpl_image_save(image,"image_with_crh.fits",CPL_BPP_IEEE_FLOAT,NULL,
00374 CPL_IO_DEFAULT);
00375
00376 check(ima_res=sinfo_remove_crh_single(image,crh_frac_max,sigma_lim,f_lim,
00377 max_iter,gain,ron),
00378 "fail to remove CRHs");
00379
00380 sinfo_free_image(&image);
00381
00382
00383
00384
00385
00386 name_o = "ima_res.fits" ;
00387
00388
00389 check_nomsg(product_frame = cpl_frame_new());
00390 check_nomsg(cpl_frame_set_filename(product_frame, name_o)) ;
00391 check_nomsg(cpl_frame_set_tag(product_frame, SI_UTL_IMA_ARITH_PROIMA)) ;
00392 check_nomsg(cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE)) ;
00393 check_nomsg(cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT)) ;
00394 check(cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL),
00395 "Error while initialising the product frame") ;
00396
00397
00398 check_nomsg(cpl_propertylist_erase_regexp(plist, "^ESO PRO CATG",0));
00399 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
00400 check(cpl_dfs_setup_product_header(plist,
00401 product_frame,
00402 framelist,
00403 parlist,
00404 "sinfo_utl_remove_crh_single",
00405 "SINFONI",
00406 KEY_VALUE_HPRO_DID,NULL),
00407 "Problem in the product DFS-compliance") ;
00408 #else
00409 check(cpl_dfs_setup_product_header(plist,
00410 product_frame,
00411 framelist,
00412 parlist,
00413 "sinfo_utl_remove_crh_single",
00414 "SINFONI",
00415 KEY_VALUE_HPRO_DID),
00416 "Problem in the product DFS-compliance") ;
00417 #endif
00418
00419 check(cpl_image_save(ima_res,
00420 name_o,
00421 CPL_BPP_IEEE_FLOAT,
00422 plist,
00423 CPL_IO_DEFAULT),
00424 "Could not save product");
00425 sinfo_free_propertylist(&plist) ;
00426
00427
00428
00429 check_nomsg(cpl_frameset_insert(framelist, product_frame)) ;
00430
00431
00432 cleanup:
00433
00434 sinfo_free_frameset(&raw_on);
00435 sinfo_free_frameset(&raw_off);
00436 sinfo_free_image(&img_on);
00437 sinfo_free_image(&img_off);
00438 sinfo_free_propertylist(&plist) ;
00439
00440
00441
00442 sinfo_free_image(&image) ;
00443
00444 if (cpl_error_get_code()) {
00445 return -1 ;
00446 } else {
00447 return 0 ;
00448 }
00449
00450 }