test_resource_copy.c 6.47 KB
Newer Older
1
2
3
4
5
#if defined (HAVE_CONFIG_H)
#include "config.h"
#endif

#include <stdio.h>
6
#include <string.h>
7
8

#include "cdi.h"
9
#include "cdi_uuid.h"
10
#include "dmemory.h"
11
#include "error.h"
12
13
#include "resource_handle.h"
#include "resource_unpack.h"
14
15
16
17

#ifdef MPI_MARSHALLING
#include <mpi.h>
#include "cdipio.h"
18
#include "pio_serialize.h"
19
#include "pio_util.h"
20
21
22
#else
typedef int MPI_Comm;
#endif
23
24

enum {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
  DOUBLE_PRECISION = CDI_DATATYPE_FLT64,
26
27
28
29
30
  nlon             = 12,
  nlat             = 6,
  nlev             = 5,
  ntsteps          = 3 };

31
32
33
static double lons[nlon] = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
static double lats[nlat] = {-75, -45, -15, 15, 45, 75};
static double levs[nlev] = {101300, 92500, 85000, 50000, 20000};
34

35
static int defineGrid (void)
36
37
38
39
40
41
42
{
  int mask_vec[nlon*nlat];
  const int * mp = &mask_vec[0];
  double area_vec[nlon*nlat];
  const double * ap = &area_vec[0];
  int i;

43
  int gridID = gridCreate(GRID_LONLAT, nlon*nlat);
44
45
46
47
48
49
50
51
52
53
54
55
56
  gridDefXsize(gridID, nlon);
  gridDefYsize(gridID, nlat);
  gridDefXvals(gridID, lons);
  gridDefYvals(gridID, lats);
  gridDefNvertex ( gridID, 1 );
  gridDefXbounds ( gridID, lons );
  gridDefYbounds ( gridID, lats );
  for ( i = 0; i < nlon*nlat; i++ )
    mask_vec[i] = i % 2 ;
  gridDefMaskGME ( gridID, mp );
  for ( i = 0; i < nlon*nlat; i++ )
    mask_vec[i] = 1;
  gridDefMask ( gridID, mp );
57
58
59
60
61
62
63
64

  cdiDefKeyString(gridID, CDI_XAXIS, CDI_KEY_NAME, "myXname");
  cdiDefKeyString(gridID, CDI_YAXIS, CDI_KEY_NAME, "myYname");
  cdiDefKeyString(gridID, CDI_XAXIS, CDI_KEY_LONGNAME, "myXlongname");
  cdiDefKeyString(gridID, CDI_YAXIS, CDI_KEY_LONGNAME, "myYlongname");
  cdiDefKeyString(gridID, CDI_XAXIS, CDI_KEY_UNITS, "myXunits");
  cdiDefKeyString(gridID, CDI_YAXIS, CDI_KEY_UNITS, "myYunits");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
  gridDefDatatype ( gridID, DOUBLE_PRECISION );
66
  gridDefTrunc ( gridID, 1 );
67
  gridDefParamGME ( gridID, 2, 3, 4, 5 );
68
69
70
71
72
73
74
75
  gridDefNumber ( gridID, 6 );
  gridDefPosition ( gridID, 7 );
  gridDefReference ( gridID, "myReference" );
  for ( i = 0; i < nlon*nlat; i++ )
    area_vec[i] = 0.1 * i;
  gridDefArea ( gridID, ap );
  for ( i = 0; i < nlon*nlat; i++ )
    mask_vec[i] = i;
76
  gridDefReducedPoints ( gridID, nlon*nlat, mp );
77
  gridDefComplexPacking ( gridID, 1 );
78
79
  {
    unsigned char uuid[CDI_UUID_SIZE];
80
    cdiCreateUUID(uuid);
81
82
    gridDefUUID(gridID, uuid);
  }
83
84
85
86

  return gridID;
}

87
static int defineZaxis (void)
88
89
90
{
  double vct[3] = { 3.0, 3.3, 3.6 };

91
  int zaxisID = zaxisCreate(ZAXIS_PRESSURE, nlev);
92
93
94
95
96
  zaxisDefLevels(zaxisID, levs);
  zaxisDefLevel ( zaxisID, 2, 8507.3 );
  zaxisDefName ( zaxisID, "myName" );
  zaxisDefLongname ( zaxisID, "myLongname" );
  zaxisDefUnits ( zaxisID, "myUnits" );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
  zaxisDefDatatype ( zaxisID, DOUBLE_PRECISION );
98
  cdiDefKeyInt(zaxisID, CDI_GLOBAL, CDI_KEY_TYPEOFFIRSTFIXEDSURFACE, 1);
99
100
101
102
  zaxisDefVct ( zaxisID, 3, vct );
  zaxisDefLbounds ( zaxisID, &levs[0] );
  zaxisDefUbounds ( zaxisID, &levs[0] );
  zaxisDefWeights ( zaxisID, &levs[0] );
103
104
  {
    unsigned char uuid[CDI_UUID_SIZE];
105
    cdiCreateUUID(uuid);
106
107
    zaxisDefUUID(zaxisID, uuid);
  }
108
109
110
111

  return zaxisID;
}

