本文整理汇总了C++中NcValues类的典型用法代码示例。如果您正苦于以下问题:C++ NcValues类的具体用法?C++ NcValues怎么用?C++ NcValues使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NcValues类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: eavlFloatArray
eavlField*
eavlNetCDFImporter::GetField(const string &name, const string &mesh, int)
{
for (unsigned int v=0; v<vars.size(); v++)
{
NcVar *var = vars[v];
if (name != var->name())
continue;
if (debugoutput) cerr << "reading var "<<v+1<<" / "<<vars.size()<<endl;
eavlFloatArray *arr = new eavlFloatArray(var->name(), 1);
arr->SetNumberOfTuples(var->num_vals());
NcValues *vals = var->values();
int n = var->num_vals();
for (int i=0; i<n; i++)
{
arr->SetComponentFromDouble(i,0, vals->as_double(i));
}
eavlField *field = new eavlField(1, arr, eavlField::ASSOC_POINTS);
return field;
}
return NULL;
}
示例2: values
char* NcTypedComponent::as_string( long n ) const
{
NcValues* tmp = values();
char* rval = tmp->as_string(n);
delete tmp;
return rval;
}
示例3: num_dims
NcValues* NcVar::values( void ) const
{
int ndims = num_dims();
size_t crnr[NC_MAX_DIMS];
size_t edgs[NC_MAX_DIMS];
for (int i = 0; i < ndims; i++) {
crnr[i] = 0;
edgs[i] = get_dim(i)->size();
}
NcValues* valp = get_space();
int status;
switch (type()) {
case ncFloat:
status = NcError::set_err(
nc_get_vara_float(the_file->id(), the_id, crnr, edgs,
(float *)valp->base())
);
break;
case ncDouble:
status = NcError::set_err(
nc_get_vara_double(the_file->id(), the_id, crnr, edgs,
(double *)valp->base())
);
break;
case ncInt:
status = NcError::set_err(
nc_get_vara_int(the_file->id(), the_id, crnr, edgs,
(int *)valp->base())
);
break;
case ncShort:
status = NcError::set_err(
nc_get_vara_short(the_file->id(), the_id, crnr, edgs,
(short *)valp->base())
);
break;
case ncByte:
status = NcError::set_err(
nc_get_vara_schar(the_file->id(), the_id, crnr, edgs,
(signed char *)valp->base())
);
break;
case ncChar:
status = NcError::set_err(
nc_get_vara_text(the_file->id(), the_id, crnr, edgs,
(char *)valp->base())
);
break;
case ncNoType:
default:
return 0;
}
if (status != NC_NOERR)
return 0;
return valp;
}
示例4: get_space
NcValues* NcAtt::values( void ) const
{
NcValues* valp = get_space();
int status;
switch (type()) {
case ncFloat:
status = NcError::set_err(
nc_get_att_float(the_file->id(), the_variable->id(), the_name,
(float *)valp->base())
);
break;
case ncDouble:
status = NcError::set_err(
nc_get_att_double(the_file->id(), the_variable->id(), the_name,
(double *)valp->base())
);
break;
case ncInt:
status = NcError::set_err(
nc_get_att_int(the_file->id(), the_variable->id(), the_name,
(int *)valp->base())
);
break;
case ncShort:
status = NcError::set_err(
nc_get_att_short(the_file->id(), the_variable->id(), the_name,
(short *)valp->base())
);
break;
case ncByte:
status = NcError::set_err(
nc_get_att_schar(the_file->id(), the_variable->id(), the_name,
(signed char *)valp->base())
);
break;
case ncChar:
status = NcError::set_err(
nc_get_att_text(the_file->id(), the_variable->id(), the_name,
(char *)valp->base())
);
break;
case ncNoType:
default:
return 0;
}
if (status != NC_NOERR) {
delete valp;
return 0;
}
return valp;
}
示例5: CopyNcVarAttributes
void CopyNcVarAttributes(
NcVar * varIn,
NcVar * varOut
) {
for (int a = 0; a < varIn->num_atts(); a++) {
NcAtt * att = varIn->get_att(a);
long num_vals = att->num_vals();
NcValues * pValues = att->values();
if (att->type() == ncByte) {
varOut->add_att(att->name(), num_vals,
(const ncbyte*)(pValues->base()));
} else if (att->type() == ncChar) {
varOut->add_att(att->name(), num_vals,
(const char*)(pValues->base()));
} else if (att->type() == ncShort) {
varOut->add_att(att->name(), num_vals,
(const short*)(pValues->base()));
} else if (att->type() == ncInt) {
varOut->add_att(att->name(), num_vals,
(const int*)(pValues->base()));
} else if (att->type() == ncFloat) {
varOut->add_att(att->name(), num_vals,
(const float*)(pValues->base()));
} else if (att->type() == ncDouble) {
varOut->add_att(att->name(), num_vals,
(const double*)(pValues->base()));
} else {
_EXCEPTIONT("Invalid attribute type");
}
delete pValues;
}
}
示例6: dim_to_index
NcValues* NcVar::get_rec(NcDim* rdim, long slice)
{
int idx = dim_to_index(rdim);
long size = num_dims();
size_t* start = new size_t[size];
long* startl = new long[size];
for (int i=1; i < size ; i++) {
start[i] = 0;
startl[i] = 0;
}
start[idx] = slice;
startl[idx] = slice;
NcBool result = set_cur(startl);
if (! result ) {
delete [] start;
delete [] startl;
return 0;
}
long* edgel = edges();
size_t* edge = new size_t[size];
for (int i=1; i < size ; i++) {
edge[i] = edgel[i];
}
edge[idx] = 1;
edgel[idx] = 1;
NcValues* valp = get_space(rec_size(rdim));
int status;
switch (type()) {
case ncFloat:
status = NcError::set_err(
nc_get_vara_float(the_file->id(), the_id, start, edge,
(float *)valp->base())
);
break;
case ncDouble:
status = NcError::set_err(
nc_get_vara_double(the_file->id(), the_id, start, edge,
(double *)valp->base())
);
break;
case ncInt:
status = NcError::set_err(
nc_get_vara_int(the_file->id(), the_id, start, edge,
(int *)valp->base())
);
break;
case ncShort:
status = NcError::set_err(
nc_get_vara_short(the_file->id(), the_id, start, edge,
(short *)valp->base())
);
break;
case ncByte:
status = NcError::set_err(
nc_get_vara_schar(the_file->id(), the_id, start, edge,
(signed char *)valp->base())
);
break;
case ncChar:
status = NcError::set_err(
nc_get_vara_text(the_file->id(), the_id, start, edge,
(char *)valp->base())
);
break;
case ncNoType:
default:
return 0;
}
delete [] start;
delete [] startl;
delete [] edge;
delete [] edgel;
if (status != NC_NOERR) {
delete valp;
return 0;
}
return valp;
}
示例7: test
int TestSuite::testVar()
{
try
{
string FILE_NAME = "tst_vars.nc";
int NDIMS = 4;
int NLAT = 6;
int NLON = 12;
// Names of things.
string LAT_NAME = "latitude";
string LON_NAME = "longitude";
int MAX_ATT_LEN = 80;
// These are used to construct some example data.
float START_LAT = 25.0;
float START_LON = -125.0;
string UNITS = "units";
string DEGREES_EAST = "degrees_east";
string DEGREES_NORTH = "degrees_north";
// For the units attributes.
string LAT_UNITS = "degrees_north";
string LON_UNITS = "degrees_east";
// Return this code to the OS in case of failure.
#define NC_ERR 2
// We will write latitude and longitude fields.
float lats[NLAT],lons[NLON];
// create some pretend data. If this wasn't an example program, we
// would have some real data to write for example, model output.
for (int lat = 0; lat < NLAT; lat++)
lats[lat] = START_LAT + 5. * lat;
for (int lon = 0; lon < NLON; lon++)
lons[lon] = START_LON + 5. * lon;
// Create the file.
NcFile test(FILE_NAME, NcFile::Replace);
// Define the dimensions. NetCDF will hand back an ncDim object for
// each.
NcDim* latDim = test.addDim(LAT_NAME, NLAT);
NcDim* lonDim = test.addDim(LON_NAME, NLON);
// Define the coordinate variables.
NcVar* latVar = test.addVar(LAT_NAME, ncFloat, latDim);
NcVar* lonVar = test.addVar(LON_NAME, ncFloat, lonDim);
// Define units attributes for coordinate vars. This attaches a
// text attribute to each of the coordinate variables, containing
// the units.
latVar->addAtt(UNITS,ncString, DEGREES_NORTH);
lonVar->addAtt(UNITS,ncString, DEGREES_EAST);
// Write the coordinate variable data to the file.
latVar->put(lats, NLAT);
lonVar->put(lons, NLON);
NcValues *latVals = latVar->getValues();
cout<<"toString returns lats: "<<latVals->toString()<<endl;
cout<<"toChar returns "<<latVals->toChar(1)<<endl;
cout<<"toShort returns "<<latVals->toShort(1)<<endl;
cout<<"toInt returns "<<latVals->toInt(1)<<endl;
cout<<"toLong returns "<<latVals->toLong(1)<<endl;
latVals->print(cout);
NcValues *lonVals = lonVar->getValues();
cout<<"toString returns lats: "<<lonVals->toString()<<endl;
lonVals->print(cout);
cout<<"no segmentation fault thus far"<<endl;
//test varaibles here
}
catch(NcException e)
{
e.what();
return 1;
}
try
{
cout<<"should test adding a variable with more than 5 dimensions here"<<endl;
// test creating a variable with more than 5 dimensions
}
catch (NcException e)
{
e.what();
return 1;
}
//.........这里部分代码省略.........
示例8: NcValues
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++) {
//.........这里部分代码省略.........
示例9: strdup
vtkDataArray *
avtS3DFileFormat::GetVar(int timeState, int domain, const char *varname)
{
debug5 << "avtS3DFileFormat::GetVar( timeState=" << timeState << ", domain="
<< domain << ", varname=" << varname << ")" << endl;
// Calculate the timestep directory that the data lives in.
char *pathcopy = strdup(mainFilename);
string dir = parse_dirname(pathcopy);
string timestepDir = CreateStringFromDouble(fileTimes[timeState]);
debug4 << "Timestep directory is <" << timestepDir << ">" << endl;
// Figure out how big this piece is.
CalculateSubpiece(domain);
// Open up the NetCDF file.
char path[256];
SNPRINTF(path,256,"%s%s%s%sfield.%05d",dir.c_str(),VISIT_SLASH_STRING, timestepDir.c_str(), VISIT_SLASH_STRING, domain);
debug5 << "avtS3DFileFormat::GetVar: Full path to data file is " << path << endl;
NcFile nf(path);
if (!nf.is_valid())
{
debug1 << nc_strerror(NcError().get_err()) << endl;
EXCEPTION1(InvalidFilesException, path);
}
debug5 << "avtS3DFileFormat::GetVar: Got valid file." << endl;
// Pull out the appropriate variable.
NcVar *v = nf.get_var(varname);
if (!v)
{
debug1 << nc_strerror(NcError().get_err()) << endl;
EXCEPTION1(InvalidVariableException, varname);
}
// Check if it fits the size of the mesh. Always node-centered, remember.
int ntuples = localDims[0] * localDims[1] * localDims[2];
debug5 << "ntuples:" << ntuples << endl;
int nvals = v->num_vals();
if (ntuples != nvals)
{
debug1 << "The variable " << v->name() <<
" does not conform to its mesh (" << nvals << " != " <<
ntuples << ")" << endl;
EXCEPTION1(InvalidVariableException, v->name());
}
// Set up the VTK dataset.
vtkFloatArray *rv = vtkFloatArray::New();
rv->SetNumberOfTuples(ntuples);
float *p = (float*)rv->GetVoidPointer(0);
NcValues *input = v->values();
if (!input)
{
debug1 << nc_strerror(NcError().get_err()) << endl;
EXCEPTION1(InvalidVariableException, v->name());
}
// Get the scaling factor.
NcAtt *scaling = v->get_att(NcToken("scale_factor"));
float scaling_factor = 1;
if (scaling)
{
scaling_factor = scaling->as_float(0);
debug5 << "avtS3DFileFormat::GetVar: Set the scaling factor as " << scaling_factor << endl;
}
// Process the variable into the returned data.
float *base = (float*)input->base();
for(int i=0;i<ntuples;i++)
{
p[i] = *(base + i) * scaling_factor;
}
return rv;
}