Commit 09e69dd2 authored by Ralf Mueller's avatar Ralf Mueller
Browse files

Replaces the (pointer to double) attribute fields with (std::vector<double>)...

Replaces the (pointer to double) attribute fields with (std::vector<double>) values [same for fieldWithLevel]
Ponters are still available as return values of 'getValuesAsPointer' and 'getValuesWithLevelAsPointer'
parent e60c048c
......@@ -47,22 +47,33 @@ int main() {
printf("Zaxis (prec):%d\n" , var.zaxis.prec);
printf("Zaxis (levels): varno:%f -1:%f\n", var.zaxis.levels[varno], var.zaxis.levels[var.zaxis.size-1]);
var.readField();
var.readFieldF();
var.readFieldWithLevel();
var.readFieldWithLevelF();
std::cout << "field[0] = " << var.field[0] << ' ';
std::cout << "field[-1] = " << var.field[var.grid.size-1] << std::endl;
std::cout << "fieldF[0] = " << var.fieldF[0] << ' ';
std::cout << "fieldF[-1] = " << var.fieldF[var.grid.size-1] << std::endl;
std::cout << "# field values ================" << std::endl;
var.getValues();
std::cout << "values[0] = " << var.values[0] << std::endl;
std::cout << "values.back() = " << var.values.back() << std::endl;
std::cout << "# field values on different levels ================" << std::endl;
var.getValuesWithLevel();
for ( int ilev=0; ilev < var.zaxis.size; ilev++)
{
std::cout << "Level:" << ilev << std::endl;
std::cout << "fieldWithLevel[" << ilev << "][0] = " << var.fieldWithLevel[ilev][0] << ' ';
std::cout << "fieldWithLevel[" << ilev << "][-1] = " << var.fieldWithLevel[ilev][var.grid.size-1] << std::endl;
std::cout << "fieldWithLevelF[" << ilev << "][0] = " << var.fieldWithLevelF[ilev][0] << ' ';
std::cout << "fieldWithLevelF[" << ilev << "][-1] = " << var.fieldWithLevelF[ilev][var.grid.size-1] << std::endl;
std::cout << "valuesWithLevel[" << ilev << "][0] = " << var.valuesWithLevel[ilev][0] << ' ';
std::cout << "valuesWithLevel[" << ilev << "][-1] = " << var.valuesWithLevel[ilev][var.grid.size-1] << std::endl;
}
std::cout << "# field values (FLOAT) ================" << std::endl;
vector<float> floats;
floats = var.getFValues();
std::cout << "floats[0] = " << floats[0] << std::endl;
std::cout << "floats.back() = " << floats.back() << std::endl;
std::cout << "# field values on different levels (FLOAT) ================" << std::endl;
vector< vector<float> > floatsWithLevel;
floatsWithLevel = var.getFValuesWithLevel();
for (int i = 0 ; i < floatsWithLevel.size();++i)
{
std::cout << "floatsWithLevel[" << i << "] = " << floatsWithLevel[i].front() << ' ' << floatsWithLevel[i].back() << std::endl;
}
std::cout << "#===============================" << std::endl;
std::cout << "#== Reading from the var map ===" << std::endl;
_name = info.variables[varno+10].name;
......@@ -76,5 +87,6 @@ int main() {
std::cout << "#==== zlevels: " << var.zaxis.size << std::endl;
std::cout << "#===============================" << std::endl;
return 0;
}
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
......@@ -166,80 +167,81 @@ CdiVariable::~CdiVariable(){ size = -1; }
void
CdiVariable::sinfo() { std::cout << name << " (" << longname << ")|units: " << units << "|size:"<< size << std::endl; }
void
CdiVariable::readField()
double *
CdiVariable::getValuesAsPointer()
{
int levelID = 0, tsID = 0, nmiss;
int vdate, vtime, nrecs;
double *field;
nrecs = streamInqTimestep(streamID, tsID);
vdate = taxisInqVdate(taxisID);
vtime = taxisInqVtime(taxisID);
field = (double *) malloc(grid.size*sizeof(double));
streamReadVarSlice(streamID, varID, levelID, field, &nmiss);
}
std::vector<double>
CdiVariable::getField()
{
std::vector<double> dvector;
for (int i = 0; i< size; i++)
dvector.push_back(field[i]);
return dvector;
return field;
}
void
CdiVariable::readFieldWithLevel(int tsID) {
double **
CdiVariable::getValuesWithLevelAsPointer(int tsID)
{
int levelID, nmiss, nrecs;
double *_field;
double **fieldWithLevel, *field;
nrecs = streamInqTimestep(streamID, tsID);
fieldWithLevel = (double **) malloc(zaxis.size*sizeof(double *));
_field = (double *) malloc(grid.size*sizeof(double));
field = (double *) malloc(grid.size*sizeof(double));
for (levelID = 0; levelID < zaxis.size; levelID++)
{
streamReadVarSlice(streamID, varID, levelID, _field, &nmiss);
fieldWithLevel[levelID] = _field;
streamReadVarSlice(streamID, varID, levelID, field, &nmiss);
fieldWithLevel[levelID] = field;
}
return fieldWithLevel;
}
void
CdiVariable::readFieldF()
CdiVariable::getValues()
{
int levelID = 0, tsID = 0, nmiss;
int vdate, vtime;
int nrecs;
nrecs = streamInqTimestep(streamID, tsID);
vdate = taxisInqVdate(taxisID);
vtime = taxisInqVtime(taxisID);
field = (double *) malloc(grid.size*sizeof(double));
double *field = getValuesAsPointer();
values.resize(grid.size);
std::copy(field, field + grid.size, values.begin());
}
streamReadVarSlice(streamID, varID, levelID, field, &nmiss);
void
CdiVariable::getValuesWithLevel(int tsID) {
double **fieldWithLevel = getValuesWithLevelAsPointer(tsID);
fieldF = (float *) field;
valuesWithLevel.resize(zaxis.size);
for (int levelID = 0; levelID < zaxis.size; levelID++)
{
valuesWithLevel[levelID].resize(grid.size);
std::copy(fieldWithLevel[levelID], fieldWithLevel[levelID] + grid.size, valuesWithLevel[levelID].begin());
}
}
void
CdiVariable::readFieldWithLevelF(int tsID)
vector<float>
CdiVariable::getFValues()
{
int levelID, nmiss, nrecs;
double *_field;
if (values.empty()) getValues();
vector<float> retval(values.begin(),values.end());
return retval;
}
nrecs = streamInqTimestep(streamID, tsID);
fieldWithLevelF = (float **) malloc(zaxis.size*sizeof(float *));
_field = (double *) malloc(grid.size*sizeof(double));
for (levelID = 0; levelID < zaxis.size; levelID++)
vector< vector<float> >
CdiVariable::getFValuesWithLevel(int tsID)
{
if (valuesWithLevel.empty()) getValuesWithLevel();
vector< vector<float> > retval;
for (vector< vector<double> >::const_iterator it = valuesWithLevel.begin(); it != valuesWithLevel.end(); it++)
{
streamReadVarSlice(streamID, varID, levelID, _field, &nmiss);
fieldWithLevelF[levelID] = (float *) _field;
vector<float> fvalues((*it).begin(),(*it).end());
retval.push_back(fvalues);
}
return retval;
}
/* } */
/*
......
......@@ -2,6 +2,8 @@
#include <map>
#define CHARSIZE 128
using namespace std;
class CdiGrid {
public:
CdiGrid();
......@@ -63,10 +65,8 @@ class CdiVariable {
int streamID;
std::string name, longname, units, stdname;
double missval;
double *field;
double **fieldWithLevel;
float *fieldF;
float **fieldWithLevelF;
vector<double> values;
vector< vector<double> > valuesWithLevel;
CdiGrid grid;
......@@ -74,11 +74,12 @@ class CdiVariable {
CdiTaxis taxis;
void sinfo();
void readField();
void readFieldWithLevel(int tsID = 0);
void readFieldF();
void readFieldWithLevelF(int tsID = 0);
std::vector<double> getField();
void getValues();
void getValuesWithLevel(int tsID = 0);
vector<float> getFValues();
vector< vector<float> > getFValuesWithLevel(int tsID = 0);
double *getValuesAsPointer();
double **getValuesWithLevelAsPointer(int tsID = 0);
};
class Cdi {
......
......@@ -7,6 +7,7 @@
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
%template(DoubleDoubleVector) vector< vector<double> >;
%template(StringVector) vector<string>;
%template(VarsVector) vector<CdiVariable>;
%template(VarsMap) map<string,CdiVariable>;
......
......@@ -189,6 +189,53 @@ class DoubleVector(_object):
DoubleVector_swigregister = _CdiObj.DoubleVector_swigregister
DoubleVector_swigregister(DoubleVector)
class DoubleDoubleVector(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, DoubleDoubleVector, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, DoubleDoubleVector, name)
__repr__ = _swig_repr
def iterator(self): return _CdiObj.DoubleDoubleVector_iterator(self)
def __iter__(self): return self.iterator()
def __nonzero__(self): return _CdiObj.DoubleDoubleVector___nonzero__(self)
def __bool__(self): return _CdiObj.DoubleDoubleVector___bool__(self)
def __len__(self): return _CdiObj.DoubleDoubleVector___len__(self)
def pop(self): return _CdiObj.DoubleDoubleVector_pop(self)
def __getslice__(self, *args): return _CdiObj.DoubleDoubleVector___getslice__(self, *args)
def __setslice__(self, *args): return _CdiObj.DoubleDoubleVector___setslice__(self, *args)
def __delslice__(self, *args): return _CdiObj.DoubleDoubleVector___delslice__(self, *args)
def __delitem__(self, *args): return _CdiObj.DoubleDoubleVector___delitem__(self, *args)
def __getitem__(self, *args): return _CdiObj.DoubleDoubleVector___getitem__(self, *args)
def __setitem__(self, *args): return _CdiObj.DoubleDoubleVector___setitem__(self, *args)
def append(self, *args): return _CdiObj.DoubleDoubleVector_append(self, *args)
def empty(self): return _CdiObj.DoubleDoubleVector_empty(self)
def size(self): return _CdiObj.DoubleDoubleVector_size(self)
def clear(self): return _CdiObj.DoubleDoubleVector_clear(self)
def swap(self, *args): return _CdiObj.DoubleDoubleVector_swap(self, *args)
def get_allocator(self): return _CdiObj.DoubleDoubleVector_get_allocator(self)
def begin(self): return _CdiObj.DoubleDoubleVector_begin(self)
def end(self): return _CdiObj.DoubleDoubleVector_end(self)
def rbegin(self): return _CdiObj.DoubleDoubleVector_rbegin(self)
def rend(self): return _CdiObj.DoubleDoubleVector_rend(self)
def pop_back(self): return _CdiObj.DoubleDoubleVector_pop_back(self)
def erase(self, *args): return _CdiObj.DoubleDoubleVector_erase(self, *args)
def __init__(self, *args):
this = _CdiObj.new_DoubleDoubleVector(*args)
try: self.this.append(this)
except: self.this = this
def push_back(self, *args): return _CdiObj.DoubleDoubleVector_push_back(self, *args)
def front(self): return _CdiObj.DoubleDoubleVector_front(self)
def back(self): return _CdiObj.DoubleDoubleVector_back(self)
def assign(self, *args): return _CdiObj.DoubleDoubleVector_assign(self, *args)
def resize(self, *args): return _CdiObj.DoubleDoubleVector_resize(self, *args)
def insert(self, *args): return _CdiObj.DoubleDoubleVector_insert(self, *args)
def reserve(self, *args): return _CdiObj.DoubleDoubleVector_reserve(self, *args)
def capacity(self): return _CdiObj.DoubleDoubleVector_capacity(self)
__swig_destroy__ = _CdiObj.delete_DoubleDoubleVector
__del__ = lambda self : None;
DoubleDoubleVector_swigregister = _CdiObj.DoubleDoubleVector_swigregister
DoubleDoubleVector_swigregister(DoubleDoubleVector)
class StringVector(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, StringVector, name, value)
......@@ -727,18 +774,12 @@ class CdiVariable(_object):
__swig_setmethods__["missval"] = _CdiObj.CdiVariable_missval_set
__swig_getmethods__["missval"] = _CdiObj.CdiVariable_missval_get
if _newclass:missval = _swig_property(_CdiObj.CdiVariable_missval_get, _CdiObj.CdiVariable_missval_set)
__swig_setmethods__["field"] = _CdiObj.CdiVariable_field_set
__swig_getmethods__["field"] = _CdiObj.CdiVariable_field_get
if _newclass:field = _swig_property(_CdiObj.CdiVariable_field_get, _CdiObj.CdiVariable_field_set)
__swig_setmethods__["fieldWithLevel"] = _CdiObj.CdiVariable_fieldWithLevel_set
__swig_getmethods__["fieldWithLevel"] = _CdiObj.CdiVariable_fieldWithLevel_get
if _newclass:fieldWithLevel = _swig_property(_CdiObj.CdiVariable_fieldWithLevel_get, _CdiObj.CdiVariable_fieldWithLevel_set)
__swig_setmethods__["fieldF"] = _CdiObj.CdiVariable_fieldF_set
__swig_getmethods__["fieldF"] = _CdiObj.CdiVariable_fieldF_get
if _newclass:fieldF = _swig_property(_CdiObj.CdiVariable_fieldF_get, _CdiObj.CdiVariable_fieldF_set)
__swig_setmethods__["fieldWithLevelF"] = _CdiObj.CdiVariable_fieldWithLevelF_set
__swig_getmethods__["fieldWithLevelF"] = _CdiObj.CdiVariable_fieldWithLevelF_get
if _newclass:fieldWithLevelF = _swig_property(_CdiObj.CdiVariable_fieldWithLevelF_get, _CdiObj.CdiVariable_fieldWithLevelF_set)
__swig_setmethods__["values"] = _CdiObj.CdiVariable_values_set
__swig_getmethods__["values"] = _CdiObj.CdiVariable_values_get
if _newclass:values = _swig_property(_CdiObj.CdiVariable_values_get, _CdiObj.CdiVariable_values_set)
__swig_setmethods__["valuesWithLevel"] = _CdiObj.CdiVariable_valuesWithLevel_set
__swig_getmethods__["valuesWithLevel"] = _CdiObj.CdiVariable_valuesWithLevel_get
if _newclass:valuesWithLevel = _swig_property(_CdiObj.CdiVariable_valuesWithLevel_get, _CdiObj.CdiVariable_valuesWithLevel_set)
__swig_setmethods__["grid"] = _CdiObj.CdiVariable_grid_set
__swig_getmethods__["grid"] = _CdiObj.CdiVariable_grid_get
if _newclass:grid = _swig_property(_CdiObj.CdiVariable_grid_get, _CdiObj.CdiVariable_grid_set)
......@@ -749,11 +790,12 @@ class CdiVariable(_object):
__swig_getmethods__["taxis"] = _CdiObj.CdiVariable_taxis_get
if _newclass:taxis = _swig_property(_CdiObj.CdiVariable_taxis_get, _CdiObj.CdiVariable_taxis_set)
def sinfo(self): return _CdiObj.CdiVariable_sinfo(self)
def readField(self): return _CdiObj.CdiVariable_readField(self)
def readFieldWithLevel(self, tsID = 0): return _CdiObj.CdiVariable_readFieldWithLevel(self, tsID)
def readFieldF(self): return _CdiObj.CdiVariable_readFieldF(self)
def readFieldWithLevelF(self, tsID = 0): return _CdiObj.CdiVariable_readFieldWithLevelF(self, tsID)
def getField(self): return _CdiObj.CdiVariable_getField(self)
def getValues(self): return _CdiObj.CdiVariable_getValues(self)
def getValuesWithLevel(self, tsID = 0): return _CdiObj.CdiVariable_getValuesWithLevel(self, tsID)
def getFValues(self): return _CdiObj.CdiVariable_getFValues(self)
def getFValuesWithLevel(self, tsID = 0): return _CdiObj.CdiVariable_getFValuesWithLevel(self, tsID)
def getValuesAsPointer(self): return _CdiObj.CdiVariable_getValuesAsPointer(self)
def getValuesWithLevelAsPointer(self, tsID = 0): return _CdiObj.CdiVariable_getValuesWithLevelAsPointer(self, tsID)
CdiVariable_swigregister = _CdiObj.CdiVariable_swigregister
CdiVariable_swigregister(CdiVariable)
......
This diff is collapsed.
......@@ -29,9 +29,14 @@ for k in range(cdi.variables.size()):
print "#========== VAR ======================================#"
var = cdi.variables[1]
var.readField()
val = var.getField()
var.getValues()
val = var.values
i=0; print 'val[',i,'] = ',val[i]
i=1; print 'val[',i,'] = ',val[i]
i=2; print 'val[',i,'] = ',val[i]
i=3; print 'val[',i,'] = ',val[i]
i=4; print 'val[',i,'] = ',val[i]
i=5; print 'val[',i,'] = ',val[i]
print "#=====================================================#"
name ="tsurf"
newvar = cdi.var[name]
......
This diff is collapsed.
......@@ -52,9 +52,10 @@ puts "#========== CDI.VAR ======================================#"
varname = cdi.variables[1].name
pp cdi.methods.sort
var_ = cdi.variables[1]
var_.readField()
val = var_.getField()
var_.getValues
val = var_.values
i=0; puts "val[#{i}] = #{val[i]}"
pp val[-5..-1]
puts "#=====================================================#"
name ="tsurf"
newvar = cdi.var[name]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment