41 #include <BESInternalError.h>
42 #include <BESNotFoundError.h>
43 #include <BaseTypeFactory.h>
52 #include <mime_util.h>
58 void csv_read_descriptors(DDS &dds,
const string &filename)
68 if (!csvObj->open(filename)) {
70 string err = (string)
"Unable to open file " + filename;
75 BESDEBUG(
"csv",
"File loaded:" << endl << *csvObj << endl );
77 dds.set_dataset_name(name_path(filename));
79 vector<string> fieldList;
80 csvObj->getFieldList(fieldList);
81 int recordCount = csvObj->getRecordCount();
83 throw BESError(
"Could not read record count from the CSV dataset.", BES_NOT_FOUND_ERROR, __FILE__, __LINE__);
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);
93 if (type.compare(
string(STRING)) == 0) {
94 string* strings =
new string[recordCount];
96 bt = dds.get_factory()->NewStr(fieldName);
98 ar->append_dim(recordCount,
"record");
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;
108 ar->set_value(strings, recordCount);
111 else if (type.compare(
string(INT16)) == 0) {
112 short* int16 =
new short[recordCount];
113 bt = dds.get_factory()->NewInt16(fieldName);
115 ar->append_dim(recordCount,
"record");
118 vector<short>::iterator iv = ((vector<short>*) data)->begin();
119 vector<short>::iterator ev = ((vector<short>*) data)->end();
120 for (; iv != ev; iv++) {
125 ar->set_value(int16, recordCount);
128 else if (type.compare(
string(INT32)) == 0) {
129 int *int32 =
new int[recordCount];
130 bt = dds.get_factory()->NewInt32(fieldName);
132 ar->append_dim(recordCount,
"record");
135 vector<int>::iterator iv = ((vector<int>*) data)->begin();
136 vector<int>::iterator ev = ((vector<int>*) data)->end();
137 for (; iv != ev; iv++) {
142 ar->set_value((dods_int32*) int32, recordCount);
145 else if (type.compare(
string(FLOAT32)) == 0) {
146 float *floats =
new float[recordCount];
147 bt = dds.get_factory()->NewFloat32(fieldName);
149 ar->append_dim(recordCount,
"record");
152 vector<float>::iterator iv = ((vector<float>*) data)->begin();
153 vector<float>::iterator ev = ((vector<float>*) data)->end();
154 for (; iv != ev; iv++) {
159 ar->set_value(floats, recordCount);
162 else if (type.compare(
string(FLOAT64)) == 0) {
163 double *doubles =
new double[recordCount];
164 bt = dds.get_factory()->NewFloat64(fieldName);
166 ar->append_dim(recordCount,
"record");
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;
176 ar->set_value(doubles, recordCount);
181 string err = (string)
"Unknown type for field " + fieldName;