37 #include "crires_recipe.h"
43 #define RECIPE_STRING "crires_util_genstd"
49 static int crires_util_genstd_save(cpl_table *,
const cpl_parameterlist *,
52 static char crires_util_genstd_description[] =
53 "This recipe is used to generate standard star photospheric flux tables.\n"
54 "The sof consists of file names tagged with "CRIRES_UTIL_GENSTD_RAW
".\n"
55 "The specified files are named after the standard star they represent\n"
56 "(e.g. HIP61007.txt).\n"
57 "The first line of the file must contain the RA and DEC (hh mm ss).\n"
58 "(e.g. # 13 20 35.818 -36 42 44.26).\n"
59 "The rest of the file must contain two columns:\n"
60 "1st: Wavelengths in increasing order (the unit is corrected by\n"
61 " the factor option to obtain nanometers).\n"
62 "2nd: The atmospheric emission.\n"
63 "The file is generated using the ASCII files in the catalogs/stdstar\n"
64 "directory of the CRIRES source-code distribution."
66 "This recipe produces 1 file for each input file:\n"
67 "First product: the table with the photospheric flux of the std.\n"
68 " (PRO TYPE = "CRIRES_PROTYPE_PHO_FLUX
")\n" ;
70 CRIRES_RECIPE_DEFINE(crires_util_genstd,
72 "Generate standard star FITS tables",
73 crires_util_genstd_description) ;
83 } crires_util_genstd_config ;
99 static int crires_util_genstd(
100 cpl_frameset * framelist,
101 const cpl_parameterlist * parlist)
105 cpl_bivector * bivec_ref ;
106 cpl_bivector * bivec ;
107 int ra1, ra2, dec1, dec2;
111 cpl_frame * cur_frame ;
112 const char * cur_fname ;
114 int nvals, nframes, nvals_ref ;
122 setlocale(LC_NUMERIC,
"C");
125 crires_util_genstd_config.display = crires_parameterlist_get_bool(
126 parlist, RECIPE_STRING, CRIRES_PARAM_PLOT) ;
129 if (crires_dfs_set_groups(framelist, NULL)) {
130 cpl_msg_error(__func__,
"Cannot identify RAW and CALIB frames") ;
133 nframes = cpl_frameset_get_size(framelist) ;
136 cur_frame = cpl_frameset_get_position(framelist, 0) ;
137 cur_fname = cpl_frame_get_filename(cur_frame) ;
138 if ((bivec_ref = cpl_bivector_read(cur_fname))==NULL){
139 cpl_msg_error(__func__,
"Cannot load the file in the bivector") ;
142 pwave_ref = cpl_bivector_get_x_data(bivec_ref) ;
143 nvals_ref = cpl_bivector_get_size(bivec_ref) ;
146 tab = cpl_table_new(nframes+1) ;
147 cpl_table_new_column(tab, CRIRES_COL_STDNAME, CPL_TYPE_STRING) ;
148 cpl_table_new_column(tab, CRIRES_COL_RA, CPL_TYPE_DOUBLE) ;
149 cpl_table_new_column(tab, CRIRES_COL_DEC, CPL_TYPE_DOUBLE) ;
150 cpl_table_new_column_array(tab, CRIRES_COL_PHOTOFLUX, CPL_TYPE_DOUBLE,
154 cpl_table_set_string(tab, CRIRES_COL_STDNAME, 0,
"WAVE") ;
155 cpl_table_set_double(tab, CRIRES_COL_RA, 0, -1.0) ;
156 cpl_table_set_double(tab, CRIRES_COL_DEC, 0, -1.0) ;
157 array = cpl_array_wrap_double(pwave_ref, nvals_ref) ;
158 cpl_table_set_array(tab, CRIRES_COL_PHOTOFLUX, 0, array) ;
159 cpl_array_unwrap(array) ;
162 for (i=0 ; i<nframes ; i++) {
165 cur_frame = cpl_frameset_get_position(framelist, i) ;
166 cur_fname = cpl_frame_get_filename(cur_frame) ;
169 if ((in = fopen(cur_fname,
"r")) == NULL) {
170 cpl_msg_error(__func__,
"Could not open %s", cur_fname) ;
171 cpl_table_delete(tab) ;
172 cpl_bivector_delete(bivec_ref) ;
175 if (fgets(line, 1024, in) == NULL) {
177 cpl_table_delete(tab) ;
178 cpl_bivector_delete(bivec_ref) ;
181 if (sscanf(line,
"#%d %d %lg %c%d %d %lg ", &ra1, &ra2, &ra3, &isign,
182 &dec1, &dec2, &dec3) != 7) {
183 cpl_msg_error(__func__,
"Invalid first line in file %s", cur_fname);
185 cpl_table_delete(tab) ;
186 cpl_bivector_delete(bivec_ref) ;
190 ra = crires_ra_hms2deg(ra1, ra2, ra3) ;
191 dec = crires_dec_hms2deg(dec1, dec2, dec3) ;
192 if (isign ==
'-') dec *= -1.0 ;
195 if ((bivec = cpl_bivector_read(cur_fname))==NULL){
196 cpl_msg_error(__func__,
"Cannot load the file in the bivector") ;
197 cpl_bivector_delete(bivec_ref) ;
198 cpl_table_delete(tab) ;
201 pwave = cpl_bivector_get_x_data(bivec) ;
202 pemiss = cpl_bivector_get_y_data(bivec) ;
203 nvals = cpl_bivector_get_size(bivec) ;
206 if (nvals != nvals_ref) {
207 cpl_msg_error(__func__,
"Invalid file size: %s", cur_fname) ;
208 cpl_bivector_delete(bivec_ref) ;
209 cpl_bivector_delete(bivec) ;
210 cpl_table_delete(tab) ;
215 for (j=0 ; j<nvals ; j++) {
216 if (pwave[j] != pwave_ref[j]) {
217 cpl_msg_error(__func__,
"Invalid bins in %s", cur_fname) ;
218 cpl_bivector_delete(bivec_ref) ;
219 cpl_bivector_delete(bivec) ;
220 cpl_table_delete(tab) ;
226 if (crires_util_genstd_config.display) {
228 "set grid;set xlabel 'Wavelength (nm)';set ylabel 'Flux (jy)';",
229 "t 'Photospheric flux' w lines",
"", bivec);
233 cpl_table_set_string(tab, CRIRES_COL_STDNAME, i+1,
234 crires_get_root_name(crires_get_base_name(cur_fname))) ;
237 cpl_table_set_double(tab, CRIRES_COL_RA, i+1, ra) ;
238 cpl_table_set_double(tab, CRIRES_COL_DEC, i+1, dec) ;
241 array = cpl_array_wrap_double(pemiss, nvals) ;
242 cpl_table_set_array(tab, CRIRES_COL_PHOTOFLUX, i+1, array) ;
243 cpl_array_unwrap(array) ;
245 cpl_bivector_delete(bivec) ;
247 cpl_bivector_delete(bivec_ref) ;
250 cpl_msg_info(__func__,
"Save the table") ;
251 if (crires_util_genstd_save(tab, parlist, framelist) == -1) {
252 cpl_msg_error(__func__,
"Cannot write the table") ;
253 cpl_table_delete(tab) ;
256 cpl_table_delete(tab) ;
270 static int crires_util_genstd_save(
271 cpl_table * out_table,
272 const cpl_parameterlist * parlist,
275 cpl_propertylist * plist ;
277 plist = cpl_propertylist_new();
278 cpl_propertylist_append_string(plist,
"INSTRUME",
"CRIRES") ;
279 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
280 CRIRES_CALPRO_STD_PHOTOFLUX) ;
281 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE,
282 CRIRES_PROTYPE_PHO_FLUX) ;
284 if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
285 NULL,
"crires_util_genstd", plist, NULL,
286 PACKAGE
"/" PACKAGE_VERSION,
287 "crires_util_genstd.fits") != CPL_ERROR_NONE) {
288 cpl_msg_error(__func__,
"Cannot save the table") ;
291 cpl_propertylist_delete(plist) ;