本文整理汇总了C++中NcVar::type方法的典型用法代码示例。如果您正苦于以下问题:C++ NcVar::type方法的具体用法?C++ NcVar::type怎么用?C++ NcVar::type使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NcVar
的用法示例。
在下文中一共展示了NcVar::type方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: dumpvars
void DumpableNcFile::dumpvars( void )
{
int n;
static const char* types[] =
{"","byte","char","short","long","float","double"};
NcVar* vp;
for(n = 0; vp = get_var(n); n++) {
cout << "\t" << types[vp->type()] << " " << vp->name() ;
if (vp->num_dims() > 0) {
cout << "(";
for (int d = 0; d < vp->num_dims(); d++) {
NcDim* dim = vp->get_dim(d);
cout << dim->name();
if (d < vp->num_dims()-1)
cout << ", ";
}
cout << ")";
}
cout << " ;\n";
// now dump each of this variable's attributes
dumpatts(*vp);
}
}
示例2: type
const std::string type( void ) const
{
switch ( m_var->type() )
{
case ncByte: return( "unsigned char" );
case ncChar: return( "char" );
case ncShort: return( "short" );
case ncInt: return( "int" );
case ncFloat: return( "float" );
case ncDouble: return( "double" );
default: return( "" );
}
}
示例3: data
const kvs::AnyValueArray data(
const size_t offset = 0,
const size_t dim1 = 1,
const size_t dim2 = 1,
const size_t dim3 = 1 ) const
{
const void* head = m_var->values()->base();
const size_t nvalues = dim1 * dim2 * dim3;
switch ( m_var->type() )
{
case ncByte:
{
kvs::UInt8* values = (kvs::UInt8*)( head ) + offset;
return( kvs::AnyValueArray( this->flip( dim1, dim2, dim3, values ), nvalues ) );
}
case ncChar:
{
kvs::Int8* values = (kvs::Int8*)( head ) + offset;
return( kvs::AnyValueArray( this->flip( dim1, dim2, dim3, values ), nvalues ) );
}
case ncShort:
{
kvs::Int16* values = (kvs::Int16*)( head ) + offset;
return( kvs::AnyValueArray( this->flip( dim1, dim2, dim3, values ), nvalues ) );
}
case ncInt:
{
kvs::Int32* values = (kvs::Int32*)( head ) + offset;
return( kvs::AnyValueArray( this->flip( dim1, dim2, dim3, values ), nvalues ) );
}
case ncFloat:
{
kvs::Real32* values = (kvs::Real32*)( head ) + offset;
return( kvs::AnyValueArray( this->flip( dim1, dim2, dim3, values ), nvalues ) );
}
case ncDouble:
{
kvs::Real64* values = (kvs::Real64*)( head ) + offset;
return( kvs::AnyValueArray( this->flip( dim1, dim2, dim3, values ), nvalues ) );
}
default: return( kvs::AnyValueArray() );
}
}
示例4: ReadCFTimeDataFromNcFile
void ReadCFTimeDataFromNcFile(
NcFile * ncfile,
const std::string & strFilename,
std::vector<Time> & vecTimes,
bool fWarnOnMissingCalendar
) {
// Empty existing Time vector
vecTimes.clear();
// Get time dimension
NcDim * dimTime = ncfile->get_dim("time");
if (dimTime == NULL) {
_EXCEPTION1("Dimension \"time\" not found in file \"%s\"",
strFilename.c_str());
}
// Get time variable
NcVar * varTime = ncfile->get_var("time");
if (varTime == NULL) {
_EXCEPTION1("Variable \"time\" not found in file \"%s\"",
strFilename.c_str());
}
if (varTime->num_dims() != 1) {
_EXCEPTION1("Variable \"time\" has more than one dimension in file \"%s\"",
strFilename.c_str());
}
if (strcmp(varTime->get_dim(0)->name(), "time") != 0) {
_EXCEPTION1("Variable \"time\" does not have dimension \"time\" in file \"%s\"",
strFilename.c_str());
}
// Calendar attribute
NcAtt * attTimeCal = varTime->get_att("calendar");
std::string strCalendar;
if (attTimeCal == NULL) {
if (fWarnOnMissingCalendar) {
Announce("WARNING: Variable \"time\" is missing \"calendar\" attribute; assuming \"standard\"");
}
strCalendar = "standard";
} else {
strCalendar = attTimeCal->as_string(0);
}
Time::CalendarType eCalendarType =
Time::CalendarTypeFromString(strCalendar);
// Units attribute
NcAtt * attTimeUnits = varTime->get_att("units");
if (attTimeUnits == NULL) {
_EXCEPTION1("Variable \"time\" is missing \"units\" attribute in file \"%s\"",
strFilename.c_str());
}
std::string strTimeUnits = attTimeUnits->as_string(0);
// Load in time data
DataVector<int> vecTimeInt;
DataVector<float> vecTimeFloat;
DataVector<double> vecTimeDouble;
DataVector<ncint64> vecTimeInt64;
if (varTime->type() == ncInt) {
vecTimeInt.Initialize(dimTime->size());
varTime->set_cur((long)0);
varTime->get(&(vecTimeInt[0]), dimTime->size());
} else if (varTime->type() == ncFloat) {
vecTimeFloat.Initialize(dimTime->size());
varTime->set_cur((long)0);
varTime->get(&(vecTimeFloat[0]), dimTime->size());
} else if (varTime->type() == ncDouble) {
vecTimeDouble.Initialize(dimTime->size());
varTime->set_cur((long)0);
varTime->get(&(vecTimeDouble[0]), dimTime->size());
} else if (varTime->type() == ncInt64) {
vecTimeInt64.Initialize(dimTime->size());
varTime->set_cur((long)0);
varTime->get(&(vecTimeInt64[0]), dimTime->size());
} else {
_EXCEPTION1("Variable \"time\" has invalid type "
"(expected \"int\", \"int64\", \"float\" or \"double\")"
" in file \"%s\"", strFilename.c_str());
}
for (int t = 0; t < dimTime->size(); t++) {
Time time(eCalendarType);
if (varTime->type() == ncInt) {
time.FromCFCompliantUnitsOffsetInt(
strTimeUnits,
vecTimeInt[t]);
} else if (varTime->type() == ncFloat) {
time.FromCFCompliantUnitsOffsetDouble(
strTimeUnits,
static_cast<double>(vecTimeFloat[t]));
} else if (varTime->type() == ncDouble) {
time.FromCFCompliantUnitsOffsetDouble(
strTimeUnits,
//.........这里部分代码省略.........
示例5: CopyNcVar
void CopyNcVar(
NcFile & ncIn,
NcFile & ncOut,
const std::string & strVarName,
bool fCopyAttributes,
bool fCopyData
) {
if (!ncIn.is_valid()) {
_EXCEPTIONT("Invalid input file specified");
}
if (!ncOut.is_valid()) {
_EXCEPTIONT("Invalid output file specified");
}
NcVar * var = ncIn.get_var(strVarName.c_str());
if (var == NULL) {
_EXCEPTION1("NetCDF file does not contain variable \"%s\"",
strVarName.c_str());
}
NcVar * varOut;
std::vector<NcDim *> dimOut;
dimOut.resize(var->num_dims());
std::vector<long> counts;
counts.resize(var->num_dims());
long nDataSize = 1;
for (int d = 0; d < var->num_dims(); d++) {
NcDim * dimA = var->get_dim(d);
dimOut[d] = ncOut.get_dim(dimA->name());
if (dimOut[d] == NULL) {
if (dimA->is_unlimited()) {
dimOut[d] = ncOut.add_dim(dimA->name());
} else {
dimOut[d] = ncOut.add_dim(dimA->name(), dimA->size());
}
if (dimOut[d] == NULL) {
_EXCEPTION2("Failed to add dimension \"%s\" (%i) to file",
dimA->name(), dimA->size());
}
}
if (dimOut[d]->size() != dimA->size()) {
if (dimA->is_unlimited() && !dimOut[d]->is_unlimited()) {
_EXCEPTION2("Mismatch between input file dimension \"%s\" and "
"output file dimension (UNLIMITED / %i)",
dimA->name(), dimOut[d]->size());
} else if (!dimA->is_unlimited() && dimOut[d]->is_unlimited()) {
_EXCEPTION2("Mismatch between input file dimension \"%s\" and "
"output file dimension (%i / UNLIMITED)",
dimA->name(), dimA->size());
} else if (!dimA->is_unlimited() && !dimOut[d]->is_unlimited()) {
_EXCEPTION3("Mismatch between input file dimension \"%s\" and "
"output file dimension (%i / %i)",
dimA->name(), dimA->size(), dimOut[d]->size());
}
}
counts[d] = dimA->size();
nDataSize *= counts[d];
}
// ncByte / ncChar type
if ((var->type() == ncByte) || (var->type() == ncChar)) {
DataVector<char> data;
data.Initialize(nDataSize);
varOut =
ncOut.add_var(
var->name(), var->type(),
dimOut.size(), (const NcDim**)&(dimOut[0]));
if (varOut == NULL) {
_EXCEPTION1("Cannot create variable \"%s\"", var->name());
}
var->get(&(data[0]), &(counts[0]));
varOut->put(&(data[0]), &(counts[0]));
}
// ncShort type
if (var->type() == ncShort) {
DataVector<short> data;
data.Initialize(nDataSize);
varOut =
ncOut.add_var(
var->name(), var->type(),
dimOut.size(), (const NcDim**)&(dimOut[0]));
if (varOut == NULL) {
_EXCEPTION1("Cannot create variable \"%s\"", var->name());
}
if (fCopyData) {
var->get(&(data[0]), &(counts[0]));
//.........这里部分代码省略.........
示例6: main
int main(int argc, char** argv)
{
if (!cmdline(argc, argv))
{
printhelp();
return EXIT_FAILURE;
}
NcFile infile(infilename.c_str(), NcFile::ReadOnly);
if (!infile.is_valid())
{
std::cerr << "Error: invalid input file -- '" << infilename << "'" << std::endl;
infile.close();
return EXIT_FAILURE;
}
NcFile outfile(outfilename.c_str(), NcFile::Replace);
if (!outfile.is_valid())
{
std::cerr << "Error: cannot open output file -- '" << outfilename << "'" << std::endl;
outfile.close();
return EXIT_FAILURE;
}
if (varstrings.size() == 0)
{
std::cerr << "Warning: no variables specified" << std::endl;
}
std::vector<NcVar*> invars;
for (std::vector<std::string>::const_iterator it = varstrings.begin();
it != varstrings.end(); ++it)
{
NcVar* var = infile.get_var((*it).c_str());
if (var == NULL)
{
std::cerr << "Error: " << *it << ": no such variable" << std::endl;
infile.close();
outfile.close();
return EXIT_FAILURE;
}
invars.push_back(var);
}
// extract the distinct set of dims
std::map<std::string, NcDim*> indims;
for (std::vector<NcVar*>::const_iterator it = invars.begin();
it != invars.end(); ++it)
{
NcVar* var = *it;
for (int i = 0; i < var->num_dims(); ++i)
{
NcDim* dim = var->get_dim(i);
indims[dim->name()] = dim;
}
}
// add dims to outfile
std::map<std::string, NcDim*> outdims;
for (std::map<std::string, NcDim*>::const_iterator it = indims.begin();
it != indims.end(); ++it)
{
NcDim* dim = (*it).second;
NcDim* outdim = NULL;
if (dim->is_unlimited())
{
outdim = outfile.add_dim(dim->name());
}
else
{
outdim = outfile.add_dim(dim->name(), dim->size());
}
if (outdim != NULL)
{
outdims[outdim->name()] = outdim;
}
}
// create variables
for (std::vector<NcVar*>::const_iterator it = invars.begin();
it != invars.end(); ++it)
{
NcVar* var = *it;
std::vector<const NcDim*> dims(var->num_dims());
for (int i = 0; i < var->num_dims(); ++i)
{
dims[i] = outdims[var->get_dim(i)->name()];
}
NcVar* outvar = outfile.add_var(var->name(), var->type(), var->num_dims(), &dims[0]);
// identify largest dim, if dim (nearly) exceeds main memory, split along that dim
int maxdim = -1;
long maxdimsize = 0;
long totallen = 1;
for (int i = 0; i < var->num_dims(); ++i)
{
NcDim* dim = var->get_dim(i);
if (dim->size() > maxdimsize)
{
//.........这里部分代码省略.........
示例7: readField
int NetcdfSource::readField(double *v, const QString& field, int s, int n) {
NcType dataType = ncNoType; /* netCDF data type */
/* Values for one record */
NcValues *record = 0;// = new NcValues(dataType,numFrameVals);
KST_DBG qDebug() << "Entering NetcdfSource::readField with params: " << field << ", from " << s << " for " << n << " frames" << endl;
/* For INDEX field */
if (field.toLower() == "index") {
if (n < 0) {
v[0] = double(s);
return 1;
}
for (int i = 0; i < n; ++i) {
v[i] = double(s + i);
}
return n;
}
/* For a variable from the netCDF file */
QByteArray bytes = field.toLatin1();
NcVar *var = _ncfile->get_var(bytes.constData()); // var is owned by _ncfile
if (!var) {
KST_DBG qDebug() << "Queried field " << field << " which can't be read" << endl;
return -1;
}
dataType = var->type();
if (s >= var->num_vals() / var->rec_size()) {
return 0;
}
bool oneSample = n < 0;
int recSize = var->rec_size();
switch (dataType) {
case ncShort:
{
if (oneSample) {
record = var->get_rec(s);
v[0] = record->as_short(0);
delete record;
} else {
for (int i = 0; i < n; i++) {
record = var->get_rec(i+s);
for (int j = 0; j < recSize; j++) {
v[i*recSize + j] = record->as_short(j);
}
delete record;
}
}
}
break;
case ncInt:
{
if (oneSample) {
record = var->get_rec(s);
v[0] = record->as_int(0);
delete record;
} else {
for (int i = 0; i < n; i++) {
record = var->get_rec(i+s);
KST_DBG qDebug() << "Read record " << i+s << endl;
for (int j = 0; j < recSize; j++) {
v[i*recSize + j] = record->as_int(j);
}
delete record;
}
}
}
break;
case ncFloat:
{
if (oneSample) {
record = var->get_rec(s);
v[0] = record->as_float(0);
delete record;
} else {
for (int i = 0; i < n; i++) {
record = var->get_rec(i+s);
for (int j = 0; j < recSize; j++) {
v[i*recSize + j] = record->as_float(j);
}
delete record;
}
}
}
break;
case ncDouble:
{
if (oneSample) {
record = var->get_rec(s);
v[0] = record->as_double(0);
delete record;
} else {
for (int i = 0; i < n; i++) {
//.........这里部分代码省略.........
示例8: loadNetCdfFile
bool EpidemicDataSet::loadNetCdfFile(const char * filename)
{
#if USE_NETCDF // TODO: should handle this differently
// change netcdf library error behavior
NcError err(NcError::verbose_nonfatal);
// open the netcdf file
NcFile ncFile(filename, NcFile::ReadOnly);
if(!ncFile.is_valid())
{
put_flog(LOG_FATAL, "invalid file %s", filename);
return false;
}
// get dimensions
NcDim * timeDim = ncFile.get_dim("time");
NcDim * nodesDim = ncFile.get_dim("nodes");
NcDim * stratificationsDim = ncFile.get_dim("stratifications");
if(timeDim == NULL || nodesDim == NULL || stratificationsDim == NULL)
{
put_flog(LOG_FATAL, "could not find a required dimension");
return false;
}
numTimes_ = timeDim->size();
// make sure we have the expected number of nodes
if(nodesDim->size() != numNodes_)
{
put_flog(LOG_FATAL, "got %i nodes, expected %i", nodesDim->size(), numNodes_);
return false;
}
put_flog(LOG_DEBUG, "file contains %i timesteps, %i nodes", numTimes_, numNodes_);
// make sure number of stratifications matches our expectation...
int numExpectedStratifications = 1;
for(unsigned int i=0; i<NUM_STRATIFICATION_DIMENSIONS; i++)
{
numExpectedStratifications *= stratifications_[i].size();
}
if(stratificationsDim->size() != numExpectedStratifications)
{
put_flog(LOG_FATAL, "got %i stratifications, expected %i", stratificationsDim->size(), numExpectedStratifications);
return false;
}
// get all float variables with dimensions (time, nodes, stratifications)
for(int i=0; i<ncFile.num_vars(); i++)
{
NcVar * ncVar = ncFile.get_var(i);
if(ncVar->num_dims() == 3 && ncVar->type() == ncFloat && strcmp(ncVar->get_dim(0)->name(), "time") == 0 && strcmp(ncVar->get_dim(1)->name(), "nodes") == 0 && strcmp(ncVar->get_dim(2)->name(), "stratifications") == 0)
{
put_flog(LOG_INFO, "found variable: %s", ncVar->name());
// full shape
blitz::TinyVector<int, 2+NUM_STRATIFICATION_DIMENSIONS> shape;
shape(0) = numTimes_;
shape(1) = numNodes_;
for(int j=0; j<NUM_STRATIFICATION_DIMENSIONS; j++)
{
shape(2 + j) = stratifications_[j].size();
}
blitz::Array<float, 2+NUM_STRATIFICATION_DIMENSIONS> var((float *)ncVar->values()->base(), shape, blitz::duplicateData);
variables_[std::string(ncVar->name())].reference(var);
}
}
#endif
return true;
}
示例9: err
//.........这里部分代码省略.........
// vatt.append(attname);
// else if (attname.contains("edge_") &&
// attname != "edge_neighbours")
// eatt.append(attname);
// }
m_vertexAttribute.clear();
m_edgeAttribute.clear();
m_vertexRadiusAttribute = -1;
m_edgeRadiusAttribute = -1;
int vri = 0;
int eri = 0;
for (int i=0; i < nvars; i++)
{
var = ncfFile.get_var(i);
QString attname = var->name();
attname.toLower();
if (attname.contains("vertex_") &&
attname != "vertex_center" &&
attname != "vertex_centre" &&
attname != "vertex_centers" &&
attname != "vertex_centres")
{
if (attname == "vertex_radius")
m_vertexRadiusAttribute = vri;
vri++;
QVector<float> val;
val.clear();
if (var->type() == ncByte || var->type() == ncChar)
{
uchar *v = new uchar[nv];
var->get((ncbyte *)v, nv);
for(int j=0; j<nv; j++)
val.append(v[j]);
delete [] v;
}
else if (var->type() == ncShort)
{
short *v = new short[nv];
var->get((short *)v, nv);
for(int j=0; j<nv; j++)
val.append(v[j]);
delete [] v;
}
else if (var->type() == ncInt)
{
int *v = new int[nv];
var->get((int *)v, nv);
for(int j=0; j<nv; j++)
val.append(v[j]);
delete [] v;
}
else if (var->type() == ncFloat)
{
float *v = new float[nv];
var->get((float *)v, nv);
for(int j=0; j<nv; j++)
val.append(v[j]);
delete [] v;
}