示例#1
0
int TestSuite::testVar()
{


   try
   {

      string FILE_NAME = "tst_vars.nc";
      int NDIMS = 4;
      int NLAT = 6;
      int NLON = 12;

      // Names of things. 
      string LAT_NAME = "latitude";
      string LON_NAME = "longitude";

      int  MAX_ATT_LEN = 80;
      // These are used to construct some example data. 
      float START_LAT = 25.0;
      float START_LON = -125.0;

      string  UNITS = "units";
      string  DEGREES_EAST =  "degrees_east";
      string  DEGREES_NORTH = "degrees_north";

      // For the units attributes. 
      string LAT_UNITS = "degrees_north";
      string LON_UNITS = "degrees_east";

      // Return this code to the OS in case of failure.
#define NC_ERR 2

  
      // We will write latitude and longitude fields. 
      float lats[NLAT],lons[NLON];

      // create some pretend data. If this wasn't an example program, we
      // would have some real data to write for example, model output.
      for (int lat = 0; lat < NLAT; lat++)
	 lats[lat] = START_LAT + 5. * lat;
      for (int lon = 0; lon < NLON; lon++)
	 lons[lon] = START_LON + 5. * lon;

      // Create the file.
      NcFile test(FILE_NAME, NcFile::Replace);

      // Define the dimensions. NetCDF will hand back an ncDim object for
      // each.
      NcDim* latDim = test.addDim(LAT_NAME, NLAT);
      NcDim* lonDim = test.addDim(LON_NAME, NLON);

   
      // Define the coordinate variables.
      NcVar* latVar = test.addVar(LAT_NAME, ncFloat, latDim);
      NcVar* lonVar = test.addVar(LON_NAME, ncFloat, lonDim);
       
      // Define units attributes for coordinate vars. This attaches a
      // text attribute to each of the coordinate variables, containing
      // the units.
      latVar->addAtt(UNITS,ncString, DEGREES_NORTH);
      lonVar->addAtt(UNITS,ncString, DEGREES_EAST);

      // Write the coordinate variable data to the file.
      latVar->put(lats, NLAT);
      lonVar->put(lons, NLON);

      NcValues *latVals = latVar->getValues();
      cout<<"toString returns lats: "<<latVals->toString()<<endl;
      cout<<"toChar returns "<<latVals->toChar(1)<<endl;
      cout<<"toShort returns "<<latVals->toShort(1)<<endl;
      cout<<"toInt returns "<<latVals->toInt(1)<<endl;
      cout<<"toLong returns "<<latVals->toLong(1)<<endl;

      latVals->print(cout);
      
      NcValues *lonVals = lonVar->getValues();
      cout<<"toString returns lats: "<<lonVals->toString()<<endl;
      lonVals->print(cout);
      
	
      cout<<"no segmentation fault thus far"<<endl;
 

      //test varaibles here
   }
   catch(NcException e)
   {
      e.what();
      return 1;
   }
   try
   {
      cout<<"should test adding a variable with more than 5 dimensions here"<<endl;
      // test creating a variable with more than 5 dimensions
   } 
   catch (NcException e)
   {
      e.what();
      return 1;
   }


 
   try  //write the file with float's b/c that's all NcValues can handle at the moment
    {  
       int NX = 6;
      int NY = 12;
       float dataOut[NX][NY];
       
  // Create some pretend data. If this wasn't an example program, we
  // would have some real data to write, for example, model output.
  for(int i = 0; i < NX; i++)
    for(int j = 0; j < NY; j++)
       dataOut[i][j] = i * NY + j;
 
  // The default behavior of the C++ API is to throw an exception i
  // an error occurs. A try catch block in necessary.
   
      // Create the file. The Replace parameter tells netCDF to overwrite
      // this file, if it already exists.
      string filename ="simples_xy.nc"; 
      NcFile dataFile(filename, NcFile::Replace);
      
      
      // When we create netCDF dimensions, we get back a pointer to an
      // NcDim for each one.
      NcDim* xDim = dataFile.addDim("x", NX);
      NcDim* yDim = dataFile.addDim("y", NY);
      
      // Define the variable. The type of the variable in this case is
      // ncInt (32-bit integer).
   NcVar *data = dataFile.addVar("data", ncFloat, xDim, yDim);
   
   // Write the pretend data to the file. Although netCDF supports
   // reading and writing subsets of data, in this case we write all
   // the data in one operation.
   data->put(&dataOut[0][0], NX, NY,0,0,0);
   
   // The file will be automatically close when the NcFile object goes
   // out of scope. This frees up any internal netCDF resources
   // associated with the file, and flushes any buffers.
   
   cout << "*** SUCCESS writing example file simples_xy.nc!" << endl;
    }
  catch(std::exception e)
    {e.what();}


   try
   {
      int NX = 6;
      int NY = 12;

// Return this in event of a problem.
      // int NC_ERR = 2;
      // This is the array we will read.
      float dataIn[NX][NY]; 

      // Open the file. The ReadOnly parameter tells netCDF we want
      // read-only access to the file.
      NcFile dataFile("simples_xy.nc", NcFile::ReadOnly);

      // Retrieve the variable named "data"
      NcVar *data = dataFile.getVar("data");
      //call getType on data

      // Read all the values from the "data" variable into memory. 
      data->get(&dataIn[0][0], NX, NY);
      // Check the values. 
      for (int i = 0; i < NX; i++)
	 for (int j = 0; j < NY; j++)
	    if (dataIn[i][j] != i * NY + j)
	       return 1;
	    
      NcValues* dataVar= data->getValues();
      cout<<dataVar->toString()<<endl;;
      dataVar->print(cout);
    
   }
   catch(NcException e)
   {
      e.what();
      return 1;
   }
      cout<<"***************** Testing Variables was successful *****************"<<endl;
      return 0;
}