netCDF 4.2.1.1
/usr/src/RPM/BUILD/libnetcdf7-seq-4.2.1.1/examples/C/pres_temp_4D_rd.c
Go to the documentation of this file.
00001 
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include <netcdf.h>
00021 
00022 /* This is the name of the data file we will read. */
00023 #define FILE_NAME "pres_temp_4D.nc"
00024 
00025 /* We are reading 4D data, a 2 x 6 x 12 lvl-lat-lon grid, with 2
00026    timesteps of data. */
00027 #define NDIMS 4
00028 #define NLAT 6
00029 #define NLON 12
00030 #define LAT_NAME "latitude"
00031 #define LON_NAME "longitude"
00032 #define NREC 2
00033 #define REC_NAME "time"
00034 #define LVL_NAME "level"
00035 #define NLVL 2
00036 
00037 /* Names of things. */
00038 #define PRES_NAME "pressure"
00039 #define TEMP_NAME "temperature"
00040 #define UNITS "units"
00041 #define DEGREES_EAST "degrees_east"
00042 #define DEGREES_NORTH "degrees_north"
00043 
00044 /* These are used to calculate the values we expect to find. */
00045 #define SAMPLE_PRESSURE 900
00046 #define SAMPLE_TEMP 9.0
00047 #define START_LAT 25.0
00048 #define START_LON -125.0
00049 
00050 /* For the units attributes. */
00051 #define UNITS "units"
00052 #define PRES_UNITS "hPa"
00053 #define TEMP_UNITS "celsius"
00054 #define LAT_UNITS "degrees_north"
00055 #define LON_UNITS "degrees_east"
00056 #define MAX_ATT_LEN 80
00057 
00058 /* Handle errors by printing an error message and exiting with a
00059  * non-zero status. */
00060 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
00061 
00062 int
00063 main()
00064 {
00065    int ncid, pres_varid, temp_varid;
00066    int lat_varid, lon_varid;
00067 
00068    /* The start and count arrays will tell the netCDF library where to
00069       read our data. */
00070    size_t start[NDIMS], count[NDIMS];
00071 
00072    /* Program variables to hold the data we will read. We will only
00073       need enough space to hold one timestep of data; one record. */
00074    float pres_in[NLVL][NLAT][NLON];
00075    float temp_in[NLVL][NLAT][NLON];
00076 
00077    /* These program variables hold the latitudes and longitudes. */
00078    float lats[NLAT], lons[NLON];
00079 
00080    /* Loop indexes. */
00081    int lvl, lat, lon, rec, i = 0;
00082    
00083    /* Error handling. */
00084    int retval;
00085 
00086    /* Open the file. */
00087    if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
00088       ERR(retval);
00089 
00090    /* Get the varids of the latitude and longitude coordinate
00091     * variables. */
00092    if ((retval = nc_inq_varid(ncid, LAT_NAME, &lat_varid)))
00093       ERR(retval);
00094    if ((retval = nc_inq_varid(ncid, LON_NAME, &lon_varid)))
00095       ERR(retval);
00096 
00097    /* Read the coordinate variable data. */
00098    if ((retval = nc_get_var_float(ncid, lat_varid, &lats[0])))
00099       ERR(retval);
00100    if ((retval = nc_get_var_float(ncid, lon_varid, &lons[0])))
00101       ERR(retval);
00102 
00103    /* Check the coordinate variable data. */
00104    for (lat = 0; lat < NLAT; lat++)
00105       if (lats[lat] != START_LAT + 5.*lat)
00106          return 2;
00107    for (lon = 0; lon < NLON; lon++)
00108       if (lons[lon] != START_LON + 5.*lon)
00109          return 2;
00110 
00111    /* Get the varids of the pressure and temperature netCDF
00112     * variables. */
00113    if ((retval = nc_inq_varid(ncid, PRES_NAME, &pres_varid)))
00114       ERR(retval);
00115    if ((retval = nc_inq_varid(ncid, TEMP_NAME, &temp_varid)))
00116       ERR(retval);
00117 
00118    /* Read the data. Since we know the contents of the file we know
00119     * that the data arrays in this program are the correct size to
00120     * hold one timestep. */
00121    count[0] = 1;
00122    count[1] = NLVL;
00123    count[2] = NLAT;
00124    count[3] = NLON;
00125    start[1] = 0;
00126    start[2] = 0;
00127    start[3] = 0;
00128 
00129    /* Read and check one record at a time. */
00130    for (rec = 0; rec < NREC; rec++)
00131    {
00132       start[0] = rec;
00133       if ((retval = nc_get_vara_float(ncid, pres_varid, start, 
00134                                       count, &pres_in[0][0][0])))
00135          ERR(retval);
00136       if ((retval = nc_get_vara_float(ncid, temp_varid, start,
00137                                       count, &temp_in[0][0][0])))
00138          ERR(retval);
00139 
00140       /* Check the data. */
00141       i = 0;
00142       for (lvl = 0; lvl < NLVL; lvl++)
00143          for (lat = 0; lat < NLAT; lat++)
00144             for (lon = 0; lon < NLON; lon++)
00145             {
00146                if (pres_in[lvl][lat][lon] != SAMPLE_PRESSURE + i) 
00147                   return 2;
00148                if (temp_in[lvl][lat][lon] != SAMPLE_TEMP + i) 
00149                   return 2;
00150                i++;
00151             }
00152 
00153    } /* next record */
00154 
00155    /* Close the file. */
00156    if ((retval = nc_close(ncid)))
00157       ERR(retval);
00158 
00159    printf("*** SUCCESS reading example file pres_temp_4D.nc!\n");
00160    return 0;
00161 }
 All Data Structures Files Functions Variables Typedefs Defines

Generated on Tue Aug 6 2013 11:40:56 for netCDF. NetCDF is a Unidata library.