cdi_read.c 1.28 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
#include <stdio.h>
2
3
4
5
#include "cdi.h"

int main(void)
{
6
7
8
9
  const int nlon = 12; // Number of longitudes
  const int nlat =  6; // Number of latitudes
  const int nlev =  5; // Number of levels
  const int nts  =  3; // Number of time steps
10
  int nmiss, vdate, vtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
11
12
  double var1[nlon*nlat];
  double var2[nlon*nlat*nlev];
13
14


15
  // Open the dataset 
16
  int streamID = streamOpenRead("example.nc");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
17
18
19
  if ( streamID < 0 )
    {
      fprintf(stderr, "%s\n", cdiStringError(streamID));
20
      return 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
    }
22

23
  // Get the variable list of the dataset 
24
  int vlistID = streamInqVlist(streamID);
25

26
  // Set the variable IDs 
27
28
  int varID1 = 0;
  int varID2 = 1;
29

30
  // Get the Time axis from the variable list 
31
  int taxisID = vlistInqTaxis(vlistID);
32

33
  // Loop over the number of time steps 
34
  for ( int tsID = 0; tsID < nts; tsID++ )
35
    {
36
      // Inquire the time step 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
      streamInqTimestep(streamID, tsID);

39
      // Get the verification date and time 
40
41
      vdate = taxisInqVdate(taxisID);
      vtime = taxisInqVtime(taxisID);
42
      printf("read timestep %d:  date=%d  time=%d\n", tsID+1, vdate, vtime);
43

44
      // Read var1 and var2 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
46
      streamReadVar(streamID, varID1, var1, &nmiss);
      streamReadVar(streamID, varID2, var2, &nmiss);
47
48
    }

49
  // Close the input stream 
50
51
52
53
  streamClose(streamID);

  return 0;
}