test_resource_copy.c 6.88 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
25
26
27
28
29
30

enum {
  DOUBLE_PRECISION = 8,
  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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{
  int gridID = CDI_UNDEFID;
  int mask_vec[nlon*nlat];
  const int * mp = &mask_vec[0];
  double area_vec[nlon*nlat];
  const double * ap = &area_vec[0];
  int i;

  gridID = gridCreate(GRID_LONLAT, nlon*nlat);
  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 );
  gridDefXname ( gridID, "myXname" );
  gridDefXlongname ( gridID, "myXlongname" );
  gridDefXunits ( gridID, "myXunits" );
  gridDefYname ( gridID, "myYname" );
  gridDefYlongname ( gridID, "myYlongname" );
  gridDefYunits ( gridID, "myYunits" );
  gridDefPrec ( gridID, DOUBLE_PRECISION );
  gridDefTrunc ( gridID, 1 );
  gridDefGMEnd ( gridID, 2 );
  gridDefGMEni ( gridID, 3 );
  gridDefGMEni2 ( gridID, 4 );
  gridDefGMEni3 ( gridID, 5 );
  gridDefNumber ( gridID, 6 );
  gridDefPosition ( gridID, 7 );
  gridDefReference ( gridID, "myReference" );
/* gridDefLCC ( gridID, double originLon, double originLat,  */
/*         double lonParY, double lat1, double lat2, double xinc, double yinc, int projflag, int scanflag); */
/* gridDefLcc2 ( gridID, double earth_radius, double lon_0,  */
/*          double lat_0, double lat_1,double lat_2);*/
/* gridDefLaea ( gridID, double earth_radius, double lon_0, double lat_0); */
  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;
  gridDefRowlon ( gridID, nlon*nlat, mp );
  gridDefComplexPacking ( gridID, 1 );
85
86
  {
    unsigned char uuid[CDI_UUID_SIZE];
87
    cdiCreateUUID(uuid);
88
89
    gridDefUUID(gridID, uuid);
  }
90
91
92
93

  return gridID;
}

94
static int defineZaxis (void)
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{
  int zaxisID = CDI_UNDEFID;
  double vct[3] = { 3.0, 3.3, 3.6 };

  zaxisID = zaxisCreate(ZAXIS_PRESSURE, nlev);
  zaxisDefLevels(zaxisID, levs);
  zaxisDefLevel ( zaxisID, 2, 8507.3 );
  zaxisDefName ( zaxisID, "myName" );
  zaxisDefLongname ( zaxisID, "myLongname" );
  zaxisDefUnits ( zaxisID, "myUnits" );
  zaxisDefPrec ( zaxisID, DOUBLE_PRECISION );
  zaxisDefLtype ( zaxisID, 1 );
  zaxisDefVct ( zaxisID, 3, vct );
  zaxisDefLbounds ( zaxisID, &levs[0] );
  zaxisDefUbounds ( zaxisID, &levs[0] );
  zaxisDefWeights ( zaxisID, &levs[0] );
111
112
  {
    unsigned char uuid[CDI_UUID_SIZE];
113
    cdiCreateUUID(uuid);
114
115
    zaxisDefUUID(zaxisID, uuid);
  }
116
117
118
119

  return zaxisID;
}

120
static int defineTaxis (void)
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{
  int taxisID = CDI_UNDEFID;

  taxisID = taxisCreate(TAXIS_ABSOLUTE);

  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;
}

140
static void defineStream ( int streamID, int vlistID )
141
142
143
144
145
146
147
{
  streamDefByteorder ( streamID, 1 );
  streamDefCompType  ( streamID, 2 );
  streamDefCompLevel ( streamID, 3 );
  streamDefVlist(streamID, vlistID);
}

148
149
150
151
struct idPair {
  int id1, id2;
};