112
static int defineTaxis (void)
113
{
114
  int taxisID =  taxisCreate(TAXIS_ABSOLUTE);
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

  taxisDefType  ( taxisID, 0 );
  taxisDefVdate ( taxisID, 1 );
  taxisDefVtime ( taxisID, 2 );
  taxisDefRdate ( taxisID, 3 );
  taxisDefRtime ( taxisID, 4 );
  taxisDefVdateBounds ( taxisID, 5, 6 );
  taxisDefVtimeBounds ( taxisID, 7, 8 );
  taxisDefCalendar ( taxisID, 1 );
  taxisDefTunit ( taxisID, 1 );
  taxisDefNumavg ( taxisID, 1 );

  return taxisID;
}

130
static void defineStream ( int streamID, int vlistID )
131
132
133
134
135
136
137
{
  streamDefByteorder ( streamID, 1 );
  streamDefCompType  ( streamID, 2 );
  streamDefCompLevel ( streamID, 3 );
  streamDefVlist(streamID, vlistID);
}

138
139
140
141
struct idPair {
  int id1, id2;
};

142
static struct idPair defineVlist ( int gridID, int zaxisID, int taxisID )
143
144
145
146
147
{
  int vlistID = CDI_UNDEFID;
  int zaxisID2 = zaxisCreate(ZAXIS_SURFACE, 1);

  vlistID = vlistCreate();
148
149
  int varID1 = vlistDefVar(vlistID, gridID, zaxisID, TIME_VARIABLE);
  int varID2 = vlistDefVar(vlistID, gridID, zaxisID2, TIME_VARIABLE);
150
151
152
  vlistDefVarName(vlistID, varID1, "varname1");
  {
    int globfac[] = { 23, 42 };
153
    cdiDefAttInt(vlistID, varID1, "seer's globule factors", CDI_DATATYPE_INT16, 2, globfac);
154
155
  }
  vlistDefVarName(vlistID, varID2, "varname2");
156
  cdiDefAttTxt(vlistID, varID2, "txt demo", 6, "banana");
157
  vlistDefTaxis(vlistID, taxisID);
Thomas Jahns's avatar
Thomas Jahns committed
158
159
160
  int vlistID2 = vlistCreate();
  vlistDefVar(vlistID2, gridID, zaxisID, TIME_VARIABLE);
  vlistCopy(vlistID2, vlistID);
161
  return (struct idPair){ vlistID, vlistID2 };
162
163
}

164
static int defineInstitute ()
165
{
166
  int instID = institutDef( 0, 0,"MYINSTITUTE", "myInstitute");
167
168
169
  return instID;
}

170
static int defineModel ( int instID )
171
{
172
  int modelID = modelDef(instID, 0, "resource_copy");
173
174
175
  return modelID;
}

176
177
static int destNamespace;

178
static int modelRun(MPI_Comm comm)
179
180
{
  char * recvBuffer, * sendBuffer;
181
  int bufferSize;
182

183
#ifdef MPI_MARSHALLING
184
  cdiPioSerializeSetMPI();
185
#endif
186

187
188
189
190
  int gridID  = defineGrid      ();
  int zaxisID = defineZaxis     ();
  int taxisID = defineTaxis     ();
  int instID  = defineInstitute ();
Thomas Jahns's avatar
Thomas Jahns committed
191
  defineModel(instID);
192
193
194
195
196
197
198
199

  struct idPair temp = defineVlist(gridID, zaxisID, taxisID);
  int vlistID = temp.id1;
  int streamID = streamOpenWrite("example.grb", CDI_FILETYPE_GRB);
  if ( streamID < 0 ) xabort ( "Could not open file" );
  defineStream ( streamID, vlistID );
  vlistDestroy(temp.id1);
  vlistDestroy(temp.id2);
200

201
  reshPackBufferCreate ( &sendBuffer, &bufferSize, &comm );
202
  recvBuffer = (char *)malloc((size_t)bufferSize);
203
#ifdef MPI_MARSHALLING
204
205
206
  xmpi(MPI_Sendrecv(sendBuffer, bufferSize, MPI_PACKED, 0, 0,
                    recvBuffer, bufferSize, MPI_PACKED, 0, 0,
                    MPI_COMM_SELF, MPI_STATUS_IGNORE));
207
#else
208
  memcpy(recvBuffer, sendBuffer, (size_t)bufferSize);
209
210
#endif
  namespaceSetActive(destNamespace);
211
  reshUnpackResources(recvBuffer, bufferSize, &comm);
212
213
  free(recvBuffer);
  reshPackBufferDestroy(&sendBuffer);
214

215
  int differ = reshListCompare ( 0, 1 );
216

217
  namespaceSetActive(0);
218
  streamClose(streamID);
219
  return differ;
220
221
222
223
}

int main (int argc, char *argv[])
{
224
  int exitCode = 77;
225
226
  MPI_Comm commModel;
#ifdef MPI_MARSHALLING
227
  MPI_Init(&argc, &argv);
228
229
  commModel = MPI_COMM_WORLD;
#else
230
  (void)argc; (void)argv;
231
232
233
  commModel = 0;
#endif
  destNamespace = namespaceNew();
234

235
  exitCode = modelRun(commModel);
236

237
#ifdef MPI_MARSHALLING
238
  xmpi(MPI_Finalize());
239
240
#endif

241
  return exitCode;
242
243
244
245
246
247
248
249
250
251
252
}

/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */