CRIRES Pipeline Reference Manual  2.3.15
crires_util_genconfig.c
1 /* $Id: crires_util_genconfig.c,v 1.19 2011-03-22 09:17:12 yjung Exp $
2  *
3  * This file is part of the CRIRES 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: yjung $
23  * $Date: 2011-03-22 09:17:12 $
24  * $Revision: 1.19 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <locale.h>
37 #include "crires_recipe.h"
38 
39 /*-----------------------------------------------------------------------------
40  Define
41  -----------------------------------------------------------------------------*/
42 
43 #define RECIPE_STRING "crires_util_genconfig"
44 
45 /*-----------------------------------------------------------------------------
46  Functions prototypes
47  -----------------------------------------------------------------------------*/
48 
49 static int crires_util_genconfig_save(cpl_table *, const cpl_parameterlist *,
50  cpl_frameset *);
51 
52 static char crires_util_genconfig_description[] =
53 "This recipe is used to generate the model configuration file.\n"
54 "The sof file contains the names of the input ASCII file\n"
55 "tagged with "CRIRES_UTIL_GENCONFIG_RAW".\n"
56 "The ASCII file must contain five columns:\n"
57 "1st: The best guess value\n"
58 "2nd: The low limit\n"
59 "3th: The high limit\n"
60 "4th: The Flag to recompute or not\n"
61 "5th: Name of the Parameter\n"
62 "The ASCII files are in the catalogs/ directory of the CRIRES distribution.\n"
63 "This recipe produces 1 file:\n"
64 "First product: the table with the configuration for the model.\n"
65 " (PRO TYPE = "CRIRES_PROTYPE_MOD_CONF")\n" ;
66 
67 CRIRES_RECIPE_DEFINE(crires_util_genconfig,
68  CRIRES_PARAM_CONFIG_MODE,
69  "Generate spectrum calibration FITS tables",
70  crires_util_genconfig_description) ;
71 
72 /*-----------------------------------------------------------------------------
73  Static variables
74  -----------------------------------------------------------------------------*/
75 
76 static struct {
77  /* Input */
78  int mode ;
79 } crires_util_genconfig_config ;
80 
81 /*-----------------------------------------------------------------------------
82  Functions code
83  -----------------------------------------------------------------------------*/
84 
85 /*----------------------------------------------------------------------------*/
92 /*----------------------------------------------------------------------------*/
93 static int crires_util_genconfig(
94  cpl_frameset * framelist,
95  const cpl_parameterlist * parlist)
96 {
97  FILE * in ;
98  char line[1024];
99  cpl_frame * cur_frame ;
100  const char * cur_fname ;
101  int nentries ;
102  char name[1024] ;
103  double best, low, high ;
104  int flag ;
105  cpl_table * tab ;
106  int i ;
107 
108  /* Needed for sscanf() */
109  setlocale(LC_NUMERIC, "C");
110 
111  /* Retrieve input parameters */
112  crires_util_genconfig_config.mode = crires_parameterlist_get_int(parlist,
113  RECIPE_STRING, CRIRES_PARAM_CONFIG_MODE) ;
114 
115  /* Identify the RAW and CALIB frames in the input frameset */
116  if (crires_dfs_set_groups(framelist, NULL)) {
117  cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
118  return -1 ;
119  }
120 
121  /* Get the config file name */
122  cur_frame = cpl_frameset_get_position(framelist, 0) ;
123  cur_fname = cpl_frame_get_filename(cur_frame) ;
124 
125  /* Open the file */
126  if ((in = fopen(cur_fname, "r")) == NULL) {
127  cpl_msg_error(__func__, "Could not open %s", cur_fname) ;
128  return -1 ;
129  }
130 
131  /* Count number of entries */
132  nentries = 0 ;
133  while (fgets(line, 1024, in) != NULL) {
134  if (line[0] != '#' && sscanf(line, "%lg %lg %lg %d %s",
135  &best, &low, &high, &flag, name) == 5) nentries++ ;
136  }
137  if (nentries == 0) {
138  cpl_msg_error(__func__, "No valid entryin the file") ;
139  fclose(in) ;
140  return -1 ;
141  }
142 
143  /* Create the output table */
144  tab = cpl_table_new(nentries) ;
145  cpl_table_new_column(tab, CRIRES_COL_MODEL_CONF_BEST, CPL_TYPE_DOUBLE) ;
146  cpl_table_new_column(tab, CRIRES_COL_MODEL_CONF_LOW, CPL_TYPE_DOUBLE) ;
147  cpl_table_new_column(tab, CRIRES_COL_MODEL_CONF_HIGH, CPL_TYPE_DOUBLE) ;
148  cpl_table_new_column(tab, CRIRES_COL_MODEL_CONF_FLAG, CPL_TYPE_INT) ;
149  cpl_table_new_column(tab, CRIRES_COL_MODEL_CONF_NAME, CPL_TYPE_STRING) ;
150 
151  /* Fill the table */
152  i = 0 ;
153  rewind(in) ;
154  while (fgets(line, 1024, in) != NULL) {
155  if (line[0] != '#' && sscanf(line, "%lg %lg %lg %d %s",
156  &best, &low, &high, &flag, name) == 5) {
157  cpl_table_set_string(tab, CRIRES_COL_MODEL_CONF_NAME, i, name) ;
158  cpl_table_set_double(tab, CRIRES_COL_MODEL_CONF_BEST, i, best) ;
159  cpl_table_set_double(tab, CRIRES_COL_MODEL_CONF_LOW, i, low) ;
160  cpl_table_set_double(tab, CRIRES_COL_MODEL_CONF_HIGH, i, high) ;
161  cpl_table_set_int(tab, CRIRES_COL_MODEL_CONF_FLAG, i, flag) ;
162  i++ ;
163  }
164  }
165  fclose(in) ;
166 
167  /* Save the table */
168  cpl_msg_info(__func__, "Saving the table with %d rows", nentries) ;
169  if (crires_util_genconfig_save(tab, parlist, framelist) == -1) {
170  cpl_msg_error(__func__, "Cannot write the table") ;
171  cpl_table_delete(tab) ;
172  return -1 ;
173  }
174  cpl_table_delete(tab) ;
175  return 0 ;
176 }
177 
178 /*----------------------------------------------------------------------------*/
186 /*----------------------------------------------------------------------------*/
187 static int crires_util_genconfig_save(
188  cpl_table * out_table,
189  const cpl_parameterlist * parlist,
190  cpl_frameset * set)
191 {
192  cpl_propertylist * plist ;
193  const char * procat ;
194 
195  if (crires_util_genconfig_config.mode == 1)
196  procat = CRIRES_CALPRO_MODEL_REFINE_CONF ;
197  else if (crires_util_genconfig_config.mode == 2)
198  procat = CRIRES_CALPRO_MODEL_CONFIG ;
199  else {
200  cpl_msg_error(__func__, "Unsupported mode") ;
201  return -1 ;
202  }
203 
204  plist = cpl_propertylist_new();
205  cpl_propertylist_append_string(plist, "INSTRUME", "CRIRES") ;
206  cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG, procat) ;
207  cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
208  CRIRES_PROTYPE_MOD_CONF) ;
209  cpl_propertylist_append_string(plist, "ESO PRO CFG-IDENT",
210  "AlwaysOK") ;
211 
212  if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
213  NULL, "crires_util_genconfig", plist, NULL,
214  PACKAGE "/" PACKAGE_VERSION,
215  "crires_util_genconfig.fits") != CPL_ERROR_NONE) {
216  cpl_msg_error(__func__, "Cannot save the table") ;
217  return -1 ;
218  }
219  cpl_propertylist_delete(plist) ;
220 
221  /* Return */
222  return 0 ;
223 }