stream_record.c 14.2 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
#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"
12
#include "cdi_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
  (*record).ilevel   = CDI_UNDEFID;
  (*record).used     = FALSE;
  (*record).varID    = CDI_UNDEFID;
  (*record).levelID  = CDI_UNDEFID;
29
  memset((*record).varname, 0, sizeof((*record).varname));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
31
32
}


33
int recordNewEntry(stream_t *streamptr, int tsID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
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
39
40

  recordSize = streamptr->tsteps[tsID].recordSize;
  records    = streamptr->tsteps[tsID].records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41
42
43
44
45
46
47
48
  /*
    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
49
      records = (record_t *) malloc(recordSize*sizeof(record_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50
51
      if ( records == NULL )
	{
52
53
          Message("recordSize = %d", recordSize);
	  SysError("Allocation of record_tTABLE failed");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	}

      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
75
      records    = (record_t *) realloc(records, recordSize*sizeof(record_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76
77
      if ( records == NULL )
	{
78
79
          Message("recordSize = %d", recordSize);
	  SysError("Reallocation of record_tTABLE failed");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
81
82
83
84
85
86
87
88
89
90
91
	}
      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
92
93
  streamptr->tsteps[tsID].recordSize = recordSize;
  streamptr->tsteps[tsID].records    = records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
95
96
97

  return (recordID);
}

98
99
static
void cdiInitRecord(stream_t *streamptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101
102
103
104
105
  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
106
  streamptr->record->param      = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
108
109
110
111
112
113
114
115
116
117
  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
118
119
120
121
122
}


void streamInqRecord(int streamID, int *varID, int *levelID)
{
Thomas Jahns's avatar
Thomas Jahns committed
123
  /* int rec = 0; */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
125
  int recID, tsID, rindex;
  int lindex;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127

Deike Kleberg's avatar
Deike Kleberg committed
128
129
130
  check_parg(varID);
  check_parg(levelID);

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

133
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
136

  cdiDefAccesstype(streamID, TYPE_REC);

137
  if ( ! streamptr->record ) cdiInitRecord(streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
138

Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
140
  tsID   = streamptr->curTsID;
  rindex = streamptr->tsteps[tsID].curRecID + 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141

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

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

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
151
  *varID   = streamptr->tsteps[tsID].records[recID].varID;
  lindex   = streamptr->tsteps[tsID].records[recID].levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152

Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
  *levelID = streamptr->vars[*varID].lindex[lindex];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
155

  if ( CDI_Debug )
156
    Message("tsID = %d, recID = %d, varID = %d, levelID = %d\n",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
	    tsID, recID, *varID, *levelID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
160
  streamptr->curTsID = tsID;
  streamptr->tsteps[tsID].curRecID = rindex;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161
162

  /*
Thomas Jahns's avatar
Thomas Jahns committed
163
  rec = recID + 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
164
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
167
168
169
170
171
172
173
174

  switch ( filetype )
    {
    case FILETYPE_GRB:
      {
        rec = grbInqRecord(streamID, varID, levelID);
	break;
      }
    case FILETYPE_SRV:
      {
175
        rec = srvInqRecord(streamptr, varID, levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
178
179
180
	break;
      }
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
    case FILETYPE_NC2:
181
    case FILETYPE_NC4:
Deike Kleberg's avatar
Deike Kleberg committed
182
    case FILETYPE_NC4C:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
183
184
185
186
187
188
189
      {
	rec = cdfInqRecord(streamID, varID, levelID);
	break;
      }
#endif
    default:
      {
190
	Error("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
192
193
194
195
196
197
198
199
200
201
	break;
      }
    }
  */
}


void streamDefRecord(int streamID, int varID, int levelID)
{
  int status = 0;
  int filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
202
  int param, gridID, zaxisID, level;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
204
  int tsID;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
206
207

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

209
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210

Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
  tsID = streamptr->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
213
214
215
216
217
218

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

219
  if ( ! streamptr->record ) cdiInitRecord(streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
220

221
  vlistID = streamptr->vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
223
  gridID  = vlistInqVarGrid(vlistID, varID);
  zaxisID = vlistInqVarZaxis(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
224
  param   = vlistInqVarParam(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
  level   = (int) zaxisInqLevel(zaxisID, levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
226

Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
228
  streamptr->record->varID    = varID;
  streamptr->record->levelID  = levelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
  streamptr->record->param    = param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
230
231
232
233
234
235
  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
236

Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
238
239
240
241
242

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


void streamReadRecord(int streamID, double *data, int *nmiss)
{
  int status = 0;
  int filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
299

Deike Kleberg's avatar
Deike Kleberg committed
300
301
302
  check_parg(data);
  check_parg(nmiss);

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

305
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
307

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

  *nmiss = 0;

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


366
void stream_write_record(int streamID, int memtype, const void *data, int nmiss)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
368
369
{
  int status = 0;
  int filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371

Deike Kleberg's avatar
Deike Kleberg committed
372
373
  check_parg(data);

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

376
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
377

Uwe Schulzweida's avatar
Uwe Schulzweida committed
378
  filetype = streamptr->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
380
381
382
383

  switch (filetype)
    {
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
384
    case FILETYPE_GRB2:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
385
      {
386
        status = grb_write_record(streamptr, memtype, data, nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387
388
389
390
391
392
	break;
      }
#endif
#if  defined  (HAVE_LIBSERVICE)
    case FILETYPE_SRV:
      {
393
        if ( memtype == MEMTYPE_FLOAT ) Error("srvWriteRecord not implemented for memtype float!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
394
        status = srvWriteRecord(streamptr, (const double*) data);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
397
398
399
400
	break;
      }
#endif
#if  defined  (HAVE_LIBEXTRA)
    case FILETYPE_EXT:
      {
401
        if ( memtype == MEMTYPE_FLOAT ) Error("extWriteRecord not implemented for memtype float!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
        status = extWriteRecord(streamptr, (const double*) data);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
404
405
406
407
408
	break;
      }
#endif
#if  defined  (HAVE_LIBIEG)
    case FILETYPE_IEG:
      {
409
        if ( memtype == MEMTYPE_FLOAT ) Error("iegWriteRecord not implemented for memtype float!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
410
        status = iegWriteRecord(streamptr, (const double*) data);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
411
412
413
414
415
416
	break;
      }
#endif
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
    case FILETYPE_NC2:
417
    case FILETYPE_NC4:
Deike Kleberg's avatar
Deike Kleberg committed
418
    case FILETYPE_NC4C:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
      {
420
	cdf_write_record(streamptr, memtype, data, nmiss);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
421
422
423
424
425
	break;
      }
#endif
    default:
      {
426
	Error("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427
428
429
430
431
432
	break;
      }
    }
}


433
434
435
436
437
438
439
440
441
442
443
void streamWriteRecord(int streamID, const double *data, int nmiss)
{
  stream_write_record(streamID, MEMTYPE_DOUBLE, (const void *) data, nmiss);
}

void streamWriteRecordF(int streamID, const float *data, int nmiss)
{
  stream_write_record(streamID, MEMTYPE_FLOAT, (const void *) data, nmiss);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
444
void streamCopyRecord(int streamID2, int streamID1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
445
446
{
  int status = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
447
  int filetype = CDI_UNDEFID, filetype1, filetype2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
448
449
  stream_t *streamptr1;
  stream_t *streamptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
450

Uwe Schulzweida's avatar
Uwe Schulzweida committed
451
452
  streamptr1 = stream_to_pointer(streamID1);
  streamptr2 = stream_to_pointer(streamID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
453

454
455
  stream_check_ptr(__func__, streamptr1);
  stream_check_ptr(__func__, streamptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
456

Uwe Schulzweida's avatar
Uwe Schulzweida committed
457
458
  filetype1 = streamptr1->filetype;
  filetype2 = streamptr2->filetype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
459

460
461
  if ( filetype1 == filetype2 ) filetype = filetype2;
  else
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
    {
      switch (filetype1)
        {
        case FILETYPE_NC:
        case FILETYPE_NC2:
        case FILETYPE_NC4:
        case FILETYPE_NC4C:
          switch (filetype2)
            {
            case FILETYPE_NC:
            case FILETYPE_NC2:
            case FILETYPE_NC4:
            case FILETYPE_NC4C:
              Warning("Streams have different file types (%s -> %s)!", strfiletype(filetype1), strfiletype(filetype2));
              filetype = filetype2;
              break;
            }
          break;
        }
    }

  if ( filetype == CDI_UNDEFID )
484
    Error("Streams have different file types (%s -> %s)!", strfiletype(filetype1), strfiletype(filetype2));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485

Uwe Schulzweida's avatar
Uwe Schulzweida committed
486
  switch (filetype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
488
489
490
491
#if  defined  (HAVE_LIBGRIB)
    case FILETYPE_GRB:
    case FILETYPE_GRB2:
      {
Thomas Jahns's avatar
Thomas Jahns committed
492
        /* FIXME: return value not inspected */
493
	status = grbCopyRecord(streamptr2, streamptr1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
494
495
496
	break;
      }
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
497
#if  defined  (HAVE_LIBSERVICE)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
498
499
    case FILETYPE_SRV:
      {
Thomas Jahns's avatar
Thomas Jahns committed
500
        /* FIXME: return value not inspected */
501
	status = srvCopyRecord(streamptr2, streamptr1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
503
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
504
505
#endif
#if  defined  (HAVE_LIBEXTRA)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507
    case FILETYPE_EXT:
      {
Thomas Jahns's avatar
Thomas Jahns committed
508
        /* FIXME: return value not inspected */
509
	status = extCopyRecord(streamptr2, streamptr1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
510
511
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512
513
#endif
#if  defined  (HAVE_LIBIEG)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
514
515
    case FILETYPE_IEG:
      {
Thomas Jahns's avatar
Thomas Jahns committed
516
        /* FIXME: return value not inspected */
517
	status = iegCopyRecord(streamptr2, streamptr1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
518
519
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
#endif
521
522
523
524
525
#if  defined  (HAVE_LIBNETCDF)
    case FILETYPE_NC:
    case FILETYPE_NC2:
    case FILETYPE_NC4:
    case FILETYPE_NC4C:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
      {
Thomas Jahns's avatar
Thomas Jahns committed
527
        /* FIXME: return value not inspected */
528
529
530
531
532
533
534
	status = cdfCopyRecord(streamptr2, streamptr1);
	break;
      }
#endif
    default:
      {
	Error("%s support not compiled in!", strfiletype(filetype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
536
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
538
539
540
    }
}


541
void cdi_create_records(stream_t *streamptr, int tsID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
543
544
{
  int nrecords, maxrecords;
  int nvars, varID, recID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
545
  record_t *records;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
546
547
  int vlistID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
548
  if ( streamptr->tsteps[tsID].records ) return;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
549

550
  vlistID  = streamptr->vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
552
553
554

  if ( tsID == 0 )
    {
      maxrecords = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
      nvars = streamptr->nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
556
      for ( varID = 0; varID < nvars; varID++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
557
	maxrecords += streamptr->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
558
559
    }
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
560
    maxrecords = streamptr->tsteps[0].recordSize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
561
562
563
564
565
566
567
568

  if ( tsID == 0 )
    {
      nrecords = maxrecords;
    }
  else if ( tsID == 1 )
    {
      nrecords = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
569
      maxrecords = streamptr->tsteps[0].recordSize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
570
571
      for ( recID = 0; recID < maxrecords; recID++ )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
572
	  varID = streamptr->tsteps[0].records[recID].varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
573
	  if ( varID != -1 ) /* varID = -1 for write mode !!! */
574
	    if ( vlistInqVarTsteptype(vlistID, varID) == TSTEP_CONSTANT )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
575
576
577
578
579
	      continue;
	  nrecords++;
	}
    }
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
580
    nrecords = streamptr->tsteps[1].nallrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
581

Uwe Schulzweida's avatar
Uwe Schulzweida committed
582
  if ( maxrecords > 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
583
    records = (record_t *) malloc(maxrecords*sizeof(record_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
584
585
586
  else
    records = NULL;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
587
588
589
  streamptr->tsteps[tsID].records    = records;
  streamptr->tsteps[tsID].recordSize = maxrecords;
  streamptr->tsteps[tsID].nallrecs   = nrecords;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
590
591
592
593

  if ( tsID == 0 )
    {
      for ( recID = 0; recID < maxrecords; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
	recordInitEntry(&streamptr->tsteps[tsID].records[recID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
595
596
597
    }
  else
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
598
599
      memcpy(streamptr->tsteps[tsID].records,
	     streamptr->tsteps[0].records,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600
	     maxrecords*sizeof(record_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
601
602
603

      for ( recID = 0; recID < maxrecords; recID++ )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
604
	  varID = streamptr->tsteps[0].records[recID].varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
605
	  if ( varID != -1 ) /* varID = -1 for write mode !!! */
606
	    if ( vlistInqVarTsteptype(vlistID, varID) != TSTEP_CONSTANT )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
	      {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
608
609
610
		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
611
612
613
614
	      }
	}
    }
}
615
616
617
618
619
620
621
622
623
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */