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
00040 #include "sinfoni_tpl_utils.h"
00041 #include "sinfoni_pfits.h"
00042 #include "sinfoni_tpl_dfs.h"
00043 #include "sinfoni_raw_types.h"
00044 #include "sinfoni_pro_types.h"
00045 #include "sinfoni_functions.h"
00046 #include <sinfoni_key_names.h>
00047 #include <sinfoni_memory.h>
00048
00049
00050
00051
00052
00053 static int si_utl_skymap_create(cpl_plugin *) ;
00054 static int si_utl_skymap_exec(cpl_plugin *) ;
00055 static int si_utl_skymap_destroy(cpl_plugin *) ;
00056 static int si_utl_skymap(cpl_parameterlist *, cpl_frameset *) ;
00057
00058
00059
00060
00061 static char si_utl_skymap_description[] =
00062 "This recipe flags as bad pixels sky lines.\n"
00063 "Input are sky frames with tag SKY\n"
00064 "Output image is called out_skymap.fits\n"
00065 "Information on relevant parameters can be found with\n"
00066 "esorex --params si_utl_skymap\n"
00067 "esorex --help si_utl_skymap\n"
00068 "\n";
00069
00070
00071
00072
00073
00074
00082
00083 int cpl_plugin_get_info(cpl_pluginlist * list)
00084 {
00085 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00086 cpl_plugin * plugin = &recipe->interface ;
00087
00088 cpl_plugin_init(plugin,
00089 CPL_PLUGIN_API,
00090 SINFONI_BINARY_VERSION,
00091 CPL_PLUGIN_TYPE_RECIPE,
00092 "si_utl_skymap",
00093 "Flags sky lines as bad pixels, with map generation",
00094 si_utl_skymap_description,
00095 "Andrea Modigliani",
00096 "Andrea.Modigliani@eso.org",
00097 sinfoni_get_license(),
00098 si_utl_skymap_create,
00099 si_utl_skymap_exec,
00100 si_utl_skymap_destroy) ;
00101
00102 cpl_pluginlist_append(list, plugin) ;
00103
00104 return 0;
00105 }
00106
00107
00116
00117 static int si_utl_skymap_create(cpl_plugin * plugin)
00118 {
00119 cpl_recipe * recipe ;
00120 cpl_parameter * p ;
00121
00122
00123 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00124 recipe = (cpl_recipe *)plugin ;
00125 else return -1 ;
00126
00127
00128 recipe->parameters = cpl_parameterlist_new() ;
00129
00130
00131
00132
00133
00134 p = cpl_parameter_new_range("sinfoni.si_utl_skymap.xsize",
00135 CPL_TYPE_INT, "X box size", "sinfoni.si_utl_skymap", 1,1,2047) ;
00136 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xsize") ;
00137 cpl_parameterlist_append(recipe->parameters, p) ;
00138
00139
00140 p = cpl_parameter_new_range("sinfoni.si_utl_skymap.ysize",
00141 CPL_TYPE_INT, "Y box size", "sinfoni.si_utl_skymap", 30,1,2047) ;
00142 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "ysize") ;
00143 cpl_parameterlist_append(recipe->parameters, p) ;
00144
00145
00146
00147
00148 p = cpl_parameter_new_value("sinfoni.si_utl_skymap.threshold",
00149 CPL_TYPE_DOUBLE, "Threshold", "sinfoni.si_utl_skymap", 30.) ;
00150 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "thresh") ;
00151 cpl_parameterlist_append(recipe->parameters, p) ;
00152
00153
00154 return 0;
00155 }
00156
00157
00163
00164 static int si_utl_skymap_exec(cpl_plugin * plugin)
00165 {
00166 cpl_recipe * recipe ;
00167
00168
00169 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00170 recipe = (cpl_recipe *)plugin ;
00171 else return -1 ;
00172
00173 return si_utl_skymap(recipe->parameters, recipe->frames) ;
00174 }
00175
00176
00182
00183 static int si_utl_skymap_destroy(cpl_plugin * plugin)
00184 {
00185 cpl_recipe * recipe ;
00186
00187
00188 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00189 recipe = (cpl_recipe *)plugin ;
00190 else return -1 ;
00191
00192 cpl_parameterlist_delete(recipe->parameters) ;
00193 return 0 ;
00194 }
00195
00196 static int si_utl_skymap(
00197 cpl_parameterlist * parlist,
00198 cpl_frameset * framelist)
00199 {
00200 const char * fctid = "si_utl_skymap" ;
00201 cpl_parameter * param =NULL;
00202 const char * name_i=NULL;
00203 int xsize=0;
00204 int ysize=0;
00205
00206 cpl_frame * sky_frm=NULL;
00207
00208 const char * name_o=NULL ;
00209 cpl_propertylist * plist =NULL;
00210 cpl_frame * product_frame=NULL;
00211 cpl_frameset * sky_set=NULL;
00212 cpl_image * sky_ima=NULL;
00213 cpl_image * sky_map=NULL;
00214 double threshold=0;
00215 int i=0;
00216 int j=0;
00217 double median=0;
00218 float* sky_ima_pix=NULL;
00219 float* sky_map_pix=NULL;
00220 int nx=0;
00221 int ny=0;
00222 int n=0;
00223
00224
00225
00226 param = cpl_parameterlist_find(parlist, "sinfoni.si_utl_skymap.out_filename");
00227 name_o = "out_skymap.fits";
00228
00229
00230 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_skymap.xsize");
00231 xsize = cpl_parameter_get_int(param) ;
00232
00233
00234 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_skymap.ysize");
00235 ysize = cpl_parameter_get_int(param) ;
00236
00237 param = cpl_parameterlist_find(parlist,"sinfoni.si_utl_skymap.threshold");
00238 threshold = cpl_parameter_get_double(param) ;
00239
00240
00241 if (sinfoni_dfs_set_groups(framelist)) {
00242 cpl_msg_error(fctid, "Cannot identify RAW and CALIB frames") ;
00243 return -1 ;
00244 }
00245
00246
00247 n=cpl_frameset_get_size(framelist);
00248 if(n<1) {
00249 cpl_msg_error(fctid,"Empty input frame list!");
00250 return -1;
00251 }
00252 sky_set=cpl_frameset_new();
00253 sinfoni_extract_frames_type(framelist,sky_set,RAW_SKY);
00254
00255 n=cpl_frameset_get_size(framelist);
00256 if(n<1) {
00257 cpl_msg_error(fctid,"No sky frames in input list!");
00258 cpl_frameset_delete(sky_set);
00259 return -1;
00260 }
00261
00262
00263 sky_frm = cpl_frameset_get_frame(sky_set,0);
00264
00265 if ((plist=cpl_propertylist_load(cpl_frame_get_filename(sky_frm),
00266 0)) == NULL) {
00267 cpl_msg_error(fctid, "Cannot read the FITS header") ;
00268 return -1 ;
00269 }
00270
00271
00272 name_i=cpl_frame_get_filename(sky_frm);
00273 sky_ima = cpl_image_load(name_i,CPL_TYPE_FLOAT,0,0);
00274 sky_map=cpl_image_duplicate(sky_ima);
00275 sky_ima_pix=cpl_image_get_data(sky_ima);
00276 sky_map_pix=cpl_image_get_data(sky_map);
00277 nx = cpl_image_get_size_x(sky_ima);
00278 ny = cpl_image_get_size_y(sky_ima);
00279
00280 for(i=1;i<nx;i++) {
00281
00282 for(j=ysize+1;j<ny-ysize;j++) {
00283
00284 median=cpl_image_get_median_window(sky_ima,i,j-ysize,i,j+xsize);
00285 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00286 cpl_msg_error(fctid,"Exit");
00287 cpl_image_delete(sky_ima);
00288 cpl_image_delete(sky_map);
00289 cpl_propertylist_delete(plist);
00290 cpl_frameset_delete(sky_set);
00291 return -1;
00292 }
00293 if(sky_ima_pix[i+j*nx] > median+threshold) {
00294 sky_map_pix[i+j*nx]=0.;
00295 } else {
00296 sky_map_pix[i+j*nx]=1.;
00297 }
00298 }
00299
00300 }
00301
00302
00303
00304 for(i=1;i<nx;i++) {
00305
00306 for(j=0;j<ysize+1;j++) {
00307
00308 sky_map_pix[i+j*nx]=0.;
00309
00310 }
00311
00312 for(j=ny-ysize+1;j<ny;j++) {
00313
00314 sky_map_pix[i+j*nx]=0.;
00315
00316 }
00317
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 name_o = "sky_map.fits" ;
00330
00331
00332 product_frame = cpl_frame_new();
00333 cpl_frame_set_filename(product_frame, name_o) ;
00334 cpl_frame_set_tag(product_frame, PRO_SKY_DUMMY) ;
00335 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_IMAGE) ;
00336 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT) ;
00337 cpl_frame_set_level(product_frame, CPL_FRAME_LEVEL_FINAL) ;
00338
00339 if (cpl_error_get_code()) {
00340 cpl_msg_error(fctid, "Error while initialising the product frame") ;
00341 cpl_propertylist_delete(plist) ;
00342 cpl_frame_delete(product_frame) ;
00343 return -1 ;
00344 }
00345
00346
00347
00348 if (cpl_dfs_setup_product_header(plist, product_frame, framelist, parlist,
00349 "si_utl_skymap", "SINFONI", KEY_VALUE_HPRO_DID) != CPL_ERROR_NONE) {
00350 cpl_msg_error(fctid, "Problem in the product DFS-compliance") ;
00351 cpl_propertylist_delete(plist) ;
00352 cpl_frame_delete(product_frame) ;
00353 cpl_image_delete(sky_ima);
00354 cpl_image_delete(sky_map);
00355 cpl_frameset_delete(sky_set);
00356 return -1 ;
00357 }
00358
00359
00360
00361
00362 if (cpl_image_save(sky_map, name_o, CPL_BPP_DEFAULT, plist,
00363 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00364 cpl_msg_error(fctid, "Could not save product");
00365 cpl_propertylist_delete(plist) ;
00366 cpl_frame_delete(product_frame) ;
00367 cpl_image_delete(sky_map) ;
00368 return -1 ;
00369 }
00370 cpl_propertylist_delete(plist) ;
00371 cpl_image_delete(sky_map) ;
00372 cpl_image_delete(sky_ima) ;
00373
00374
00375 cpl_frameset_insert(framelist, product_frame) ;
00376 cpl_frameset_delete(sky_set);
00377
00378
00379
00380
00381
00382 if (cpl_error_get_code())
00383 return -1 ;
00384 else
00385 return 0 ;
00386 }