00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include <stdio.h>
00037 #include <string.h>
00038
00039 #include "visir_recipe.h"
00040
00041
00042
00043
00044
00045 #define RECIPE_STRING "visir_util_img_std_cat"
00046
00047
00048
00049
00050
00051 static cpl_error_code visir_util_img_std_cat_save(cpl_frameset *,
00052 const cpl_parameterlist *,
00053 const cpl_table *);
00054
00055 static cpl_error_code visir_util_img_std_cat_parse(const char*, char *,
00056 double *, double *, char *,
00057 double *, int);
00058
00059 VISIR_RECIPE_DEFINE(visir_util_img_std_cat, 0,
00060 "Convert ASCII-file(s) to a FITS standard star catalog",
00061 "This recipe generates a FITS standard star catalog for "
00062 "imaging from one or \n"
00063 "more ASCII-files.\n"
00064 "Each line in the text file must have 45 fields separated "
00065 "by white-space.\n"
00066 "The first field is the star name, e.g. 'HD108903' which "
00067 "will be stored in a \n"
00068 "table column labeled 'STARS'.\n"
00069 "The 3 next fields are the RA (hh mm ss) which will be "
00070 "stored in degrees in \n"
00071 "a table column labeled 'RA' - all three are non-negative "
00072 "and hh and mm are \n"
00073 "integer.\n"
00074 "The 3 next fields are the DEC (dd mm ss) which will be "
00075 "stored in degrees in \n"
00076 "a table column labeled 'DEC' - all three are non-negative, "
00077 "dd and mm are \n"
00078 "integer, while dd has either a '+' or a '-' prepended "
00079 "(including -00).\n"
00080 "The next field is the spectral type which will be "
00081 "stored in a \n"
00082 "table column labeled 'SP_TYPE'.\n"
00083 "The 31 next fields are the JY values for the 31 supported "
00084 "image filters.\n"
00085 "The 6 next fields are the JY values for the 6 supported "
00086 "spectral filters.\n"
00087 "All JY values must be positive.\n"
00088 "For each filter the JY value is stored in a table column "
00089 "labeled with the \n"
00090 "filter name.\n"
00091 "The 37 filter names are hard-coded in the recipe.\n"
00092 "\n"
00093 "Lines beginning with a hash (#) are treated as comments.\n"
00094 "\n"
00095 "The ASCII-input should be tagged " VISIR_IMG_LINES_ASCII
00096 ", but all input \n"
00097 "files will currently be processed regardless.\n");
00098
00099
00103
00104
00105
00106
00107
00108
00109
00117
00118 static int visir_util_img_std_cat(cpl_frameset * framelist,
00119 const cpl_parameterlist * parlist)
00120 {
00121 FILE * in = NULL;
00122
00123 char line[1024];
00124 cpl_table * tab = NULL;
00125 char sname[512];
00126 char stype[512];
00127 const char * filters[]
00128 = {"MV", "N_BAND", "SIC", "PAH1_1", "PAH1", "ARIII", "SIV_1", "SIV",
00129 "PAH2_1", "SIV_2", "PAH2", "PAH2_2", "NEII_1", "NEII", "NEII_2",
00130 "J7_9", "J8_9", "J9_8", "J12_1", "J12_2", "B8_7", "B9_7", "B10_7",
00131 "B11_7", "B12_4", "Q0", "QH2", "Q1", "Q2", "Q3", "Q4", "Q7", "Q8",
00132 "12_4_AGP", "10_5_4QP", "11_3_4QP", "N_SW_spec",
00133 "H2S4_spec", "ARIII_spec", "NEII_2_spec", "H2S3_spec", "H2S1_spec",
00134 };
00135 const int nfilters = sizeof(filters) / sizeof(filters[0]);
00136 double jys[nfilters];
00137 const double max_radius = VISIR_STAR_MAX_RADIUS;
00138 double mindist;
00139 int iloc1, iloc2;
00140 int nrows = 425;
00141 int irow = 0;
00142 int i, j;
00143
00144
00145 if (cpl_error_get_code()) return cpl_error_get_code();
00146
00147
00148 skip_if (visir_dfs_set_groups(framelist));
00149
00150
00151 tab = cpl_table_new(nrows);
00152 skip_if (cpl_table_new_column(tab, "STARS", CPL_TYPE_STRING));
00153 skip_if (cpl_table_new_column(tab, "SP_TYPE", CPL_TYPE_STRING));
00154 skip_if (cpl_table_new_column(tab, "RA", CPL_TYPE_DOUBLE));
00155 skip_if (cpl_table_new_column(tab, "DEC", CPL_TYPE_DOUBLE));
00156
00157 for (j=0 ; j<nfilters ; j++)
00158 skip_if (cpl_table_new_column(tab, filters[j], CPL_TYPE_DOUBLE));
00159
00160
00161 for (i=0; i < cpl_frameset_get_size(framelist); i++) {
00162 const cpl_frame * frame = cpl_frameset_get_position_const(framelist, i);
00163
00164
00165 const char * filename = cpl_frame_get_filename(frame);
00166 int jrow = 0;
00167
00168
00169
00170 skip_if (filename == NULL);
00171 in = fopen(filename, "r");
00172 if (in == NULL) {
00173 cpl_msg_error(cpl_func, "Could not open the %d. file: %s", i+1,
00174 filename);
00175 skip_if (1);
00176 }
00177
00178 while (fgets(line, 1024, in) != NULL) {
00179 jrow++;
00180 if (line[0] != '#') {
00181 double ra, dec;
00182
00183 if (visir_util_img_std_cat_parse(line, sname, &ra, &dec,
00184 stype, jys, nfilters)) {
00185 cpl_msg_error(cpl_func, "Unparsable line %d in %d. file "
00186 "%s", jrow, i+1, filename);
00187 skip_if(1);
00188 }
00189 if (irow == nrows) {
00190
00191 nrows *= 2;
00192 skip_if (cpl_table_set_size(tab, nrows));
00193 }
00194 skip_if (cpl_table_set_string(tab, "STARS", irow, sname));
00195 skip_if (cpl_table_set_string(tab, "SP_TYPE", irow, stype));
00196 skip_if (cpl_table_set_double(tab, "RA", irow, ra));
00197 skip_if (cpl_table_set_double(tab, "DEC", irow, dec));
00198 for (j=0 ; j<nfilters ; j++)
00199 skip_if(cpl_table_set_double(tab, filters[j], irow,
00200 jys[j]));
00201 irow++;
00202
00203 }
00204 }
00205 fclose(in);
00206 in = NULL;
00207 if (jrow == 0) {
00208 cpl_msg_warning(cpl_func, "No usable lines in file %s", filename);
00209 }
00210 }
00211
00212 skip_if (irow == 0);
00213
00214
00215 nrows = irow;
00216 skip_if (cpl_table_set_size(tab, nrows));
00217
00218 mindist = visir_star_dist_min(cpl_table_get_data_double(tab, "RA"),
00219 cpl_table_get_data_double(tab, "DEC"), nrows,
00220 &iloc1, &iloc2);
00221
00222 if (mindist < max_radius)
00223 cpl_msg_warning(cpl_func, "The pair of closest stars is %s (%d) and %s "
00224 "(%d) with the distance: %g",
00225 cpl_table_get_string(tab, "STARS", iloc1), iloc1,
00226 cpl_table_get_string(tab, "STARS", iloc2), iloc2,
00227 mindist);
00228 else
00229 cpl_msg_info(cpl_func, "The pair of closest stars is %s (%d) and %s "
00230 "(%d) with the distance: %g",
00231 cpl_table_get_string(tab, "STARS", iloc1), iloc1,
00232 cpl_table_get_string(tab, "STARS", iloc2), iloc2,
00233 mindist);
00234
00235
00236 cpl_msg_info(cpl_func, "Saving the table with %d rows and %d filters",
00237 nrows, nfilters);
00238
00239 skip_if (visir_util_img_std_cat_save(framelist, parlist, tab));
00240
00241 end_skip;
00242
00243 if (in) fclose(in);
00244 cpl_table_delete(tab);
00245
00246 return cpl_error_get_code();
00247 }
00248
00249
00262
00263 static cpl_error_code visir_util_img_std_cat_parse(const char * line,
00264 char * sname, double * pra,
00265 double * pdec, char *stype,
00266 double * jys, int njys)
00267 {
00268
00269 const char * format = "%s %d %d %lg %c%d %d %lg %s %lg %lg %lg %lg %lg %lg "
00270 "%lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg "
00271 "%lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg";
00272
00273 int nvals;
00274 int ra1, ra2;
00275 int dec1, dec2;
00276 double ra3, dec3;
00277 char isign;
00278
00279 assert( line );
00280 assert( sname );
00281 assert( stype );
00282 assert( jys );
00283
00284
00285 nvals = sscanf(line, format,
00286 sname, &ra1, &ra2, &ra3, &isign, &dec1, &dec2,
00287 &dec3, stype, &(jys[0]), &(jys[1]), &(jys[2]),
00288 &(jys[3]), &(jys[4]), &(jys[5]), &(jys[6]),
00289 &(jys[7]), &(jys[8]), &(jys[9]), &(jys[10]),
00290 &(jys[11]), &(jys[12]), &(jys[13]), &(jys[14]),
00291 &(jys[15]), &(jys[16]), &(jys[17]), &(jys[18]),
00292 &(jys[19]), &(jys[20]), &(jys[21]), &(jys[22]),
00293 &(jys[23]), &(jys[24]), &(jys[25]), &(jys[26]),
00294 &(jys[27]), &(jys[28]), &(jys[29]), &(jys[30]),
00295 &(jys[31]), &(jys[32]), &(jys[33]), &(jys[34]),
00296 &(jys[35]), &(jys[36]), &(jys[37]), &(jys[38]),
00297 &(jys[39]), &(jys[40]), &(jys[41]));
00298
00299 if (nvals != njys+9) {
00300 cpl_msg_error(cpl_func, "Line with length=%u has %d not %d items "
00301 "formatted: %s", (unsigned)strlen(line), nvals, njys+9,
00302 format);
00303 cpl_ensure_code(0, CPL_ERROR_BAD_FILE_FORMAT);
00304 }
00305
00306 return visir_star_convert(line, ra1, ra2, ra3, isign, dec1, dec2,
00307 dec3, jys, njys, pra, pdec);
00308
00309 }
00310
00311
00319
00320 static
00321 cpl_error_code visir_util_img_std_cat_save(cpl_frameset * set,
00322 const cpl_parameterlist * parlist,
00323 const cpl_table * tab)
00324 {
00325 cpl_propertylist * applist = cpl_propertylist_new();
00326
00327
00328 bug_if(cpl_propertylist_append_string(applist, "INSTRUME", "VISIR"));
00329
00330 skip_if(irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
00331 VISIR_IMA_STD_CAT_PROCATG,
00332 applist, NULL, visir_pipe_id,
00333 RECIPE_STRING CPL_DFS_FITS));
00334
00335 end_skip;
00336
00337 cpl_propertylist_delete(applist);
00338
00339 return cpl_error_get_code();
00340 }
00341