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
00041 #include <irplib_utils.h>
00042
00043
00044
00045
00046
00047
00048
00049 #include <flames_utils_science.h>
00050 #include <uves_dfs.h>
00051 #include <flames_dfs.h>
00052 #include <uves_chip.h>
00053 #include <uves_pfits.h>
00054 #include <uves_msg.h>
00055 #include <uves_error.h>
00056 #include <uves_utils_wrappers.h>
00057
00058 #define recipe_id "flames_utl_unpack"
00059
00060
00061
00062
00063
00064 static int flames_utl_unpack_create(cpl_plugin *) ;
00065 static int flames_utl_unpack_exec(cpl_plugin *) ;
00066 static int flames_utl_unpack_destroy(cpl_plugin *) ;
00067 static int flames_utl_unpack(cpl_parameterlist *, cpl_frameset *) ;
00068
00069
00070 static cpl_error_code
00071 flames_unpack_spectra_from_image(cpl_frame* frm,
00072 cpl_parameterlist * parameters,
00073 cpl_frameset* set);
00074 static cpl_error_code
00075 flames_unpack_image_from_cube(cpl_frame* frm,
00076 cpl_parameterlist * parameters,
00077 cpl_frameset* set);
00078
00079
00080
00081
00082
00083 static char flames_utl_unpack_description[] =
00084 "This recipe unpack flames-uves packed products.\n"
00085 "Information on relevant parameters can be found with\n"
00086 "esorex --params flames_utl_unpack\n"
00087 "esorex --help flames_utl_unpack\n"
00088 "\n";
00089
00090
00091
00092
00093
00097
00098
00100
00108
00109 int cpl_plugin_get_info(cpl_pluginlist * list)
00110 {
00111 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00112 cpl_plugin * plugin = &recipe->interface ;
00113
00114 cpl_plugin_init(plugin,
00115 CPL_PLUGIN_API,
00116 UVES_BINARY_VERSION,
00117 CPL_PLUGIN_TYPE_RECIPE,
00118 "flames_utl_unpack",
00119 "Unpack flames-uves packed frames",
00120 flames_utl_unpack_description,
00121 "Andrea Modigliani",
00122 "Andrea.Modigliani@eso.org",
00123 uves_get_license(),
00124 flames_utl_unpack_create,
00125 flames_utl_unpack_exec,
00126 flames_utl_unpack_destroy) ;
00127
00128 cpl_pluginlist_append(list, plugin) ;
00129
00130 return 0;
00131 }
00132
00133
00142
00143 static int flames_utl_unpack_create(cpl_plugin * plugin)
00144 {
00145 cpl_recipe * recipe ;
00146
00147
00148 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00149 recipe = (cpl_recipe *)plugin ;
00150 else return -1 ;
00151 cpl_error_reset();
00152 irplib_reset();
00153
00154
00155 recipe->parameters = cpl_parameterlist_new() ;
00156
00157
00158
00159
00160
00161 return 0;
00162 }
00163
00164
00170
00171 static int flames_utl_unpack_exec(cpl_plugin * plugin)
00172 {
00173 cpl_recipe * recipe ;
00174 int code=0;
00175 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00176
00177
00178 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00179 recipe = (cpl_recipe *)plugin ;
00180 else return -1 ;
00181 cpl_error_reset();
00182 irplib_reset();
00183 code = flames_utl_unpack(recipe->parameters, recipe->frames) ;
00184
00185
00186 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00187
00188
00189 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00190 }
00191
00192 return code ;
00193 }
00194
00195
00201
00202 static int flames_utl_unpack_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
00222
00223 static int
00224 flames_utl_unpack( cpl_parameterlist * parlist,
00225 cpl_frameset * set)
00226 {
00227 uves_propertylist * plist=NULL ;
00228 int n=0;
00229 int i=0;
00230 int naxis=0;
00231 cpl_frame* frm=NULL;
00232 const char* tag=NULL;
00233 char name[80];
00234 cpl_frameset* out=NULL;
00235
00236 uves_msg("Welcome to UVES Pipeline release %d.%d.%d",
00237 UVES_MAJOR_VERSION,UVES_MINOR_VERSION,UVES_MICRO_VERSION);
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 n=cpl_frameset_get_size(set);
00248 if(n<1) {
00249 uves_msg_error("Empty input frame list!");
00250 goto cleanup ;
00251 }
00252
00253 check_nomsg(out=cpl_frameset_new());
00254 for(i=0;i<n;i++) {
00255 check_nomsg(frm=cpl_frameset_get_frame(set,i));
00256 check_nomsg(tag=cpl_frame_get_tag(frm));
00257 check_nomsg(sprintf(name,"%s",cpl_frame_get_filename(frm)));
00258 check_nomsg(plist=uves_propertylist_load(name,0));
00259 check_nomsg(naxis=uves_pfits_get_naxis(plist));
00260
00261 if(naxis==3) {
00262 check_nomsg(flames_unpack_image_from_cube(frm,parlist,out));
00263 } else if (naxis==2) {
00264 check_nomsg(flames_unpack_spectra_from_image(frm,parlist,out));
00265
00266 }
00267
00268 uves_free_propertylist(&plist);
00269 }
00270
00271
00272 cleanup:
00273
00274
00275 if (cpl_error_get_code()) {
00276 return -1 ;
00277 } else {
00278 return 0 ;
00279 }
00280
00281 }
00282
00283
00284 static cpl_error_code
00285 flames_unpack_image_from_cube(cpl_frame* frm,
00286 cpl_parameterlist * parameters,
00287 cpl_frameset* set)
00288 {
00289
00290 const char* name=NULL;
00291 char fname[80];
00292 const char* tag=NULL;
00293 uves_propertylist* href=NULL;
00294 uves_propertylist* hout=NULL;
00295 cpl_imagelist* iml=NULL;
00296 int size=0;
00297 int i=0;
00298 cpl_frame* frame=NULL;
00299 cpl_image* img=NULL;
00300
00301 check_nomsg(name=cpl_frame_get_filename(frm));
00302 check_nomsg(tag=cpl_frame_get_tag(frm));
00303 check_nomsg(href=uves_propertylist_load(name,0));
00304 check_nomsg(hout=uves_propertylist_duplicate(href));
00305
00306 check_nomsg(iml=cpl_imagelist_load(name,CPL_TYPE_FLOAT,0));
00307 check_nomsg(size=cpl_imagelist_get_size(iml));
00308 for(i=0;i<size;i++) {
00309 frame=cpl_frame_new();
00310 check_nomsg(img=cpl_imagelist_get(iml,i));
00311 sprintf(fname,"%s%d%s%s","slic_",i,"_",name);
00312 check_nomsg( uves_save_image(img,fname,hout,true, true) );
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 }
00331
00332
00333 cleanup:
00334 uves_free_propertylist(&href);
00335 uves_free_propertylist(&hout);
00336 uves_free_imagelist(&iml);
00337
00338
00339 return cpl_error_get_code() ;
00340
00341 }
00342
00343
00344
00345 static cpl_error_code
00346 flames_unpack_spectra_from_image(cpl_frame* frm,
00347 cpl_parameterlist * parameters,
00348 cpl_frameset* set)
00349 {
00350
00351
00352
00353 const char* name=NULL;
00354 char fname[80];
00355 const char* tag=NULL;
00356 cpl_propertylist* href=NULL;
00357 cpl_propertylist* hout=NULL;
00358 cpl_image* ima=NULL;
00359
00360 int i=0;
00361 cpl_frame* frame=NULL;
00362 cpl_image* img=NULL;
00363 int sx=0;
00364 int sy=0;
00365
00366 check_nomsg(name=cpl_frame_get_filename(frm));
00367 check_nomsg(tag=cpl_frame_get_tag(frm));
00368 check_nomsg(href=cpl_propertylist_load(name,0));
00369 check_nomsg(hout=cpl_propertylist_duplicate(href));
00370
00371 check_nomsg(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00372 check_nomsg(sx=cpl_image_get_size_x(ima));
00373 check_nomsg(sy=cpl_image_get_size_y(ima));
00374
00375 for(i=0;i<sy;i++) {
00376 frame=cpl_frame_new();
00377 img=cpl_image_new(sx,1,CPL_TYPE_FLOAT);
00378 check_nomsg(cpl_image_copy(img,cpl_image_extract(ima,1,i+1,sx,i+1),1,1));
00379 sprintf(fname,"%s%d%s%s","slic_",i,"_",name);
00380 cpl_image_save(img,fname,CPL_BPP_IEEE_FLOAT,hout,CPL_IO_DEFAULT);
00381
00382
00383
00384
00385
00386 check_nomsg(cpl_frame_set_filename(frame, fname)) ;
00387 check_nomsg(cpl_frame_set_tag(frame, tag)) ;
00388 check_nomsg(cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE)) ;
00389 check_nomsg(cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT)) ;
00390 check(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL),
00391 "Error while initialising the product frame") ;
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 uves_free_image(&img);
00422
00423 }
00424
00425
00426 cleanup:
00427 cpl_propertylist_delete(href);
00428 cpl_propertylist_delete(hout);
00429 uves_free_image(&ima);
00430
00431 return cpl_error_get_code() ;
00432
00433
00434 }
00435
00436
00437
00438