netCDF  4.2.1.1
/usr/src/RPM/BUILD/libnetcdf7-seq-4.2.1.1/examples/C/sfc_pres_temp_rd.c
Go to the documentation of this file.
00001 
00014 #include <stdio.h>
00015 #include <string.h>
00016 #include <netcdf.h>
00017 
00018 /* This is the name of the data file we will read. */
00019 #define FILE_NAME "sfc_pres_temp.nc"
00020 
00021 /* We are reading 2D data, a 6 x 12 lat-lon grid. */
00022 #define NDIMS 2
00023 #define NLAT 6
00024 #define NLON 12
00025 
00026 #define LAT_NAME "latitude"
00027 #define LON_NAME "longitude"
00028 #define PRES_NAME "pressure"
00029 #define TEMP_NAME "temperature"
00030 
00031 /* These are used to calculate the values we expect to find. */
00032 #define SAMPLE_PRESSURE 900
00033 #define SAMPLE_TEMP 9.0
00034 #define START_LAT 25.0
00035 #define START_LON -125.0
00036 
00037 /* For the units attributes. */
00038 #define UNITS "units"
00039 #define PRES_UNITS "hPa"
00040 #define TEMP_UNITS "celsius"
00041 #define LAT_UNITS "degrees_north"
00042 #define LON_UNITS "degrees_east"
00043 #define MAX_ATT_LEN 80
00044 
00045 /* Handle errors by printing an error message and exiting with a
00046  * non-zero status. */
00047 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
00048 
00049 int
00050 main()
00051 {
00052    int ncid, pres_varid, temp_varid;
00053    int lat_varid, lon_varid;
00054 
00055    /* We will read surface temperature and pressure fields. */
00056    float pres_in[NLAT][NLON];
00057    float temp_in[NLAT][NLON];
00058 
00059    /* For the lat lon coordinate variables. */
00060    float lats_in[NLAT], lons_in[NLON];
00061 
00062    /* To check the units attributes. */
00063    char pres_units_in[MAX_ATT_LEN], temp_units_in[MAX_ATT_LEN];
00064    char lat_units_in[MAX_ATT_LEN], lon_units_in[MAX_ATT_LEN];
00065 
00066    /* We will learn about the data file and store results in these
00067       program variables. */
00068    int ndims_in, nvars_in, ngatts_in, unlimdimid_in;
00069 
00070    /* Loop indexes. */
00071    int lat, lon;
00072 
00073    /* Error handling. */
00074    int retval;
00075 
00076    /* Open the file. */
00077    if ((retval = nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
00078       ERR(retval);
00079 
00080    /* There are a number of inquiry functions in netCDF which can be
00081       used to learn about an unknown netCDF file. NC_INQ tells how
00082       many netCDF variables, dimensions, and global attributes are in
00083       the file; also the dimension id of the unlimited dimension, if
00084       there is one. */
00085    if ((retval = nc_inq(ncid, &ndims_in, &nvars_in, &ngatts_in, 
00086                         &unlimdimid_in)))
00087       ERR(retval);
00088 
00089    /* In this case we know that there are 2 netCDF dimensions, 4
00090       netCDF variables, no global attributes, and no unlimited
00091       dimension. */
00092    if (ndims_in != 2 || nvars_in != 4 || ngatts_in != 0 || 
00093        unlimdimid_in != -1) return 2;
00094 
00095    /* Get the varids of the latitude and longitude coordinate
00096     * variables. */
00097    if ((retval = nc_inq_varid(ncid, LAT_NAME, &lat_varid)))
00098       ERR(retval);
00099    if ((retval = nc_inq_varid(ncid, LON_NAME, &lon_varid)))
00100       ERR(retval);
00101 
00102    /* Read the coordinate variable data. */
00103    if ((retval = nc_get_var_float(ncid, lat_varid, &lats_in[0])))
00104       ERR(retval);
00105    if ((retval = nc_get_var_float(ncid, lon_varid, &lons_in[0])))
00106       ERR(retval);
00107 
00108    /* Check the coordinate variable data. */
00109    for (lat = 0; lat < NLAT; lat++)
00110       if (lats_in[lat] != START_LAT + 5.*lat)
00111          return 2;
00112    for (lon = 0; lon < NLON; lon++)
00113       if (lons_in[lon] != START_LON + 5.*lon)
00114          return 2;
00115 
00116    /* Get the varids of the pressure and temperature netCDF
00117     * variables. */
00118    if ((retval = nc_inq_varid(ncid, PRES_NAME, &pres_varid)))
00119       ERR(retval);
00120    if ((retval = nc_inq_varid(ncid, TEMP_NAME, &temp_varid)))
00121       ERR(retval);
00122 
00123    /* Read the data. Since we know the contents of the file we know
00124     * that the data arrays in this program are the correct size to
00125     * hold all the data. */
00126    if ((retval = nc_get_var_float(ncid, pres_varid, &pres_in[0][0])))
00127       ERR(retval);
00128    if ((retval = nc_get_var_float(ncid, temp_varid, &temp_in[0][0])))
00129       ERR(retval);
00130 
00131    /* Check the data. */
00132    for (lat = 0; lat < NLAT; lat++)
00133       for (lon = 0; lon < NLON; lon++)
00134          if (pres_in[lat][lon] != SAMPLE_PRESSURE + (lon * NLAT + lat) ||
00135              temp_in[lat][lon] != SAMPLE_TEMP + .25 * (lon * NLAT + lat))
00136             return 2;
00137 
00138    /* Each of the netCDF variables has a "units" attribute. Let's read
00139       them and check them. */
00140    if ((retval = nc_get_att_text(ncid, lat_varid, UNITS, lat_units_in)))
00141       ERR(retval);
00142    if (strncmp(lat_units_in, LAT_UNITS, strlen(LAT_UNITS))) 
00143       return 2;
00144 
00145    if ((retval = nc_get_att_text(ncid, lon_varid, UNITS, lon_units_in)))
00146       ERR(retval);
00147    if (strncmp(lon_units_in, LON_UNITS, strlen(LON_UNITS))) 
00148       return 2;
00149 
00150    if ((retval = nc_get_att_text(ncid, pres_varid, UNITS, pres_units_in)))
00151       ERR(retval);
00152    if (strncmp(pres_units_in, PRES_UNITS, strlen(PRES_UNITS)))
00153       return 2;
00154 
00155    if ((retval = nc_get_att_text(ncid, temp_varid, UNITS, temp_units_in)))
00156       ERR(retval);
00157    if (strncmp(temp_units_in, TEMP_UNITS, strlen(TEMP_UNITS))) return 2;
00158 
00159    /* Close the file. */
00160    if ((retval = nc_close(ncid)))
00161       ERR(retval);
00162 
00163    printf("*** SUCCESS reading example file sfc_pres_temp.nc!\n");
00164    return 0;
00165 }
 All Data Structures Files Functions Variables Typedefs Defines

Generated on Sat Sep 15 2012 12:44:32 for netCDF. NetCDF is a Unidata library.