SINFONI Pipeline Reference Manual  2.5.2
sinfo_flat_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 : sinfo_flat_ini_by_cpl.c
22  Author : Andrea Modigliani
23  Created on : May 19, 2004
24  Description : read cpl input for SPIFFI
25  ---------------------------------------------------------------------------*/
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29 /*---------------------------------------------------------------------------
30  Includes
31  ---------------------------------------------------------------------------*/
32 #include <string.h>
33 #include "sinfo_flat_ini_by_cpl.h"
34 #include "sinfo_error.h"
35 #include "sinfo_hidden.h"
36 #include "sinfo_pro_types.h"
37 #include "sinfo_raw_types.h"
38 #include "sinfo_functions.h"
39 #include "sinfo_file_handling.h"
40 /*---------------------------------------------------------------------------
41  Functions private to this module
42  ---------------------------------------------------------------------------*/
43 void sinfo_flat_free_alloc(flat_config * cfg);
44 static void
45 parse_section_frames(flat_config *, cpl_frameset* sof,
46  cpl_frameset** raw, int* status);
47 static void parse_section_cleanmean(flat_config *, cpl_parameterlist* cpl_cfg);
48 static void parse_section_badpixel(flat_config *, cpl_parameterlist* cpl_cfg);
49 static void parse_section_badpix(flat_config *, cpl_parameterlist* cpl_cfg);
50 static void parse_section_thresh(flat_config *, cpl_parameterlist* cpl_cfg);
51 static void
52 parse_section_qclog(flat_config * cfg, cpl_parameterlist * cpl_cfg);
74 flat_config *
75 sinfo_parse_cpl_input_flat(cpl_parameterlist* cpl_cfg,
76  cpl_frameset* sof,
77  cpl_frameset** raw)
78 {
79  flat_config * cfg = sinfo_flat_cfg_create();
80  int status=0;
81  /*
82  * Perform sanity checks, fill up the structure with what was
83  * found in the ini file
84  */
85  parse_section_badpixel (cfg, cpl_cfg);
86  parse_section_cleanmean (cfg, cpl_cfg);
87  parse_section_badpix (cfg, cpl_cfg);
88  parse_section_thresh (cfg, cpl_cfg);
89  parse_section_qclog (cfg,cpl_cfg);
90 
91  parse_section_frames (cfg, sof, raw, &status);
92  if (status > 0) {
93  sinfo_msg_error("parsing cpl input");
94  sinfo_flat_cfg_destroy(cfg);
95  cfg = NULL ;
96  return NULL ;
97  }
98  return cfg ;
99 }
100 
110 static void
111 parse_section_frames(flat_config * cfg,
112  cpl_frameset * sof,
113  cpl_frameset ** raw,
114  int* status)
115 {
116  int i;
117 
118  char * name ;
119 
120  int nobj, noff ;
121  int nditherobj, nditheroff ;
122  int found_sky ;
123  int found_dither ;
124  //int nframes=0;
125  int nraw=0;
126  //char* tag;
127  char spat_res[FILE_NAME_SZ];
128  char lamp_status[FILE_NAME_SZ];
129  char band[FILE_NAME_SZ];
130  int ins_set=0;
131 
132  cpl_frame* frame = NULL;
133 
134 
135  //nframes = cpl_frameset_get_size(sof);
136 
137  sinfo_extract_raw_frames_type(sof,raw,RAW_FLAT_LAMP);
138  nraw=cpl_frameset_get_size(*raw);
139 
140  if (nraw==0) {
141  sinfo_extract_raw_frames_type(sof,raw,RAW_FLAT_NS);
142  }
143 
144 
145  nraw=cpl_frameset_get_size(*raw);
146  if (nraw==0) {
147  sinfo_msg("Frame %s or %s not found!", RAW_FLAT_LAMP,RAW_FLAT_NS);
148  (*status)++;
149  return ;
150  }
151 
152  nraw = cpl_frameset_get_size(*raw);
153 
154  sinfo_msg("nraw=%d",nraw);
155  if(nraw<1) {
156  sinfo_msg_error("no good raw frame in input, something wrong!");
157  (*status)++;
158  return;
159  }
160 
161  cknull_nomsg(frame = cpl_frameset_get_frame(*raw,0));
162 
163  ck0_nomsg(sinfo_get_spatial_res(frame,spat_res));
164  switch(sinfo_frame_is_on(frame))
165  {
166 
167  case 0:
168  strcpy(lamp_status,"on");
169  break;
170  case 1:
171  strcpy(lamp_status,"off");
172  break;
173  case -1:
174  strcpy(lamp_status,"undefined");
175  break;
176  default:
177  strcpy(lamp_status,"undefined");
178  break;
179 
180  }
181 
182  sinfo_get_band(frame,band);
183  sinfo_msg("Spatial resolution: %s lamp status: %s band: %s \n",
184  spat_res, lamp_status, band);
185 
186 
187  sinfo_get_ins_set(band,&ins_set);
188 
189 
190  /* Allocate structures to go into the blackboard */
191  cfg->framelist = cpl_malloc(nraw * sizeof(char*));
192  cfg->frametype = cpl_malloc(nraw * sizeof(int));
193  cfg->frameposition = cpl_malloc(nraw * sizeof(int));
194 
195  found_sky = 0 ;
196  found_dither = 0 ;
197  nobj = 0 ;
198  noff = 0 ;
199  nditheroff = 0 ;
200  nditherobj = 0 ;
201  for (i=0;i<nraw;i++) {
202  cfg->framelist[i]=NULL;
203  cfg->frametype[i]=-1;
204  cfg->frameposition[i]=-1;
205  }
206 
207 
208  cfg->nframes = nraw ;
209  /* Browse through the charmatrix to get names and file types */
210  for (i=0 ; i<nraw ; i++) {
211  frame = cpl_frameset_get_frame(*raw,i);
212  name= (char*) cpl_frame_get_filename(frame);
213  if(sinfo_file_exists(name)==1) {
214  /* to go on the file must exist */
215  if(cpl_frame_get_tag(frame) != NULL) {
216  /* If the frame has a tag we process it. Else it is an object */
217  //tag= (char*) cpl_frame_get_tag(frame);
218  /* sinfo_msg("frame %s tag =%s \n",name,tag); */
219  if((sinfo_frame_is_on(frame) == 0) ||
220  (sinfo_frame_is_sky(frame) == 1) )
221  {
222 
223  cfg->framelist[i]=(char*) cpl_frame_get_filename(frame);
224  cfg->frametype[i] = FRAME_OFF ;
225  found_sky = 1;
226  if (sinfo_frame_is_dither(frame))
227  {
228  cfg->frameposition[i] = FRAME_POS2 ;
229  found_dither = 1 ;
230  nditheroff++ ;
231  }
232  else
233  {
234  cfg->frameposition[i] = FRAME_POS1 ;
235  noff++ ;
236  }
237  }
238  else if(sinfo_frame_is_on(frame) == 1)
239  {
240  cfg->framelist[i]=(char*) cpl_frame_get_filename(frame);
241  cfg->frametype[i] = FRAME_ON ;
242  if (sinfo_frame_is_dither(frame))
243  {
244  cfg->frameposition[i] = FRAME_POS2 ;
245  found_dither = 1 ;
246  nditherobj++ ;
247  }
248  else
249  {
250  cfg->frameposition[i] = FRAME_POS1 ;
251  nobj++ ;
252  }
253  }
254 
255  }
256 
257  else
258  {
259  /* No type means an object */
260  /* No type means position 1 */
261  /*
262  cfg->frametype[i] = FRAME_ON ;
263  cfg->frameposition[i] = FRAME_POS1 ;
264  nobj ++ ;
265  */
266  }
267  }
268  /* Store file name into framelist */
269  /* sinfo_msg("frame=%s\n",cfg->framelist[i]); */
270  }
271 
272  /*
273  sinfo_msg("Noff= %d Nobj= %d Nditheroff= %d Nditherobj= %d",
274  noff,nobj,nditheroff,nditherobj);
275  */
276 
277 
278  if((nobj<1) && (nditherobj< 1)) {
279  sinfo_msg_error("no ON raw frame in input, something wrong!");
280  sinfo_flat_free_alloc(cfg);
281  (*status)++;
282  return;
283  }
284 
285  /* Copy relevant information into the blackboard */
286  cfg->nobj = nobj ;
287  cfg->noff = noff ;
288  cfg->nditherobj = nditherobj ;
289  cfg->nditheroff = nditheroff ;
290  cfg->contains_sky = found_sky ;
291  cfg->contains_dither = found_dither ;
292 
293  strcpy(cfg -> outName, LAMP_FLATS_OUT_FILENAME);
294 
295  if(cfg->interpolInd != 0) {
296 
297  if(NULL != cpl_frameset_find(sof,PRO_BP_MAP)) {
298  frame = cpl_frameset_find(sof,PRO_BP_MAP);
299  strcpy(cfg -> mask,cpl_frame_get_filename(frame));
300  if(sinfo_file_exists(cfg->mask)==1) {
301  } else {
302  sinfo_msg_error("Filename %s for Frame %s not found!",
303  cfg->mask, PRO_BP_MAP);
304  sinfo_flat_free_alloc(cfg);
305  (*status)++;
306  return;
307 
308  }
309 
310  } else {
311  sinfo_msg_error("Frame %s not found!", PRO_BP_MAP);
312  sinfo_flat_free_alloc(cfg);
313  (*status)++;
314  return;
315  }
316  if(NULL != cpl_frameset_find(sof,PRO_SLIT_POS)) {
317  frame = cpl_frameset_find(sof,PRO_SLIT_POS);
318  strcpy(cfg -> slitposList,cpl_frame_get_filename(frame));
319  if(sinfo_file_exists(cfg->mask) == 1) {
320  } else {
321  sinfo_msg_error("Filename %s for Frame %s not found!",
322  cfg->slitposList, PRO_SLIT_POS);
323  sinfo_flat_free_alloc(cfg);
324  (*status)++;
325  return;
326  }
327  } else {
328  sinfo_msg_error("Frame %s not found!", PRO_SLIT_POS);
329  sinfo_flat_free_alloc(cfg);
330  (*status)++;
331  return;
332  }
333 
334  }
335 
336  cleanup:
337 
338  return;
339 }
340 
348 static void
349 parse_section_cleanmean(flat_config * cfg, cpl_parameterlist * cpl_cfg)
350 {
351  cpl_parameter *p;
352 
353  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.low_rejection");
354  cfg -> loReject = cpl_parameter_get_double(p);
355 
356  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.high_rejection");
357  cfg -> hiReject = cpl_parameter_get_double(p);
358 
359 }
360 
368 static void
369 parse_section_badpixel(flat_config * cfg, cpl_parameterlist * cpl_cfg)
370 {
371  cpl_parameter *p;
372 
373  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.interpol_index");
374  cfg -> interpolInd = cpl_parameter_get_bool(p);
375 
376  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.max_rad");
377  cfg -> maxRad = cpl_parameter_get_int(p);
378 
379 }
380 
388 static void
389 parse_section_badpix(flat_config * cfg, cpl_parameterlist * cpl_cfg)
390 {
391 
392  cpl_parameter* p;
393 
394  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.bad_ind");
395  cfg -> badInd = cpl_parameter_get_bool(p);
396 
397  strcpy(cfg -> maskname, LAMP_FLATS_OUT_BPMAP);
398 
399  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.sigma_factor");
400  cfg -> sigmaFactor = cpl_parameter_get_double(p);
401 
402  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.factor");
403  cfg -> factor = cpl_parameter_get_double(p);
404 
405  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.iterations");
406  cfg -> iterations = cpl_parameter_get_int(p);
407 
408  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.bad_low_rejection");
409  cfg -> badLoReject = cpl_parameter_get_double(p);
410 
411  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.bad_high_rejection");
412  cfg -> badHiReject = cpl_parameter_get_double(p);
413 
414  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.llx");
415  cfg -> llx = cpl_parameter_get_int(p);
416 
417  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.lly");
418  cfg -> lly = cpl_parameter_get_int(p);
419 
420  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.urx");
421  cfg -> urx = cpl_parameter_get_int(p);
422 
423  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.ury");
424  cfg -> ury = cpl_parameter_get_int(p);
425 
426 }
427 
435 static void
436 parse_section_thresh(flat_config * cfg,cpl_parameterlist* cpl_cfg)
437 {
438 
439  cpl_parameter* p;
440  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.thresh_ind");
441  cfg -> threshInd =cpl_parameter_get_bool(p);
442 
443  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.mean_factor");
444  cfg -> meanfactor = cpl_parameter_get_double(p);
445 
446 
447 }
448 
449 
457 static void
458 parse_section_qclog(flat_config * cfg, cpl_parameterlist * cpl_cfg)
459 {
460  cpl_parameter *p;
461 
462  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_xmin1");
463  cfg -> qc_fpn_xmin1 = cpl_parameter_get_int(p);
464 
465  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_xmax1");
466  cfg -> qc_fpn_xmax1 = cpl_parameter_get_int(p);
467 
468  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_ymin1");
469  cfg -> qc_fpn_ymin1 = cpl_parameter_get_int(p);
470 
471  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_ymax1");
472  cfg -> qc_fpn_ymax1 = cpl_parameter_get_int(p);
473 
474  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_xmin2");
475  cfg -> qc_fpn_xmin2 = cpl_parameter_get_int(p);
476 
477  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_xmax2");
478  cfg -> qc_fpn_xmax2 = cpl_parameter_get_int(p);
479 
480  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_ymin2");
481  cfg -> qc_fpn_ymin2 = cpl_parameter_get_int(p);
482 
483  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_fpn_ymax2");
484  cfg -> qc_fpn_ymax2 = cpl_parameter_get_int(p);
485 
486  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_thresh_min");
487  cfg -> qc_thresh_min = cpl_parameter_get_int(p);
488 
489  p = cpl_parameterlist_find(cpl_cfg, "sinfoni.lamp_flats.qc_thresh_max");
490  cfg -> qc_thresh_max = cpl_parameter_get_int(p);
491 
492 }
500 void
501 sinfo_flat_free(flat_config ** cfg)
502 {
503  if ((*cfg) != NULL) {
504  sinfo_flat_free_alloc(*cfg);
505  sinfo_flat_cfg_destroy(*cfg);
506  *cfg=NULL;
507  }
508  return;
509 
510 }
518 void
519 sinfo_flat_free_alloc(flat_config * cfg)
520 {
521  if(cfg->frametype != NULL){
522  cpl_free(cfg->frametype);
523  }
524  if(cfg->framelist != NULL) {
525  cpl_free(cfg->framelist);
526  }
527  if(cfg->frameposition != NULL) {
528  cpl_free(cfg->frameposition);
529  }
530  return ;
531 }