36#include "visir_recipe.h"
37#include "visir_spectro.h"
38#include "visir_spc_distortion.h"
39#include "visir_spc_photom.h"
40#include "visir_cpl_compat.h"
51#define RECIPE_STRING "visir_util_spc_std_cat"
57static cpl_error_code visir_util_spc_std_cat_save(cpl_frameset *,
58 const cpl_parameterlist *,
62(visir_util_spc_std_cat, 0,
63 "Generate a FITS catalog of spectroscopic standard stars",
64 "This recipe shall be used to generate a FITS catalog of spectroscopic "
65 "standard stars for the VISIR pipeline.\n"
66 "The sof file shall consist of lines with the name of an ASCII-file named "
67 " <Star_Name>.txt, e.g. HD133165.txt and the tag " VISIR_SPC_CAT_ASCII
".\n"
68 "All input ASCII-files must comprise the same number of lines.\nThe first "
69 "line of the ASCII-file must have 6 fields separated by white-space.\nThe "
70 "first three fields are the RA (hh mm ss) which will be stored in degrees "
71 "in a table column labeled 'RA' - all three are non-negative and hh and mm "
73 "The 3 last fields are the DEC (dd mm ss) which will be stored in degrees in "
74 "a table column labeled 'DEC' - all three are non-negative, dd and mm are "
75 "integer, and dd has either a '+' or a '-' prepended (including -00).\n"
76 "The remaining lines must all consist of two fields separated by white-"
78 "The first field is the wavelength (in microns) and the second the (positive) "
79 "model flux in W/m2/m. The wavelengths must be identical in all the input "
104static int visir_util_spc_std_cat(cpl_frameset * framelist,
105 const cpl_parameterlist * parlist)
108 cpl_bivector * spectrum = NULL;
109 cpl_table * catalog = NULL;
110 cpl_array ** wlen_arr;
111 cpl_array ** flux_arr;
117 const double max_radius = VISIR_STAR_MAX_RADIUS;
124 if (cpl_error_get_code())
return cpl_error_get_code();
130 nfiles = cpl_frameset_get_size(framelist);
132 skip_if (nfiles < 1);
134 catalog = cpl_table_new(nfiles);
136 bug_if (catalog == NULL);
140 bug_if (cpl_table_new_column(catalog,
"STARS", CPL_TYPE_STRING));
141 bug_if (cpl_table_new_column(catalog,
"RA", CPL_TYPE_DOUBLE));
142 bug_if (cpl_table_new_column(catalog,
"DEC", CPL_TYPE_DOUBLE));
143 bug_if (cpl_table_new_column_array(catalog,
"WAVELENGTHS", CPL_TYPE_DOUBLE,
145 bug_if (cpl_table_new_column_array(catalog,
"MODEL_FLUX", CPL_TYPE_DOUBLE,
148 star_arr = cpl_table_get_data_string(catalog,
"STARS");
149 wlen_arr = cpl_table_get_data_array(catalog,
"WAVELENGTHS");
150 flux_arr = cpl_table_get_data_array(catalog,
"MODEL_FLUX");
152 bug_if(star_arr == NULL);
153 bug_if(wlen_arr == NULL);
154 bug_if(flux_arr == NULL);
157 for (i=0; i < cpl_frameset_get_size(framelist); i++) {
158 int ra1, ra2, dec1, dec2;
167 const cpl_frame * frame = cpl_frameset_get_position_const(framelist, i);
169 const char * filename = cpl_frame_get_filename(frame);
171 skip_if (filename == NULL);
174 if ((in = fopen(filename,
"r")) == NULL) {
175 cpl_msg_error(cpl_func,
"Could not open file number %d (%s)", i+1,
182 if (fgets(line, 1024, in) == NULL) {
183 cpl_msg_error(cpl_func,
"fgets() returned NULL when reading the "
184 "first line in file number %d (%s)",
189 if (sscanf(line,
"%d %d %lg %c%d %d %lg ", &ra1, &ra2, &ra3, &isign,
190 &dec1, &dec2, &dec3) != 7) {
191 cpl_msg_error(cpl_func,
"Invalid first line in file number %d (%s)",
198 spectrum = cpl_bivector_new(nvals);
199 skip_if (visir_bivector_load(spectrum, in));
205 nvals = cpl_bivector_get_size(spectrum);
206 cpl_table_set_column_depth(catalog,
"WAVELENGTHS", nvals);
207 cpl_table_set_column_depth(catalog,
"MODEL_FLUX", nvals);
208 }
else if (nvals != cpl_bivector_get_size(spectrum)) {
210 cpl_msg_error(cpl_func,
"Length of spectrum in file number %d (%s) "
211 "is different from the previous (with length %d)",
212 i+1, filename, nvals);
216 skip_if (visir_star_convert(line, ra1, ra2, ra3, isign, dec1, dec2,
217 dec3, cpl_bivector_get_y_data(spectrum),
221 fstart = strrchr(filename,
'/');
222 fstart = fstart == NULL ? filename : 1 + fstart;
223 fstop = strrchr(fstart,
'.');
225 ilen = fstop == NULL ? strlen(fstart) : (size_t) (fstop - fstart);
227 if ((
int)ilen <= 0) {
228 cpl_msg_error(cpl_func,
"File number %d (%s) has no basename for "
229 "the FITS table", i+1, filename);
234 star_arr[i] = cpl_malloc(1+ilen);
235 memcpy(star_arr[i], fstart, ilen);
236 star_arr[i][ilen] =
'\0';
239 wlen = cpl_vector_unwrap(cpl_bivector_get_x(spectrum));
240 flux = cpl_vector_unwrap(cpl_bivector_get_y(spectrum));
242 cpl_bivector_unwrap_vectors(spectrum);
245 wlen_arr[i] = cpl_array_wrap_double(wlen, nvals);
246 flux_arr[i] = cpl_array_wrap_double(flux, nvals);
248 bug_if (cpl_table_set_double(catalog,
"RA", i, ra));
249 bug_if (cpl_table_set_double(catalog,
"DEC", i, dec));
253 skip_if (i != nfiles);
255 ras = cpl_table_get_data_double(catalog,
"RA");
256 decs = cpl_table_get_data_double(catalog,
"DEC");
258 mindist = visir_star_dist_min(ras, decs, nfiles, &iloc1, &iloc2);
260 if (mindist < max_radius) {
261 cpl_frame * floc1 = visir_frameset_get_frame(framelist, iloc1);
262 cpl_frame * floc2 = visir_frameset_get_frame(framelist, iloc2);
263 cpl_msg_warning(cpl_func,
"The pair of closest stars is %s (%d) and %s "
264 "(%d) with the distance: %g",
265 cpl_frame_get_filename(floc1), iloc1,
266 cpl_frame_get_filename(floc2), iloc2, mindist);
270 cpl_frame * floc1 = visir_frameset_get_frame(framelist, iloc1);
271 cpl_frame * floc2 = visir_frameset_get_frame(framelist, iloc2);
272 cpl_msg_info(cpl_func,
"The pair of closest stars is %s (%d) and %s "
273 "(%d) with the distance: %g",
274 cpl_frame_get_filename(floc1), iloc1,
275 cpl_frame_get_filename(floc2), iloc2, mindist);
279 cpl_msg_info(cpl_func,
"Saving the table with %d rows each with a spectrum "
280 "length of %d", nfiles, nvals);
281 skip_if (visir_util_spc_std_cat_save(framelist, parlist, catalog));
285 cpl_bivector_delete(spectrum);
286 cpl_table_delete(catalog);
289 return cpl_error_get_code();
305cpl_error_code visir_util_spc_std_cat_save(cpl_frameset * set,
306 const cpl_parameterlist * parlist,
307 const cpl_table * catalog)
309 cpl_propertylist * applist = cpl_propertylist_new();
312 bug_if (catalog == NULL);
313 bug_if (parlist == NULL);
314 bug_if (set == NULL);
316 bug_if(cpl_propertylist_append_string(applist,
"INSTRUME",
"VISIR"));
318 skip_if (irplib_dfs_save_table(set, parlist, set, catalog, NULL,
319 RECIPE_STRING, VISIR_SPEC_STD_CAT_PROCATG,
320 applist, NULL, visir_pipe_id,
321 RECIPE_STRING CPL_DFS_FITS));
325 cpl_propertylist_delete(applist);
327 return cpl_error_get_code();
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.