stream_record.c 12.2 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <stdio.h>
#include <string.h>

#include "dmemory.h"

#include "cdi.h"
#include "cdf_int.h"
#include "stream_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
13
#include "stream_grb.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14
15
16
17
18
19
#include "stream_cdf.h"
#include "stream_srv.h"
#include "stream_ext.h"
#include "stream_ieg.h"


Uwe Schulzweida's avatar
Uwe Schulzweida committed
20
void recordInitEntry(record_t *record)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22
23
{
  (*record).position = CDI_UNDEFID;
  (*record).size     = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
24
  (*record).param    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
27
28
29
30
31
32
33
  (*record).ilevel   = CDI_UNDEFID;
  (*record).used     = FALSE;
  (*record).varID    = CDI_UNDEFID;
  (*record).levelID  = CDI_UNDEFID;
}


int recordNewEntry(int streamID, int tsID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
  static const char *func = "recordNewEntry";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36
  int recordID = 0;
  int recordSize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
  record_t *records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
38
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
39

Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
41
42
43
  streamptr = stream_to_pointer(streamID);

  recordSize = streamptr->tsteps[tsID].recordSize;
  records    = streamptr->tsteps[tsID].records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
45
46
47
48
49
50
51
  /*
    Look for a free slot in record.
    (Create the table the first time through).
  */
  if ( ! recordSize )
    {
      int i;
      recordSize = 1;   /*  <<<<----  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
      records = (record_t *) malloc(recordSize*sizeof(record_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
54
55
      if ( records == NULL )
	{
          Message(func, "recordSize = %d", recordSize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
	  SysError(func, "Allocation of record_tTABLE failed");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
	}

      for ( i = 0; i < recordSize; i++ )
	records[i].used = CDI_UNDEFID;
    }
  else
    {
      while ( recordID < recordSize )
	{
	  if ( records[recordID].used == CDI_UNDEFID ) break;
	  recordID++;
	}
    }
  /*
    If the table overflows, double its size.
  */
  if ( recordID == recordSize )
    {
      int i;

      recordSize = 2*recordSize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
      records    = (record_t *) realloc(records, recordSize*sizeof(record_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80
81
      if ( records == NULL )
	{
          Message(func, "recordSize = %d", recordSize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
82
	  SysError(func, "Reallocation of record_tTABLE failed");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
83
84
85
86
87
88
89
90
91
92
93
94
	}
      recordID = recordSize/2;

      for ( i = recordID; i < recordSize; i++ )
	records[i].used = CDI_UNDEFID;
    }


  recordInitEntry(&records[recordID]);

  records[recordID].used = 1;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
96
  streamptr->tsteps[tsID].recordSize = recordSize;
  streamptr->tsteps[tsID].records    = records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
98
99
100
101
102
103

  return (recordID);
}


void cdiInitRecord(int streamID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
106
107
108
109
110
111
112

  streamptr = stream_to_pointer(streamID);

  streamptr->record = (Record *) malloc(sizeof(Record));

  streamptr->record->used       = 0;
  streamptr->record->nrec       = 0;
  streamptr->record->dataread   = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
  streamptr->record->param      = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
116
117
118
119
120
121
122
123
124
  streamptr->record->level      = 0;
  streamptr->record->date       = 0;
  streamptr->record->time       = 0;
  streamptr->record->gridID     = 0;
  streamptr->record->zaxisID    = 0;
  streamptr->record->buffer     = NULL;
  streamptr->record->buffersize = 0;
  streamptr->record->position   = 0;
  streamptr->record->varID      = 0;
  streamptr->record->levelID    = CDI_UNDEFID;
  streamptr->record->recid      = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
126
127
128
129
}


void streamInqRecord(int streamID, int *varID, int *levelID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
130
  static const char *func = "streamInqRecord";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
  int rec = 0;
  int recID, tsID, rindex;
  int lindex;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135

Uwe Schulzweida's avatar
Uwe Schulzweida committed
136
137
138
  streamptr = stream_to_pointer(streamID);

  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
140
141

  cdiDefAccesstype(streamID, TYPE_REC);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
  if ( ! streamptr->record ) cdiInitRecord(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143

Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
145
  tsID   = streamptr->curTsID;
  rindex = streamptr->tsteps[tsID].curRecID + 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146

Uwe Schulzweida's avatar
Uwe Schulzweida committed
147
  if ( rindex >= streamptr->tsteps[tsID].nrecs )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149
    Error(func, "record %d not available at timestep %d", rindex+1, tsID+1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
  recID  = streamptr->tsteps[tsID].recIDs[rindex];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151

Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
  if ( recID == -1 || recID >= streamptr->tsteps[tsID].nallrecs )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154
    Error(func, "Internal problem! tsID = %d recID = %d", tsID, recID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
156
  *varID   = streamptr->tsteps[tsID].records[recID].varID;
  lindex   = streamptr->tsteps[tsID].records[recID].levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157

Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
  *levelID = streamptr->vars[*varID].lindex[lindex];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
160
161
162
163

  if ( CDI_Debug )
    Message(func, "tsID = %d, recID = %d, varID = %d, levelID = %d\n",
	    tsID, recID, *varID, *levelID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
164
165
  streamptr->curTsID = tsID;
  streamptr->tsteps[tsID].curRecID = rindex;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
166
167
168

  rec = recID + 1;
  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

  switch ( filetype )
    {
    case FILETYPE_GRB:
      {
        rec = grbInqRecord(streamID, varID, levelID);
	break;
      }
    case FILETYPE_SRV:
      {
        rec = srvInqRecord(streamID, varID, levelID);
	break;
      }
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
    case FILETYPE_NC2:
186
    case FILETYPE_NC4:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
187
188
189
190
191
192
193
      {
	rec = cdfInqRecord(streamID, varID, levelID);
	break;
      }
#endif
    default:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194
	Error(func, "%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
195
196
197
198
199
200
201
202
203
	break;
      }
    }
  */
}


void streamDefRecord(int streamID, int varID, int levelID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
  static const char *func = "streamDefRecord";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
206
  int status = 0;
  int filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
  int param, gridID, zaxisID, level;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
208
209
  int tsID;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
212

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213

Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215

Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
  tsID = streamptr->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
217
218
219
220
221
222
223

  if ( tsID == CDI_UNDEFID )
    {
      tsID++;
      streamDefTimestep(streamID, tsID);
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
224
  if ( ! streamptr->record ) cdiInitRecord(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
226
227
228

  vlistID = streamInqVlist(streamID);
  gridID  = vlistInqVarGrid(vlistID, varID);
  zaxisID = vlistInqVarZaxis(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
  param   = vlistInqVarParam(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
230
  level   = (int) zaxisInqLevel(zaxisID, levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
231

Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
  streamptr->record->varID    = varID;
  streamptr->record->levelID  = levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234
  streamptr->record->param    = param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
235
236
237
238
239
240
  streamptr->record->level    = level;
  streamptr->record->date     = streamptr->tsteps[tsID].taxis.vdate;
  streamptr->record->time     = streamptr->tsteps[tsID].taxis.vtime;
  streamptr->record->gridID   = gridID;
  streamptr->record->zaxisID  = zaxisID;
  streamptr->record->prec     = vlistInqVarDatatype(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241

Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
245
246
247

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
249
250
251
252
253
    case FILETYPE_GRB2:
      {
        status = grbDefRecord(streamID);
	break;
      }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
        status = srvDefRecord(streamID);
	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
        status = extDefRecord(streamID);
	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
        status = iegDefRecord(streamID);
	break;
      }
#endif
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
    case FILETYPE_NC2:
278
    case FILETYPE_NC4:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
279
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
280
	if ( streamptr->accessmode == 0 ) cdfEndDef(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
281
282
283
284
285
286
	status = cdfDefRecord(streamID);
	break;
      }
#endif
    default:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
287
	Error(func, "%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
288
289
290
291
292
293
294
295
	break;
      }
    }
}


void streamReadRecord(int streamID, double *data, int *nmiss)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
296
  static const char *func = "streamReadRecord";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
297
298
  int status = 0;
  int filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
299
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
300

Uwe Schulzweida's avatar
Uwe Schulzweida committed
301
  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
302

Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
304
305
  stream_check_ptr(func, streamptr);

  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
307
308
309
310
311
312

  *nmiss = 0;

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
      {
        status = grbReadRecord(streamID, data, nmiss);
	break;
      }
#endif
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
        status = srvReadRecord(streamID, data, nmiss);
	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
        status = extReadRecord(streamID, data, nmiss);
	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
        status = iegReadRecord(streamID, data, nmiss);
	break;
      }
#endif
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
    case FILETYPE_NC2:
343
    case FILETYPE_NC4:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344
345
346
347
348
349
350
      {
	status = cdfReadRecord(streamID, data, nmiss);
	break;
      }
#endif
    default:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
351
	Error(func, "%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
352
353
354
355
356
357
	break;
      }
    }
}


358
void streamWriteRecord(int streamID, const double *data, int nmiss)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
359
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360
  static const char *func = "streamWriteRecord";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
361
362
  int status = 0;
  int filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
363
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
365

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366

Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368

Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
371
372
373
374

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
375
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
      {
        status = grbWriteRecord(streamID, data, nmiss);
	break;
      }
#endif
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
        status = srvWriteRecord(streamID, data);
	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
        status = extWriteRecord(streamID, data);
	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
        status = iegWriteRecord(streamID, data);
	break;
      }
#endif
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
    case FILETYPE_NC2:
405
    case FILETYPE_NC4:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
407
408
409
410
411
412
      {
	cdfWriteRecord(streamID, data, nmiss);
	break;
      }
#endif
    default:
      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413
	Error(func, "%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
414
415
416
417
418
419
	break;
      }
    }
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
420
void streamCopyRecord(int streamID2, int streamID1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
421
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
  static const char *func = "streamCopyRecord";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
423
  int status = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
424
  int filetype = CDI_UNDEFID, filetype1, filetype2;
425
  int byteorder1, byteorder2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
426
427
  stream_t *streamptr1;
  stream_t *streamptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
428

Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
430
  streamptr1 = stream_to_pointer(streamID1);
  streamptr2 = stream_to_pointer(streamID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
431

Uwe Schulzweida's avatar
Uwe Schulzweida committed
432
433
  stream_check_ptr(func, streamptr1);
  stream_check_ptr(func, streamptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
434

Uwe Schulzweida's avatar
Uwe Schulzweida committed
435
436
  filetype1 = streamptr1->filetype;
  filetype2 = streamptr2->filetype;
437
438
  byteorder1 = streamptr1->byteorder;
  byteorder2 = streamptr2->byteorder;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
439

Uwe Schulzweida's avatar
Uwe Schulzweida committed
440
  if ( filetype1  == filetype2 ) filetype = filetype2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
441

Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
  switch (filetype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
443
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
444
445
446
447
448
449
450
451
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
    case FILETYPE_GRB2:
      {
	status = grbCopyRecord(streamID2, streamID1);
	break;
      }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
452
#if  defined  (HAVE_LIBSERVICE)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
453
454
455
456
457
    case FILETYPE_SRV:
      {
	status = srvCopyRecord(streamID2, streamID1);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
458
459
#endif
#if  defined  (HAVE_LIBEXTRA)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
460
461
462
463
464
    case FILETYPE_EXT:
      {
	status = extCopyRecord(streamID2, streamID1);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
465
466
#endif
#if  defined  (HAVE_LIBIEG)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
467
468
469
470
471
    case FILETYPE_IEG:
      {
	status = iegCopyRecord(streamID2, streamID1);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
472
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
474
475
476
477
    default:
      {
	status = cdfCopyRecord(streamID2, streamID1);
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
478
479
480
481
482
483
    }
}


void cdiCreateRecords(int streamID, int tsID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
484
  static const char *func = "cdiCreateRecords";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486
  int nrecords, maxrecords;
  int nvars, varID, recID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
  record_t *records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
488
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
489
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
490
491

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
492

Uwe Schulzweida's avatar
Uwe Schulzweida committed
493
  stream_check_ptr(func, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
494

Uwe Schulzweida's avatar
Uwe Schulzweida committed
495
  if ( streamptr->tsteps[tsID].records ) return;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
496
497
498
499
500
501

  vlistID  = streamInqVlist(streamID);

  if ( tsID == 0 )
    {
      maxrecords = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
      nvars = streamptr->nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
503
      for ( varID = 0; varID < nvars; varID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
504
	maxrecords += streamptr->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
505
506
    }
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
507
    maxrecords = streamptr->tsteps[0].recordSize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
508
509
510
511
512
513
514
515

  if ( tsID == 0 )
    {
      nrecords = maxrecords;
    }
  else if ( tsID == 1 )
    {
      nrecords = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
516
      maxrecords = streamptr->tsteps[0].recordSize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
518
      for ( recID = 0; recID < maxrecords; recID++ )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
519
	  varID = streamptr->tsteps[0].records[recID].varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
521
522
523
524
525
526
	  if ( varID != -1 ) /* varID = -1 for write mode !!! */
	    if ( vlistInqVarTime(vlistID, varID) == TIME_CONSTANT )
	      continue;
	  nrecords++;
	}
    }
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
527
    nrecords = streamptr->tsteps[1].nallrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
528

Uwe Schulzweida's avatar
Uwe Schulzweida committed
529
  if ( maxrecords > 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
530
    records = (record_t *) malloc(maxrecords*sizeof(record_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
532
533
  else
    records = NULL;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
534
535
536
  streamptr->tsteps[tsID].records    = records;
  streamptr->tsteps[tsID].recordSize = maxrecords;
  streamptr->tsteps[tsID].nallrecs   = nrecords;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
538
539
540

  if ( tsID == 0 )
    {
      for ( recID = 0; recID < maxrecords; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
	recordInitEntry(&streamptr->tsteps[tsID].records[recID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
543
544
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
545
546
      memcpy(streamptr->tsteps[tsID].records,
	     streamptr->tsteps[0].records,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
547
	     maxrecords*sizeof(record_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
548
549
550

      for ( recID = 0; recID < maxrecords; recID++ )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
	  varID = streamptr->tsteps[0].records[recID].varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
552
553
554
	  if ( varID != -1 ) /* varID = -1 for write mode !!! */
	    if ( vlistInqVarTime(vlistID, varID) == TIME_VARIABLE )
	      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
556
557
		streamptr->tsteps[tsID].records[recID].position = CDI_UNDEFID;
		streamptr->tsteps[tsID].records[recID].size     = 0;
		streamptr->tsteps[tsID].records[recID].used     = FALSE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
558
559
560
561
	      }
	}
    }
}