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