SINFONI Pipeline Reference Manual  2.6.0
sinfo_lamp_ini.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 : lamp_ini.c
22  Author : Juergen Schreiber
23  Created on :Mar 08, 2002
24  Description :prepare lamp spectrum frames ini file handling for SPIFFI
25  ---------------------------------------------------------------------------*/
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29 /*---------------------------------------------------------------------------
30  Includes
31  ---------------------------------------------------------------------------*/
32 #include "sinfo_lamp_ini.h"
33 /*---------------------------------------------------------------------------
34  Functions private to this module
35  ---------------------------------------------------------------------------*/
36 static void
37 parse_section_general(dictionary *, lamp_config *, int *);
38 static void
39 parse_section_resampling(dictionary *, lamp_config *, int *);
40 static void
41 parse_section_extractspectrum(dictionary *, lamp_config *, int *);
42 
50 /*-------------------------------------------------------------------------*/
66 /*--------------------------------------------------------------------------*/
67 
68 int
69 generateLamp_ini_file(char * ini_name, char * name_i, char * name_o,
70  char * name_c)
71 {
72  FILE * ini_file;
73 
74  if (sinfo_file_exists(ini_name)) {
75  sinfo_msg_warning("overwriting %s", ini_name);
76  }
77  if ((ini_file = fopen(ini_name, "w")) == (FILE*) NULL ) {
78  sinfo_msg_error("cannot create .ini file %s", ini_name);
79  return -1;
80  }
81 
82  fprintf(ini_file,
83  "#\n"
84  "# Configuration file for the extraction of a halogen lamp spectrumn"
85  "#\n");
86 
87  fprintf(ini_file, "#\n\n[Eclipse]\n");
88  fprintf(ini_file, "VersionNumber = %s\n\n", get_eclipse_version());
89  fprintf(ini_file,
90  "\n"
91  "#\n"
92  "# ----- [General] configures various software stuff\n"
93  "#\n"
94  "# This section is not mandatory. All eclipse routines\n"
95  "# should be set once for all in a .eclipse-rc file.\n"
96  "# If you choose to use the variables here, they will\n"
97  "# override the settings you have in the environment.\n"
98  "# See the eclipse installation manual to see what\n"
99  "# these variables refer to.\n"
100  "#\n"
101  "\n"
102  "[General]\n"
103  "# MaximumMemory = 512 ; integer, maximum megs to allocate in RAM\n"
104  "# MaximumSwap = 2048 ; integer, maximum megs to allocate in swap\n"
105  "# TmpDirName = . ; path to temporary directory\n"
106  "\n"
107  "Verbose = no ; verbose mode activation\n"
108  "Debug = no ; debug mode activation\n"
109  "\n"
110  "# LogFile = yes ; activate message logging to a file\n"
111  "# LogFileName = /tmp/spiffi-log ; log file name\n"
112  "\n"
113  "\n");
114  fprintf(ini_file,
115  "#\n"
116  "# the following are the names given in the argument of the python script\n"
117  "#\n"
118  "\n");
119  fprintf(ini_file, "InFrame = %s ; input file name\n"
120  "Wavemapim = %s ; file name of the wavelength map\n"
121  "OutName = %s ; name of output fits file\n", name_i,
122  name_c, name_o);
123 
124  fprintf(ini_file,
125  "#\n"
126  "# [Resampling] resamples the spectra to a given pixel length\n"
127  "# ExtractSpectrum] takes the clean mean along the spatial pixels \n"
128  "# by avoiding the bad pixel positions and delivers the final \n"
129  "# halogen lamp\n" "# spectrum\n"
130  "\n"
131  "[Resampling]\n"
132  "Ncoeffs = 3 ; number of coefficients for the polynomial\n"
133  " interpolation\n"
134  "Nrows = 2560 ; number of image rows in the resampled frame\n"
135  " (2560 for single frames, 5120 for dithered)\n"
136  "[ExtractSpectrum]\n"
137  "LoReject = 0.1 ; percentage of rejected low intensity pixels\n"
138  " before averaging\n"
139  "HiReject = 0.1 ; percentage of rejected high intensity pixels\n"
140  " before averaging\n"
141  "CountsToIntensity = 1. ; intensity conversion factor: counts per\n"
142  " intensity unit\n"
143  "\n"
144  "\n");
145 
146  fclose(ini_file);
147  return 0;
148 }
149 
150 /*-------------------------------------------------------------------------*/
161 /*--------------------------------------------------------------------------*/
162 
163 lamp_config *
164 parse_lamp_ini_file(char * ini_name)
165 {
166  dictionary * sym;
167  lamp_config * cfg;
168  int status;
169 
170  if (!sinfo_file_exists(ini_name)) {
171  sinfo_msg_error("cannot find ini file [%s]: aborting", ini_name);
172  return NULL ;
173  }
174  sym = iniparser_load(ini_name);
175  if (sym == NULL ) {
176  sinfo_msg_error("in parsing ini file [%s]: aborting", ini_name);
177  return NULL ;
178  }
179 
180  cfg = sinfo_lamp_cfg_create();
181  if (cfg == NULL ) {
182  sinfo_msg_error("allocating lamp_config struct");
183  iniparser_freedict(sym);
184  return NULL ;
185  }
186 
187  /*
188  * Perform sanity checks, fill up the structure with what was
189  * found in the ini file
190  */
191 
192  status = 0;
193  parse_section_general(sym, cfg, &status);
194  parse_section_resampling(sym, cfg, &status);
195  parse_section_extractspectrum(sym, cfg, &status);
196 
197  iniparser_freedict(sym);
198 
199  if (status > 0) {
200  sinfo_msg_error("%d errors in ini file [%s]", status, ini_name);
201  sinfo_lamp_cfg_destroy(cfg);
202  cfg = NULL;
203  return NULL ;
204  }
205  return cfg;
206 }
207 
208 /*---------------------------------------------------------------------------
209  Functions: parse_section_xxx()
210  In : symbolic table read from ini file
211  Out : void
212  Job : update a lamp_config structure from what can be
213  found in the ini file.
214  Notice : all of these functions update a status integer to
215  indicate if an error occurred, or leave it as it is if
216  everything went Ok.
217 
218  parse_section_general()
219  parse_section_resampling ()
220  parse_section_extractspectrum ()
221 
222  ---------------------------------------------------------------------------*/
223 
224 static void
225 parse_section_general(dictionary * sym, lamp_config * cfg, int *status)
226 {
227  char * cval;
228  int ival;
229 
230  /*
231  * General section
232  */
233  cval = iniparser_getstr(sym, "eclipse:versionnumber");
234  if (cval != NULL ) {
235  if (strcmp(cval, get_eclipse_version())) {
236  sinfo_msg_warning("this ini file produced by version %s", cval);
237  sinfo_msg_warning("you are running version %s",
238  get_eclipse_version());
239  }
240  }
241  else {
242  sinfo_msg_warning("no eclipse version number found in file");
243  }
244 
245  ival = iniparser_getint(sym, "general:maximummemory", -1);
246  if (ival > 0)
247  sinfo_set_memory_parameter("max_ram", ival);
248  ival = iniparser_getint(sym, "general:maximumswap", -1);
249  if (ival > 0)
250  sinfo_set_memory_parameter("max_swap", ival);
251  sinfo_set_verbose(iniparser_getboolean(sym, "general:verbose", 0));
252  sinfo_set_debug(iniparser_getboolean(sym, "general:debug", 0));
253 
254  cval = iniparser_getstr(sym, "general:tmpdirname");
255  if (cval != NULL ) {
256  sinfo_set_tmpdirname(cval);
257  }
258  else {
259  sinfo_set_tmpdirname(".");
260  }
261 
262  ival = iniparser_getboolean(sym, "general:logfile", 0);
263  if (ival) {
264  cval = iniparser_getstr(sym, "general:logfilename");
265  if (cval != NULL ) {
266  sinfo_set_logfile(1);
267  sinfo_set_logfilename(cval);
268  }
269  else {
270  sinfo_set_logfile(0);
271  }
272  }
273  cval = iniparser_getstr(sym, "general:inframe");
274  if (cval != NULL ) {
275  strcpy(cfg->inFrame, cval);
276  }
277  else {
278  sinfo_msg_error(" InFrame in the .ini file was not found!\n");
279  (*status)++;
280  }
281  cval = iniparser_getstr(sym, "general:wavemapim");
282  if (cval != NULL ) {
283  strcpy(cfg->wavemapim, cval);
284  }
285  else {
286  sinfo_msg_error(" Wavemapim in the .ini file was not found!\n");
287  (*status)++;
288  }
289 
290  cval = iniparser_getstr(sym, "general:outname");
291  if (cval != NULL ) {
292  strcpy(cfg->outName, cval);
293  }
294  else {
295  sinfo_msg_error(" OutName in the .ini file was not found!\n");
296  (*status)++;
297  }
298 
299  if (sinfo_verbose_active())
300  sinfo_print_memory_parameters();
301  return;
302 }
303 
304 static void
305 parse_section_resampling(dictionary * sym, lamp_config * cfg, int *status)
306 {
307  int ival;
308 
309  ival = iniparser_getint(sym, "resampling:ncoeffs", -1);
310  if (ival != -1) {
311  cfg->ncoeffs = ival;
312  }
313  else {
314  sinfo_msg_error(" ncoeffs in the .ini file was not found!\n");
315  (*status)++;
316  }
317 
318  ival = iniparser_getint(sym, "resampling:nrows", -1);
319  if (ival != -1) {
320  cfg->nrows = ival;
321  }
322  else {
323  sinfo_msg_error(" nrows in the .ini file was not found!\n");
324  (*status)++;
325  }
326 
327  return;
328 
329 }
330 
331 static void
332 parse_section_extractspectrum(dictionary * sym, lamp_config * cfg, int *status)
333 {
334  float dval;
335 
336  dval = iniparser_getdouble(sym, "extractspectrum:loreject", -1.);
337  if (dval != -1.) {
338  cfg->loReject = dval;
339  }
340  else {
341  sinfo_msg_error(" LoReject in the .ini file was not found!\n");
342  (*status)++;
343  }
344  dval = iniparser_getdouble(sym, "extractspectrum:hireject", -1.);
345  if (dval != -1.) {
346  cfg->hiReject = dval;
347  }
348  else {
349  sinfo_msg_error(" hiReject in the .ini file was not found!\n");
350  (*status)++;
351  }
352  dval = iniparser_getdouble(sym, "extractspectrum:countstointensity", -1.);
353  if (dval != -1.) {
354  cfg->countsToIntensity = dval;
355  }
356  else {
357  sinfo_msg_error("CountsToIntensity in the .ini file was not found!");
358  (*status)++;
359  }
360 
361  return;
362 }
363 
#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