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

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


static int  VLIST_Debug = 0;   /* If set to 1, debugging */

14
static int _vlist_min = MIN_VLISTS;
15
static int _vlist_max = MAX_VLISTS;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
16
17
18
19
20
21
22

static void vlist_initialize(void);

static int _vlist_init = FALSE;
#if  defined  (HAVE_LIBPTHREAD)
#  include <pthread.h>

23
static pthread_once_t  _vlist_init_thread = PTHREAD_ONCE_INIT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
24
25
static pthread_mutex_t _vlist_mutex;

26
27
28
29
#  define VLIST_LOCK()         pthread_mutex_lock(&_vlist_mutex)
#  define VLIST_UNLOCK()       pthread_mutex_unlock(&_vlist_mutex)
#  define VLIST_INIT()        \
   if ( _vlist_init == FALSE ) pthread_once(&_vlist_init_thread, vlist_initialize)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30
31
32

#else

33
34
35
36
#  define VLIST_LOCK()
#  define VLIST_UNLOCK()
#  define VLIST_INIT() 	      \
   if ( _vlist_init == FALSE ) vlist_initialize()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
37
38
39
40
41

#endif


typedef struct _vlistPtrToIdx {
42
43
  int      idx;
  int      next;
44
  vlist_t *ptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
45
46
47
} vlistPtrToIdx;


Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
static vlistPtrToIdx *_vlistList  = NULL;
49
static int            _vlistAvail = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50

51
52
static
void vlist_list_new(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
53
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
55
  assert(_vlistList == NULL);

56
  _vlistList = (vlistPtrToIdx *) malloc(_vlist_min*sizeof(vlistPtrToIdx));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
58
}

