bes  Updated for version 3.20.6
CSVDDS.cc
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 #include <BESDebug.h>
57 
58 void csv_read_descriptors(DDS &dds, const string &filename)
59 {
60  string type;
61  int index = 0;
62 
63  Array* ar = 0;
64  void* data = 0;
65  BaseType *bt = 0;
66 
67  CSV_Obj* csvObj = new CSV_Obj();
68  if (!csvObj->open(filename)) {
69  delete csvObj;
70  string err = (string) "Unable to open file " + filename;
71  throw BESNotFoundError(err, __FILE__, __LINE__);
72  }
73  csvObj->load();
74 
75  BESDEBUG( "csv", "File loaded:" << endl << *csvObj << endl );
76 
77  dds.set_dataset_name(name_path(filename));
78 
79  vector<string> fieldList;
80  csvObj->getFieldList(fieldList);
81  int recordCount = csvObj->getRecordCount();
82  if (recordCount < 0)
83  throw BESError("Could not read record count from the CSV dataset.", BES_NOT_FOUND_ERROR, __FILE__, __LINE__);
84 
85  vector<string>::iterator it = fieldList.begin();
86  vector<string>::iterator et = fieldList.end();
87  for (; it != et; it++) {
88  string fieldName = (*it);
89  type = csvObj->getFieldType(fieldName);
90  ar = dds.get_factory()->NewArray(fieldName);
91  data = csvObj->getFieldData(fieldName);
92 
93  if (type.compare(string(STRING)) == 0) {
94  string* strings = new string[recordCount];
95 
96  bt = dds.get_factory()->NewStr(fieldName);
97  ar->add_var(bt);
98  ar->append_dim(recordCount, "record");
99 
100  index = 0;
101  vector<string>::iterator iv = ((vector<string>*) data)->begin();
102  vector<string>::iterator ev = ((vector<string>*) data)->end();
103  for (; iv != ev; iv++) {
104  strings[index] = *iv;
105  index++;
106  }
107 
108  ar->set_value(strings, recordCount);
109  delete[] strings;
110  }
111  else if (type.compare(string(INT16)) == 0) {
112  short* int16 = new short[recordCount];
113  bt = dds.get_factory()->NewInt16(fieldName);
114  ar->add_var(bt);
115  ar->append_dim(recordCount, "record");
116 
117  index = 0;
118  vector<short>::iterator iv = ((vector<short>*) data)->begin();
119  vector<short>::iterator ev = ((vector<short>*) data)->end();
120  for (; iv != ev; iv++) {
121  int16[index] = *iv;
122  index++;
123  }
124 
125  ar->set_value(int16, recordCount);
126  delete[] int16;
127  }
128  else if (type.compare(string(INT32)) == 0) {
129  int *int32 = new int[recordCount];
130  bt = dds.get_factory()->NewInt32(fieldName);
131  ar->add_var(bt);
132  ar->append_dim(recordCount, "record");
133 
134  index = 0;
135  vector<int>::iterator iv = ((vector<int>*) data)->begin();
136  vector<int>::iterator ev = ((vector<int>*) data)->end();
137  for (; iv != ev; iv++) {
138  int32[index] = *iv;
139  index++;
140  }
141 
142  ar->set_value((dods_int32*) int32, recordCount);
143  delete[] int32;
144  }
145  else if (type.compare(string(FLOAT32)) == 0) {
146  float *floats = new float[recordCount];
147  bt = dds.get_factory()->NewFloat32(fieldName);
148  ar->add_var(bt);
149  ar->append_dim(recordCount, "record");
150 
151  index = 0;
152  vector<float>::iterator iv = ((vector<float>*) data)->begin();
153  vector<float>::iterator ev = ((vector<float>*) data)->end();
154  for (; iv != ev; iv++) {
155  floats[index] = *iv;
156  index++;
157  }
158 
159  ar->set_value(floats, recordCount);
160  delete[] floats;
161  }
162  else if (type.compare(string(FLOAT64)) == 0) {
163  double *doubles = new double[recordCount];
164  bt = dds.get_factory()->NewFloat64(fieldName);
165  ar->add_var(bt);
166  ar->append_dim(recordCount, "record");
167 
168  index = 0;
169  vector<double>::iterator iv = ((vector<double>*) data)->begin();
170  vector<double>::iterator ev = ((vector<double>*) data)->end();
171  for (; iv != ev; iv++) {
172  doubles[index] = *iv;
173  index++;
174  }
175 
176  ar->set_value(doubles, recordCount);
177  delete[] doubles;
178  }
179  else {
180  delete csvObj;
181  string err = (string) "Unknown type for field " + fieldName;
182  throw BESInternalError(err, __FILE__, __LINE__);
183  }
184 
185  dds.add_var(ar);
186 
187  //if( ar ) {
188  delete ar;
189  ar = 0; // }
190  //if( bt ) {
191  delete bt;
192  bt = 0; //}
193  }
194 
195  delete csvObj;
196  csvObj = 0;
197 }
198 
BESNotFoundError
error thrown if the resource requested cannot be found
Definition: BESNotFoundError.h:40
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
BESError
Abstract exception class for the BES with basic string message.
Definition: BESError.h:58
CSV_Obj
Definition: CSV_Obj.h:47