"Fossies" - the Fresh Open Source Software Archive

Member "netcdf-cxx4-4.3.1/examples/pres_temp_4D_plugin_wr.cpp" (11 Sep 2019, 6236 Bytes) of package /linux/misc/netcdf-cxx4-4.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* This is part of the netCDF package.
    2    Copyright 2006 University Corporation for Atmospheric Research/Unidata.
    3    See COPYRIGHT file for conditions of use.
    4 
    5    This is an example program which writes some 4D pressure and
    6    temperatures. This example demonstrates the netCDF C++ API.
    7 
    8    This is part of the netCDF tutorial:
    9    http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-tutorial
   10 
   11    Full documentation of the netCDF C++ API can be found at:
   12    http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-cxx
   13 
   14    $Id: pres_temp_4D_wr.cpp,v 1.6 2010/02/11 22:36:42 russ Exp $
   15 */
   16 
   17 #include <netcdf>
   18 #include <iostream>
   19 #include <string>
   20 using namespace std;
   21 using namespace netCDF;
   22 using namespace netCDF::exceptions;
   23 
   24 // This is the name of the data file we will create.
   25 #define FILE_NAME "pres_temp_plugin_4D.nc"
   26 
   27 // We are writing 4D data, a 2 x 6 x 12 lvl-lat-lon grid, with 2
   28 // timesteps of data.
   29 #define NDIMS    4
   30 #define NLVL     2
   31 #define NLAT     6
   32 #define NLON     12
   33 #define NREC     2
   34 
   35 // Filter specs.
   36 #define BZIP2_ID 307
   37 #define BZIP2_LEVEL 9
   38 #define BZIP2_NPARAMS 1
   39 unsigned int level = BZIP2_LEVEL;
   40 unsigned int idp = BZIP2_ID;
   41 size_t nparamsp = BZIP2_NPARAMS;
   42 
   43 // Names of things.
   44 #define LVL_NAME "level"
   45 #define LAT_NAME "latitude"
   46 #define LON_NAME "longitude"
   47 #define REC_NAME "time"
   48 #define PRES_NAME     "pressure"
   49 #define TEMP_NAME     "temperature"
   50 #define MAX_ATT_LEN  80
   51 // These are used to construct some example data.
   52 #define SAMPLE_PRESSURE 900
   53 #define SAMPLE_TEMP     9.0
   54 #define START_LAT       25.0
   55 #define START_LON       -125.0
   56 
   57 
   58 string  UNITS = "units";
   59 string  DEGREES_EAST =  "degrees_east";
   60 string  DEGREES_NORTH = "degrees_north";
   61 
   62 
   63 // For the units attributes.
   64 string PRES_UNITS = "hPa";
   65 string TEMP_UNITS = "celsius";
   66 string LAT_UNITS = "degrees_north";
   67 string LON_UNITS = "degrees_east";
   68 
   69 // Return this code to the OS in case of failure.
   70 #define NC_ERR 2
   71 
   72 int main()
   73 {
   74   nc_set_log_level(5);
   75   // We will write latitude and longitude fields.
   76    float lats[NLAT],lons[NLON];
   77 
   78    // Program variables to hold the data we will write out. We will
   79    // only need enough space to hold one timestep of data; one record.
   80    float pres_out[NLVL][NLAT][NLON];
   81    float temp_out[NLVL][NLAT][NLON];
   82 
   83    int i=0;  //used in the data generation loop
   84 
   85    // create some pretend data. If this wasn't an example program, we
   86    // would have some real data to write for example, model output.
   87    for (int lat = 0; lat < NLAT; lat++)
   88       lats[lat] = START_LAT + 5. * lat;
   89    for (int lon = 0; lon < NLON; lon++)
   90       lons[lon] = START_LON + 5. * lon;
   91 
   92    for (int lvl = 0; lvl < NLVL; lvl++)
   93      for (int lat = 0; lat < NLAT; lat++)
   94        for (int lon = 0; lon < NLON; lon++)
   95      {
   96        pres_out[lvl][lat][lon] =(float) (SAMPLE_PRESSURE + i);
   97        temp_out[lvl][lat][lon]  = (float)(SAMPLE_TEMP + i++);
   98      }
   99 
  100    try
  101    {
  102 
  103 
  104       // Create the file.
  105       NcFile test(FILE_NAME, NcFile::replace);
  106       nc_set_log_level(5);
  107       // Define the dimensions. NetCDF will hand back an ncDim object for
  108       // each.
  109       NcDim lvlDim = test.addDim(LVL_NAME, NLVL);
  110       NcDim latDim = test.addDim(LAT_NAME, NLAT);
  111       NcDim lonDim = test.addDim(LON_NAME, NLON);
  112       NcDim recDim = test.addDim(REC_NAME);  //adds an unlimited dimension
  113 
  114       // Define the coordinate variables.
  115       NcVar latVar = test.addVar(LAT_NAME, ncFloat, latDim);
  116       NcVar lonVar = test.addVar(LON_NAME, ncFloat, lonDim);
  117 
  118       // Define units attributes for coordinate vars. This attaches a
  119       // text attribute to each of the coordinate variables, containing
  120       // the units.
  121       latVar.putAtt(UNITS, DEGREES_NORTH);
  122       lonVar.putAtt(UNITS, DEGREES_EAST);
  123 
  124       // Define the netCDF variables for the pressure and temperature
  125       // data.
  126       vector<NcDim> dimVector;
  127       dimVector.push_back(recDim);
  128       dimVector.push_back(lvlDim);
  129       dimVector.push_back(latDim);
  130       dimVector.push_back(lonDim);
  131       NcVar pressVar = test.addVar(PRES_NAME, ncFloat, dimVector);
  132       NcVar tempVar = test.addVar(TEMP_NAME, ncFloat, dimVector);
  133 
  134       // Define units attributes for coordinate vars. This attaches a
  135       // text attribute to each of the coordinate variables, containing
  136       // the units.
  137       pressVar.putAtt(UNITS, PRES_UNITS);
  138       tempVar.putAtt(UNITS, TEMP_UNITS);
  139 
  140       // Write the coordinate variable data to the file.
  141 
  142       //latVar.putVar(lats);
  143       //lonVar.putVar(lons);
  144       vector<size_t> chunks;
  145       for(int i = 0; i < NDIMS; i++) {
  146         chunks.push_back(4);
  147       }
  148 
  149       latVar.setChunking(NcVar::nc_CHUNKED,chunks);
  150       //Testing the filter ability in a write function
  151       cout<<"BZIP2_ID: " << BZIP2_ID <<"BZIP2_NPARAMS: "<< BZIP2_NPARAMS << " level: "<< level;
  152       latVar.setFilter(BZIP2_ID,BZIP2_NPARAMS,&level);
  153 
  154       latVar.getFilter(&idp,&nparamsp, &level);
  155 
  156 
  157 
  158       latVar.putVar(lats);
  159       lonVar.putVar(lons);
  160 
  161       // Write the pretend data. This will write our surface pressure and
  162       // surface temperature data. The arrays only hold one timestep
  163       // worth of data. We will just rewrite the same data for each
  164       // timestep. In a real application, the data would change between
  165       // timesteps.
  166       vector<size_t> startp,countp;
  167       startp.push_back(0);
  168       startp.push_back(0);
  169       startp.push_back(0);
  170       startp.push_back(0);
  171       countp.push_back(1);
  172       countp.push_back(NLVL);
  173       countp.push_back(NLAT);
  174       countp.push_back(NLON);
  175       for (size_t rec = 0; rec < NREC; rec++)
  176         {
  177           startp[0]=rec;
  178           pressVar.putVar(startp,countp,pres_out);
  179           tempVar.putVar(startp,countp,temp_out);
  180         }
  181 
  182       // The file is automatically closed by the destructor. This frees
  183       // up any internal netCDF resources associated with the file, and
  184       // flushes any buffers.
  185 
  186       //cout << "*** SUCCESS writing example file " << FILE_NAME << "!" << endl;
  187       test.close();
  188       return 0;
  189    }
  190    catch(NcException& e)
  191    {
  192       e.what();
  193       return NC_ERR;
  194    }
  195 }