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 #ifdef HAVE_CONFIG_H
00035 #include <config.h>
00036 #endif
00037
00038
00039 #include <strings.h>
00040 #include <string.h>
00041 #include <stdio.h>
00042
00043
00044 #include <cpl.h>
00045
00046 #include <xsh_irplib_utils.h>
00047
00048
00049 #include <xsh_dfs.h>
00050 #include <xsh_pfits.h>
00051 #include <xsh_msg.h>
00052 #include <xsh_error.h>
00053 #include <xsh_utils_wrappers.h>
00054 #include <xsh_parameters.h>
00055 #include <xsh_badpixelmap.h>
00056
00057
00058
00059
00060
00061 #define RECIPE_ID "xsh_util_bpmap_coadd"
00062 #define RECIPE_AUTHOR "A.Modigliani"
00063 #define RECIPE_CONTACT "amodigli@eso.org"
00064
00065
00066
00067
00068
00069 const char * xsh_get_licence(void);
00070 static int xsh_util_bpmap_coadd_create(cpl_plugin *plugin);
00071 static int xsh_util_bpmap_coadd_exec(cpl_plugin *plugin);
00072 static int xsh_util_bpmap_coadd_destroy(cpl_plugin *plugin);
00073 static int xsh_util_bpmap_coadd(cpl_parameterlist *, cpl_frameset *);
00074
00075
00076
00077
00078
00079 static char xsh_util_bpmap_coadd_description[] =
00080 "This recipe performs bad pixel maps combination via bitwise OR.\n"
00081 "The input files are several (at least 2) bad pixel masks in the sof file\n"
00082 "Their tab should contain the string BP_MAP.\n"
00083 "The output is an image resulting from the logical operator OR \n"
00084 "applied to all the masks.\n"
00085 "Information on relevant parameters can be found with\n"
00086 "esorex --params xsh_util_bpmap_coadd\n"
00087 "esorex --help xsh_util_bpmap_coadd\n"
00088 "\n";
00089
00090 static char xsh_util_bpmap_coadd_description_short[] ="Add bad pixels masks";
00091
00092
00093
00094
00095
00096
00100
00102
00103
00104 int
00105 cpl_plugin_get_info(cpl_pluginlist *list)
00106 {
00107
00108 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00109 cpl_plugin *plugin = &recipe->interface;
00110
00111
00112 cpl_plugin_init(plugin,
00113 CPL_PLUGIN_API,
00114 XSH_BINARY_VERSION,
00115 CPL_PLUGIN_TYPE_RECIPE,
00116 "xsh_util_bpmap_coadd",
00117 xsh_util_bpmap_coadd_description_short,
00118 xsh_util_bpmap_coadd_description,
00119 "Andrea Modigliani",
00120 "Andrea.Modigliani@eso.org",
00121 xsh_get_license(),
00122 xsh_util_bpmap_coadd_create,
00123 xsh_util_bpmap_coadd_exec,
00124 xsh_util_bpmap_coadd_destroy);
00125
00126 cpl_pluginlist_append(list, plugin);
00127
00128 return 0;
00129
00130 }
00131
00132
00139 static int
00140 xsh_util_bpmap_coadd_create(cpl_plugin *plugin)
00141 {
00142 cpl_recipe* recipe=NULL;
00143
00144
00145
00146
00147
00148
00149
00150
00151 xsh_init();
00152
00153
00154 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
00155
00156
00157 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
00158 CPL_ERROR_TYPE_MISMATCH,
00159 "Plugin is not a recipe");
00160
00161 recipe = (cpl_recipe *)plugin;
00162
00163 recipe->parameters = cpl_parameterlist_new();
00164 if(recipe->parameters == NULL) {
00165 return 1;
00166 }
00167 xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ;
00168 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
00169 cpl_error_reset();
00170 irplib_reset();
00171
00172
00173
00174
00175
00176 cleanup:
00177 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00178 xsh_error_dump(CPL_MSG_ERROR);
00179 return 1;
00180 }
00181 else {
00182 return 0;
00183 }
00184
00185 }
00186
00187
00193 static int
00194 xsh_util_bpmap_coadd_exec(cpl_plugin *plugin)
00195 {
00196
00197
00198 cpl_recipe *recipe = (cpl_recipe *) plugin;
00199 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00200 int code=0;
00201
00202 if(recipe->parameters == NULL ) {
00203 return 1;
00204 }
00205 if(recipe->frames == NULL) {
00206 return 1;
00207 }
00208
00209 check(code=xsh_util_bpmap_coadd(recipe->parameters, recipe->frames));
00210
00211 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00212
00213
00214 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00215 }
00216 cleanup:
00217
00218 return code;
00219
00220 }
00221
00222
00229 static int
00230 xsh_util_bpmap_coadd_destroy(cpl_plugin *plugin)
00231 {
00232 cpl_recipe *recipe = (cpl_recipe *) plugin;
00233
00234
00235
00236
00237
00238
00239 cpl_parameterlist_delete(recipe->parameters);
00240 return 0;
00241
00242 }
00243
00244
00245
00246
00247
00248
00249
00263 static int
00264 xsh_util_bpmap_coadd(cpl_parameterlist *parameters, cpl_frameset *sof)
00265 {
00266 const char* recipe_tags[1] = {"xsh_util_bpmap_coadd"};
00267 int recipe_tags_size = 1;
00268 cpl_frameset* bpf_set=NULL;
00269 int n=0;
00270 cpl_frame* frm=NULL;
00271 cpl_frame* frm_tmp=NULL;
00272 char name[256];
00273 char tag_pro[256];
00274 const char* tag=NULL;
00275 const char* filename=NULL;
00276 int found=false;
00277 int i=0;
00278 xsh_instrument* instrument=NULL;
00279 cpl_frameset* calib=0;
00280 cpl_frameset* raws=0;
00281 cpl_frame* msk_frm=NULL;
00282 cpl_propertylist* plist=NULL;
00283 cpl_image* ima=NULL;
00284
00285 xsh_msg("Welcome to Shooter Pipeline release %d.%d.%d",
00286 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
00287
00288 check( xsh_begin( sof, parameters, &instrument, &raws, &calib,
00289 recipe_tags, recipe_tags_size, RECIPE_ID,
00290 XSH_BINARY_VERSION,
00291 xsh_util_bpmap_coadd_description_short ) ) ;
00292
00293 if(xsh_dfs_set_groups(sof)) {
00294 xsh_msg_error("Cannot indentify RAW and CALIB frames") ;
00295 }
00296
00297 n=cpl_frameset_get_size(calib);
00298 if(n<1) {
00299 xsh_msg_error("Empty input frame list!");
00300 goto cleanup ;
00301 }
00302 bpf_set=cpl_frameset_new();
00303 for(i=0;i<n;i++) {
00304 found=false;
00305 frm=cpl_frameset_get_frame(calib,i);
00306 tag=cpl_frame_get_tag(frm);
00307 if(strstr(tag,XSH_BP_MAP_REF) != NULL) {
00308 found=true;
00309 } else if(strstr(tag,XSH_BP_MAP_NL) != NULL) {
00310 found=true;
00311 } else if(strstr(tag,XSH_BP_MAP_RP) != NULL) {
00312 found=true;
00313 } else if(strstr(tag,XSH_BP_MAP_HP) != NULL) {
00314 found=true;
00315 } else if(strstr(tag,XSH_BP_MAP_CP) != NULL) {
00316 found=true;
00317 } else if(strstr(tag,XSH_BP_MAP_NP) != NULL) {
00318 found=true;
00319 } else if(strstr(tag,XSH_BP_MAP_DP) != NULL) {
00320 found=true;
00321 } else if(strstr(tag,XSH_BP_MAP_SP) != NULL) {
00322 found=true;
00323 } else if(strstr(tag,XSH_BP_MAP_PN) != NULL) {
00324 found=true;
00325 } else if(strstr(tag,XSH_MASTER_BP_MAP_NLIN) != NULL) {
00326 found=true;
00327 } else if(strstr(tag,XSH_MASTER_BP_MAP_BIAS) != NULL) {
00328 found=true;
00329 } else if(strstr(tag,XSH_MASTER_BP_MAP_DARK) != NULL) {
00330 found=true;
00331 } else if(strstr(tag,XSH_MASTER_BP_MAP_FLAT) != NULL) {
00332 found=true;
00333 } else if(strstr(tag,XSH_MASTER_BP_MAP) != NULL) {
00334 found=true;
00335 } else {
00336 xsh_msg_warning("Frame with tag %s is not a supported bad pixel map",
00337 tag);
00338 }
00339 if(found) {
00340 cpl_frameset_insert(bpf_set,cpl_frame_duplicate(frm));
00341 }
00342 }
00343 n=cpl_frameset_get_size(bpf_set);
00344 for(i=0;i<n;i++) {
00345 if(i==0) {
00346 frm=cpl_frameset_get_frame(bpf_set,i);
00347 } else {
00348 frm_tmp=cpl_frameset_get_frame(bpf_set,i);
00349 xsh_msg("combine %s with %s via bitwise OR",
00350 cpl_frame_get_tag(frm),cpl_frame_get_tag(frm_tmp));
00351
00352 xsh_badpixelmap_coadd(frm,frm_tmp);
00353 }
00354 }
00355
00356 sprintf(tag_pro,"%s_%s",XSH_MASTER_BP_MAP,
00357 xsh_instrument_arm_tostring(instrument));
00358 sprintf(name,"%s.fits",tag_pro);
00359 filename=cpl_frame_get_filename(frm);
00360 ima=cpl_image_load(filename,CPL_TYPE_FLOAT,0,0);
00361 plist=cpl_propertylist_load(filename,0);
00362 xsh_pfits_set_pcatg(plist,tag_pro);
00363
00364
00365 check(msk_frm=xsh_frame_product(name,tag_pro,CPL_FRAME_TYPE_IMAGE,
00366 CPL_FRAME_GROUP_PRODUCT,
00367 CPL_FRAME_LEVEL_FINAL));
00368
00369 check( xsh_add_product_image(msk_frm, sof,
00370 parameters, RECIPE_ID, instrument,NULL));
00371
00372 cleanup:
00373 xsh_free_frameset(&bpf_set);
00374 xsh_free_image(&ima);
00375 xsh_free_propertylist(&plist);
00376
00377 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00378 return -1;
00379 } else {
00380 return 0;
00381 }
00382
00383
00384
00385 }