NACO Pipeline Reference Manual  4.4.0
naco_util_spc_argon.c
1 /* $Id: naco_util_spc_argon.c,v 1.6 2011-12-22 11:21:03 llundin Exp $
2  *
3  * This file is part of the NACO 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., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: llundin $
23  * $Date: 2011-12-22 11:21:03 $
24  * $Revision: 1.6 $
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 <string.h>
37 
38 #include "naco_recipe.h"
39 
40 #include "naco_spc.h"
41 
42 /*-----------------------------------------------------------------------------
43  Recipe defines
44  -----------------------------------------------------------------------------*/
45 
46 #define RECIPE_STRING "naco_util_spc_argon"
47 
48 #ifndef NACO_SPC_ARGON_SIZE
49 #define NACO_SPC_ARGON_SIZE 80
50 #endif
51 
52 /*-----------------------------------------------------------------------------
53  Private Functions prototypes
54  -----------------------------------------------------------------------------*/
55 
56 NACO_RECIPE_DEFINE(naco_util_spc_argon, 0,
57  "Generate FITS table with argon lines",
58  "The input frame(s) must be tagged "
59  NACO_SPC_ARGON_ASCII ".\n"
60  "The file must comprise two columns:"
61  "the 1st with the line wavelength [Angstrom],\n"
62  "the 2nd with the line intensity.\n"
63  "\n"
64  "The default input ASCII-file is in the catalogs/ "
65  "directory of the NACO source-code distribution.");
66 
67 static IRPLIB_UTIL_SET_ROW(naco_util_spc_argon_set_row);
68 static IRPLIB_UTIL_CHECK(naco_util_spc_argon_check);
69 
70 static unsigned nzero = 0; /* Quick hack to count zero-intensity lines */
71 
72 /*----------------------------------------------------------------------------*/
76 /*----------------------------------------------------------------------------*/
77 
78 /*-----------------------------------------------------------------------------
79 
80  Functions code
81  -----------------------------------------------------------------------------*/
82 
83 /*----------------------------------------------------------------------------*/
90 /*----------------------------------------------------------------------------*/
91 static int naco_util_spc_argon(cpl_frameset * framelist,
92  const cpl_parameterlist * parlist)
93 {
94  irplib_framelist * allframes = NULL;
95  irplib_framelist * rawframes = NULL;
96  cpl_frameset * useframes = NULL;
97  cpl_table * self = NULL;
98 
99 
100  if (cpl_error_get_code()) return cpl_error_get_code();
101 
102  /* Identify the RAW frames in the input frameset */
103  skip_if (naco_dfs_set_groups(framelist));
104 
105  /* FIXME: Using framelists is the simplest way to extract the relevant
106  frames :-( */
107 
108  allframes = irplib_framelist_cast(framelist);
109  bug_if(allframes == NULL);
110 
111  rawframes = irplib_framelist_extract(allframes, NACO_SPC_ARGON_ASCII);
112  skip_if (rawframes == NULL);
113 
114  irplib_framelist_empty(allframes);
115 
116  useframes = irplib_frameset_cast(rawframes);
117  bug_if(allframes == NULL);
118 
119  /* At least one row per file */
120  self = cpl_table_new(irplib_framelist_get_size(rawframes));
121 
122  irplib_framelist_empty(rawframes);
123 
124  /* Create the table columns - with units */
125  bug_if (cpl_table_new_column(self, NACO_SPC_LAB_WAVE, CPL_TYPE_DOUBLE));
126  bug_if (cpl_table_new_column(self, NACO_SPC_LAB_INTENS, CPL_TYPE_DOUBLE));
127 
128  bug_if(cpl_table_set_column_unit(self, NACO_SPC_LAB_WAVE, "micron"));
129 
130  skip_if(irplib_dfs_table_convert(self, framelist, useframes,
131  NACO_SPC_ARGON_SIZE, '#', NULL,
132  NACO_SPC_ARGON, parlist, RECIPE_STRING,
133  NULL, NULL, NULL, "NACO", naco_pipe_id,
134  naco_util_spc_argon_set_row,
135  naco_util_spc_argon_check));
136  end_skip;
137 
138  cpl_table_delete(self);
139  cpl_frameset_delete(useframes);
140  irplib_framelist_delete(allframes);
141  irplib_framelist_delete(rawframes);
142 
143  return cpl_error_get_code();
144 }
145 
146 /*----------------------------------------------------------------------------*/
158 /*----------------------------------------------------------------------------*/
159 static
160 cpl_boolean naco_util_spc_argon_set_row(cpl_table * self,
161  const char * line,
162  int irow,
163  const cpl_frame * rawframe,
164  const cpl_parameterlist * parlist)
165 {
166 
167 
168  /* gcc can only check sscanf()s format when it is a string literal */
169 #define FORMAT "%lg %lg"
170 
171  int nvals;
172  double wlen, intens;
173 
174 
175  bug_if(0);
176  bug_if(self == NULL);
177  bug_if(line == NULL);
178  bug_if(irow < 0);
179  bug_if(rawframe == NULL);
180  bug_if(parlist == NULL);
181 
182  nvals = sscanf(line, FORMAT, &wlen, &intens);
183 
184  error_if (nvals != 2, CPL_ERROR_BAD_FILE_FORMAT,
185  "Line with length=%u has %d not 2 items formatted: %s",
186  (unsigned)strlen(line), nvals, FORMAT);
187 
188  error_if (wlen <= 0.0, CPL_ERROR_BAD_FILE_FORMAT,
189  "Non-positive wavelength %g in line %s", wlen, line);
190 
191  error_if (intens < 0.0, CPL_ERROR_BAD_FILE_FORMAT,
192  "Negative intensity %g in line %s", intens, line);
193 
194  wlen *= 1e-4; /* Convert from Angstrom to Micron */
195 
196  bug_if(cpl_table_set_double(self, NACO_SPC_LAB_WAVE, irow, wlen));
197  bug_if(cpl_table_set_double(self, NACO_SPC_LAB_INTENS, irow, intens));
198 
199  if (intens <= 0.0) nzero++; /* Don't use == to avoid warning ... */
200 
201  end_skip;
202 
203  return CPL_TRUE;
204 
205 }
206 
207 
208 /*----------------------------------------------------------------------------*/
217 /*----------------------------------------------------------------------------*/
218 static
219 cpl_error_code naco_util_spc_argon_check(cpl_table * self,
220  const cpl_frameset * useframes,
221  const cpl_parameterlist * parlist)
222 {
223 
224  cpl_propertylist * reflist = cpl_propertylist_new();
225 
226 
227  bug_if(0);
228  bug_if(self == NULL);
229  bug_if(parlist == NULL);
230 
231  /* Sort after wavelength with smallest first */
232  bug_if(cpl_propertylist_append_bool(reflist, NACO_SPC_LAB_WAVE, 0));
233 
234  bug_if(cpl_table_sort(self, reflist));
235 
236  cpl_msg_info(cpl_func, "Created table of %d argon lines (%u with zero "
237  "intensity) from %d file(s)", (int)cpl_table_get_nrow(self),
238  nzero, (int)cpl_frameset_get_size(useframes));
239 
240  end_skip;
241 
242  cpl_propertylist_delete(reflist);
243 
244  return cpl_error_get_code();
245 }
246 
cpl_frameset * irplib_frameset_cast(const irplib_framelist *self)
Create a CPL frameset from an irplib_framelist.
int naco_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: naco_dfs.c:62
cpl_error_code irplib_dfs_table_convert(cpl_table *self, cpl_frameset *allframes, const cpl_frameset *useframes, int maxlinelen, char commentchar, const char *product_name, const char *procatg, const cpl_parameterlist *parlist, const char *recipe_name, const cpl_propertylist *mainlist, const cpl_propertylist *extlist, const char *remregexp, const char *instrume, const char *pipe_id, cpl_boolean(*table_set_row)(cpl_table *, const char *, int, const cpl_frame *, const cpl_parameterlist *), cpl_error_code(*table_check)(cpl_table *, const cpl_frameset *, const cpl_parameterlist *))
Create a DFS product with one table from one or more (ASCII) file(s)
Definition: irplib_utils.c:860
void irplib_framelist_empty(irplib_framelist *self)
Erase all frames from a framelist.
irplib_framelist * irplib_framelist_extract(const irplib_framelist *self, const char *tag)
Extract the frames with the given tag from a framelist.
void irplib_framelist_delete(irplib_framelist *self)
Deallocate an irplib_framelist with its frames and properties.
irplib_framelist * irplib_framelist_cast(const cpl_frameset *frameset)
Create an irplib_framelist from a cpl_framelist.
int irplib_framelist_get_size(const irplib_framelist *self)
Get the size of a framelist.