stream_ieg.c 37.4 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

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

#include "dmemory.h"

#include "error.h"
#include "file.h"
#include "cdi.h"
#include "stream_int.h"
#include "varscan.h"
#include "datetime.h"
#include "ieg.h"
#include "vlist.h"


#undef  UNDEFID
#define UNDEFID  CDI_UNDEFID

#define SINGLE_PRECISION  4
#define DOUBLE_PRECISION  8

#if defined (HAVE_LIBIEG)

30
31

typedef struct {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32
  int param;
33
34
35
36
  int level;
} IEGCOMPVAR; 


Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
40
int iegInqDatatype(int prec)
{
  int datatype;

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

  return (datatype);
}


int iegDefDatatype(int datatype)
{
  int prec;

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

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

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

  return (prec);
}

64
/* not used
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
66
67
68
int iegInqRecord(int streamID, int *varID, int *levelID)
{
  int status;
  int fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
  int icode, ilevel;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
70
71
  int zaxisID = -1;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
  iegrec_t *iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74

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

77
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
79
80

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81
  iegp    = streamptr->record->iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
82
83
84
85
86
87
88

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

  status = iegRead(fileID, iegp);
  if ( status != 0 ) return (0);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
89
  icode  = IEG_P_Parameter(iegp->ipdb);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
91
92
93
94
  if ( IEG_P_LevelType(iegp->ipdb) == IEG_LTYPE_HYBRID_LAYER )
    ilevel = IEG_P_Level1(iegp->ipdb);
  else
    ilevel = IEG_P_Level2(iegp->ipdb);

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

97
  if ( *varID == UNDEFID ) Error("Code %d undefined", icode);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
99
100
101
102
103
104

  zaxisID = vlistInqVarZaxis(vlistID, *varID);

  *levelID = zaxisInqLevelID(zaxisID, (double) ilevel);
  
  return (1);
}
105
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
107
108
109
110
111
112
113
114
115

int iegReadRecord(int streamID, double *data, int *nmiss)
{
  int vlistID, fileID;
  int status;
  int recID, vrecID, tsID;
  off_t recpos;
  int varID, gridID;
  int i, size;
  double missval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
  iegrec_t *iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119

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

121
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
123
124

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
126
127
128
129
130
  tsID    = streamptr->curTsID;
  vrecID  = streamptr->tsteps[tsID].curRecID;
  recID   = streamptr->tsteps[tsID].recIDs[vrecID];
  recpos  = streamptr->tsteps[tsID].records[recID].position;
  varID   = streamptr->tsteps[tsID].records[recID].varID;
  iegp    = streamptr->record->iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
134
135
136
137
138

  fileSetPos(fileID, recpos, SEEK_SET);

  status = iegRead(fileID, iegp);
  if ( status != 0 ) return (0);

  iegInqDataDP(iegp, data);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
  missval = vlistInqVarMissval(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
141
  gridID  = vlistInqVarGrid(vlistID, varID);
  size    = gridInqSize(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142

Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
  streamptr->numvals += size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
145
146
147
148
149
150
151
152
153
154
155

  *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)++;
      }

  return (1);
}

156
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
int iegGetZaxisType(int iegleveltype)
{
  int leveltype = 0;

  switch ( iegleveltype )
    {
    case IEG_LTYPE_SURFACE:
      {
	leveltype = ZAXIS_SURFACE;
	break;
      }
    case IEG_LTYPE_99:
    case IEG_LTYPE_ISOBARIC:
      {
	leveltype = ZAXIS_PRESSURE;
	break;
      }
    case IEG_LTYPE_HEIGHT:
      {
	leveltype = ZAXIS_HEIGHT;
	break;
      }
    case IEG_LTYPE_ALTITUDE:
      {
	leveltype = ZAXIS_ALTITUDE;
	break;
      }
    case IEG_LTYPE_HYBRID:
    case IEG_LTYPE_HYBRID_LAYER:
      {
	leveltype = ZAXIS_HYBRID;
	break;
      }
    case IEG_LTYPE_LANDDEPTH:
    case IEG_LTYPE_LANDDEPTH_LAYER:
      {
	leveltype = ZAXIS_DEPTH_BELOW_LAND;
	break;
      }
    case IEG_LTYPE_SEADEPTH:
      {
	leveltype = ZAXIS_DEPTH_BELOW_SEA;
	break;
      }
    default:
      {
	leveltype = ZAXIS_GENERIC;
	break;
      }
    }

  return (leveltype);
}


212
void iegDefTime(int *pdb, int date, int time, int taxisID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213
{
214
  int year, month, day, hour, minute, second;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
  int timetype = -1;

217
  if ( taxisID != -1 ) timetype = taxisInqType(taxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
219
220

  if ( timetype == TAXIS_ABSOLUTE || timetype == TAXIS_RELATIVE )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
222
      cdiDecodeDate(date, &year, &month, &day);
      cdiDecodeTime(time, &hour, &minute, &second);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

      IEG_P_Year(pdb)     = year;
      IEG_P_Month(pdb)    = month;
      IEG_P_Day(pdb)      = day;
      IEG_P_Hour(pdb)     = hour;
      IEG_P_Minute(pdb)   = minute;

      pdb[15] = 1;
      pdb[16] = 0;
      pdb[17] = 0;
      pdb[18] = 10;
      pdb[36] = 1;
    }

  pdb[5] = 128;
}

240
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
242
243
244
245
246
247
248
void iegDefGrid(int *gdb, int gridID)
{
  int gridtype;

  gridtype = gridInqType(gridID);

  if ( gridtype == GRID_GENERIC )
    {
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
      int xsize, ysize;

      xsize = gridInqXsize(gridID);
      ysize = gridInqYsize(gridID);

      if ( (ysize == 32  || ysize == 48 || ysize == 64 ||
	    ysize == 96  || ysize == 160) && 
	   (xsize == 2*ysize || xsize == 1) )
	{
	  gridtype = GRID_GAUSSIAN;
	  gridChangeType(gridID, gridtype);
	}
      else if ( (xsize == 1 && ysize == 1) || (xsize == 0 && ysize == 0) )
	{
	  gridtype = GRID_LONLAT;
	  gridChangeType(gridID, gridtype);
	}
      else if ( gridInqXvals(gridID, NULL) && gridInqYvals(gridID, NULL) )
	{
	  gridtype = GRID_LONLAT;
	  gridChangeType(gridID, gridtype);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
272
273
274
275
276
    }
  else if ( gridtype == GRID_CURVILINEAR )
    {
      gridtype = GRID_LONLAT;
    }

277
  if ( gridtype == GRID_LONLAT || gridtype == GRID_GAUSSIAN )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
278
279
    {
      int nlon, nlat;
280
281
      double xfirst = 0, xlast = 0, xinc = 0;
      double yfirst = 0, ylast = 0, yinc = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
283
284
285

      nlon = (int) gridInqXsize(gridID);
      nlat = (int) gridInqYsize(gridID);

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
      if ( nlon == 0 )
	{
	  nlon = 1;
	}
      else
	{
	  xfirst = gridInqXval(gridID,      0);
	  xlast  = gridInqXval(gridID, nlon-1);
	  xinc   = gridInqXinc(gridID);
	}

      if ( nlat == 0 )
	{
	  nlat = 1;
	}
      else
	{
	  yfirst = gridInqYval(gridID,      0);
	  ylast  = gridInqYval(gridID, nlat-1);
	  yinc   = gridInqYinc(gridID);
	}

308
309
310
      if ( gridtype == GRID_GAUSSIAN )
	IEG_G_GridType(gdb) = 4;
      else if ( gridtype == GRID_LONLAT && gridIsRotated(gridID) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
312
313
314
315
316
	IEG_G_GridType(gdb) = 10;
      else
	IEG_G_GridType(gdb) = 0;

      IEG_G_NumLon(gdb)   = nlon;
      IEG_G_NumLat(gdb)   = nlat;
317
318
319
320
321
322
      IEG_G_FirstLat(gdb) = NINT(yfirst*1000);
      IEG_G_LastLat(gdb)  = NINT(ylast*1000);
      IEG_G_FirstLon(gdb) = NINT(xfirst*1000);
      IEG_G_LastLon(gdb)  = NINT(xlast*1000);
      IEG_G_LonIncr(gdb)  = NINT(xinc*1000);
      if ( fabs(xinc*1000 - IEG_G_LonIncr(gdb)) > FLT_EPSILON )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
324
	IEG_G_LonIncr(gdb) = 0;

325
326
327
328
      if ( gridtype == GRID_GAUSSIAN )
	IEG_G_LatIncr(gdb) = nlat/2;
      else
	{
329
330
	  IEG_G_LatIncr(gdb) = NINT(yinc*1000);
	  if ( fabs(yinc*1000 - IEG_G_LatIncr(gdb)) > FLT_EPSILON )
331
	    IEG_G_LatIncr(gdb) = 0;
332
333

	  if ( IEG_G_LatIncr(gdb) < 0 ) IEG_G_LatIncr(gdb) = -IEG_G_LatIncr(gdb);
334
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335

336
337
338
339
340
341
342
343
344
345
346
      if ( IEG_G_NumLon(gdb) > 1 && IEG_G_NumLat(gdb) == 1 ) 
	if ( IEG_G_LonIncr(gdb) != 0 && IEG_G_LatIncr(gdb) == 0 ) IEG_G_LatIncr(gdb) = IEG_G_LonIncr(gdb);

      if ( IEG_G_NumLon(gdb) == 1 && IEG_G_NumLat(gdb) > 1 ) 
	if ( IEG_G_LonIncr(gdb) == 0 && IEG_G_LatIncr(gdb) != 0 ) IEG_G_LonIncr(gdb) = IEG_G_LatIncr(gdb);

      if ( IEG_G_LatIncr(gdb) == 0 || IEG_G_LonIncr(gdb) == 0 )
	IEG_G_ResFlag(gdb) = 0;
      else
	IEG_G_ResFlag(gdb) = 128;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
348
349
350
351
352
353
354
355
356
357
358
359
      if ( gridIsRotated(gridID) )
	{
	  IEG_G_LatSP(gdb) = - NINT(gridInqYpole(gridID) * 1000);
	  IEG_G_LonSP(gdb) =   NINT((gridInqXpole(gridID) + 180) * 1000);
	  IEG_G_Size(gdb)  = 42;
	}
      else
	{
	  IEG_G_Size(gdb)  = 32;
	}
    }
  else
    {
360
      Error("Unsupported grid type: %s", gridNamePtr(gridtype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
361
362
363
364
365
    }

  IEG_G_ScanFlag(gdb) = 64;
}

366
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
368
369
370
371
372
373
374
375
376
377
void iegDefLevel(int *pdb, int *gdb, double *vct, int zaxisID, int levelID)
{
  double level;
  int ilevel, leveltype;
  static int warning = 1;
  static int vct_warning = 1;

  leveltype = zaxisInqType(zaxisID);

  if ( leveltype == ZAXIS_GENERIC )
    {
378
      Message("Changed zaxis type from %s to %s",
379
380
	      zaxisNamePtr(leveltype),
	      zaxisNamePtr(ZAXIS_PRESSURE));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
382
      leveltype = ZAXIS_PRESSURE;
      zaxisChangeType(zaxisID, leveltype);
383
      zaxisDefUnits(zaxisID, "Pa");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
    }

  /*  IEG_G_NumVCP(gdb) = 0; */

  switch (leveltype)
    {
    case ZAXIS_SURFACE:
      {
	IEG_P_LevelType(pdb) = IEG_LTYPE_SURFACE;
	IEG_P_Level1(pdb)    = 0;
	IEG_P_Level2(pdb)    = (int) zaxisInqLevel(zaxisID, levelID);
	break;
      }
    case ZAXIS_HYBRID:
      {
	int vctsize;

	if ( zaxisInqLbounds(zaxisID, NULL) && zaxisInqUbounds(zaxisID, NULL) )
	  {
	    IEG_P_LevelType(pdb) = IEG_LTYPE_HYBRID_LAYER;
	    IEG_P_Level1(pdb)    = (int) zaxisInqLbound(zaxisID, levelID);
	    IEG_P_Level2(pdb)    = (int) zaxisInqUbound(zaxisID, levelID);
	  }
	else
	  {
	    IEG_P_LevelType(pdb) = IEG_LTYPE_HYBRID;
	    IEG_P_Level1(pdb)    = 0;
	    IEG_P_Level2(pdb)    = (int) zaxisInqLevel(zaxisID, levelID);
	  }

	vctsize = zaxisInqVctSize(zaxisID);
	if ( vctsize == 0 && warning )
	  {
417
	    Warning("VCT missing. ( code = %d, zaxisID = %d )",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
418
419
420
421
422
423
424
425
		    IEG_P_Parameter(pdb), zaxisID);
	    warning = 0;
	  }
	if ( vctsize > 100 )
	  {
	    /*	    IEG_G_NumVCP(gdb) = 0; */
	    if ( vct_warning )
	      {
426
		Warning("VCT size of %d is too large (maximum is 100). Set to 0!", vctsize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
		vct_warning = 0;
	      }
	  }
	else
	  {
	    IEG_G_Size(gdb) += (vctsize*4);
	    memcpy(vct, zaxisInqVctPtr(zaxisID), vctsize/2*sizeof(double));
	    memcpy(vct+50, zaxisInqVctPtr(zaxisID)+vctsize/2, vctsize/2*sizeof(double));
	  }
	break;
      }
    case ZAXIS_PRESSURE:
      {
	double dum;
	char units[128];

	level = zaxisInqLevel(zaxisID, levelID);
	if ( level < 0 )
445
	  Warning("pressure level of %f Pa is below 0.", level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
446
447

	zaxisInqUnits(zaxisID, units);
448
	if ( memcmp(units, "hPa", 3) == 0 || memcmp(units, "mb",2 ) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
	  level = level*100;

	ilevel = (int) level;
	if ( level < 32768 && (level < 100 || modf(level/100, &dum) > 0) )
	  {
	    IEG_P_LevelType(pdb) = IEG_LTYPE_99;
	    IEG_P_Level1(pdb)    = 0;
	    IEG_P_Level2(pdb)    = ilevel;
	  }
	else
	  {
	    IEG_P_LevelType(pdb) = IEG_LTYPE_ISOBARIC;
	    IEG_P_Level1(pdb)    = 0;
	    IEG_P_Level2(pdb)    = ilevel/100;
	  }
	break;
      }
    case ZAXIS_HEIGHT:
      {
	level = zaxisInqLevel(zaxisID, levelID);

	ilevel = (int) level;
	IEG_P_LevelType(pdb) = IEG_LTYPE_HEIGHT;
	IEG_P_Level1(pdb)    = 0;
	IEG_P_Level2(pdb)    = ilevel;

	break;
      }
    case ZAXIS_ALTITUDE:
      {
	level = zaxisInqLevel(zaxisID, levelID);

	ilevel = (int) level;
	IEG_P_LevelType(pdb) = IEG_LTYPE_ALTITUDE;
	IEG_P_Level1(pdb)    = 0;
	IEG_P_Level2(pdb)    = ilevel;

	break;
      }
    case ZAXIS_DEPTH_BELOW_LAND:
      {
	if ( zaxisInqLbounds(zaxisID, NULL) && zaxisInqUbounds(zaxisID, NULL) )
	  {
	    IEG_P_LevelType(pdb) = IEG_LTYPE_LANDDEPTH_LAYER;
	    IEG_P_Level1(pdb)    = (int) zaxisInqLbound(zaxisID, levelID);
	    IEG_P_Level2(pdb)    = (int) zaxisInqUbound(zaxisID, levelID);
	  }
	else
	  {
	    level = zaxisInqLevel(zaxisID, levelID);

	    ilevel = (int) level;
	    IEG_P_LevelType(pdb) = IEG_LTYPE_LANDDEPTH;
	    IEG_P_Level1(pdb)    = 0;
	    IEG_P_Level2(pdb)    = ilevel;
	  }

	break;
      }
    case ZAXIS_DEPTH_BELOW_SEA:
      {
	level = zaxisInqLevel(zaxisID, levelID);

	ilevel = (int) level;
	IEG_P_LevelType(pdb) = IEG_LTYPE_SEADEPTH;
	IEG_P_Level1(pdb)    = 0;
	IEG_P_Level2(pdb)    = ilevel;

	break;
      }
    case ZAXIS_ISENTROPIC:
      {
	level = zaxisInqLevel(zaxisID, levelID);

	ilevel = (int) level;
	IEG_P_LevelType(pdb) = 113;
	IEG_P_Level1(pdb)    = 0;
	IEG_P_Level2(pdb)    = ilevel;

	break;
      }
    default:
      {
532
	Error("Unsupported zaxis type: %s", zaxisNamePtr(leveltype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
533
534
535
536
537
538
	break;
      }
    }
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
539
int iegCopyRecord(int streamID2, int streamID1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
540
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
  int fileID1, fileID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
543
544
545
546
  int tsID, recID, vrecID;
  long recsize;
  off_t recpos;
  int status = 0;
  char *buffer;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
547
548
  stream_t *streamptr1;
  stream_t *streamptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
549

Uwe Schulzweida's avatar
Uwe Schulzweida committed
550
551
  streamptr1 = stream_to_pointer(streamID1);
  streamptr2 = stream_to_pointer(streamID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
552

553
554
  stream_check_ptr(__func__, streamptr1);
  stream_check_ptr(__func__, streamptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
555

Uwe Schulzweida's avatar
Uwe Schulzweida committed
556
557
  fileID1 = streamInqFileID(streamID1);
  fileID2 = streamInqFileID(streamID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
558

Uwe Schulzweida's avatar
Uwe Schulzweida committed
559
560
561
562
563
564
565
  tsID    = streamptr1->curTsID;
  vrecID  = streamptr1->tsteps[tsID].curRecID;
  recID   = streamptr1->tsteps[tsID].recIDs[vrecID];
  recpos  = streamptr1->tsteps[tsID].records[recID].position;
  recsize = streamptr1->tsteps[tsID].records[recID].size;

  fileSetPos(fileID1, recpos, SEEK_SET);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
566
567
568

  buffer = (char *) malloc(recsize);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
569
  fileRead(fileID1, buffer, recsize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
570

Uwe Schulzweida's avatar
Uwe Schulzweida committed
571
  fileWrite(fileID2, buffer, recsize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586

  free(buffer);

  return (status);
}


int iegDefRecord(int streamID)
{
  int status = 0;
  int fileID, vlistID;
  int gridID;
  int date, time;
  int datatype;
  int i;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
587
  int param, pdis, pcat, pnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
588
589
  int varID, levelID, tsID, zaxisID;
  int byteorder;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
590
  iegrec_t *iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
591
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
592
593

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

595
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
596
597
598

  fileID  = streamInqFileID(streamID);
  vlistID = streamInqVlist(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
599
600
  iegp    = streamptr->record->iegp;
  byteorder = streamptr->byteorder;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
601

Uwe Schulzweida's avatar
Uwe Schulzweida committed
602
603
604
  varID   = streamptr->record->varID;
  levelID = streamptr->record->levelID;
  tsID    = streamptr->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
605
606
607
608

  gridID  = vlistInqVarGrid(vlistID, varID);
  zaxisID = vlistInqVarZaxis(vlistID, varID);
  
609
  iegInitMem(iegp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
610
611
612
613
  for ( i = 0; i < 37; i++ ) iegp->ipdb[i] = -1;

  iegp->byteswap = getByteswap(byteorder);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
614
  param =  vlistInqVarParam(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
615
  cdiDecodeParam(param, &pnum, &pcat, &pdis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
616
617
  IEG_P_Parameter(iegp->ipdb) = pnum;
  if ( pdis == 255 ) IEG_P_CodeTable(iegp->ipdb) = pcat;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
618
619
  date     = streamptr->tsteps[tsID].taxis.vdate;
  time     = streamptr->tsteps[tsID].taxis.vtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
620
621
622
623
624

  iegDefTime(iegp->ipdb, date, time, vlistInqTaxis(vlistID));
  iegDefGrid(iegp->igdb, gridID);
  iegDefLevel(iegp->ipdb, iegp->igdb, iegp->vct, zaxisID, levelID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
625
  datatype = streamptr->record->prec;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
626
627
628
629
630
631
632
633
634
635
636
637
638

  iegp->dprec = iegDefDatatype(datatype);

  return (status);
}


int iegWriteRecord(int streamID, const double *data)
{
  int fileID;
  int status = 0;
  int i, gridsize, gridID;
  double refval;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
639
  iegrec_t *iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
640
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
641

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

644
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
645
646

  fileID = streamInqFileID(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
647
648
  iegp   = streamptr->record->iegp;
  gridID = streamptr->record->gridID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
  
  gridsize = gridInqSize(gridID);

  refval = data[0];
  for ( i = 1; i < gridsize; i++ )
    if ( data[i] < refval ) refval = data[i];

  iegp->refval = refval;

  iegDefDataDP(iegp, data);

  iegWrite(fileID, iegp);

  return (status);
}

665
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
666
void iegAddRecord(int streamID, int param, int *pdb, int *gdb, double *vct,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
667
668
669
670
671
672
673
674
675
676
		  long recsize, off_t position, int prec)
{
  int leveltype;
  int gridID = UNDEFID;
  int levelID = 0;
  int tsID, recID, varID;
  int datatype;
  int level1, level2;
  int gridtype;
  int lbounds = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
677
  record_t *record;
678
  grid_t grid;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
679
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
680
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
681
682

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

  vlistID = streamInqVlist(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
685
  tsID    = streamptr->curTsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
686
  recID   = recordNewEntry(streamID, tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
687
  record  = &streamptr->tsteps[tsID].records[recID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
688
689
690
691
692
693
694
695
696
697

  if ( IEG_P_LevelType(pdb) == IEG_LTYPE_HYBRID_LAYER )
    {
      level1 = IEG_P_Level1(pdb);
      level2 = IEG_P_Level2(pdb);
    }
  else
    {
      level1 = IEG_P_Level2(pdb);
      level2 = 0;
698
      if ( IEG_P_LevelType(pdb) == 100 ) level1 *= 100;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
699
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
700

Uwe Schulzweida's avatar
Uwe Schulzweida committed
701
702
  (*record).size     = recsize;
  (*record).position = position;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
703
  (*record).param    = param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
704
705
706
707
708
709
  (*record).ilevel   = level1;
  (*record).ilevel2  = level2;
  (*record).ltype    = IEG_P_LevelType(pdb);

  if ( IEG_G_GridType(gdb) == 0 || IEG_G_GridType(gdb) == 10 )
    gridtype = GRID_LONLAT;
710
  else if ( IEG_G_GridType(gdb) == 4 )
711
    gridtype = GRID_GAUSSIAN;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
712
713
714
  else
    gridtype = GRID_GENERIC;

715
  memset(&grid, 0, sizeof(grid_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
716
717
718
719
  grid.type  = gridtype; 
  grid.size  = IEG_G_NumLon(gdb)*IEG_G_NumLat(gdb);
  grid.xsize = IEG_G_NumLon(gdb);
  grid.ysize = IEG_G_NumLat(gdb);
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
  grid.xinc  = 0;
  grid.yinc  = 0;
  grid.xdef  = 0;
  /* if ( IEG_G_FirstLon != 0 || IEG_G_LastLon != 0 ) */
  {
    if ( grid.xsize > 1 )
      {
	if ( IEG_G_ResFlag(gdb) && IEG_G_LonIncr(gdb) > 0 )
	  grid.xinc = IEG_G_LonIncr(gdb) * 0.001;
	else
	  grid.xinc = (IEG_G_LastLon(gdb) - IEG_G_FirstLon(gdb)) * 0.001 / (grid.xsize - 1);

	/* correct xinc if necessary */
	if ( IEG_G_FirstLon(gdb) == 0 && IEG_G_LastLon(gdb) > 354000 )
	  {
	    double xinc = 360. / grid.xsize;
	    
	    if ( fabs(grid.xinc-xinc) > 0.0 )
	      {
		grid.xinc = xinc;
740
		if ( CDI_Debug ) Message("set xinc to %g", grid.xinc);
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
	      }
	  }
      }
    grid.xfirst = IEG_G_FirstLon(gdb) * 0.001;
    grid.xlast  = IEG_G_LastLon(gdb)  * 0.001;
    grid.xdef   = 2;	    
  }
  grid.ydef  = 0;
  /* if ( IEG_G_FirstLat != 0 || IEG_G_LastLat != 0 ) */
  {
    if ( grid.ysize > 1 )
      {
	if ( IEG_G_ResFlag(gdb) && IEG_G_LatIncr(gdb) > 0 )
	  grid.yinc = IEG_G_LatIncr(gdb) * 0.001;
	else
	  grid.yinc = (IEG_G_LastLat(gdb) - IEG_G_FirstLat(gdb)) * 0.001 / (grid.ysize - 1);
      }
    grid.yfirst = IEG_G_FirstLat(gdb) * 0.001;
    grid.ylast  = IEG_G_LastLat(gdb)  * 0.001;
    grid.ydef   = 2;	    
  }
  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
763
764
765
766
767
768
769
  grid.xfirst= IEG_G_FirstLon(gdb) * 0.001;
  grid.xlast = IEG_G_LastLon(gdb) * 0.001;
  grid.xinc  = IEG_G_LonIncr(gdb) * 0.001;
  grid.xdef  = 2;	    
  grid.yfirst= IEG_G_FirstLat(gdb) * 0.001;
  grid.ylast = IEG_G_LastLat(gdb) * 0.001;
  grid.yinc  = IEG_G_LatIncr(gdb) * 0.001;
770
771
  grid.ydef  = 2;
  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
  grid.xvals = NULL;
  grid.yvals = NULL;

  grid.isRotated = FALSE;
  if ( IEG_G_GridType(gdb) == 10 )
    {
      grid.isRotated = TRUE;
      grid.ypole     = - IEG_G_LatSP(gdb) * 0.001;
      grid.xpole     =   IEG_G_LonSP(gdb) * 0.001 - 180;
      grid.angle     = 0;
    }

  gridID = varDefGrid(vlistID, grid, 0);

  leveltype = iegGetZaxisType(IEG_P_LevelType(pdb));
  
  if ( leveltype == ZAXIS_HYBRID )
    {
      int i;
      double tmpvct[100];
      int vctsize = IEG_G_NumVCP(gdb);

      for ( i = 0; i < vctsize/2; i++ ) tmpvct[i] = vct[i];
      for ( i = 0; i < vctsize/2; i++ ) tmpvct[i+vctsize/2] = vct[i+50];

      varDefVCT(vctsize, tmpvct);
    }

  if ( IEG_P_LevelType(pdb) == IEG_LTYPE_HYBRID_LAYER ) lbounds = 1;

  datatype = iegInqDatatype(prec);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
804
  varAddRecord(recID, param, gridID, leveltype, lbounds, level1, level2,
805
	       datatype, &varID, &levelID, UNDEFID, 0, 0, NULL, NULL, NULL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
806
807
808
809

  (*record).varID   = varID;
  (*record).levelID = levelID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
810
811
  streamptr->tsteps[tsID].nallrecs++;
  streamptr->nrecs++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
812
813

  if ( CDI_Debug )
814
    Message("varID = %d gridID = %d levelID = %d",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
815
816
817
	    varID, gridID, levelID);
}

Thomas Jahns's avatar
Thomas Jahns committed
818
#if 0
819
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
820
void iegCmpRecord(int streamID, int tsID, int recID, off_t position, int param,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
821
822
823
824
		  int level, int xsize, int ysize)
{
  int varID = 0;
  int levelID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
825
  record_t *record;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
826
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
827

Uwe Schulzweida's avatar
Uwe Schulzweida committed
828
829
830
  streamptr = stream_to_pointer(streamID);

  record  = &streamptr->tsteps[tsID].records[recID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
831

Uwe Schulzweida's avatar
Uwe Schulzweida committed
832
  if ( param != (*record).param || level != (*record).ilevel )
833
    Error("inconsistent timestep");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
834
835
836
837
838
839

  (*record).position = position;
  /*
  varID   = (*record).varID;
  levelID = (*record).levelID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
840
  streamptr->vars[varID].level[levelID] = recID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
841

Uwe Schulzweida's avatar
Uwe Schulzweida committed
842
843
  streamptr->tsteps[tsID].nallrecs++;
  streamptr->nrecs++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
844
845
  */
  if ( CDI_Debug )
846
    Message("varID = %d levelID = %d", varID, levelID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
847
}
Thomas Jahns's avatar
Thomas Jahns committed
848
#endif
849

Uwe Schulzweida's avatar
Uwe Schulzweida committed
850
851
852
853
854
855
856
857
858
859
860
861
862
863
void iegDateTime(int *pdb, int *date, int *time)
{
  int ryear, rmonth, rday, rhour, rminute;

  ryear   = IEG_P_Year(pdb);

  rmonth  = IEG_P_Month(pdb);
  rday    = IEG_P_Day(pdb);

  rhour   = IEG_P_Hour(pdb);
  rminute = IEG_P_Minute(pdb);

  if ( rminute == -1 ) rminute = 0;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
864
865
  *date = cdiEncodeDate(ryear, rmonth, rday);
  *time = cdiEncodeTime(rhour, rminute, 0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
866
867
}

868
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
869
870
871
872
873
void iegScanTimestep1(int streamID)
{  
  int prec = 0;
  int status;
  int fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
874
875
  int tabnum;
  int param = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
876
877
878
879
880
881
882
883
  int rcode = 0, rlevel = 0, vdate = 0, vtime = 0;
  DateTime datetime, datetime0;
  int tsID;
  int varID;
  long recsize;
  off_t recpos;
  int nrecords, nrecs, recID;
  int taxisID = -1;
884
  taxis_t *taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
885
  int vlistID;
886
  IEGCOMPVAR compVar, compVar0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
887
  iegrec_t *iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
888
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
889
890

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

892
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
893

Uwe Schulzweida's avatar
Uwe Schulzweida committed
894
  streamptr->curTsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
895

Uwe Schulzweida's avatar
Uwe Schulzweida committed
896
  iegp  = streamptr->record->iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
897
  tsID  = tstepsNewEntry(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
898
  taxis = &streamptr->tsteps[tsID].taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
899
900

  if ( tsID != 0 )
901
    Error("Internal problem! tstepsNewEntry returns %d", tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
902
903
904
905
906
907
908
909
910
911

  fileID = streamInqFileID(streamID);

  nrecs = 0;
  while ( TRUE )
    {
      recpos = fileGetPos(fileID);
      status = iegRead(fileID, iegp);
      if ( status != 0 )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
912
	  streamptr->ntsteps = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
913
914
915
916
917
918
	  break;
	}
      recsize = fileGetPos(fileID) - recpos;

      prec   = iegp->dprec;
      rcode  = IEG_P_Parameter(iegp->ipdb);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
919
      tabnum = IEG_P_CodeTable(iegp->ipdb);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
920
      param  = cdiEncodeParam(rcode, tabnum, 255);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
921

Uwe Schulzweida's avatar
Uwe Schulzweida committed
922
923
924
925
926
      if ( IEG_P_LevelType(iegp->ipdb) == IEG_LTYPE_HYBRID_LAYER )
	rlevel = IEG_P_Level1(iegp->ipdb);
      else
	rlevel = IEG_P_Level2(iegp->ipdb);

927
928
      if ( IEG_P_LevelType(iegp->ipdb) == 100 ) rlevel *= 100;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
929
930
931
932
933
934
935
936
937
938
939
      iegDateTime(iegp->ipdb, &vdate, &vtime);

      if ( nrecs == 0 )
	{
	  datetime0.date = vdate;
	  datetime0.time = vtime;
	}
      else
	{
	  datetime.date = vdate;
	  datetime.time = vtime;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
940
	  compVar.param = param;
941
          compVar.level = rlevel;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
942
943
	  for ( recID = 0; recID < nrecs; recID++ )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
944
	      compVar0.param = streamptr->tsteps[0].records[recID].param;
945
946
947
	      compVar0.level = streamptr->tsteps[0].records[recID].ilevel;

	      if ( memcmp(&compVar0, &compVar, sizeof(IEGCOMPVAR)) == 0 ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
948
949
950
	    }
	  if ( recID < nrecs ) break;
	  if ( memcmp(&datetime, &datetime0, sizeof(DateTime)) )
951
	    Warning("Inconsistent verification time for param %d level %d", param, rlevel);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
952
953
954
955
956
	}

      nrecs++;

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
959
      iegAddRecord(streamID, param, iegp->ipdb, iegp->igdb, iegp->vct, recsize, recpos, prec);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
960
961
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
962
  streamptr->rtsteps = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
963
964
965

  cdiGenVars(streamID);

966
  taxisID = taxisCreate(TAXIS_ABSOLUTE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
967
968
969
970
971
972
973
974
975
  taxis->type  = TAXIS_ABSOLUTE;
  taxis->vdate = datetime0.date;
  taxis->vtime = datetime0.time;

  vlistID = streamInqVlist(streamID);
  vlistDefTaxis(vlistID, taxisID);

  cdiCheckContents(streamID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
976
977
  nrecords = streamptr->tsteps[0].nallrecs;
  if ( nrecords < streamptr->tsteps[0].recordSize )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
978
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
979
980
      streamptr->tsteps[0].recordSize = nrecords;
      streamptr->tsteps[0].records =
Uwe Schulzweida's avatar
Uwe Schulzweida committed
981
	(record_t *) realloc(streamptr->tsteps[0].records, nrecords*sizeof(record_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
982
983
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
984
985
  streamptr->tsteps[0].recIDs = (int *) malloc(nrecords*sizeof(int));
  streamptr->tsteps[0].nrecs = nrecords;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
986
  for ( recID = 0; recID < nrecords; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
987
    streamptr->tsteps[0].recIDs[recID] = recID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
988

Uwe Schulzweida's avatar
Uwe Schulzweida committed
989
  if ( streamptr->ntsteps == -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
990
991
    {
      tsID = tstepsNewEntry(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
992
      if ( tsID != streamptr->rtsteps )
993
	Error("Internal error. tsID = %d", tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
994

Uwe Schulzweida's avatar
Uwe Schulzweida committed
995
996
      streamptr->tsteps[tsID-1].next   = TRUE;
      streamptr->tsteps[tsID].position = recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
997
998
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
999
  if ( streamptr->ntsteps == 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1000
1001
1002
    {
      if ( taxis->vdate == 0 && taxis->vtime == 0 )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1003
1004
	  streamptr->ntsteps = 0;
	  for ( varID = 0; varID < streamptr->nvars; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1005
	    {
1006
	      vlistDefVarTsteptype(vlistID, varID, TSTEP_CONSTANT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1007
1008
1009
1010
1011
	    }
	}
    }
}

1012
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1013
1014
1015
1016
int iegScanTimestep2(int streamID)
{  
  int status;
  int fileID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1017
1018
  int tabnum;
  int param = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1019
1020
1021
1022
1023
1024
1025
  int rcode = 0, rlevel = 0, vdate = 0, vtime = 0;
  int tsID;
  int varID;
  long recsize;
  off_t recpos = 0;
  int nrecords, nrecs, recID, rindex;
  int nextstep;
1026
  taxis_t *taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1027
  int vlistID;
1028
  IEGCOMPVAR compVar, compVar0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1029
  iegrec_t *iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1030
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1031

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

1034
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1035
1036

  streamptr->curTsID = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1037
1038
1039

  vlistID = streamInqVlist(streamID);
  fileID  = streamInqFileID(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1040
  iegp    = streamptr->record->iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1041

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1042
  tsID = streamptr->rtsteps;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1043
  if ( tsID != 1 )
1044
    Error("Internal problem! unexpeceted timestep %d", tsID+1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1045

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1048
  fileSetPos(fileID, streamptr->tsteps[tsID].position, SEEK_SET);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1049
1050
1051

  cdiCreateRecords(streamID, tsID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1052
1053
1054
  nrecords = streamptr->tsteps[0].nallrecs;
  streamptr->tsteps[1].recIDs = (int *) malloc(nrecords*sizeof(int));
  streamptr->tsteps[1].nrecs = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1055
  for ( recID = 0; recID < nrecords; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1056
    streamptr->tsteps[1].recIDs[recID] = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1057
1058
1059

  for ( recID = 0; recID < nrecords; recID++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1060
1061
1062
1063
1064
      varID = streamptr->tsteps[0].records[recID].varID;
      streamptr->tsteps[tsID].records[recID].position = 
	streamptr->tsteps[0].records[recID].position;
      streamptr->tsteps[tsID].records[recID].size     = 
	streamptr->tsteps[0].records[recID].size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1065
1066
1067
1068
1069
1070
1071
1072
    }

  for ( rindex = 0; rindex <= nrecords; rindex++ )
    {
      recpos = fileGetPos(fileID);
      status = iegRead(fileID, iegp);
      if ( status != 0 )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1073
	  streamptr->ntsteps = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1074
1075
1076
1077
1078
	  break;
	}
      recsize = fileGetPos(fileID) - recpos;

      rcode  = IEG_P_Parameter(iegp->ipdb);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1079
      tabnum = IEG_P_CodeTable(iegp->ipdb);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1080
      param  = cdiEncodeParam(rcode, tabnum, 255);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1081

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1082
1083
1084
1085
1086
      if ( IEG_P_LevelType(iegp->ipdb) == IEG_LTYPE_HYBRID_LAYER )
	rlevel = IEG_P_Level1(iegp->ipdb);
      else
	rlevel = IEG_P_Level2(iegp->ipdb);

1087
1088
      if ( IEG_P_LevelType(iegp->ipdb) == 100 ) rlevel *= 100;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1089
1090
1091
1092
1093
1094
1095
1096
1097
      iegDateTime(iegp->ipdb, &vdate, &vtime);

      if ( rindex == 0 )
	{
	  taxis->type  = TAXIS_ABSOLUTE;
	  taxis->vdate = vdate;
	  taxis->vtime = vtime;
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1098
      compVar.param = param;
1099
      compVar.level = rlevel;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1100
1101
1102
      nextstep = FALSE;
      for ( recID = 0; recID < nrecords; recID++ )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1103
	  compVar0.param = streamptr->tsteps[tsID].records[recID].param;
1104
1105
1106
	  compVar0.level = streamptr->tsteps[tsID].records[recID].ilevel;

	  if ( memcmp(&compVar0, &compVar, sizeof(IEGCOMPVAR)) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1107
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1108
	      if ( streamptr->tsteps[tsID].records[recID].used )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1109
1110
1111
1112
1113
		{
		  nextstep = TRUE;
		}
	      else
		{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1114
1115
		  streamptr->tsteps[tsID].records[recID].used = TRUE;
		  streamptr->tsteps[tsID].recIDs[rindex] = recID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1116
1117
1118
1119
1120
1121
		}
	      break;
	    }
	}
      if ( recID == nrecords )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1122
1123
	  char paramstr[32];
	  cdiParamToString(param, paramstr, sizeof(paramstr));
1124
	  Warning("param %s level %d not defined at timestep 1", paramstr, rlevel);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1125
1126
1127
1128
1129
1130
	  return (CDI_EUFSTRUCT);
	}

      if ( nextstep ) break;

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

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1135
      compVar0.param = streamptr->tsteps[tsID].records[recID].param;
1136
      compVar0.level = streamptr->tsteps[tsID].records[recID].ilevel;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1137

1138
      if ( memcmp(&compVar0, &compVar, sizeof(IEGCOMPVAR)) != 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1139
	{
1140
	  Message("tsID = %d recID = %d param = %3d new %3d  level = %3d new %3d",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1141
		  tsID, recID,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1142
		  streamptr->tsteps[tsID].records[recID].param, param,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1143
		  streamptr->tsteps[tsID].records[recID].ilevel, rlevel);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1144
1145
1146
	  return (CDI_EUFSTRUCT);
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1147
      streamptr->tsteps[1].records[recID].position = recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1148
1149
1150
1151
1152
    }

  nrecs = 0;
  for ( recID = 0; recID < nrecords; recID++ )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1153
      if ( ! streamptr->tsteps[tsID].records[recID].used )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1154
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1155
	  varID = streamptr->tsteps[tsID].records[recID].varID;
1156
          vlistDefVarTsteptype(vlistID, varID, TSTEP_CONSTANT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1157
1158
1159
1160
1161
1162
	}
      else
	{
	  nrecs++;
	}
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1163
  streamptr->tsteps[tsID].nrecs = nrecs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1164

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1165
  streamptr->rtsteps = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1166

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1167
  if ( streamptr->ntsteps == -1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1168
1169
    {
      tsID = tstepsNewEntry(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1170
      if ( tsID != streamptr->rtsteps )
1171
	Error("Internal error. tsID = %d", tsID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1172

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1173
1174
      streamptr->tsteps[tsID-1].next   = TRUE;
      streamptr->tsteps[tsID].position = recpos;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
    }

  return (0);
}


int iegInqContents(int streamID)
{
  int fileID;
  int status = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1185
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1186
1187

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

1189
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1190
1191
1192

  fileID = streamInqFileID(streamID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1193
  streamptr->curTsID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1194
1195
1196

  iegScanTimestep1(streamID);
 
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1197
  if ( streamptr->ntsteps == -1 ) status = iegScanTimestep2(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1198
1199
1200
1201
1202
1203

  fileSetPos(fileID, 0, SEEK_SET);

  return (status);
}

1204
static
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1205
1206
1207
1208
1209
int iegScanTimestep(int streamID)
{
  int status;
  int fileID;
  int tsID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1210
1211
  int tabnum;
  int param = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1212
1213
1214
1215
  int rcode = 0, rlevel = 0, vdate = 0, vtime = 0;
  long recsize = 0;
  off_t recpos = 0;
  int recID;
1216
  taxis_t *taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1217
  int rindex, nrecs = 0;
1218
  IEGCOMPVAR compVar, compVar0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1219
  iegrec_t *iegp;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1220
  stream_t *streamptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1221

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

1224
  stream_check_ptr(__func__, streamptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1225
1226
1227

  if ( CDI_Debug )
    {
1228
1229
1230
1231
      Message("streamID = %d", streamID);
      Message("cts = %d", streamptr->curTsID);
      Message("rts = %d", streamptr->rtsteps);
      Message("nts = %d", streamptr->ntsteps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1232
1233
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1234
  if ( streamptr->rtsteps == 0 )
1235
    Error("Internal problem! Missing contents.");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1236

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1237
1238
1239
  iegp  = streamptr->record->iegp;
  tsID  = streamptr->rtsteps;
  taxis = &streamptr->tsteps[tsID].taxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1240

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1241
  if ( streamptr->tsteps[tsID].recordSize == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1242
1243
1244
    {
      cdiCreateRecords(streamID, tsID);

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1247
1248
      streamptr->tsteps[tsID].nrecs = nrecs;
      streamptr->tsteps[tsID].recIDs = (int *) malloc(nrecs*sizeof(int));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1249
      for ( recID = 0; recID < nrecs; recID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1250
	streamptr->tsteps[tsID].recIDs[recID] = streamptr->tsteps[1].recIDs[recID];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1251
1252
1253

      fileID = streamInqFileID(streamID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1254
      fileSetPos(fileID, streamptr->tsteps[tsID].position, SEEK_SET);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1255
1256
1257
1258
1259
1260
1261

      for ( rindex = 0; rindex <= nrecs; rindex++ )
	{
	  recpos = fileGetPos(fileID);
	  status = iegRead(fileID, iegp);
	  if ( status != 0 )
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1262
	      streamptr->ntsteps = streamptr->rtsteps + 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1263
1264
1265
1266
1267
	      break;
	    }
	  recsize = fileGetPos(fileID) - recpos;

	  rcode  = IEG_P_Parameter(iegp->ipdb);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1268
	  tabnum = IEG_P_CodeTable(iegp->ipdb);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1269
	  param  = cdiEncodeParam(rcode, tabnum, 255);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1270

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1271
1272
1273
1274
1275
	  if ( IEG_P_LevelType(iegp->ipdb) == IEG_LTYPE_HYBRID_LAYER )
	    rlevel = IEG_P_Level1(iegp->ipdb);
	  else
	    rlevel = IEG_P_Level2(iegp->ipdb);

1276
1277
	  if ( IEG_P_LevelType(iegp->ipdb) == 100 ) rlevel *= 100;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1278
1279
	  iegDateTime(iegp->ipdb, &vdate, &vtime);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1280
	  // if ( rindex == nrecs ) break; gcc-4.5 internal compiler error
1281
	  if ( rindex == nrecs ) continue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1282
	  recID = streamptr->tsteps[tsID].recIDs[rindex];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1283
1284
1285
1286
1287
1288
1289
1290

	  if ( rindex == 0 )
	    {
	      taxis->type  = TAXIS_ABSOLUTE;
	      taxis->vdate = vdate;
	      taxis->vtime = vtime;
	    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1291
	  compVar.param = param;
1292
          compVar.level = rlevel;
Uwe Schulzweida's avatar