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