File: cdi_read.c

package info (click to toggle)
cdo 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 49,836 kB
  • sloc: cpp: 185,271; ansic: 95,766; sh: 7,192; f90: 6,147; makefile: 1,977; ruby: 1,078; csh: 1,028; python: 995; fortran: 319; pascal: 219; perl: 9
file content (56 lines) | stat: -rw-r--r-- 1,282 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include "cdi.h"

int
main(void)
{
  enum
  {
    nlon = 12,  // Number of longitudes
    nlat = 6,   // Number of latitudes
    nlev = 5,   // Number of levels
    nts = 3,    // Number of time steps
  };
  SizeType numMissVals;
  double var1[nlon * nlat];
  double var2[nlon * nlat * nlev];

  // Open the dataset
  int streamID = streamOpenRead("example.nc");
  if (streamID < 0)
  {
    fprintf(stderr, "%s\n", cdiStringError(streamID));
    return 1;
  }

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

  // Set the variable IDs
  int varID1 = 0;
  int varID2 = 1;

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

  // Loop over the number of time steps
  for (int tsID = 0; tsID < nts; tsID++)
  {
    // Inquire the time step
    streamInqTimestep(streamID, tsID);

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

    // Read var1 and var2
    streamReadVar(streamID, varID1, var1, &numMissVals);
    streamReadVar(streamID, varID2, var2, &numMissVals);
  }

  // Close the input stream
  streamClose(streamID);

  return 0;
}