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 <flames_utils.h>
00051 #include <uves_dfs.h>
00052 #include <flames_dfs.h>
00053 #include <uves_chip.h>
00054 #include <uves_pfits.h>
00055 #include <uves_msg.h>
00056 #include <uves_error.h>
00057 #include <uves_utils_wrappers.h>
00058
00059 #define recipe_id "flames_utl_unpack"
00060
00061
00062
00063
00064
00065 static int flames_utl_unpack_create(cpl_plugin *) ;
00066 static int flames_utl_unpack_exec(cpl_plugin *) ;
00067 static int flames_utl_unpack_destroy(cpl_plugin *) ;
00068 static int flames_utl_unpack(cpl_parameterlist *, cpl_frameset *) ;
00069
00070
00071 static cpl_error_code
00072 flames_unpack_spectra_from_image(cpl_frame* frm,
00073 cpl_parameterlist * parameters,
00074 cpl_frameset* set);
00075 static cpl_error_code
00076 flames_unpack_image_from_cube(cpl_frame* frm,
00077 cpl_parameterlist * parameters,
00078 cpl_frameset* set);
00079
00080
00081
00082
00083
00084 static char flames_utl_unpack_description[] =
00085 "This recipe unpack flames-uves packed products.\n"
00086 "Information on relevant parameters can be found with\n"
00087 "esorex --params flames_utl_unpack\n"
00088 "esorex --help flames_utl_unpack\n"
00089 "\n";
00090
00091
00092
00093
00094
00098
00099
00101
00109
00110 int cpl_plugin_get_info(cpl_pluginlist * list)
00111 {
00112 cpl_recipe * recipe = cpl_calloc(1, sizeof *recipe ) ;
00113 cpl_plugin * plugin = &recipe->interface ;
00114
00115 cpl_plugin_init(plugin,
00116 CPL_PLUGIN_API,
00117 UVES_BINARY_VERSION,
00118 CPL_PLUGIN_TYPE_RECIPE,
00119 "flames_utl_unpack",
00120 "Unpack flames-uves packed frames",
00121 flames_utl_unpack_description,
00122 "Andrea Modigliani",
00123 "Andrea.Modigliani@eso.org",
00124 uves_get_license(),
00125 flames_utl_unpack_create,
00126 flames_utl_unpack_exec,
00127 flames_utl_unpack_destroy) ;
00128
00129 cpl_pluginlist_append(list, plugin) ;
00130
00131 return 0;
00132 }
00133
00134
00143
00144 static int flames_utl_unpack_create(cpl_plugin * plugin)
00145 {
00146 cpl_recipe * recipe ;
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 irplib_reset();
00154
00155
00156 recipe->parameters = cpl_parameterlist_new() ;
00157
00158
00159
00160
00161
00162 return 0;
00163 }
00164
00165
00171
00172 static int flames_utl_unpack_exec(cpl_plugin * plugin)
00173 {
00174 cpl_recipe * recipe ;
00175 int code=0;
00176 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00177
00178
00179 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00180 recipe = (cpl_recipe *)plugin ;
00181 else return -1 ;
00182 cpl_error_reset();
00183 irplib_reset();
00184 code = flames_utl_unpack(recipe->parameters, recipe->frames) ;
00185
00186
00187 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00188
00189
00190 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00191 }
00192
00193 return code ;
00194 }
00195
00196
00202
00203 static int flames_utl_unpack_destroy(cpl_plugin * plugin)
00204 {
00205 cpl_recipe * recipe ;
00206
00207
00208 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00209 recipe = (cpl_recipe *)plugin ;
00210 else return -1 ;
00211
00212 cpl_parameterlist_delete(recipe->parameters) ;
00213 return 0 ;
00214 }
00215
00216
00223
00224 static int
00225 flames_utl_unpack( cpl_parameterlist * parlist,
00226 cpl_frameset * set)
00227 {
00228 uves_propertylist * plist=NULL ;
00229 int n=0;
00230 int i=0;
00231 int naxis=0;
00232 cpl_frame* frm=NULL;
00233 const char* tag=NULL;
00234 const char* name=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 for(i=0;i<n;i++) {
00254 check_nomsg(frm=cpl_frameset_get_frame(set,i));
00255 check_nomsg(tag=cpl_frame_get_tag(frm));
00256 check_nomsg(name=cpl_frame_get_filename(frm));
00257 check_nomsg(plist=uves_propertylist_load(name,0));
00258 check_nomsg(naxis=uves_pfits_get_naxis(plist));
00259
00260 if(naxis==3) {
00261 check_nomsg(flames_unpack_image_from_cube(frm,parlist,set));
00262 } else if (naxis==2) {
00263 check_nomsg(flames_unpack_spectra_from_image(frm,parlist,set));
00264 } else {
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 const char* fname=NULL;
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 cknull_nomsg(parameters);
00302 check_nomsg(name=cpl_frame_get_filename(frm));
00303 check_nomsg(tag=cpl_frame_get_tag(frm));
00304 check_nomsg(href=uves_propertylist_load(name,0));
00305 check_nomsg(hout=uves_propertylist_duplicate(href));
00306
00307 check_nomsg(iml=cpl_imagelist_load(name,CPL_TYPE_FLOAT,0));
00308 check_nomsg(size=cpl_imagelist_get_size(iml));
00309 for(i=0;i<size;i++) {
00310 frame=cpl_frame_new();
00311 check_nomsg(img=cpl_imagelist_get(iml,i));
00312 check_nomsg(fname=uves_sprintf("slic_%d_%s",i,flames_get_basename(name)));
00313 check_nomsg( uves_save_image(img,fname,hout,true, true) );
00314
00315 check_nomsg(cpl_frame_set_filename(frame, fname)) ;
00316 check_nomsg(cpl_frame_set_tag(frame, tag)) ;
00317 check_nomsg(cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE)) ;
00318 check_nomsg(cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT)) ;
00319 check(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL),
00320 "Error while initialising the product frame") ;
00321
00322 cpl_frameset_insert(set,frame);
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 }
00351
00352
00353 cleanup:
00354 uves_free_propertylist(&href);
00355 uves_free_propertylist(&hout);
00356 uves_free_imagelist(&iml);
00357
00358
00359 return cpl_error_get_code() ;
00360
00361 }
00362
00363
00364
00365 static cpl_error_code
00366 flames_unpack_spectra_from_image(cpl_frame* frm,
00367 cpl_parameterlist * parameters,
00368 cpl_frameset* set)
00369 {
00370
00371
00372
00373 const char* name=NULL;
00374 const char* fname=NULL;
00375 const char* tag=NULL;
00376 cpl_propertylist* href=NULL;
00377 cpl_propertylist* hout=NULL;
00378 cpl_image* ima=NULL;
00379
00380 int i=0;
00381 cpl_frame* frame=NULL;
00382 cpl_image* img=NULL;
00383 int sx=0;
00384 int sy=0;
00385
00386 cknull_nomsg(parameters);
00387 check_nomsg(name=cpl_frame_get_filename(frm));
00388 check_nomsg(tag=cpl_frame_get_tag(frm));
00389 check_nomsg(href=cpl_propertylist_load(name,0));
00390 check_nomsg(hout=cpl_propertylist_duplicate(href));
00391
00392 check_nomsg(ima=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00393 check_nomsg(sx=cpl_image_get_size_x(ima));
00394 check_nomsg(sy=cpl_image_get_size_y(ima));
00395
00396 for(i=0;i<sy;i++) {
00397 frame=cpl_frame_new();
00398
00399 check_nomsg(img=cpl_image_extract(ima,1,i+1,sx,i+1));
00400 check_nomsg(fname=uves_sprintf("slic_%d_%s",i,flames_get_basename(name)));
00401 cpl_image_save(img,fname,CPL_BPP_IEEE_FLOAT,hout,CPL_IO_DEFAULT);
00402
00403
00404
00405
00406
00407 check_nomsg(cpl_frame_set_filename(frame, fname)) ;
00408 check_nomsg(cpl_frame_set_tag(frame, tag)) ;
00409 check_nomsg(cpl_frame_set_type(frame, CPL_FRAME_TYPE_IMAGE)) ;
00410 check_nomsg(cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT)) ;
00411 check(cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL),
00412 "Error while initialising the product frame") ;
00413 cpl_frameset_insert(set,frame);
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443 uves_free_image(&img);
00444
00445 }
00446
00447
00448 cleanup:
00449 cpl_propertylist_delete(href);
00450 cpl_propertylist_delete(hout);
00451 uves_free_image(&ima);
00452 uves_free_image(&img);
00453
00454 return cpl_error_get_code() ;
00455
00456
00457 }
00458
00459
00460
00461