stream_srv.c 22.4 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

5
#include <limits.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
#include <stdio.h>
7
#include <stdlib.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
8
9
10
11
12
13
14
#include <string.h>

#include "dmemory.h"

#include "error.h"
#include "file.h"
#include "cdi.h"
15
#include "cdi_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
16
17
18
#include "varscan.h"
#include "datetime.h"
#include "service.h"
19
#include "stream_fcommon.h"
20
#include "stream_srv.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22
23
24
25
26
27
28
29
30
31
#include "vlist.h"


#undef  UNDEFID
#define UNDEFID  CDI_UNDEFID

#define SINGLE_PRECISION  4
#define DOUBLE_PRECISION  8

#if defined (HAVE_LIBSERVICE)

32
33

typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
  int param;
35
  int level;
36
} SRVCOMPVAR;
37
38


Thomas Jahns's avatar
Thomas Jahns committed
39
static int srvInqDatatype(int prec)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
41
42
{
  int datatype;

43
44
  if ( prec == DOUBLE_PRECISION ) datatype = DATATYPE_FLT64;
  else                            datatype = DATATYPE_FLT32;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
46
47
48
49

  return (datatype);
}


Thomas Jahns's avatar
Thomas Jahns committed
50
static int srvDefDatatype(int datatype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
52
53
{
  int prec;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
  if ( datatype == DATATYPE_CPX32 || datatype == DATATYPE_CPX64 )
55
    Error("CDI/SERVICE library does not support complex numbers!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56

57
58
  if ( datatype != DATATYPE_FLT32 && datatype != DATATYPE_FLT64 )
    datatype = DATATYPE_FLT32;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
59

60
  if ( datatype == DATATYPE_FLT64 ) prec = DOUBLE_PRECISION;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
62
63
64
65
  else                              prec = SINGLE_PRECISION;

  return (prec);
}

66
/* not used
67
int srvInqRecord(stream_t *streamptr, int *varID, int *levelID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
69
70
{
  int status;
  int fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
  int icode, ilevel;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
73
74
  int zaxisID = -1;
  int header[8];
  int vlistID;
75
  void *srvp = streamptr->record->exsep;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76

77
78
  vlistID = streamptr->vlistID;
  fileID  = streamptr->fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80
81
82
83
84
85
86
87

  *varID   = -1;
  *levelID = -1;

  status = srvRead(fileID, srvp);
  if ( status != 0 ) return (0);

  srvInqHeader(srvp, header);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
88
  icode  = header[0];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
89
90
  ilevel = header[1];

Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
  *varID = vlistInqVarID(vlistID, icode);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92

93
  if ( *varID == UNDEFID ) Error("Code %d undefined", icode);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
96
97

  zaxisID = vlistInqVarZaxis(vlistID, *varID);

  *levelID = zaxisInqLevelID(zaxisID, (double) ilevel);
98

Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
100
  return (1);
}
101
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
102

103
void srvReadRecord(stream_t *streamptr, double *data, int *nmiss)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
106
{
  int status;
  int header[8];
107
  int gridID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
108
109
  int i, size;
  double missval;
110
  void *srvp = streamptr->record->exsep;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111

112
113
114
115
116
117
118
  int vlistID  = streamptr->vlistID;
  int fileID   = streamptr->fileID;
  int tsID     = streamptr->curTsID;
  int vrecID   = streamptr->tsteps[tsID].curRecID;
  int recID    = streamptr->tsteps[tsID].recIDs[vrecID];
  int varID    = streamptr->tsteps[tsID].records[recID].varID;
  off_t recpos = streamptr->tsteps[tsID].records[recID].position;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
119
120
121
122

  fileSetPos(fileID, recpos, SEEK_SET);

  status = srvRead(fileID, srvp);
123
124
  if ( status != 0 )
    Error("Failed to read record from SRV file");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
126
127
128

  srvInqHeader(srvp, header);
  srvInqDataDP(srvp, data);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
  missval = vlistInqVarMissval(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
131
  gridID  = vlistInqVarGrid(vlistID, varID);
  size    = gridInqSize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132

Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
  streamptr->numvals += size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
136
137
138
139
140
141
142
143
144

  *nmiss = 0;
  for ( i = 0; i < size; i++ )
    if ( DBL_IS_EQUAL(data[i], missval) || DBL_IS_EQUAL(data[i], (float)missval) )
      {
	data[i] = missval;
	(*nmiss)++;
      }
}


145
void srvCopyRecord(stream_t *streamptr2, stream_t *streamptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
{
147
  streamFCopyRecord(streamptr2, streamptr1, "SRV");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149
150
}


151
void srvDefRecord(stream_t *streamptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
153
{
  int header[8];
154
155
156
  Record *restrict record = streamptr->record;
  srvrec_t *restrict srvp = (srvrec_t*) record->exsep;
  int gridID = record->gridID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157

158
159
  int pdis, pcat, pnum;
  cdiDecodeParam(record->param, &pnum, &pcat, &pdis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
  header[0] = pnum;
161
162
163
  header[1] = record->level;
  header[2] = record->date;
  header[3] = record->time;
164

165
166
  int xsize = gridInqXsize(gridID),
    ysize = gridInqYsize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
168
169
170
171
  if ( xsize == 0 || ysize == 0 )
    {
      xsize = gridInqSize(gridID);
      ysize = 1;
    }
172
  if ( gridInqType(gridID) == GRID_UNSTRUCTURED ) ysize = 1;
173
  if ( gridInqSize(gridID) != xsize*ysize )
174
    Error("Internal problem with gridsize!");
175

Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
178
179
180
  header[4] = xsize;
  header[5] = ysize;
  header[6] = 0;
  header[7] = 0;

181
  int datatype = record->prec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
183
184
185
186
187
188

  srvp->dprec = srvDefDatatype(datatype);

  srvDefHeader(srvp, header);
}


189
void srvWriteRecord(stream_t *streamptr, const double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
{
191
  int fileID = streamptr->fileID;
192
  void *srvp = streamptr->record->exsep;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
194
195
196
197

  srvDefDataDP(srvp, data);
  srvWrite(fileID, srvp);
}

198
199
static
void srv_add_record(stream_t *streamptr, int param, int level, int xsize, int ysize,
200
                    size_t recsize, off_t position, int prec)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
{
Thomas Jahns's avatar
Thomas Jahns committed
202
203
204
205
  int vlistID = streamptr->vlistID;
  int tsID    = streamptr->curTsID;
  int recID   = recordNewEntry(streamptr, tsID);
  record_t *record = &streamptr->tsteps[tsID].records[recID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
206

Thomas Jahns's avatar
Thomas Jahns committed
207
208
209
210
  record->size     = recsize;
  record->position = position;
  record->param    = param;
  record->ilevel   = level;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211

212
213
  grid_t *grid = (grid_t *)Malloc(sizeof (*grid));
  grid_init(grid);
214
  cdiGridTypeInit(grid, GRID_GENERIC, xsize*ysize);
215
216
217
218
  grid->xsize = xsize;
  grid->ysize = ysize;
  grid->xvals = NULL;
  grid->yvals = NULL;
219
220
221
  struct addIffNewRes gridAdded = cdiVlistAddGridIfNew(vlistID, grid, 0);
  int gridID = gridAdded.Id;
  if (!gridAdded.isNew) Free(grid);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
223
224
225
  /*
  if ( level == 0 ) leveltype = ZAXIS_SURFACE;
  else              leveltype = ZAXIS_GENERIC;
  */
Thomas Jahns's avatar
Thomas Jahns committed
226
  int leveltype = ZAXIS_GENERIC;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227

Thomas Jahns's avatar
Thomas Jahns committed
228
  int datatype = srvInqDatatype(prec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229

Thomas Jahns's avatar
Thomas Jahns committed
230
231
  int levelID = 0;
  int varID;
232
  varAddRecord(recID, param, gridID, leveltype, 0, level, 0, 0, 0,
233
234
	       datatype, &varID, &levelID, TSTEP_INSTANT, 0, 0, -1,
               NULL, NULL, NULL, NULL, NULL, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
235

236
  xassert(varID <= SHRT_MAX && levelID <= SHRT_MAX);
Thomas Jahns's avatar
Thomas Jahns committed
237
238
  record->varID   = (short)varID;
  record->levelID = (short)levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239

Uwe Schulzweida's avatar
Uwe Schulzweida committed
240
241
  streamptr->tsteps[tsID].nallrecs++;
  streamptr->nrecs++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
243

  if ( CDI_Debug )
244
    Message("varID = %d gridID = %d levelID = %d",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
246
247
	    varID, gridID, levelID);
}

248
static
249
250
void srvScanTimestep1(stream_t *streamptr)
{
251
  DateTime datetime0 = { LONG_MIN, LONG_MIN };
Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
  off_t recpos;
253
  taxis_t *taxis;
254
  srvrec_t *srvp = (srvrec_t*) streamptr->record->exsep;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255

Uwe Schulzweida's avatar
Uwe Schulzweida committed
256
  streamptr->curTsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
257

258
259
260
261
262
263
  {
    int tsID  = tstepsNewEntry(streamptr);
    if ( tsID != 0 )
      Error("Internal problem! tstepsNewEntry returns %d", tsID);
    taxis = &streamptr->tsteps[tsID].taxis;
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
265


266
  int fileID = streamptr->fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
267

268
  int nrecs = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
270
  while ( TRUE )
    {
271
      int header[8];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
      recpos = fileGetPos(fileID);
273
      int status = srvRead(fileID, srvp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
274
275
      if ( status != 0 )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
276
	  streamptr->ntsteps = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
277
278
	  break;
	}
279
      size_t recsize = (size_t)(fileGetPos(fileID) - recpos);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
280
281
282

      srvInqHeader(srvp, header);

283
284
285
286
287
288
289
      int prec   = srvp->dprec;
      int rcode  = header[0];
      int rlevel = header[1];
      int vdate  = header[2];
      int vtime  = header[3];
      int rxsize = header[4];
      int rysize = header[5];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290

291
      int param = cdiEncodeParam(rcode, 255, 255);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292

Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
294
295
296
297
298
299
      if ( nrecs == 0 )
	{
	  datetime0.date = vdate;
	  datetime0.time = vtime;
	}
      else
	{
300
	  for ( int recID = 0; recID < nrecs; recID++ )
Thomas Jahns's avatar
Thomas Jahns committed
301
302
            if (    streamptr->tsteps[0].records[recID].param  == param
                 && streamptr->tsteps[0].records[recID].ilevel == rlevel )
303
              goto tstepScanLoopFinished;
304
305
	  DateTime datetime = { .date = vdate, .time = vtime };
	  if ( datetimeCmp(datetime, datetime0) )
306
	    Warning("Inconsistent verification time for code %d level %d", rcode, rlevel);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
307
308
309
310
311
	}

      nrecs++;

      if ( CDI_Debug )
312
	Message("%4d%8d%4d%8d%8d%6d", nrecs, (int)recpos, rcode, rlevel, vdate, vtime);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313

314
      srv_add_record(streamptr, param, rlevel, rxsize, rysize, recsize, recpos, prec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
315
316
    }

317
  tstepScanLoopFinished:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
  streamptr->rtsteps = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
319

320
  cdi_generate_vars(streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
321

322
  int taxisID = taxisCreate(TAXIS_ABSOLUTE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
  taxis->type  = TAXIS_ABSOLUTE;
324
325
  taxis->vdate = (int)datetime0.date;
  taxis->vtime = (int)datetime0.time;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
326

327
  int vlistID = streamptr->vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
328
329
  vlistDefTaxis(vlistID, taxisID);

330
  vlist_check_contents(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
331

332
  int nrecords = streamptr->tsteps[0].nallrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
333
  if ( nrecords < streamptr->tsteps[0].recordSize )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
334
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336
      streamptr->tsteps[0].recordSize = nrecords;
      streamptr->tsteps[0].records =
337
	(record_t *) Realloc(streamptr->tsteps[0].records,
338
                             (size_t)nrecords * sizeof(record_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
339
340
    }

341
  streamptr->tsteps[0].recIDs = (int *) Malloc((size_t)nrecords * sizeof (int));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
342
  streamptr->tsteps[0].nrecs = nrecords;
343
  for ( int recID = 0; recID < nrecords; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
    streamptr->tsteps[0].recIDs[recID] = recID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345

Uwe Schulzweida's avatar
Uwe Schulzweida committed
346
  if ( streamptr->ntsteps == -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
    {
348
      int tsID = tstepsNewEntry(streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
      if ( tsID != streamptr->rtsteps )
350
	Error("Internal error. tsID = %d", tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
351

Uwe Schulzweida's avatar
Uwe Schulzweida committed
352
353
      streamptr->tsteps[tsID-1].next   = TRUE;
      streamptr->tsteps[tsID].position = recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
355
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
356
  if ( streamptr->ntsteps == 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
358
359
    {
      if ( taxis->vdate == 0 && taxis->vtime == 0 )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360
	  streamptr->ntsteps = 0;
361
362
	  for ( int varID = 0; varID < streamptr->nvars; varID++ )
            vlistDefVarTsteptype(vlistID, varID, TSTEP_CONSTANT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
363
364
365
366
	}
    }
}

367
static
368
369
int srvScanTimestep2(stream_t *streamptr)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
371
372
  int header[8];
  int status;
  int fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
374
  int param = 0;
  int rcode = 0, rlevel = 0, vdate = 0, vtime = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
375
376
377
378
379
  int tsID;
  int varID;
  off_t recpos = 0;
  int nrecords, nrecs, recID, rindex;
  int nextstep;
380
  taxis_t *taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
  int vlistID;
382
  SRVCOMPVAR compVar, compVar0;
383
  void *srvp = streamptr->record->exsep;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
384
385

  streamptr->curTsID = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
386

387
388
  vlistID = streamptr->vlistID;
  fileID  = streamptr->fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389

Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
  tsID = streamptr->rtsteps;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
391
  if ( tsID != 1 )
392
    Error("Internal problem! unexpected timestep %d", tsID+1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393

Uwe Schulzweida's avatar
Uwe Schulzweida committed
394
  taxis = &streamptr->tsteps[tsID].taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395

Uwe Schulzweida's avatar
Uwe Schulzweida committed
396
  fileSetPos(fileID, streamptr->tsteps[tsID].position, SEEK_SET);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
397

398
  cdi_create_records(streamptr, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
399

Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
  nrecords = streamptr->tsteps[0].nallrecs;
401
  streamptr->tsteps[1].recIDs = (int *) Malloc((size_t)nrecords * sizeof (int));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
  streamptr->tsteps[1].nrecs = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
  for ( recID = 0; recID < nrecords; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
404
    streamptr->tsteps[1].recIDs[recID] = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
405
406
407

  for ( recID = 0; recID < nrecords; recID++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
408
      varID = streamptr->tsteps[0].records[recID].varID;
409
      streamptr->tsteps[tsID].records[recID].position =
Uwe Schulzweida's avatar
Uwe Schulzweida committed
410
	streamptr->tsteps[0].records[recID].position;
411
      streamptr->tsteps[tsID].records[recID].size     =
Uwe Schulzweida's avatar
Uwe Schulzweida committed
412
	streamptr->tsteps[0].records[recID].size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413
414
415
416
417
418
419
420
    }

  for ( rindex = 0; rindex <= nrecords; rindex++ )
    {
      recpos = fileGetPos(fileID);
      status = srvRead(fileID, srvp);
      if ( status != 0 )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
421
	  streamptr->ntsteps = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
423
	  break;
	}
424
      size_t recsize = (size_t)(fileGetPos(fileID) - recpos);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
425
426
427

      srvInqHeader(srvp, header);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
428
      rcode  = header[0];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
430
431
432
      rlevel = header[1];
      vdate  = header[2];
      vtime  = header[3];

Uwe Schulzweida's avatar
Uwe Schulzweida committed
433
      param = cdiEncodeParam(rcode, 255, 255);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
434

Uwe Schulzweida's avatar
Uwe Schulzweida committed
435
436
437
438
439
440
441
      if ( rindex == 0 )
	{
	  taxis->type  = TAXIS_ABSOLUTE;
	  taxis->vdate = vdate;
	  taxis->vtime = vtime;
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
      compVar.param = param;
443
      compVar.level = rlevel;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
444
445
446
      nextstep = FALSE;
      for ( recID = 0; recID < nrecords; recID++ )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
447
	  compVar0.param  = streamptr->tsteps[tsID].records[recID].param;
448
449
450
	  compVar0.level = streamptr->tsteps[tsID].records[recID].ilevel;

	  if ( memcmp(&compVar0, &compVar, sizeof(SRVCOMPVAR)) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
451
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
452
	      if ( streamptr->tsteps[tsID].records[recID].used )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
453
454
455
456
457
		{
		  nextstep = TRUE;
		}
	      else
		{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
458
459
		  streamptr->tsteps[tsID].records[recID].used = TRUE;
		  streamptr->tsteps[tsID].recIDs[rindex] = recID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
460
461
462
463
464
465
		}
	      break;
	    }
	}
      if ( recID == nrecords )
	{
466
	  Warning("Code %d level %d not found at timestep %d", rcode, rlevel, tsID+1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
467
468
469
470
471
472
	  return (CDI_EUFSTRUCT);
	}

      if ( nextstep ) break;

      if ( CDI_Debug )
473
	Message("%4d%8d%4d%8d%8d%6d", rindex+1, (int)recpos, rcode, rlevel, vdate, vtime);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
474

Uwe Schulzweida's avatar
Uwe Schulzweida committed
475
      streamptr->tsteps[tsID].records[recID].size = recsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
476

Uwe Schulzweida's avatar
Uwe Schulzweida committed
477
      compVar0.param  = streamptr->tsteps[tsID].records[recID].param;
478
      compVar0.level = streamptr->tsteps[tsID].records[recID].ilevel;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
479

480
      if ( memcmp(&compVar0, &compVar, sizeof(SRVCOMPVAR)) != 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
481
	{
482
	  Message("tsID = %d recID = %d param = %3d new %3d  level = %3d new %3d",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
483
		  tsID, recID,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
484
		  streamptr->tsteps[tsID].records[recID].param, param,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
		  streamptr->tsteps[tsID].records[recID].ilevel, rlevel);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
486
487
488
	  return (CDI_EUFSTRUCT);
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
489
      streamptr->tsteps[1].records[recID].position = recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
490
491
492
493
494
    }

  nrecs = 0;
  for ( recID = 0; recID < nrecords; recID++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
495
      if ( ! streamptr->tsteps[tsID].records[recID].used )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
496
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
497
	  varID = streamptr->tsteps[tsID].records[recID].varID;
498
          vlistDefVarTsteptype(vlistID, varID, TSTEP_CONSTANT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
499
500
501
502
503
504
	}
      else
	{
	  nrecs++;
	}
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
505
  streamptr->tsteps[tsID].nrecs = nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506

Uwe Schulzweida's avatar
Uwe Schulzweida committed
507
  streamptr->rtsteps = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
508

Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
  if ( streamptr->ntsteps == -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
510
    {
511
      tsID = tstepsNewEntry(streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512
      if ( tsID != streamptr->rtsteps )
513
	Error("Internal error. tsID = %d", tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
514

Uwe Schulzweida's avatar
Uwe Schulzweida committed
515
516
      streamptr->tsteps[tsID-1].next   = TRUE;
      streamptr->tsteps[tsID].position = recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
518
519
520
521
522
    }

  return (0);
}


523
int srvInqContents(stream_t *streamptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
524
525
526
{
  int fileID;
  int status = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
527

528
  fileID = streamptr->fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
529

Uwe Schulzweida's avatar
Uwe Schulzweida committed
530
  streamptr->curTsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
531

532
533
534
  srvScanTimestep1(streamptr);

  if ( streamptr->ntsteps == -1 ) status = srvScanTimestep2(streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
536
537
538
539
540

  fileSetPos(fileID, 0, SEEK_SET);

  return (status);
}

541
static
542
long srvScanTimestep(stream_t *streamptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
544
545
546
{
  int header[8];
  int status;
  int fileID;
Thomas Jahns's avatar
Thomas Jahns committed
547
  /* int rxsize = 0, rysize = 0; */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
548
549
  int param = 0;
  int rcode = 0, rlevel = 0, vdate = 0, vtime = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
550
551
552
  off_t recpos = 0;
  int recID;
  int rindex, nrecs = 0;
553
  void *srvp = streamptr->record->exsep;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
554
  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
556
  if ( CDI_Debug )
    {
557
      Message("streamID = %d", streamptr->self);
558
559
560
      Message("cts = %d", streamptr->curTsID);
      Message("rts = %d", streamptr->rtsteps);
      Message("nts = %d", streamptr->ntsteps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
561
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
562
  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
563

Uwe Schulzweida's avatar
Uwe Schulzweida committed
564
565
  int tsID  = streamptr->rtsteps;
  taxis_t *taxis = &streamptr->tsteps[tsID].taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
566

Uwe Schulzweida's avatar
Uwe Schulzweida committed
567
  if ( streamptr->tsteps[tsID].recordSize == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
568
    {
569
      cdi_create_records(streamptr, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
570

Uwe Schulzweida's avatar
Uwe Schulzweida committed
571
      nrecs = streamptr->tsteps[1].nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
572

Uwe Schulzweida's avatar
Uwe Schulzweida committed
573
      streamptr->tsteps[tsID].nrecs = nrecs;
574
      streamptr->tsteps[tsID].recIDs = (int *) Malloc((size_t)nrecs * sizeof (int));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
575
      for ( recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
576
	streamptr->tsteps[tsID].recIDs[recID] = streamptr->tsteps[1].recIDs[recID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
577

578
      fileID = streamptr->fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
579

Uwe Schulzweida's avatar
Uwe Schulzweida committed
580
      fileSetPos(fileID, streamptr->tsteps[tsID].position, SEEK_SET);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
581
582
583
584
585
586
587

      for ( rindex = 0; rindex <= nrecs; rindex++ )
	{
	  recpos = fileGetPos(fileID);
	  status = srvRead(fileID, srvp);
	  if ( status != 0 )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
588
	      streamptr->ntsteps = streamptr->rtsteps + 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
589
590
	      break;
	    }
591
	  size_t recsize = (size_t)(fileGetPos(fileID) - recpos);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
592
593
594

	  srvInqHeader(srvp, header);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
595
	  rcode  = header[0];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
596
597
598
	  rlevel = header[1];
	  vdate  = header[2];
	  vtime  = header[3];
599
600
          /* rxsize = header[4]; */
          /* rysize = header[5]; */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
601

Uwe Schulzweida's avatar
Uwe Schulzweida committed
602
	  param = cdiEncodeParam(rcode, 255, 255);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
603

604
605
	  // if ( rindex == nrecs ) break; gcc-4.5 internal compiler error
	  if ( rindex == nrecs ) continue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
606
	  recID = streamptr->tsteps[tsID].recIDs[rindex];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
608
609
610
611
612
613

	  if ( rindex == 0 )
	    {
	      taxis->type  = TAXIS_ABSOLUTE;
	      taxis->vdate = vdate;
	      taxis->vtime = vtime;
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
614

Thomas Jahns's avatar
Thomas Jahns committed
615
616
          if (    param  != streamptr->tsteps[tsID].records[recID].param
               || rlevel != streamptr->tsteps[tsID].records[recID].ilevel )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
617
	    {
618
	      Message("tsID = %d recID = %d param = %3d new %3d  level = %3d new %3d",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
619
		      tsID, recID,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
620
		      streamptr->tsteps[tsID].records[recID].param, param,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
621
		      streamptr->tsteps[tsID].records[recID].ilevel, rlevel);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
622
	      Error("Invalid, unsupported or inconsistent record structure!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
623
624
	    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
625
626
	  streamptr->tsteps[tsID].records[recID].position = recpos;
	  streamptr->tsteps[tsID].records[recID].size = recsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
627
628

	  if ( CDI_Debug )
629
	    Message("%4d%8d%4d%8d%8d%6d", rindex, (int)recpos, rcode, rlevel, vdate, vtime);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
630
631
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
632
      streamptr->rtsteps++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
633

Uwe Schulzweida's avatar
Uwe Schulzweida committed
634
      if ( streamptr->ntsteps != streamptr->rtsteps )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
635
	{
636
	  tsID = tstepsNewEntry(streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
637
	  if ( tsID != streamptr->rtsteps )
638
	    Error("Internal error. tsID = %d", tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
639

Uwe Schulzweida's avatar
Uwe Schulzweida committed
640
641
	  streamptr->tsteps[tsID-1].next   = 1;
	  streamptr->tsteps[tsID].position = recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
642
643
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
644
645
      fileSetPos(fileID, streamptr->tsteps[tsID].position, SEEK_SET);
      streamptr->tsteps[tsID].position = recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
646
647
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
648
  if ( nrecs > 0 && nrecs < streamptr->tsteps[tsID].nrecs )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
649
    {
650
      Warning("Incomplete timestep. Stop scanning at timestep %d.", tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
651
      streamptr->ntsteps = tsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
652
653
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
654
  return (streamptr->ntsteps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
655
656
657
}


658
int srvInqTimestep(stream_t *streamptr, int tsID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
659
{
660
661
  long ntsteps;
  int nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
662

Uwe Schulzweida's avatar
Uwe Schulzweida committed
663
  if ( tsID == 0 && streamptr->rtsteps == 0 )
664
    Error("Call to cdiInqContents missing!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
665
666

  if ( CDI_Debug )
667
    Message("tsID = %d rtsteps = %d", tsID, streamptr->rtsteps);
668

Uwe Schulzweida's avatar
Uwe Schulzweida committed
669
  ntsteps = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
670
  while ( ( tsID + 1 ) > streamptr->rtsteps && ntsteps == UNDEFID )
671
    ntsteps = srvScanTimestep(streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
672

673
  if ( tsID >= streamptr->ntsteps && streamptr->ntsteps != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
674
675
676
677
678
    {
      nrecs = 0;
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
679
680
      streamptr->curTsID = tsID;
      nrecs = streamptr->tsteps[tsID].nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
681
682
683
684
685
686
    }

  return (nrecs);
}


687
void srvReadVarDP(stream_t *streamptr, int varID, double *data, int *nmiss)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
688
689
690
691
692
693
694
695
696
{
  int vlistID, fileID;
  int levID, nlevs, gridID, gridsize;
  off_t recpos, currentfilepos;
  int header[8];
  int tsid;
  int recID;
  int i;
  double missval;
697
  void *srvp = streamptr->record->exsep;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
698

699
700
  vlistID  = streamptr->vlistID;
  fileID   = streamptr->fileID;
701
702
  /* NOTE: tiles are not supported here! */
  nlevs    = streamptr->vars[varID].recordTable[0].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
703
704
705
  missval  = vlistInqVarMissval(vlistID, varID);
  gridID   = vlistInqVarGrid(vlistID, varID);
  gridsize = gridInqSize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
706
  tsid     = streamptr->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
707
708

  if ( CDI_Debug )
709
    Message("nlevs = %d gridID = %d gridsize = %d", nlevs, gridID, gridsize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
710
711
712
713
714

  currentfilepos = fileGetPos(fileID);

  for (levID = 0; levID < nlevs; levID++)
    {
715
716
      /* NOTE: tiles are not supported here! */
      recID = streamptr->vars[varID].recordTable[0].recordID[levID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
717
      recpos = streamptr->tsteps[tsid].records[recID].position;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
718
      fileSetPos(fileID, recpos, SEEK_SET);
Thomas Jahns's avatar
Thomas Jahns committed
719
720
      if (srvRead(fileID, srvp) < 0)
        abort();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
      srvInqHeader(srvp, header);
      srvInqDataDP(srvp, &data[levID*gridsize]);
    }
  fileSetPos(fileID, currentfilepos, SEEK_SET);

  *nmiss = 0;
  for ( i = 0; i < nlevs*gridsize; i++ )
    if ( DBL_IS_EQUAL(data[i], missval) || DBL_IS_EQUAL(data[i], (float)missval) )
      {
	data[i] = missval;
	(*nmiss)++;
      }
}


736
void srvReadVarSliceDP(stream_t *streamptr, int varID, int levID, double *data, int *nmiss)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
737
738
739
740
741
742
743
744
745
{
  int vlistID, fileID;
  int nlevs, gridID, gridsize;
  off_t recpos, currentfilepos;
  int header[8];
  int tsid;
  int recID;
  int i;
  double missval;
746
  void *srvp = streamptr->record->exsep;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
747

748
749
  vlistID  = streamptr->vlistID;
  fileID   = streamptr->fileID;
750
751
  /* NOTE: tiles are not supported here! */
  nlevs    = streamptr->vars[varID].recordTable[0].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
752
753
754
  missval  = vlistInqVarMissval(vlistID, varID);
  gridID   = vlistInqVarGrid(vlistID, varID);
  gridsize = gridInqSize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
755
  tsid     = streamptr->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
757

  if ( CDI_Debug )
758
    Message("nlevs = %d gridID = %d gridsize = %d",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
759
760
761
762
	     nlevs, gridID, gridsize);

  currentfilepos = fileGetPos(fileID);

763
764
  /* NOTE: tiles are not supported here! */
  recID = streamptr->vars[varID].recordTable[0].recordID[levID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
765
  recpos = streamptr->tsteps[tsid].records[recID].position;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
766
  fileSetPos(fileID, recpos, SEEK_SET);
Thomas Jahns's avatar
Thomas Jahns committed
767
768
  if (srvRead(fileID, srvp) < 0)
    abort();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
  srvInqHeader(srvp, header);
  srvInqDataDP(srvp, data);

  fileSetPos(fileID, currentfilepos, SEEK_SET);

  *nmiss = 0;
  for ( i = 0; i < gridsize; i++ )
    if ( DBL_IS_EQUAL(data[i], missval) || DBL_IS_EQUAL(data[i], (float)missval) )
      {
	data[i] = missval;
	(*nmiss)++;
      }
}


784
void srvWriteVarDP(stream_t *streamptr, int varID, const double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
785
786
787
788
789
790
{
  int fileID;
  int levID, nlevs, gridID, gridsize;
  int zaxisID;
  double level;
  int header[8];
791
  int xsize, ysize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
792
793
794
  int datatype;
  int tsID;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
795
  int pdis, pcat, pnum;
796
  srvrec_t *srvp = (srvrec_t*) streamptr->record->exsep;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
797
798

  if ( CDI_Debug )
799
    Message("streamID = %d  varID = %d", streamptr->self, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
800

801
802
  vlistID  = streamptr->vlistID;
  fileID   = streamptr->fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
803
  tsID     = streamptr->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
804
805
806
807
808
809
  gridID   = vlistInqVarGrid(vlistID, varID);
  gridsize = gridInqSize(gridID);
  zaxisID  = vlistInqVarZaxis(vlistID, varID);
  nlevs    = zaxisInqSize(zaxisID);

  if ( CDI_Debug )
810
    Message("nlevs = %d gridID = %d gridsize = %d", nlevs, gridID, gridsize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
811

Uwe Schulzweida's avatar
Uwe Schulzweida committed
812
  cdiDecodeParam(vlistInqVarParam(vlistID, varID), &pnum, &pcat, &pdis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
813
814

  header[0] = pnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
815
816
  header[2] = streamptr->tsteps[tsID].taxis.vdate;
  header[3] = streamptr->tsteps[tsID].taxis.vtime;
817
818
819
820

  xsize = gridInqXsize(gridID);
  ysize = gridInqYsize(gridID);
  if ( xsize == 0 || ysize == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
821
    {
822
823
      xsize = gridInqSize(gridID);
      ysize = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
824
    }
825
  if ( gridInqType(gridID) == GRID_UNSTRUCTURED ) ysize = 1;
826
  if ( gridInqSize(gridID) != xsize*ysize )
827
    Error("Internal problem with gridsize!");
828
829
830

  header[4] = xsize;
  header[5] = ysize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
831
832
833
834
835
836
837
  header[6] = 0;
  header[7] = 0;

  datatype = vlistInqVarDatatype(vlistID, varID);

  srvp->dprec = srvDefDatatype(datatype);

838
  for ( levID = 0; levID < nlevs; levID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
839
840
841
842
843
844
845
846
847
848
849
    {
      level = zaxisInqLevel(zaxisID, levID);

      header[1] = (int) level;
      srvDefHeader(srvp, header);
      srvDefDataDP(srvp, &data[levID*gridsize]);
      srvWrite(fileID, srvp);
    }
}


850
void srvWriteVarSliceDP(stream_t *streamptr, int varID, int levID, const double *data)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
851
852
853
854
855
856
{
  int fileID;
  int gridID;
  int zaxisID;
  double level;
  int header[8];
857
  int xsize, ysize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
858
859
860
  int datatype;
  int tsID;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
861
  int pdis, pcat, pnum;
862
  srvrec_t *srvp = (srvrec_t*) streamptr->record->exsep;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
863

864
865
  vlistID  = streamptr->vlistID;
  fileID   = streamptr->fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
866
  tsID     = streamptr->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
867
868
869
870
871
  gridID   = vlistInqVarGrid(vlistID, varID);
  zaxisID  = vlistInqVarZaxis(vlistID, varID);
  level    = zaxisInqLevel(zaxisID, levID);

  if ( CDI_Debug )
872
    Message("gridID = %d zaxisID = %d", gridID, zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
873

Uwe Schulzweida's avatar
Uwe Schulzweida committed
874
  cdiDecodeParam(vlistInqVarParam(vlistID, varID), &pnum, &pcat, &pdis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
875
876

  header[0] = pnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
877
  header[1] = (int) level;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
878
879
  header[2] = streamptr->tsteps[tsID].taxis.vdate;
  header[3] = streamptr->tsteps[tsID].taxis.vtime;
880
881
882
883

  xsize = gridInqXsize(gridID);
  ysize = gridInqYsize(gridID);
  if ( xsize == 0 || ysize == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
884
    {
885
886
      xsize = gridInqSize(gridID);
      ysize = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
887
    }
888
  if ( gridInqType(gridID) == GRID_UNSTRUCTURED ) ysize = 1;
889
  if ( gridInqSize(gridID) != xsize*ysize )
890
    Error("Internal problem with gridsize!");
891
892
893

  header[4] = xsize;
  header[5] = ysize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
894
895
896
897
898
899
900
901
902
903
904
905
906
  header[6] = 0;
  header[7] = 0;

  datatype = vlistInqVarDatatype(vlistID, varID);

  srvp->dprec = srvDefDatatype(datatype);

  srvDefHeader(srvp, header);
  srvDefDataDP(srvp, data);
  srvWrite(fileID, srvp);
}

#endif /* HAVE_LIBSERVICE */
907
908
909
910
911
912
913
914
915
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */