SINFONI Pipeline Reference Manual  2.5.2
sinfo_rec_distortion.c
1 /* $Id: sinfo_rec_distortion.c,v 1.43 2013-09-17 08:13:17 amodigli Exp $
2  *
3  * This file is part of the SINFONI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: amodigli $
23  * $Date: 2013-09-17 08:13:17 $
24  * $Revision: 1.43 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /****************************************************************
29  * Distortions Frames Data Reduction *
30  ****************************************************************/
31 #ifdef HAVE_CONFIG_H
32 #include <config.h> /* allows the program compilation */
33 #endif
34 
35 /*-----------------------------------------------------------------------------
36  Includes
37  ----------------------------------------------------------------------------*/
38 /* std */
39 #include <strings.h>
40 #include <string.h>
41 
42 #include <stdio.h>
43 #include <math.h>
44 
45 /* cpl */
46 #include <cpl.h>
47 
48 /* irplib */
49 #include <irplib_utils.h>
50 
51 #include <sinfo_hidden.h>
52 #include <sinfo_pro_types.h>
53 #include <sinfo_raw_types.h>
54 #include <sinfo_key_names.h>
55 
56 #include <sinfo_globals.h>
57 #include <sinfo_general_config.h>
58 #include <sinfo_utilities.h>
59 #include <sinfo_product_config.h>
60 #include <sinfo_lamp_flats_config.h>
61 #include <sinfo_bp_config.h>
62 #include <sinfo_bp_dist_config.h>
63 #include <sinfo_prepare_stacked_frames_config.h>
64 
65 #include <sinfo_north_south_test_config.h>
66 #include <sinfo_distortion_config.h>
67 #include <sinfo_new_lamp_flats.h>
68 #include <sinfo_bp_norm.h>
69 #include <sinfo_new_prepare_stacked_frames.h>
70 #include <sinfo_new_find_distortions.h>
71 #include <sinfo_new_nst.h>
72 
73 #include <sinfo_functions.h>
74 #include <sinfo_tpl_utils.h>
75 #include <sinfo_tpl_dfs.h>
76 #include <sinfo_msg.h>
77 #include <sinfo_error.h>
78 #include <sinfo_utils_wrappers.h>
79 //Only for sinfo_propertylist_has
80 #include <sinfo_dfs.h>
81 
82 
83 
84 /*-----------------------------------------------------------------------------
85  Functions prototypes
86  ----------------------------------------------------------------------------*/
87 
88 static int sinfo_rec_distortion_create(cpl_plugin *plugin);
89 static int sinfo_rec_distortion_exec(cpl_plugin *plugin);
90 static int sinfo_rec_distortion_destroy(cpl_plugin *plugin);
91 static int sinfo_rec_distortion(cpl_parameterlist *config, cpl_frameset *set);
92 static int new_pre_process(cpl_frameset* set,
93  const float lo_rej, const float hi_rej, const char* name_o);
94 
95 /*-----------------------------------------------------------------------------
96  Static variables
97  ----------------------------------------------------------------------------*/
98 static char sinfo_rec_distortion_description1[] =
99  "This recipe determines the optical distortions and the slitlets distances.\n"
100  "Necessary input are:\n"
101  "Several (usually 75) raw frames classified as FIBRE_NS\n"
102  "Standard (an 'ON' and an 'OFF') flat frames having classified as FLAT_NS\n"
103  "Standard (an 'ON' and an 'OFF') arc lamp frames having classified as WAVE_NS\n"
104  "A corresponding (band) reference arc line list classified as REF_LINE_ARC\n"
105  "A reference table with data reduction parameters classified as DRS_SETUP_WAVE.\n";
106 
107 
108 static char sinfo_rec_distortion_description2[] =
109  "Default output are (with their PRO.CATG)\n"
110  "A master flat: MASTER_FLAT_LAMP\n"
111  "A Bad pixel map: BP_MAP_DI\n"
112  "A fake-off fibre stacked frame: FIBRE_NS_STACKED_OFF\n"
113  "A fake-on fibre stacked frame: FIBRE_NS_STACKED_ON\n"
114  "A fake on-off fibre stacked frame: FIBRE_NS_STACKED\n"
115  "A table with optical distortion coefficients: DISTORTION\n";
116 
117 
118 static char sinfo_rec_distortion_description3[] =
119  "A distortion corrected frame: FIBRE_NS_STACKED_DIST\n"
120  "A temporary frame: MASTER_SLIT\n"
121  "A table with the slitlets distances: SLITLETS_DISTANCE\n"
122  "\n";
123 
124 static char sinfo_rec_distortion_description[1300];
125 
126 /*-----------------------------------------------------------------------------
127  Functions code
128  ----------------------------------------------------------------------------*/
129 /*---------------------------------------------------------------------------*/
133 /*---------------------------------------------------------------------------*/
135 /*---------------------------------------------------------------------------*/
144 /*---------------------------------------------------------------------------*/
145 int
146 cpl_plugin_get_info(cpl_pluginlist *list)
147 {
148 
149  cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
150  cpl_plugin *plugin = &recipe->interface;
151  strcpy(sinfo_rec_distortion_description,sinfo_rec_distortion_description1);
152  strcat(sinfo_rec_distortion_description,sinfo_rec_distortion_description2);
153  strcat(sinfo_rec_distortion_description,sinfo_rec_distortion_description3);
154 
155 
156  cpl_plugin_init(plugin,
157  CPL_PLUGIN_API,
158  SINFONI_BINARY_VERSION,
159  CPL_PLUGIN_TYPE_RECIPE,
160  "sinfo_rec_distortion",
161  "Finds optical distortions and slitlets distances",
162  sinfo_rec_distortion_description,
163  "Andrea Modigliani",
164  "Andrea.Modigliani@eso.org",
165  sinfo_get_license(),
166  sinfo_rec_distortion_create,
167  sinfo_rec_distortion_exec,
168  sinfo_rec_distortion_destroy);
169 
170  cpl_pluginlist_append(list, plugin);
171 
172  return 0;
173 
174 }
175 
176 
177 /*---------------------------------------------------------------------------*/
185 /*---------------------------------------------------------------------------*/
186 static int sinfo_rec_distortion_create(cpl_plugin *plugin)
187 {
188  cpl_recipe * recipe ;
189 
190  /* Check that the plugin is part of a valid recipe */
191  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
192  recipe = (cpl_recipe *)plugin ;
193  else return -1 ;
194  cpl_error_reset();
195  irplib_reset();
196 
197  /* Create the parameters list in the cpl_recipe object */
198  recipe->parameters = cpl_parameterlist_new() ;
199 
200 
201  /*
202  * Fill the parameter list.
203  */
204 
205 
206  /* Output file name */
207  sinfo_general_config_add(recipe->parameters);
208  sinfo_product_config_add(recipe->parameters);
209  sinfo_lamp_flats_config_add(recipe->parameters);
210  sinfo_bp_config_add(recipe->parameters);
211  sinfo_bp_dist_config_add(recipe->parameters);
212  sinfo_prepare_stacked_frames_config_add(recipe->parameters);
213  sinfo_distortion_config_add(recipe->parameters);
214  sinfo_north_south_test_config_add(recipe->parameters);
215 
216 
217  return 0;
218 
219 }
220 /*---------------------------------------------------------------------------*/
226 /*---------------------------------------------------------------------------*/
227 static int sinfo_rec_distortion_exec(cpl_plugin *plugin)
228 {
229  cpl_recipe * recipe ;
230  int status=0;
231 
232  cpl_errorstate initial_errorstate = cpl_errorstate_get();
233 
234  /* Get the recipe out of the plugin */
235  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
236  recipe = (cpl_recipe *)plugin ;
237  else return -1 ;
238  status=sinfo_rec_distortion(recipe->parameters, recipe->frames);
239 
240  if (!cpl_errorstate_is_equal(initial_errorstate)) {
241  /* Dump the error history since recipe execution start.
242  At this point the recipe cannot recover from the error */
243  cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
244  }
245  return status;
246 
247 }
248 
249 /*---------------------------------------------------------------------------*/
255 /*---------------------------------------------------------------------------*/
256 static int sinfo_rec_distortion_destroy(cpl_plugin *plugin)
257 {
258  cpl_recipe * recipe ;
259 
260  /* Get the recipe out of the plugin */
261  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
262  recipe = (cpl_recipe *)plugin ;
263  else return -1 ;
264 
265  cpl_parameterlist_delete(recipe->parameters);
266 
267  return 0;
268 
269 }
270 
271 /*---------------------------------------------------------------------------*/
278 /*---------------------------------------------------------------------------*/
279 /*
280  * The actual recipe actually start here.
281  */
282 
283 static int
284 sinfo_rec_distortion(cpl_parameterlist *config, cpl_frameset *set)
285 {
286 
287  cpl_parameter *p=NULL;
288  cpl_frame* frame=NULL;
289  cpl_propertylist* plist=NULL;
290  cpl_image* ima=NULL;
291  cpl_frameset* raw_set=NULL;
292  cpl_frameset* set_off=NULL;
293  cpl_frameset* set_on=NULL;
294  cpl_frameset* set_fibre_ns=NULL;
295  cpl_frameset* set_flat_ns=NULL;
296  cpl_frameset* set_wave_ns=NULL;
297  char file_name[FILE_NAME_SZ];
298 
299  fake* fk=sinfo_fake_new();
300  int pdensity=0;
301  int line_cor=0;
302 
303  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.product.density"));
304  check_nomsg(pdensity=cpl_parameter_get_int(p));
305 
306 
307  sinfo_msg("Welcome to SINFONI Pipeline release %d.%d.%d",
308  SINFONI_MAJOR_VERSION,SINFONI_MINOR_VERSION,SINFONI_MICRO_VERSION);
309  if(sinfo_dfs_set_groups(set)) {
310  sinfo_msg_error("Cannot indentify RAW and CALIB frames") ;
311  return -1;
312  }
313 
314  check_nomsg(p=cpl_parameterlist_find(config, "sinfoni.general.lc_sw"));
315  check_nomsg(line_cor=cpl_parameter_get_bool(p));
316  if(line_cor==1) {
317  check_nomsg(sinfo_ima_line_cor(config,set));
318  }
319 
320  cknull_nomsg(set_fibre_ns=sinfo_frameset_extract(set,RAW_FIBRE_NS));
321  cknull_nomsg(set_flat_ns=sinfo_frameset_extract(set,RAW_FLAT_NS));
322  cknull_nomsg(set_wave_ns=sinfo_frameset_extract(set,RAW_WAVE_NS));
323  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.bp.method"));
324  check_nomsg(cpl_parameter_set_string(p,"Normal"));
325  check_nomsg(raw_set=cpl_frameset_duplicate(set));
326 
327  sinfo_msg("-----------------------------");
328  sinfo_msg(" DETERMINE MASTER_LAMP_NS ");
329  sinfo_msg("-----------------------------");
330 
331  sinfo_msg("REDUCE LAMPFLAT") ;
332  ck0(sinfo_new_lamp_flats(cpl_func,config, set,set_flat_ns ),
333  "reducing lampflats") ;
334  sinfo_msg("SUCCES: DETERMINATION MASTER_LAMP_NS") ;
335 
336  sinfo_msg("-----------------------------");
337  sinfo_msg(" DETERMINE BP_MAP_DI ");
338  sinfo_msg("-----------------------------");
339 
340  check_nomsg(p=cpl_parameterlist_find(config,"sinfoni.bp.method"));
341  check_nomsg(cpl_parameter_set_string(p,"Normal"));
342 
343  ck0(sinfo_new_bp_search_normal(cpl_func,config,set,set_flat_ns,
344  PRO_BP_MAP_DI),
345  "determining %s",PRO_BP_MAP_DI);
346  sinfo_msg("SUCCESS DETERMINATION %s",PRO_BP_MAP_DI);
347  /*
348  ---------------------------------------------------------
349  pre iteration: get a collassed frame
350  ---------------------------------------------------------
351  */
352 
353  check_nomsg(set_off=cpl_frameset_duplicate(raw_set));
354  ck0_nomsg(new_pre_process(set_off, 0.0, 0.2,"out_fibre_off.fits"));
355 
356 
357  /* add the off artificial frame to the frameset */
358  if(NULL != cpl_frameset_find(set_off,RAW_FIBRE_NS)) {
359  frame = cpl_frameset_find(set_off,RAW_FIBRE_NS);
360  cpl_frameset_erase(set,RAW_FIBRE_NS);
361  cpl_frameset_insert(set,cpl_frame_duplicate(frame));
362  } else {
363  sinfo_msg_error("Frame %s not found!", RAW_FIBRE_NS);
364  goto cleanup;
365  }
366 
367 
368  /*
369  ---------------------------------------------------------
370  1st iteration: get off frame
371  ---------------------------------------------------------
372  */
373 
374  sinfo_msg("STACK FIBRE,NS TO GET FAKE OFF\n") ;
375  strcpy(fk->pro_class,RAW_FIBRE_NS);
376  fk->frm_switch=1;
377  fk->mask_index=0;
378  fk->ind_index=0;
379  fk->flat_index=0;
380  fk->wfix_index=0;
381  fk->low_rej=0.0;
382  fk->hig_rej=0.2;
383 
384 
385  ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
386  PRO_FIBRE_NS_STACKED_OFF,0,fk),
387  "Error stacking frame %s",PRO_FIBRE_NS_STACKED_OFF);
388 
389  strcpy(file_name,"out_fibre_off.fits");
390  check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
391  sinfo_free_frameset(&set_off);
392 
393  cknull(plist = cpl_propertylist_load(file_name, 0),
394  "getting header from reference ima frame %s",file_name);
395 
396  if (sinfo_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
397  cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_OFF);
398  } else {
399  cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_OFF);
400  }
401 
402  sinfo_free_propertylist(&plist);
403  sinfo_free_image(&ima);
404  sinfo_msg("SUCCESS: STACKED FIBRE,NS TO GET FAKE OFF\n") ;
405 
406  /*
407  ---------------------------------------------------------
408  2nd iteration: get on frame
409  ---------------------------------------------------------
410  */
411 
412  check_nomsg(set_on=cpl_frameset_duplicate(raw_set));
413 
414  ck0_nomsg(new_pre_process(set_on, 0.0, 0.0,"out_fibre_on.fits"));
415 
416  if(NULL != cpl_frameset_find(set_on,RAW_FIBRE_NS)) {
417  frame = cpl_frameset_find(set_on,RAW_FIBRE_NS);
418  cpl_frameset_erase(set,RAW_FIBRE_NS);
419  cpl_frameset_insert(set,cpl_frame_duplicate(frame));
420  } else {
421  sinfo_msg_error("Frame %s not found!", RAW_FIBRE_NS);
422  goto cleanup;
423  }
424 
425 
426  sinfo_msg("STACK FIBRE,NS TO GET FAKE ON\n") ;
427 
428  strcpy(fk->pro_class,RAW_FIBRE_NS);
429  fk->frm_switch=1;
430  fk->mask_index=0;
431  fk->ind_index=0;
432  fk->flat_index=0;
433  fk->wfix_index=0;
434  fk->low_rej=0.0;
435  fk->hig_rej=0.0;
436 
437 
438  ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
439  PRO_FIBRE_NS_STACKED_ON,0,fk),
440  "error stacking frame %s",PRO_FIBRE_NS_STACKED_ON);
441  sinfo_msg("SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED_ON) ;
442 
443  /* cpl_frameset_erase(set,RAW_FIBRE_NS); */
444  sinfo_free_frameset(&set_on);
445 
446  /* here we have a problem with pipefile of following step product */
447  /*
448  ---------------------------------------------------------
449  3rd iteration combines on and off fake frames
450  ---------------------------------------------------------
451  */
452 
453  sinfo_msg("COMBINES FAKE ON AND OFF\n") ;
454  strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED);
455  fk->frm_switch=1;
456  fk->mask_index=0;
457  fk->ind_index=0;
458  fk->flat_index=1;
459  fk->wfix_index=0;
460  fk->low_rej=0.0;
461  fk->hig_rej=0.0;
462 
463  ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
464  PRO_FIBRE_NS_STACKED,0,fk),
465  "Stacking frame %s",PRO_FIBRE_NS_STACKED);
466  sinfo_msg("SUCCESS DETERMINATION %s",PRO_FIBRE_NS_STACKED) ;
467 
468  /* cpl_frameset_erase(set,RAW_FIBRE_NS); */
469 
470  /*
471  ---------------------------------------------------------
472  STACK WAVECAL
473  ---------------------------------------------------------
474  */
475 
476  sinfo_msg("STACK on WAVE frame\n") ;
477  strcpy(fk->pro_class,RAW_WAVE_NS);
478  fk->frm_switch=1;
479  fk->mask_index=0;
480  fk->ind_index=0;
481  fk->flat_index=1;
482  fk->wfix_index=0;
483  fk->low_rej=0.1;
484  fk->hig_rej=0.1;
485 
486  sinfo_msg("STACK on WAVE frame\n") ;
487  ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_wave_ns,
488  PRO_WAVE_LAMP_STACKED,0,fk),
489  "spacking frame %s",PRO_WAVE_LAMP_STACKED);
490  sinfo_msg("SUCCESS DETERMINATION %s",PRO_WAVE_LAMP_STACKED) ;
491 
492  if(pdensity < 2) {
493  cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED_OFF);
494  cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED_ON);
495  cpl_frameset_erase(set,PRO_MASTER_FLAT_LAMP);
496  }
497  /*
498  ---------------------------------------------------------
499  DISTORTIONS
500  ---------------------------------------------------------
501  */
502 
503  sinfo_msg("COMPUTE DISTORTIONS\n") ;
504  ck0(sinfo_new_find_distortions(cpl_func,config, set,set_fibre_ns),
505  "computing distortions");
506 
507 
508  if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED)) {
509  frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED);
510  strcpy(file_name,cpl_frame_get_filename(frame));
511  } else {
512  sinfo_msg_error("Frame %s not found!", PRO_FIBRE_NS_STACKED);
513  goto cleanup;
514  }
515  check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
516 
517  check(plist = cpl_propertylist_load(file_name, 0),
518  "getting header from reference ima frame %s",file_name);
519 
520 
521  if (cpl_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
522  cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
523  } else {
524  cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON);
525  }
526 
527 
528  /* Save the file */
529  /*
530  if (cpl_image_save(ima, file_name, CPL_BPP_IEEE_FLOAT,
531  plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
532  sinfo_msg_error("Cannot save the product %s",file_name);
533  goto cleanup;
534 
535  }
536  */
537 
538  sinfo_free_image(&ima);
539  sinfo_free_propertylist(&plist);
540  sinfo_msg("SUCCESS: COMPUTED DISTORTIONS\n") ;
541 
542  /*
543  ---------------------------------------------------------
544  4th iteration: distort fake frame
545  ---------------------------------------------------------
546  */
547 
548 
549 
550  sinfo_msg("DISTORT FAKE FRAME\n") ;
551 
552  strcpy(fk->pro_class,PRO_FIBRE_NS_STACKED_DIST);
553  fk->frm_switch=1;
554  fk->mask_index=1;
555  fk->ind_index=1;
556  fk->flat_index=0;
557  fk->wfix_index=1;
558  fk->low_rej=0.0;
559  fk->hig_rej=0.0;
560 
561  ck0(sinfo_new_prepare_stacked_frames(cpl_func,config, set, set_fibre_ns,
562  PRO_FIBRE_NS_STACKED_DIST,0,fk),
563  "Stacking frame %s",PRO_FIBRE_NS_STACKED_DIST);
564 
565  sinfo_msg("SUCCESS: DISTORTED FAKE FRAME\n") ;
566 
567 
568 
569  if(NULL != cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST)) {
570  frame = cpl_frameset_find(set,PRO_FIBRE_NS_STACKED_DIST);
571  strcpy(file_name,cpl_frame_get_filename(frame));
572  } else {
573  sinfo_msg_error("Frame %s not found!", PRO_FIBRE_NS_STACKED_DIST);
574  goto cleanup;
575  }
576  check_nomsg(ima=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0));
577  check(plist = cpl_propertylist_load(file_name, 0),
578  "getting header from reference ima frame %s",file_name);
579 
580 
581  if (cpl_propertylist_has(plist, KEY_NAME_LAMP_HALO)) {
582  cpl_propertylist_set_bool(plist, KEY_NAME_LAMP_HALO, LAMP_ON);
583  } else {
584  cpl_propertylist_append_bool(plist, KEY_NAME_LAMP_HALO,LAMP_ON);
585  }
586 
587  /* Save the file */
588  /*
589  if (cpl_image_save(ima, file_name, CPL_BPP_IEEE_FLOAT,
590  plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
591  sinfo_msg_error("Cannot save the product %s",file_name);
592  goto cleanup;
593  }
594  */
595  sinfo_free_propertylist(&plist);
596  sinfo_free_image(&ima);
597 
598  /*
599  ---------------------------------------------------------
600  NST
601  ---------------------------------------------------------
602  */
603 
604  if(pdensity < 2) {
605  cpl_frameset_erase(set,PRO_BP_MAP_DI);
606  cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED);
607  cpl_frameset_erase(set,PRO_WAVE_LAMP_STACKED);
608  }
609 
610 
611  sinfo_msg("RUN NORD SOUTH TEST\n") ;
612  ck0(sinfo_new_nst(cpl_func,config, set,set_fibre_ns),
613  "Running north south test");
614 
615 
616  if(pdensity < 2) {
617  cpl_frameset_erase(set,PRO_FIBRE_NS_STACKED_DIST);
618  }
619 
620  sinfo_msg("SUCCESS: RUNNED NORD SUD TEST\n") ;
621  sinfo_msg("SUCCESS: RECIPE\n") ;
622 
623  sinfo_free_frameset(&raw_set);
624  sinfo_fake_delete(&fk);
625 
626 
627  cleanup:
628 
629  sinfo_free_frameset(&set_on);
630  sinfo_free_frameset(&set_fibre_ns);
631  sinfo_free_frameset(&set_wave_ns);
632  sinfo_free_frameset(&set_flat_ns);
633  sinfo_free_image(&ima);
634  sinfo_free_propertylist(&plist) ;
635  sinfo_free_frameset(&set_off);
636  sinfo_free_frameset(&raw_set);
637  sinfo_free_frameset(&raw_set);
638  sinfo_fake_delete(&fk);
639 
640  if (cpl_error_get_code() != CPL_ERROR_NONE) {
641  return -1;
642  } else {
643  return 0;
644  }
645 
646 
647 }
648 
649 
650 
651 static int
652 new_pre_process(cpl_frameset* set,
653  const float lo_rej, const float hi_rej, const char* name_o)
654 {
655 
656  int nf=0;
657  int ng=10;
658  int ns=0;
659  int nr=0;
660  int nt=0;
661 
662  int i=0;
663  int j=0;
664  int k=0;
665  int lo_cut=0;
666  int hi_cut=0;
667 
668 
669  cpl_frame* frame=NULL;
670  cpl_frame* pframe=NULL;
671  cpl_frameset* sof=NULL;
672  cpl_frameset* fibre_raw_set=NULL;
673  char* file_name=NULL;
674 
675  cpl_propertylist* plist=NULL;
676  cpl_imagelist* imset_tmp=NULL;
677  cpl_imagelist* imset_tot=NULL;
678  cpl_image** avg_img_list=NULL;
679 
680  cpl_image* img=NULL;
681  cpl_image* img_tmp=NULL;
682  cpl_image* img_dup=NULL;
683 
684  cpl_image** img_list=NULL;
685 
686 
687  sof = cpl_frameset_duplicate(set);
688  fibre_raw_set = cpl_frameset_new();
689 
690  sinfo_contains_frames_kind(sof,fibre_raw_set,RAW_FIBRE_NS);
691 
692  nf = cpl_frameset_get_size(fibre_raw_set);
693  if(nf < 1) {
694  sinfo_msg_warning("No input frames in data set");
695  sinfo_free_frameset(&sof);
696  sinfo_free_frameset(&fibre_raw_set);
697  return -1;
698  }
699  frame = cpl_frameset_get_frame(fibre_raw_set,0);
700  file_name=(char*) cpl_frame_get_filename(frame);
701 
702 
703  if ((plist = cpl_propertylist_load(file_name, 0)) == NULL) {
704  sinfo_msg_error("getting header from ima frame %s",file_name);
705  return -1 ;
706  }
707 
708 
709  if( nf > ng) {
710 
711  sinfo_msg("Total raw frames nf=%d > max frm per group ng=%d",nf,ng);
712  ns = (nf+1)/ng;
713  nr = nf-ns*ng;
714  imset_tot=cpl_imagelist_new();
715  avg_img_list=cpl_malloc((ns+1) * sizeof(cpl_image *));
716 
717 
718  for (i=0;i<ns;i++) {
719  sinfo_msg("iteration i=%d\n",i);
720  imset_tmp=cpl_imagelist_new();
721  img_list=cpl_malloc(ng * sizeof(cpl_image *));
722 
723 
724  for (j=0;j<ng;j++) {
725  k=i*ng+j;
726  frame = cpl_frameset_get_frame(fibre_raw_set,k);
727  file_name=(char*)cpl_frame_get_filename(frame);
728  img_tmp=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
729  img_dup=cpl_image_duplicate(img_tmp);
730  cpl_imagelist_set(imset_tmp,img_dup,j);
731  cpl_image_delete(img_tmp);
732  }
733 
734 
735  nt=cpl_imagelist_get_size(imset_tmp);
736  lo_cut=(floor)(lo_rej*nt+0.5);
737  hi_cut=(floor)(hi_rej*nt+0.5);
738  avg_img_list[i]=cpl_imagelist_collapse_minmax_create(imset_tmp,
739  lo_cut,hi_cut);
740  cpl_imagelist_set(imset_tot,avg_img_list[i],i);
741  cpl_imagelist_delete(imset_tmp);
742  cpl_free(img_list);
743 
744  }
745 
746  if(ns*ng<nf) {
747  imset_tmp=cpl_imagelist_new();
748  img_list=cpl_malloc((nf-ns*ng) * sizeof(cpl_image *));
749  for(i=0;i<nr;i++) {
750  k=i+ns*ng;
751  frame = cpl_frameset_get_frame(fibre_raw_set,k);
752  file_name = (char*) cpl_frame_get_filename(frame);
753 
754  img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
755  cpl_imagelist_set(imset_tmp,img_list[i],i);
756  }
757  nt=cpl_imagelist_get_size(imset_tmp);
758  lo_cut=(floor)(lo_rej*nt+0.5);
759  hi_cut=(floor)(hi_rej*nt+0.5);
760 
761  avg_img_list[ns]=cpl_imagelist_collapse_minmax_create(imset_tmp,
762  lo_cut,hi_cut);
763  cpl_imagelist_set(imset_tot,avg_img_list[ns],ns);
764 
765  cpl_free(img);
766  cpl_imagelist_delete(imset_tmp);
767  cpl_free(img_list);
768  }
769 
770  } else {
771 
772  sinfo_msg("Total raw frames nf=%d < max frm per group ng=%d",nf,ng);
773  imset_tot=cpl_imagelist_new();
774  img_list=cpl_malloc(nf * sizeof(cpl_image *));
775  for (i=0;i<nf;i++) {
776  frame = cpl_frameset_get_frame(fibre_raw_set,i);
777  file_name = (char*) cpl_frame_get_filename(frame);
778  img_list[i]=cpl_image_load(file_name,CPL_TYPE_FLOAT,0,0);
779  cpl_imagelist_set(imset_tot,img_list[i],i);
780  }
781  cpl_free(img_list);
782  }
783  cpl_free(avg_img_list);
784 
785  cpl_frameset_delete(fibre_raw_set);
786  nt=cpl_imagelist_get_size(imset_tot);
787  lo_cut=(floor)(lo_rej*nt+0.5);
788  hi_cut=(floor)(hi_rej*nt+0.5);
789 
790  if( (img = cpl_imagelist_collapse_minmax_create(imset_tot,
791  lo_cut,hi_cut)) == NULL) {
792  sinfo_msg_error("Error code");
793  sinfo_msg_error("%s", (char* ) cpl_error_get_message());
794  cpl_imagelist_delete(imset_tot);
795  cpl_frameset_delete(sof);
796  cpl_propertylist_delete(plist) ;
797  return -1;
798  }
799 
800  if (cpl_image_save(img,name_o, CPL_BPP_IEEE_FLOAT,
801  plist,CPL_IO_DEFAULT)!=CPL_ERROR_NONE) {
802  sinfo_msg_error("Cannot save the product %s",name_o);
803  cpl_imagelist_delete(imset_tot);
804  cpl_frameset_delete(sof);
805  cpl_propertylist_delete(plist) ;
806  return -1 ;
807  }
808  cpl_imagelist_delete(imset_tot);
809  cpl_frameset_erase(set,RAW_FIBRE_NS);
810 
811  /* Create product frame */
812  pframe = cpl_frame_new();
813  cpl_frame_set_filename(pframe, name_o) ;
814  cpl_frame_set_tag(pframe, "FIBRE_NS") ;
815  cpl_frame_set_type(pframe, CPL_FRAME_TYPE_IMAGE) ;
816  cpl_frame_set_group(pframe, CPL_FRAME_GROUP_RAW) ;
817  cpl_frame_set_level(pframe, CPL_FRAME_LEVEL_FINAL) ;
818  if (cpl_error_get_code()) {
819  sinfo_msg_error("Error while initialising the product frame") ;
820  cpl_propertylist_delete(plist) ;
821  cpl_frame_delete(pframe) ;
822  cpl_image_delete(img) ;
823  return -1 ;
824  }
825 
826 
827  /* Save the file */
828  if (cpl_image_save(img, name_o, CPL_BPP_IEEE_FLOAT, plist,
829  CPL_IO_DEFAULT) != CPL_ERROR_NONE) {
830  sinfo_msg_error("Could not save product");
831  cpl_propertylist_delete(plist) ;
832  cpl_frame_delete(pframe) ;
833  cpl_image_delete(img) ;
834  return -1 ;
835  }
836  cpl_propertylist_delete(plist) ;
837  cpl_image_delete(img) ;
838 
839  /* Log the saved file in the input frameset */
840  cpl_frameset_insert(set, pframe) ;
841  cpl_frameset_delete(sof);
842 
843  return 0;
844 }