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 "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 
00047 
00048 
00049 #define RECIPE_NAME "isaac_util_seds"
00050 
00051 
00052 
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 
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     
00116     skip_if (isaac_dfs_set_groups(framelist));
00117 
00118     
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     
00128     for (i = 0; i < nframes; i++) {
00129         const cpl_frame * rawframe = cpl_frameset_get_frame_const(rawframes, i);
00130 
00131         
00132         char name[4];
00133         const char * rawfile = cpl_frame_get_filename(rawframe);
00134         
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         
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             
00155             out = cpl_table_new(nlines);
00156             
00157             bug_if(cpl_table_wrap_double(out, cpl_vector_get_data(sedsx),
00158                                          "Wavelength"));
00159             sedsx1 = sedsx;
00160             sedsx = NULL;
00161         } else {
00162             
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             
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         
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     
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     
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