cdi_write.c 2.51 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
10
11
12
13
  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
  int nmiss = 0;
  double lons[] = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
  double lats[] = {-75, -45, -15, 15, 45, 75};
  double levs[] = {101300, 92500, 85000, 50000, 20000};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14
15
  double var1[nlon*nlat];
  double var2[nlon*nlat*nlev];
16

Uwe Schulzweida's avatar
Uwe Schulzweida committed
17

18
  // Create a regular lon/lat grid
19
  int gridID = gridCreate(GRID_LONLAT, nlon*nlat);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
20
21
  gridDefXsize(gridID, nlon);
  gridDefYsize(gridID, nlat);
22
23
24
  gridDefXvals(gridID, lons);
  gridDefYvals(gridID, lats);

25
  // Create a surface level Z-axis
26
  int zaxisID1 = zaxisCreate(ZAXIS_SURFACE, 1);
27

28
  // Create a pressure level Z-axis
29
  int zaxisID2 = zaxisCreate(ZAXIS_PRESSURE, nlev);
30
31
  zaxisDefLevels(zaxisID2, levs);
 
32
  // Create a variable list
33
  int vlistID = vlistCreate();
34

35
  // Define the variables
36
37
  int varID1 = vlistDefVar(vlistID, gridID, zaxisID1, TSTEP_INSTANT);
  int varID2 = vlistDefVar(vlistID, gridID, zaxisID2, TSTEP_INSTANT);
38

39
  // Define the variable names
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
  vlistDefVarName(vlistID, varID1, "varname1");
41
42
  vlistDefVarName(vlistID, varID2, "varname2");

43
  // Create a Time axis
44
  int taxisID = taxisCreate(TAXIS_ABSOLUTE);
45

46
  // Assign the Time axis to the variable list
47
48
  vlistDefTaxis(vlistID, taxisID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
  // Create a dataset in netCDF format
50
  int streamID = streamOpenWrite("example.nc", FILETYPE_NC);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
52
53
  if ( streamID < 0 )
    {
      fprintf(stderr, "%s\n", cdiStringError(streamID));
54
      return 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
    }
56

57
  // Assign the variable list to the dataset
58
59
  streamDefVlist(streamID, vlistID);

60
  // Loop over the number of time steps
61
  for ( int tsID = 0; tsID < nts; tsID++ )
62
    {
63
      // Set the verification date to 1985-01-01 + tsID
64
      taxisDefVdate(taxisID, 19850101+tsID);
65
      // Set the verification time to 12:00:00
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
      taxisDefVtime(taxisID, 120000);
67
      // Define the time step
68
69
      streamDefTimestep(streamID, tsID);

70
      // Init var1 and var2
71
72
      for ( int i = 0; i < nlon*nlat;      i++ ) var1[i] = 1.1;
      for ( int i = 0; i < nlon*nlat*nlev; i++ ) var2[i] = 2.2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73

74
      // Write var1 and var2
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
      streamWriteVar(streamID, varID1, var1, nmiss);
76
77
78
      streamWriteVar(streamID, varID2, var2, nmiss);
    }

79
  // Close the output stream
80
81
  streamClose(streamID);

82
  // Destroy the objects
83
84
85
86
87
88
89
90
  vlistDestroy(vlistID);
  taxisDestroy(taxisID);
  zaxisDestroy(zaxisID1);
  zaxisDestroy(zaxisID2);
  gridDestroy(gridID);

  return 0;
}