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; }
bool NetcdfSource::initFile() { _ncfile = new NcFile(_filename.toUtf8().data(), NcFile::ReadOnly); if (!_ncfile->is_valid()) { qDebug() << _filename << ": failed to open in initFile()" << endl; return false; } KST_DBG qDebug() << _filename << ": building field list" << endl; _fieldList.clear(); _fieldList += "INDEX"; int nb_vars = _ncfile->num_vars(); KST_DBG qDebug() << nb_vars << " vars found in total" << endl; _maxFrameCount = 0; for (int i = 0; i < nb_vars; i++) { NcVar *var = _ncfile->get_var(i); if (var->num_dims() == 0) { _scalarList += var->name(); } else if (var->num_dims() == 1) { _fieldList += var->name(); int fc = var->num_vals() / var->rec_size(); _maxFrameCount = qMax(_maxFrameCount, fc); _frameCounts[var->name()] = fc; } else if (var->num_dims() == 2) { _matrixList += var->name(); } } // Get strings int globalAttributesNb = _ncfile->num_atts(); for (int i = 0; i < globalAttributesNb; ++i) { // Get only first value, should be enough for a start especially as strings are complete NcAtt *att = _ncfile->get_att(i); if (att) { QString attrName = QString(att->name()); char *attString = att->as_string(0); QString attrValue = QString(att->as_string(0)); delete[] attString; //TODO port //KstString *ms = new KstString(KstObjectTag(attrName, tag()), this, attrValue); _stringList += attrName; } delete att; } // TODO update(); // necessary? slows down initial loading return true; }
//YUAN: recid - the order (from ZERO) in the .nc file, chtid - the cohort id int SiteinInputer::getRecID(const int &siteid){ NcError err(NcError::silent_nonfatal); NcFile siteFile(siteinfname.c_str(), NcFile::ReadOnly); NcVar* siteidV = siteFile.get_var("CHTID"); int id = -1; for (int i=0; i<(int)siteidV->num_vals(); i++){ siteidV->set_cur(i); siteidV->get(&id, 1); if(id==siteid) return i; } return -1; }
Kst::Object::UpdateType NetcdfSource::internalDataSourceUpdate() { //TODO port /* if (KstObject::checkUpdateCounter(u)) { return lastUpdateResult(); } */ _ncfile->sync(); bool updated = false; /* Update member variables _ncfile, _maxFrameCount, and _frameCounts and indicate that an update is needed */ int nb_vars = _ncfile->num_vars(); for (int j = 0; j < nb_vars; j++) { NcVar *var = _ncfile->get_var(j); int fc = var->num_vals() / var->rec_size(); _maxFrameCount = qMax(_maxFrameCount, fc); updated = updated || (_frameCounts[var->name()] != fc); _frameCounts[var->name()] = fc; } return updated ? Object::Updated : Object::NoChange; }
RadarData_t ReadRadarFile(const string &filename, const float latmin, const float latmax, const float lonmin, const float lonmax) { RadarData_t inputData; // Sets the internal netcdf error handling to nonfatal for this scope NcError error_handler(NcError::verbose_nonfatal); NcFile radarFile(filename.c_str()); if (!radarFile.is_valid()) { cerr << "ERROR: Could not open radar file: " << filename << " for reading.\n"; // Error is indicated by the lack of initialization of // the filename member of the struct. return(inputData); } NcVar* latVar = radarFile.get_var("lat"); if (NULL == latVar) { cerr << "ERROR: invalid data file. No variable called 'lat'!\n"; radarFile.close(); return(inputData); } long latCnt = latVar->num_vals(); double* latVals = new double[latCnt]; latVar->get(latVals, latCnt); inputData.latUnits = GrabAttribute(latVar, 0); inputData.latSpacing = strtod(GrabAttribute(latVar, 1).c_str(), NULL); const long minLatIndex = lower_bound(latVals, latmin, latCnt); const long maxLatIndex = upper_bound(latVals, latmax, latCnt); delete latVals; latCnt = (maxLatIndex - minLatIndex) + 1; latVar->set_cur(minLatIndex); inputData.latVals = new double[latCnt]; latVar->get(inputData.latVals, latCnt); NcVar* lonVar = radarFile.get_var("lon"); if (NULL == lonVar) { cerr << "ERROR: invalid data file. No variable called 'lon'!\n"; radarFile.close(); return(inputData); } long lonCnt = lonVar->num_vals(); double* lonVals = new double[lonCnt]; lonVar->get(lonVals, lonCnt); inputData.lonUnits = GrabAttribute(lonVar, 0); inputData.lonSpacing = strtod(GrabAttribute(lonVar, 1).c_str(), NULL); const long minLonIndex = lower_bound(lonVals, lonmin, lonCnt); const long maxLonIndex = upper_bound(lonVals, lonmax, lonCnt); delete lonVals; lonCnt = (maxLonIndex - minLonIndex) + 1; lonVar->set_cur(minLonIndex); inputData.lonVals = new double[lonCnt]; lonVar->get(inputData.lonVals, lonCnt); NcVar* reflectVar = NULL; reflectVar = radarFile.get_var("value"); if ( reflectVar == NULL ) { // Try this variable name reflectVar = radarFile.get_var("Reflectivity"); } if (reflectVar == NULL) { cerr << "ERROR: invalid data file. No variable called 'value'!\n"; radarFile.close(); return(inputData); } inputData.dataEdges = reflectVar->edges(); // [0] - time, [1] - lat, [2] - lon inputData.dataEdges[1] = latCnt; inputData.dataEdges[2] = lonCnt; inputData.dataVals = new double[inputData.dataEdges[0] * inputData.dataEdges[1] * inputData.dataEdges[2]]; reflectVar->set_cur(0, minLatIndex, minLonIndex); reflectVar->get(inputData.dataVals, inputData.dataEdges); inputData.var_LongName = GrabAttribute(reflectVar, 0); inputData.var_Units = "dBZ";//GrabAttribute(reflectVar, 1); NcVar* timeVar = radarFile.get_var("time"); if (NULL == timeVar) { cerr << "ERROR: invalid data file. No variable called 'time'!\n"; radarFile.close(); return(inputData); } inputData.scanTime = timeVar->as_long(0); inputData.timeUnits = GrabAttribute(timeVar, 0); NcAtt* titleAttrib = radarFile.get_att("title"); inputData.fileTitle = (NULL == titleAttrib ? "" : titleAttrib->as_string(0)); delete titleAttrib; radarFile.close(); // Success! inputData.inputFilename = filename; return(inputData); }
const size_t nvals( void ) const { return( static_cast<size_t>( m_var->num_vals() ) ); }
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++) { record = var->get_rec(i+s); for (int j = 0; j < recSize; j++) { v[i*recSize + j] = record->as_double(j); } delete record; } } } break; default: KST_DBG qDebug() << field << ": wrong datatype for kst, no values read" << endl; return -1; break; } KST_DBG qDebug() << "Finished reading " << field << endl; return oneSample ? 1 : n * recSize; }
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; }
void avtS3DFileFormat::PopulateDatabaseMetaData(avtDatabaseMetaData *md, int timeState) { debug5 << "avtS3DFileFormat::PopulateDatabaseMetaData" << endl; // Get the metadata from the log file first. OpenLogFile(); // Mesh avtMeshMetaData *mesh = new avtMeshMetaData; mesh->name = "mesh"; mesh->meshType = AVT_RECTILINEAR_MESH; mesh->numBlocks = procs[0] * procs[1] * procs[2]; mesh->blockOrigin = 1; mesh->cellOrigin = 0; mesh->spatialDimension = 3; mesh->topologicalDimension = 3; mesh->blockTitle = "blocks"; mesh->blockPieceName = "block"; mesh->hasSpatialExtents = false; mesh->xUnits = "mm"; mesh->yUnits = "mm"; mesh->zUnits = "mm"; md->Add(mesh); // // Look in the NetCDF file for the first block for the list of variables. // // Calculate the timestep directory that the data lives in. char *pathcopy = strdup(mainFilename); string dir = parse_dirname(pathcopy); string timestepDir = CreateStringFromDouble(fileTimes[timeState]); char path[256]; SNPRINTF(path,256,"%s%s%s%sfield.00000",dir.c_str(),VISIT_SLASH_STRING, timestepDir.c_str(), VISIT_SLASH_STRING); NcError err(NcError::verbose_nonfatal); NcFile nf(path); if (!nf.is_valid()) { EXCEPTION1(InvalidFilesException, path); } debug5 << "avtS3DFileFormat::PopulateDatabaseMetaData: Got valid file" << endl; int nvars = nf.num_vars(); debug5 << "avtS3DFileFormat::PopulateDatabaseMetaData: Found " << nvars << " variables" << endl; for (int i=0 ; i<nvars; i++) { NcVar *v = nf.get_var(i); if (!v) continue; debug4 << "Found variable " << v->name() << endl; // Check dimensionality int nvals = v->num_vals(); if (nvals != 1) // Single scalars are useless. { avtScalarMetaData *scalar = new avtScalarMetaData(); scalar->name = v->name(); scalar->meshName = "mesh"; scalar->centering = AVT_NODECENT; scalar->hasDataExtents = false; scalar->treatAsASCII = false; NcAtt *units = v->get_att(NcToken("units")); if (units) { long nv = units->num_vals(); if (nv == 0) { scalar->hasUnits = false; } else { char *unitString = units->as_string(0); scalar->units = unitString; scalar->hasUnits = true; } } else scalar->hasUnits = false; md->Add(scalar); } else { debug4 << "Unable to process variable " << v->name() << " since it is a single scalar" << endl; } } #if 0 // Expressions Expression tempGradient_expr; tempGradient_expr.SetName("Temperature_gradient"); tempGradient_expr.SetDefinition("gradient(Temperature)"); tempGradient_expr.SetType(Expression::VectorMeshVar); tempGradient_expr.SetHidden(true); md->AddExpression(&tempGradient_expr); Expression tempUnit_expr; tempUnit_expr.SetName("Temperature_grad_unit"); //tempUnit_expr.SetDefinition("(Temperature_gradient + {1e-6,0,0})/(magnitude(Temperature_gradient) + 1e-6)"); //tempUnit_expr.SetDefinition("Temperature_gradient/(magnitude(Temperature_gradient) + 1e-6)"); tempUnit_expr.SetDefinition("normalize(Temperature_gradient)"); tempUnit_expr.SetType(Expression::VectorMeshVar); tempUnit_expr.SetHidden(true); md->AddExpression(&tempUnit_expr); Expression tempCurv_expr; tempCurv_expr.SetName("Temperature_curvature"); tempCurv_expr.SetDefinition("divergence(Temperature_grad_unit)"); tempCurv_expr.SetType(Expression::ScalarMeshVar); tempUnit_expr.SetHidden(false); md->AddExpression(&tempCurv_expr); #endif }