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 #define NUM_DATA_ITEMS 3
00049 /* enum        EColname     { Eenergy,  Ealpha,  Egamma  }; */
00050 /* static char *colname[] = { "energy", "alpha", "gamma" }; */
00051 
00052 typedef enum
00053 {
00054   Ealpha_gamma
00055 } ERfldataType;
00056 
00057 static TokListToken
00058 dieldata_type[] =
00059 {
00060   { "alpha_gamma",  Ealpha_gamma },
00061 };
00062 TokList dieldata_types = GenTokList( dieldata_type );
00063 
00064 //-------------------------------------------------------------------------
00065 // dtor, ctors...
00066 
00067 rl_DielectricPOD_rdb::
00068 ~rl_DielectricPOD_rdb()
00069 {}
00070 
00071 rl_DielectricPOD_rdb::
00072 rl_DielectricPOD_rdb( char const rdb_file[] )
00073   : rl_DielectricPODArray()
00074 { if ( strlen(rdb_file) != 0 ) { init( rdb_file ); } }
00075 
00076 void rl_DielectricPOD_rdb::
00077 init( char const rdb_filename_in[] )
00078 {
00079   TraceFct tf( "rl_DielectricPOD_rdb::init" );
00080   typedef struct rfl_record
00081   {
00082     double energy;
00083     double alpha;
00084     double gamma;
00085   } rfl_record;
00086   rfl_record record;
00087   RDBFieldStInfo fields[] =
00088   {
00089     RDBentry(energy, RDB_Num, rfl_record),
00090     RDBentry(alpha,  RDB_Num, rfl_record),
00091     RDBentry(gamma,  RDB_Num, rfl_record)
00092   };
00093   #define NFIELDS (sizeof(fields) / sizeof(RDBFieldStInfo) )
00094 
00095   /*
00096    * open file containing reflectance data
00097    */
00098 
00099   int error;
00100   char *rdb_filename =
00101     str_interp( rdb_filename_in, 1, NULL, NULL, &error );
00102 
00103   if ( error )
00104     tf.die( "error interpolating filename: %s", rdb_filename_in );
00105 
00106   FILE* in = fopen(rdb_filename, "r");
00107   if (! in )
00108     tf.exit(ExitERR_fopen, "unable to open input `%s'", rdb_filename);
00109 
00110   rdbHeader* hdr = rdb_rd_hdr(in);  
00111   if (! hdr )
00112     tf.die( "rdb file `%s' contains no rdb headers!", rdb_filename );
00113 
00114   DataColumnMap_st* map = rdb_map_cols_stst(hdr, NFIELDS, fields);
00115 
00116   /*
00117    * count number of rows (excluding header & comments) in rdb file
00118    */
00119   nelts_ = rdb_count(in, hdr);
00120   if ( nelts_ < 0 )
00121     tf.die(
00122        "reflectance rdb file '%s' is not rewindable", rdb_filename);
00123 
00124   else if ( nelts_ < 2 )
00125     tf.die(
00126        "reflectance rdb file '%s' must have at least two rows", rdb_filename);
00127 
00128 
00129   /*
00130    * allocate arrays to hold data
00131    */
00132   double* energy = new double[nelts_];
00133   double* alpha  = new double[nelts_];
00134   double* gamma  = new double[nelts_];
00135 
00136   data_ = new rl_Traits::rl_DielectricPOD[ nelts_ ];
00137   if (! data_ )
00138     tf.exit(ExitERR_alloc, "unable to malloc rl_DielectricPOD array");
00139 
00140 
00141   /*
00142    * read reflectance data into rl_DielectricPOD array
00143    */
00144   int   n;
00145   for ( n = 0 ; n < nelts_ ; ++n )
00146   {
00147     rdb_col_read_st(in, hdr, map, &record);
00148     energy[n] = record.energy;
00149     alpha[n]  = record.alpha;
00150     gamma[n]  = record.gamma;
00151   }
00152 
00153   rl_DielectricPODArray::init( nelts_, energy, alpha, gamma );
00154 
00155   /* 
00156    * clean up
00157    */
00158   delete [] energy;
00159   delete [] alpha;
00160   delete [] gamma;
00161 
00162   free( rdb_filename );
00163   rdb_free_map(map);       /* free up data column map */
00164   rdb_free_hdr(hdr);       /* free up rdbHeader dynamic allocation */
00165   fclose(in);
00166 }