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