HAWKI Pipeline Reference Manual  1.8.12
hawki_util_stdstars.c
1 /* $Id: hawki_util_stdstars.c,v 1.16 2011/07/06 08:21:36 cgarcia Exp $
2  *
3  * This file is part of the HAWKI 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: cgarcia $
23  * $Date: 2011/07/06 08:21:36 $
24  * $Revision: 1.16 $
25  * $Name: hawki-1_8_12 $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <math.h>
37 #include <cpl.h>
38 
39 #include "irplib_utils.h"
40 #include "irplib_stdstar.h"
41 
42 #include "hawki_utils.h"
43 #include "hawki_load.h"
44 #include "hawki_pfits.h"
45 #include "hawki_dfs.h"
46 
47 /*-----------------------------------------------------------------------------
48  Functions prototypes
49  -----------------------------------------------------------------------------*/
50 
51 static int hawki_util_stdstars_create(cpl_plugin *) ;
52 static int hawki_util_stdstars_exec(cpl_plugin *) ;
53 static int hawki_util_stdstars_destroy(cpl_plugin *) ;
54 static int hawki_util_stdstars(cpl_frameset *) ;
55 static cpl_table * hawki_util_stdstars_convert(const char *) ;
56 
57 /*-----------------------------------------------------------------------------
58  Static variables
59  -----------------------------------------------------------------------------*/
60 
61 static char hawki_util_stdstars_description[] =
62 "hawki_util_stdstars -- HAWK-I standard stars catalog creation.\n"
63 "The files listed in the Set Of Frames (sof-file) must be tagged:\n"
64 "raw-file.txt "HAWKI_UTIL_STDSTARS_RAW"\n" ;
65 
66 /*-----------------------------------------------------------------------------
67  Functions code
68  -----------------------------------------------------------------------------*/
69 
70 /*----------------------------------------------------------------------------*/
78 /*----------------------------------------------------------------------------*/
79 int cpl_plugin_get_info(cpl_pluginlist * list)
80 {
81  cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
82  cpl_plugin * plugin = &recipe->interface ;
83 
84  cpl_plugin_init(plugin,
85  CPL_PLUGIN_API,
86  HAWKI_BINARY_VERSION,
87  CPL_PLUGIN_TYPE_RECIPE,
88  "hawki_util_stdstars",
89  "Standard stars catalog creation",
90  hawki_util_stdstars_description,
91  "Cesar Enrique Garcia Dabo",
92  PACKAGE_BUGREPORT,
94  hawki_util_stdstars_create,
95  hawki_util_stdstars_exec,
96  hawki_util_stdstars_destroy) ;
97 
98  cpl_pluginlist_append(list, plugin) ;
99 
100  return 0;
101 }
102 
103 /*----------------------------------------------------------------------------*/
112 /*----------------------------------------------------------------------------*/
113 static int hawki_util_stdstars_create(cpl_plugin * plugin)
114 {
115  cpl_recipe * recipe ;
116 
117  /* Get the recipe out of the plugin */
118  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
119  recipe = (cpl_recipe *)plugin ;
120  else return -1 ;
121 
122  /* Create the parameters list in the cpl_recipe object */
123  recipe->parameters = cpl_parameterlist_new() ;
124 
125  /* Return */
126  return 0;
127 }
128 
129 /*----------------------------------------------------------------------------*/
135 /*----------------------------------------------------------------------------*/
136 static int hawki_util_stdstars_exec(cpl_plugin * plugin)
137 {
138  cpl_recipe * recipe ;
139 
140  /* Get the recipe out of the plugin */
141  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
142  recipe = (cpl_recipe *)plugin ;
143  else return -1 ;
144 
145  /* Issue a banner */
147 
148  return hawki_util_stdstars(recipe->frames) ;
149 }
150 
151 /*----------------------------------------------------------------------------*/
157 /*----------------------------------------------------------------------------*/
158 static int hawki_util_stdstars_destroy(cpl_plugin * plugin)
159 {
160  cpl_recipe * recipe ;
161 
162  /* Get the recipe out of the plugin */
163  if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
164  recipe = (cpl_recipe *)plugin ;
165  else return -1 ;
166 
167  cpl_parameterlist_delete(recipe->parameters) ;
168  return 0 ;
169 }
170 
171 /*----------------------------------------------------------------------------*/
177 /*----------------------------------------------------------------------------*/
178 static int hawki_util_stdstars(
179  cpl_frameset * framelist)
180 {
181  cpl_frameset * rawframes ;
182  const char * recipe_name = "hawki_util_stdstars" ;
183 
184 
185  /* Identify the RAW and CALIB frames in the input frameset */
186  if (hawki_dfs_set_groups(framelist)) {
187  cpl_msg_error(__func__, "Cannot identify RAW and CALIB frames") ;
188  return -1 ;
189  }
190 
191  /* Retrieve raw frames */
192  if ((rawframes = hawki_extract_frameset(framelist,
193  HAWKI_UTIL_STDSTARS_RAW)) == NULL) {
194  cpl_msg_error(__func__, "Cannot find raw frames in the input list") ;
195  return -1 ;
196  }
197 
198  /* Write the catalog */
199  if (irplib_stdstar_write_catalogs(framelist,
200  rawframes,
201  recipe_name,
202  HAWKI_CALPRO_STDSTARS,
203  HAWKI_PROTYPE_STDSTARS,
204  PACKAGE "/" PACKAGE_VERSION,
205  "HAWKI",
206  hawki_util_stdstars_convert) == -1) {
207  cpl_msg_error(__func__, "Cannot write the catalogs") ;
208  cpl_frameset_delete(rawframes) ;
209  return -1 ;
210  }
211  cpl_frameset_delete(rawframes) ;
212 
213  /* Return */
214  if (cpl_error_get_code())
215  {
216  cpl_msg_error(__func__,
217  "HAWK-I pipeline could not recover from previous errors");
218  return -1 ;
219  }
220  else return 0 ;
221 }
222 
223 /*----------------------------------------------------------------------------*/
244 /*----------------------------------------------------------------------------*/
245 static cpl_table * hawki_util_stdstars_convert(const char * filename)
246 {
247  cpl_table * out ;
248  int nfilters ;
249  const char * filters[4];
250  double mags[4] ;
251  int nbentries ;
252  FILE * in ;
253  char line[1024];
254  double ra, dec ;
255  char sname[512];
256  char stype[512];
257  int i ;
258 
259  /* Check entries */
260  if (filename == NULL) return NULL ;
261 
262  /* Initialise */
263  nfilters = 4 ;
264  filters[0] = hawki_std_band_name(HAWKI_BAND_J) ;
265  filters[1] = hawki_std_band_name(HAWKI_BAND_H) ;
266  filters[2] = hawki_std_band_name(HAWKI_BAND_K) ;
267  filters[3] = hawki_std_band_name(HAWKI_BAND_Y) ;
268 
269  /* Get the number of lines */
270  nbentries = 0 ;
271  if ((in = fopen(filename, "r")) == NULL) {
272  return NULL ;
273  }
274  while (fgets(line, 1024, in) != NULL) {
275  if (line[0] != '#') nbentries ++ ;
276  }
277  fclose(in) ;
278 
279  /* Create the table */
280  out = cpl_table_new(nbentries);
281  cpl_table_new_column(out, IRPLIB_STDSTAR_STAR_COL, CPL_TYPE_STRING);
282  cpl_table_new_column(out, IRPLIB_STDSTAR_TYPE_COL, CPL_TYPE_STRING);
283  cpl_table_new_column(out, IRPLIB_STDSTAR_RA_COL, CPL_TYPE_DOUBLE);
284  cpl_table_new_column(out, IRPLIB_STDSTAR_DEC_COL, CPL_TYPE_DOUBLE);
285  for (i=0 ; i<nfilters ; i++)
286  cpl_table_new_column(out, filters[i], CPL_TYPE_DOUBLE);
287 
288  /* Parse the file */
289  if ((in = fopen(filename, "r")) == NULL) {
290  cpl_table_delete(out) ;
291  return NULL ;
292  }
293  nbentries = 0 ;
294  while (fgets(line, 1024, in) != NULL) {
295  if (line[0] != '#') {
296  if (sscanf(line, "%s %lg %lg %s %lg %lg %lg %lg",
297  sname, &ra, &dec, stype, &(mags[0]), &(mags[1]),
298  &(mags[2]), &(mags[3])) != 8) {
299  cpl_table_delete(out) ;
300  return NULL ;
301  }
302  cpl_table_set_string(out, IRPLIB_STDSTAR_STAR_COL,nbentries, sname);
303  cpl_table_set_string(out, IRPLIB_STDSTAR_TYPE_COL,nbentries, stype);
304  cpl_table_set_double(out, IRPLIB_STDSTAR_RA_COL, nbentries, ra);
305  cpl_table_set_double(out, IRPLIB_STDSTAR_DEC_COL, nbentries, dec);
306  for (i=0 ; i<nfilters ; i++)
307  cpl_table_set_double(out, filters[i], nbentries, mags[i]);
308  nbentries ++ ;
309  }
310  }
311  fclose(in) ;
312 
313  return out ;
314 }
315