152
static struct idPair defineVlist ( int gridID, int zaxisID, int taxisID )
153
154
155
156
157
158
159
160
161
162
163
{
  int vlistID = CDI_UNDEFID;
  int zaxisID2 = zaxisCreate(ZAXIS_SURFACE, 1);
  int varID1, varID2;

  vlistID = vlistCreate();
  varID1 = vlistDefVar(vlistID, gridID, zaxisID, TIME_VARIABLE);
  varID2 = vlistDefVar(vlistID, gridID, zaxisID2, TIME_VARIABLE);
  vlistDefVarName(vlistID, varID1, "varname1");
  {
    int globfac[] = { 23, 42 };
164
    cdiDefAttInt(vlistID, varID1, "seer's globule factors", DATATYPE_INT16, 2, globfac);
165
166
  }
  vlistDefVarName(vlistID, varID2, "varname2");
167
  cdiDefAttTxt(vlistID, varID2, "txt demo", 6, "banana");
168
  vlistDefTaxis(vlistID, taxisID);
Thomas Jahns's avatar
Thomas Jahns committed
169
170
171
  int vlistID2 = vlistCreate();
  vlistDefVar(vlistID2, gridID, zaxisID, TIME_VARIABLE);
  vlistCopy(vlistID2, vlistID);
172
  return (struct idPair){ vlistID, vlistID2 };
173
174
}

175
static int defineInstitute ()
176
177
178
179
180
181
182
183
{
  int instID = CDI_UNDEFID;

  instID = institutDef( 0, 0,"MYINSTITUTE", "myInstitute");

  return instID;
}

184
static int defineModel ( int instID )
185
186
187
{
  int modelID = CDI_UNDEFID;

Thomas Jahns's avatar
Thomas Jahns committed
188
  modelID = modelDef(instID, 0, "resource_copy");
189
190
191
192

  return modelID;
}

193
194
static int destNamespace;

195
static int modelRun(MPI_Comm comm)
196
{
Thomas Jahns's avatar
Thomas Jahns committed
197
  int gridID, zaxisID, taxisID, instID, vlistID, streamID;
198
199
200
201

  char * recvBuffer, * sendBuffer;
  int bufferSize, differ;

202
#ifdef MPI_MARSHALLING
203
  cdiPioSerializeSetMPI();
204
#endif
205
206
207
208
209

  gridID  = defineGrid      ();
  zaxisID = defineZaxis     ();
  taxisID = defineTaxis     ();
  instID  = defineInstitute ();
Thomas Jahns's avatar
Thomas Jahns committed
210
  defineModel(instID);
211
212
213
214
215
216
  {
    struct idPair temp = defineVlist(gridID, zaxisID, taxisID);
    vlistID = temp.id1;
    streamID = streamOpenWrite("example.grb", FILETYPE_GRB);
    if ( streamID < 0 ) xabort ( "Could not open file" );
    defineStream ( streamID, vlistID );
217
    vlistDestroy(temp.id1);
218
219
    vlistDestroy(temp.id2);
  }
220

221
  reshPackBufferCreate ( &sendBuffer, &bufferSize, &comm );
222
  recvBuffer = (char *)malloc((size_t)bufferSize);
223
#ifdef MPI_MARSHALLING
224
225
226
  xmpi(MPI_Sendrecv(sendBuffer, bufferSize, MPI_PACKED, 0, 0,
                    recvBuffer, bufferSize, MPI_PACKED, 0, 0,
                    MPI_COMM_SELF, MPI_STATUS_IGNORE));
227
#else
228
  memcpy(recvBuffer, sendBuffer, (size_t)bufferSize);
229
230
#endif
  namespaceSetActive(destNamespace);
231
  reshUnpackResources(recvBuffer, bufferSize, &comm);
232
233
  free(recvBuffer);
  reshPackBufferDestroy(&sendBuffer);
234
235
236

  differ = reshListCompare ( 0, 1 );

237
  namespaceSetActive(0);
238
  streamClose(streamID);
239
  return differ;
240
241
242
243
}

int main (int argc, char *argv[])
{
244
  int exitCode = 77;
245
246
  MPI_Comm commModel;
#ifdef MPI_MARSHALLING
247
  MPI_Init(&argc, &argv);
248
249
  commModel = MPI_COMM_WORLD;
#else
250
  (void)argc; (void)argv;
251
252
253
  commModel = 0;
#endif
  destNamespace = namespaceNew();
254

255
  exitCode = modelRun(commModel);
256

257
#ifdef MPI_MARSHALLING
258
  xmpi(MPI_Finalize());
259
260
#endif

261
  return exitCode;
262
263
264
265
266
267
268
269
270
271
272
}

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