SINFONI Pipeline Reference Manual  2.5.2
sinfo_stack_ini_by_cpl.c
1 /*
2  * This file is part of the ESO SINFONI Pipeline
3  * Copyright (C) 2004,2005 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18  */
19 /*----------------------------------------------------------------------------
20 
21  File name : stack_ini_by_cpl.c
22  Author : Andrea Modigliani
23  Created on : May 23, 2004
24  Description : prepare stacked frames cpl input handling for SPIFFI
25 
26  ---------------------------------------------------------------------------*/
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 /*---------------------------------------------------------------------------
33  Includes
34  ---------------------------------------------------------------------------*/
35 
36 #include <string.h>
37 #include "sinfo_stack_ini_by_cpl.h"
38 #include "sinfo_error.h"
39 #include "sinfo_pro_types.h"
40 #include "sinfo_raw_types.h"
41 #include "sinfo_ref_types.h"
42 #include "sinfo_functions.h"
43 
44 #include "sinfo_file_handling.h"
45 
46 /*---------------------------------------------------------------------------
47  Functions private to this module
48  ---------------------------------------------------------------------------*/
49 static void sinfo_stack_free_alloc(stack_config_n * cfg);
50 
51 
52 static void
53 parse_section_frames(stack_config_n *,
54  cpl_frameset* sof,
55  cpl_frameset** raw,
56  int* status,
57  fake* fk);
58 
59 static void
60 parse_section_cleanmean(stack_config_n *, cpl_parameterlist* cpl_cfg);
61 static void
62 parse_section_flatfield(stack_config_n *, cpl_parameterlist* cpl_cfg);
63 static void
64 parse_section_badpixel(stack_config_n *, cpl_parameterlist* cpl_cfg);
65 static void
66 parse_section_interleaving(stack_config_n *);
67 static void
68 parse_section_gaussconvolution(stack_config_n *, cpl_parameterlist* cpl_cfg);
69 static void
70 parse_section_shiftframes(stack_config_n *);
71 static void
72 parse_section_warpfix(stack_config_n *, cpl_parameterlist* cpl_cfg);
73 static void
74 parse_section_qclog(stack_config_n *, cpl_parameterlist* cpl_cfg);
82 /* generateStack_ini_file */
83 /*-------------------------------------------------------------------------*/
94 /*--------------------------------------------------------------------------*/
95 
96 stack_config_n * sinfo_parse_cpl_input_stack(cpl_parameterlist* cpl_cfg,
97  cpl_frameset* sof,
98  cpl_frameset** raw,
99  fake* fk)
100 {
101  stack_config_n * cfg =sinfo_stack_cfg_create_n();
102  int status=0;
103 
104 
105 
106  /*
107  * Perform sanity checks, fill up the structure with what was
108  * found in the ini file
109  */
110 
111  parse_section_cleanmean (cfg, cpl_cfg);
112  parse_section_flatfield (cfg, cpl_cfg);
113  parse_section_badpixel (cfg, cpl_cfg);
114  parse_section_interleaving (cfg);
115  parse_section_gaussconvolution (cfg, cpl_cfg);
116  parse_section_shiftframes (cfg);
117  parse_section_warpfix (cfg, cpl_cfg);
118  parse_section_qclog (cfg, cpl_cfg);
119  parse_section_frames (cfg, sof, raw, &status, fk);
120  if (status > 0) {
121  sinfo_msg_error("parsing cpl input");
122  sinfo_stack_cfg_destroy_n(cfg);
123  cfg = NULL ;
124  return NULL ;
125  }
126  return cfg ;
127 }
137 static void
138 parse_section_frames(stack_config_n * cfg,
139  cpl_frameset* sof, cpl_frameset** raw_set,int* status,
140  fake* fk)
141 {
142 
143  int i;
144  const char * name;
145  char file[FILE_NAME_SZ];
146  int nobj, noff, ndark ;
147  int nditherobj, nditheroff ;
148  int found_sky ;
149  int found_ref ;
150  int found_dither ;
151  int found_dark ;
152  int nraw=0;
153  //int nsof=0;
154 
155  cpl_frame* frame = NULL;
156 
157  char spat_res[FILE_NAME_SZ];
158  char lamp_status[FILE_NAME_SZ];
159  char band[FILE_NAME_SZ];
160  int ins_set=0;
161  char* tag=NULL;
162  char* do_class=NULL;
163 
164  //nsof = cpl_frameset_get_size(sof);
165 
166 
167  do_class=fk->pro_class;
168  /* sinfo_msg("do_class=%s",do_class); */
169  if(strcmp(do_class,"DEFAULT") == 0) {
170  sinfo_extract_raw_stack_frames(sof,raw_set);
171  } else if (strcmp(do_class,PRO_FIBRE_NS_STACKED_DIST) == 0) {
172  sinfo_is_fibres_on_off(sof,*raw_set);
173  } else if (strcmp(do_class,PRO_FIBRE_NS_STACKED) == 0) {
174  sinfo_contains_frames_kind(sof,*raw_set,(char*)PRO_FIBRE_NS_STACKED);
175  } else if (strcmp(do_class,RAW_STACKED_SLITPOS) == 0) {
176  sinfo_extract_raw_stack_frames(sof,raw_set);
177  } else {
178  sinfo_extract_raw_frames_type(sof,raw_set,do_class);
179  }
180  nraw = cpl_frameset_get_size(*raw_set);
181  if (nraw < 1) {
182  sinfo_msg_error("Too few raw frames present in frameset!");
183  (*status)++;
184  return;
185  }
186 
187 
188  /* Allocate structures to go into the blackboard */
189  cfg->framelist = cpl_malloc(nraw * sizeof(char*));
190  cfg->frametype = cpl_malloc(nraw * sizeof(int));
191  cfg->frameposition = cpl_malloc(nraw * sizeof(int));
192 
193  for (i=0;i<nraw;i++) {
194  cfg->framelist[i]=NULL;
195  cfg->frametype[i]=-1;
196  cfg->frameposition[i]=-1;
197  }
198 
199  found_sky = 0 ;
200  found_ref = 0 ;
201  found_dither = 0 ;
202  found_dark = 0 ;
203  nobj = 0 ;
204  noff = 0 ;
205  ndark = 0 ;
206  nditherobj = 0 ;
207  nditheroff = 0 ;
208 
209 
210 
211  /* Browse through the charmatrix to get names and file types */
212  /* for (i=0 ; i<nraw ; i++) { */
213  for (i=0 ; i<nraw ; i++) {
214  frame = cpl_frameset_get_frame(*raw_set,i);
215  name=cpl_frame_get_filename(frame);
216  if(sinfo_file_exists((char*)name)==1) {
217  /* to go on the file must exist */
218  if(cpl_frame_get_tag(frame) != NULL) {
219  /* If the frame has a tag we process it. Else it is an object */
220  tag= (char*) cpl_frame_get_tag(frame);
221  if((sinfo_frame_is_on(frame) == 0) ||
222  (sinfo_frame_is_sky(frame) == 1))
223  {
224  cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
225  cfg->frametype[i] = FRAME_OFF ;
226  found_sky = 1;
227  if (sinfo_frame_is_dither(frame))
228  {
229  cfg->frameposition[i] = FRAME_POS2 ;
230  nditheroff++ ;
231  }
232  else
233  {
234  cfg->frameposition[i] = FRAME_POS1 ;
235  noff++ ;
236  }
237  }
238  else if(strstr(tag,RAW_REF) != NULL)
239  {
240  cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
241  cfg->frametype[i] = FRAME_REF ;
242  found_ref=1;
243  if (sinfo_frame_is_dither(frame))
244  {
245  cfg->frameposition[i] = FRAME_POS2 ;
246  }
247  else
248  {
249  cfg->frameposition[i] = FRAME_POS1 ;
250  }
251  }
252  else if(sinfo_is_dark(tag))
253  {
254  cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
255  cfg->frametype[i] = FRAME_DRK ;
256  cfg->frameposition[i] = FRAME_POS1 ;
257  found_dark=1;
258  ndark++;
259  sinfo_msg("Frame is sinfo_dark on\n");
260  }
261  else
262  {
263 
264  cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
265  cfg->frametype[i] = FRAME_ON ;
266  found_ref=1;
267  if (sinfo_frame_is_dither(frame))
268  {
269  cfg->frameposition[i] = FRAME_POS2 ;
270  found_dither=1;
271  nditherobj++;
272  }
273  else
274  {
275  cfg->frameposition[i] = FRAME_POS1 ;
276  nobj++;
277  }
278  }
279  }
280  else
281  {
282  /* No type means an object */
283  cfg->frametype[i] = FRAME_ON ;
284  /* No type means position 1 */
285  cfg->frameposition[i] = FRAME_POS1 ;
286 
287  nobj ++ ;
288  }
289  }
290 
291  /* Store file name into framelist */
292  }
293 
294  sinfo_msg("Noff= %d Nobj= %d Nditheroff= %d Nditherobj= %d",
295  noff,nobj,nditheroff,nditherobj);
296 
297 
298 
299  /* Copy relevant information into the blackboard */
300  cfg->nframes = nraw ;
301  cfg->nobj = nobj ;
302  cfg->noff = noff ;
303  cfg->ndark = ndark ;
304  cfg->nditherobj = nditherobj ;
305  cfg->nditheroff = nditheroff ;
306  cfg->contains_sky = found_sky ;
307  cfg->contains_ref = found_ref ;
308  cfg->contains_dither = found_dither ;
309  cfg->contains_dark = found_dark ;
310 
311 
312  frame = cpl_frameset_get_frame(*raw_set,0);
313 
314  ck0_nomsg(sinfo_get_spatial_res(frame,spat_res));
315 
316 
317 
318  if(sinfo_frame_is_on(frame) == 1) {
319  strcpy(lamp_status,"on");
320  } else {
321  strcpy(lamp_status,"off");
322  }
323  switch(sinfo_frame_is_on(frame))
324  {
325  case 0:
326  strcpy(lamp_status,"on");
327  break;
328  case 1:
329  strcpy(lamp_status,"off");
330  break;
331  case -1:
332  strcpy(lamp_status,"undefined");
333  break;
334  default:
335  strcpy(lamp_status,"undefined");
336  break;
337 
338 
339  }
340  sinfo_get_band(frame,band);
341 
342 
343  sinfo_msg("Spatial resolution: %s lamp status: %s band: %s",
344  spat_res, lamp_status, band);
345 
346  sinfo_get_ins_set(band,&ins_set);
347  frame = cpl_frameset_get_frame(*raw_set,0);
348  tag=(char*)cpl_frame_get_tag(frame);
349 
350 
351  /* Update flatInd setting if we had changed the mflat switch */
352  /* take care of NS test special case */
353  if(fk->frm_switch==1) {
354  /*
355  In this case we force certain values indipendently from the
356  setting occurring during CPL parameters parsing
357  */
358 
359  cfg->maskInd = fk->mask_index;
360  cfg->indind = fk->ind_index;
361  cfg->flatInd = fk->flat_index;
362  cfg -> loReject = fk->low_rej;
363  cfg -> hiReject = fk->hig_rej;
364  cfg -> warpfixInd = fk->wfix_index;
365  /*
366  sinfo_msg("Fake frame: reset parameter values: ");
367  sinfo_msg("maskInd: %d", cfg->maskInd);
368  sinfo_msg("indind: %d", cfg->indind);
369  sinfo_msg("flatInd: %d", cfg->flatInd);
370  sinfo_msg("loReject: %f", cfg->loReject);
371  sinfo_msg("hiReject: %f", cfg->hiReject);
372  */
373  }
374 
375 
376 
377 
378 
379  if(cfg -> flatInd) {
380  if(NULL != cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP)) {
381  frame = cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP);
382  strcpy(file,cpl_frame_get_filename(frame));
383  strcpy(cfg -> flatfield1, file);
384  } else if(NULL != cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP1)) {
385  frame = cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP1);
386  strcpy(file,cpl_frame_get_filename(frame));
387  strcpy(cfg -> flatfield1, file);
388  } else {
389  sinfo_msg_error("Frame %s not found!", PRO_MASTER_FLAT_LAMP);
390  sinfo_msg_error("Frame %s not found!", PRO_MASTER_FLAT_LAMP1);
391  sinfo_stack_free_alloc(cfg);
392  (*status)++;
393  return;
394  }
395 
396  if(found_dither) {
397  if(NULL != cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP2)) {
398  frame = cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP2);
399  strcpy(file,cpl_frame_get_filename(frame));
400  strcpy(cfg -> flatfield2, file);
401  } else {
402  sinfo_msg("Frame %s not found!", PRO_MASTER_FLAT_LAMP2);
403  }
404  }
405  }
406 
407  /* bad pixel section */
408  if(cfg->maskInd != 0) {
409  if(strstr(do_class,"FIBRE_NS") != NULL) {
410  if(NULL != cpl_frameset_find(sof,PRO_BP_MAP_DI)) {
411  frame = cpl_frameset_find(sof,PRO_BP_MAP_DI);
412  strcpy(file,cpl_frame_get_filename(frame));
413  strcpy(cfg -> mask, file);
414  } else {
415  sinfo_msg_error("Frame %s not found!", PRO_BP_MAP_DI);
416  sinfo_stack_free_alloc(cfg);
417  (*status)++;
418  return;
419  }
420 
421  } else {
422 
423  if(NULL != cpl_frameset_find(sof,PRO_MASTER_BP_MAP)) {
424  frame = cpl_frameset_find(sof,PRO_MASTER_BP_MAP);
425  strcpy(file,cpl_frame_get_filename(frame));
426  strcpy(cfg -> mask, file);
427  } else {
428  sinfo_msg_error("Frame %s not found!", PRO_MASTER_BP_MAP);
429  sinfo_stack_free_alloc(cfg);
430  (*status)++;
431  return;
432  }
433 
434  }
435 
436  if (strcmp(do_class,RAW_STACKED_SLITPOS) == 0) {
437  cfg -> indind = 1;
438  }
439 
440  if(cfg -> indind == 0) {
441  if(NULL != cpl_frameset_find(sof,PRO_SLIT_POS)) {
442  frame = cpl_frameset_find(sof,PRO_SLIT_POS);
443  strcpy(file,cpl_frame_get_filename(frame));
444  strcpy(cfg -> slitposList, file);
445  sinfo_msg("Using %s to interpolate bad pixels",
446  PRO_SLIT_POS);
447  cpl_error_reset();
448  } else if(NULL != cpl_frameset_find(sof,PRO_SLIT_POS_GUESS)) {
449  frame = cpl_frameset_find(sof,PRO_SLIT_POS_GUESS);
450  strcpy(file,cpl_frame_get_filename(frame));
451  strcpy(cfg -> slitposList, file);
452  sinfo_msg("Using %s to interpolated bad pixels",
453  PRO_SLIT_POS_GUESS);
454  cpl_error_reset();
455  } else {
456  sinfo_msg_error("Frame %s nor %s found!",
457  PRO_SLIT_POS,PRO_SLIT_POS_GUESS);
458  sinfo_stack_free_alloc(cfg);
459  (*status)++;
460  return;
461  }
462 
463 
464  }
465  if(cfg -> maskInd == 2) {
466  if(NULL != cpl_frameset_find(sof,PRO_INDEX_LIST)) {
467  frame = cpl_frameset_find(sof,PRO_INDEX_LIST);
468  strcpy(file,cpl_frame_get_filename(frame));
469  strcpy(cfg ->indexlist, file);
470  } else {
471  sinfo_msg_error("Frame %s not found!", PRO_INDEX_LIST);
472  sinfo_stack_free_alloc(cfg);
473  (*status)++;
474  return;
475  }
476 
477  }
478  }
479 
480  cfg -> warpfixInd = fk->wfix_index;
481  if(cfg->warpfixInd != 0) {
482  if(NULL != cpl_frameset_find(sof,PRO_DISTORTION)) {
483  frame = cpl_frameset_find(sof,PRO_DISTORTION);
484  strcpy(file,cpl_frame_get_filename(frame));
485  strcpy(cfg -> polyFile, file);
486  } else {
487  sinfo_msg_error("Frame %s not found!", PRO_DISTORTION);
488  sinfo_stack_free_alloc(cfg);
489  (*status)++;
490  return;
491  }
492  }
493 
494  if(NULL != cpl_frameset_find(sof,PRO_MASTER_DARK)) {
495  frame = cpl_frameset_find(sof,PRO_MASTER_DARK);
496  strcpy(file,cpl_frame_get_filename(frame));
497  strcpy(cfg -> mdark, file);
498  cfg->mdark_ind=1;
499 
500  } else {
501  sinfo_msg("Frame %s not found", PRO_MASTER_DARK);
502  cfg->mdark_ind=0;
503  }
504 
505  cleanup:
506  return ;
507 }
508 
516 static void
517 parse_section_cleanmean(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
518 {
519 
520  cpl_parameter* p;
521  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.low_rejection");
522  cfg -> loReject = cpl_parameter_get_double(p);
523 
524  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.high_rejection");
525  cfg -> hiReject = cpl_parameter_get_double(p);
526 
527  return ;
528 }
535 static void
536 parse_section_flatfield(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
537 {
538 
539  cpl_parameter* p;
540  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.flat_index");
541  cfg -> flatInd = cpl_parameter_get_bool(p);
542 
543 }
550 static void
551 parse_section_badpixel(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
552 {
553  cpl_parameter* p;
554 
555 
556  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.ind_index");
557  cfg -> indind = cpl_parameter_get_bool(p);
558 
559  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.mask_index");
560  cfg -> maskInd = cpl_parameter_get_int(p);
561 
562  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.mask_rad");
563  cfg -> maxRad = cpl_parameter_get_int(p);
564  /*
565  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.sigma_factor");
566  */
567  cfg -> sigmaFactor = 3.;
568 
569 }
575 static void
576 parse_section_interleaving(stack_config_n * cfg)
577 {
578  cfg -> interInd = 0;
579  cfg -> noRows = 400;
580 
581 }
588 static void
589 parse_section_gaussconvolution(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
590 {
591 
592  cpl_parameter* p;
593  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.gauss_index");
594  cfg -> gaussInd = cpl_parameter_get_bool(p);
595 
596  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.kernel_half_width");
597  cfg -> hw = cpl_parameter_get_int(p);
598 
599 }
605 static void
606 parse_section_shiftframes(stack_config_n * cfg)
607 {
608  /*
609  cpl_parameter* p;
610  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.shift_frame_index");
611  cfg -> sfInd = cpl_parameter_get_bool(p);
612 
613  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.shift_frame_type");
614  cfg -> sfType = cpl_parameter_get_int(p);
615 
616  p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.shift_frame_order");
617  cfg -> sfOrder = cpl_parameter_get_int(p);
618  */
619 
620  cfg -> sfInd = 0;
621  cfg -> sfType = 1;
622  cfg -> sfOrder = 2;
623 
624 }
631 static void
632 parse_section_warpfix(stack_config_n * cfg,cpl_parameterlist * cpl_cfg)
633 {
634 
635  cpl_parameter* p;
636 
637  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.warpfix_ind");
638  cfg -> warpfixInd = cpl_parameter_get_bool(p);
639 
640 
641  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.warpfix_kernel");
642  strcpy(cfg -> kernel, cpl_parameter_get_string(p));
643 
644  return ;
645 }
646 
653 static void
654 parse_section_qclog(stack_config_n * cfg,cpl_parameterlist * cpl_cfg)
655 {
656 
657  cpl_parameter* p;
658 
659  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.qc_thresh_min");
660  cfg -> qc_thresh_min = cpl_parameter_get_int(p);
661 
662  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.qc_thresh_max");
663  cfg -> qc_thresh_max = cpl_parameter_get_int(p);
664 
665  return ;
666 }
672 void
673 sinfo_stack_free(stack_config_n ** cfg)
674 {
675  if(*cfg != NULL) {
676  sinfo_stack_free_alloc(*cfg);
677  sinfo_stack_cfg_destroy_n(*cfg);
678  *cfg = NULL;
679  }
680  return;
681 
682 }
683 
689 static void
690 sinfo_stack_free_alloc(stack_config_n * cfg)
691 {
692  int i=0;
693  for (i=0; i< cfg->nframes; i++) {
694  if(cfg->framelist[i] != NULL) {
695  cpl_free(cfg->framelist[i]);
696  cfg->framelist[i]=NULL;
697  }
698  }
699  if(cfg->frametype != NULL) {
700  cpl_free(cfg->frametype);
701  cfg->frametype=NULL;
702  }
703  if(cfg->framelist != NULL) {
704  cpl_free(cfg->framelist);
705  cfg->framelist=NULL;
706  }
707  if(cfg->frameposition != NULL) {
708  cpl_free(cfg->frameposition);
709  cfg->frameposition=NULL;
710  }
711 }