varscan.c 17.9 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
30
31
32
33
34
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

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

#include "cdi.h"
#include "stream_int.h"
#include "dmemory.h"
#include "varscan.h"
#include "vlist.h"

#undef  UNDEFID
#define UNDEFID -1

static size_t Vctsize = 0;
static double *Vct = NULL;

typedef struct
{
  int      level1;
  int      level2;
  int      recID;
  int      lindex;
}
LEVELTABLE;

typedef struct
{
  int         code;
  int         prec;
  int         average;
  int         gridID;
35
36
  int         zaxistype;
  int         ltype;     /* GRIB level type */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
40
41
42
43
44
45
  int         lbounds;
  int         zaxisID;
  int         nlevels;
  int         levelTableSize;
  LEVELTABLE *levelTable;
  int         instID;
  int         modelID;
  int         tableID;
  int         codetable;
46
  int         szip;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
48
49
}
VARTABLE;

50

Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
52
53
54
55
int vartableInit = 0;
VARTABLE *vartable;
static int varTablesize = 0;
int nvars = 0;

56
57

static void codeInitEntry(int varID, int code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58
59
60
61
{
  vartable[varID].code           = code;
  vartable[varID].prec           = 0;
  vartable[varID].average        = 0;
62
63
64
  vartable[varID].gridID         = UNDEFID;
  vartable[varID].zaxistype      = 0;
  vartable[varID].ltype          = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
66
67
68
69
70
  vartable[varID].levelTable     = NULL;
  vartable[varID].levelTableSize = 0;
  vartable[varID].nlevels        = 0;
  vartable[varID].instID         = UNDEFID;
  vartable[varID].modelID        = UNDEFID;
  vartable[varID].tableID        = UNDEFID;
71
  vartable[varID].szip           = FALSE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
73
}

74

75
static int varGetEntry(int code, int zaxistype, int codetable, int ltype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76
77
78
79
80
81
{
  int varID;

  for ( varID = 0; varID < varTablesize; varID++ )
    {
      if ( vartable[varID].code      == code      &&
82
83
	   vartable[varID].zaxistype == zaxistype &&
	   vartable[varID].ltype     == ltype &&
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84
85
86
87
88
89
90
	   vartable[varID].codetable == codetable )
	return (varID);
    }

  return (UNDEFID);
}

91

Uwe Schulzweida's avatar
Uwe Schulzweida committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
void varFree(void)
{
  static char func[] = "varFree";
  int varID;

  for ( varID = 0; varID < nvars; varID++ )
    {
      if ( vartable[varID].levelTable )
	free(vartable[varID].levelTable);
    }

  if ( vartable )
    free(vartable);

  vartable = NULL;
  varTablesize = 0;
  nvars = 0;

  if ( Vct )
    free(Vct);

  Vct = NULL;
  Vctsize = 0;
}

int levelNewEntry(int varID, int level1, int level2)
{
  static char func[] = "levelNewEntry";
  int levelID = 0;
  int levelTableSize;
  LEVELTABLE *levelTable;

  levelTableSize = vartable[varID].levelTableSize;
  levelTable     = vartable[varID].levelTable;

  /*
    Look for a free slot in levelTable.
    (Create the table the first time through).
  */
  if ( ! levelTableSize )
    {
      int i;

      levelTableSize = 2;
      levelTable = (LEVELTABLE *) malloc(levelTableSize*sizeof(LEVELTABLE));
      if( levelTable == NULL )
	{
          Message(func, "levelTableSize = %d", levelTableSize);
	  SysError(func, "Allocation of LEVELTABLE failed");
	}

      for( i = 0; i < levelTableSize; i++ )
	levelTable[i].recID = UNDEFID;
    }
  else
    {
      while( levelID < levelTableSize )
	{
	  if ( levelTable[levelID].recID == UNDEFID ) break;
	  levelID++;
	}
    }
  /*
    If the table overflows, double its size.
  */
  if( levelID == levelTableSize )
    {
      int i;

      levelTableSize = 2*levelTableSize;
      levelTable = (LEVELTABLE *) realloc(levelTable, levelTableSize*sizeof(LEVELTABLE));
      if( levelTable == NULL )
	{
          Message(func, "levelTableSize = %d", levelTableSize);
	  SysError(func, "Reallocation of LEVELTABLE failed");
	}
      levelID = levelTableSize/2;

      for( i = levelID; i < levelTableSize; i++ )
	levelTable[i].recID = UNDEFID;
    }

  levelTable[levelID].level1 = level1;
  levelTable[levelID].level2 = level2;
  levelTable[levelID].lindex = levelID;

  vartable[varID].nlevels = levelID+1;
  vartable[varID].levelTableSize = levelTableSize;
  vartable[varID].levelTable = levelTable;

  return (levelID);
}

#define  UNDEF_CODE  -4711

int codeNewEntry (int code)
{
  static char func[] = "codeNewEntry";
  int varID = 0;

  /*
    Look for a free slot in vartable.
    (Create the table the first time through).
  */
  if ( ! varTablesize )
    {
      int i;

      varTablesize = 2;
      vartable = (VARTABLE *) malloc(varTablesize*sizeof(VARTABLE));
      if( vartable == NULL )
	{
          Message(func, "varTablesize = %d", varTablesize);
	  SysError(func, "Allocation of VARTABLE failed");
	}

      for( i = 0; i < varTablesize; i++ )
	vartable[i].code = UNDEF_CODE;
    }
  else
    {
      while( varID < varTablesize )
	{
	  if ( vartable[varID].code == UNDEF_CODE ) break;
	  varID++;
	}
    }
  /*
    If the table overflows, double its size.
  */
  if( varID == varTablesize )
    {
      int i;

      varTablesize = 2*varTablesize;
      vartable = (VARTABLE *) realloc(vartable, varTablesize*sizeof(VARTABLE));
      if( vartable == NULL )
	{
          Message(func, "varTablesize = %d", varTablesize);
	  SysError(func, "Reallocation of VARTABLE failed");
	}
      varID = varTablesize/2;

      for( i = varID; i < varTablesize; i++ )
	vartable[i].code = UNDEF_CODE;
    }

  codeInitEntry(varID, code);

  return (varID);
}


245
void varAddRecord(int recID, int code, int gridID, int zaxistype, int lbounds,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
		  int level1, int level2, int prec, int *pvarID, int *plevelID,
247
		  int numavg, int codetable, int ltype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
249
{
  static char func[] = "varAddRecord";
250
  int varID = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
252
  int levelID = -1;

253
254
  if ( ! (cdiSplitLtype105 == 1 && zaxistype == ZAXIS_HEIGHT) )
    varID = varGetEntry(code, zaxistype, codetable, ltype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
256
257
258
259
260
261

  if ( varID == UNDEFID )
    {
      nvars++;
      varID = codeNewEntry(code);
      if ( prec > vartable[varID].prec ) vartable[varID].prec = prec;
      vartable[varID].gridID    = gridID;
262
263
      vartable[varID].zaxistype = zaxistype;
      vartable[varID].ltype     = ltype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
265
266
267
268
269
270
271
272
273
274
      vartable[varID].lbounds   = lbounds;
      if ( numavg ) vartable[varID].average = 1;
      vartable[varID].codetable = codetable;
    }
  else
    {
      if ( vartable[varID].gridID != gridID )
	{
	  Message(func, "code = %d gridID = %d", code, gridID);
	  Error(func, "horizontal grid must not change for same code");
	}
275
      if ( vartable[varID].zaxistype != zaxistype )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
276
	{
277
278
	  Message(func, "code = %d zaxistype = %d", code, zaxistype);
	  Error(func, "zaxistype must not change for same code");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
	}
    }

  levelID = levelNewEntry(varID, level1, level2);
  vartable[varID].levelTable[levelID].recID = recID;

  if ( CDI_Debug )
    Message(func, "varID = %d  levelID = %d", varID, levelID);

  *pvarID   = varID;
  *plevelID = levelID;
}


int dblcmp(const void *s1, const void *s2)
{
  int cmp = 0;

  if      ( *((double *) s1) < *((double *) s2) ) cmp = -1;
  else if ( *((double *) s1) > *((double *) s2) ) cmp =  1;

  return (cmp);
}


int cmpLevelTable(const void *s1, const void *s2)
{
  int cmp = 0;
  LEVELTABLE *x = (LEVELTABLE *) s1;
  LEVELTABLE *y = (LEVELTABLE *) s2;
  /*
  printf("%g %g  %d %d\n", x->leve11, y->level1, x, y);
  */
  if      ( x->level1 < y->level1 ) cmp = -1;
  else if ( x->level1 > y->level1 ) cmp =  1;

  return (cmp);
}


void cdiGenVars(int streamID)
{
  static char func[] = "cdiGenVars";
  int varID, gridID, zaxisID, levelID;
  int instID, modelID, tableID;
324
  int code, nlevels, zaxistype, lindex, ltype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
325
326
327
  int prec;
  int average;
  int lbounds;
328
  int szip;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
329
330
331
332
333
  char name[256], longname[256], units[256];
  double *dlevels = NULL;
  double *dlevels1 = NULL;
  double *dlevels2 = NULL;
  int vlistID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
334
335
336
  STREAM *streamptr;

  streamptr = stream_to_pointer(streamID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
337
338
339
340
341
342
343
344

  vlistID =  streamInqVlist(streamID);

  for ( varID = 0; varID < nvars; varID++ )
    {
      gridID    = vartable[varID].gridID;
      code      = vartable[varID].code;
      nlevels   = vartable[varID].nlevels;
345
346
347
348
      ltype     = vartable[varID].ltype;
      zaxistype = vartable[varID].zaxistype;
      if ( ltype == 0 && zaxistype == ZAXIS_GENERIC && cdiDefaultLeveltype != -1 )
	zaxistype = cdiDefaultLeveltype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
350
351
352
353
354
      lbounds   = vartable[varID].lbounds;
      prec      = vartable[varID].prec;
      instID    = vartable[varID].instID;
      modelID   = vartable[varID].modelID;
      tableID   = vartable[varID].tableID;
      average   = vartable[varID].average;
355
      szip      = vartable[varID].szip;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
356
357
358

      zaxisID = UNDEFID;

359
360
      if ( ltype == 0 && zaxistype == ZAXIS_GENERIC && nlevels == 1 && ! (fabs(vartable[varID].levelTable[0].level1)>0) )
	zaxistype = ZAXIS_SURFACE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
361
362
363

      dlevels = (double *) malloc(nlevels*sizeof(double));

364
      if ( lbounds && zaxistype != ZAXIS_HYBRID && zaxistype != ZAXIS_HYBRID_HALF )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
	for ( levelID = 0; levelID < nlevels; levelID++ )
	  dlevels[levelID] = (vartable[varID].levelTable[levelID].level1 +
	                      vartable[varID].levelTable[levelID].level2)/2;
      else
	for ( levelID = 0; levelID < nlevels; levelID++ )
	  dlevels[levelID] = vartable[varID].levelTable[levelID].level1;

      if ( nlevels > 1 )
	{
	  int linc = FALSE, ldec = FALSE;
	  /* check increasing of levels */
	  for ( levelID = 1; levelID < nlevels; levelID++ )
	    if ( dlevels[levelID] < dlevels[levelID-1] ) break;

	  if ( levelID == nlevels ) linc = TRUE;

	  if ( linc == FALSE )
	    {
	      /* check decreasing of levels */
	      for ( levelID = 1; levelID < nlevels; levelID++ )
		if ( dlevels[levelID] > dlevels[levelID-1] ) break;

	      if ( levelID == nlevels ) ldec = TRUE;

	      if ( ldec == FALSE ||
390
391
		   zaxistype == ZAXIS_HYBRID ||
		   zaxistype == ZAXIS_DEPTH_BELOW_LAND )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392
393
394
395
396
397
398
		{
		  /*
		  qsort(dlevels, nlevels, sizeof(double), dblcmp);
		  */
		  qsort(vartable[varID].levelTable, nlevels, 
			sizeof(LEVELTABLE), cmpLevelTable);

399
		  if ( lbounds && zaxistype != ZAXIS_HYBRID && zaxistype != ZAXIS_HYBRID_HALF )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
		    for ( levelID = 0; levelID < nlevels; levelID++ )
		      dlevels[levelID] = (vartable[varID].levelTable[levelID].level1 +
					  vartable[varID].levelTable[levelID].level2)/2.;
		  else
		    for ( levelID = 0; levelID < nlevels; levelID++ )
		      dlevels[levelID] = vartable[varID].levelTable[levelID].level1;
		}
	    }
	}

      if ( lbounds )
	{
	  dlevels1 = (double *) malloc(nlevels*sizeof(double));
	  for ( levelID = 0; levelID < nlevels; levelID++ )
	    dlevels1[levelID] = vartable[varID].levelTable[levelID].level1;
	  dlevels2 = (double *) malloc(nlevels*sizeof(double));
	  for ( levelID = 0; levelID < nlevels; levelID++ )
	    dlevels2[levelID] = vartable[varID].levelTable[levelID].level2;
	}

420
421
      zaxisID = varDefZaxis(vlistID, zaxistype, nlevels, dlevels, lbounds, dlevels1, dlevels2,
			    Vctsize, Vct, NULL, NULL, NULL, 0, 0, ltype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
423
424
425
426
427
428
429
430
431
432

      if ( lbounds ) free(dlevels1);
      if ( lbounds ) free(dlevels2);
      free(dlevels);

      varID = streamNewVar(streamID, gridID, zaxisID);
      varID = vlistDefVar(vlistID, gridID, zaxisID, TIME_VARIABLE);

      vlistDefVarCode(vlistID, varID, code);
      vlistDefVarDatatype(vlistID, varID, prec);
      vlistDefVarAverage(vlistID, varID, average);
433
      vlistDefVarSzip(vlistID, varID, szip);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
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

      if ( cdiDefaultTableID != UNDEFID )
	{
	  if ( tableInqParNamePtr(cdiDefaultTableID, code) )
	    {
	      if ( tableID != UNDEFID )
		{
		  strcpy(name, tableInqParNamePtr(cdiDefaultTableID, code));
		  vlistDefVarName(vlistID, varID, name);
		  if ( tableInqParLongnamePtr(cdiDefaultTableID, code) )
		    {
		      strcpy(longname, tableInqParLongnamePtr(cdiDefaultTableID, code));
		      vlistDefVarLongname(vlistID, varID, longname);
		    }
		  if ( tableInqParUnitsPtr(cdiDefaultTableID, code) )
		    {
		      strcpy(units, tableInqParUnitsPtr(cdiDefaultTableID, code));
		      vlistDefVarUnits(vlistID, varID, units);
		    }
		}
	      else
		tableID = cdiDefaultTableID;
	    }
	  if ( cdiDefaultModelID != UNDEFID ) modelID = cdiDefaultModelID;
	  if ( cdiDefaultInstID  != UNDEFID )  instID = cdiDefaultInstID;
	}

      if ( instID  != UNDEFID ) vlistDefVarInstitut(vlistID, varID, instID);
      if ( modelID != UNDEFID ) vlistDefVarModel(vlistID, varID, modelID);
      if ( tableID != UNDEFID ) vlistDefVarTable(vlistID, varID, tableID);
    }

  for ( varID = 0; varID < nvars; varID++ )
    {
      nlevels   = vartable[varID].nlevels;
      /*
      for ( levelID = 0; levelID < nlevels; levelID++ )
	{
	  lindex = vartable[varID].levelTable[levelID].lindex;
	  printf("%d %d %d %d %d\n", varID, levelID, 
		 vartable[varID].levelTable[levelID].lindex,
		 vartable[varID].levelTable[levelID].recID,
		 vartable[varID].levelTable[levelID].level1);
	}
      */
      for ( levelID = 0; levelID < nlevels; levelID++ )
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
481
	  streamptr->vars[varID].level[levelID] =
Uwe Schulzweida's avatar
Uwe Schulzweida committed
482
483
484
485
486
487
488
	    vartable[varID].levelTable[levelID].recID;
	  for ( lindex = 0; lindex < nlevels; lindex++ )
	    if ( levelID == vartable[varID].levelTable[lindex].lindex ) break;

	  if ( lindex == nlevels )
	    Error(func, "Internal problem! lindex not found.");

Uwe Schulzweida's avatar
Uwe Schulzweida committed
489
	  streamptr->vars[varID].lindex[levelID] = lindex;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
	}
    }

  varFree();
}


void varDefVCT(size_t vctsize, double *vctptr)
{
  static char func[] = "varDefVCT";

  if ( Vct == NULL && vctptr != NULL && vctsize > 0 )
    {
      Vctsize = vctsize;
      Vct = (double *) malloc(vctsize*sizeof(double));
      memcpy(Vct, vctptr, vctsize*sizeof(double)); 
    }
}


int varDefGrid(int vlistID, GRID grid, int mode)
{
  /*
    mode: 0 search in vlist and grid table
          1 search in grid table
   */
  static char func[] = "varDefGrid";
  int gridglobdefined = 0;
  int griddefined;
  int ngrids;
  int gridID = UNDEFID;
  int index;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  griddefined = 0;
  ngrids = vlistptr->ngrids;

  if ( mode == 0 )
    for ( index = 0; index < ngrids; index++ )
      {
	gridID = vlistptr->gridIDs[index];
	if ( gridID == UNDEFID )
	  Error(func, "undefined gridID %d", gridID);

	if ( gridCompare(gridID, grid) == 0 )
	  {
	    griddefined = 1;
	    break;
	  }
      }

  if ( ! griddefined )
    {
      ngrids = gridSize();
      for ( gridID = 0; gridID < ngrids; gridID++ )
	{
	  if ( gridCompare(gridID, grid) == 0 )
	    {
	      gridglobdefined = 1;
	      break;
	    }
	}

      ngrids = vlistptr->ngrids;
      if ( mode == 1 )
	for ( index = 0; index < ngrids; index++ )
	  if ( vlistptr->gridIDs[index] == gridID )
	    {
	      gridglobdefined = FALSE;
	      break;
	    }
    }

  if ( ! griddefined )
    {
      if ( ! gridglobdefined ) gridID = gridGenerate(grid);
      ngrids = vlistptr->ngrids;
      vlistptr->gridIDs[ngrids] = gridID;
      vlistptr->ngrids++;
    }

  return (gridID);
}


577
int zaxisCompare(int zaxisID, int zaxistype, int nlevels, int lbounds, double *levels, char *longname, char *units, int ltype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
578
579
580
581
{
  int differ = 1;
  int levelID;
  int zlbounds = 0;
582
  int ltype_is_equal = FALSE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
583

584
585
586
  if ( ltype == zaxisInqLtype(zaxisID) ) ltype_is_equal = TRUE;

  if ( ltype_is_equal && (zaxistype == zaxisInqType(zaxisID) || zaxistype == ZAXIS_GENERIC) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
587
588
589
590
591
592
593
594
595
596
597
    {
      if ( zaxisInqLbounds(zaxisID, NULL) > 0 ) zlbounds = 1;
      if ( nlevels == zaxisInqSize(zaxisID) && zlbounds == lbounds )
	{
	  const double *dlevels;
	  char zlongname[256];
	  char zunits[256];
	  
	  dlevels = zaxisInqLevelsPtr(zaxisID);
	  for ( levelID = 0; levelID < nlevels; levelID++ )
	    {
598
	      if ( fabs(dlevels[levelID] - levels[levelID]) > 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
		break;
	    }

	  if ( levelID == nlevels ) differ = 0;

	  if ( ! differ )
	    {
	      zaxisInqLongname(zaxisID, zlongname);
	      zaxisInqUnits(zaxisID, zunits);
	      if ( longname && zlongname[0] )
		{
		  if ( strcmp(longname, zlongname) != 0 ) differ = 1;
		}
	      if ( units && zunits[0] )
		{
		  if ( strcmp(units, zunits) != 0 ) differ = 1;
		}
	    }
	}
    }

  return (differ);
}


624
625
626
int varDefZaxis(int vlistID, int zaxistype, int nlevels, double *levels, int lbounds, 
		double *levels1, double *levels2, int vctsize, double *vct, char *name, 
		char *longname, char *units, int prec, int mode, int ltype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
{
  /*
    mode: 0 search in vlist and zaxis table
          1 search in zaxis table
   */
  static char func[] = "varDefZaxis";
  int zaxisdefined;
  int nzaxis;
  int zaxisID = UNDEFID;
  int index;
  int zaxisglobdefined = 0;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

  if ( mode == 0 )
    for ( index = 0; index < nzaxis; index++ )
      {
	zaxisID = vlistptr->zaxisIDs[index];

650
	if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, longname, units, ltype) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
651
652
653
654
655
656
657
658
659
660
	  {
	    zaxisdefined = 1;
	    break;
	  }
      }

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
      for ( zaxisID = 0; zaxisID < nzaxis; zaxisID++ )
661
	if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, longname, units, ltype) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
	  {
	    zaxisglobdefined = 1;
	    break;
	  }

      nzaxis = vlistptr->nzaxis;
      if ( mode == 1 )
	for ( index = 0; index < nzaxis; index++ )
	  if ( vlistptr->zaxisIDs[index] == zaxisID )
	    {
	      zaxisglobdefined = FALSE;
	      break;
	    }
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
	{
681
	  zaxisID = zaxisCreate(zaxistype, nlevels);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
682
683
684
685
686
687
688
	  zaxisDefLevels(zaxisID, levels);
	  if ( lbounds )
	    {
	      zaxisDefLbounds(zaxisID, levels1);
	      zaxisDefUbounds(zaxisID, levels2);
	    }

689
	  if ( zaxistype == ZAXIS_HYBRID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
690
691
	    {
	      /* if ( vctsize > 0 && vctsize >= 2*(nlevels+1)) */
692
693
	      /* if ( vctsize > 0 && vctsize >= 2*(nlevels)) */
	      if ( vctsize > 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
694
695
696
697
698
699
700
701
702
		zaxisDefVct(zaxisID, vctsize, vct);
	      else
		Warning(func, "VCT missing");
	    }

	  zaxisDefName(zaxisID, name);
	  zaxisDefLongname(zaxisID, longname);
	  zaxisDefUnits(zaxisID, units);
	  zaxisDefPrec(zaxisID, prec);
703
	  zaxisDefLtype(zaxisID, ltype);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
704
705
706
707
708
709
710
711
712
713
714
	}

      nzaxis = vlistptr->nzaxis;
      vlistptr->zaxisIDs[nzaxis] = zaxisID;
      vlistptr->nzaxis++;
    }

  return (zaxisID);
}


715
716
717
718
719
720
721
void varDefSzip(int varID, int szip)
{
  if ( szip )
    vartable[varID].szip = TRUE;
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
int varInqInst(int varID)
{
  return (vartable[varID].instID);
}


void varDefInst(int varID, int instID)
{
  vartable[varID].instID = instID;
}


int varInqModel(int varID)
{
  return (vartable[varID].modelID);
}


void varDefModel(int varID, int modelID)
{
  vartable[varID].modelID = modelID;
}


int varInqTable(int varID)
{
  return (vartable[varID].tableID);
}


void varDefTable(int varID, int tableID)
{
  vartable[varID].tableID = tableID;
}