-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasetdescription.cpp
190 lines (140 loc) · 4.35 KB
/
datasetdescription.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2017 Fred Hutchinson Cancer Research Center
#include <string.h>
#include "datasetdescription.h"
#include "variable.h"
#include "sspt_list.h"
struct DimensionDesc {
char name[NC_MAX_NAME+1];
size_t size;
bool unlimited;
};
struct VariableDesc {
nc_type xtype;
char name[NC_MAX_NAME+1];
sspt_Array<DimensionDesc*> dims;
};
DataSetDescription::DataSetDescription()
{
}
DataSetDescription::~DataSetDescription()
{
//TODO free memory
}
bool DataSetDescription::addDimension(const char *dimname, size_t size)
{
sspt_Cord dname(dimname);
DimensionDesc *desc = 0;
if (!m_dims.find(dname, &desc)) {
desc = new DimensionDesc;
strncpy(desc->name, dname.c_str(), NC_MAX_NAME);
desc->unlimited = false;
desc->size = size;
m_dims.insert(dname, desc);
}
return true;
}
bool DataSetDescription::addVariable(const char *varname, nc_type xtype, const char *dim1, const char *dim2, const char *dim3)
{
sspt_Cord key(varname);
if (m_vars.find(key, 0)) {
fprintf(stderr, "ERROR duplicate variable when creating description %s\n", varname);
return false;
}
//todo switch to using variable arguments
sspt_List<const char *> dimNamesList;
dimNamesList.insertRear(dim1);
dimNamesList.insertRear(dim2);
dimNamesList.insertRear(dim3);
sspt_Array<const char *> dimNames;
dimNamesList.toArray(&dimNames);
sspt_List<DimensionDesc*> dimList;
for (size_t i = 0; i < dimNames.size(); i++) {
const char *name = dimNames[i];
if (0 == name)
continue;
sspt_Cord dname(name);
DimensionDesc *desc = 0;
if (!m_dims.find(dname, &desc)) {
fprintf(stderr, "ERROR adding variable %s with unknown dimension %s\n", varname, name);
return false;
}
dimList.insertRear(desc);
}
VariableDesc *vdesc = new VariableDesc;
vdesc->xtype = xtype;
strncpy(vdesc->name, varname, NC_MAX_NAME+1);
dimList.toArray(&vdesc->dims);
m_vars.insert(key, vdesc);
return true;
}
bool DataSetDescription::reviseDimensionSize(const char *name, size_t revisedSize)
{
sspt_Cord dname(name);
DimensionDesc *desc = 0;
if (!m_dims.find(dname, &desc)) {
fprintf(stderr, "ERROR could not find dimension %s\n", name);
return false;
}
desc->size = revisedSize;
return true;
}
bool DataSetDescription::createEmptyNetCDF(int *ncid, const char *file)
{
sspt_AVLTree<sspt_Cord,int> dimensionIndex;
int nret;
//open netcdf file
nret = nc_create(file, NC_CLOBBER | NC_NETCDF4, ncid);
FALSE_ON_NETCDF_ERROR(nret, "ERROR failed to create netcdf output file %s", file);
//two passes, first unlimited, second normal
for (sspt_AVLIterator<sspt_Cord,DimensionDesc*> iter = m_dims.begin();
!iter.atEnd(); iter.moveNext()) {
DimensionDesc *d = iter.data();
if (!d->unlimited)
continue;
int dim;
nret = nc_def_dim(*ncid, d->name, NC_UNLIMITED, &dim);
FALSE_ON_NETCDF_ERROR(nret, "ERROR failed to create %s dimension", d->name);
dimensionIndex.insert(iter.key(), dim);
}
for (sspt_AVLIterator<sspt_Cord,DimensionDesc*> iter = m_dims.begin();
!iter.atEnd(); iter.moveNext()) {
DimensionDesc *d = iter.data();
if (d->unlimited)
continue;
int dim;
nret = nc_def_dim(*ncid, d->name, d->size, &dim);
FALSE_ON_NETCDF_ERROR(nret, "ERROR failed to create %s dimension", d->name);
dimensionIndex.insert(iter.key(), dim);
}
for (sspt_AVLIterator<sspt_Cord,VariableDesc*> iter = m_vars.begin();
!iter.atEnd(); iter.moveNext()) {
VariableDesc *v = iter.data();
int *dims = new int[v->dims.size()];
int var;
for (size_t i = 0; i < v->dims.size(); i++) {
DimensionDesc *d = v->dims[i];
sspt_Cord name(d->name);
if (!dimensionIndex.find(name, &dims[i])) {
fprintf(stderr, "ERROR could not find dimension %s\n", d->name);
return false;
}
}
nret = nc_def_var(*ncid, v->name, v->xtype, v->dims.size(), dims, &var);
FALSE_ON_NETCDF_ERROR(nret, "ERROR failed to create %s variable\n", v->name);
delete[] dims;
}
ncendef(*ncid);
//nc_close(*ncid);
return true;
}
bool DataSetDescription::dimensionSize(const char *dimension, size_t *size)
{
sspt_Cord dname(dimension);
DimensionDesc *desc = 0;
if (!m_dims.find(dname, &desc)) {
fprintf(stderr, "ERROR could not find dimension %s\n", dimension);
return false;
}
*size = desc->size;
return true;
}