Main Page   File List  

visir_util_img_std_cat.c

00001 /* $Id: visir_util_img_std_cat.c,v 1.36 2006/01/04 13:39:29 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: 2006/01/04 13:39:29 $
00024  * $Revision: 1.36 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <cpl.h>
00037 #include <assert.h>
00038 
00039 #include "visir_utils.h"
00040 #include "visir_pfits.h"
00041 #include "visir_dfs.h"
00042 #include "visir_inputs.h"
00043 
00044 /*-----------------------------------------------------------------------------
00045                                 Defines
00046  -----------------------------------------------------------------------------*/
00047 #define NFILTERS 29
00048 
00049 /*-----------------------------------------------------------------------------
00050                             Functions prototypes
00051  -----------------------------------------------------------------------------*/
00052 
00053 static int visir_util_img_std_cat_create(cpl_plugin *);
00054 static int visir_util_img_std_cat_exec(cpl_plugin *);
00055 static int visir_util_img_std_cat_destroy(cpl_plugin *);
00056 static int visir_util_img_std_cat(cpl_parameterlist *, cpl_frameset *);
00057 static int visir_util_img_std_cat_save(const cpl_table *,
00058                                        const cpl_parameterlist *,
00059                                        cpl_frameset *);
00060 static cpl_error_code visir_util_img_std_cat_parse(const char*, char *,
00061                                                    double *, double *, char *,
00062                                                    double *, int);
00063 /*-----------------------------------------------------------------------------
00064                             Static variables
00065  -----------------------------------------------------------------------------*/
00066 
00067 static const char * recipename = "visir_util_img_std_cat";
00068 
00069 static char visir_util_img_std_cat_description[] =
00070 "This recipe generates a FITS standard star catalog for imaging from one or \n"
00071 "more ASCII-files.\n"
00072 "Each line in the text file must have 31 fields separated by white-space.\n"
00073 "The first field is the star name, e.g. 'HD108903' which will be stored in a \n"
00074 "table column labeled 'STARS'.\n"
00075 "The second field is the spectral type which will be stored in a \n"
00076 "table column labeled 'SP_TYPE'.\n"
00077 "The 3 next fields are the RA (hh mm ss) which will be stored in degrees in \n"
00078 "a table column labeled 'RA' - all three are non-negative and hh and mm are \n"
00079 "integer.\n"
00080 "The 3 next fields are the DEC (dd mm ss) which will be stored in degrees in \n"
00081 "a table column labeled 'DEC' - all three are non-negative, dd and mm are \n"
00082 "integer, while dd has either a '+' or a '-' prepended (including -00).\n"
00083 "The 23 next fields are the JY values for the 23 supported image filters.\n"
00084 "The 6 next fields are the JY values for the 6 supported spectral filters.\n"
00085 "All JY values must be positive.\n"
00086 "For each filter the JY value is stored in a table column labeled with the \n"
00087 "filter name.\n"
00088 "The 29 filter names are hard-coded in the recipe.\n"
00089 "\n"
00090 "Lines beginning with a hash (#) are treated as comments.\n"
00091 "\n"
00092 "The ASCII-input should be tagged " VISIR_IMG_LINES_ASCII ", but all input \n"
00093 "files will currently be processed regardless.\n";
00094 
00095 
00096 /*-----------------------------------------------------------------------------
00097                                 Functions code
00098  -----------------------------------------------------------------------------*/
00099 
00100 /*----------------------------------------------------------------------------*/
00109 /*----------------------------------------------------------------------------*/
00110 int cpl_plugin_get_info(cpl_pluginlist * list)
00111 {
00112     cpl_recipe  *   recipe = cpl_calloc(1, sizeof(*recipe));
00113     cpl_plugin  *   plugin = &recipe->interface;
00114 
00115 
00116     if (cpl_plugin_init(plugin,
00117                     CPL_PLUGIN_API,
00118                     VISIR_BINARY_VERSION,
00119                     CPL_PLUGIN_TYPE_RECIPE,
00120                     recipename,
00121                     "Convert ASCII-file(s) to a FITS standard star catalog",
00122                     visir_util_img_std_cat_description,
00123                     "Lars Lundin",
00124                     PACKAGE_BUGREPORT,
00125                     visir_get_license(),
00126                     visir_util_img_std_cat_create,
00127                     visir_util_img_std_cat_exec,
00128                     visir_util_img_std_cat_destroy)) return 1;
00129 
00130     if (cpl_pluginlist_append(list, plugin)) return 1;
00131     
00132     return 0;
00133 }
00134 
00135 /*----------------------------------------------------------------------------*/
00144 /*----------------------------------------------------------------------------*/
00145 static int visir_util_img_std_cat_create(cpl_plugin * plugin)
00146 {
00147     cpl_recipe * recipe = (cpl_recipe *)plugin;
00148 
00149     /* Verify plugin type */
00150     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00151 
00152     /* Create the parameters list in the cpl_recipe object */
00153     recipe->parameters = cpl_parameterlist_new();
00154 
00155     return 0;
00156 }
00157 
00158 /*----------------------------------------------------------------------------*/
00164 /*----------------------------------------------------------------------------*/
00165 static int visir_util_img_std_cat_exec(cpl_plugin * plugin)
00166 {
00167     cpl_recipe * recipe = (cpl_recipe *)plugin;
00168 
00169     /* Verify plugin type */
00170     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00171 
00172     return visir_util_img_std_cat(recipe->parameters, recipe->frames);
00173 }
00174 
00175 /*----------------------------------------------------------------------------*/
00181 /*----------------------------------------------------------------------------*/
00182 static int visir_util_img_std_cat_destroy(cpl_plugin * plugin)
00183 {
00184     cpl_recipe  *   recipe = (cpl_recipe *)plugin;
00185 
00186     /* Verify plugin type */
00187     if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) return 1;
00188     cpl_parameterlist_delete(recipe->parameters);
00189     return  0;
00190 }
00191 
00192 /*----------------------------------------------------------------------------*/
00200 /*----------------------------------------------------------------------------*/
00201 static int visir_util_img_std_cat(
00202         cpl_parameterlist   *   parlist,
00203         cpl_frameset        *   framelist)
00204 {
00205     const int    nfilters = NFILTERS;
00206     const cpl_frame *  frame;
00207     FILE       * in = NULL;
00208 
00209     char         line[1024];
00210     cpl_table  * tab = NULL;
00211     char         sname[512];
00212     char         stype[512];
00213     double       jys[NFILTERS];
00214     const char * filters[NFILTERS];
00215     const double max_radius = VISIR_STAR_MAX_RADIUS;
00216     double       mindist;
00217     int          iloc1, iloc2;
00218     int          nrows = 425; /* Some positive number */
00219     int          irow  = 0;
00220     int          i, j;
00221 
00222 
00223     if (cpl_error_get_code()) return cpl_error_get_code();
00224 
00225     /* Identify the RAW frames in the input frameset */
00226     skip_if (visir_dfs_set_groups(framelist));
00227  
00228     filters[0] = "MV";
00229     filters[1] = "N-BAND";
00230     filters[2] = "SIC";
00231     filters[3] = "PAH1_1";
00232     filters[4] = "PAH1";
00233     filters[5] = "ARIII";
00234     filters[6] = "SIV_1";
00235     filters[7] = "SIV";
00236     filters[8] = "PAH2_1";
00237     filters[9] = "SIV_2";
00238     filters[10] = "PAH2";
00239     filters[11] = "PAH2_2";
00240     filters[12] = "NEII_1";
00241     filters[13] = "NEII";
00242     filters[14] = "NEII_2";
00243     filters[15] = "Q0";
00244     filters[16] = "QH2";
00245     filters[17] = "Q1";
00246     filters[18] = "Q2";
00247     filters[19] = "Q3";
00248     filters[20] = "Q4";
00249     filters[21] = "Q7";
00250     filters[22] = "Q8";
00251     filters[23] = "N-SW-spec";
00252     filters[24] = "H2S4-spec";
00253     filters[25] = "ARIII-spec";
00254     filters[26] = "NEII_2-spec";
00255     filters[27] = "H2S3-spec";
00256     filters[28] = "H2S1-spec";
00257 
00258     /* Create the table */
00259     tab = cpl_table_new(nrows);
00260     skip_if (cpl_table_new_column(tab, "STARS", CPL_TYPE_STRING));
00261     skip_if (cpl_table_new_column(tab, "SP_TYPE", CPL_TYPE_STRING));
00262     skip_if (cpl_table_new_column(tab, "RA", CPL_TYPE_DOUBLE));
00263     skip_if (cpl_table_new_column(tab, "DEC", CPL_TYPE_DOUBLE));
00264 
00265     for (j=0 ; j<nfilters ; j++)
00266         skip_if (cpl_table_new_column(tab, filters[j], CPL_TYPE_DOUBLE));
00267     
00268     /* Loop on all input frames */
00269     for (i=0, frame = cpl_frameset_get_first(framelist); frame != NULL ;
00270          i++, frame = cpl_frameset_get_next(framelist)) {
00271 
00272         /* Get file name */
00273         const char * filename = cpl_frame_get_filename(frame);
00274         int jrow = 0;
00275 
00276 
00277         /* Open the file */
00278         skip_if (filename == NULL);
00279         in = fopen(filename, "r");
00280         if (in == NULL) {
00281             cpl_msg_error(__func__, "Could not open the %d. file: %s", i+1,
00282                           filename);
00283             skip_if (1);
00284         }
00285 
00286         while (fgets(line, 1024, in) != NULL) {
00287             jrow++;
00288             if (line[0] != '#') {
00289                 double ra, dec;
00290 
00291                 if (visir_util_img_std_cat_parse(line, sname, &ra, &dec,
00292                                                  stype, jys, nfilters)) {
00293                     cpl_msg_error(__func__, "Unparsable line %d in %d. file "
00294                                   "%s", jrow, i+1, filename);
00295                     skip_if(1);
00296                 }
00297                 if (irow == nrows) {
00298                     /* Table needs more rows */
00299                     nrows *= 2;
00300                     skip_if (cpl_table_set_size(tab, nrows));
00301                 }
00302                 skip_if (cpl_table_set_string(tab, "STARS", irow, sname));
00303                 skip_if (cpl_table_set_string(tab, "SP_TYPE", irow, stype));
00304                 skip_if (cpl_table_set_double(tab, "RA", irow, ra));
00305                 skip_if (cpl_table_set_double(tab, "DEC", irow, dec));
00306                 for (j=0 ; j<nfilters ; j++) 
00307                     skip_if(cpl_table_set_double(tab, filters[j], irow,
00308                                                  jys[j]));
00309                 irow++;
00310 
00311             }
00312         }
00313         fclose(in);
00314         in = NULL;
00315         if (jrow == 0) {
00316             cpl_msg_warning(__func__, "No usable lines in file %s", filename);
00317         }
00318     }
00319 
00320     skip_if( frame != NULL );
00321 
00322     skip_if (irow == 0);
00323 
00324     /* Resize the table to the actual number of rows read */
00325     nrows = irow;
00326     skip_if (cpl_table_set_size(tab, nrows));
00327 
00328     mindist = visir_star_dist_min(cpl_table_get_data_double(tab, "RA"),
00329                                   cpl_table_get_data_double(tab, "DEC"), nrows,
00330                                   &iloc1, &iloc2);
00331 
00332     if (mindist < max_radius)
00333         cpl_msg_warning(__func__, "The pair of closest stars is %s (%d) and %s "
00334                         "(%d) with the distance: %g",
00335                         cpl_table_get_string(tab, "STARS", iloc1), iloc1,
00336                         cpl_table_get_string(tab, "STARS", iloc2), iloc2,
00337                         mindist);
00338     else
00339         cpl_msg_info(__func__, "The pair of closest stars is %s (%d) and %s "
00340                      "(%d) with the distance: %g",
00341                      cpl_table_get_string(tab, "STARS", iloc1), iloc1,
00342                      cpl_table_get_string(tab, "STARS", iloc2), iloc2,
00343                      mindist);
00344 
00345     /* Save the table */
00346     cpl_msg_info(__func__, "Saving the table with %d rows", nrows);
00347 
00348     skip_if (visir_util_img_std_cat_save(tab, parlist, framelist));
00349 
00350     end_skip;
00351 
00352     if (in) fclose(in);
00353     cpl_table_delete(tab);
00354     
00355     return cpl_error_get_code();
00356 }
00357 
00358 /*----------------------------------------------------------------------------*/
00371 /*----------------------------------------------------------------------------*/
00372 static cpl_error_code visir_util_img_std_cat_parse(const char * line,
00373                                                    char * sname, double * pra,
00374                                                    double * pdec, char *stype,
00375                                                    double * jys, int njys)
00376 {
00377 
00378     const char * format = "%s %d %d %lg %c%d %d %lg %s %lg %lg %lg %lg %lg %lg "
00379         "%lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg "
00380         "%lg %lg %lg %lg %lg";
00381 
00382     int nvals;
00383     int ra1, ra2;
00384     int dec1, dec2;
00385     double ra3, dec3;
00386     char isign;
00387 
00388     assert( line );
00389     assert( sname );
00390     assert( stype );
00391     assert( jys );
00392     assert( njys == NFILTERS );
00393 
00394 
00395     nvals = sscanf(line, format,
00396                    sname, &ra1, &ra2, &ra3, &isign, &dec1, &dec2,
00397                    &dec3, stype, &(jys[0]), &(jys[1]), &(jys[2]),
00398                    &(jys[3]), &(jys[4]), &(jys[5]), &(jys[6]),
00399                    &(jys[7]), &(jys[8]), &(jys[9]), &(jys[10]),
00400                    &(jys[11]), &(jys[12]), &(jys[13]), &(jys[14]),
00401                    &(jys[15]), &(jys[16]), &(jys[17]), &(jys[18]),
00402                    &(jys[19]), &(jys[20]), &(jys[21]), &(jys[22]),
00403                    &(jys[23]), &(jys[24]), &(jys[25]), &(jys[26]),
00404                    &(jys[27]), &(jys[28]));
00405 
00406     if (nvals != njys+9) {
00407         cpl_msg_error(__func__, "Line with length=%u has %d not %d items "
00408                       "formatted: %s", (unsigned)strlen(line), nvals, njys+9,
00409                       format);
00410         visir_assure_code(0, CPL_ERROR_BAD_FILE_FORMAT);
00411     }
00412 
00413     return visir_star_convert(line, ra1, ra2, ra3, isign, dec1, dec2,
00414                               dec3, jys, njys, pra, pdec);
00415                                     
00416 }
00417 
00418 /*----------------------------------------------------------------------------*/
00426 /*----------------------------------------------------------------------------*/
00427 static int visir_util_img_std_cat_save(
00428         const cpl_table         * tab,
00429         const cpl_parameterlist * parlist,
00430         cpl_frameset            * set)
00431 {
00432 
00433     return visir_table_save(parlist, set, tab, recipename,
00434                             VISIR_IMA_STD_CAT_PROCATG, NULL, "");
00435 }
00436 

Generated on Mon Jan 23 12:16:37 2006 for VISIR Pipeline Reference Manual by doxygen1.2.18