void COORD::read_netcdf(PRMTOP* Mol, char* filename){ NcFile nc_mdcrd(filename, NcFile::ReadOnly); if (!nc_mdcrd.is_valid()){ printf("$ Could not open trajectory file %s. Please check.\n", filename); } NcDim* FrameDim = nc_mdcrd.get_dim("frame"); int size = FrameDim->size(); printf("# NetCDF Frame dimension: %d\n", size); NcDim* NDim = nc_mdcrd.get_dim("atom"); const int Ndim = NDim->size(); if (Ndim != Mol->N){ printf("# Mismatch among number of atoms in PRMTOP (%d) and NETCDF (%d) files. Please check.\n", Mol->N, Ndim); exit(1); } else { printf("# NetCDF number of atoms: %d\n", Ndim); } NcVar* nc_Coordinates = nc_mdcrd.get_var("coordinates"); double coords[Ndim][3]; printf("#%12s %12s %12s %12s\n", "Step", "Elec", "VDW", "Total"); for (int frame=1; frame <= size; frame++){ nc_Coordinates->get(&coords[0][0], 1, Ndim, 3); printf(" %12d ", frame); Energy->compute_nb2(Mol, coords, this->astart, this->aend, this->bstart, this->bend); nc_Coordinates->set_cur(frame); } }
void DumpableNcFile::dumpdims( void ) { for (int n=0; n < num_dims(); n++) { NcDim* dim = get_dim(n); cout << "\t" << dim->name() << " = " ; if (dim->is_unlimited()) cout << "UNLIMITED" << " ;\t " << "// " << dim->size() << " currently\n"; else cout << dim->size() << " ;\n"; } }
void LoadMetaDataFile( const std::string & strInputMeta, DataMatrix3D<int> & dataGLLNodes, DataMatrix3D<double> & dataGLLJacobian ) { NcFile ncMeta(strInputMeta.c_str(), NcFile::ReadOnly); NcDim * dimNp = ncMeta.get_dim("np"); if (dimNp == NULL) { _EXCEPTIONT("Dimension \"np\" missing from metadata file"); } NcDim * dimNelem = ncMeta.get_dim("nelem"); if (dimNelem == NULL) { _EXCEPTIONT("Dimension \"nelem\" missing from metadata file"); } NcVar * varGLLNodes = ncMeta.get_var("GLLnodes"); if (dimNelem == NULL) { _EXCEPTIONT("Variable \"GLLnodes\" missing from metadata file"); } NcVar * varGLLJacobian = ncMeta.get_var("J"); if (dimNelem == NULL) { _EXCEPTIONT("Variable \"J\" missing from metadata file"); } int nP = dimNp->size(); int nElem = dimNelem->size(); DataMatrix3D<int> dataGLLNodes_tmp; DataMatrix3D<double> dataGLLJacobian_tmp; dataGLLNodes.Initialize(nP, nP, nElem); dataGLLJacobian.Initialize(nP, nP, nElem); dataGLLNodes_tmp.Initialize(nP, nP, nElem); dataGLLJacobian_tmp.Initialize(nP, nP, nElem); varGLLNodes->get(&(dataGLLNodes_tmp[0][0][0]), nP, nP, nElem); varGLLJacobian->get(&(dataGLLJacobian_tmp[0][0][0]), nP, nP, nElem); for (int i = 0; i < nP; i++) { for (int j = 0; j < nP; j++) { for (int k = 0; k < nElem; k++) { dataGLLNodes[i][j][k] = dataGLLNodes_tmp[j][i][k]; dataGLLJacobian[i][j][k] = dataGLLJacobian_tmp[j][i][k]; } } } }
FileArome::FileArome(std::string iFilename, bool iReadOnly) : FileNetcdf(iFilename, iReadOnly) { // Set dimensions NcDim* dTime = getDim("time"); NcDim* dLon = getDim("x"); NcDim* dLat = getDim("y"); mNTime = dTime->size(); mNLat = dLat->size(); mNLon = dLon->size(); mNEns = 1; mLats = getLatLonVariable("latitude"); mLons = getLatLonVariable("longitude"); if(hasVariableCore("surface_geopotential")) { FieldPtr elevField = getFieldCore("surface_geopotential", 0); mElevs.resize(getNumLat()); for(int i = 0; i < getNumLat(); i++) { mElevs[i].resize(getNumLon()); for(int j = 0; j < getNumLon(); j++) { float value = (*elevField)(i,j,0) / 9.81; mElevs[i][j] = value; } } std::cout << "Deriving altitude from geopotential height in " << getFilename() << std::endl; } else { mElevs = getLatLonVariable("altitude"); } if(hasVar("time")) { NcVar* vTime = getVar("time"); double* times = new double[mNTime]; vTime->get(times , mNTime); setTimes(std::vector<double>(times, times+mNTime)); delete[] times; } else { std::vector<double> times; times.resize(getNumTime(), Util::MV); setTimes(times); } if(hasVar("forecast_reference_time")) { NcVar* vReferenceTime = getVar("forecast_reference_time"); double referenceTime = getReferenceTime(); vReferenceTime->get(&referenceTime, 1); setReferenceTime(referenceTime); } Util::status( "File '" + iFilename + " 'has dimensions " + getDimenionString()); }
void InputRdaNetcdf::getLocationsCore(std::vector<Location>& iLocations) const { std::string filename = getLocationFilename(); NcFile ncfile(filename.c_str()); assert(ncfile.is_valid()); NcVar* ncLats = ncfile.get_var("latitude"); NcVar* ncLons = ncfile.get_var("longitude"); NcVar* ncElevs = ncfile.get_var("altitude"); NcVar* ncNames = ncfile.get_var("station_id"); NcDim* ncNamesDim = ncfile.get_dim("id_len"); int namesLength = ncNamesDim->size(); NcDim* ncLocationDim = ncfile.get_dim("station"); int numLocations = ncLocationDim->size(); float* lats = new float[numLocations]; float* lons = new float[numLocations]; float* elevs = new float[numLocations]; char* names = new char[numLocations*namesLength]; long count[2] = {numLocations, namesLength}; ncLats->get(lats, count); ncLons->get(lons, count); ncElevs->get(elevs, count); ncNames->get(names, count); iLocations.resize(numLocations); for(int i = 0; i < numLocations; i++) { int id = i; float lat = lats[i]; float lon = lons[i]; float elev = elevs[i]; Location loc(getName(), id, lat, lon); loc.setElev(elev); iLocations[i] = loc; int nameIndex = i*namesLength; std::string name = std::string(&names[nameIndex], namesLength); mLocationNames[name] = i; } delete[] lats; delete[] lons; delete[] elevs; delete[] names; ncfile.close(); }
void InputNetcdf::getLocationsCore(std::vector<Location>& iLocations) const { iLocations.clear(); std::string filename = getLocationFilename(); NcFile ncfile(filename.c_str()); if(ncfile.is_valid()) { NcDim* ncLocationDim = ncfile.get_dim("Location"); int numLocations = ncLocationDim->size(); NcVar* ncLats = ncfile.get_var("Lat"); NcVar* ncLons = ncfile.get_var("Lon"); NcError q(NcError::silent_nonfatal); NcVar* ncElevs = ncfile.get_var("Elev"); NcVar* ncIds = ncfile.get_var("Id"); bool hasId = false; bool hasElev = false; if(ncIds) hasId = true; if(ncElevs) hasElev = true; float* lats = new float[numLocations]; float* lons = new float[numLocations]; float* elevs = new float[numLocations]; int* ids = new int[numLocations]; long count[1] = {numLocations}; ncLats->get(lats, count); ncLons->get(lons, count); if(hasId) ncIds->get(ids, count); if(hasElev) ncElevs->get(elevs, count); for(int i = 0; i < numLocations; i++) { int id = i; if(hasId) id = ids[i]; float lat = lats[i]; float lon = lons[i]; float elev = Global::MV; if(hasElev) elev = elevs[i]; Location loc(getName(), id, lat, lon); loc.setElev(elev); iLocations.push_back(loc); } delete[] lats; delete[] lons; delete[] elevs; delete[] ids; ncfile.close(); } else { notifyInvalidSampleFile(); } }
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; }
static int load_nc_dim(const NcFile& ncf, const string& name, bool required = true) { NcDim* d = 0; try { d = ncf.get_dim(name.c_str()); } catch(char* str) { check(!required, string(str) + "\ndimension " + name + " not found in netcdf file"); } int size = d ? d->size() : 0; return size; }
void Regioner::createCohorList4Run(){ // read in a list of cohorts to run //netcdf error NcError err(NcError::silent_nonfatal); //open file and check if valid string filename = md.runchtfile; NcFile runFile(filename.c_str(), NcFile::ReadOnly); if(!runFile.is_valid()){ string msg = filename+" is not valid"; char* msgc = const_cast< char* > ( msg.c_str()); throw Exception(msgc, I_NCFILE_NOT_EXIST); } NcDim* chtD = runFile.get_dim("CHTID"); if(!chtD->is_valid()){ string msg="CHT Dimension is not valid in createCohortList4Run"; char* msgc = const_cast<char*> (msg.c_str()); throw Exception(msgc, I_NCDIM_NOT_EXIST); } NcVar* chtV = runFile.get_var("CHTID"); if(chtV==NULL){ string msg="Cannot get CHTID in createCohortList4Run "; char* msgc = const_cast<char*> (msg.c_str()); throw Exception(msgc, I_NCVAR_NOT_EXIST); } int numcht = chtD->size(); int chtid = -1; int chtid0 = -1; int chtidx = -1; for (int i=0; i<numcht; i++){ chtV->set_cur(i); chtV->get(&chtid, 1); runchtlist.push_back(chtid); if (i==0) chtid0=chtid; if (i==numcht-1) chtidx=chtid; } cout <<md.casename << ": " <<numcht <<" cohorts to be run @" <<md.runstages<< "\n"; cout <<" from: " <<chtid0<<" to: " <<chtidx <<"\n"; };
CCatchmentSetupParams::CCatchmentSetupParams(const std::string & szFilenameForCatchment) { if(!boost::filesystem::exists(szFilenameForCatchment)) throw "szFilenameForCatchment doesn't exist"; pCatchmentDescriptionFile = new NcFile(szFilenameForCatchment.c_str()); if (!pCatchmentDescriptionFile->is_valid()) { throw "Couldn't open file!"; } NcVar * pReachIDs = pCatchmentDescriptionFile->get_var("rchid"); NcDim* numReaches = pReachIDs->get_dim(0); const int nNumReaches = numReaches->size(); cout << "numReaches=" << nNumReaches << endl; //std::vector<int> aReachIDs(nNumReaches, -1); int anReaches[nNumReaches]; pReachIDs->get(anReaches, nNumReaches); //This is the mapping from rchid to nrch (=idx) for(int nrch=0;nrch<nNumReaches;nrch++) { std::cout << "Reach: " << anReaches[nrch] << std::endl; aSubcatchments[nrch] = new CSubcatchmentParams(nrch, anReaches[nrch], this); } int anDownstreamReaches[nNumReaches]; pCatchmentDescriptionFile->get_var("dsrch_nrch")->get(anDownstreamReaches, nNumReaches); for(int nrch=0;nrch<nNumReaches;nrch++) { if(anDownstreamReaches[nrch]>=0) { std::cout << "Catchment " << nrch << " Downstream reach: " << anDownstreamReaches[nrch] << std::endl; //aSubcatchments[nrch] = new CSubcatchmentParams(nrch, this); aSubcatchments[nrch]->setDownstreamCatchment(aSubcatchments[anDownstreamReaches[nrch]]); } } std::cout << std::endl; }
void InputNetcdf::getOffsetsCore(std::vector<float>& iOffsets) const { iOffsets.clear(); std::string filename = getSampleFilename(); NcFile ncfile(filename.c_str()); if(ncfile.is_valid()) { NcDim* ncOffsetDim = ncfile.get_dim("Offset"); int numOffsets = ncOffsetDim->size(); NcVar* ncOffsets = ncfile.get_var("Offset"); float* offsets = new float[numOffsets]; //float* elevs = new float[numOffsets]; long count[1] = {numOffsets}; ncOffsets->get(offsets, count); for(int i = 0; i < numOffsets; i++) { iOffsets.push_back(offsets[i]); } delete[] offsets; ncfile.close(); } else { notifyInvalidSampleFile(); } }
void InputNetcdf::getMembersCore(std::vector<Member>& iMembers) const { iMembers.clear(); std::string filename = getSampleFilename(); NcFile ncfile(filename.c_str()); if(ncfile.is_valid()) { NcDim* ncMemberDim = ncfile.get_dim("Member"); int numMembers = ncMemberDim->size(); NcVar* ncRes = ncfile.get_var("Resolution"); float* res = new float[numMembers]; long count[1] = {numMembers}; ncRes->get(res, count); for(int i = 0; i < numMembers; i++) { Member member(getName(), res[i], "", i); iMembers.push_back(member); } delete[] res; ncfile.close(); } else { notifyInvalidSampleFile(); } }
ERMsg C20thReanalysisProject::GetGridInfo(const CString& filePath, int& sizeX, int& sizeY, int& nbBand, CGeoRectWP& rect, float& noData, double& cellSizeX, double& cellSizeY) { ERMsg msg; // CString filePathIn; //filePathIn.Format("%scccma_cgcm3_1-20c3m-run1-pr-1961-2000_monthly.nc", m_path); NcError error( NcError::verbose_nonfatal ); NcFile file(filePath); if( !file.is_valid() ) { CString err; err.FormatMessage(IDS_CMN_UNABLE_OPEN_READ, filePath); msg.ajoute(err); return msg; } int nbDim = file.num_dims(); int nbAtt = file.num_atts(); int nbVar = file.num_vars(); //NcDim* dim0 = file.get_dim(0); //NcDim* dim1 = file.get_dim(1); //NcDim* dim2 = file.get_dim(2); //NcVar* pVarX = file.get_var("xc"); //NcVar* pVarY = file.get_var("yc"); NcDim* pDimX = file.get_dim("lon"); NcDim* pDimY = file.get_dim("lat"); NcDim* pDimTime = file.get_dim("time"); if( pDimX && pDimY && pDimTime) { sizeX = pDimX->size(); sizeY = pDimY->size(); nbBand = pDimTime->size(); //double offset = pVarElev->get_att("add_offset")->as_float(0); //double scaleFactor = pVarElev->get_att("scale_factor")->as_float(0); typedef vector<float> CDataArray; CDataArray Y(sizeY); CDataArray X(sizeX); NcVar* pVarX = file.get_var("lon"); NcVar* pVarY = file.get_var("lat"); pVarX->get(&(X[0]), sizeX); pVarY->get(&(Y[0]), sizeY); CStatistic Xstat; CStatistic Ystat; Xstat+=X[0];Xstat+=X[sizeX-1]; Ystat+=Y[0];Ystat+=Y[sizeY-1]; cellSizeX = (Xstat[HIGHEST]-Xstat[LOWEST])/(sizeX-1); cellSizeY = (Ystat[HIGHEST]-Ystat[LOWEST])/(sizeY-1); noData=-999; rect.SetPrj( CProjection( CProjection::GEO ) ); rect.m_xMin = Xstat[LOWEST]-cellSizeX/2-180; rect.m_yMin = Ystat[LOWEST]-cellSizeY/2; rect.m_xMax = rect.m_xMin+cellSizeX*sizeX; rect.m_yMax = rect.m_yMin+cellSizeY*sizeY; } else { msg.ajoute("Unable to find one of dimention \"lat, lon or time\""); } file.close(); return msg; }
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; }
int main(int argc, char ** argv){ NcError error(NcError::verbose_nonfatal); try{ std::string inFile; std::string outFile; std::string varName; std::string inList; // bool calcStdDev; BeginCommandLine() CommandLineString(inFile, "in", ""); CommandLineString(inList, "inlist",""); CommandLineString(varName, "var", ""); CommandLineString(outFile, "out", ""); // CommandLineBool(calcStdDev, "std"); ParseCommandLine(argc, argv); EndCommandLine(argv) AnnounceBanner(); if ((inFile != "") && (inList != "")){ _EXCEPTIONT("Can only open one file (--in) or list (--inlist)."); } //file list vector std::vector<std::string> vecFiles; if (inFile != ""){ vecFiles.push_back(inFile); } if (inList != ""){ GetInputFileList(inList,vecFiles); } //open up first file NcFile readin(vecFiles[0].c_str()); if (!readin.is_valid()){ _EXCEPTION1("Unable to open file %s for reading",\ vecFiles[0].c_str()); } int tLen,latLen,lonLen; NcDim * time = readin.get_dim("time"); tLen = time->size(); NcVar * timeVar = readin.get_var("time"); NcDim * lat = readin.get_dim("lat"); latLen = lat->size(); NcVar * latVar = readin.get_var("lat"); NcDim * lon = readin.get_dim("lon"); lonLen = lon->size(); NcVar * lonVar = readin.get_var("lon"); //read input variable NcVar * inVar = readin.get_var(varName.c_str()); //Create output matrix DataMatrix<double> outMat(latLen,lonLen); densCalc(inVar,outMat); //Option for calculating the yearly standard deviation /* if (calcStdDev){ for (int a=0; a<latLen; a++){ for (int b=0; b<lonLen; b++){ storeMat[0][a][b] = outMat[a][b]; } } } */ //If multiple files, add these values to the output if (vecFiles.size()>1){ DataMatrix<double> addMat(latLen,lonLen); std::cout<<"There are "<<vecFiles.size()<<" files."<<std::endl; for (int v=1; v<vecFiles.size(); v++){ NcFile addread(vecFiles[v].c_str()); NcVar * inVar = addread.get_var(varName.c_str()); densCalc(inVar,addMat); for (int a=0; a<latLen; a++){ for (int b=0; b<lonLen; b++){ outMat[a][b]+=addMat[a][b]; } } /* if (calcStdDev){ for (int a=0; a<latLen; a++){ for (int b=0; b<lonLen; b++){ storeMat[v][a][b] = addMat[a][b]; } } }*/ addread.close(); } //Divide output by number of files double div = 1./((double) vecFiles.size()); for (int a=0; a<latLen; a++){ for (int b=0; b<lonLen; b++){ outMat[a][b]*=div; } } } NcFile readout(outFile.c_str(),NcFile::Replace, NULL,0,NcFile::Offset64Bits); NcDim * outLat = readout.add_dim("lat", latLen); NcDim * outLon = readout.add_dim("lon", lonLen); NcVar * outLatVar = readout.add_var("lat",ncDouble,outLat); NcVar * outLonVar = readout.add_var("lon",ncDouble,outLon); std::cout<<"Copying dimension attributes."<<std::endl; copy_dim_var(latVar,outLatVar); copy_dim_var(lonVar,outLonVar); std::cout<<"Creating density variable."<<std::endl; NcVar * densVar = readout.add_var("dens",ncDouble,outLat,outLon); densVar->set_cur(0,0); densVar->put((&outMat[0][0]),latLen,lonLen); /* if (calcStdDev){ NcVar * stdDevVar = readout.add_var("stddev", ncDouble,outLat,outLon); DataMatrix<double> stdDevMat(latLen,lonLen); yearlyStdDev(storeMat,vecFiles.size(),latLen,lonLen,stdDevMat); stdDevVar->set_cur(0,0); stdDevVar->put(&(stdDevMat[0][0]),latLen,lonLen); std::cout<<" created sd variable"<<std::endl; } */ readout.close(); readin.close(); } catch (Exception &e){ std::cout<<e.ToString()<<std::endl; } }
void PolygonManager::init() { string fileName; ConfigTools::read("parcel_polygon_file", fileName); NOTICE("PolygonManager::init", "Reading polygons from \""+fileName+"\" ..."); NcFile file(fileName.c_str(), NcFile::ReadOnly); if (!file.is_valid()) { REPORT_ERROR(string("Failed to open file "+fileName+".")) } NcError ncError(NcError::silent_nonfatal); if (TimeManager::onLine()) { NcAtt *timeAtt = file.get_att("time"); NcAtt *timeStepAtt = file.get_att("time_step"); NcAtt *stepsAtt = file.get_att("steps"); if (timeAtt != NULL && timeStepAtt != NULL && stepsAtt != NULL) { TimeManager::reset(); double dt = timeStepAtt->as_double(0); double second = timeAtt->as_double(0); double steps = stepsAtt->as_int(0); TimeManager::setClock(dt, second, steps); } } NcDim *numVertexDim = file.get_dim("num_total_vertex"); if (numVertexDim == NULL) { Message message; message << "Failed to find \"num_total_vertex\" dimension in file \""; message << fileName << "\"!"; REPORT_ERROR(message.str()); } NcDim *numEdgeDim = file.get_dim("num_total_edge"); if (numEdgeDim == NULL) { Message message; message << "Failed to find \"num_total_edge\" dimension in file \""; message << fileName << "\"!"; REPORT_ERROR(message.str()); } NcDim *numPolygonDim = file.get_dim("num_total_polygon"); if (numPolygonDim == NULL) { Message message; message << "Failed to find \"num_total_polygon\" dimension in file \""; message << fileName << "\"!"; REPORT_ERROR(message.str()); } int numVertex = static_cast<int>(numVertexDim->size()); int numEdge = static_cast<int>(numEdgeDim->size()); int numPolygon = static_cast<int>(numPolygonDim->size()); // ------------------------------------------------------------------------- // vertices part vertices.create(numVertex); double *oldVtxLon = new double[numVertex]; double *oldVtxLat = new double[numVertex]; double *oldVtxLev = new double[numVertex]; double *newVtxLon = new double[numVertex]; double *newVtxLat = new double[numVertex]; double *newVtxLev = new double[numVertex]; file.get_var("old_vertex_lon")->get(oldVtxLon, numVertex); file.get_var("old_vertex_lat")->get(oldVtxLat, numVertex); file.get_var("new_vertex_lon")->get(newVtxLon, numVertex); file.get_var("new_vertex_lat")->get(newVtxLat, numVertex); if (file.get_var("old_vertex_lev") != NULL) { file.get_var("old_vertex_lev")->get(oldVtxLev, numVertex); file.get_var("new_vertex_lev")->get(newVtxLev, numVertex); } else { for (int i = 0; i < vertices.size(); ++i) { oldVtxLev[i] = 0.0; newVtxLev[i] = 0.0; } } // change the units from degree to rad for (int i = 0; i < numVertex; ++i) { oldVtxLon[i] /= Rad2Deg; oldVtxLat[i] /= Rad2Deg; newVtxLon[i] /= Rad2Deg; newVtxLat[i] /= Rad2Deg; } Vertex *vertexMap[vertices.size()]; Vertex *vertex = vertices.front(); for (int i = 0; i < vertices.size(); ++i) { vertexMap[i] = vertex; vertex->setCoordinate(newVtxLon[i], newVtxLat[i], newVtxLev[i], NewTimeLevel); vertex->setCoordinate(oldVtxLon[i], oldVtxLat[i], oldVtxLev[i], OldTimeLevel); vertex = vertex->next; } delete [] newVtxLon; delete [] newVtxLat; delete [] newVtxLev; delete [] oldVtxLon; delete [] oldVtxLat; delete [] oldVtxLev; // ------------------------------------------------------------------------- // edges part edges.create(numEdge); int *firstPoint = new int[numEdge]; int *secondPoint = new int[numEdge]; file.get_var("first_point_idx")->get(firstPoint, numEdge); file.get_var("second_point_idx")->get(secondPoint, numEdge); Edge *edgeMap[edges.size()]; Edge *edge = edges.front(); for (int i = 0; i < edges.size(); ++i) { edgeMap[i] = edge; edge->linkEndPoint(FirstPoint, vertexMap[firstPoint[i]-1]); edge->linkEndPoint(SecondPoint, vertexMap[secondPoint[i]-1]); edge->calcNormVector(); edge = edge->next; } delete [] firstPoint; delete [] secondPoint; // test point double *oldTestLon = new double[numEdge]; double *oldTestLat = new double[numEdge]; double *newTestLon = new double[numEdge]; double *newTestLat = new double[numEdge]; file.get_var("old_testpoint_lon")->get(oldTestLon, numEdge); file.get_var("old_testpoint_lat")->get(oldTestLat, numEdge); file.get_var("new_testpoint_lon")->get(newTestLon, numEdge); file.get_var("new_testpoint_lat")->get(newTestLat, numEdge); for (int i = 0; i < numEdge; ++i) { oldTestLon[i] /= Rad2Deg; oldTestLat[i] /= Rad2Deg; newTestLon[i] /= Rad2Deg; newTestLat[i] /= Rad2Deg; } edge = edges.front(); for (int i = 0; i < numEdge; ++i) { Vertex *testPoint = edge->getTestPoint(); testPoint->setCoordinate(oldTestLon[i], oldTestLat[i], OldTimeLevel); testPoint->setCoordinate(newTestLon[i], newTestLat[i], NewTimeLevel); edge = edge->next; } delete [] oldTestLon; delete [] oldTestLat; delete [] newTestLon; delete [] newTestLat; // ------------------------------------------------------------------------- // polygons part polygons.create(numPolygon); int edgeNum[numPolygon]; file.get_var("edge_num")->get(edgeNum, numPolygon); int numEdgeIdx = static_cast<int>(file.get_dim("num_edge_idx")->size()); int *edgeIdx = new int[numEdgeIdx]; int *edgeOnt = new int[numEdgeIdx]; file.get_var("edge_idx")->get(edgeIdx, numEdgeIdx); file.get_var("edge_ont")->get(edgeOnt, numEdgeIdx); Polygon *polygon = polygons.front(); int counter = 0; for (int i = 0; i < polygons.size(); ++i) { for (int j = 0; j < edgeNum[i]; ++j) { OrientStatus orient; if (edgeOnt[counter] == 0) { orient = OrientLeft; } else if (edgeOnt[counter] == 1) { orient = OrientRight; } else { string message = "Invalid edge_ont in file "+fileName+"."; REPORT_ERROR(message.c_str()); } edgeMap[edgeIdx[counter]-1]->linkPolygon(orient, polygon); counter++; } polygon->edgePointers.ring(); // calculate the angles EdgePointer *edgePointer = polygon->edgePointers.front(); for (int j = 0; j < polygon->edgePointers.size(); ++j) { edgePointer->calcAngle(); edgePointer = edgePointer->next; } polygon->calcArea(); polygon = polygon->next; } delete [] edgeIdx; delete [] edgeOnt; // ------------------------------------------------------------------------- file.close(); }
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])); varOut->put(&(data[0]), &(counts[0])); } } // ncInt type if (var->type() == ncInt) { DataVector<int> 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])); varOut->put(&(data[0]), &(counts[0])); } } // ncFloat type if (var->type() == ncFloat) { DataVector<float> 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])); varOut->put(&(data[0]), &(counts[0])); } } // ncDouble type if (var->type() == ncDouble) { DataVector<double> 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])); varOut->put(&(data[0]), &(counts[0])); } } // ncInt64 type if (var->type() == ncInt64) { DataVector<ncint64> 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])); varOut->put(&(data[0]), &(counts[0])); } } // Check output variable exists if (varOut == NULL) { _EXCEPTION1("Unable to create output variable \"%s\"", var->name()); } // Copy attributes if (fCopyAttributes) { CopyNcVarAttributes(var, varOut); } }
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) { maxdim = i; maxdimsize = dim->size(); } totallen *= dim->size(); } // TODO: support other data types totallen *= sizeof(float); // TODO: configurable page size const unsigned long pagesize = 1000000000; #ifdef __linux__ struct sysinfo info; sysinfo(&info); if (pagesize >= info.freeram) { std::cerr << "Warning: page size exceeds free memory" << std::endl; } #endif int numpages = 1; long pagesizedim = var->get_dim(maxdim)->size(); if (totallen < pagesize) { } else { long mul = 1; for (int i = 0; i < var->num_dims(); ++i) { if (i != maxdim) { NcDim* dim = var->get_dim(i); mul *= dim->size(); } } // TODO: support other data types mul *= sizeof(float); pagesizedim = pagesize / mul; numpages = var->get_dim(maxdim)->size() / pagesizedim; if (var->get_dim(maxdim)->size() % pagesizedim > 0) { ++numpages; } } std::vector< std::vector<long> > curvec; std::vector< std::vector<long> > countsvec; std::vector<long> lengths; int pages = numpages > 0 ? numpages : 1; for (int p = 0; p < pages; ++p) { long len = 1; std::vector<long> cur; std::vector<long> counts; for (int i = 0; i < var->num_dims(); ++i) { NcDim* dim = var->get_dim(i); long current = 0; long count = dim->size(); if (i == maxdim) { current = pagesizedim * p; count = pagesizedim; if (p == pages -1) { if (dim->size() % pagesizedim != 0) { count = dim->size() % pagesizedim; } } } cur.push_back(current); counts.push_back(count); len *= count; } curvec.push_back(cur); countsvec.push_back(counts); lengths.push_back(len); } std::vector< std::vector<long> >::const_iterator it1; std::vector< std::vector<long> >::const_iterator it2; std::vector<long>::const_iterator it3; for (it1 = curvec.begin(), it2 = countsvec.begin(), it3 = lengths.begin(); it1 != curvec.end() && it2 != countsvec.end() && it3 != lengths.end(); ++it1, ++it2, ++it3) { std::vector<long> cur = *it1; std::vector<long> counts = *it2; long len = *it3; var->set_cur(&cur[0]); outvar->set_cur(&cur[0]); switch (outvar->type()) { case ncByte: { ncbyte* barr = new ncbyte[len]; var->get(barr, &counts[0]); outvar->put(barr, &counts[0]); delete[] barr; break; } case ncChar: { char* carr = new char[len]; var->get(carr, &counts[0]); outvar->put(carr, &counts[0]); delete[] carr; break; } case ncShort: { short* sarr = new short[len]; var->get(sarr, &counts[0]); outvar->put(sarr, &counts[0]); delete[] sarr; break; } case ncInt: { long* larr = new long[len]; var->get(larr, &counts[0]); outvar->put(larr, &counts[0]); delete[] larr; break; } case ncFloat: { float* farr = new float[len]; var->get(farr, &counts[0]); outvar->put(farr, &counts[0]); delete[] farr; break; } case ncDouble: { double* darr = new double[len]; var->get(darr, &counts[0]); outvar->put(darr, &counts[0]); delete[] darr; break; } default: break; } } } infile.close(); outfile.close(); return 0; }
ERMsg CMADIS::ExtractLOC(const CString& filePath, CSCCallBack& callback) { ERMsg msg; callback.SetCurrentDescription("Extract location"); callback.SetCurrentStepRange(0, 3, 1); callback.SetStartNewStep(); CString outputFilePath(filePath); UtilWin::SetFileExtension( outputFilePath, ".loc"); // CString outputFilePathGrid(filePath); // UtilWin::SetFileExtension( outputFilePathGrid, ".bil"); // CShapeFileBase shapefile; // shapefile.Read("C:\\ouranos_data\\MapInput\\canada.shp"); NcFile file(filePath); if( !file.is_valid() ) { CString err; err.FormatMessage(IDS_CMN_UNABLE_OPEN_READ, filePath); msg.ajoute(err); } //CMapBinaryFile grid; // grid.SetProjection( GetDataGrid().GetPrj() ); // grid.SetNbCols(180); // grid.SetNbRows(172); // grid.SetBoundingBox(GetDataGrid()); // grid.SetNoData(-9999); // grid.SetCellSizeX( 45000 ); // grid.SetCellSizeY( 45000 ); CStdioFileEx fileOut; //msg += grid.Open(outputFilePathGrid, CGeoFileInterface::modeWrite); msg += fileOut.Open( outputFilePath, CFile::modeCreate|CFile::modeWrite); if(!msg) return msg; fileOut.WriteString("LOC_FILE 4 2,ID,Latitude,Longitude,Elevation,Slope(%),Orientation(°)\n"); //char providerId(recNum, maxProviderIdLen) ; // providerId:long_name = "Data Provider station Id" ; // providerId:reference = "station table" ; //char stationId(recNum, maxStaIdLen) ; // stationId:long_name = "alphanumeric station Id" ; // stationId:reference = "station table" ; //char handbook5Id(recNum, maxStaIdLen) ; // handbook5Id:long_name = "Handbook5 Id (AFOS or SHEF id)" ; // handbook5Id:reference = "station table" ; //char stationName(recNum, maxNameLength) ; // stationName:long_name = "alphanumeric station name" ; // stationName:reference = "station table" ; //float latitude(recNum) ; // latitude:long_name = "latitude" ; // latitude:units = "degree_north" ; // latitude:_FillValue = 3.4028235e+038f ; // latitude:missing_value = -9999.f ; // latitude:reference = "station table" ; // latitude:standard_name = "latitude" ; //float longitude(recNum) ; // longitude:long_name = "longitude" ; // longitude:units = "degree_east" ; // longitude:_FillValue = 3.4028235e+038f ; // longitude:missing_value = -9999.f ; // longitude:reference = "station table" ; // longitude:standard_name = "longitude" ; //float elevation(recNum) ; // elevation:long_name = "elevation" ; // elevation:units = "meter" ; // elevation:_FillValue = 3.4028235e+038f ; // elevation:missing_value = -9999.f ; // elevation:reference = "station table" ; // elevation:standard_name = "elevation" ; //NcVar* pVarX = file.get_var("xc"); //NcVar* pVarY = file.get_var("yc"); NcVar* pVarLat = file.get_var("staLat");//file.get_var("latitude"); NcVar* pVarLon = file.get_var("staLon");//file.get_var("longitude"); NcVar* pVarElev = file.get_var("staElev");//file.get_var("elevation"); NcDim* pDim = pVarLat->get_dim(0); //double offset = pVarElev->get_att("add_offset")->as_float(0); //double scaleFactor = pVarElev->get_att("scale_factor")->as_float(0); int nbRect = pDim->size(); //delete pDim; pDim = NULL; //typedef boost::multi_array<char, 1> CDataCharArray; typedef boost::multi_array<float, 1> CDataFloatArray; //CDataFloatArray X(boost::extents[180]); //CDataFloatArray Y(boost::extents[172]); CDataFloatArray lat(boost::extents[nbRect]); CDataFloatArray lon(boost::extents[nbRect]); CDataFloatArray elev(boost::extents[nbRect]); ASSERT( pVarLat->get_dim(0)->size() == pVarLon->get_dim(0)->size() ); ASSERT( pVarLat->get_dim(0)->size() == pVarElev->get_dim(0)->size() ); //pVarX->get(&(X[0]), 180); //pVarY->get(&(Y[0]), 172); pVarLat->get(&(lat[0]), nbRect);callback.StepIt(); pVarLon->get(&(lon[0]), nbRect);callback.StepIt(); pVarElev->get(&(elev[0]), nbRect);callback.StepIt(); //CProjection prj = GetDataGrid().GetPrj(); for(int i=0; i<nbRect; i++) { double Lat=0, Lon=0; CString line; line.Format("%d,%d,%f,%f,%.1f,0,0\n",i+1,i+1,lat[i],lon[i],elev[i]); fileOut.WriteString(line); } fileOut.Close(); return msg; }
const size_t size( void ) const { return( static_cast<size_t>( m_dim->size() ) ); }
// // Reads variable data from dump file // bool DataVar::initFromFile(const string& filename, const_DomainChunk_ptr dom) { cleanup(); #if ESYS_HAVE_NETCDF NcError ncerr(NcError::silent_nonfatal); NcFile* input = new NcFile(filename.c_str()); if (!input->is_valid()) { cerr << "Could not open input file " << filename << "." << endl; delete input; return false; } NcDim* dim; NcAtt* att; att = input->get_att("type_id"); int typeID = att->as_int(0); if (typeID != 2) { cerr << "WARNING: Only expanded data supported!" << endl; delete input; return false; } att = input->get_att("rank"); rank = att->as_int(0); dim = input->get_dim("num_data_points_per_sample"); ptsPerSample = dim->size(); att = input->get_att("function_space_type"); funcSpace = att->as_int(0); centering = dom->getCenteringForFunctionSpace(funcSpace); dim = input->get_dim("num_samples"); numSamples = dim->size(); #ifdef _DEBUG cout << varName << ":\t" << numSamples << " samples, " << ptsPerSample << " pts/s, rank: " << rank << endl; #endif domain = dom; NodeData_ptr nodes = domain->getMeshForFunctionSpace(funcSpace); if (nodes == NULL) { delete input; return false; } meshName = nodes->getName(); siloMeshName = nodes->getFullSiloName(); initialized = true; size_t dimSize = 1; vector<long> counts; if (rank > 0) { dim = input->get_dim("d0"); int d = dim->size(); shape.push_back(d); counts.push_back(d); dimSize *= d; } if (rank > 1) { dim = input->get_dim("d1"); int d = dim->size(); shape.push_back(d); counts.push_back(d); dimSize *= d; } if (rank > 2) { cerr << "WARNING: Rank " << rank << " data is not supported!\n"; initialized = false; } if (initialized && numSamples > 0) { sampleID.insert(sampleID.end(), numSamples, 0); NcVar* var = input->get_var("id"); var->get(&sampleID[0], numSamples); size_t dataSize = dimSize*numSamples*ptsPerSample; counts.push_back(ptsPerSample); counts.push_back(numSamples); float* tempData = new float[dataSize]; var = input->get_var("data"); var->get(tempData, &counts[0]); const float* srcPtr = tempData; for (size_t i=0; i < dimSize; i++, srcPtr++) { float* c = averageData(srcPtr, dimSize); dataArray.push_back(c); } delete[] tempData; initialized = reorderSamples(); } delete input; #endif // ESYS_HAVE_NETCDF return initialized; }
float InputRdaNetcdf::getValueCore(const Key::Input& iKey) const { float returnValue = Global::MV; std::string filename = getFilename(iKey); NcFile ncfile(filename.c_str()); std::string localVariable; bool found = getLocalVariableName(iKey.variable, localVariable); assert(found); Key::Input key = iKey; // Pre-fill incase file does not contain some keys std::vector<float> offsets = getOffsets(); const std::vector<Location>& locations = getLocations(); for(int o = 0; o < offsets.size(); o++) { key.offset = offsets[o]; for(key.location = 0; key.location < locations.size(); key.location++) { if((mCacheOtherOffsets || iKey.offset == key.offset) || (mCacheOtherLocations || iKey.location == key.location)) { Input::addToCache(key, Global::MV); } } } if(ncfile.is_valid() && localVariable != "") { // Record data NcVar* ncvar = ncfile.get_var(localVariable.c_str()); NcVar* ncTimes = ncfile.get_var("time_observation"); NcVar* ncStationIds = ncfile.get_var("parent_index"); NcDim* ncNamesDim = ncfile.get_dim("id_len"); NcDim* ncRecordsDim = ncfile.get_dim("recNum"); long numRecords = ncRecordsDim->size(); float values[numRecords]; float stationIds[numRecords]; int times[numRecords]; long count[1] = {numRecords}; ncvar->get(values, count); ncTimes->get(times, count); ncStationIds->get(stationIds, count); // Station data NcVar* ncNames = ncfile.get_var("station_id"); NcDim* ncLocationDim = ncfile.get_dim("station"); long numCurrLocations = ncLocationDim->size(); long namesLength = ncNamesDim->size(); char names[numCurrLocations*namesLength]; long count2[2] = {numCurrLocations, namesLength}; ncNames->get(names, count2); ncfile.close(); // Set all values to missing std::vector<float> vec; vec.resize(locations.size()*offsets.size(), Global::MV); // Read data for(int i = 0; i < numRecords; i++) { int id = stationIds[i]; int namesIndex = id*namesLength; std::string name = std::string(&names[namesIndex], namesLength); std::map<std::string,int>::const_iterator it = mLocationNames.find(name); if(it != mLocationNames.end()) { key.location = it->second; key.member = 0; int time = times[i]; int year = Global::getYear(key.date); int month = Global::getMonth(key.date); int day = Global::getDay(key.date); boost::gregorian::date epochDate(1970, 1, 1); boost::gregorian::date currDate(year, month, day); boost::gregorian::date_period diff(epochDate, currDate); int daysSinceEpoch = diff.length().days(); int offsetIndex = round((float) (time - 86400*daysSinceEpoch)/3600); int secondsOffHour = (time - 86400*daysSinceEpoch) % 3600; if(secondsOffHour < 0) secondsOffHour = 3600 + secondsOffHour; if(secondsOffHour > 1800) { secondsOffHour = 3600 - secondsOffHour; } assert(offsetIndex >= 0); if(secondsOffHour < mTimeTolerance && offsetIndex < 24) { assert(key.location < locations.size()); int ind = offsetIndex*locations.size() + key.location; assert(offsetIndex >= 0 && offsetIndex < offsets.size()); key.offset = offsets[offsetIndex]; assert(ind < (int) vec.size() && ind >= 0); //std::cout << key.location << " " << key.offset << std::endl; // Rda dataset uses a different missing value indicator if(values[i] == mMV) values[i] = Global::MV; if((mCacheOtherOffsets || iKey.offset == key.offset) || (mCacheOtherLocations || iKey.location == key.location)) { Input::addToCache(key, values[i]); if(iKey == key) { returnValue = values[i]; } } } } } } return returnValue; }
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, vecTimeDouble[t]); } else if (varTime->type() == ncInt64) { time.FromCFCompliantUnitsOffsetInt( strTimeUnits, (int)(vecTimeInt64[t])); } vecTimes.push_back(time); } }
eavlNetCDFImporter::eavlNetCDFImporter(const string &filename) { file = new NcFile(filename.c_str(), NcFile::ReadOnly); if (!file->is_valid()) { THROW(eavlException,"Couldn't open file!\n"); } if (debugoutput) cerr << "num_dims="<<file->num_dims()<<endl; if (debugoutput) cerr << "num_vars="<<file->num_vars()<<endl; if (debugoutput) cerr << "num_atts="<<file->num_atts()<<endl; for (int i=0; i<file->num_dims(); i++) { NcDim *d = file->get_dim(i); if (debugoutput) cerr << " dim["<<i<<"]: name="<<d->name()<<" size="<<d->size()<<endl; } for (int i=0; i<file->num_atts(); i++) { NcAtt *a = file->get_att(i); if (debugoutput) cerr << " att["<<i<<"]: name="<<a->name()<<" numvals="<<a->num_vals()<<endl; } bool found_grid = false; for (int i=0; i<file->num_vars(); i++) { NcVar *v = file->get_var(i); if (debugoutput) { cerr << " var["<<i<<"]: name="<<v->name(); cerr << " ndims="<<v->num_dims(); cerr << " dims = "; for (int j=0; j<v->num_dims(); j++) { cerr << v->get_dim(j)->name(); if (j<v->num_dims()-1) cerr << "*"; } cerr << endl; } // Here's the condition for what we're going to use; // we only support one mesh for the moment, so we're picking one. // Also, the netcdf files we have have the time dim size as "1" if (v->num_dims() == 4 && string(v->get_dim(0)->name())=="time") { if (!found_grid) { dims.push_back(v->get_dim(1)); dims.push_back(v->get_dim(2)); dims.push_back(v->get_dim(3)); found_grid = true; vars.push_back(v); if (debugoutput) cerr << " * using as first real var\n"; } else { if (string(v->get_dim(1)->name()) == dims[0]->name() && string(v->get_dim(2)->name()) == dims[1]->name() && string(v->get_dim(3)->name()) == dims[2]->name()) { vars.push_back(v); if (debugoutput) cerr << " * using as another var; matches the first real one's dims\n"; } } } } }