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 #include <xsh_dfs.h>
00041 #include <xsh_data_pre.h>
00042 #include <xsh_utils.h>
00043 #include <xsh_parameters.h>
00044 #include <xsh_drl.h>
00045 #include <xsh_msg.h>
00046 #include <xsh_pfits.h>
00047 #include <xsh_error.h>
00048
00049
00050
00051
00052
00053
00054
00055 #define XSH_UTL_IMA_ARITH_RECIPE_ID "xsh_util_ima_rebin"
00056 #define XSH_UTL_IMA_ARITH_RECIPE_AUTHOR "A.Modigliani"
00057 #define XSH_UTL_IMA_ARITH_RECIPE_CONTACT "Andrea.Modigliani@eso.org"
00058 #define PRO_IMA "PRO_IMA_UVB"
00059 #define KEY_VALUE_HPRO_DID "PRO-1.15"
00060
00061
00062
00063
00064 static int xsh_util_ima_rebin_create(cpl_plugin *) ;
00065 static int xsh_util_ima_rebin_exec(cpl_plugin *) ;
00066 static int xsh_util_ima_rebin_destroy(cpl_plugin *) ;
00067 static int xsh_util_ima_rebin(cpl_parameterlist *, cpl_frameset *) ;
00068
00069
00070
00071
00072
00073 static char
00074 xsh_util_ima_rebin_description_short[] = "Rebin images";
00075 static char xsh_util_ima_rebin_description[] =
00076 "This recipe performs image rebinning (no flux conservation).\n"
00077 "The input files should be list in an input file (no need to specify a tag)\n"
00078 "The output are resampled images with filenames\n"
00079 "binX_binY_filename.fits, \n"
00080 "where X and Y are the input binx/y parameters values\n"
00081 "Information on relevant parameters can be found with\n"
00082 "esorex --params xsh_util_ima_rebin\n"
00083 "esorex --help xsh_util_ima_rebin\n"
00084 "\n";
00085
00086
00087
00088
00089
00094
00095
00097
00105
00106 int cpl_plugin_get_info(cpl_pluginlist * list)
00107 {
00108 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00109 cpl_plugin * plugin = &recipe->interface ;
00110
00111 cpl_plugin_init(plugin,
00112 CPL_PLUGIN_API,
00113 XSH_BINARY_VERSION,
00114 CPL_PLUGIN_TYPE_RECIPE,
00115 XSH_UTL_IMA_ARITH_RECIPE_ID,
00116 xsh_util_ima_rebin_description_short,
00117 xsh_util_ima_rebin_description,
00118 XSH_UTL_IMA_ARITH_RECIPE_AUTHOR,
00119 XSH_UTL_IMA_ARITH_RECIPE_CONTACT,
00120 xsh_get_license(),
00121 xsh_util_ima_rebin_create,
00122 xsh_util_ima_rebin_exec,
00123 xsh_util_ima_rebin_destroy) ;
00124
00125 cpl_pluginlist_append(list, plugin) ;
00126
00127 return 0;
00128 }
00129
00130
00139
00140 static int xsh_util_ima_rebin_create(cpl_plugin * plugin)
00141 {
00142 cpl_recipe * recipe ;
00143 cpl_parameter * p ;
00144
00145
00146 xsh_init();
00147
00148
00149 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00150 recipe = (cpl_recipe *)plugin ;
00151 else return -1 ;
00152 cpl_error_reset();
00153
00154
00155
00156 recipe->parameters = cpl_parameterlist_new() ;
00157
00158
00159
00160 check( xsh_parameters_generic(XSH_UTL_IMA_ARITH_RECIPE_ID,
00161 recipe->parameters ) ) ;
00162
00163
00164 p = cpl_parameter_new_range("xsh.xsh_util_ima_rebin.binx",
00165 CPL_TYPE_INT,
00166 "Rebinning X factor: pix_size_o/pix_size_i",
00167 "xsh.xsh_util_ima_rebin",1,1,2);
00168 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "binx") ;
00169 cpl_parameterlist_append(recipe->parameters, p) ;
00170
00171
00172
00173 p = cpl_parameter_new_range("xsh.xsh_util_ima_rebin.biny",
00174 CPL_TYPE_INT,
00175 "Rebinning Y factor: pix_size_o/pix_size_i",
00176 "xsh.xsh_util_ima_rebin",1,1,2);
00177 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "biny") ;
00178 cpl_parameterlist_append(recipe->parameters, p) ;
00179
00180 cleanup:
00181
00182
00183 return 0;
00184 }
00185
00186
00192
00193 static int xsh_util_ima_rebin_exec(cpl_plugin * plugin)
00194 {
00195 cpl_recipe * recipe ;
00196 int code=0;
00197 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00198
00199
00200 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00201 recipe = (cpl_recipe *)plugin ;
00202 else return -1 ;
00203 cpl_error_reset();
00204
00205 code = xsh_util_ima_rebin(recipe->parameters, recipe->frames) ;
00206
00207
00208 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00209
00210
00211 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00212 }
00213
00214 return code ;
00215 }
00216
00217
00223
00224 static int xsh_util_ima_rebin_destroy(cpl_plugin * plugin)
00225 {
00226 cpl_recipe * recipe ;
00227
00228
00229 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00230 recipe = (cpl_recipe *)plugin ;
00231 else return -1 ;
00232
00233 cpl_parameterlist_delete(recipe->parameters) ;
00234 return 0 ;
00235 }
00236
00237
00244
00245 static int
00246 xsh_util_ima_rebin( cpl_parameterlist * parlist,
00247 cpl_frameset * frames)
00248 {
00249 cpl_parameter * param= NULL ;
00250 cpl_frame* frm=NULL;
00251 cpl_image* ima_tot=NULL;
00252 cpl_image* ima_pre=NULL;
00253 cpl_image* ima_ove=NULL;
00254 cpl_image* ima_dat=NULL;
00255
00256 cpl_image* ima_datr=NULL;
00257 cpl_image* ima_prer=NULL;
00258 cpl_image* ima_over=NULL;
00259
00260 cpl_image* ima_new=NULL;
00261 cpl_propertylist* plist=NULL;
00262
00263 int binx=1;
00264 int biny=1;
00265 int nfrm=0;
00266 const char* name=NULL;
00267 char new_name[256];
00268 int prscx=0;
00269 int prscy=0;
00270 int ovscx=0;
00271 int ovscy=0;
00272 int nx=0;
00273 int ny=0;
00274
00275 int nxpr=0;
00276 int nypr=0;
00277 int nxdr=0;
00278 int nydr=0;
00279 int nxor=0;
00280 int nyor=0;
00281 double cdelt1=0;
00282 double cdelt2=0;
00283 const char* base_name=NULL;
00284
00285
00286 xsh_msg("Welcome to XSHOOTER Pipeline release %d.%d.%d",
00287 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
00288
00289
00290
00291 check(param=cpl_parameterlist_find(parlist,
00292 "xsh.xsh_util_ima_rebin.binx"));
00293 check(binx=cpl_parameter_get_int(param));
00294
00295
00296 check(param=cpl_parameterlist_find(parlist,
00297 "xsh.xsh_util_ima_rebin.biny"));
00298 check(biny=cpl_parameter_get_int(param));
00299 nfrm=cpl_frameset_get_size(frames);
00300
00301
00302 for (frm = cpl_frameset_get_first(frames);
00303 frm != NULL;
00304 frm = cpl_frameset_get_next(frames)) {
00305 check(name=cpl_frame_get_filename(frm));
00306 check(plist=cpl_propertylist_load(name,0));
00307
00308 check(prscx=xsh_pfits_get_prscx(plist));
00309 check(prscy=xsh_pfits_get_prscy(plist));
00310 check(ovscx=xsh_pfits_get_ovscx(plist));
00311 check(ovscy=xsh_pfits_get_ovscy(plist));
00312
00313 xsh_msg("Prescan: %d,%d Overscan: %d,%d",prscx,prscy,ovscx,ovscy);
00314
00315 if(cpl_propertylist_has(plist,XSH_WIN_BINX) > 0) {
00316 cpl_propertylist_set_int(plist,XSH_WIN_BINX,binx);
00317 } else {
00318 cpl_propertylist_append_int(plist,XSH_WIN_BINX,binx);
00319 }
00320
00321 if(cpl_propertylist_has(plist,XSH_WIN_BINY) > 0) {
00322 cpl_propertylist_set_int(plist,XSH_WIN_BINY,biny);
00323 } else {
00324 cpl_propertylist_append_int(plist,XSH_WIN_BINY,biny);
00325 }
00326
00327
00328 if(cpl_propertylist_has(plist,XSH_CDELT1) > 0) {
00329 check(cdelt1=xsh_pfits_get_cdelt1(plist));
00330 if(cdelt1==1) {
00331 check(cpl_propertylist_set_double(plist,XSH_CDELT1,cdelt1*binx));
00332 }
00333 } else {
00334 if(cdelt1>1) {
00335 cpl_propertylist_append_double(plist,XSH_CDELT1,2);
00336 }
00337 }
00338
00339
00340
00341
00342 if(cpl_propertylist_has(plist,XSH_CDELT2) > 0) {
00343 check(cdelt2=xsh_pfits_get_cdelt2(plist));
00344 if(cdelt2==1) {
00345 check(cpl_propertylist_set_double(plist,XSH_CDELT2,cdelt2*binx));
00346 }
00347 } else {
00348 if(cdelt2>1) {
00349 cpl_propertylist_append_double(plist,XSH_CDELT2,2);
00350 }
00351 }
00352
00353
00354
00355 check(ima_tot=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00356
00357 check(nx=cpl_image_get_size_x(ima_tot));
00358 check(ny=cpl_image_get_size_y(ima_tot));
00359
00360 check(ima_dat=cpl_image_extract(ima_tot,prscx+1,prscy+1,nx-ovscx,ny-ovscy));
00361 check(ima_pre=cpl_image_extract(ima_tot,1,prscy+1,prscx,ny));
00362 check(ima_ove=cpl_image_extract(ima_tot,nx-ovscx+1,prscy+1,nx,ny));
00363 base_name=xsh_get_basename(name);
00364 sprintf(new_name,"pre_binx%d_biny%d_%s",binx,biny,base_name);
00365
00366
00367
00368 sprintf(new_name,"ove_binx%d_biny%d_%s",binx,biny,base_name);
00369
00370
00371
00372 sprintf(new_name,"dat_binx%d_biny%d_%s",binx,biny,base_name);
00373
00374
00375
00376
00377 check(ima_datr=cpl_image_extract_subsample(ima_dat,binx,biny));
00378 check(ima_prer=cpl_image_extract_subsample(ima_pre,binx,biny));
00379 check(ima_over=cpl_image_extract_subsample(ima_ove,binx,biny));
00380
00381 check(nxpr=cpl_image_get_size_x(ima_prer));
00382 check(nypr=cpl_image_get_size_y(ima_prer));
00383 check(nxor=cpl_image_get_size_x(ima_over));
00384 check(nyor=cpl_image_get_size_y(ima_over));
00385 check(nxdr=cpl_image_get_size_x(ima_datr));
00386 check(nydr=cpl_image_get_size_y(ima_datr));
00387
00388 check(ima_new=cpl_image_new(nxpr+nxdr+nxor,ny/2,
00389 CPL_TYPE_FLOAT));
00390 check(cpl_image_copy(ima_new,ima_prer,1,1));
00391 check(cpl_image_copy(ima_new,ima_datr,1+nxpr,1));
00392 check(cpl_image_copy(ima_new,ima_over,1+nxpr+nxdr,1));
00393
00394 sprintf(new_name,"datr_binx%d_biny%d_%s",binx,biny,base_name);
00395
00396
00397
00398 sprintf(new_name,"binx%d_biny%d_%s",binx,biny,base_name);
00399 xsh_msg("new_name=%s",new_name);
00400
00401
00402
00403 check(cpl_image_save(ima_new,new_name,CPL_BPP_IEEE_FLOAT,plist,
00404 CPL_IO_DEFAULT));
00405 xsh_free_image(&ima_tot);
00406 xsh_free_image(&ima_dat);
00407 xsh_free_image(&ima_pre);
00408 xsh_free_image(&ima_ove);
00409 xsh_free_image(&ima_new);
00410 xsh_free_propertylist(&plist);
00411 }
00412
00413 cleanup:
00414 xsh_free_image(&ima_tot);
00415 xsh_free_image(&ima_dat);
00416 xsh_free_image(&ima_pre);
00417 xsh_free_image(&ima_ove);
00418 xsh_free_image(&ima_new);
00419
00420
00421 if (cpl_error_get_code()) {
00422 return -1 ;
00423 } else {
00424 return 0 ;
00425 }
00426
00427 }