VISIR Pipeline Reference Manual  4.1.0
visir_util_img_std_cat.c
1 /* $Id: visir_util_img_std_cat.c,v 1.58 2012-02-06 10:15:47 llundin Exp $
2  *
3  * This file is part of the VISIR 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: 2012-02-06 10:15:47 $
24  * $Revision: 1.58 $
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 <stdio.h>
37 #include <string.h>
38 
39 #include "visir_recipe.h"
40 
41 /*-----------------------------------------------------------------------------
42  Defines
43  -----------------------------------------------------------------------------*/
44 
45 #define RECIPE_STRING "visir_util_img_std_cat"
46 
47 /*-----------------------------------------------------------------------------
48  Private Functions prototypes
49  -----------------------------------------------------------------------------*/
50 
51 static cpl_error_code visir_util_img_std_cat_save(cpl_frameset *,
52  const cpl_parameterlist *,
53  const cpl_table *);
54 
55 static cpl_error_code visir_util_img_std_cat_parse(const char*, char *,
56  double *, double *, char *,
57  double *, int);
58 
59 VISIR_RECIPE_DEFINE(visir_util_img_std_cat, 0,
60  "Convert ASCII-file(s) to a FITS standard star catalog",
61  "This recipe generates a FITS standard star catalog for "
62  "imaging from one or \n"
63  "more ASCII-files.\n"
64  "Each line in the text file must have 45 fields separated "
65  "by white-space.\n"
66  "The first field is the star name, e.g. 'HD108903' which "
67  "will be stored in a \n"
68  "table column labeled 'STARS'.\n"
69  "The 3 next fields are the RA (hh mm ss) which will be "
70  "stored in degrees in \n"
71  "a table column labeled 'RA' - all three are non-negative "
72  "and hh and mm are \n"
73  "integer.\n"
74  "The 3 next fields are the DEC (dd mm ss) which will be "
75  "stored in degrees in \n"
76  "a table column labeled 'DEC' - all three are non-negative, "
77  "dd and mm are \n"
78  "integer, while dd has either a '+' or a '-' prepended "
79  "(including -00).\n"
80  "The next field is the spectral type which will be "
81  "stored in a \n"
82  "table column labeled 'SP_TYPE'.\n"
83  "The 31 next fields are the JY values for the 31 supported "
84  "image filters.\n"
85  "The 6 next fields are the JY values for the 6 supported "
86  "spectral filters.\n"
87  "All JY values must be positive.\n"
88  "For each filter the JY value is stored in a table column "
89  "labeled with the \n"
90  "filter name.\n"
91  "The 37 filter names are hard-coded in the recipe.\n"
92  "\n"
93  "Lines beginning with a hash (#) are treated as comments.\n"
94  "\n"
95  "The ASCII-input should be tagged " VISIR_IMG_LINES_ASCII
96  ", but all input \n"
97  "files will currently be processed regardless.\n");
98 
99 /*----------------------------------------------------------------------------*/
103 /*----------------------------------------------------------------------------*/
104 
105 /*-----------------------------------------------------------------------------
106  Functions code
107  -----------------------------------------------------------------------------*/
108 
109 /*----------------------------------------------------------------------------*/
117 /*----------------------------------------------------------------------------*/
118 static int visir_util_img_std_cat(cpl_frameset * framelist,
119  const cpl_parameterlist * parlist)
120 {
121  FILE * in = NULL;
122 
123  char line[1024];
124  cpl_table * tab = NULL;
125  char sname[512];
126  char stype[512];
127  const char * filters[]
128  = {"MV", "N_BAND", "SIC", "PAH1_1", "PAH1", "ARIII", "SIV_1", "SIV",
129  "PAH2_1", "SIV_2", "PAH2", "PAH2_2", "NEII_1", "NEII", "NEII_2",
130  "J7_9", "J8_9", "J9_8", "J12_1", "J12_2", "B8_7", "B9_7", "B10_7",
131  "B11_7", "B12_4", "Q0", "QH2", "Q1", "Q2", "Q3", "Q4", "Q7", "Q8",
132  "AGPM_12_4", "4QPM_10_5", "4QPM_11_3", "N_SW_spec",
133  "H2S4_spec", "ARIII_spec", "NEII_2_spec", "H2S3_spec", "H2S1_spec",
134  };
135  const int nfilters = sizeof(filters) / sizeof(filters[0]);
136  double jys[nfilters];
137  const double max_radius = VISIR_STAR_MAX_RADIUS;
138  double mindist;
139  int iloc1, iloc2;
140  int nrows = 425; /* Some positive number */
141  int irow = 0;
142  int i, j;
143 
144 
145  if (cpl_error_get_code()) return cpl_error_get_code();
146 
147  /* Identify the RAW frames in the input frameset */
148  skip_if (visir_dfs_set_groups(framelist));
149 
150  /* Create the table */
151  tab = cpl_table_new(nrows);
152  skip_if (cpl_table_new_column(tab, "STARS", CPL_TYPE_STRING));
153  skip_if (cpl_table_new_column(tab, "SP_TYPE", CPL_TYPE_STRING));
154  skip_if (cpl_table_new_column(tab, "RA", CPL_TYPE_DOUBLE));
155  skip_if (cpl_table_new_column(tab, "DEC", CPL_TYPE_DOUBLE));
156 
157  for (j=0 ; j<nfilters ; j++)
158  skip_if (cpl_table_new_column(tab, filters[j], CPL_TYPE_DOUBLE));
159 
160  /* Loop on all input frames */
161  for (i=0; i < cpl_frameset_get_size(framelist); i++) {
162  const cpl_frame * frame = cpl_frameset_get_position_const(framelist, i);
163 
164  /* Get file name */
165  const char * filename = cpl_frame_get_filename(frame);
166  int jrow = 0;
167 
168 
169  /* Open the file */
170  skip_if (filename == NULL);
171  in = fopen(filename, "r");
172  if (in == NULL) {
173  cpl_msg_error(cpl_func, "Could not open the %d. file: %s", i+1,
174  filename);
175  skip_if (1);
176  }
177 
178  while (fgets(line, 1024, in) != NULL) {
179  jrow++;
180  if (line[0] != '#') {
181  double ra, dec;
182 
183  if (visir_util_img_std_cat_parse(line, sname, &ra, &dec,
184  stype, jys, nfilters)) {
185  cpl_msg_error(cpl_func, "Unparsable line %d in %d. file "
186  "%s", jrow, i+1, filename);
187  skip_if(1);
188  }
189  if (irow == nrows) {
190  /* Table needs more rows */
191  nrows *= 2;
192  skip_if (cpl_table_set_size(tab, nrows));
193  }
194  skip_if (cpl_table_set_string(tab, "STARS", irow, sname));
195  skip_if (cpl_table_set_string(tab, "SP_TYPE", irow, stype));
196  skip_if (cpl_table_set_double(tab, "RA", irow, ra));
197  skip_if (cpl_table_set_double(tab, "DEC", irow, dec));
198  for (j=0 ; j<nfilters ; j++)
199  skip_if(cpl_table_set_double(tab, filters[j], irow,
200  jys[j]));
201  irow++;
202 
203  }
204  }
205  fclose(in);
206  in = NULL;
207  if (jrow == 0) {
208  cpl_msg_warning(cpl_func, "No usable lines in file %s", filename);
209  }
210  }
211 
212  skip_if (irow == 0);
213 
214  /* Resize the table to the actual number of rows read */
215  nrows = irow;
216  skip_if (cpl_table_set_size(tab, nrows));
217 
218  mindist = visir_star_dist_min(cpl_table_get_data_double(tab, "RA"),
219  cpl_table_get_data_double(tab, "DEC"), nrows,
220  &iloc1, &iloc2);
221 
222  if (mindist < max_radius)
223  cpl_msg_warning(cpl_func, "The pair of closest stars is %s (%d) and %s "
224  "(%d) with the distance: %g",
225  cpl_table_get_string(tab, "STARS", iloc1), iloc1,
226  cpl_table_get_string(tab, "STARS", iloc2), iloc2,
227  mindist);
228  else
229  cpl_msg_info(cpl_func, "The pair of closest stars is %s (%d) and %s "
230  "(%d) with the distance: %g",
231  cpl_table_get_string(tab, "STARS", iloc1), iloc1,
232  cpl_table_get_string(tab, "STARS", iloc2), iloc2,
233  mindist);
234 
235  /* Save the table */
236  cpl_msg_info(cpl_func, "Saving the table with %d rows and %d filters",
237  nrows, nfilters);
238 
239  skip_if (visir_util_img_std_cat_save(framelist, parlist, tab));
240 
241  end_skip;
242 
243  if (in) fclose(in);
244  cpl_table_delete(tab);
245 
246  return cpl_error_get_code();
247 }
248 
249 /*----------------------------------------------------------------------------*/
262 /*----------------------------------------------------------------------------*/
263 static cpl_error_code visir_util_img_std_cat_parse(const char * line,
264  char * sname, double * pra,
265  double * pdec, char *stype,
266  double * jys, int njys)
267 {
268 
269  const char * format = "%s %d %d %lg %c%d %d %lg %s %lg %lg %lg %lg %lg %lg "
270  "%lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg "
271  "%lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg";
272 
273  int nvals;
274  int ra1, ra2;
275  int dec1, dec2;
276  double ra3, dec3;
277  char isign;
278 
279  assert( line );
280  assert( sname );
281  assert( stype );
282  assert( jys );
283 
284 
285  nvals = sscanf(line, format,
286  sname, &ra1, &ra2, &ra3, &isign, &dec1, &dec2,
287  &dec3, stype, &(jys[0]), &(jys[1]), &(jys[2]),
288  &(jys[3]), &(jys[4]), &(jys[5]), &(jys[6]),
289  &(jys[7]), &(jys[8]), &(jys[9]), &(jys[10]),
290  &(jys[11]), &(jys[12]), &(jys[13]), &(jys[14]),
291  &(jys[15]), &(jys[16]), &(jys[17]), &(jys[18]),
292  &(jys[19]), &(jys[20]), &(jys[21]), &(jys[22]),
293  &(jys[23]), &(jys[24]), &(jys[25]), &(jys[26]),
294  &(jys[27]), &(jys[28]), &(jys[29]), &(jys[30]),
295  &(jys[31]), &(jys[32]), &(jys[33]), &(jys[34]),
296  &(jys[35]), &(jys[36]), &(jys[37]), &(jys[38]),
297  &(jys[39]), &(jys[40]), &(jys[41]));
298 
299  if (nvals != njys+9) {
300  cpl_msg_error(cpl_func, "Line with length=%u has %d not %d items "
301  "formatted: %s", (unsigned)strlen(line), nvals, njys+9,
302  format);
303  cpl_ensure_code(0, CPL_ERROR_BAD_FILE_FORMAT);
304  }
305 
306  return visir_star_convert(line, ra1, ra2, ra3, isign, dec1, dec2,
307  dec3, jys, njys, pra, pdec);
308 
309 }
310 
311 /*----------------------------------------------------------------------------*/
319 /*----------------------------------------------------------------------------*/
320 static
321 cpl_error_code visir_util_img_std_cat_save(cpl_frameset * set,
322  const cpl_parameterlist * parlist,
323  const cpl_table * tab)
324 {
325  cpl_propertylist * applist = cpl_propertylist_new();
326 
327 
328  bug_if(cpl_propertylist_append_string(applist, "INSTRUME", "VISIR"));
329 
330  skip_if(irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
331  VISIR_IMA_STD_CAT_PROCATG,
332  applist, NULL, visir_pipe_id,
333  RECIPE_STRING CPL_DFS_FITS));
334 
335  end_skip;
336 
337  cpl_propertylist_delete(applist);
338 
339  return cpl_error_get_code();
340 }
341 
cpl_error_code irplib_dfs_save_table(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_table *table, const cpl_propertylist *tablelist, const char *recipe, const char *procat, const cpl_propertylist *applist, const char *remregexp, const char *pipe_id, const char *filename)
Save a table as a DFS-compliant pipeline product.
Definition: irplib_utils.c:335
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: visir_dfs.c:72