本文整理汇总了C++中NcVar类的典型用法代码示例。如果您正苦于以下问题:C++ NcVar类的具体用法?C++ NcVar怎么用?C++ NcVar使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NcVar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: writeResultsFile
//TODO optimize this thing
void writeResultsFile(string dataFileName, string yDimension, vector<double> * pValues)
{
NcFile file(dataFileName.c_str(), NcFile::ReadOnly);
NcVar *geneNames = file.get_var("gene");
int dataYsize = file.get_dim(yDimension.c_str())->size();
FILE * pFile;
pFile = fopen ("results.txt","w");
cout << "Writing results to file.." << endl;
int percentage = dataYsize / 10;
int percentageCount = 10;
for(int i = 0; i < dataYsize; i++){
char * c = geneNames->as_string(i*28);
std::string str;
if(pValues->at(i) > 0.05){
std::ostringstream strs;
strs << pValues->at(i);
str = strs.str();
}else{
str = "NA";
}
fprintf (pFile, "%-20s-%20s", c, str.c_str());
fprintf (pFile, "\n");
if(i == percentage)
{
cout << percentageCount << "% complete" << endl;
percentageCount += 10;
percentage += dataYsize / 10;
}
delete c;
}
fclose(pFile);
}
示例2: getVar
FieldPtr FileArome::getFieldCore(std::string iVariable, int iTime) const {
// Not cached, retrieve data
NcVar* var = getVar(iVariable);
int nLat = mNLat;
int nLon = mNLon;
int numDims = var->num_dims();
long* count;
long totalCount = nLat*nLon;
if(numDims == 4) {
// Variable has a surface dimension
count = new long[4];
count[0] = 1;
count[1] = 1;
count[2] = nLat;
count[3] = nLon;
var->set_cur(iTime, 0, 0, 0);
}
else if(numDims == 3) {
count = new long[3];
count[0] = 1;
count[1] = nLat;
count[2] = nLon;
var->set_cur(iTime, 0, 0);
}
else {
std::stringstream ss;
ss << "Cannot read variable '" << iVariable << "' from '" << getFilename() << "'";
Util::error(ss.str());
}
float* values = new float[nLat*nLon];
var->get(values, count);
float MV = getMissingValue(var);
float offset = getOffset(var);
float scale = getScale(var);
int index = 0;
FieldPtr field = getEmptyField();
for(int lat = 0; lat < nLat; lat++) {
for(int lon = 0; lon < nLon; lon++) {
float value = values[index];
assert(index < totalCount);
if(value == MV) {
// Field has missing value indicator and the value is missing
// Save values using our own internal missing value indicator
value = Util::MV;
}
else {
value = scale*values[index] + offset;
}
(*field)(lat,lon,0) = value;
index++;
}
}
delete[] values;
delete[] count;
return field;
}
示例3: readScalar
int NetcdfSource::readScalar(double *v, const QString& field)
{
// TODO error handling
QByteArray bytes = field.toLatin1();
NcVar *var = _ncfile->get_var(bytes.constData()); // var is owned by _ncfile
var->get(v);
return 1;
}
示例4: setVariableSelect
void NetCdfConfigureDialog::setVariableSelect()
{
for (int i=0; i<(_currentFile->num_vars()); i++)
{
NcVar *focusedVar = _currentFile->get_var(i);
if (focusedVar->num_dims() > 0) comboBoxVariable->addItem(focusedVar->name());
}
}
示例5: Var
Var( const NcVar* var ):
m_var( var )
{
const int ndims = m_var->num_dims();
for ( int i = 0; i < ndims; i++ )
{
m_dims.push_back( new Dim( m_var->get_dim(i) ) );
}
}
示例6: read_vector
std::vector<T> read_vector(NcFile &nc, std::string const &var_name)
{
// Read points vector
NcVar *vpoints = nc.get_var(var_name.c_str());
long npoints = vpoints->get_dim(0)->size();
std::vector<T> points(npoints);
vpoints->get(&points[0], npoints);
return points;
}
示例7: dumpdata
void DumpableNcFile::dumpdata( )
{
NcVar* vp;
for (int n = 0; vp = get_var(n); n++) {
cout << " " << vp->name() << " = ";
NcValues* vals = vp->values();
cout << *vals << " ;" << endl ;
delete vals;
}
}
示例8: assert
bool ReadAstro::open(const char *path)
{
assert(!m_ncfile);
m_ncfile = new NcFile(path, NcFile::ReadOnly);
if (!m_ncfile->is_valid())
{
close();
sendError("failed to open NetCDF file %s", path);
return false;
}
if (m_ncfile->get_format() == NcFile::BadFormat)
{
close();
sendError("bad NetCDF file");
return false;
}
fprintf(stderr, "dims=%d, vars=%d, attrs=%d\n",
m_ncfile->num_dims(), m_ncfile->num_vars(), m_ncfile->num_atts());
for (int i = 0; i < m_ncfile->num_dims(); ++i)
{
fprintf(stderr, "%s: %ld\n",
m_ncfile->get_dim(i)->name(),
m_ncfile->get_dim(i)->size());
}
for (int i = 0; i < m_ncfile->num_vars(); ++i)
{
fprintf(stderr, "%s: dims=%d atts=%d vals=%ld type=%d\n",
m_ncfile->get_var(i)->name(),
m_ncfile->get_var(i)->num_dims(),
m_ncfile->get_var(i)->num_atts(),
m_ncfile->get_var(i)->num_vals(),
m_ncfile->get_var(i)->type());
//int dims = m_ncfile->get_var(i)->num_dims();
NcVar *var = m_ncfile->get_var(i);
for (int j = 0; j < var->num_dims(); ++j)
{
fprintf(stderr, " %s: %ld edge=%ld\n",
var->get_dim(j)->name(),
var->get_dim(j)->size(),
var->edges()[j]);
}
}
for (int i = 0; i < m_ncfile->num_atts(); ++i)
{
fprintf(stderr, "%s\n", m_ncfile->get_att(i)->name());
}
return true;
}
示例9:
vector<string>
eavlNetCDFImporter::GetFieldList(const string &mesh)
{
vector<string> retval;
for (unsigned int v=0; v<vars.size(); v++)
{
NcVar *var = vars[v];
retval.push_back(var->name());
}
return retval;
}
示例10: samplesPerFrame
int NetcdfSource::samplesPerFrame(const QString& field) {
if (field.toLower() == "index") {
return 1;
}
QByteArray bytes = field.toLatin1();
NcVar *var = _ncfile->get_var(bytes.constData());
if (!var) {
return 0;
}
return var->rec_size();
}
示例11: getTimeStep
int NetCdfConfigureDialog::getTimeStep()
{
NcVar* timeVar = _currentFile->get_var(comboBoxDim2->currentIndex());
const double datesToMinutes = convertDateToMinutes(_currentInitialDateTime,dateTimeEditDim3->date(),dateTimeEditDim3->time());
double timeArray[1] = {datesToMinutes};
double currentTime = timeVar->get_index(timeArray);
if (currentTime < 0) currentTime=0; //if the value isn't found in the array, set it to 0 as default...
return currentTime;
}
示例12: dumpatts
void dumpatts(NcVar& var)
{
NcToken vname = var.name();
NcAtt* ap;
for(int n = 0; ap = var.get_att(n); n++) {
cout << "\t\t" << vname << ":" << ap->name() << " = " ;
NcValues* vals = ap->values();
cout << *vals << " ;" << endl ;
delete ap;
delete vals;
}
}
示例13: main
int main(int argc, char *argv[])
{
NcFile at(atpath, NcFile::ReadOnly);
if(!at.is_valid() || at.num_dims() != 3 || at.num_vars() != 4) {
fprintf(stderr, "failed reading file: %s\n", atpath);
return 1;
}
NcVar* data = at.get_var("rhum");
if(!data->is_valid() || data->num_dims() != 3) {
fprintf(stderr, "rhum has incorrect dimensions");
return 1;
}
NcDim* time = data->get_dim(0);
int timecnt = time->size();
float *rhumd = new float[timecnt*LATS*LONS];
data->get(rhumd, timecnt, LATS, LONS);
float rhumdmon[12][LATS][LONS];
for(int i = 0; i<LATS; i++)
for(int j = 0; j<LONS; j++) {
float rhumdmoncnt[12];
for(int k = 0; k<12; k++) {
rhumdmon[k][i][j] = 0;
rhumdmoncnt[k] = 0;
}
for(int k = 0; k<timecnt; k++) {
double v = rhumd[(k*LATS+i)*LONS+j]*.1 + 3276.5;
if(v >= 0 && v <= 100) {
rhumdmon[k%12][i][j] += v;
rhumdmoncnt[k%12]++;
}
}
for(int k = 0; k<12; k++)
rhumdmon[k][i][j] /= rhumdmoncnt[k];
}
delete [] rhumd;
/* use a single byte instead of 2 to save memory,
resolution of 1/5th of a mm/day resolution */
uint8_t rhumbyte[12][LATS][LONS];
for(int i = 0; i<12; i++)
for(int j = 0; j<LATS; j++)
for(int k = 0; k<LONS; k++)
if(isnan(rhumdmon[i][j][k]) || fabs(rhumdmon[i][j][k]) > 100)
rhumbyte[i][j][k] = 255;
else
rhumbyte[i][j][k] = rhumdmon[i][j][k]*2.0;
fwrite(rhumbyte, sizeof rhumbyte, 1, stdout);
return 0;
}
示例14: read_from_netcdf
void ConstantSet::read_from_netcdf(NcFile &nc, std::string const &vname)
{
NcVar *ncvar = giss::get_var_safe(nc, vname.c_str(), true);
if (!ncvar) {
fprintf(stderr, "ConstantSet::read_from_netcdf() cannot find variable %s\n", vname.c_str());
throw std::exception();
}
int n = ncvar->num_atts();
// Read through the attributes, getting units and names separately
std::map<std::string, std::string> units;
std::map<std::string, double> consts;
std::map<std::string, std::string> descriptions;
for (int i=0; i<n; ++i) {
auto att = giss::get_att(ncvar, i);
std::string att_name(att->name());
if (giss::ends_with(att_name, "_description")) {
descriptions.insert(std::make_pair(
att_name.substr(0, att_name.size() - std::strlen("_description")),
std::string(att->as_string(0))));
} else if (giss::ends_with(att_name, "_units")) {
units.insert(std::make_pair(
att_name.substr(0, att_name.size() - std::strlen("_units")),
std::string(att->as_string(0))));
} else {
consts.insert(std::make_pair(
att_name, att->as_double(0)));
}
}
// Now go through them again, matching up constants and units
for (auto ii = consts.begin(); ii != consts.end(); ++ii) {
std::string const &name = ii->first;
double const val = ii->second;
auto ui = units.find(name);
if (ui == units.end()) {
fprintf(stderr, "Could not find _units attribute for %s\n", name.c_str());
}
auto di = descriptions.find(name);
if (di == descriptions.end()) {
fprintf(stderr, "Could not find _description attribute for %s\n", name.c_str());
}
std::string const &u = units.find(name)->second;
std::string const &d = descriptions.find(name)->second;
set(name, val, u, d);
}
}
示例15: main
int
main(void)
@{
// This is the data array we will write. It will just be filled
// with a progression of numbers for this example.
int dataOut[NX][NY];
// 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 i = 0; i < NX; i++)
for(int j = 0; j < NY; j++)
dataOut[i][j] = i * NY + j;
// Create the file. The Replace parameter tells netCDF to overwrite
// this file, if it already exists.
NcFile dataFile("simple_xy.nc", NcFile::Replace);
// You should always check whether a netCDF file creation or open
// constructor succeeded.
if (!dataFile.is_valid())
@{
cout << "Couldn't open file!\n";
return NC_ERR;
@}
// For other method calls, the default behavior of the C++ API is
// to exit with a message if there is an error. If that behavior
// is OK, there is no need to check return values in simple cases
// like the following.
// When we create netCDF dimensions, we get back a pointer to an
// NcDim for each one.
NcDim* xDim = dataFile.add_dim("x", NX);
NcDim* yDim = dataFile.add_dim("y", NY);
// Define a netCDF variable. The type of the variable in this case
// is ncInt (32-bit integer).
NcVar *data = dataFile.add_var("data", ncInt, xDim, yDim);
// Write the pretend data to the file. Although netCDF supports
// reading and writing subsets of data, in this case we write all
// the data in one operation.
data->put(&dataOut[0][0], NX, NY);
// The file will be automatically close when the NcFile object goes
// out of scope. This frees up any internal netCDF resources
// associated with the file, and flushes any buffers.
cout << "*** SUCCESS writing example file simple_xy.nc!" << endl;
return 0;
@}