visir_util_img_std_cat.c

00001 /* $Id: visir_util_img_std_cat.c,v 1.57 2009/01/29 08:56:58 llundin Exp $
00002  *
00003  * This file is part of the VISIR Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2009/01/29 08:56:58 $
00024  * $Revision: 1.57 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <stdio.h>
00037 #include <string.h>
00038 
00039 #include "visir_recipe.h"
00040 
00041 /*-----------------------------------------------------------------------------
00042                                 Defines
00043  -----------------------------------------------------------------------------*/
00044 #define NFILTERS 37
00045 
00046 #define RECIPE_STRING   "visir_util_img_std_cat"
00047 
00048 /*-----------------------------------------------------------------------------
00049                             Private Functions prototypes
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                                 Functions code
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; /* Some positive number */
00141     int          irow  = 0;
00142     int          i, j;
00143 
00144 
00145     if (cpl_error_get_code()) return cpl_error_get_code();
00146 
00147     /* Identify the RAW frames in the input frameset */
00148     skip_if (visir_dfs_set_groups(framelist));
00149 
00150     bug_if(sizeof(filters) != NFILTERS * sizeof(char*));
00151  
00152     /* Create the table */
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     /* Loop on all input frames */
00163     for (i=0, frame = cpl_frameset_get_first(framelist); frame != NULL ;
00164          i++, frame = cpl_frameset_get_next(framelist)) {
00165 
00166         /* Get file name */
00167         const char * filename = cpl_frame_get_filename(frame);
00168         int jrow = 0;
00169 
00170 
00171         /* Open the file */
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                     /* Table needs more rows */
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     /* Resize the table to the actual number of rows read */
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     /* Save the table */
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 

Generated on Thu Mar 24 11:59:40 2011 for VISIR Pipeline Reference Manual by  doxygen 1.5.8