cdi_copy.c 1.54 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;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
11
12
  double var1[nlon*nlat];
  double var2[nlon*nlat*nlev];
13
14


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

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

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
  // Open the output dataset (GRIB format)
31
  int streamID2  = streamOpenWrite("example.grb", FILETYPE_GRB);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32
33
34
  if ( streamID2 < 0 )
    {
      fprintf(stderr, "%s\n", cdiStringError(streamID2));
35
      return 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
    }
37

38
  int vlistID2 = vlistDuplicate(vlistID1);
39

Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
  streamDefVlist(streamID2, vlistID2);
41

42
  // Loop over the number of time steps
43
  for ( int tsID = 0; tsID < nts; tsID++ )
44
    {
45
      // Inquire the input time step
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
47
      streamInqTimestep(streamID1, tsID);

48
      // Define the output time step
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
      streamDefTimestep(streamID2, tsID);

51
      // Read var1 and var2 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
53
54
      streamReadVar(streamID1, varID1, var1, &nmiss);
      streamReadVar(streamID1, varID2, var2, &nmiss);

55
      // Write var1 and var2
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
57
58
      streamWriteVar(streamID2, varID1, var1, nmiss);
      streamWriteVar(streamID2, varID2, var2, nmiss);
    }
59

60
  // Close the streams
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
62
  streamClose(streamID1);
  streamClose(streamID2);
63
64
65

  return 0;
}