visir_util_spc_std_cat.c

00001 /* $Id: visir_util_spc_std_cat.c,v 1.55 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.55 $
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 #include "visir_spectro.h"
00041 #include "visir_spc_distortion.h"
00042 #include "visir_spc_photom.h"
00043 
00044 
00045 
00046 /*-----------------------------------------------------------------------------
00047                             Recipe defines
00048  -----------------------------------------------------------------------------*/
00049 
00050 #define RECIPE_STRING   "visir_util_spc_std_cat"
00051 
00052 /*-----------------------------------------------------------------------------
00053                             Private Functions prototypes
00054  -----------------------------------------------------------------------------*/
00055 
00056 static cpl_error_code visir_util_spc_std_cat_save(cpl_frameset *, 
00057                                                   const cpl_parameterlist *,
00058                                                   const cpl_table *);
00059 
00060 VISIR_RECIPE_DEFINE
00061 (visir_util_spc_std_cat, 0,
00062  "Generate a FITS catalog of spectroscopic standard stars",
00063  "This recipe shall be used to generate a FITS catalog of spectroscopic "
00064  "standard stars for the VISIR pipeline.\n"
00065  "The sof file shall consist of lines with the name of an ASCII-file named "
00066  " <Star_Name>.txt, e.g. HD133165.txt and the tag " VISIR_SPC_CAT_ASCII ".\n"
00067  "All input ASCII-files must comprise the same number of lines.\nThe first "
00068  "line of the ASCII-file must have 6 fields separated by white-space.\nThe "
00069  "first three fields are the RA (hh mm ss) which will be stored in degrees "
00070  "in a table column labeled 'RA' - all three are non-negative and hh and mm "
00071  "are integer.\n"
00072  "The 3 last fields are the DEC (dd mm ss) which will be stored in degrees in "
00073  "a table column labeled 'DEC' - all three are non-negative, dd and mm are "
00074  "integer, and dd has either a '+' or a '-' prepended (including -00).\n"
00075  "The remaining lines must all consist of two fields separated by white-"
00076  "space.\n"
00077  "The first field is the wavelength (in microns) and the second the (positive) "
00078  "model flux in W/m2/m. The wavelengths must be identical in all the input "
00079  "files.\n");
00080 
00081 /*----------------------------------------------------------------------------*/
00085 /*----------------------------------------------------------------------------*/
00086 
00087 /*-----------------------------------------------------------------------------
00088                                 Functions code
00089  -----------------------------------------------------------------------------*/
00090 
00091 /*----------------------------------------------------------------------------*/
00102 /*----------------------------------------------------------------------------*/
00103 static int visir_util_spc_std_cat(cpl_frameset            * framelist,
00104                                   const cpl_parameterlist * parlist)
00105 {
00106     int               nfiles;
00107     const cpl_frame * frame;
00108     cpl_bivector    * spectrum = NULL;  /* Flux model for one star */
00109     cpl_table       * catalog  = NULL;  /* Flux model for all stars */ 
00110     cpl_array      ** wlen_arr;
00111     cpl_array      ** flux_arr;
00112     char           ** star_arr;
00113     FILE            * in = NULL;
00114     char              line[1024];
00115     const double    * ras;              /* RAs */
00116     const double    * decs;             /* DECs */
00117     const double      max_radius = VISIR_STAR_MAX_RADIUS;
00118     double            mindist;
00119     int               iloc1, iloc2;
00120     int               nvals;
00121     int               i;
00122     
00123 
00124     if (cpl_error_get_code()) return cpl_error_get_code();
00125 
00126     /* Identify the RAW frames in the input frameset */
00127     skip_if (visir_dfs_set_groups(framelist));
00128 
00129     /* Get the number of files */
00130     nfiles = cpl_frameset_get_size(framelist);
00131 
00132     skip_if (nfiles < 1);
00133 
00134     catalog = cpl_table_new(nfiles); /* One row per file */
00135 
00136     bug_if (catalog == NULL);
00137 
00138     nvals = 2300; /* 1st guess at length of spectra */
00139 
00140     bug_if (cpl_table_new_column(catalog, "STARS", CPL_TYPE_STRING));
00141     bug_if (cpl_table_new_column(catalog, "RA",    CPL_TYPE_DOUBLE));
00142     bug_if (cpl_table_new_column(catalog, "DEC",   CPL_TYPE_DOUBLE));
00143     bug_if (cpl_table_new_column_array(catalog, "WAVELENGTHS", CPL_TYPE_DOUBLE,
00144                                        nvals));
00145     bug_if (cpl_table_new_column_array(catalog, "MODEL_FLUX", CPL_TYPE_DOUBLE,
00146                                        nvals));
00147 
00148     star_arr = cpl_table_get_data_string(catalog, "STARS");
00149     wlen_arr = cpl_table_get_data_array(catalog,  "WAVELENGTHS");
00150     flux_arr = cpl_table_get_data_array(catalog,  "MODEL_FLUX");
00151 
00152     bug_if(star_arr == NULL);
00153     bug_if(wlen_arr == NULL);
00154     bug_if(flux_arr == NULL);
00155 
00156     /* Loop on all input frames */
00157     for (i=0, frame = cpl_frameset_get_first(framelist); frame != NULL ;
00158          i++, frame = cpl_frameset_get_next(framelist)) {
00159         int ra1, ra2, dec1, dec2;
00160         double ra3, dec3;
00161         double ra, dec; /* Converted star position [degree] */
00162         const char * fstart;
00163         const char * fstop;
00164         size_t ilen;
00165         char isign;
00166         double  * wlen;
00167         double  * flux;
00168         /* Get file name */
00169         const char * filename = cpl_frame_get_filename(frame);
00170 
00171         skip_if (filename == NULL);
00172 
00173         /* Open the file */
00174         if ((in = fopen(filename, "r")) == NULL) {
00175             cpl_msg_error(cpl_func, "Could not open file number %d (%s)", i+1,
00176                           filename);
00177             skip_if (1);
00178         }
00179 
00180         /* Read header */
00181         /* Get RA and DEC */
00182         if (fgets(line, 1024, in) == NULL) {
00183             cpl_msg_error(cpl_func, "fgets() returned NULL when reading the "
00184                           "first line in file number %d (%s)",
00185                           i+1, filename);
00186             skip_if (1);
00187         }
00188 
00189         if (sscanf(line, "%d %d %lg %c%d %d %lg ", &ra1, &ra2, &ra3, &isign,
00190                     &dec1,  &dec2, &dec3) != 7) {
00191             cpl_msg_error(cpl_func, "Invalid first line in file number %d (%s)",
00192                           i+1, filename);
00193             skip_if (1);
00194         }
00195 
00196         /* Get the flux model */
00197         /* On 1st iteration a resizing may be needed */
00198         spectrum = cpl_bivector_new(nvals);
00199         skip_if (visir_bivector_load(spectrum, in));
00200 
00201         fclose(in);
00202         in = NULL;
00203     
00204         if (i == 0) {
00205             nvals = cpl_bivector_get_size(spectrum); /* Get actual length */
00206             cpl_table_set_column_depth(catalog, "WAVELENGTHS", nvals);
00207             cpl_table_set_column_depth(catalog, "MODEL_FLUX",  nvals);
00208         } else if (nvals != cpl_bivector_get_size(spectrum)) {
00209             /* The number of vals must be the same in all files */
00210             cpl_msg_error(cpl_func, "Length of spectrum in file number %d (%s) "
00211                           "is different from the previous (with length %d)",
00212                           i+1, filename, nvals);
00213             skip_if (1);
00214         }
00215 
00216         skip_if (visir_star_convert(line, ra1, ra2, ra3, isign, dec1, dec2,
00217                                     dec3, cpl_bivector_get_y_data(spectrum),
00218                                     nvals, &ra, &dec));
00219 
00220         /* Get the star name, i.e. the basename of the file */
00221         fstart = strrchr(filename, '/');
00222         fstart = fstart == NULL ? filename : 1 + fstart;
00223         fstop  = strrchr(fstart, '.');
00224         /* ilen is the length of the star name without the null-terminator */
00225         ilen = fstop == NULL ? strlen(fstart) : (size_t) (fstop - fstart);
00226 
00227         if ((int)ilen <= 0) {
00228             cpl_msg_error(cpl_func, "File number %d (%s) has no basename for "
00229                           "the FITS table", i+1, filename);
00230             skip_if(1);
00231         }
00232         
00233         /* Create, copy and NULL-terminate starname */
00234         star_arr[i] = cpl_malloc(1+ilen);
00235         memcpy(star_arr[i], fstart, ilen);
00236         star_arr[i][ilen] = '\0';
00237 
00238         /* Pointers to the wavelengths and fluxes of one star */
00239         wlen = cpl_vector_unwrap(cpl_bivector_get_x(spectrum));
00240         flux = cpl_vector_unwrap(cpl_bivector_get_y(spectrum));
00241 
00242         cpl_bivector_unwrap_vectors(spectrum);
00243         spectrum = NULL;
00244 
00245         wlen_arr[i] = cpl_array_wrap_double(wlen, nvals);
00246         flux_arr[i] = cpl_array_wrap_double(flux, nvals);
00247 
00248         bug_if (cpl_table_set_double(catalog, "RA",  i, ra));
00249         bug_if (cpl_table_set_double(catalog, "DEC", i, dec));
00250 
00251     }
00252 
00253     skip_if (i != nfiles);
00254 
00255     ras  = cpl_table_get_data_double(catalog, "RA");
00256     decs = cpl_table_get_data_double(catalog, "DEC");
00257 
00258     mindist = visir_star_dist_min(ras, decs, nfiles, &iloc1, &iloc2);
00259 
00260     if (mindist < max_radius)
00261         cpl_msg_warning(cpl_func, "The pair of closest stars is %s (%d) and %s "
00262                         "(%d) with the distance: %g",
00263                         cpl_frame_get_filename(cpl_frameset_get_frame(framelist,
00264                                                                  iloc1)), iloc1,
00265                         cpl_frame_get_filename(cpl_frameset_get_frame(framelist,
00266                                                                  iloc2)), iloc2,
00267                         mindist);
00268 
00269 
00270     else
00271         cpl_msg_info(cpl_func, "The pair of closest stars is %s (%d) and %s "
00272                      "(%d) with the distance: %g",
00273                      cpl_frame_get_filename(cpl_frameset_get_frame(framelist,
00274                                                                  iloc1)), iloc1,
00275                      cpl_frame_get_filename(cpl_frameset_get_frame(framelist,
00276                                                                  iloc2)), iloc2,
00277                      mindist);
00278     
00279     /* Save the table */
00280     cpl_msg_info(cpl_func, "Saving the table with %d rows each with a spectrum "
00281                  "length of %d", nfiles, nvals);
00282     skip_if (visir_util_spc_std_cat_save(framelist, parlist, catalog));
00283 
00284     end_skip;
00285 
00286     cpl_bivector_delete(spectrum);
00287     cpl_table_delete(catalog);
00288     if (in) fclose(in);
00289 
00290     return cpl_error_get_code();
00291 }
00292 
00293 /*----------------------------------------------------------------------------*/
00304 /*----------------------------------------------------------------------------*/
00305 static
00306 cpl_error_code visir_util_spc_std_cat_save(cpl_frameset            * set,
00307                                            const cpl_parameterlist * parlist,
00308                                            const cpl_table         * catalog)
00309 {
00310     cpl_propertylist * applist = cpl_propertylist_new();
00311 
00312 
00313     bug_if (catalog == NULL);
00314     bug_if (parlist == NULL);
00315     bug_if (set     == NULL);
00316 
00317     bug_if(cpl_propertylist_append_string(applist, "INSTRUME", "VISIR"));
00318 
00319     skip_if (irplib_dfs_save_table(set, parlist, set, catalog, NULL,
00320                                RECIPE_STRING, VISIR_SPEC_STD_CAT_PROCATG,
00321                                applist, NULL, visir_pipe_id,
00322                                RECIPE_STRING CPL_DFS_FITS));
00323     
00324     end_skip;
00325 
00326     cpl_propertylist_delete(applist);
00327 
00328     return cpl_error_get_code();
00329 }

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