55 FONcBaseType(), _a(0), _array_type(NC_NAT), _ndims(0), _actual_ndims(0), _nelements(1), _dim_ids(0), _dim_sizes(
56 0), _str_data(0), _dont_use_it(false), _chunksizes(0), _grid_maps(0)
58 _a =
dynamic_cast<Array *
>(b);
60 string s = (string)
"File out netcdf, FONcArray was passed a " +
"variable that is not a DAP Array";
81 vector<FONcDim *>::iterator i = _dims.begin();
82 vector<FONcDim *>::iterator e = _dims.end();
96 vector<FONcDim*>::iterator d = _dims.begin();
97 while (d != _dims.end()) {
103 vector<FONcMap*>::iterator i = _grid_maps.begin();
104 while (i != _grid_maps.end()) {
113 delete[] _chunksizes;
134 BESDEBUG(
"fonc",
"FONcArray::convert - converting array " <<
_varname << endl);
137 _ndims = _a->dimensions();
138 _actual_ndims = _ndims;
139 if (_array_type == NC_CHAR) {
145 _dim_ids =
new int[_ndims];
146 _dim_sizes =
new size_t[_ndims];
148 _chunksizes =
new size_t[_ndims];
150 Array::Dim_iter di = _a->dim_begin();
151 Array::Dim_iter de = _a->dim_end();
153 for (; di != de; di++) {
154 int size = _a->dimension_size(di,
true);
155 _dim_sizes[dimnum] = size;
160 _chunksizes[dimnum] = size;
167 "FONcArray::convert - dimension size: " << size <<
" chunksize: " << _chunksizes[dimnum] << endl << *
this << endl);
172 FONcDim *use_dim = find_dim(embed, _a->dimension_name(di), size);
173 _dims.push_back(use_dim);
178 if (_array_type == NC_CHAR) {
180 int array_length = _a->length();
181 _str_data =
new string[array_length];
182 _a->buf2val((
void**) &_str_data);
185 size_t max_length = 0;
186 for (
int i = 0; i < array_length; i++) {
187 if (_str_data[i].length() > max_length) {
188 max_length = _str_data[i].length();
192 vector<string> empty_embed;
193 string lendim_name =
_varname +
"_len";
195 FONcDim *use_dim = find_dim(empty_embed, lendim_name, max_length,
true);
197 if (use_dim->
size() <
static_cast<int>(max_length)) {
201 _dim_sizes[_ndims - 1] = use_dim->
size();
202 _dim_ids[_ndims - 1] = use_dim->
dimid();
203 _dims.push_back(use_dim);
209 if (!
FONcGrid::InGrid && _actual_ndims == 1 && _a->name() == _a->dimension_name(_a->dim_begin())) {
215 _grid_maps.push_back(new_map);
223 BESDEBUG(
"fonc",
"FONcArray::convert - done converting array " <<
_varname << endl << *
this << endl);
240 FONcArray::find_dim(vector<string> &embed,
const string &name,
int size,
bool ignore_size)
247 for (; i != e && !ret_dim; i++) {
248 if (!((*i)->name().empty()) && ((*i)->name() ==
name)) {
252 else if ((*i)->size() == size) {
256 if (embed.size() > 0) {
258 return find_dim(tmp, ename, size);
260 string err = (string)
"fileout_netcdf:dimension found " +
"with the same name, but different size";
266 ret_dim =
new FONcDim(name, size);
291 BESDEBUG(
"fonc",
"FONcArray::define - defining array " <<
_varname << endl);
294 vector<FONcDim *>::iterator i = _dims.begin();
295 vector<FONcDim *>::iterator e = _dims.end();
297 for (; i != e; i++) {
300 _dim_ids[dimnum] = fd->
dimid();
301 BESDEBUG(
"fonc",
"FONcArray::dim_id " << fd->
dimid() <<
" size:" << fd->
size() << endl);
305 int stax = nc_def_var(ncid,
_varname.c_str(), _array_type, _ndims, _dim_ids, &
_varid);
306 if (stax != NC_NOERR) {
307 string err = (string)
"fileout.netcdf - " +
"Failed to define variable " +
_varname;
312 stax = nc_def_var_chunking(ncid,
_varid, NC_CHUNKED, _chunksizes);
314 if (stax != NC_NOERR) {
315 string err = (string)
"fileout.netcdf - " +
"Failed to define chunking for variable " +
_varname;
321 int deflate_level = 4;
322 stax = nc_def_var_deflate(ncid,
_varid, shuffle, deflate, deflate_level);
324 if (stax != NC_NOERR) {
325 string err = (string)
"fileout.netcdf - " +
"Failed to define compression deflation level for variable "
331 FONcAttributes::add_attributes(ncid,
_varid, _a);
338 BESDEBUG(
"fonc",
" variable " <<
_varname <<
" is already defined" << endl);
345 BESDEBUG(
"fonc",
"FONcArray::define - done defining array " <<
_varname << endl);
362 BESDEBUG(
"fonc",
"FONcTransform::write not using variable " <<
_varname << endl);
369 if (_array_type != NC_CHAR) {
370 string var_type = _a->var()->type_name();
373 switch (_array_type) {
375 unsigned char *data =
new unsigned char[_nelements];
376 _a->buf2val((
void**) &data);
377 stax = nc_put_var_uchar(ncid,
_varid, data);
380 if (stax != NC_NOERR) {
381 string err =
"fileout.netcdf - Failed to create array of bytes for " +
_varname;
388 short *data =
new short[_nelements];
394 if (var_type ==
"Byte") {
396 unsigned char *orig_data =
new unsigned char[_nelements];
397 _a->buf2val((
void**) &orig_data);
399 for (
int d_i = 0; d_i < _nelements; d_i++)
400 data[d_i] = orig_data[d_i];
405 _a->buf2val((
void**) &data);
407 int stax = nc_put_var_short(ncid,
_varid, data);
410 if (stax != NC_NOERR) {
411 string err = (string)
"fileout.netcdf - Failed to create array of shorts for " +
_varname;
418 int *data =
new int[_nelements];
421 if (var_type ==
"UInt16") {
422 unsigned short *orig_data =
new unsigned short[_nelements];
423 _a->buf2val((
void**) &orig_data);
425 for (
int d_i = 0; d_i < _nelements; d_i++)
426 data[d_i] = orig_data[d_i];
431 _a->buf2val((
void**) &data);
434 int stax = nc_put_var_int(ncid,
_varid, data);
437 if (stax != NC_NOERR) {
438 string err = (string)
"fileout.netcdf - Failed to create array of ints for " +
_varname;
445 float *data =
new float[_nelements];
446 _a->buf2val((
void**) &data);
447 int stax = nc_put_var_float(ncid,
_varid, data);
450 if (stax != NC_NOERR) {
451 string err = (string)
"fileout.netcdf - Failed to create array of floats for " +
_varname;
458 double *data =
new double[_nelements];
459 _a->buf2val((
void**) &data);
460 int stax = nc_put_var_double(ncid,
_varid, data);
463 if (stax != NC_NOERR) {
464 string err = (string)
"fileout.netcdf - Failed to create array of doubles for " +
_varname;
471 string err = (string)
"Failed to transform array of unknown type in file out netcdf";
478 size_t var_count[_ndims];
479 size_t var_start[_ndims];
481 for (dim = 0; dim < _ndims; dim++) {
491 for (
int element = 0; element < _nelements; element++) {
492 var_count[_ndims - 1] = _str_data[element].size() + 1;
493 var_start[_ndims - 1] = 0;
496 int stax = nc_put_vara_text(ncid,
_varid, var_start, var_count, _str_data[element].c_str());
497 if (stax != NC_NOERR) {
498 string err = (string)
"fileout.netcdf - Failed to create array of strings for " +
_varname;
503 if (element + 1 < _nelements) {
507 var_start[dim] = var_start[dim] + 1;
508 if (var_start[dim] == _dim_sizes[dim]) {
544 strm <<
BESIndent::LMarg <<
"FONcArray::dump - (" << (
void *)
this <<
")" << endl;
553 vector<FONcDim *>::const_iterator i = _dims.begin();
554 vector<FONcDim *>::const_iterator e = _dims.end();
555 for (; i != e; i++) {
A map of a DAP Grid with file out netcdf information included.
exception thrown if inernal error encountered
virtual bool isNetCDF4()
Returns true if NetCDF4 features will be required.
FONcArray(BaseType *b)
Constructor for FONcArray that takes a DAP Array.
virtual void write(int ncid)
Write the array out to the netcdf file.
static bool InGrid
tells whether we are converting or defining a grid.
static vector< FONcDim * > Dimensions
static void add_original_name(int ncid, int varid, const string &var_name, const string &orig)
Adds an attribute for the variable if the variable name had to be modified in any way...
virtual ~FONcArray()
Destructor that cleans up the array.
virtual void decref()
Decrement the reference count for this dimension.
static FONcMap * InMaps(Array *array)
virtual void update_size(int newsize)
static ostream & LMarg(ostream &strm)
static nc_type get_nc_type(BaseType *element)
translate the OPeNDAP data type to a netcdf data type
A class that represents the dimension of an array.
virtual void define(int ncid)
define the DAP Array in the netcdf file
static void handle_error(int stax, string &err, const string &file, int line)
handle any netcdf errors
virtual void convert(vector< string > embed)
Converts the DAP Array to a FONcArray.
A DAP BaseType with file out netcdf information included.
virtual void define(int ncid)
define the DAP dimension in the netcdf file
static string gen_name(const vector< string > &embed, const string &name, string &original)
generate a new name for the embedded variable
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
virtual string name()
returns the name of the DAP Array
virtual void convert(vector< string > embed)
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
static vector< FONcMap * > Maps
global list of maps that could be shared amongst the different grids