Esempio n. 1
0
int main()
{
  // This is the data array we will write. It will just be filled
  // with a progression of numbers for this example.
  int dataOut[NX][NY];
  
  // Create some pretend data. If this wasn't an example program, we
  // would have some real data to write, for example, model output.
  for(int i = 0; i < NX; i++)
    for(int j = 0; j < NY; j++)
      dataOut[i][j] = i * NY + j;
  
  // The default behavior of the C++ API is to throw an exception i
  // an error occurs. A try catch block is necessary.
   
  try
    {  
      // Create the file. The Replace parameter tells netCDF to overwrite
      // this file, if it already exists.
      NcFile dataFile("simple_xy.nc", NcFile::replace);
      
      // Create netCDF dimensions
      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).
      vector<NcDim> dims;
      dims.push_back(xDim);
      dims.push_back(yDim);
      NcVar data = dataFile.addVar("data", ncInt, dims);
   
      // Write the 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.putVar(dataOut);
      
      // The file will be automatically close when the NcFile object goes
      // out of scope. This frees up any internal netCDF resources
      // associated with the file, and flushes any buffers.
      
      //cout << "*** SUCCESS writing example file simple_xy.nc!" << endl;
      return 0; 
    }
  catch(NcException& e)
    {e.what();
      return NC_ERR;
    }
}
int main()
{
   // We will write latitude and longitude fields. 
   float lats[NLAT],lons[NLON];

   // Program variables to hold the data we will write out. We will
   // only need enough space to hold one timestep of data; one record.
   float pres_out[NLVL][NLAT][NLON];
   float temp_out[NLVL][NLAT][NLON];

   int i=0;  //used in the data generation loop
  
   // 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;

   for (int lvl = 0; lvl < NLVL; lvl++)
     for (int lat = 0; lat < NLAT; lat++)
       for (int lon = 0; lon < NLON; lon++)
	 {
	   pres_out[lvl][lat][lon] =(float) (SAMPLE_PRESSURE + i);
	   temp_out[lvl][lat][lon]  = (float)(SAMPLE_TEMP + i++);
	 }
   
   try
   {
    
   
      // Create the file.
      NcFile test(FILE_NAME, NcFile::replace);

      // Define the dimensions. NetCDF will hand back an ncDim object for
      // each.
      NcDim lvlDim = test.addDim(LVL_NAME, NLVL);
      NcDim latDim = test.addDim(LAT_NAME, NLAT);
      NcDim lonDim = test.addDim(LON_NAME, NLON);
      NcDim recDim = test.addDim(REC_NAME);  //adds an unlimited dimension
       
      // 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.putAtt(UNITS, DEGREES_NORTH);
      lonVar.putAtt(UNITS, DEGREES_EAST);
       
      // Define the netCDF variables for the pressure and temperature
      // data.
      vector<NcDim> dimVector;
      dimVector.push_back(recDim);
      dimVector.push_back(lvlDim);
      dimVector.push_back(latDim);
      dimVector.push_back(lonDim);
      NcVar pressVar = test.addVar(PRES_NAME, ncFloat, dimVector);
      NcVar tempVar = test.addVar(TEMP_NAME, ncFloat, dimVector);
       
      // Define units attributes for coordinate vars. This attaches a
      // text attribute to each of the coordinate variables, containing
      // the units.
      pressVar.putAtt(UNITS, PRES_UNITS);
      tempVar.putAtt(UNITS, TEMP_UNITS);

      // Write the coordinate variable data to the file.
      latVar.putVar(lats);
      lonVar.putVar(lons);
            
      // Write the pretend data. This will write our surface pressure and
      // surface temperature data. The arrays only hold one timestep
      // worth of data. We will just rewrite the same data for each
      // timestep. In a real application, the data would change between
      // timesteps.
      vector<size_t> startp,countp;
      startp.push_back(0);
      startp.push_back(0);
      startp.push_back(0);
      startp.push_back(0);
      countp.push_back(1);
      countp.push_back(NLVL);
      countp.push_back(NLAT);
      countp.push_back(NLON);
      for (size_t rec = 0; rec < NREC; rec++) 
      {
	startp[0]=rec;
	pressVar.putVar(startp,countp,pres_out);
	tempVar.putVar(startp,countp,temp_out);
      }

      // The file is automatically closed by the destructor. This frees
      // up any internal netCDF resources associated with the file, and
      // flushes any buffers.
   
      //cout << "*** SUCCESS writing example file " << FILE_NAME << "!" << endl;
      return 0;
   }
   catch(NcException& e)
   {
      e.what(); 
      return NC_ERR;
   }
}
Esempio n. 3
0
int main(void)
{
    // We will write surface temperature and pressure fields.
    float presOut[NLAT][NLON];
    float tempOut[NLAT][NLON];
    float lats[NLAT];
    float lons[NLON];

    // In addition to the latitude and longitude dimensions, we will
    // also create latitude and longitude netCDF variables which will
    // hold the actual latitudes and longitudes. Since they hold data
    // about the coordinate system, the netCDF term for these is:
    // "coordinate variables."
    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 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++)
        for(int lon = 0; lon < NLON; lon++)
        {
            presOut[lat][lon] = SAMPLE_PRESSURE + (lon * NLAT + lat);
            tempOut[lat][lon] = SAMPLE_TEMP + .25 * (lon * NLAT +lat);
        }

    try
    {

        // Create the file. The Replace parameter tells netCDF to overwrite
        // this file, if it already exists.
        NcFile sfc(FILE_NAME, NcFile::replace);

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

        // Define coordinate netCDF variables. They will hold the
        // coordinate information, that is, the latitudes and
        // longitudes. An pointer to a NcVar object is returned for
        // each.
        NcVar latVar = sfc.addVar(LAT_NAME, ncFloat, latDim);//creates variable
        NcVar lonVar = sfc.addVar(LON_NAME, ncFloat, lonDim);

        // Write the coordinate variable data. This will put the latitudes
        // and longitudes of our data grid into the netCDF file.
        latVar.putVar(lats);
        lonVar.putVar(lons);

        // Define units attributes for coordinate vars. This attaches a
        // text attribute to each of the coordinate variables, containing
        // the units. Note that we are not writing a trailing NULL, just
        // "units", because the reading program may be fortran which does
        // not use null-terminated strings. In general it is up to the
        // reading C program to ensure that it puts null-terminators on
        // strings where necessary.
        lonVar.putAtt(UNITS,DEGREES_EAST);
        latVar.putAtt(UNITS,DEGREES_NORTH);

        // Define the netCDF data variables.
        vector<NcDim> dims;
        dims.push_back(latDim);
        dims.push_back(lonDim);
        NcVar presVar = sfc.addVar(PRES_NAME, ncFloat, dims);
        NcVar tempVar = sfc.addVar(TEMP_NAME, ncFloat, dims);

        // Define units attributes for vars.
        presVar.putAtt(UNITS,"hPa");
        tempVar.putAtt(UNITS,"celsius");

        // Write the pretend data. This will write our surface pressure and
        // surface temperature data. The arrays of data are the same size
        // as the netCDF variables we have defined.
        presVar.putVar(presOut);
        tempVar.putVar(tempOut);

        // The file is automatically closed by the destructor. This frees
        // up any internal netCDF resources associated with the file, and
        // flushes any buffers.

        //cout << "*** SUCCESS writing example file " << FILE_NAME << "!" << endl;
        return 0;
    }
    catch(NcException& e)
    {
        e.what();
        return NC_ERR;
    }
}