59
60
static
void vlist_list_delete(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
62
63
64
{
  if ( _vlistList ) free(_vlistList);
}

65
66
static
void vlist_init_pointer(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
68
69
{
  int  i;
  
70
  for ( i = 0; i < _vlist_min; ++i )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
72
    {
      _vlistList[i].idx  = i;
73
74
      _vlistList[i].next = i + 1;
      _vlistList[i].ptr  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
76
    }

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  _vlistList[_vlist_min-1].next = -1;

  _vlistAvail = 0;
}

static
void vlist_list_extend(void)
{
  int nvlists;
  int i;

  assert(_vlistList != NULL);

  nvlists = _vlist_min + MIN_VLISTS;

  if ( nvlists <= _vlist_max)
    {
      _vlistList = (vlistPtrToIdx *) realloc(_vlistList, nvlists*sizeof(vlistPtrToIdx));
  
      for ( i = _vlist_min; i < nvlists; ++i )
	{
	  _vlistList[i].idx  = i;
	  _vlistList[i].next = i + 1;
	  _vlistList[i].ptr  = NULL;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
102

103
104
105
106
107
108
109
      _vlistAvail = _vlist_min;
      _vlistList[_vlist_min-1].next = _vlist_min;
      _vlist_min = nvlists;
      _vlistList[_vlist_min-1].next = -1;
    }
  else
    Warning("Too many open vlists (limit is %d)!", _vlist_max);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
110
111
112
}


113
vlist_t *vlist_to_pointer(int idx)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
{
115
  vlist_t *vlistptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116

117
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118

119
  if ( idx >= 0 && idx < _vlist_min )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
    {
121
      VLIST_LOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
123
124

      vlistptr = _vlistList[idx].ptr;

125
      VLIST_UNLOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127
    }
  else
128
    Error("vlist index %d undefined!", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
130
131
132
133

  return (vlistptr);
}

/* Create an index from a pointer */
134
135
static
int vlist_from_pointer(vlist_t *ptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
136
137
138
139
140
{
  int      idx = -1;

  if ( ptr )
    {
141
142
143
      VLIST_LOCK();

      if ( _vlistAvail < 0 ) vlist_list_extend();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144

145
      if ( _vlistAvail >= 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
	{
147
148
149
150
151
	  vlistPtrToIdx *newptr;

	  newptr       = &_vlistList[_vlistAvail];
	  _vlistAvail  = newptr->next;
	  newptr->next = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
153
154
155
	  idx	       = newptr->idx;
	  newptr->ptr  = ptr;
      
	  if ( VLIST_Debug )
156
	    Message("Pointer %p has idx %d from vlist list", ptr, idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
	}

159
      VLIST_UNLOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
161
    }
  else
162
    Error("Internal problem (pointer %p undefined)", ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
165
166

  return (idx);
}

167
168
static
void vlist_init_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
{
  vlistptr->self           = vlist_from_pointer(vlistptr);

  vlistptr->nlock          = 0;
  vlistptr->nvars          = 0;
  vlistptr->vars           = NULL;
  vlistptr->ngrids         = 0;
  vlistptr->nzaxis         = 0;
  vlistptr->taxisID        = CDI_UNDEFID; 
  vlistptr->instID         = cdiDefaultInstID;
  vlistptr->modelID        = cdiDefaultModelID;
  vlistptr->tableID        = cdiDefaultTableID;
  vlistptr->varsAllocated  = 0;
  vlistptr->ntsteps        = CDI_UNDEFID;
183
  vlistptr->atts.nalloc    = MAX_ATTRIBUTES;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
  vlistptr->atts.nelems    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
185
186
}

187
188
static
vlist_t *vlist_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
{
190
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191

192
  vlistptr = (vlist_t *) malloc(sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
194
195
196
197
198

  if ( vlistptr ) vlist_init_entry(vlistptr);

  return (vlistptr);
}

199
200
static
void vlist_delete_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
202
203
204
205
{
  int idx;

  idx = vlistptr->self;

206
  VLIST_LOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
209
210
211

  free(vlistptr);

  _vlistList[idx].next = _vlistAvail;
  _vlistList[idx].ptr  = 0;
212
  _vlistAvail          = idx;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213

214
  VLIST_UNLOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216

  if ( VLIST_Debug )
217
    Message("Removed idx %d from vlist list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
219
}

220
221
static
void vlist_initialize(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
{
  char *env;

#if  defined  (HAVE_LIBPTHREAD)
  /* initialize global API mutex lock */
  pthread_mutex_init(&_vlist_mutex, NULL);
#endif

  env = getenv("VLIST_DEBUG");
  if ( env ) VLIST_Debug = atoi(env);

  vlist_list_new();
  atexit(vlist_list_delete);

236
  VLIST_LOCK();
237

Uwe Schulzweida's avatar
Uwe Schulzweida committed
238
239
  vlist_init_pointer();

240
  VLIST_UNLOCK();
241

Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
243
244
  _vlist_init = TRUE;
}

245
246
static
void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
248
249
250
{
  int vlistID2;

  vlistID2 = vlistptr2->self;
251
  memcpy(vlistptr2, vlistptr1, sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
  vlistptr2->atts.nelems = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253
254
255
  vlistptr2->self = vlistID2;
}

256
257
static
void vlist_check_ptr(const char *caller, vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
258
259
{
  if ( vlistptr == NULL )
260
    Errorc("vlist undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
261
262
263
}

/*
264
@Function  vlistCreate
Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
266
@Title     Create a variable list

267
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
268

Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
@Example
Here is an example using @func{vlistCreate} to create a variable list
and add a variable with @func{vlistDefVar}.

@Source
#include "cdi.h"
   ...
int vlistID, varID;
   ...
vlistID = vlistCreate();
varID = vlistDefVar(vlistID, gridID, zaxisID, TIME_VARIABLE);
   ...
streamDefVlist(streamID, vlistID);
   ...
vlistDestroy(vlistID);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286
287
@EndFunction
*/
288
int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
290
{
  int vlistID = 0;
291
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292

293
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
295

  vlistptr = vlist_new_entry();
296
  if ( ! vlistptr ) Error("No memory");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
297
298
299

  vlistID = vlistptr->self;

300
  if ( vlistID >= 0 ) vlistLock(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
301
302
303
304
305

  return (vlistID);
}

/*
306
@Function  vlistDestroy
307
@Title     Destroy a variable list
Uwe Schulzweida's avatar
Uwe Schulzweida committed
308

309
@Prototype void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
@Parameter
311
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
313
314

@EndFunction
*/
315
void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
{
317
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
319
320

  vlistptr = vlist_to_pointer(vlistID);

321
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
323
324
325
326

  vlistUnlock(vlistID);

  if ( vlistptr->nlock > 0 )
    {
327
      Warning("vlist %d is locked!", vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
328
329
330
331
    }
  else
    {
      int nvars = vlistptr->nvars;
332
      int varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
333

334
      vlistDelAtts(vlistID, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336

      for ( varID = 0; varID < nvars; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
337
	{
338
339
340
341
342
	  if ( vlistptr->vars[varID].levinfo )  free(vlistptr->vars[varID].levinfo);
	  if ( vlistptr->vars[varID].name )     free(vlistptr->vars[varID].name);
	  if ( vlistptr->vars[varID].longname ) free(vlistptr->vars[varID].longname);
	  if ( vlistptr->vars[varID].stdname )  free(vlistptr->vars[varID].stdname);
	  if ( vlistptr->vars[varID].units )    free(vlistptr->vars[varID].units);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
344
345

	  vlistDelAtts(vlistID, varID);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
346
347
348
349
350
351
352

      if ( vlistptr->vars ) free(vlistptr->vars);

      vlist_delete_entry(vlistptr);
    }
}

353

Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
355
int vlistNlock(int vlistID)
{
356
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
358
359

  vlistptr = vlist_to_pointer(vlistID);

360
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
361
362
363
364

  return (vlistptr->nlock);
}

365

Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
367
void vlistLock(int vlistID)
{
368
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
370
371

  vlistptr = vlist_to_pointer(vlistID);

372
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
374

  vlistptr->nlock++;
375
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
377
}

378

Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
380
void vlistUnlock(int vlistID)
{
381
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
382
383
384

  vlistptr = vlist_to_pointer(vlistID);

385
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
386
387

  vlistptr->nlock--;
388
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389
390
391
392
393
394
395
396
}

/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
397
398
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
400

@Description
401
The function @func{vlistCopy} copies all entries from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
403
404
405
406

@EndFunction
*/
void vlistCopy(int vlistID2, int vlistID1)
{
407
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
408
409
410
411

  vlistptr1 = vlist_to_pointer(vlistID1);
  vlistptr2 = vlist_to_pointer(vlistID2);

412
413
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
414
415
416

  vlist_copy(vlistptr2, vlistptr1);

417
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
418

Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
420
421
422
423
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
      int nlevs, varID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
424
425
      vlistptr2->vars = (var_t *) malloc(nvars*sizeof(var_t));
      memcpy(vlistptr2->vars, vlistptr1->vars, nvars*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

      for ( varID = 0; varID < nvars; varID++ )
	{
	  if ( vlistptr1->vars[varID].name )
	    vlistptr2->vars[varID].name = strdupx(vlistptr1->vars[varID].name);

	  if ( vlistptr1->vars[varID].longname )
	    vlistptr2->vars[varID].longname = strdupx(vlistptr1->vars[varID].longname);

	  if ( vlistptr1->vars[varID].stdname )
	    vlistptr2->vars[varID].stdname = strdupx(vlistptr1->vars[varID].stdname);

	  if ( vlistptr1->vars[varID].units )
	    vlistptr2->vars[varID].units = strdupx(vlistptr1->vars[varID].units);

	  nlevs = vlistptr1->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
	  vlistptr2->vars[varID].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
443
	  memcpy(vlistptr2->vars[varID].levinfo,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
444
		 vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
445
446
447

	  vlistptr2->vars[varID].atts.nelems = 0;
	  vlistCopyVarAtts(vlistID1, varID, vlistID2, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
448
449
450
451
452
453
454
455
456
457
458
459
	}
    }

  vlistptr2->nlock = 0;
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

@Prototype int vlistDuplicate(int vlistID)
@Parameter
460
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
461
462

@Description
463
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
464
465

@Result
466
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
467
468
469
470
471
472

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  int vlistIDnew;
473
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
474
475
476

  vlistptr = vlist_to_pointer(vlistID);

477
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
478

479
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
480
481
482
483
484
485
486
487
488
489

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


void vlistClearFlag(int vlistID)
{
  int varID, levID;
490
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
491
492
493
494
495
496
497
498
499
500
501
502
503
504

  vlistptr = vlist_to_pointer(vlistID);

  for ( varID = 0; varID < vlistptr->nvars; varID++ )
    {
      vlistptr->vars[varID].flag = FALSE;
      for ( levID = 0; levID < vlistptr->vars[varID].nlevs; levID++ )
	{
	  vlistptr->vars[varID].levinfo[levID].flag = FALSE;
	}
    }
}


505
int vlistInqZaxis(int vlistID, int zaxistype, int nlevels, double *levels, int lbounds, double *levels2,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507
508
509
510
511
512
		  int vctsize, const double *vct)
{
  int zaxisdefined;
  int nzaxis;
  int zaxisID = CDI_UNDEFID;
  int index;
  int zaxisglobdefined = 0;
513
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
514
515
516

  vlistptr = vlist_to_pointer(vlistID);

517
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
518
519
520
521
522
523
524
525

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

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

526
      if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
527
528
529
530
531
532
533
534
535
536
	{
	  zaxisdefined = 1;
	  break;
	}
    }

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
      for ( zaxisID = 0; zaxisID < nzaxis; zaxisID++ )
537
	if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
538
539
540
541
542
543
544
545
546
547
	  {
	    zaxisglobdefined = 1;
	    break;
	  }
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
	{
548
	  zaxisID = zaxisCreate(zaxistype, nlevels);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
549
550
551
552
553
554
555
	  zaxisDefLevels(zaxisID, levels);
	  if ( lbounds )
	    {
	      zaxisDefLbounds(zaxisID, levels);
	      zaxisDefUbounds(zaxisID, levels2);
	    }

556
	  if ( zaxistype == ZAXIS_HYBRID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
557
558
559
560
	    {
	      if ( vctsize > 0 )
		zaxisDefVct(zaxisID, vctsize, vct);
	      else
561
		Warning("VCT missing");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
	    }
	}

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

  return (zaxisID);
}

/*
@Function  vlistCopyFlag
@Title     Copy some entries of a variable list

@Prototype void vlistCopyFlag(int vlistID2, int vlistID1)
@Parameter
579
580
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
581
582

@Description
583
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
584
585
586
587
588

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
589
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
590
591
592
593

  vlistptr1 = vlist_to_pointer(vlistID1);
  vlistptr2 = vlist_to_pointer(vlistID2);

594
595
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
596
597
598

  vlist_copy(vlistptr2, vlistptr1);
  
599
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600

Uwe Schulzweida's avatar
Uwe Schulzweida committed
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
      int nvars2 = 0, levID2;
      int nlevs, nlevs2, levID, varID, varID2;
      int gridID, zaxisID;
      int index;

      vlistptr2->ngrids = 0;
      vlistptr2->nzaxis = 0;

      for ( varID = 0; varID < nvars; varID++ )
	if ( vlistptr1->vars[varID].flag ) nvars2++;

      vlistptr2->nvars = nvars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
616
      if ( nvars2 > 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
617
	vlistptr2->vars  = (var_t *) malloc(nvars2*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
618
619
      else
	vlistptr2->vars  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
620
621
622
623
624
625
626
627
628

      varID2 = 0;
      for ( varID = 0; varID < nvars; varID++ )
	if ( vlistptr1->vars[varID].flag )
	  {
	    vlistptr2->vars[varID2].flag = FALSE;
	    zaxisID = vlistptr1->vars[varID].zaxisID;
	    gridID  = vlistptr1->vars[varID].gridID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
629
	    memcpy(&vlistptr2->vars[varID2], &vlistptr1->vars[varID], sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
630
631
632
633

	    vlistptr1->vars[varID].fvarID = varID2;
	    vlistptr2->vars[varID2].fvarID = varID;

634
635
	    vlistptr2->vars[varID2].mvarID = varID2;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
	    if ( vlistptr1->vars[varID].name )
	      vlistptr2->vars[varID2].name = strdupx(vlistptr1->vars[varID].name);

	    if ( vlistptr1->vars[varID].longname )
	      vlistptr2->vars[varID2].longname = strdupx(vlistptr1->vars[varID].longname);

	    if ( vlistptr1->vars[varID].stdname )
	      vlistptr2->vars[varID2].stdname = strdupx(vlistptr1->vars[varID].stdname);

	    if ( vlistptr1->vars[varID].units )
	      vlistptr2->vars[varID2].units = strdupx(vlistptr1->vars[varID].units);

	    nlevs  = vlistptr1->vars[varID].nlevs;
	    nlevs2 = 0;
	    for ( levID = 0; levID < nlevs; levID++ )
	      if ( vlistptr1->vars[varID].levinfo[levID].flag ) nlevs2++;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
653
	    vlistptr2->vars[varID2].levinfo = (levinfo_t *) malloc(nlevs2*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
654

Uwe Schulzweida's avatar
Uwe Schulzweida committed
655
656
657
	    vlistptr2->vars[varID2].atts.nelems = 0;
	    vlistCopyVarAtts(vlistID1, varID, vlistID2, varID2);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
	    if ( nlevs != nlevs2 )
	      {
		int zaxisType;
		int zaxisID2;
		int nvct = 0;
		int lbounds;
		double *levels;
		double *levels2;
		const double *vct = NULL;

		zaxisID = vlistptr1->vars[varID].zaxisID;
		levels = (double *) malloc(nlevs2*sizeof(double));
		levID2 = 0;
		for ( levID = 0; levID < nlevs; levID++ )
		  if ( vlistptr1->vars[varID].levinfo[levID].flag )
		    {
		      vlistptr1->vars[varID].levinfo[levID].flevelID = levID2;
675
		      vlistptr1->vars[varID].levinfo[levID].mlevelID = levID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
676
677
678
679
680
681
682
683
684
685
		      levels[levID2++] = zaxisInqLevel(zaxisID, levID);
		    }

		zaxisType = zaxisInqType(zaxisID);

		if ( zaxisType == ZAXIS_HYBRID )
		  {
		    nvct = zaxisInqVctSize(zaxisID);
		    vct  = zaxisInqVctPtr(zaxisID);
		  }
686
687
		lbounds  = 0;     /* <------------ have to define */
		levels2  = NULL;  /* <------------ have to define */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
688
689
		zaxisID2 = vlistInqZaxis(vlistID2, zaxisType, nlevs2, levels, lbounds, levels2, nvct, vct);
		free(levels);
690
691
692
693
694
695
696
697
698
699
700

		{
		  char ctemp[256];
		  zaxisInqName(zaxisID, ctemp);
		  zaxisDefName(zaxisID2, ctemp);
		  zaxisInqLongname(zaxisID, ctemp);
		  zaxisDefLongname(zaxisID2, ctemp);
		  zaxisInqUnits(zaxisID, ctemp);
		  zaxisDefUnits(zaxisID2, ctemp);
		}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
701
702
703
704
705
706
707
708
709
710
711
712
713
714
		zaxisID = zaxisID2;
		vlistptr2->vars[varID2].zaxisID = zaxisID2;
		vlistptr2->vars[varID2].nlevs   = nlevs2;
	      }

	    for ( levID = 0; levID < nlevs2; levID++ )
	      {
		vlistptr2->vars[varID2].levinfo[levID].flag  = FALSE;
		vlistptr2->vars[varID2].levinfo[levID].index = -1;
	      }

	    levID2 = 0;
	    for ( levID = 0; levID < nlevs; levID++ )
	      if ( vlistptr1->vars[varID].levinfo[levID].flag )
715
716
717
718
719
		{
		  vlistptr2->vars[varID2].levinfo[levID2].flevelID = levID;
		  vlistptr2->vars[varID2].levinfo[levID2].mlevelID = levID;
		  levID2++;
		}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
720
721
722
723

	    for ( index = 0; index <vlistptr2->ngrids; index++ )
	      if (vlistptr2->gridIDs[index] == gridID ) break;

724
	    if ( index == vlistptr2->ngrids )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
725
726
	      {
		vlistptr2->gridIDs[vlistptr2->ngrids++] = gridID;
727
		if (vlistptr2->ngrids >= MAX_GRIDS_PS )
728
		  Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
729
730
	      }

731
732
	    for ( index = 0; index < vlistptr2->nzaxis; index++ )
	      if ( vlistptr2->zaxisIDs[index] == zaxisID ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
733

734
	    if ( index == vlistptr2->nzaxis )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
735
736
	      {
		vlistptr2->zaxisIDs[vlistptr2->nzaxis++] = zaxisID;
737
738
		if (vlistptr2->nzaxis >= MAX_ZAXES_PS )
		  Error("Internal Problem! More than %d zaxis.", MAX_ZAXES_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
739
740
741
742
743
744
745
746
747
748
749
750
751
	      }

	    varID2++;
	  }
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

@Prototype void vlistCat(int vlistID2, int vlistID1)
@Parameter
752
753
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
754
755
756
757
758
759
760
761
762
763
764

@Description
Concatenate the variable list vlistID1 at the end of vlistID2.

@EndFunction
*/
void vlistCat(int vlistID2, int vlistID1)
{
  int nvars, nvars1, nvars2;
  int varID, varID2, nlevs;
  int index, gridID, zaxisID;
765
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
766
767
768
769

  vlistptr1 = vlist_to_pointer(vlistID1);
  vlistptr2 = vlist_to_pointer(vlistID2);

770
771
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
772
773
774
775
776
777

  nvars1 = vlistptr1->nvars;
  nvars2 = vlistptr2->nvars;
  nvars = nvars1 + nvars2;
  vlistptr2->nvars = nvars;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
778
779
  vlistptr2->vars = (var_t *) realloc(vlistptr2->vars, nvars*sizeof(var_t));
  memcpy(vlistptr2->vars+nvars2, vlistptr1->vars, nvars1*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
780
781
782
783
784
785
786

  for ( varID = 0; varID < nvars1; varID++ )
    {
      varID2 = varID + nvars2;
      vlistptr1->vars[varID].fvarID = varID2;
      vlistptr2->vars[varID2].fvarID = varID;

787
788
789
      vlistptr1->vars[varID].mvarID = varID2;
      vlistptr2->vars[varID2].mvarID = varID;

790
791
792
793
794
795
796
797
      if ( vlistptr1->vars[varID].param < 0 )
	{
	  int pnum, pcat, pdis;
	  cdiDecodeParam(vlistptr1->vars[varID].param, &pnum, &pcat, &pdis);
	  pnum=-(varID2+1);
	  vlistptr2->vars[varID2].param = cdiEncodeParam(pnum, pcat, pdis);
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
798
799
800
801
802
803
804
805
806
807
808
809
810
      if ( vlistptr1->vars[varID].name )
	vlistptr2->vars[varID2].name = strdupx(vlistptr1->vars[varID].name);

      if ( vlistptr1->vars[varID].longname )
	vlistptr2->vars[varID2].longname = strdupx(vlistptr1->vars[varID].longname);

      if ( vlistptr1->vars[varID].stdname )
	vlistptr2->vars[varID2].stdname = strdupx(vlistptr1->vars[varID].stdname);

      if ( vlistptr1->vars[varID].units )
	vlistptr2->vars[varID2].units = strdupx(vlistptr1->vars[varID].units);

      nlevs = vlistptr1->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
811
812
      vlistptr2->vars[varID2].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
      memcpy(vlistptr2->vars[varID2].levinfo, vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
813

814
815
816
      vlistptr2->vars[varID2].atts.nelems = 0;
      vlistCopyVarAtts(vlistID1, varID, vlistID2, varID2);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
817
818
819
820
821
822
823
      gridID = vlistptr1->vars[varID].gridID;
      for ( index = 0; index < vlistptr2->ngrids; index++ )
	if ( gridID == vlistptr2->gridIDs[index] ) break;

      if ( index == vlistptr2->ngrids )
	{
	  vlistptr2->gridIDs[vlistptr2->ngrids++] = gridID;
824
	  if ( vlistptr2->ngrids >= MAX_GRIDS_PS )
825
	    Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
826
827
828
829
830
831
832
833
834
	}

      zaxisID = vlistptr1->vars[varID].zaxisID;
      for ( index = 0; index < vlistptr2->nzaxis; index++ )
	if ( zaxisID == vlistptr2->zaxisIDs[index] ) break;

      if ( index == vlistptr2->nzaxis )
	{
	  vlistptr2->zaxisIDs[vlistptr2->nzaxis++] = zaxisID;
835
836
	  if ( vlistptr2->nzaxis >= MAX_ZAXES_PS )
	    Error("Internal Problem! More than %d zaxis.", MAX_ZAXES_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
837
838
839
840
841
842
843
844
845
846
	}
    }
}

/*
@Function  vlistMerge
@Title     Merge two variable lists

@Prototype void vlistMerge(int vlistID2, int vlistID1)
@Parameter
847
848
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
849
850

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
851
Merge the variable list vlistID1 to the variable list vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
852
853
854
855
856
857
858
859
860
861
862

@EndFunction
*/
void vlistMerge(int vlistID2, int vlistID1)
{
  int nvars1, nvars2;
  int varID = 0, varID2, levID, nlevs, nlevs1, nlevs2;
  int index, zaxisID;
  int zaxisID1, zaxisID2;
  int *lvar;
  double *levels;
863
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
864
865
866
867

  vlistptr1 = vlist_to_pointer(vlistID1);
  vlistptr2 = vlist_to_pointer(vlistID2);

868
869
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884

  nvars1 = vlistptr1->nvars;
  nvars2 = vlistptr2->nvars;

  if ( nvars1 == nvars2 )
    {
      for ( varID = 0; varID < nvars2; varID++ )
	{
	  if ( vlistptr1->vars[varID].name && vlistptr2->vars[varID].name )
	    {
	      if ( strcmp(vlistptr1->vars[varID].name,
			  vlistptr2->vars[varID].name) != 0 ) break;
	    }
	  else
	    {
885
	      if ( vlistptr1->vars[varID].param != vlistptr2->vars[varID].param )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
886
887
888
889
890
891
892
893
894
895
896
897
		break;
	    }
	}
    }

  if ( varID == nvars2 ) /* same variables in vlistID1 and vlistID2 */
    {
      for ( varID = 0; varID < nvars2; varID++ )
	{
	  vlistptr1->vars[varID].fvarID = varID;
	  vlistptr2->vars[varID].fvarID = varID;

898
899
900
	  vlistptr1->vars[varID].mvarID = varID;
	  vlistptr2->vars[varID].mvarID = varID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
901
902
903
904
905
906
	  nlevs1 = vlistptr1->vars[varID].nlevs;
	  nlevs2 = vlistptr2->vars[varID].nlevs;

	  nlevs = nlevs1 + nlevs2;

	  vlistptr2->vars[varID].nlevs = nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
907
	  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
908
	  fprintf(stderr, "var %d %d %d %d %d\n", varID, nlevs1, nlevs2, nlevs, sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
909
	  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
910
	  vlistptr2->vars[varID].levinfo =
Uwe Schulzweida's avatar
Uwe Schulzweida committed
911
	    (levinfo_t *) realloc(vlistptr2->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
912

Uwe Schulzweida's avatar
Uwe Schulzweida committed
913
	  memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
914
		 vlistptr1->vars[varID].levinfo, nlevs1*sizeof(levinfo_t));
915
916
917
918
919

	  for ( levID = 0; levID < nlevs1; levID++ )
	    {
	      vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
	}

      lvar = (int *) malloc(nvars2*sizeof(int));
      for ( varID = 0; varID < nvars2; varID++ ) lvar[varID] = FALSE;

      for ( varID = 0; varID < nvars2; varID++ )
	{
	  if ( lvar[varID] == TRUE ) continue;

	  zaxisID1 = vlistptr1->vars[varID].zaxisID;
	  zaxisID2 = vlistptr2->vars[varID].zaxisID;
	  /*
	  nlevs1 = vlistptr1->vars[varID].nlevs;
	  nlevs2 = vlistptr2->vars[varID].nlevs;
	  */
	  nlevs1 = zaxisInqSize(zaxisID1);
	  nlevs2 = zaxisInqSize(zaxisID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
937
938
939
	  /*
	  fprintf(stderr, "zaxis %d %d %d %d\n", zaxisID1, zaxisID2, nlevs1, nlevs2);
	  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
940
941
942
943
944
945
946
947
948
	  nlevs = nlevs1 + nlevs2;

	  zaxisID = zaxisDuplicate(zaxisID2);

	  zaxisResize(zaxisID, nlevs);

	  levels = (double *) malloc(nlevs1*sizeof(double));

	  zaxisInqLevels(zaxisID1, levels);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
949
950
951
952
	  /*
	  for ( levID = 0; levID < nlevs1; levID++ )
	    fprintf(stderr, "%d %d %d %d %d %g\n", varID, levID, nlevs1, nlevs2, vlistptr2->vars[varID].nlevs, levels[levID]);
	  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
953
954
955
956
957
958
959
960
961
	  for ( levID = 0; levID < nlevs1; levID++ )
	    zaxisDefLevel(zaxisID, nlevs2+levID, levels[levID]);

	  free(levels);

	  for ( index = 0; index < vlistptr2->nzaxis; index++ )
	    if ( vlistptr2->zaxisIDs[index] == zaxisID2 )
	      vlistptr2->zaxisIDs[index] = zaxisID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
962
	  for ( varID2 = 0; varID2 < nvars2; varID2++ )
963
	    if ( lvar[varID2] == FALSE && vlistptr2->vars[varID2].zaxisID == zaxisID2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
	      {
		vlistptr2->vars[varID2].zaxisID = zaxisID;
		lvar[varID2] = TRUE;
	      }
	}

      free(lvar);
    }
  else
    {
      vlistCat(vlistID2, vlistID1);
    }
}

/*
@Function  vlistNvars
@Title     Number of variables in a variable list

@Prototype int vlistNvars(int vlistID)
@Parameter
984
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
985
986

@Description
987
The function @func{vlistNvars} returns the number of variables in the variable list vlistID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
988
989

@Result
990
@func{vlistNvars} returns the number of variables in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
991
992
993
994
995

@EndFunction
*/
int vlistNvars(int vlistID)
{
996
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
997
998
999

  vlistptr = vlist_to_pointer(vlistID);

1000
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1001
1002
1003
1004

  return (vlistptr->nvars);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1005

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1006
1007
1008
int vlistNrecs(int vlistID)
{
  int varID, nrecs = 0;
1009
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1010
1011
1012

  vlistptr = vlist_to_pointer(vlistID);

1013
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1014
1015
1016
1017
1018
1019
1020
1021

  for ( varID = 0; varID < vlistptr->nvars; varID++ )
    nrecs +=  vlistptr->vars[varID].nlevs;

  return (nrecs);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
1022
1023
int vlistNumber(int vlistID)
{
1024
  int varID, number, number2, datatype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1025
1026
1027
1028
  vlist_t *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

1029
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1030

1031
1032
1033
1034
1035
1036
  datatype = vlistptr->vars[0].datatype;
  if (  datatype== DATATYPE_CPX32 || datatype == DATATYPE_CPX64 )
    number = CDI_COMP;
  else
    number = CDI_REAL;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1037
  for ( varID = 1; varID < vlistptr->nvars; varID++ )
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
    {
      datatype = vlistptr->vars[varID].datatype;
      if ( datatype == DATATYPE_CPX32 || datatype == DATATYPE_CPX64 )
	number2 = CDI_COMP;
      else
	number2 = CDI_REAL;

      if ( number2 != number )
	{
	  number = CDI_BOTH;
	  break;
	}
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1051
1052
1053
1054

  return (number);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1055
1056
1057
1058
1059
1060
/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

@Prototype int vlistNgrids(int vlistID)
@Parameter
1061
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1062
1063

@Description
1064
The function @func{vlistNgrids} returns the number of grids in the variable list vlistID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1065
1066

@Result
1067
@func{vlistNgrids} returns the number of grids in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1068
1069
1070
1071
1072

@EndFunction
*/
int vlistNgrids(int vlistID)
{
1073
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1074
1075
1076

  vlistptr = vlist_to_pointer(vlistID);

1077
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087

  return (vlistptr->ngrids);
}

/*
@Function  vlistNzaxis
@Title     Number of zaxis in a variable list

@Prototype int vlistNzaxis(int vlistID)
@Parameter
1088
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1089
1090

@Description
1091
The function @func{vlistNzaxis} returns the number of zaxis in the variable list vlistID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1092
1093

@Result
1094
@func{vlistNzaxis} returns the number of zaxis in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1095
1096
1097
1098
1099

@EndFunction
*/
int vlistNzaxis(int vlistID)
{
1100
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1101
1102
1103

  vlistptr = vlist_to_pointer(vlistID);

1104
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1105
1106
1107
1108
1109
1110
1111

  return (vlistptr->nzaxis);
}


void vlistDefNtsteps(int vlistID, int nts)
{
1112
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1113
1114
1115

  vlistptr = vlist_to_pointer(vlistID);

1116
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1117
1118
1119
1120
1121
1122
1123

  vlistptr->ntsteps = nts;
}


int vlistNtsteps(int vlistID)
{
1124
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1125
1126
1127

  vlistptr = vlist_to_pointer(vlistID);

1128
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1129
1130
1131
1132
1133
1134
1135
1136

  return (vlistptr->ntsteps);
}


void vlistPrint(int vlistID)
{
  int nvars, flag, index;
1137
  int varID, fvarID, mvarID, flevID, mlevID, levID;
1138
  int param, gridID, zaxisID, timeID, nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1139
  int dtype;
1140
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1141
1142
  char *name, *longname, *units;
  double level;
1143
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1144
1145
1146

  vlistptr = vlist_to_pointer(vlistID);

1147
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164

  printf("#\n# vlistID %d\n#\n", vlistID);

  if ( vlistID == CDI_UNDEFID ) return;

  nvars = vlistptr->nvars;

  printf("nvars   %d\n", nvars);
  printf("ngrids  %d\n", vlistptr->ngrids);
  printf("nzaxis  %d\n", vlistptr->nzaxis);
  printf("taxisID %d\n", vlistptr->taxisID);
  printf("instID  %d\n", vlistptr->instID);
  printf("modelID %d\n", vlistptr->modelID);
  printf("tableID %d\n", vlistptr->tableID);

  if ( nvars > 0 )
    {
1165
      printf(" varID param    gridID zaxisID timeID nlevel flag  name     longname\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1166
1167
      for ( varID = 0; varID < nvars; varID++ )
	{
1168
	  param    = vlistptr->vars[varID].param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1169
1170
1171
1172
1173
1174
1175
1176
	  gridID   = vlistptr->vars[varID].gridID;
	  zaxisID  = vlistptr->vars[varID].zaxisID;
	  timeID   = vlistptr->vars[varID].timeID;
	  nlevs    = vlistptr->vars[varID].nlevs;
	  name     = vlistptr->vars[varID].name;
	  longname = vlistptr->vars[varID].longname;
	  units    = vlistptr->vars[varID].units;
	  flag     = vlistptr->vars[varID].flag;
1177
1178
1179
1180

	  cdiParamToString(param, paramstr, sizeof(paramstr));
	  printf("%6d %-8s %6d %6d %6d %6d %5d  %-8s %s",
		 varID, paramstr, gridID, zaxisID, timeID, nlevs, flag,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1181
1182
1183
1184
1185
1186
		 name ? name : "", longname ? longname : "");
	  if ( units ) printf("   [%s]", units);
	  printf("\n");
	}

      printf("\n");
1187
      printf(" varID  levID fvarID flevID mvarID mlevID  index  dtype  flag  level\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1188
1189
1190
1191
1192
      for ( varID = 0; varID < nvars; varID++ )
	{
	  nlevs    = vlistptr->vars[varID].nlevs;
	  zaxisID  = vlistptr->vars[varID].zaxisID;
	  fvarID   = vlistptr->vars[varID].fvarID;
1193
	  mvarID   = vlistptr->vars[varID].mvarID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1194
1195
1196
1197
	  dtype    = vlistptr->vars[varID].datatype;
	  for ( levID = 0; levID < nlevs; levID++ )
	    {
	      flevID = vlistptr->vars[varID].levinfo[levID].flevelID;
1198
	      mlevID = vlistptr->vars[varID].levinfo[levID].mlevelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1199
1200
1201
	      index  = vlistptr->vars[varID].levinfo[levID].index;
	      flag   = vlistptr->vars[varID].levinfo[levID].flag;
	      level  = zaxisInqLevel(zaxisID, levID);
1202
1203
	      printf("%6d %6d %6d %6d %6d %6d %6d %6d %5d  %.9g\n",
		     varID, levID, fvarID, flevID, mvarID, mlevID, index, dtype, flag, level);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1204
1205
1206
1207
1208
	    }
	}
    }
}

1209
1210
1211
1212
1213
1214
1215
/*
@Function  vlistDefTaxis
@Title     Define the time axis

@Prototype void vlistDefTaxis(int vlistID, int taxisID)
@Parameter
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1216
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
1217
1218
1219
1220
1221
1222

@Description
The function @func{vlistDefTaxis} defines the time axis of a variable list.

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1223
1224
void vlistDefTaxis(int vlistID, int taxisID)
{
1225
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1226
1227
1228

  vlistptr = vlist_to_pointer(vlistID);

1229
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1230
1231
1232
1233

  vlistptr->taxisID = taxisID;
}

1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
/*
@Function  vlistInqTaxis
@Title     Get the time axis

@Prototype int vlistInqTaxis(int vlistID)
@Parameter
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}

@Description
The function @func{vlistInqTaxis} returns the time axis of a variable list.

@Result
@func{vlistInqTaxis} returns an identifier to the time axis.

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1250
1251
int vlistInqTaxis(int vlistID)
{
1252
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1253
1254
1255

  vlistptr = vlist_to_pointer(vlistID);

1256
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1257
1258
1259
1260

  return (vlistptr->taxisID);
}

1261

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1262
1263
void  vlistDefTable(int vlistID, int tableID)
{
1264
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1265
1266
1267

  vlistptr = vlist_to_pointer(vlistID);

1268
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1269
1270
1271
1272

  vlistptr->tableID = tableID;
}

1273

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1274
1275
int vlistInqTable(int vlistID)
{
1276
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1277
1278
1279

  vlistptr = vlist_to_pointer(vlistID);

1280
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1281
1282
1283
1284

  return (vlistptr->tableID);
}

1285

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1286
1287
void vlistDefInstitut(int vlistID, int instID)
{
1288
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1289
1290
1291

  vlistptr = vlist_to_pointer(vlistID);

1292
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1293
1294
1295
1296

  vlistptr->instID = instID;
}

1297

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1298
1299
1300
int vlistInqInstitut(int vlistID)
{
  int varID, instID;
1301
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1302
1303
1304

  vlistptr = vlist_to_pointer(vlistID);

1305
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325

  instID = vlistptr->instID;

  if ( instID == CDI_UNDEFID )
    {
      instID  = vlistInqVarInstitut(vlistID, 0);

      for ( varID = 1; varID < vlistptr->nvars; varID++ )
	if ( instID != vlistInqVarInstitut(vlistID, varID) )
	  {
	    instID = CDI_UNDEFID;
	    break;
      }
      
      vlistDefInstitut(vlistID, instID);
    }

  return (instID);
}

1326

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1327
1328
void vlistDefModel(int vlistID, int modelID)
{
1329
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1330
1331
1332

  vlistptr = vlist_to_pointer(vlistID);

1333
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1334
1335
1336
1337

  vlistptr->modelID = modelID;
}

1338

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1339
1340
1341
int vlistInqModel(int vlistID)
{
  int varID, modelID;
1342
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1343
1344
1345

  vlistptr = vlist_to_pointer(vlistID);

1346
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366

  modelID = vlistptr->modelID;

  if ( modelID == CDI_UNDEFID )
    {
      modelID = vlistInqVarModel(vlistID, 0);

      for ( varID = 1; varID < vlistptr->nvars; varID++ )
	if ( modelID != vlistInqVarModel(vlistID, varID) )
	  {
	    modelID = CDI_UNDEFID;
	    break;
	  }

      vlistDefModel(vlistID, modelID);
    }

  return (modelID);
}

1367