#include<stdio.h>
#include<stdlib.h>
#include<netcdf.h> // for reading NR files
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
int main(void){
// file name
char* T_file = "http://opendap.nccs.nasa.gov:9090/dods/OSSE/GEOS-5.12/BETA9/0.5000_deg/inst/inst01hr_3d_T_Cv";
// netCDF ID for the file and data variable
int ncid, varid;
// global 4D array: (time,lev,lat,lon), one time step
const int IM = 720;
const int JM = 361;
const int LM = 72;
const int asyz = 1*LM*JM*IM;
float *T = NULL;
// hypercube for reading one array for a given time
size_t start[4] = {36, 0, 0, 0}; // time step 37
size_t count[4] = {1, LM, JM, IM}; // 1 time step, 3D (lon,lat,lev) array
// return code
int rc;
// min/max values
float minval, maxval;
// misc counters
int ctr;
// allocate memory for T
T = malloc(asyz*sizeof(float));
// read the data file
printf("Reading T..."); fflush(stdout);
if (rc = nc_open(T_file, NC_NOWRITE, &ncid)) ERR(rc);
if (rc = nc_inq_varid(ncid, "t", &varid)) ERR(rc);
if (rc = nc_get_vara_float(ncid, varid, start, count, T)) ERR(rc);
printf("done.\n"); fflush(stdout);
// min/max of T
minval = 1.0e15;
maxval = -1.0e15;
for (ctr=0; ctr<asyz; ctr++){
if (T[ctr]<minval){
minval = T[ctr];
}
if (T[ctr]>maxval){
maxval = T[ctr];
}
}
printf("min(T): %f\n", minval);
printf("max(T): %f\n", maxval);
// free memory
free(T);
return 0;
}