rl_raysuplib  1.0.13
rl_DielectricPOD_rdb.cc
1 // File: rl_dielectricpod_rdb.cc
2 // Author: Terry Gaetz
3 
4 // --8<--8<--8<--8<--
5 //
6 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
7 //
8 // This file is part of rl_ray
9 //
10 // rl_ray is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // as published by the Free Software Foundation; either version 2
13 // of the License, or (at your option) any later version.
14 //
15 // rl_ray is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the
22 // Free Software Foundation, Inc.
23 // 51 Franklin Street, Fifth Floor
24 // Boston, MA 02110-1301, USA
25 //
26 // -->8-->8-->8-->8--
27 
28 // Description: read in dielectric constant information from rdb table;
29 // organize into bsearchable array
30 //
31 
32 #include <rl_raysuplib/rl_DielectricPOD_rdb.h>
33 #include <rl_raylib/rl_DielectricPODArray.h>
34 
35 //#include <rl_Traits.h>
36 
37 #include <cstdio> // fopen
38 #include <cstring>
39 #include <cmath>
40 
41 #include <tracefctxx/TraceFct.h>
42 #include <tracefct/exiterrvals.h>
43 #include <mst_rdb/mst_rdb.h>
44 #include <suplib/str.h>
45 
46 using namespace std;
47 
48 #define NUM_DATA_ITEMS 3
49 /* enum EColname { Eenergy, Ealpha, Egamma }; */
50 /* static char *colname[] = { "energy", "alpha", "gamma" }; */
51 
52 typedef enum
53 {
54  Ealpha_gamma
55 } ERfldataType;
56 
57 static TokListToken
58 dieldata_type[] =
59 {
60  { "alpha_gamma", Ealpha_gamma },
61 };
62 TokList dieldata_types = GenTokList( dieldata_type );
63 
64 //-------------------------------------------------------------------------
65 // dtor, ctors...
66 
69 {}
70 
72 rl_DielectricPOD_rdb( char const rdb_file[] )
73  : rl_DielectricPODArray()
74 { if ( strlen(rdb_file) != 0 ) { init( rdb_file ); } }
75 
77 init( char const rdb_filename_in[] )
78 {
79  TraceFct tf( "rl_DielectricPOD_rdb::init" );
80  typedef struct rfl_record
81  {
82  double energy;
83  double alpha;
84  double gamma;
85  } rfl_record;
86  rfl_record record;
87  RDBFieldStInfo fields[] =
88  {
89  RDBentry(energy, RDB_Num, rfl_record),
90  RDBentry(alpha, RDB_Num, rfl_record),
91  RDBentry(gamma, RDB_Num, rfl_record)
92  };
93  #define NFIELDS (sizeof(fields) / sizeof(RDBFieldStInfo) )
94 
95  /*
96  * open file containing reflectance data
97  */
98 
99  int error;
100  char *rdb_filename =
101  str_interp( rdb_filename_in, 1, NULL, NULL, &error );
102 
103  if ( error )
104  tf.die( "error interpolating filename: %s", rdb_filename_in );
105 
106  FILE* in = fopen(rdb_filename, "r");
107  if (! in )
108  tf.exit(ExitERR_fopen, "unable to open input `%s'", rdb_filename);
109 
110  rdbHeader* hdr = rdb_rd_hdr(in);
111  if (! hdr )
112  tf.die( "rdb file `%s' contains no rdb headers!", rdb_filename );
113 
114  DataColumnMap_st* map = rdb_map_cols_stst(hdr, NFIELDS, fields);
115 
116  /*
117  * count number of rows (excluding header & comments) in rdb file
118  */
119  nelts_ = rdb_count(in, hdr);
120  if ( nelts_ < 0 )
121  tf.die(
122  "reflectance rdb file '%s' is not rewindable", rdb_filename);
123 
124  else if ( nelts_ < 2 )
125  tf.die(
126  "reflectance rdb file '%s' must have at least two rows", rdb_filename);
127 
128 
129  /*
130  * allocate arrays to hold data
131  */
132  double* energy = new double[nelts_];
133  double* alpha = new double[nelts_];
134  double* gamma = new double[nelts_];
135 
136  data_ = new rl_Traits::rl_DielectricPOD[ nelts_ ];
137  if (! data_ )
138  tf.exit(ExitERR_alloc, "unable to malloc rl_DielectricPOD array");
139 
140 
141  /*
142  * read reflectance data into rl_DielectricPOD array
143  */
144  int n;
145  for ( n = 0 ; n < nelts_ ; ++n )
146  {
147  rdb_col_read_st(in, hdr, map, &record);
148  energy[n] = record.energy;
149  alpha[n] = record.alpha;
150  gamma[n] = record.gamma;
151  }
152 
153  rl_DielectricPODArray::init( nelts_, energy, alpha, gamma );
154 
155  /*
156  * clean up
157  */
158  delete [] energy;
159  delete [] alpha;
160  delete [] gamma;
161 
162  free( rdb_filename );
163  rdb_free_map(map); /* free up data column map */
164  rdb_free_hdr(hdr); /* free up rdbHeader dynamic allocation */
165  fclose(in);
166 }
~rl_DielectricPOD_rdb()
Destructor.
void init(char const rdb_file[])
Initializer.
rl_DielectricPOD_rdb(char const rdb_file[]="")
Constructor.