rl_DielectricPOD_rdb.cc

00001 // File:   rl_dielectricpod_rdb.cc
00002 // Author: Terry Gaetz
00003 
00004 // --8<--8<--8<--8<--
00005 //
00006 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00007 //
00008 // This file is part of rl_ray
00009 //
00010 // rl_ray is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public License
00012 // as published by the Free Software Foundation; either version 2
00013 // of the License, or (at your option) any later version.
00014 //
00015 // rl_ray is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public License
00021 // along with this program; if not, write to the 
00022 //       Free Software Foundation, Inc. 
00023 //       51 Franklin Street, Fifth Floor
00024 //       Boston, MA  02110-1301, USA
00025 //
00026 // -->8-->8-->8-->8--
00027 
00028 // Description: read in dielectric constant information from rdb table; 
00029 //              organize into bsearchable array
00030 //
00031 
00032 #include <rl_raysuplib/rl_DielectricPOD_rdb.h>
00033 #include <rl_raylib/rl_DielectricPODArray.h>
00034 
00035 //#include <rl_Traits.h>
00036 
00037 #include <cstdio>                     // fopen
00038 #include <cstring>
00039 #include <cmath>
00040 
00041 #include <tracefctxx/TraceFct.h>
00042 #include <tracefct/exiterrvals.h>
00043 #include <mst_rdb/mst_rdb.h>
00044 #include <suplib/str.h>
00045 
00046 using namespace std;
00047 
00048 
00049 #define NUM_DATA_ITEMS 3
00050 /* enum        EColname     { Eenergy,  Ealpha,  Egamma  }; */
00051 /* static char *colname[] = { "energy", "alpha", "gamma" }; */
00052 
00053 typedef enum
00054 {
00055   Ealpha_gamma
00056 } ERfldataType;
00057 
00058 static TokListToken
00059 dieldata_type[] =
00060 {
00061   { "alpha_gamma",  Ealpha_gamma },
00062 };
00063 TokList dieldata_types = GenTokList( dieldata_type );
00064 
00065 //-------------------------------------------------------------------------
00066 // dtor, ctors...
00067 
00068 rl_DielectricPOD_rdb::
00069 ~rl_DielectricPOD_rdb()
00070 {}
00071 
00072 rl_DielectricPOD_rdb::
00073 rl_DielectricPOD_rdb( char const rdb_file[] )
00074   : rl_DielectricPODArray()
00075 { if ( strlen(rdb_file) != 0 ) { init( rdb_file ); } }
00076 
00077 void rl_DielectricPOD_rdb::
00078 init( char const rdb_filename_in[] )
00079 {
00080   TraceFct tf( "rl_DielectricPOD_rdb::init" );
00081   typedef struct rfl_record
00082   {
00083     double energy;
00084     double alpha;
00085     double gamma;
00086   } rfl_record;
00087   rfl_record record;
00088   RDBFieldStInfo fields[] =
00089   {
00090     RDBentry(energy, RDB_Num, rfl_record),
00091     RDBentry(alpha,  RDB_Num, rfl_record),
00092     RDBentry(gamma,  RDB_Num, rfl_record)
00093   };
00094   #define NFIELDS (sizeof(fields) / sizeof(RDBFieldStInfo) )
00095 
00096   /*
00097    * open file containing reflectance data
00098    */
00099 
00100   int error;
00101   char *rdb_filename =
00102     str_interp( rdb_filename_in, 1, NULL, NULL, &error );
00103 
00104   if ( error )
00105     tf.die( "error interpolating filename: %s", rdb_filename_in );
00106 
00107   FILE* in = fopen(rdb_filename, "r");
00108   if (! in )
00109     tf.exit(ExitERR_fopen, "unable to open input `%s'", rdb_filename);
00110 
00111   rdbHeader* hdr = rdb_rd_hdr(in);  
00112   if (! hdr )
00113     tf.die( "rdb file `%s' contains no rdb headers!", rdb_filename );
00114 
00115   DataColumnMap_st* map = rdb_map_cols_stst(hdr, NFIELDS, fields);
00116 
00117   /*
00118    * count number of rows (excluding header & comments) in rdb file
00119    */
00120   nelts_ = rdb_count(in, hdr);
00121   if ( nelts_ < 0 )
00122     tf.die(
00123        "reflectance rdb file '%s' is not rewindable", rdb_filename);
00124 
00125   else if ( nelts_ < 2 )
00126     tf.die(
00127        "reflectance rdb file '%s' must have at least two rows", rdb_filename);
00128 
00129 
00130   /*
00131    * allocate arrays to hold data
00132    */
00133   double* energy = new double[nelts_];
00134   double* alpha  = new double[nelts_];
00135   double* gamma  = new double[nelts_];
00136 
00137   data_ = new rl_Traits::rl_DielectricPOD[ nelts_ ];
00138   if (! data_ )
00139     tf.exit(ExitERR_alloc, "unable to malloc rl_DielectricPOD array");
00140 
00141 
00142   /*
00143    * read reflectance data into rl_DielectricPOD array
00144    */
00145   int   n;
00146   for ( n = 0 ; n < nelts_ ; ++n )
00147   {
00148     rdb_col_read_st(in, hdr, map, &record);
00149     energy[n] = record.energy;
00150     alpha[n]  = record.alpha;
00151     gamma[n]  = record.gamma;
00152   }
00153 
00154   rl_DielectricPODArray::init( nelts_, energy, alpha, gamma );
00155 
00156   /* 
00157    * clean up
00158    */
00159   delete [] energy;
00160   delete [] alpha;
00161   delete [] gamma;
00162 
00163   free( rdb_filename );
00164   rdb_free_map(map);       /* free up data column map */
00165   rdb_free_hdr(hdr);       /* free up rdbHeader dynamic allocation */
00166   fclose(in);
00167 }

Generated on Mon Dec 15 11:15:34 2008 for rl_raysuplib by  doxygen 1.5.6