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 xsh_parameters_decode_bp(XSH_UTL_IMA_ARITH_RECIPE_ID,recipe->parameters,-1);
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 cpl_propertylist* hext=NULL;
00263
00264 int binx=1;
00265 int biny=1;
00266 int nfrm=0;
00267 const char* name=NULL;
00268 char new_name[256];
00269 int prscx=0;
00270 int prscy=0;
00271 int ovscx=0;
00272 int ovscy=0;
00273 int nx=0;
00274 int ny=0;
00275
00276 int nxpr=0;
00277 int nypr=0;
00278 int nxdr=0;
00279 int nydr=0;
00280 int nxor=0;
00281 int nyor=0;
00282 int next=0;
00283 int k=0;
00284 double cdelt1=0;
00285 double cdelt2=0;
00286 const char* base_name=NULL;
00287
00288
00289 xsh_msg("Welcome to XSHOOTER Pipeline release %d.%d.%d",
00290 XSH_MAJOR_VERSION,XSH_MINOR_VERSION,XSH_MICRO_VERSION);
00291
00292
00293
00294 check(param=cpl_parameterlist_find(parlist,
00295 "xsh.xsh_util_ima_rebin.binx"));
00296 check(binx=cpl_parameter_get_int(param));
00297
00298
00299 check(param=cpl_parameterlist_find(parlist,
00300 "xsh.xsh_util_ima_rebin.biny"));
00301 check(biny=cpl_parameter_get_int(param));
00302 nfrm=cpl_frameset_get_size(frames);
00303
00304
00305 for (frm = cpl_frameset_get_first(frames);
00306 frm != NULL;
00307 frm = cpl_frameset_get_next(frames)) {
00308 check(name=cpl_frame_get_filename(frm));
00309 next=cpl_frame_get_nextensions(frm);
00310 check(plist=cpl_propertylist_load(name,0));
00311
00312 check(prscx=xsh_pfits_get_prscx(plist));
00313 check(prscy=xsh_pfits_get_prscy(plist));
00314 check(ovscx=xsh_pfits_get_ovscx(plist));
00315 check(ovscy=xsh_pfits_get_ovscy(plist));
00316
00317 xsh_msg("Prescan: %d,%d Overscan: %d,%d",prscx,prscy,ovscx,ovscy);
00318
00319 if(cpl_propertylist_has(plist,XSH_WIN_BINX) > 0) {
00320 cpl_propertylist_set_int(plist,XSH_WIN_BINX,binx);
00321 } else {
00322 cpl_propertylist_append_int(plist,XSH_WIN_BINX,binx);
00323 }
00324
00325 if(cpl_propertylist_has(plist,XSH_WIN_BINY) > 0) {
00326 cpl_propertylist_set_int(plist,XSH_WIN_BINY,biny);
00327 } else {
00328 cpl_propertylist_append_int(plist,XSH_WIN_BINY,biny);
00329 }
00330
00331
00332 if(cpl_propertylist_has(plist,XSH_CDELT1) > 0) {
00333 check(cdelt1=xsh_pfits_get_cdelt1(plist));
00334 if(cdelt1==1) {
00335 check(cpl_propertylist_set_double(plist,XSH_CDELT1,cdelt1*binx));
00336 }
00337 } else {
00338 if(cdelt1>1) {
00339 cpl_propertylist_append_double(plist,XSH_CDELT1,2);
00340 }
00341 }
00342
00343
00344
00345
00346 if(cpl_propertylist_has(plist,XSH_CDELT2) > 0) {
00347 check(cdelt2=xsh_pfits_get_cdelt2(plist));
00348 if(cdelt2==1) {
00349 check(cpl_propertylist_set_double(plist,XSH_CDELT2,cdelt2*binx));
00350 }
00351 } else {
00352 if(cdelt2>1) {
00353 cpl_propertylist_append_double(plist,XSH_CDELT2,2);
00354 }
00355 }
00356
00357
00358 for(k=0;k<=next;k++) {
00359 check(ima_tot=cpl_image_load(name,CPL_TYPE_FLOAT,0,k));
00360 check(hext=cpl_propertylist_load(name,k));
00361
00362 check(nx=cpl_image_get_size_x(ima_tot));
00363 check(ny=cpl_image_get_size_y(ima_tot));
00364
00365 check(ima_dat=cpl_image_extract(ima_tot,prscx+1,prscy+1,nx-ovscx,ny-ovscy));
00366 check(ima_pre=cpl_image_extract(ima_tot,1,prscy+1,prscx,ny));
00367 check(ima_ove=cpl_image_extract(ima_tot,nx-ovscx+1,prscy+1,nx,ny));
00368 base_name=xsh_get_basename(name);
00369 sprintf(new_name,"pre_binx%d_biny%d_%s",binx,biny,base_name);
00370
00371
00372
00373 sprintf(new_name,"ove_binx%d_biny%d_%s",binx,biny,base_name);
00374
00375
00376
00377 sprintf(new_name,"dat_binx%d_biny%d_%s",binx,biny,base_name);
00378
00379
00380
00381
00382 check(ima_datr=cpl_image_extract_subsample(ima_dat,binx,biny));
00383 check(ima_prer=cpl_image_extract_subsample(ima_pre,binx,biny));
00384 check(ima_over=cpl_image_extract_subsample(ima_ove,binx,biny));
00385
00386 check(nxpr=cpl_image_get_size_x(ima_prer));
00387 check(nypr=cpl_image_get_size_y(ima_prer));
00388 check(nxor=cpl_image_get_size_x(ima_over));
00389 check(nyor=cpl_image_get_size_y(ima_over));
00390 check(nxdr=cpl_image_get_size_x(ima_datr));
00391 check(nydr=cpl_image_get_size_y(ima_datr));
00392
00393 check(ima_new=cpl_image_new(nxpr+nxdr+nxor,ny/2,
00394 CPL_TYPE_FLOAT));
00395 check(cpl_image_copy(ima_new,ima_prer,1,1));
00396 check(cpl_image_copy(ima_new,ima_datr,1+nxpr,1));
00397 check(cpl_image_copy(ima_new,ima_over,1+nxpr+nxdr,1));
00398
00399 sprintf(new_name,"datr_binx%d_biny%d_%s",binx,biny,base_name);
00400
00401
00402
00403 sprintf(new_name,"binx%d_biny%d_%s",binx,biny,base_name);
00404 xsh_msg("new_name=%s",new_name);
00405
00406
00407
00408 if(k==0) {
00409 check(cpl_image_save(ima_new,new_name,XSH_PRE_DATA_BPP,plist,
00410 CPL_IO_DEFAULT));
00411 } else if (k==1) {
00412 check(cpl_image_save(ima_new,new_name,XSH_PRE_ERRS_BPP,hext,
00413 CPL_IO_EXTEND));
00414 } else if (k==2) {
00415 check(cpl_image_save(ima_new,new_name,XSH_PRE_QUAL_BPP,hext,
00416 CPL_IO_EXTEND));
00417 }
00418 xsh_free_image(&ima_tot);
00419 xsh_free_image(&ima_dat);
00420 xsh_free_image(&ima_pre);
00421 xsh_free_image(&ima_ove);
00422 xsh_free_image(&ima_new);
00423 xsh_free_propertylist(&plist);
00424 xsh_free_propertylist(&hext);
00425 }
00426 }
00427
00428 cleanup:
00429 xsh_free_image(&ima_tot);
00430 xsh_free_image(&ima_dat);
00431 xsh_free_image(&ima_pre);
00432 xsh_free_image(&ima_ove);
00433 xsh_free_image(&ima_new);
00434
00435
00436 if (cpl_error_get_code()) {
00437 return -1 ;
00438 } else {
00439 return 0 ;
00440 }
00441
00442 }