isaac_util_seds.c

00001 /* $Id: isaac_util_seds.c,v 1.14 2011/07/11 13:39:50 llundin Exp $
00002  *
00003  * This file is part of the ISAAC 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2011/07/11 13:39:50 $
00024  * $Revision: 1.14 $
00025  * $Name: isaac-6_1_3 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include "irplib_utils.h"
00037 
00038 #include "isaac_utils.h"
00039 #include "isaac_pfits.h"
00040 #include "isaac_dfs.h"
00041 
00042 #include <string.h>
00043 #include <math.h>
00044 
00045 /*-----------------------------------------------------------------------------
00046                                 Defines
00047  -----------------------------------------------------------------------------*/
00048 
00049 #define RECIPE_NAME "isaac_util_seds"
00050 
00051 /*-----------------------------------------------------------------------------
00052                             Functions prototypes
00053  -----------------------------------------------------------------------------*/
00054 
00055 cpl_recipe_define(isaac_util_seds, ISAAC_BINARY_VERSION,
00056                   "Lars Lundin", PACKAGE_BUGREPORT, "2008", 
00057                   "ISAAC SEDS table creation",
00058                   RECIPE_NAME " -- ISAAC SEDS table creation.\n"
00059                   "The files listed in the Set Of Frames (sof-file) " 
00060                   "must be tagged:\n"
00061                   "ASCII-SEDS.txt " ISAAC_UTIL_SEDS_RAW "\n"
00062                   "The first three characters of the input file names must be "
00063                   "unique. Lines starting with a '#' are ignored. The other "
00064                   "lines must have (at least) two columns, the first with a "
00065                   "wavelength, the second with an SED. All files must have "
00066                   "an identical number of non-comment rows and all files are "
00067                   "assumed to contain identical sequences of wavelengths.\n");
00068 
00069 static
00070 cpl_error_code isaac_util_seds_save(cpl_frameset *, const cpl_table *,
00071                                     const cpl_parameterlist *);
00072 
00073 /*-----------------------------------------------------------------------------
00074                                 Functions code
00075  -----------------------------------------------------------------------------*/
00076 
00077 /*----------------------------------------------------------------------------*/
00085 /*----------------------------------------------------------------------------*/
00086 static
00087 cpl_error_code isaac_util_seds_fill_parameterlist(cpl_parameterlist * self) {
00088 
00089     return self != NULL ? CPL_ERROR_NONE
00090         : cpl_error_set(cpl_func, CPL_ERROR_NULL_INPUT);
00091 }
00092 
00093 /*----------------------------------------------------------------------------*/
00101 /*----------------------------------------------------------------------------*/
00102 static int isaac_util_seds(cpl_frameset            * framelist,
00103                            const cpl_parameterlist * parlist)
00104 {
00105     cpl_frameset * rawframes = NULL;
00106     cpl_table    * out       = NULL;
00107     cpl_bivector * seds      = NULL;
00108     cpl_vector   * sedsx     = NULL;
00109     cpl_vector   * sedsx1    = NULL;
00110     cpl_vector   * sedsy     = NULL;
00111     int            nframes   = 0;
00112     int            nlines    = 0;
00113     int            i;
00114     
00115     /* Identify the RAW and CALIB frames in the input frameset */
00116     skip_if (isaac_dfs_set_groups(framelist));
00117 
00118     /* Retrieve raw frames */
00119     rawframes = isaac_extract_frameset(framelist, ISAAC_UTIL_SEDS_RAW);
00120     irplib_ensure(rawframes != NULL, CPL_ERROR_DATA_NOT_FOUND,
00121                   "Could not find raw frames in the input list");
00122 
00123     nframes = cpl_frameset_get_size(rawframes);
00124     irplib_ensure(nframes > 0, CPL_ERROR_DATA_NOT_FOUND,
00125                   "Could not find raw frames in the input list");
00126  
00127     /* Loop over ASCII files */
00128     for (i = 0; i < nframes; i++) {
00129         const cpl_frame * rawframe = cpl_frameset_get_frame_const(rawframes, i);
00130 
00131         /* Create the 3-letter label name from the catalogue name */
00132         char name[4];
00133         const char * rawfile = cpl_frame_get_filename(rawframe);
00134         /* Skip path, if any */
00135         const char * cat_name = strrchr(rawfile, '/');
00136 
00137         cat_name = cat_name ? 1+cat_name : rawfile;
00138         bug_if(cat_name == NULL);
00139         (void)strncpy(name, cat_name, 3);
00140         name[3] = '\0';
00141 
00142         /* Load ASCII file */
00143         seds = cpl_bivector_read(cpl_frame_get_filename(rawframe));
00144         irplib_ensure(seds != NULL, cpl_error_get_code(),
00145                       "Could not load SED no. %d of %d", i+1, nframes);
00146         sedsx = cpl_bivector_get_x(seds);
00147         sedsy = cpl_bivector_get_y(seds);
00148         cpl_bivector_unwrap_vectors(seds);
00149         seds = NULL;
00150         
00151         if (i == 0) {
00152             nlines = cpl_vector_get_size(sedsx);
00153 
00154             /* Create the table */
00155             out = cpl_table_new(nlines);
00156             /* - and the wavelength column */
00157             bug_if(cpl_table_wrap_double(out, cpl_vector_get_data(sedsx),
00158                                          "Wavelength"));
00159             sedsx1 = sedsx;
00160             sedsx = NULL;
00161         } else {
00162             /* Verify the consistency of length */
00163             const int ilines = cpl_vector_get_size(sedsx);
00164             double dmin, dmax;
00165 
00166             irplib_ensure(ilines == nlines, CPL_ERROR_INCOMPATIBLE_INPUT,
00167                           "Number of lines in SED no. %d and %d differ: "
00168                           "%d <=> %d", 1, i+1, nlines, ilines);
00169 
00170             /* FIXME: Support different wavelengths ? */
00171             bug_if(cpl_vector_subtract(sedsx, sedsx1));
00172 
00173             dmin = cpl_vector_get_min(sedsx);
00174             dmax = cpl_vector_get_max(sedsx);
00175 
00176             if (dmin != 0.0 || dmax != 0.0) {
00177                 cpl_msg_warning(cpl_func, "Wavelengths in SED no. %d differ "
00178                                 "from those in no. 1 (min, max): %g %g",
00179                                 i+1, dmin, dmax);
00180             }
00181 
00182             cpl_vector_delete(sedsx);
00183             sedsx = NULL;
00184         }
00185 
00186         /* Create the column */
00187         skip_if(cpl_table_wrap_double(out, cpl_vector_get_data(sedsy), name));
00188         (void)cpl_vector_unwrap(sedsy);
00189         sedsy = NULL;
00190     }
00191 
00192     /* Save the table */
00193     skip_if (isaac_util_seds_save(framelist, out, parlist));
00194 
00195     end_skip;
00196     
00197     cpl_bivector_unwrap_vectors(seds);
00198     (void)cpl_vector_unwrap(sedsx1);
00199     cpl_vector_delete(sedsx);
00200     cpl_vector_delete(sedsy);
00201     cpl_frameset_delete(rawframes);
00202     cpl_table_delete(out);
00203 
00204     return cpl_error_get_code();
00205 }
00206 
00207 /*----------------------------------------------------------------------------*/
00216 /*----------------------------------------------------------------------------*/
00217 static
00218 cpl_error_code isaac_util_seds_save(cpl_frameset            * set,
00219                                     const cpl_table         * sed,
00220                                     const cpl_parameterlist * parlist)
00221 {
00222     cpl_propertylist * plist = cpl_propertylist_new();
00223 
00224     bug_if(cpl_propertylist_append_string(plist, "INSTRUME", "ISAAC"));
00225  
00226     /* Write the table */
00227     skip_if(irplib_dfs_save_table(set,
00228                                   parlist,
00229                                   set,
00230                                   sed,
00231                                   NULL,
00232                                   RECIPE_NAME,
00233                                   ISAAC_UTIL_SEDS_RES,
00234                                   plist,
00235                                   NULL,
00236                                   PACKAGE "/" PACKAGE_VERSION,
00237                                   RECIPE_NAME CPL_DFS_FITS));
00238 
00239     end_skip;
00240 
00241     cpl_propertylist_delete(plist);
00242 
00243     return cpl_error_get_code();
00244 }
00245 
00246 

Generated on Wed Mar 6 08:18:22 2013 for ISAAC Pipeline Reference Manual by  doxygen 1.5.8