OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
CSVDDS.cc
Go to the documentation of this file.
1 // CSVDDS.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Stephan Zednik <zednik@ucar.edu> and Patrick West <pwest@ucar.edu>
8 // and Jose Garcia <jgarcia@ucar.edu>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library 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 GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact University Corporation for Atmospheric Research at
25 // 3080 Center Green Drive, Boulder, CO 80301
26 
27 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
28 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
29 //
30 // Authors:
31 // zednik Stephan Zednik <zednik@ucar.edu>
32 // pwest Patrick West <pwest@ucar.edu>
33 // jgarcia Jose Garcia <jgarcia@ucar.edu>
34 
35 #include <vector>
36 #include <string>
37 
38 #include "CSVDDS.h"
39 #include "CSV_Obj.h"
40 
41 #include <BESInternalError.h>
42 #include <BESNotFoundError.h>
43 #include <BaseTypeFactory.h>
44 #include <DDS.h>
45 #include <Error.h>
46 
47 #include <Str.h>
48 #include <Int16.h>
49 #include <Int32.h>
50 #include <Float32.h>
51 #include <Float64.h>
52 #include <mime_util.h>
53 
54 #include <Array.h>
55 
56 
57 #include <BESDebug.h>
58 
59 void
60 csv_read_descriptors( DDS &dds, const string &filename )
61 {
62  string type ;
63  int index = 0 ;
64 
65  Array* ar = 0 ;
66  void* data = 0 ;
67  BaseType *bt = 0 ;
68 
69  CSV_Obj* csvObj = new CSV_Obj();
70  if( !csvObj->open( filename ) )
71  {
72  delete csvObj;
73  string err = (string)"Unable to open file " + filename ;
74  throw BESNotFoundError( err, __FILE__, __LINE__ ) ;
75  }
76  csvObj->load() ;
77 
78  BESDEBUG( "csv", "File loaded:" << endl << *csvObj << endl ) ;
79 
80  dds.set_dataset_name( name_path( filename ) ) ;
81 
82  vector<string> fieldList;
83  csvObj->getFieldList( fieldList ) ;
84  int recordCount = csvObj->getRecordCount();
85 
86  vector<string>::iterator it = fieldList.begin() ;
87  vector<string>::iterator et = fieldList.end() ;
88  for( ; it != et; it++ )
89  {
90  string fieldName = (*it) ;
91  type = csvObj->getFieldType( fieldName ) ;
92  ar = dds.get_factory()->NewArray( fieldName ) ;
93  data = csvObj->getFieldData( fieldName ) ;
94 
95  if( type.compare( string( STRING ) ) == 0 )
96  {
97  string* strings = new string[recordCount] ;
98 
99  bt = dds.get_factory()->NewStr( fieldName ) ;
100  ar->add_var( bt ) ;
101  ar->append_dim( recordCount, "record" ) ;
102 
103  index = 0 ;
104  vector<string>::iterator iv = ((vector<string>*)data)->begin() ;
105  vector<string>::iterator ev = ((vector<string>*)data)->end() ;
106  for( ; iv != ev; iv++)
107  {
108  strings[index] = *iv ;
109  index++ ;
110  }
111 
112  ar->set_value( strings, recordCount ) ;
113  delete [] strings ;
114  }
115  else if( type.compare( string( INT16 ) ) == 0 )
116  {
117  short* int16 = new short[recordCount] ;
118  bt = dds.get_factory()->NewInt16( fieldName ) ;
119  ar->add_var( bt ) ;
120  ar->append_dim( recordCount, "record" ) ;
121 
122  index = 0 ;
123  vector<short>::iterator iv = ((vector<short>*)data)->begin() ;
124  vector<short>::iterator ev = ((vector<short>*)data)->end() ;
125  for( ; iv != ev; iv++)
126  {
127  int16[index] = *iv ;
128  index++ ;
129  }
130 
131  ar->set_value( int16, recordCount ) ;
132  delete [] int16 ;
133  }
134  else if( type.compare( string( INT32 ) ) == 0 )
135  {
136  int *int32 = new int[recordCount] ;
137  bt = dds.get_factory()->NewInt32( fieldName ) ;
138  ar->add_var( bt ) ;
139  ar->append_dim( recordCount, "record" ) ;
140 
141  index = 0 ;
142  vector<int>::iterator iv = ((vector<int>*)data)->begin() ;
143  vector<int>::iterator ev = ((vector<int>*)data)->end() ;
144  for( ; iv != ev; iv++)
145  {
146  int32[index] = *iv ;
147  index++ ;
148  }
149 
150  ar->set_value( (dods_int32*)int32, recordCount ) ;
151  delete [] int32 ;
152  }
153  else if( type.compare( string( FLOAT32 ) ) == 0 )
154  {
155  float *floats = new float[recordCount] ;
156  bt = dds.get_factory()->NewFloat32( fieldName ) ;
157  ar->add_var( bt ) ;
158  ar->append_dim( recordCount, "record" ) ;
159 
160  index = 0 ;
161  vector<float>::iterator iv = ((vector<float>*)data)->begin() ;
162  vector<float>::iterator ev = ((vector<float>*)data)->end() ;
163  for( ; iv != ev; iv++)
164  {
165  floats[index] = *iv ;
166  index++ ;
167  }
168 
169  ar->set_value( floats, recordCount ) ;
170  delete [] floats ;
171  }
172  else if( type.compare( string( FLOAT64 ) ) == 0 )
173  {
174  double *doubles = new double[recordCount] ;
175  bt = dds.get_factory()->NewFloat64( fieldName ) ;
176  ar->add_var( bt ) ;
177  ar->append_dim( recordCount, "record" ) ;
178 
179  index = 0 ;
180  vector<double>::iterator iv = ((vector<double>*)data)->begin() ;
181  vector<double>::iterator ev = ((vector<double>*)data)->end() ;
182  for( ; iv != ev; iv++)
183  {
184  doubles[index] = *iv;
185  index++ ;
186  }
187 
188  ar->set_value( doubles, recordCount ) ;
189  delete [] doubles ;
190  }
191  else
192  {
193  delete csvObj;
194  string err = (string)"Unknown type for field " + fieldName ;
195  throw BESInternalError( err, __FILE__, __LINE__ ) ;
196  }
197 
198  dds.add_var( ar ) ;
199 
200  //if( ar ) {
201  delete ar ; ar = 0 ; // }
202  //if( bt ) {
203  delete bt ; bt = 0 ; //}
204  }
205 
206  delete csvObj ;
207  csvObj = 0 ;
208 }
209 
error thrown if the resource requested cannot be found
exception thrown if inernal error encountered
void * getFieldData(const string &field)
Definition: CSV_Obj.cc:158
bool open(const string &filepath)
Definition: CSV_Obj.cc:79
void csv_read_descriptors(DDS &dds, const string &filename)
Definition: CSVDDS.cc:60
int getRecordCount()
Definition: CSV_Obj.cc:132
void load()
Definition: CSV_Obj.cc:84
string getFieldType(const string &fieldName)
Definition: CSV_Obj.cc:127
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
void getFieldList(vector< string > &list)
Definition: CSV_Obj.cc:122