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);
	}
}
Exemple #2
0
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);
    }
}
Exemple #3
0
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;
}
Exemple #5
0
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";
    }
}
Exemple #6
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 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];
			}
		}
	}
}
Exemple #8
0
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";
   
};
Exemple #9
0
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();
}
Exemple #10
0
void SiteinInputer::initSiteinFile(string & dirfile){

	siteinfname = dirfile;

	NcError err(NcError::silent_nonfatal);
	NcFile siteFile(siteinfname.c_str(), NcFile::ReadOnly);
 	if(!siteFile.is_valid()){
 		string msg = siteinfname+" is not valid";
 		char* msgc = const_cast< char* > ( msg.c_str());
 		throw Exception( msgc, I_NCFILE_NOT_EXIST);
 	}

 	NcDim* siteD = siteFile.get_dim("CHTID");
  	if(!siteD->is_valid()){
  		throw Exception("SITED Dimension is not Valid in siteinFile", I_NCDIM_NOT_EXIST);
 	}

}
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;
	
}
Exemple #12
0
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();
   }
}
Exemple #13
0
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();
   }
}
Exemple #14
0
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());
}
Exemple #15
0
int main()
{
  try
    {
      cout<<"Opening file \"firstFile.cdf\" with NcFile::replace"<<endl;
      NcFile ncFile("firstFile.cdf",NcFile::replace);
    
      cout<<left<<setw(55)<<"Testing addGroup(\"groupName\")";
      NcGroup groupA(ncFile.addGroup("groupA"));
      NcGroup groupA0(ncFile.addGroup("groupA0"));
      NcGroup groupB(groupA.addGroup("groupB"));
      NcGroup groupC(groupA.addGroup("groupC"));
      cout <<"    -----------   passed\n";
    
      cout <<left<<setw(55)<<"Testing addDim(\"dimensionName\")";
      NcDim dim1 = ncFile.addDim("dim1",11);
      NcDim dim2 = ncFile.addDim("dim2");
      NcDim dim3 = ncFile.addDim("dim3",13);
      NcDim dim4 = groupB.addDim("dim4",14);
      NcDim dim5 = groupB.addDim("dim5",15);
      NcDim dim6 = groupB.addDim("dim6",16);
      NcDim dim7 = groupB.addDim("dim7",17);
      cout <<"    -----------   passed\n";




      cout <<left<<setw(55)<<"Testing NcDim::isUnlimited()";
      if( dim1.isUnlimited())    throw NcException("NcException","Error in test 1.1",__FILE__,__LINE__);
      if( !dim2.isUnlimited())   throw NcException("NcException","Error in test 1.2",__FILE__,__LINE__);
      if( dim3.isUnlimited())    throw NcException("NcException","Error in test 1.3",__FILE__,__LINE__);
      if( dim4.isUnlimited())    throw NcException("NcException","Error in test 1.4",__FILE__,__LINE__);
      if( dim5.isUnlimited())    throw NcException("NcException","Error in test 1.5",__FILE__,__LINE__);
      if( dim6.isUnlimited())    throw NcException("NcException","Error in test 1.6",__FILE__,__LINE__);
      if( dim7.isUnlimited())    throw NcException("NcException","Error in test 1.7",__FILE__,__LINE__);
      cout <<"    -----------   passed\n";

     cout <<left<<setw(55)<<"Testing NcDim::isNull()";
     if( dim1.isNull())    throw NcException("NcException","Error in test 2.1",__FILE__,__LINE__);
     NcDim tmpDim;
     if( !tmpDim.isNull()) throw NcException("NcException","Error in test 2.2",__FILE__,__LINE__);
      cout <<"    -----------   passed\n";

     cout <<left<<setw(55)<<"Testing NcDim::getSize()";
     if( dim1.getSize() != 11)    throw NcException("NcException","Error in test 3.1",__FILE__,__LINE__);
     if( dim2.getSize() != 0 )    throw NcException("NcException","Error in test 3.2",__FILE__,__LINE__);
     if( dim3.getSize() != 13)    throw NcException("NcException","Error in test 3.3",__FILE__,__LINE__);
     if( dim4.getSize() != 14)    throw NcException("NcException","Error in test 3.4",__FILE__,__LINE__);
     if( dim5.getSize() != 15)    throw NcException("NcException","Error in test 3.5",__FILE__,__LINE__);
     if( dim6.getSize() != 16)    throw NcException("NcException","Error in test 3.6",__FILE__,__LINE__);
     if( dim7.getSize() != 17)    throw NcException("NcException","Error in test 3.7",__FILE__,__LINE__);
     cout <<"    -----------   passed\n";

     cout <<left<<setw(55)<<"Testing NcDim::getParentGroup()";
     if( !(dim1.getParentGroup() == ncFile))   throw NcException("NcException","Error in test 4.1",__FILE__,__LINE__);
     if( !(dim2.getParentGroup() == ncFile))   throw NcException("NcException","Error in test 4.2",__FILE__,__LINE__);
     if( !(dim3.getParentGroup() == ncFile))   throw NcException("NcException","Error in test 4.3",__FILE__,__LINE__);
     if( !(dim4.getParentGroup() == groupB))   throw NcException("NcException","Error in test 4.4",__FILE__,__LINE__);
     if( !(dim5.getParentGroup() == groupB))   throw NcException("NcException","Error in test 4.5",__FILE__,__LINE__);
     if( !(dim6.getParentGroup() == groupB))   throw NcException("NcException","Error in test 4.6",__FILE__,__LINE__);
     if( !(dim7.getParentGroup() == groupB))   throw NcException("NcException","Error in test 4.7",__FILE__,__LINE__);
     cout <<"    -----------   passed\n";

     cout <<left<<setw(55)<<"Testing NcDim::getName()";
     if( dim1.getName() != "dim1")   throw NcException("NcException","Error in test 5.1",__FILE__,__LINE__);
     if( dim2.getName() != "dim2")   throw NcException("NcException","Error in test 5.2",__FILE__,__LINE__);
     if( dim3.getName() != "dim3")   throw NcException("NcException","Error in test 5.3",__FILE__,__LINE__);
     if( dim4.getName() != "dim4")   throw NcException("NcException","Error in test 5.4",__FILE__,__LINE__);
     if( dim5.getName() != "dim5")   throw NcException("NcException","Error in test 5.5",__FILE__,__LINE__);
     if( dim6.getName() != "dim6")   throw NcException("NcException","Error in test 5.6",__FILE__,__LINE__);
     if( dim7.getName() != "dim7")   throw NcException("NcException","Error in test 5.7",__FILE__,__LINE__);
     cout <<"    -----------   passed\n";





      cout <<left<<setw(55)<<"Testing NcGroup::getDimCount([netCDF::Location])";
      if( ncFile.getDimCount() != 3)                           throw NcException("NcException","Error in test 6.1",__FILE__,__LINE__);
      if( ncFile.getDimCount(NcGroup::Current) != 3)           throw NcException("NcException","Error in test 6.2",__FILE__,__LINE__);
      if( ncFile.getDimCount(NcGroup::All) != 7)               throw NcException("NcException","Error in test 6.3",__FILE__,__LINE__);
      if( ncFile.getDimCount(NcGroup::Parents) != 0)           throw NcException("NcException","Error in test 6.4",__FILE__,__LINE__);
      if( ncFile.getDimCount(NcGroup::Children) != 4)          throw NcException("NcException","Error in test 6.5",__FILE__,__LINE__);
      if( ncFile.getDimCount(NcGroup::ParentsAndCurrent) != 3) throw NcException("NcException","Error in test 6.6",__FILE__,__LINE__);
      if( ncFile.getDimCount(NcGroup::ChildrenAndCurrent) != 7)throw NcException("NcException","Error in test 6.7",__FILE__,__LINE__);
      if( groupA.getDimCount() != 0)                           throw NcException("NcException","Error in test 6.8",__FILE__,__LINE__);
      if( groupA.getDimCount(NcGroup::Current) != 0)           throw NcException("NcException","Error in test 6.9",__FILE__,__LINE__);
      if( groupA.getDimCount(NcGroup::All) != 7)               throw NcException("NcException","Error in test 6.10",__FILE__,__LINE__);
      if( groupA.getDimCount(NcGroup::Parents) != 3)           throw NcException("NcException","Error in test 6.11",__FILE__,__LINE__);
      if( groupA.getDimCount(NcGroup::Children) != 4)          throw NcException("NcException","Error in test 6.12",__FILE__,__LINE__);
      if( groupA.getDimCount(NcGroup::ParentsAndCurrent) != 3) throw NcException("NcException","Error in test 6.13",__FILE__,__LINE__);
      if( groupA.getDimCount(NcGroup::ChildrenAndCurrent) != 4)throw NcException("NcException","Error in test 6.14",__FILE__,__LINE__);
      cout <<"    -----------   passed\n";


	
      cout <<left<<setw(55)<<"Testing NcGroup::getDims([netCDF::Location])";
      {
	multimap<string,NcDim> dimMap;
	multimap<string,NcDim>::iterator iter;

	// operations on ncFile

	dimMap = ncFile.getDims();
	if( dimMap.size() != 3)        throw NcException("NcException","Error in test 7.1",__FILE__,__LINE__);
	iter=dimMap.find("dim1");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.2",__FILE__,__LINE__);
	iter=dimMap.find("dim2");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.3",__FILE__,__LINE__);
	iter=dimMap.find("dim3");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.4",__FILE__,__LINE__);

	dimMap = ncFile.getDims(NcGroup::Current);
	if( dimMap.size() != 3)        throw NcException("NcException","Error in test 7.5",__FILE__,__LINE__);
	iter=dimMap.find("dim1");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.6",__FILE__,__LINE__);
	iter=dimMap.find("dim2");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.7",__FILE__,__LINE__);
	iter=dimMap.find("dim3");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.8",__FILE__,__LINE__);

	dimMap = ncFile.getDims(NcGroup::Parents);
	if( dimMap.size() != 0)        throw NcException("NcException","Error in test 7.9",__FILE__,__LINE__);

	dimMap = ncFile.getDims(NcGroup::Children);
	if( dimMap.size() != 4)        throw NcException("NcException","Error in test 7.10",__FILE__,__LINE__);

	dimMap = ncFile.getDims(NcGroup::ParentsAndCurrent);
	if( dimMap.size() != 3)        throw NcException("NcException","Error in test 7.11",__FILE__,__LINE__);
	iter=dimMap.find("dim1");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.12",__FILE__,__LINE__);
	iter=dimMap.find("dim2");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.13",__FILE__,__LINE__);
	iter=dimMap.find("dim3");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.14",__FILE__,__LINE__);

	dimMap = ncFile.getDims(NcGroup::All);
	if( dimMap.size() != 7)        throw NcException("NcException","Error in test 7.15",__FILE__,__LINE__);
	iter=dimMap.find("dim1");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.16",__FILE__,__LINE__);
	iter=dimMap.find("dim2");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.17",__FILE__,__LINE__);
	iter=dimMap.find("dim3");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.18",__FILE__,__LINE__);
	iter=dimMap.find("dim4");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.19",__FILE__,__LINE__);
	iter=dimMap.find("dim5");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.20",__FILE__,__LINE__);
	iter=dimMap.find("dim6");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.21",__FILE__,__LINE__);
	iter=dimMap.find("dim7");
	if( iter == dimMap.end())    throw NcException("NcException","Error in test 7.22",__FILE__,__LINE__);
      
      }
      cout <<"    -----------   passed\n";


	
      cout <<left<<setw(55)<<"Testing NcGroup::getDims(\"name\",[netCDF::Location])";
      {

	set<NcDim> dimSet;
	set<NcDim>::iterator iter;

	// operations on ncFile:dim2

	dimSet = ncFile.getDims("dim2");
	if( dimSet.size() != 1)       throw NcException("NcException","Error in test 8.1",__FILE__,__LINE__);
	iter=dimSet.find(dim2);
	if( iter == dimSet.end())     throw NcException("NcException","Error in test 8.2",__FILE__,__LINE__);
	if( iter->getName() != "dim2")throw NcException("NcException","Error in test 8.3",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim2",NcGroup::Current);
	if( dimSet.count(dim2) != 1) throw NcException("NcException","Error in test 8.4",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim2",NcGroup::Parents);
	if( dimSet.count(dim2) != 0) throw NcException("NcException","Error in test 8.5",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim2",NcGroup::Children);
	if( dimSet.count(dim2) != 0) throw NcException("NcException","Error in test 8.6",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim2",NcGroup::ParentsAndCurrent);
	if( dimSet.count(dim2) != 1) throw NcException("NcException","Error in test 8.7",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim2",NcGroup::ChildrenAndCurrent);
	if( dimSet.count(dim2) != 1) throw NcException("NcException","Error in test 8.8",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim2",NcGroup::All);
	if( dimSet.count(dim2) != 1) throw NcException("NcException","Error in test 8.9",__FILE__,__LINE__);


	// operations on ncFile:dim6

	dimSet = ncFile.getDims("dim6");
	if( dimSet.size() != 0)       throw NcException("NcException","Error in test 8.10",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim6",NcGroup::Current);
	if( dimSet.count(dim6) != 0) throw NcException("NcException","Error in test 8.11",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim6",NcGroup::Parents);
	if( dimSet.count(dim6) != 0) throw NcException("NcException","Error in test 8.12",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim6",NcGroup::Children);
	if( dimSet.count(dim6) != 1) throw NcException("NcException","Error in test 8.13",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim6",NcGroup::ParentsAndCurrent);
	if( dimSet.count(dim6) != 0) throw NcException("NcException","Error in test 8.14",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim6",NcGroup::ChildrenAndCurrent);
	if( dimSet.count(dim6) != 1) throw NcException("NcException","Error in test 8.15",__FILE__,__LINE__);

	dimSet = ncFile.getDims("dim6",NcGroup::All);
	if( dimSet.count(dim6) != 1) throw NcException("NcException","Error in test 8.16",__FILE__,__LINE__);


	// operations on groupB:dim1

	dimSet = groupB.getDims("dim1");
	if( dimSet.size() != 0)       throw NcException("NcException","Error in test 8.17",__FILE__,__LINE__);

	dimSet = groupB.getDims("dim1",NcGroup::Current);
	if( dimSet.count(dim1) != 0) throw NcException("NcException","Error in test 8.18",__FILE__,__LINE__);

	dimSet = groupB.getDims("dim1",NcGroup::Parents);
	if( dimSet.count(dim1) != 1) throw NcException("NcException","Error in test 8.19",__FILE__,__LINE__);

	dimSet = groupB.getDims("dim1",NcGroup::Children);
	if( dimSet.count(dim1) != 0) throw NcException("NcException","Error in test 8.20",__FILE__,__LINE__);

	dimSet = groupB.getDims("dim1",NcGroup::ParentsAndCurrent);
	if( dimSet.count(dim1) != 1) throw NcException("NcException","Error in test 8.21",__FILE__,__LINE__);

	dimSet = groupB.getDims("dim1",NcGroup::ChildrenAndCurrent);
	if( dimSet.count(dim1) != 0) throw NcException("NcException","Error in test 8.22",__FILE__,__LINE__);

	dimSet = groupB.getDims("dim1",NcGroup::All);
	if( dimSet.count(dim1) != 1) throw NcException("NcException","Error in test 8.23",__FILE__,__LINE__);


	// operations on groupA:dim1

	dimSet = groupA.getDims("dim1");
	if( dimSet.size() != 0)       throw NcException("NcException","Error in test 8.24",__FILE__,__LINE__);

	dimSet = groupA.getDims("dim1",NcGroup::Current);
	if( dimSet.count(dim1) != 0) throw NcException("NcException","Error in test 8.25",__FILE__,__LINE__);

	dimSet = groupA.getDims("dim1",NcGroup::Parents);
	if( dimSet.count(dim1) != 1) throw NcException("NcException","Error in test 8.26",__FILE__,__LINE__);

	dimSet = groupA.getDims("dim1",NcGroup::Children);
	if( dimSet.count(dim1) != 0) throw NcException("NcException","Error in test 8.27",__FILE__,__LINE__);

	dimSet = groupA.getDims("dim1",NcGroup::ParentsAndCurrent);
	if( dimSet.count(dim1) != 1) throw NcException("NcException","Error in test 8.28",__FILE__,__LINE__);

	dimSet = groupA.getDims("dim1",NcGroup::ChildrenAndCurrent);
	if( dimSet.count(dim1) != 0) throw NcException("NcException","Error in test 8.29",__FILE__,__LINE__);

	dimSet = groupA.getDims("dim1",NcGroup::All);
	if( dimSet.count(dim1) != 1) throw NcException("NcException","Error in test 8.30",__FILE__,__LINE__);

      }
      cout <<"    -----------   passed\n";





      cout <<left<<setw(55)<<"Testing NcGroup::getDim(\"name\",[netCDF::Location])";
      {
	if( ncFile.getDim("dim1",NcGroup::All).getName() !="dim1") throw NcException("NcException","Error in test 9.1",__FILE__,__LINE__);
	if( ncFile.getDim("dim2",NcGroup::All).getName() !="dim2") throw NcException("NcException","Error in test 9.2",__FILE__,__LINE__);
	if( ncFile.getDim("dim3",NcGroup::All).getName() !="dim3") throw NcException("NcException","Error in test 9.3",__FILE__,__LINE__);
	if( ncFile.getDim("dim4",NcGroup::All).getName() !="dim4") throw NcException("NcException","Error in test 9.4",__FILE__,__LINE__);
	if( ncFile.getDim("dim5",NcGroup::All).getName() !="dim5") throw NcException("NcException","Error in test 9.5",__FILE__,__LINE__);
	if( ncFile.getDim("dim6",NcGroup::All).getName() !="dim6") throw NcException("NcException","Error in test 9.6",__FILE__,__LINE__);
	if( ncFile.getDim("dim7",NcGroup::All).getName() !="dim7") throw NcException("NcException","Error in test 9.7",__FILE__,__LINE__);
	if( groupB.getDim("dim1",NcGroup::All).getName() !="dim1") throw NcException("NcException","Error in test 9.8",__FILE__,__LINE__);
	if( groupB.getDim("dim2",NcGroup::All).getName() !="dim2") throw NcException("NcException","Error in test 9.9",__FILE__,__LINE__);
	if( groupB.getDim("dim3",NcGroup::All).getName() !="dim3") throw NcException("NcException","Error in test 9.10",__FILE__,__LINE__);
	if( groupB.getDim("dim4",NcGroup::All).getName() !="dim4") throw NcException("NcException","Error in test 9.11",__FILE__,__LINE__);
	if( groupB.getDim("dim5",NcGroup::All).getName() !="dim5") throw NcException("NcException","Error in test 9.12",__FILE__,__LINE__);
	if( groupB.getDim("dim6",NcGroup::All).getName() !="dim6") throw NcException("NcException","Error in test 9.13",__FILE__,__LINE__);
	if( groupB.getDim("dim7",NcGroup::All).getName() !="dim7") throw NcException("NcException","Error in test 9.14",__FILE__,__LINE__);
	if( !ncFile.getDim("dim7").isNull())                            throw NcException("NcException","Error in test 9.15",__FILE__,__LINE__);
	if( !ncFile.getDim("dim7",NcGroup::Current).isNull())           throw NcException("NcException","Error in test 9.16",__FILE__,__LINE__);
	if( !ncFile.getDim("dim7",NcGroup::Parents).isNull())           throw NcException("NcException","Error in test 9.17",__FILE__,__LINE__);
	if(  ncFile.getDim("dim7",NcGroup::Children).isNull())          throw NcException("NcException","Error in test 9.18",__FILE__,__LINE__);
	if( !ncFile.getDim("dim7",NcGroup::ParentsAndCurrent).isNull()) throw NcException("NcException","Error in test 9.19",__FILE__,__LINE__);
	if(  ncFile.getDim("dim7",NcGroup::ChildrenAndCurrent).isNull())throw NcException("NcException","Error in test 9.20",__FILE__,__LINE__);

	if( !groupA.getDim("dim7").isNull())                            throw NcException("NcException","Error in test 9.21",__FILE__,__LINE__);
	if( !groupA.getDim("dim7",NcGroup::Current).isNull())           throw NcException("NcException","Error in test 9.22",__FILE__,__LINE__);
	if( !groupA.getDim("dim7",NcGroup::Parents).isNull())           throw NcException("NcException","Error in test 9.23",__FILE__,__LINE__);
	if(  groupA.getDim("dim7",NcGroup::Children).isNull())          throw NcException("NcException","Error in test 9.24",__FILE__,__LINE__);
	if( !groupA.getDim("dim7",NcGroup::ParentsAndCurrent).isNull()) throw NcException("NcException","Error in test 9.25",__FILE__,__LINE__);
	if(  groupA.getDim("dim7",NcGroup::ChildrenAndCurrent).isNull())throw NcException("NcException","Error in test 9.26",__FILE__,__LINE__);
	if(  groupA.getDim("dim7",NcGroup::All).isNull())               throw NcException("NcException","Error in test 9.27",__FILE__,__LINE__);

	if(  groupB.getDim("dim7").isNull())                            throw NcException("NcException","Error in test 9.28",__FILE__,__LINE__);
	if(  groupB.getDim("dim7",NcGroup::Current).isNull())           throw NcException("NcException","Error in test 9.29",__FILE__,__LINE__);
	if( !groupB.getDim("dim7",NcGroup::Parents).isNull())           throw NcException("NcException","Error in test 9.30",__FILE__,__LINE__);
	if( !groupB.getDim("dim7",NcGroup::Children).isNull())          throw NcException("NcException","Error in test 9.31",__FILE__,__LINE__);
	if(  groupB.getDim("dim7",NcGroup::ParentsAndCurrent).isNull()) throw NcException("NcException","Error in test 9.32",__FILE__,__LINE__);
	if(  groupB.getDim("dim7",NcGroup::ChildrenAndCurrent).isNull())throw NcException("NcException","Error in test 9.33",__FILE__,__LINE__);
	if(  groupB.getDim("dim7",NcGroup::All).isNull())               throw NcException("NcException","Error in test 9.34",__FILE__,__LINE__);
	if( !ncFile.getDim("dimX",NcGroup::All).isNull())               throw NcException("NcException","Error in test 9.35",__FILE__,__LINE__);
      }

      cout <<"    -----------   passed\n";


    }
  catch (NcException& e)
    {
      cout <<"\n";
      e.what();
    }
}
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;
}
Exemple #17
0
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 kernel::openCoordNetcdf () {
  
  // Open the NetCDF coordinate file output from the solver.
  
  using namespace netCDF;
  using namespace netCDF::exceptions;

  // Local mpi variables.
  int myRank = MPI::COMM_WORLD.Get_rank ();
  int worldSize = MPI::COMM_WORLD.Get_size ();
  
  if (myRank == 0)
    std::cout << "Opening coordinate file: " << blu << "./krn/xyzCrustMantle.nc"  << rst 
      << " with " << worldSize << " processors." << std::flush << std::endl;
  
  try {

    std::string coordName = "./krn/xyzCrustMantle.nc";

    // Open the file.
    NcFile dataFile (coordName, NcFile::read);
    
    // Get variable.
    NcVar NcRadius = dataFile.getVar ("radius");
    NcVar NcTheta  = dataFile.getVar ("theta");
    NcVar NcPhi    = dataFile.getVar ("phi");
    
    // Get array sizes.
    NcDim procDim  = NcRadius.getDim (0);
    NcDim coordDim = NcRadius.getDim (1);  
    numWroteProcs  = procDim.getSize ();
    numGLLPoints   = coordDim.getSize ();
  
    if (myRank == 0)
      std::cout << mgn << "Number of solver processers:\t " << numWroteProcs
        << "\nNumber of GLL points:\t\t " << numGLLPoints << rst << "\n" << std::endl;
    
    // Current error handling. Only can have as many cores as we did for the simulation.
    if (worldSize > numWroteProcs) {
      
      if (myRank == 0)
        std::cout << red << "Currently, you can only use as many cores for processing as were used " 
          << "in the forward run. Exiting.\n" << rst << std::flush << std::endl;
      
      MPI::COMM_WORLD.Abort (EXIT_FAILURE);
      exit (EXIT_FAILURE);
      
    }
    
    // Set up the MPI read chunk array.
    std::vector<size_t> start;
    std::vector<size_t> count;
    start.resize(2);
    count.resize(2);
    
    // Row major MPI read. Start at [myRank, 0]
    start[0] = myRank;
    start[1] = 0;
    
    // Read until end of line [myrank, numGLLPoints]
    count[0] = 1;
    count[1] = numGLLPoints;
    
    // Preallocate cartesian arrays.
    xStore = new float [numGLLPoints];
    yStore = new float [numGLLPoints];
    zStore = new float [numGLLPoints];
    
    // Of course only read in with the number of processors used to create the file.
    if (myRank < numWroteProcs) {
      radius = new float [numGLLPoints];
      theta  = new float [numGLLPoints];
      phi    = new float [numGLLPoints];
      
      NcRadius.getVar (start, count, radius);
      NcTheta.getVar  (start, count, theta);
      NcPhi.getVar    (start, count, phi);
    }
    
    // Save original arrays.
    radiusOrig = new float [numGLLPoints];
    thetaOrig  = new float [numGLLPoints];
    phiOrig    = new float [numGLLPoints];    
    for (size_t i=0; i<numGLLPoints; i++) {
      
      radiusOrig[i] = radius[i];
      thetaOrig[i]  = theta[i];
      phiOrig[i]    = phi[i];
      
    }
    
    // Destructor will close file.
        
  } catch (NcException &error) {
    
    std::cout << error.what() << std::endl;
    std::cout << red << "Failure reading: " << fileName << std::endl;
    std::exit (EXIT_FAILURE);
    
  }
  
}
Exemple #19
0
//
// Creates NetCDF product
//
bool NetCDFProduct(MSG_header *PRO_head, MSG_data* PRO_data,
                   int totalsegs, int *segsindexes,
		   MSG_header *header, MSG_data *msgdat)
{
  struct tm *tmtime;
  char NcName[1024];
  char reftime[64];
  char projname[16];
  int wd, hg;
  int bpp;
  int ncal;
  float *cal;
  NcVar *ivar;
  NcVar *tvar;
  NcDim *tdim;
  NcDim *ldim;
  NcDim *cdim;
  NcDim *caldim;

  int npix = header[0].image_structure->number_of_columns;
  int nlin = header[0].image_structure->number_of_lines;
  size_t npixperseg = npix*nlin;

  size_t total_size = totalsegs*npixperseg;
  MSG_SAMPLE *pixels = new MSG_SAMPLE[total_size];
  memset(pixels, 0, total_size*sizeof(MSG_SAMPLE));
  size_t pos = 0;
  for (int i = 0; i < totalsegs; i ++)
  {
    if (segsindexes[i] >= 0)
      memcpy(pixels+pos, msgdat[segsindexes[i]].image->data,
             npixperseg*sizeof(MSG_SAMPLE));
    pos += npixperseg;
  }

  nlin = nlin*totalsegs;

  // Manage subarea
  if (is_subarea)
  {
    if (AreaLinStart < 0                             ||
        AreaLinStart > nlin - AreaNlin ||
        AreaNlin > nlin - AreaLinStart)
    {
      std::cerr << "Wrong Subarea in lines...." << std::endl;
      throw;
    }
    if (AreaPixStart < 0               ||
        AreaPixStart > npix - AreaNpix ||
        AreaNpix > npix - AreaPixStart)
    {
      std::cerr << "Wrong Subarea in Pixels...." << std::endl;
      throw;
    }
    size_t newsize = AreaNpix * AreaNlin;
    MSG_SAMPLE *newpix = new MSG_SAMPLE[newsize];
    memset(newpix, 0, newsize*sizeof(MSG_SAMPLE));
    for (int i = 0; i < AreaNlin; i ++)
      memcpy(newpix + i * AreaNpix,
             pixels + (AreaLinStart + i) * npix + AreaPixStart,
             AreaNpix * sizeof(MSG_SAMPLE));
    delete [ ] pixels;
    pixels = newpix;
    total_size = newsize;
  }
  else
  {
    AreaNpix = npix;
    AreaNlin = nlin;
  }

  tmtime = PRO_data->prologue->image_acquisition.PlannedAquisitionTime.TrueRepeatCycleStart.get_timestruct( );
  t_enum_MSG_spacecraft spc = header[0].segment_id->spacecraft_id;
  uint_1 chn = header[0].segment_id->spectral_channel_id;
  float sublon = header[0].image_navigation->subsatellite_longitude;
  int cfac = header[0].image_navigation->column_scaling_factor;
  int lfac = header[0].image_navigation->line_scaling_factor;
  int coff = header[0].image_navigation->column_offset;
  int loff = header[0].image_navigation->line_offset;
  float sh = header[0].image_navigation->satellite_h;

  char *channelstring = strdup(MSG_channel_name(spc, chn).c_str( ));
  char *channel = chname(channelstring, strlen(channelstring) + 1);

  // Build up output NetCDF file name and open it
  sprintf( NcName, "%s_%4d%02d%02d_%02d%02d.nc", channel,
           tmtime->tm_year + 1900, tmtime->tm_mon + 1, tmtime->tm_mday,
	   tmtime->tm_hour, tmtime->tm_min );
  NcFile ncf ( NcName , NcFile::Replace );
  if (! ncf.is_valid()) return false;

  // Fill arrays on creation
  ncf.set_fill(NcFile::Fill);

  // Add Global Attributes
  if (! ncf.add_att("Satellite", MSG_spacecraft_name(spc).c_str()))
	            return false;
  sprintf(reftime, "%04d-%02d-%02d %02d:%02d:00 UTC",
      tmtime->tm_year + 1900, tmtime->tm_mon + 1, tmtime->tm_mday,
      tmtime->tm_hour, tmtime->tm_min);
  if (! ncf.add_att("Antenna", "Fixed") ) return false;
  if (! ncf.add_att("Receiver", "HIMET") ) return false;
  if (! ncf.add_att("Time", reftime) ) return false;
  if (! ncf.add_att("Area_Name", "SpaceView" ) ) return false;
  sprintf(projname, "GEOS(%3.1f)", sublon);
  if (! ncf.add_att("Projection", projname) ) return false;
  if (! ncf.add_att("Columns", AreaNpix ) ) return false;
  if (! ncf.add_att("Lines", AreaNlin ) ) return false;
  if (! ncf.add_att("SampleX", 1.0 ) ) return false;
  if (! ncf.add_att("SampleY", 1.0 ) ) return false;
  if (! ncf.add_att("AreaStartPix", AreaPixStart ) ) return false;
  if (! ncf.add_att("AreaStartLin", AreaLinStart ) ) return false;
  if (! ncf.add_att("Column_Scale_Factor", cfac) ) return false;
  if (! ncf.add_att("Line_Scale_Factor", lfac) ) return false;
  if (! ncf.add_att("Column_Offset", coff) ) return false;
  if (! ncf.add_att("Line_Offset", loff) ) return false;
  if (! ncf.add_att("Orbit_Radius", sh) ) return false;
  if (! ncf.add_att("Longitude", sublon) ) return false;
  if (! ncf.add_att("NortPolar", 1) ) return false;
  if (! ncf.add_att("NorthSouth", 1) ) return false;
  if (! ncf.add_att("title", TITLE) ) return false;
  if (! ncf.add_att("Institution", INSTITUTION) ) return false;
  if (! ncf.add_att("Type", TYPE) ) return false;
  if (! ncf.add_att("Version", HIMET_VERSION) ) return false;
  if (! ncf.add_att("Conventions", "COARDS") ) return false;
  if (! ncf.add_att("history", "Created from raw data") ) return false;

  // Dimensions
  wd = AreaNpix;
  hg = AreaNlin;
  bpp = header[0].image_structure->number_of_bits_per_pixel;
  ncal = (int) pow(2.0, bpp);

  tdim = ncf.add_dim("time");
  if (!tdim->is_valid()) return false;
  ldim = ncf.add_dim("line", hg);
  if (!ldim->is_valid()) return false;
  cdim = ncf.add_dim("column", wd);
  if (!cdim->is_valid()) return false;
  caldim = ncf.add_dim("calibration", ncal);
  if (!caldim->is_valid()) return false;

  // Get calibration values
  cal = PRO_data->prologue->radiometric_proc.get_calibration((int) chn, bpp);

  // Add Calibration values
  NcVar *cvar = ncf.add_var("calibration", ncFloat, caldim);
  if (!cvar->is_valid()) return false;
  cvar->add_att("long_name", "Calibration coefficients");
  cvar->add_att("variable", channel);
  if (chn > 3 && chn < 12)
    cvar->add_att("units", "K");
  else
    cvar->add_att("units", "mW m^-2 sr^-1 (cm^-1)^-1");
  if (!cvar->put(cal, ncal)) return false;

  tvar = ncf.add_var("time", ncDouble, tdim);
  if (!tvar->is_valid()) return false;
  tvar->add_att("long_name", "Time");
  tvar->add_att("units", "seconds since 2000-01-01 00:00:00 UTC");
  double atime;
  time_t ttime;
  extern long timezone;
  ttime = mktime(tmtime);
  atime = ttime - 946684800 - timezone;
  if (!tvar->put(&atime, 1)) return false;

  ivar = ncf.add_var(channel, ncShort, tdim, ldim, cdim);
  if (!ivar->is_valid()) return false;
  if (!ivar->add_att("add_offset", 0.0)) return false;
  if (!ivar->add_att("scale_factor", 1.0)) return false;
  if (!ivar->add_att("chnum", chn)) return false;

  // Write output values
  if (!ivar->put((const short int *) pixels, 1, hg, wd)) return false;

  // Close NetCDF output
  (void) ncf.close( );

  delete [ ] pixels;
  delete [ ] cal;

  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 kernel::openKernelNetcdf () {
  
  // Open the kernel file output from the solver.
  
  using namespace netCDF;
  using namespace netCDF::exceptions;

  // Local mpi variables.
  int myRank = MPI::COMM_WORLD.Get_rank ();
  int worldSize = MPI::COMM_WORLD.Get_size ();
  
  if (myRank == 0)
    std::cout << "Opening kernel file: " << blu << fileName << rst << " with " << worldSize 
      << " processors." << std::flush << std::endl;
  
  try {

    // Open the file.
    NcFile dataFile (fileName, NcFile::read);
    
    // Get variable.
    NcVar NcKernel = dataFile.getVar ("rawKernel");
    
    // Get array sizes.
    NcDim procDim = NcKernel.getDim (0);
    NcDim kernDim = NcKernel.getDim (1);  
    numWroteProcs = procDim.getSize ();
    numGLLPoints  = kernDim.getSize ();
  
    if (myRank == 0)
      std::cout << mgn << "Number of solver processers:\t " << numWroteProcs
        << "\nNumber of GLL points:\t\t " << numGLLPoints << rst << "\n" << std::endl;
    
    // Set up the MPI read chunk array.
    std::vector<size_t> start;
    std::vector<size_t> count;
    start.resize(2);
    count.resize(2);
    
    // Row major MPI read. Start at [myRank, 0]
    start[0] = myRank;
    start[1] = 0;
    
    // Read until end of line [myrank, numGLLPoints]
    count[0] = 1;
    count[1] = numGLLPoints;
    
    // Of course only read in with the number of processors used to create the file.
    if (myRank < numWroteProcs) {
      rawKernel = new float [numGLLPoints];
      NcKernel.getVar (start, count, rawKernel);
    }
        
    // Destructor will close file.
    
  } catch (NcException &error) {
    
    std::cout << error.what() << std::endl;
    std::cout << red << "Failure reading: " << fileName << std::endl;
    std::exit (EXIT_FAILURE);
    
  }
    
}
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);
	}
}
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;
}
Exemple #24
0
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;
}
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);
	}
}
Exemple #26
0
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";
                }
            }
        }

    }
}
Exemple #27
0
 const size_t size( void ) const
 {
     return( static_cast<size_t>( m_dim->size() ) );
 }
Exemple #28
0
 const std::string name( void ) const
 {
     return( m_dim->name() );
 }
Exemple #29
0
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();
}
Exemple #30
0
//
// 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;
}