IIINSTRUMENT Pipeline Reference Manual 1.5.16
sofi_util_stdstars.c
1/* $Id: sofi_util_stdstars.c,v 1.10 2013-03-12 08:04:52 llundin Exp $
2 *
3 * This file is part of the SOFI 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: 2013-03-12 08:04:52 $
24 * $Revision: 1.10 $
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 <math.h>
37#include <cpl.h>
38
39#include "irplib_utils.h"
40#include "irplib_stdstar.h"
41
42#include "sofi_utils.h"
43#include "sofi_pfits.h"
44#include "sofi_dfs.h"
45
46/*-----------------------------------------------------------------------------
47 Functions prototypes
48 -----------------------------------------------------------------------------*/
49
50static int sofi_util_stdstars_create(cpl_plugin *) ;
51static int sofi_util_stdstars_exec(cpl_plugin *) ;
52static int sofi_util_stdstars_destroy(cpl_plugin *) ;
53static int sofi_util_stdstars(cpl_frameset *) ;
54static cpl_table * sofi_util_stdstars_convert(const char *) ;
55
56/*-----------------------------------------------------------------------------
57 Static variables
58 -----------------------------------------------------------------------------*/
59
60static char sofi_util_stdstars_description[] =
61"sofi_util_stdstars -- SOFI standard stars catalog creation.\n"
62"The files listed in the Set Of Frames (sof-file) must be tagged:\n"
63"raw-file.fits "SOFI_UTIL_STDSTARS_RAW"\n" ;
64
65/*-----------------------------------------------------------------------------
66 Functions code
67 -----------------------------------------------------------------------------*/
68
69/*----------------------------------------------------------------------------*/
77/*----------------------------------------------------------------------------*/
78int cpl_plugin_get_info(cpl_pluginlist * list)
79{
80 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
81 cpl_plugin * plugin = &recipe->interface ;
82
83 cpl_plugin_init(plugin,
84 CPL_PLUGIN_API,
85 SOFI_BINARY_VERSION,
86 CPL_PLUGIN_TYPE_RECIPE,
87 "sofi_util_stdstars",
88 "Standard stars catalog creation",
89 sofi_util_stdstars_description,
90 "Yves Jung",
91 "yjung@eso.org",
93 sofi_util_stdstars_create,
94 sofi_util_stdstars_exec,
95 sofi_util_stdstars_destroy) ;
96
97 cpl_pluginlist_append(list, plugin) ;
98
99 return 0;
100}
101
102/*----------------------------------------------------------------------------*/
111/*----------------------------------------------------------------------------*/
112static int sofi_util_stdstars_create(cpl_plugin * plugin)
113{
114 cpl_recipe * recipe ;
115
116 /* Get the recipe out of the plugin */
117 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
118 recipe = (cpl_recipe *)plugin ;
119 else return CPL_ERROR_UNSPECIFIED;
120
121 /* Create the parameters list in the cpl_recipe object */
122 recipe->parameters = cpl_parameterlist_new() ;
123
124 /* Return */
125 return 0;
126}
127
128/*----------------------------------------------------------------------------*/
134/*----------------------------------------------------------------------------*/
135static int sofi_util_stdstars_exec(cpl_plugin * plugin)
136{
137 cpl_recipe * recipe ;
138
139 /* Get the recipe out of the plugin */
140 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
141 recipe = (cpl_recipe *)plugin ;
142 else return CPL_ERROR_UNSPECIFIED;
143
144 return sofi_util_stdstars(recipe->frames) ;
145}
146
147/*----------------------------------------------------------------------------*/
153/*----------------------------------------------------------------------------*/
154static int sofi_util_stdstars_destroy(cpl_plugin * plugin)
155{
156 cpl_recipe * recipe ;
157
158 /* Get the recipe out of the plugin */
159 if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE)
160 recipe = (cpl_recipe *)plugin ;
161 else return CPL_ERROR_UNSPECIFIED;
162
163 cpl_parameterlist_delete(recipe->parameters) ;
164 return 0 ;
165}
166
167/*----------------------------------------------------------------------------*/
173/*----------------------------------------------------------------------------*/
174static int sofi_util_stdstars(
175 cpl_frameset * framelist)
176{
177 cpl_frameset * rawframes ;
178
179 /* Identify the RAW and CALIB frames in the input frameset */
180 if (sofi_dfs_set_groups(framelist)) {
181 cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames") ;
182 return CPL_ERROR_UNSPECIFIED;
183 }
184
185 /* Retrieve raw frames */
186 if ((rawframes = sofi_extract_frameset(framelist,
187 SOFI_UTIL_STDSTARS_RAW)) == NULL) {
188 cpl_msg_error(cpl_func, "Cannot find raw frames in the input list") ;
189 return CPL_ERROR_UNSPECIFIED;
190 }
191
192 /* Write the catalog */
193 if (irplib_stdstar_write_catalogs(framelist,
194 rawframes,
195 "sofi_util_stdstars",
196 SOFI_UTIL_STDSTARS_RES,
197 NULL,
198 PACKAGE "/" PACKAGE_VERSION,
199 "SOFI",
200 sofi_util_stdstars_convert)) {
201 cpl_msg_error(cpl_func, "Cannot write the catalogs") ;
202 cpl_frameset_delete(rawframes) ;
203 return CPL_ERROR_UNSPECIFIED;
204 }
205 cpl_frameset_delete(rawframes) ;
206 return 0 ;
207}
208
209/*----------------------------------------------------------------------------*/
230/*----------------------------------------------------------------------------*/
231static cpl_table * sofi_util_stdstars_convert(const char * filename)
232{
233 cpl_table * out ;
234 int nfilters ;
235 const char * filters[8];
236 double mags[8] ;
237 int nbentries ;
238 FILE * in ;
239 char line[1024];
240 double ra, dec ;
241 char sname[512];
242 char stype[512];
243 int i ;
244
245 /* Check entries */
246 if (filename == NULL) {
247 (void)cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
248 return NULL;
249 }
250
251 /* Initialise */
252 nfilters = 8 ;
253 filters[0] = "J" ;
254 filters[1] = "H" ;
255 filters[2] = "K" ;
256 filters[3] = "Ks" ;
257 filters[4] = "L" ;
258 filters[5] = "M" ;
259 filters[6] = "Lp" ;
260 filters[7] = "Mp" ;
261
262 /* Get the number of lines */
263 nbentries = 0 ;
264 if ((in = fopen(filename, "r")) == NULL) {
265 (void)cpl_error_set(cpl_func, CPL_ERROR_FILE_IO);
266 return NULL ;
267 }
268 while (fgets(line, 1024, in) != NULL) {
269 if (line[0] != '#') nbentries ++ ;
270 }
271 rewind(in); /* FIXME: Avoid this.... */
272
273 /* Create the table */
274 out = cpl_table_new(nbentries);
275 cpl_table_new_column(out, IRPLIB_STDSTAR_STAR_COL, CPL_TYPE_STRING);
276 cpl_table_new_column(out, IRPLIB_STDSTAR_TYPE_COL, CPL_TYPE_STRING);
277 cpl_table_new_column(out, IRPLIB_STDSTAR_RA_COL, CPL_TYPE_DOUBLE);
278 cpl_table_new_column(out, IRPLIB_STDSTAR_DEC_COL, CPL_TYPE_DOUBLE);
279 for (i=0 ; i<nfilters ; i++)
280 cpl_table_new_column(out, filters[i], CPL_TYPE_DOUBLE);
281
282 /* Parse the file */
283 nbentries = 0 ;
284 while (fgets(line, 1024, in) != NULL) {
285 if (line[0] != '#') {
286 if (sscanf(line,
287 "%511s %lg %lg %511s %lg %lg %lg %lg %lg %lg %lg %lg",
288 sname, &ra, &dec, stype, &(mags[0]), &(mags[1]),
289 &(mags[2]), &(mags[3]), &(mags[4]), &(mags[5]),
290 &(mags[6]), &(mags[7])) != 12) {
291 fclose(in);
292 cpl_table_delete(out) ;
293 (void)cpl_error_set(cpl_func, CPL_ERROR_BAD_FILE_FORMAT);
294 return NULL ;
295 }
296 cpl_table_set_string(out, IRPLIB_STDSTAR_STAR_COL, nbentries,sname);
297 cpl_table_set_string(out, IRPLIB_STDSTAR_TYPE_COL, nbentries,stype);
298 cpl_table_set_double(out, IRPLIB_STDSTAR_RA_COL, nbentries, ra);
299 cpl_table_set_double(out, IRPLIB_STDSTAR_DEC_COL, nbentries, dec);
300 for (i=0 ; i<nfilters ; i++)
301 cpl_table_set_double(out, filters[i], nbentries, mags[i]);
302 nbentries ++ ;
303 }
304 }
305 fclose(in) ;
306
307 if (cpl_table_get_nrow(out) == 0) {
308 cpl_table_delete(out);
309 (void)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
310 "Empty catalogue in '%s'", filename);
311 return NULL ;
312 }
313
314 return out ;
315}
316
int sofi_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition sofi_dfs.c:60
const char * sofi_get_license(void)
Get the pipeline copyright and license.
Definition sofi_utils.c:60
cpl_frameset * sofi_extract_frameset(const cpl_frameset *in, const char *tag)
Extract the frames with the given tag from a frameset.
Definition sofi_utils.c:298