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
00036
00037 #include <cpl.h>
00038
00039 #include <irplib_utils.h>
00040
00041
00042 #include <sinfo_tpl_utils.h>
00043 #include <sinfo_pfits.h>
00044 #include <sinfo_tpl_dfs.h>
00045 #include <sinfo_raw_types.h>
00046 #include <sinfo_pro_types.h>
00047 #include <sinfo_functions.h>
00048 #include <sinfo_key_names.h>
00049 #include <sinfo_msg.h>
00050 #include <sinfo_error.h>
00051 #include <sinfo_utils_wrappers.h>
00052
00053
00054
00055
00056 static int sinfo_utl_skymap_create(cpl_plugin *) ;
00057 static int sinfo_utl_skymap_exec(cpl_plugin *) ;
00058 static int sinfo_utl_skymap_destroy(cpl_plugin *) ;
00059 static int sinfo_utl_skymap(cpl_parameterlist *, cpl_frameset *) ;
00060
00061
00062
00063
00064 static char sinfo_utl_skymap_description[] =
00065 "This recipe flags as bad pixels sky lines.\n"
00066 "Input are sky frames with tag SKY\n"
00067 "Output image is called out_skymap.fits\n"
00068 "Information on relevant parameters can be found with\n"
00069 "esorex --params sinfo_utl_skymap\n"
00070 "esorex --help sinfo_utl_skymap\n"
00071 "\n";
00072
00073
00074
00075
00076
00080
00081
00083
00091
00092 int cpl_plugin_get_info(cpl_pluginlist * list)
00093 {
00094 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00095 cpl_plugin * plugin = &recipe->interface ;
00096
00097 cpl_plugin_init(plugin,
00098 CPL_PLUGIN_API,
00099 SINFONI_BINARY_VERSION,
00100 CPL_PLUGIN_TYPE_RECIPE,
00101 "sinfo_utl_skymap",
00102 "Flags sky lines as bad pixels, with map generation",
00103 sinfo_utl_skymap_description,
00104 "Andrea Modigliani",
00105 "Andrea.Modigliani@eso.org",
00106 sinfo_get_license(),
00107 sinfo_utl_skymap_create,
00108 sinfo_utl_skymap_exec,
00109 sinfo_utl_skymap_destroy) ;
00110
00111 cpl_pluginlist_append(list, plugin) ;
00112
00113 return 0;
00114 }
00115
00116
00125
00126 static int sinfo_utl_skymap_create(cpl_plugin * plugin)
00127 {
00128 cpl_recipe * recipe ;
00129 cpl_parameter * p ;
00130
00131
00132 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00133 recipe = (cpl_recipe *)plugin ;
00134 else return -1 ;
00135
00136
00137 recipe->parameters = cpl_parameterlist_new() ;
00138
00139
00140
00141
00142
00143 p = cpl_parameter_new_range("sinfoni.sinfo_utl_skymap.xsize",
00144 CPL_TYPE_INT, "X box size", "sinfoni.sinfo_utl_skymap", 1,1,2047) ;
00145 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xsize") ;
00146 cpl_parameterlist_append(recipe->parameters, p) ;
00147
00148
00149 p = cpl_parameter_new_range("sinfoni.sinfo_utl_skymap.ysize",
00150 CPL_TYPE_INT, "Y box size", "sinfoni.sinfo_utl_skymap", 30,1,2047) ;
00151 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ysize") ;
00152 cpl_parameterlist_append(recipe->parameters, p) ;
00153
00154
00155
00156
00157 p = cpl_parameter_new_value("sinfoni.sinfo_utl_skymap.threshold",
00158 CPL_TYPE_DOUBLE, "Threshold", "sinfoni.sinfo_utl_skymap", 30.) ;
00159 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "thresh") ;
00160 cpl_parameterlist_append(recipe->parameters, p) ;
00161
00162
00163 return 0;
00164 }
00165
00166
00172
00173 static int sinfo_utl_skymap_exec(cpl_plugin * plugin)
00174 {
00175 cpl_recipe * recipe ;
00176 int code=0;
00177 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00178
00179
00180 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00181 recipe = (cpl_recipe *)plugin ;
00182 else return -1 ;
00183 cpl_error_reset();
00184
00185 check_nomsg(code = sinfo_utl_skymap(recipe->parameters, recipe->frames)) ;
00186 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00187
00188
00189 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00190 }
00191 cleanup:
00192 return code ;
00193 }
00194
00195
00201
00202 static int sinfo_utl_skymap_destroy(cpl_plugin * plugin)
00203 {
00204 cpl_recipe * recipe ;
00205
00206
00207 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00208 recipe = (cpl_recipe *)plugin ;
00209 else return -1 ;
00210
00211 cpl_parameterlist_delete(recipe->parameters) ;
00212 return 0 ;
00213 }
00214
00215 static int sinfo_utl_skymap(
00216 cpl_parameterlist * parlist,
00217 cpl_frameset * framelist)
00218 {
00219 cpl_parameter * param =NULL;
00220 const char * name_i=NULL;
00221 int xsize=0;
00222 int ysize=0;
00223
00224 cpl_frame * sky_frm=NULL;
00225
00226 const char * name_o=NULL ;
00227 cpl_propertylist * plist =NULL;
00228 cpl_frame * product_frame=NULL;
00229 cpl_frameset * sky_set=NULL;
00230 cpl_image * sky_ima=NULL;
00231 cpl_image * sky_map=NULL;
00232 double threshold=0;
00233 int i=0;
00234 int j=0;
00235 double sinfo_median=0;
00236 float* sky_ima_pix=NULL;
00237 float* sky_map_pix=NULL;
00238 int nx=0;
00239 int ny=0;
00240 int n=0;
00241
00242
00243
00244
00245 sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00246 SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00247
00248 param = cpl_parameterlist_find(parlist,
00249 "sinfoni.sinfo_utl_skymap.out_filename");
00250 name_o = "out_skymap.fits";
00251
00252
00253 param = cpl_parameterlist_find(parlist,"sinfoni.sinfo_utl_skymap.xsize");
00254 xsize = cpl_parameter_get_int(param) ;
00255
00256
00257 param = cpl_parameterlist_find(parlist,"sinfoni.sinfo_utl_skymap.ysize");
00258 ysize = cpl_parameter_get_int(param) ;
00259
00260 param = cpl_parameterlist_find(parlist,
00261 "sinfoni.sinfo_utl_skymap.threshold");
00262 threshold = cpl_parameter_get_double(param) ;
00263
00264
00265 if (sinfo_dfs_set_groups(framelist)) {
00266 sinfo_msg_error("Cannot identify RAW and CALIB frames") ;
00267 return -1 ;
00268 }
00269
00270
00271 n=cpl_frameset_get_size(framelist);
00272 if(n<1) {
00273 sinfo_msg_error("Empty input frame list!");
00274 return -1;
00275 }
00276 sky_set=cpl_frameset_new();
00277 sinfo_extract_frames_type(framelist,sky_set,RAW_SKY);
00278
00279 n=cpl_frameset_get_size(framelist);
00280 if(n<1) {
00281 sinfo_msg_error("No sky frames in input list!");
00282 sinfo_free_frameset(&sky_set);
00283 return -1;
00284 }
00285
00286
00287 check_nomsg(sky_frm = cpl_frameset_get_frame(sky_set,0));
00288
00289 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(sky_frm),
00290 0)) == NULL) {
00291 sinfo_msg_error("Cannot read the FITS header") ;
00292 return -1 ;
00293 }
00294
00295
00296 name_i=cpl_frame_get_filename(sky_frm);
00297 sky_ima = cpl_image_load(name_i,CPL_TYPE_FLOAT,0,0);
00298 sky_map=cpl_image_duplicate(sky_ima);
00299 sky_ima_pix=cpl_image_get_data(sky_ima);
00300 sky_map_pix=cpl_image_get_data(sky_map);
00301 nx = cpl_image_get_size_x(sky_ima);
00302 ny = cpl_image_get_size_y(sky_ima);
00303 if (nx != SIZEX || ny != SIZEY) {
00304 sinfo_msg_error("nx=%d ny=%d, expected nx=%d ny=%d",nx,ny,SIZEX,SIZEY);
00305 goto cleanup;
00306
00307 }
00308 for(i=1;i<nx;i++) {
00309
00310 for(j=ysize+1;j<ny-ysize;j++) {
00311
00312 sinfo_median=cpl_image_get_median_window(sky_ima,i,j-ysize,i,j+xsize);
00313 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00314 sinfo_msg_error("Exit");
00315 sinfo_free_image(&sky_ima);
00316 sinfo_free_image(&sky_map);
00317 sinfo_free_propertylist(&plist);
00318 sinfo_free_frameset(&sky_set);
00319 return -1;
00320 }
00321 if(sky_ima_pix[i+j*nx] > sinfo_median+threshold) {
00322 sky_map_pix[i+j*nx]=0.;
00323 } else {
00324 sky_map_pix[i+j*nx]=1.;
00325 }
00326 }
00327
00328 }
00329
00330
00331
00332 for(i=1;i<nx;i++) {
00333
00334 for(j=0;j<ysize+1;j++) {
00335
00336 sky_map_pix[i+j*nx]=0.;
00337
00338 }
00339
00340 for(j=ny-ysize+1;j<ny;j++) {
00341
00342 sky_map_pix[i+j*nx]=0.;
00343
00344 }
00345
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 name_o = "sky_map.fits" ;
00358
00359
00360 product_frame = cpl_frame_new();
00361 cpl_frame_set_filename(product_frame, name_o) ;
00362 cpl_frame_set_tag(product_frame, PRO_SKY_DUMMY) ;
00363 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00364 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00365 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00366
00367 if (cpl_error_get_code()) {
00368 sinfo_msg_error("Error while initialising the product frame") ;
00369 sinfo_free_propertylist(&plist) ;
00370 sinfo_free_frame(&product_frame) ;
00371 return -1 ;
00372 }
00373
00374
00375 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
00376 if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00377 "sinfo_utl_skymap",
00378 "SINFONI", KEY_VALUE_HPRO_DID,NULL)
00379 != CPL_ERROR_NONE) {
00380 sinfo_msg_error("Problem in the product DFS-compliance") ;
00381 sinfo_free_propertylist(&plist) ;
00382 sinfo_free_frame(&product_frame) ;
00383 sinfo_free_image(&sky_ima);
00384 sinfo_free_image(&sky_map);
00385 sinfo_free_frameset(&sky_set);
00386 return -1 ;
00387 }
00388 #else
00389 if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00390 "sinfo_utl_skymap",
00391 "SINFONI", KEY_VALUE_HPRO_DID)
00392 != CPL_ERROR_NONE) {
00393 sinfo_msg_error("Problem in the product DFS-compliance") ;
00394 sinfo_free_propertylist(&plist) ;
00395 sinfo_free_frame(&product_frame) ;
00396 sinfo_free_image(&sky_ima);
00397 sinfo_free_image(&sky_map);
00398 sinfo_free_frameset(&sky_set);
00399 return -1 ;
00400 }
00401 #endif
00402
00403
00404
00405 if (cpl_image_save(sky_map, name_o, CPL_BPP_IEEE_FLOAT, plist,
00406 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00407 sinfo_msg_error("Could not save product");
00408 sinfo_free_propertylist(&plist) ;
00409 sinfo_free_frame(&product_frame) ;
00410 sinfo_free_image(&sky_map) ;
00411 return -1 ;
00412 }
00413 sinfo_free_propertylist(&plist) ;
00414 sinfo_free_image(&sky_map) ;
00415 sinfo_free_image(&sky_ima) ;
00416
00417
00418 cpl_frameset_insert(framelist, product_frame) ;
00419 sinfo_free_frameset(&sky_set);
00420
00421
00422
00423 cleanup:
00424 sinfo_free_image(&sky_ima);
00425 sinfo_free_image(&sky_map);
00426 sinfo_free_propertylist(&plist);
00427 sinfo_free_frameset(&sky_set);
00428
00429 if( cpl_error_get_code()!=CPL_ERROR_NONE) {
00430 return -1 ;
00431 } else {
00432 return 0 ;
00433 }
00434 }