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
00029
00030
00031 #ifdef HAVE_CONFIG_H
00032 #include <config.h>
00033 #endif
00034
00035
00036
00037
00038
00039 #include <strings.h>
00040 #include <string.h>
00041
00042 #include <stdio.h>
00043 #include <math.h>
00044
00045
00046 #include <cpl.h>
00047
00048
00049 #include <irplib_utils.h>
00050
00051 #include <sinfo_hidden.h>
00052 #include <sinfo_pro_types.h>
00053 #include <sinfo_raw_types.h>
00054 #include <sinfo_key_names.h>
00055
00056 #include <sinfo_globals.h>
00057 #include <sinfo_general_config.h>
00058 #include <sinfo_utilities.h>
00059 #include <sinfo_product_config.h>
00060 #include <sinfo_lamp_flats_config.h>
00061 #include <sinfo_bp_config.h>
00062 #include <sinfo_bp_dist_config.h>
00063 #include <sinfo_prepare_stacked_frames_config.h>
00064
00065 #include <sinfo_north_south_test_config.h>
00066 #include <sinfo_distortion_config.h>
00067 #include <sinfo_new_lamp_flats.h>
00068 #include <sinfo_bp_norm.h>
00069 #include <sinfo_new_prepare_stacked_frames.h>
00070 #include <sinfo_new_find_distortions.h>
00071 #include <sinfo_new_nst.h>
00072
00073 #include <sinfo_functions.h>
00074 #include <sinfo_tpl_utils.h>
00075 #include <sinfo_tpl_dfs.h>
00076 #include <sinfo_msg.h>
00077 #include <sinfo_error.h>
00078 #include <sinfo_utils_wrappers.h>
00079
00080 #include <sinfo_dfs.h>
00081
00082 #define CPL_BPP_IEEE_FLOAT CPL_BPP_IEEE_FLOAT
00083
00084
00085
00086
00087
00088
00089 static int sinfo_rec_distortion_create(cpl_plugin *plugin);
00090 static int sinfo_rec_distortion_exec(cpl_plugin *plugin);
00091 static int sinfo_rec_distortion_destroy(cpl_plugin *plugin);
00092 static int sinfo_rec_distortion(cpl_parameterlist *config, cpl_frameset *set);
00093 static int new_pre_process(cpl_frameset* set,
00094 const float lo_rej, const float hi_rej, const char* name_o);
00095
00096
00097
00098
00099 static char sinfo_rec_distortion_description1[] =
00100 "This recipe determines the optical distortions and the slitlets distances.\n"
00101 "Necessary input are:\n"
00102 "Several (usually 75) raw frames classified as FIBRE_NS\n"
00103 "Standard (an 'ON' and an 'OFF') flat frames having classified as FLAT_NS\n"
00104 "Standard (an 'ON' and an 'OFF') arc lamp frames having classified as WAVE_NS\n"
00105 "A corresponding (band) reference arc line list classified as REF_LINE_ARC\n"
00106 "A reference table with data reduction parameters classified as DRS_SETUP_WAVE.\n";
00107
00108
00109 static char sinfo_rec_distortion_description2[] =
00110 "Default output are (with their PRO.CATG)\n"
00111 "A master flat: MASTER_FLAT_LAMP\n"
00112 "A Bad pixel map: BP_MAP_DI\n"
00113 "A fake-off fibre stacked frame: FIBRE_NS_STACKED_OFF\n"
00114 "A fake-on fibre stacked frame: FIBRE_NS_STACKED_ON\n"
00115 "A fake on-off fibre stacked frame: FIBRE_NS_STACKED\n"
00116 "A table with optical distortion coefficients: DISTORTION\n";
00117
00118
00119 static char sinfo_rec_distortion_description3[] =
00120 "A distortion corrected frame: FIBRE_NS_STACKED_DIST\n"
00121 "A temporary frame: MASTER_SLIT\n"
00122 "A table with the slitlets distances: SLITLETS_DISTANCE\n"
00123 "Information on relevant parameters can be found with:\n"
00124 "esorex --params sinfo_rec_distortion\n"
00125 "esorex --help sinfo_rec_distortion\n"
00126 "\n";
00127
00128 static char sinfo_rec_distortion_description[1300];
00129
00130
00131
00132
00133
00137
00139
00148
00149 int
00150 cpl_plugin_get_info(cpl_pluginlist *list)
00151 {
00152
00153 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00154 cpl_plugin *plugin = &recipe->interface;
00155 strcpy(sinfo_rec_distortion_description,sinfo_rec_distortion_description1);
00156 strcat(sinfo_rec_distortion_description,sinfo_rec_distortion_description2);
00157 strcat(sinfo_rec_distortion_description,sinfo_rec_distortion_description3);
00158
00159
00160 cpl_plugin_init(plugin,
00161 CPL_PLUGIN_API,
00162 SINFONI_BINARY_VERSION,
00163 CPL_PLUGIN_TYPE_RECIPE,
00164 "sinfo_rec_distortion",
00165 "Finds optical distortions and slitlets distances",
00166 sinfo_rec_distortion_description,
00167 "Andrea Modigliani",
00168 "Andrea.Modigliani@eso.org",
00169 sinfo_get_license(),
00170 sinfo_rec_distortion_create,
00171 sinfo_rec_distortion_exec,
00172 sinfo_rec_distortion_destroy);
00173
00174 cpl_pluginlist_append(list, plugin);
00175
00176 return 0;
00177
00178 }
00179
00180
00181
00189
00190 static int sinfo_rec_distortion_create(cpl_plugin *plugin)
00191 {
00192 cpl_recipe * recipe ;
00193
00194
00195 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00196 recipe = (cpl_recipe *)plugin ;
00197 else return -1 ;
00198 cpl_error_reset();
00199 irplib_reset();
00200
00201
00202 recipe->parameters = cpl_parameterlist_new() ;
00203
00204
00205
00206
00207
00208
00209
00210
00211 sinfo_general_config_add(recipe->parameters);
00212 sinfo_product_config_add(recipe->parameters);
00213 sinfo_lamp_flats_config_add(recipe->parameters);
00214 sinfo_bp_config_add(recipe->parameters);
00215 sinfo_bp_dist_config_add(recipe->parameters);
00216 sinfo_prepare_stacked_frames_config_add(recipe->parameters);
00217 sinfo_distortion_config_add(recipe->parameters);
00218 sinfo_north_south_test_config_add(recipe->parameters);
00219
00220
00221 return 0;
00222
00223 }
00224
00230
00231 static int sinfo_rec_distortion_exec(cpl_plugin *plugin)
00232 {
00233 cpl_recipe * recipe ;
00234 int status=0;
00235
00236 cpl_errorstate initial_errorstate = cpl_errorstate_get();
00237
00238
00239 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00240 recipe = (cpl_recipe *)plugin ;
00241 else return -1 ;
00242 status=sinfo_rec_distortion(recipe->parameters, recipe->frames);
00243
00244 if (!cpl_errorstate_is_equal(initial_errorstate)) {
00245
00246
00247 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
00248 }
00249 return status;
00250
00251 }
00252
00253
00259
00260 static int sinfo_rec_distortion_destroy(cpl_plugin *plugin)
00261 {
00262 cpl_recipe * recipe ;
00263
00264
00265 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
00266 recipe = (cpl_recipe *)plugin ;
00267 else return -1 ;
00268
00269 cpl_parameterlist_delete(recipe->parameters);
00270
00271 return 0;
00272
00273 }
00274
00275
00282
00283
00284
00285
00286
00287 static int
00288 sinfo_rec_distortion(cpl_parameterlist *config, cpl_frameset *set)
00289 {
00290
00291 cpl_parameter *p=NULL;
00292 cpl_frame* frame=NULL;
00293 cpl_propertylist* plist=NULL;
00294 cpl_image* ima=NULL;
00295 cpl_frameset* raw_set=NULL;
00296 cpl_frameset* set_off=NULL;
00297 cpl_frameset* set_on=NULL;
00298 cpl_frameset* set_fibre_ns=NULL;
00299 cpl_frameset* set_flat_ns=NULL;
00300 cpl_frameset* set_wave_ns=NULL;
00301 char file_name[FILE_NAME_SZ];
00302
00303 fake* fk=sinfo_fake_new();
00304 int pdensity=0;
00305 int line_cor=0;
00306
00307 check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
00308 check_nomsg(pdensity=cpl_parameter_get_int(p));
00309
00310
00311 sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
00312 SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
00313 if(sinfo_dfs_set_groups(set)) {
00314 sinfo_msg_error("Cannot indentify RAW and CALIB frames") ;
00315 return -1;
00316 }
00317
00318 check_nomsg(p=cpl_parameterlist_find(config, "sinfoni.general.lc_sw"));
00319 check_nomsg(line_cor=cpl_parameter_get_bool(p));
00320 if(line_cor==1) {
00321 check_nomsg(sinfo_ima_line_cor(config,set));
00322 }
00323
00324 cknull_nomsg(set_fibre_ns=sinfo_frameset_extract(set,RAW_FIBRE_NS));
00325 cknull_nomsg(set_flat_ns=sinfo_frameset_extract(set,RAW_FLAT_NS));
00326 cknull_nomsg(set_wave_ns=sinfo_frameset_extract(set,RAW_WAVE_NS));
00327 check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.bp.method"));
00328 check_nomsg(cpl_parameter_set_string(p,"Normal"));
00329 check_nomsg(raw_set=cpl_frameset_duplicate(set));
00330
00331 sinfo_msg("-----------------------------");
00332 sinfo_msg(" DETERMINE MASTER_LAMP_NS ");
00333 sinfo_msg("-----------------------------");
00334
00335 sinfo_msg("REDUCE LAMPFLAT") ;
00336 ck0(sinfo_new_lamp_flats(cpl_func,config, set,set_flat_ns ),
00337 "reducing lampflats") ;
00338 sinfo_msg("SUCCES: DETERMINATION MASTER_LAMP_NS") ;
00339
00340 sinfo_msg("-----------------------------");
00341 sinfo_msg(" DETERMINE BP_MAP_DI ");
00342 sinfo_msg("-----------------------------");
00343
00344 check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.bp.method"));
00345 check_nomsg(cpl_parameter_set_string(p,"Normal"));
00346
00347 ck0(sinfo_new_bp_search_normal(cpl_func,config,set,set_flat_ns,
00348 PRO_BP_MAP_DI),
00349 "determining %s",PRO_BP_MAP_DI);
00350 sinfo_msg("SUCCESS DETERMINATION %s",PRO_BP_MAP_DI);
00351
00352
00353
00354
00355
00356
00357 check_nomsg(set_off=cpl_frameset_duplicate(raw_set));
00358 ck0_nomsg(new_pre_process(set_off, 0.0, 0.2,"out_fibre_off.fits"));
00359
00360
00361
00362 if(NULL != cpl_frameset_find(set_off,RAW_FIBRE_NS)) {
00363 frame = cpl_frameset_find(set_off,RAW_FIBRE_NS);
00364 cpl_frameset_erase(set,RAW_FIBRE_NS);
00365 cpl_frameset_insert(set,cpl_frame_duplicate(frame));
00366 } else {
00367 sinfo_msg_error("Frame %s not found!", RAW_FIBRE_NS);
00368 goto cleanup;
00369 }
00370
00371
00372
00373
00374
00375
00376
00377
00378 sinfo_msg("STACK FIBRE,NS TO GET FAKE OFF\n") ;
00379 strcpy(fk->pro_class,RAW_FIBRE_NS);
00380 fk->frm_switch=1;
00381 fk->mask_index=0;
00382 fk->ind_index=0;
00383 fk->flat_index=0;
00384 fk->wfix_index=0;
00385 fk->low_rej=0.0;
00386 fk->hig_rej=0.2;
00387
00388
00389 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
00390 PRO_FIBRE_NS_STACKED_OFF,0,fk),
00391 "Error stacking frame %s",PRO_FIBRE_NS_STACKED_OFF);
00392
00393 strcpy(file_name,"out_fibre_off.fits");
00394 check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
00395 sinfo_free_frameset(&set_off);
00396
00397 cknull(plist = cpl_propertylist_load(file_name, 0),
00398 "getting header from reference ima frame %s",file_name);
00399
00400 if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
00401 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_OFF);
00402 } else {
00403 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_OFF);
00404 }
00405
00406 sinfo_free_propertylist(&plist);
00407 sinfo_free_image(&ima);
00408 sinfo_msg("SUCCESS: STACKED FIBRE,NS TO GET FAKE OFF\n") ;
00409
00410
00411
00412
00413
00414
00415
00416 check_nomsg(set_on=cpl_frameset_duplicate(raw_set));
00417
00418 ck0_nomsg(new_pre_process(set_on, 0.0, 0.0,"out_fibre_on.fits"));
00419
00420 if(NULL != cpl_frameset_find(set_on,RAW_FIBRE_NS)) {
00421 frame = cpl_frameset_find(set_on,RAW_FIBRE_NS);
00422 cpl_frameset_erase(set,RAW_FIBRE_NS);
00423 cpl_frameset_insert(set,cpl_frame_duplicate(frame));
00424 } else {
00425 sinfo_msg_error("Frame %s not found!", RAW_FIBRE_NS);
00426 goto cleanup;
00427 }
00428
00429
00430 sinfo_msg("STACK FIBRE,NS TO GET FAKE ON\n") ;
00431
00432 strcpy(fk->pro_class,RAW_FIBRE_NS);
00433 fk->frm_switch=1;
00434 fk->mask_index=0;
00435 fk->ind_index=0;
00436 fk->flat_index=0;
00437 fk->wfix_index=0;
00438 fk->low_rej=0.0;
00439 fk->hig_rej=0.0;
00440
00441
00442 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
00443 PRO_FIBRE_NS_STACKED_ON,0,fk),
00444 "error stacking frame %s",PRO_FIBRE_NS_STACKED_ON);
00445 sinfo_msg("SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED_ON) ;
00446
00447
00448 sinfo_free_frameset(&set_on);
00449
00450
00451
00452
00453
00454
00455
00456
00457 sinfo_msg("COMBINES FAKE ON AND OFF\n") ;
00458 strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED);
00459 fk->frm_switch=1;
00460 fk->mask_index=0;
00461 fk->ind_index=0;
00462 fk->flat_index=1;
00463 fk->wfix_index=0;
00464 fk->low_rej=0.0;
00465 fk->hig_rej=0.0;
00466
00467 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
00468 PRO_FIBRE_NS_STACKED,0,fk),
00469 "Stacking frame %s",PRO_FIBRE_NS_STACKED);
00470 sinfo_msg("SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED) ;
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480 sinfo_msg("STACK on WAVE frame\n") ;
00481 strcpy(fk->pro_class,RAW_WAVE_NS);
00482 fk->frm_switch=1;
00483 fk->mask_index=0;
00484 fk->ind_index=0;
00485 fk->flat_index=1;
00486 fk->wfix_index=0;
00487 fk->low_rej=0.1;
00488 fk->hig_rej=0.1;
00489
00490 sinfo_msg("STACK on WAVE frame\n") ;
00491 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_wave_ns,
00492 PRO_WAVE_LAMP_STACKED,0,fk),
00493 "spacking frame %s",PRO_WAVE_LAMP_STACKED);
00494 sinfo_msg("SUCCESS DETERMINATION %s",PRO_WAVE_LAMP_STACKED) ;
00495
00496 if(pdensity < 2) {
00497 cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED_OFF);
00498 cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED_ON);
00499 cpl_frameset_erase(set,PRO_MASTER_FLAT_LAMP);
00500 }
00501
00502
00503
00504
00505
00506
00507 sinfo_msg("COMPUTE DISTORTIONS\n") ;
00508 ck0(sinfo_new_find_distortions(cpl_func,config, set,set_fibre_ns),
00509 "computing distortions");
00510
00511
00512 if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED)) {
00513 frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED);
00514 strcpy(file_name,cpl_frame_get_filename(frame));
00515 } else {
00516 sinfo_msg_error("Frame %s not found!", PRO_FIBRE_NS_STACKED);
00517 goto cleanup;
00518 }
00519 check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
00520
00521 check(plist = cpl_propertylist_load(file_name, 0),
00522 "getting header from reference ima frame %s",file_name);
00523
00524
00525 if (cpl_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
00526 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
00527 } else {
00528 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON);
00529 }
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542 sinfo_free_image(&ima);
00543 sinfo_free_propertylist(&plist);
00544 sinfo_msg("SUCCESS: COMPUTED DISTORTIONS\n") ;
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 sinfo_msg("DISTORT FAKE FRAME\n") ;
00555
00556 strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED_DIST);
00557 fk->frm_switch=1;
00558 fk->mask_index=1;
00559 fk->ind_index=1;
00560 fk->flat_index=0;
00561 fk->wfix_index=1;
00562 fk->low_rej=0.0;
00563 fk->hig_rej=0.0;
00564
00565 ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
00566 PRO_FIBRE_NS_STACKED_DIST,0,fk),
00567 "Stacking frame %s",PRO_FIBRE_NS_STACKED_DIST);
00568
00569 sinfo_msg("SUCCESS: DISTORTED FAKE FRAME\n") ;
00570
00571
00572
00573 if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST)) {
00574 frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST);
00575 strcpy(file_name,cpl_frame_get_filename(frame));
00576 } else {
00577 sinfo_msg_error("Frame %s not found!", PRO_FIBRE_NS_STACKED_DIST);
00578 goto cleanup;
00579 }
00580 check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
00581 check(plist = cpl_propertylist_load(file_name, 0),
00582 "getting header from reference ima frame %s",file_name);
00583
00584
00585 if (cpl_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
00586 cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
00587 } else {
00588 cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON);
00589 }
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599 sinfo_free_propertylist(&plist);
00600 sinfo_free_image(&ima);
00601
00602
00603
00604
00605
00606
00607
00608 if(pdensity < 2) {
00609 cpl_frameset_erase(set,PRO_BP_MAP_DI);
00610 cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED);
00611 cpl_frameset_erase(set,PRO_WAVE_LAMP_STACKED);
00612 }
00613
00614
00615 sinfo_msg("RUN NORD SOUTH TEST\n") ;
00616 ck0(sinfo_new_nst(cpl_func,config, set,set_fibre_ns),
00617 "Running north south test");
00618
00619
00620 if(pdensity < 2) {
00621 cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED_DIST);
00622 }
00623
00624 sinfo_msg("SUCCESS: RUNNED NORD SUD TEST\n") ;
00625 sinfo_msg("SUCCESS: RECIPE\n") ;
00626
00627 sinfo_free_frameset(&raw_set);
00628 sinfo_fake_delete(&fk);
00629
00630
00631 cleanup:
00632
00633 sinfo_free_frameset(&set_on);
00634 sinfo_free_frameset(&set_fibre_ns);
00635 sinfo_free_frameset(&set_wave_ns);
00636 sinfo_free_frameset(&set_flat_ns);
00637 sinfo_free_image(&ima);
00638 sinfo_free_propertylist(&plist) ;
00639 sinfo_free_frameset(&set_off);
00640 sinfo_free_frameset(&raw_set);
00641 sinfo_free_frameset(&raw_set);
00642 sinfo_fake_delete(&fk);
00643
00644 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00645 return -1;
00646 } else {
00647 return 0;
00648 }
00649
00650
00651 }
00652
00653
00654
00655 static int
00656 new_pre_process(cpl_frameset* set,
00657 const float lo_rej, const float hi_rej, const char* name_o)
00658 {
00659
00660 int nf=0;
00661 int ng=10;
00662 int ns=0;
00663 int nr=0;
00664 int nt=0;
00665
00666 int i=0;
00667 int j=0;
00668 int k=0;
00669 int lo_cut=0;
00670 int hi_cut=0;
00671
00672
00673 cpl_frame* frame=NULL;
00674 cpl_frame* pframe=NULL;
00675 cpl_frameset* sof=NULL;
00676 cpl_frameset* fibre_raw_set=NULL;
00677 char* file_name=NULL;
00678
00679 cpl_propertylist* plist=NULL;
00680 cpl_imagelist* imset_tmp=NULL;
00681 cpl_imagelist* imset_tot=NULL;
00682 cpl_image** avg_img_list=NULL;
00683
00684 cpl_image* img=NULL;
00685 cpl_image* img_tmp=NULL;
00686 cpl_image* img_dup=NULL;
00687
00688 cpl_image** img_list=NULL;
00689
00690
00691 sof = cpl_frameset_duplicate(set);
00692 fibre_raw_set = cpl_frameset_new();
00693
00694 sinfo_contains_frames_kind(sof,fibre_raw_set,RAW_FIBRE_NS);
00695
00696 nf = cpl_frameset_get_size(fibre_raw_set);
00697 if(nf < 1) {
00698 sinfo_msg_warning("No input frames in data set");
00699 sinfo_free_frameset(&sof);
00700 sinfo_free_frameset(&fibre_raw_set);
00701 return -1;
00702 }
00703 frame = cpl_frameset_get_frame(fibre_raw_set,0);
00704 file_name=(char*) cpl_frame_get_filename(frame);
00705
00706
00707 if ((plist = cpl_propertylist_load(file_name, 0)) == NULL) {
00708 sinfo_msg_error("getting header from ima frame %s",file_name);
00709 return -1 ;
00710 }
00711
00712
00713 if( nf > ng) {
00714
00715 sinfo_msg("Total raw frames nf=%d > max frm per group ng=%d",nf,ng);
00716 ns = (nf+1)/ng;
00717 nr = nf-ns*ng;
00718 imset_tot=cpl_imagelist_new();
00719 avg_img_list=cpl_malloc((ns+1) * sizeof(cpl_image *));
00720
00721
00722 for (i=0;i<ns;i++) {
00723 sinfo_msg("iteration i=%d\n",i);
00724 imset_tmp=cpl_imagelist_new();
00725 img_list=cpl_malloc(ng * sizeof(cpl_image *));
00726
00727
00728 for (j=0;j<ng;j++) {
00729 k=i*ng+j;
00730 frame = cpl_frameset_get_frame(fibre_raw_set,k);
00731 file_name=(char*)cpl_frame_get_filename(frame);
00732 img_tmp=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00733 img_dup=cpl_image_duplicate(img_tmp);
00734 cpl_imagelist_set(imset_tmp,img_dup,j);
00735 cpl_image_delete(img_tmp);
00736 }
00737
00738
00739 nt=cpl_imagelist_get_size(imset_tmp);
00740 lo_cut=(floor)(lo_rej*nt+0.5);
00741 hi_cut=(floor)(hi_rej*nt+0.5);
00742 avg_img_list[i]=cpl_imagelist_collapse_minmax_create(imset_tmp,
00743 lo_cut,hi_cut);
00744 cpl_imagelist_set(imset_tot,avg_img_list[i],i);
00745 cpl_imagelist_delete(imset_tmp);
00746 cpl_free(img_list);
00747
00748 }
00749
00750 if(ns*ng<nf) {
00751 imset_tmp=cpl_imagelist_new();
00752 img_list=cpl_malloc((nf-ns*ng) * sizeof(cpl_image *));
00753 for(i=0;i<nr;i++) {
00754 k=i+ns*ng;
00755 frame = cpl_frameset_get_frame(fibre_raw_set,k);
00756 file_name = (char*) cpl_frame_get_filename(frame);
00757
00758 img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00759 cpl_imagelist_set(imset_tmp,img_list[i],i);
00760 }
00761 nt=cpl_imagelist_get_size(imset_tmp);
00762 lo_cut=(floor)(lo_rej*nt+0.5);
00763 hi_cut=(floor)(hi_rej*nt+0.5);
00764
00765 avg_img_list[ns]=cpl_imagelist_collapse_minmax_create(imset_tmp,
00766 lo_cut,hi_cut);
00767 cpl_imagelist_set(imset_tot,avg_img_list[ns],ns);
00768
00769 cpl_free(img);
00770 cpl_imagelist_delete(imset_tmp);
00771 cpl_free(img_list);
00772 }
00773
00774 } else {
00775
00776 sinfo_msg("Total raw frames nf=%d < max frm per group ng=%d",nf,ng);
00777 imset_tot=cpl_imagelist_new();
00778 img_list=cpl_malloc(nf * sizeof(cpl_image *));
00779 for (i=0;i<nf;i++) {
00780 frame = cpl_frameset_get_frame(fibre_raw_set,i);
00781 file_name = (char*) cpl_frame_get_filename(frame);
00782 img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
00783 cpl_imagelist_set(imset_tot,img_list[i],i);
00784 }
00785 cpl_free(img_list);
00786 }
00787 cpl_free(avg_img_list);
00788
00789 cpl_frameset_delete(fibre_raw_set);
00790 nt=cpl_imagelist_get_size(imset_tot);
00791 lo_cut=(floor)(lo_rej*nt+0.5);
00792 hi_cut=(floor)(hi_rej*nt+0.5);
00793
00794 if( (img = cpl_imagelist_collapse_minmax_create(imset_tot,
00795 lo_cut,hi_cut)) == NULL) {
00796 sinfo_msg_error("Error code");
00797 sinfo_msg_error((char* ) cpl_error_get_message());
00798 cpl_imagelist_delete(imset_tot);
00799 cpl_frameset_delete(sof);
00800 cpl_propertylist_delete(plist) ;
00801 return -1;
00802 }
00803
00804 if (cpl_image_save(img,name_o, CPL_BPP_IEEE_FLOAT,
00805 plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
00806 sinfo_msg_error("Cannot save the product %s",name_o);
00807 cpl_imagelist_delete(imset_tot);
00808 cpl_frameset_delete(sof);
00809 cpl_propertylist_delete(plist) ;
00810 return -1 ;
00811 }
00812 cpl_imagelist_delete(imset_tot);
00813 cpl_frameset_erase(set,RAW_FIBRE_NS);
00814
00815
00816 pframe = cpl_frame_new();
00817 cpl_frame_set_filename(pframe, name_o) ;
00818 cpl_frame_set_tag(pframe, "FIBRE_NS") ;
00819 cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE) ;
00820 cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW) ;
00821 cpl_frame_set_level(pframe, CPL_FRAME_LEVEL_FINAL) ;
00822 if (cpl_error_get_code()) {
00823 sinfo_msg_error("Error while initialising the product frame") ;
00824 cpl_propertylist_delete(plist) ;
00825 cpl_frame_delete(pframe) ;
00826 cpl_image_delete(img) ;
00827 return -1 ;
00828 }
00829
00830
00831
00832 if (cpl_image_save(img, name_o, CPL_BPP_IEEE_FLOAT, plist,
00833 CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
00834 sinfo_msg_error("Could not save product");
00835 cpl_propertylist_delete(plist) ;
00836 cpl_frame_delete(pframe) ;
00837 cpl_image_delete(img) ;
00838 return -1 ;
00839 }
00840 cpl_propertylist_delete(plist) ;
00841 cpl_image_delete(img) ;
00842
00843
00844 cpl_frameset_insert(set, pframe) ;
00845 cpl_frameset_delete(sof);
00846
00847 return 0;
00848 }