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