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 map coaddition.\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 cpl_error_reset();
00169 irplib_reset();
00170
00171
00172
00173
00174
00175 cleanup:
00176 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
00177 xsh_error_dump(CPL_MSG_ERROR);
00178 return 1;
00179 }
00180 else {
00181 return 0;
00182 }
00183
00184 }
00185
00186
00192 static int
00193 xsh_util_bpmap_coadd_exec(cpl_plugin *plugin)
00194 {
00195
00196
00197 cpl_recipe *recipe = (cpl_recipe *) plugin;
00198 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00199 int code=0;
00200
00201 if(recipe->parameters == NULL ) {
00202 return 1;
00203 }
00204 if(recipe->frames == NULL) {
00205 return 1;
00206 }
00207
00208 check(code=xsh_util_bpmap_coadd(recipe->parameters, recipe->frames));
00209
00210 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00211
00212
00213 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00214 }
00215 cleanup:
00216
00217 return code;
00218
00219 }
00220
00221
00228 static int
00229 xsh_util_bpmap_coadd_destroy(cpl_plugin *plugin)
00230 {
00231 cpl_recipe *recipe = (cpl_recipe *) plugin;
00232
00233
00234
00235
00236
00237
00238 cpl_parameterlist_delete(recipe->parameters);
00239 return 0;
00240
00241 }
00242
00243
00244
00245
00246
00247
00248
00262 static int
00263 xsh_util_bpmap_coadd(cpl_parameterlist *parameters, cpl_frameset *sof)
00264 {
00265 const char* recipe_tags[1] = {"xsh_util_bpmap_coadd"};
00266 int recipe_tags_size = 1;
00267 cpl_frameset* bpf_set=NULL;
00268 int n=0;
00269 cpl_frame* frm=NULL;
00270 cpl_frame* frm_tmp=NULL;
00271 char name[80];
00272 char tag_pro[80];
00273 const char* tag=NULL;
00274 const char* filename=NULL;
00275 int found=false;
00276 int i=0;
00277 xsh_instrument* instrument=NULL;
00278 cpl_frameset* calib=0;
00279 cpl_frameset* raws=0;
00280 cpl_frame* msk_frm=NULL;
00281 cpl_propertylist* plist=NULL;
00282 cpl_image* ima=NULL;
00283
00284 xsh_msg("Welcome to Shooter Pipeline release %d.%d.%d",
00285 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
00286
00287 check( xsh_begin( sof, parameters, &instrument, &raws, &calib,
00288 recipe_tags, recipe_tags_size, RECIPE_ID,
00289 XSH_BINARY_VERSION,
00290 xsh_util_bpmap_coadd_description_short ) ) ;
00291
00292 if(xsh_dfs_set_groups(sof)) {
00293 xsh_msg_error("Cannot indentify RAW and CALIB frames") ;
00294 }
00295
00296 n=cpl_frameset_get_size(calib);
00297 if(n<1) {
00298 xsh_msg_error("Empty input frame list!");
00299 goto cleanup ;
00300 }
00301 bpf_set=cpl_frameset_new();
00302 for(i=0;i<n;i++) {
00303 found=false;
00304 frm=cpl_frameset_get_frame(calib,i);
00305 tag=cpl_frame_get_tag(frm);
00306 if(strstr(tag,XSH_BP_MAP_REF) != NULL) {
00307 found=true;
00308 } else if(strstr(tag,XSH_BP_MAP_NL) != NULL) {
00309 found=true;
00310 } else if(strstr(tag,XSH_BP_MAP_RP) != NULL) {
00311 found=true;
00312 } else if(strstr(tag,XSH_BP_MAP_HP) != NULL) {
00313 found=true;
00314 } else if(strstr(tag,XSH_BP_MAP_CP) != NULL) {
00315 found=true;
00316 } else if(strstr(tag,XSH_BP_MAP_NP) != NULL) {
00317 found=true;
00318 } else if(strstr(tag,XSH_BP_MAP_PN) != NULL) {
00319 found=true;
00320 } else if(strstr(tag,XSH_MASTER_BP_MAP) != NULL) {
00321 found=true;
00322 } else {
00323 xsh_msg_warning("Frame with tag %s is not a supported bad pixel map",
00324 tag);
00325 }
00326 if(found) {
00327 cpl_frameset_insert(bpf_set,cpl_frame_duplicate(frm));
00328 }
00329 }
00330 n=cpl_frameset_get_size(bpf_set);
00331 for(i=0;i<n;i++) {
00332 if(i==0) {
00333 frm=cpl_frameset_get_frame(bpf_set,i);
00334 } else {
00335 frm_tmp=cpl_frameset_get_frame(bpf_set,i);
00336 xsh_msg("coadd %s with %s",
00337 cpl_frame_get_tag(frm),cpl_frame_get_tag(frm_tmp));
00338
00339 xsh_badpixelmap_coadd(frm,frm_tmp);
00340 }
00341 }
00342
00343 sprintf(tag_pro,"%s_%s",XSH_MASTER_BP_MAP,
00344 xsh_instrument_arm_tostring(instrument));
00345 sprintf(name,"%s.fits",tag_pro);
00346 filename=cpl_frame_get_filename(frm);
00347 ima=cpl_image_load(filename,CPL_TYPE_FLOAT,0,0);
00348 plist=cpl_propertylist_load(filename,0);
00349 xsh_pfits_set_pcatg(plist,tag_pro);
00350 check(cpl_image_save(ima,name,CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
00351
00352 check(msk_frm=xsh_frame_product(name,tag_pro,CPL_FRAME_TYPE_IMAGE,
00353 CPL_FRAME_GROUP_PRODUCT,
00354 CPL_FRAME_LEVEL_FINAL));
00355
00356 check( xsh_add_product_image(msk_frm, sof,
00357 parameters, RECIPE_ID, instrument,
00358 cpl_frame_get_tag(msk_frm)));
00359
00360 cleanup:
00361 xsh_free_frameset(&bpf_set);
00362 xsh_free_image(&ima);
00363 xsh_free_propertylist(&plist);
00364
00365 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00366 return -1;
00367 } else {
00368 return 0;
00369 }
00370
00371
00372
00373 }