vlist.c 37.2 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
#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"
10
#include "namespace.h"
11
#include "pio_util.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
12
13
14
15


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

16
17
18
19
20
static int *vlist_list_size;

enum {
  _vlist_max = MAX_VLISTS,
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22
23
24
25
26
27

static void vlist_initialize(void);

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

28
static pthread_once_t  _vlist_init_thread = PTHREAD_ONCE_INIT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
30
static pthread_mutex_t _vlist_mutex;

31
32
33
34
#  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
35
36
37

#else

38
39
40
41
#  define VLIST_LOCK()
#  define VLIST_UNLOCK()
#  define VLIST_INIT() 	      \
   if ( _vlist_init == FALSE ) vlist_initialize()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
42
43
44
45
46

#endif


typedef struct _vlistPtrToIdx {
47
  int      code;
48
  int      next;
49
  vlist_t *ptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50
51
52
} vlistPtrToIdx;


53
54
static vlistPtrToIdx **_vlistList  = NULL;
static int            *_vlistAvail;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55

56
57
static
void vlist_list_new(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
58
{
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  int nnsp, i;

  nnsp = namespaceGetNumber();

  _vlistList = xcalloc(nnsp, sizeof(_vlistList[0]));
  _vlistAvail = xcalloc(nnsp, sizeof(_vlistAvail[0]));
  vlist_list_size = xcalloc(nnsp, sizeof(vlist_list_size[0]));
  for (i = 0; i < nnsp; ++i)
  {
    assert(_vlistList[i] == NULL);
    vlist_list_size[i] = MIN_VLISTS;
    _vlistList[i]
      = (vlistPtrToIdx *) xmalloc(vlist_list_size[i] * sizeof(vlistPtrToIdx));
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
74
}

75
76
static
void vlist_list_delete(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
{
78
79
80
81
82
83
84
85
86
87
88
89
  int nnsp;

  nnsp = namespaceGetNumber();
  if ( _vlistList )
  {
    int i;
    for (i = 0; i < nnsp; ++i)
      if (_vlistList[i])
        free(_vlistList[i]);
    free(_vlistList);
    free(_vlistAvail);
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
91
}

92
93
static
void vlist_init_pointer(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
94
{
95
96
97
98
99
100
101
  int nnsp, i, j;

  nnsp = namespaceGetNumber();

  for ( j = 0; j < nnsp; ++j )
  {
    for ( i = 0; i < vlist_list_size[j]; ++i )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
102
    {
103
      _vlistList[j][i].code = namespaceIdxEncode2(i, j);
104
105
      _vlistList[j][i].next = i + 1;
      _vlistList[j][i].ptr  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
106
107
    }

108
109
110
    _vlistList[j][vlist_list_size[j]-1].next = -1;
    _vlistAvail[j] = 0;
  }
111
112
113
114
115
}

static
void vlist_list_extend(void)
{
116
117
  int new_list_size;
  int i, nsp;
118

119
  nsp = namespaceGetActive ();
120

121
  assert(_vlistList[nsp]);
122

123
124
125
  new_list_size = vlist_list_size[nsp] + MIN_VLISTS;

  if ( new_list_size <= _vlist_max)
126
    {
127
128
129
130
      _vlistList[nsp] =
        (vlistPtrToIdx *) xrealloc(_vlistList[nsp],
                                   new_list_size * sizeof(vlistPtrToIdx));
      for ( i = vlist_list_size[nsp]; i < new_list_size; ++i )
131
	{
132
	  _vlistList[nsp][i].code = namespaceIdxEncode2(i, nsp);
133
134
	  _vlistList[nsp][i].next = i + 1;
	  _vlistList[nsp][i].ptr  = NULL;
135
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
136

137
138
139
140
      _vlistAvail[nsp] = vlist_list_size[nsp];
      _vlistList[nsp][vlist_list_size[nsp]-1].next = vlist_list_size[nsp];
      vlist_list_size[nsp] = new_list_size;
      _vlistList[nsp][vlist_list_size[nsp]-1].next = -1;
141
142
143
    }
  else
    Warning("Too many open vlists (limit is %d)!", _vlist_max);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
145
146
}


147
vlist_t *vlist_to_pointer(int code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
{
149
  vlist_t *vlistptr = NULL;
150
151
  int i, nsp, idx;
  namespaceTuple_t nspt;
152

153
154
155
156
157
  nsp = namespaceGetActive();

  nspt = namespaceIdxDecode(code);
  assert(nspt.nsp == nsp);
  idx = nspt.idx;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158

159
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160

161
  if ( idx >= 0 && idx < vlist_list_size[nsp] )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
    {
163
      VLIST_LOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
164

165
      vlistptr = _vlistList[nsp][idx].ptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
166

167
      VLIST_UNLOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
169
    }
  else
170
    Error("vlist index %d undefined!", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
171
172
173
174
175

  return (vlistptr);
}

/* Create an index from a pointer */
176
177
static
int vlist_from_pointer(vlist_t *ptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
{
179
  int code = -1, nsp;
180
181

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
183
184

  if ( ptr )
    {
185
186
      VLIST_LOCK();

187
      if ( _vlistAvail[nsp] < 0 ) vlist_list_extend();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188

189
      if ( _vlistAvail[nsp] >= 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
	{
191
192
	  vlistPtrToIdx *newptr;

193
194
	  newptr       = &_vlistList[nsp][_vlistAvail[nsp]];
	  _vlistAvail[nsp]  = newptr->next;
195
	  newptr->next = -1;
196
	  code	       = newptr->code;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
	  newptr->ptr  = ptr;
198

Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
	  if ( VLIST_Debug )
200
	    Message("Pointer %p has idx %d from vlist list", ptr, code);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
202
	}

203
      VLIST_UNLOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
205
    }
  else
206
    Error("Internal problem (pointer %p undefined)", ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207

208
  return (code);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209
210
}

211
212
static
void vlist_init_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
213
214
215
216
217
218
219
220
{
  vlistptr->self           = vlist_from_pointer(vlistptr);

  vlistptr->nlock          = 0;
  vlistptr->nvars          = 0;
  vlistptr->vars           = NULL;
  vlistptr->ngrids         = 0;
  vlistptr->nzaxis         = 0;
221
  vlistptr->taxisID        = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
223
224
225
226
  vlistptr->instID         = cdiDefaultInstID;
  vlistptr->modelID        = cdiDefaultModelID;
  vlistptr->tableID        = cdiDefaultTableID;
  vlistptr->varsAllocated  = 0;
  vlistptr->ntsteps        = CDI_UNDEFID;
227
  vlistptr->atts.nalloc    = MAX_ATTRIBUTES;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
228
  vlistptr->atts.nelems    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
230
}

231
232
static
vlist_t *vlist_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
233
{
234
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
235

236
  vlistptr = (vlist_t *)xmalloc(sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
238
239
240
241
242

  if ( vlistptr ) vlist_init_entry(vlistptr);

  return (vlistptr);
}

243
244
static
void vlist_delete_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
{
246
247
248
  int idx, nsp;

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
250
251

  idx = vlistptr->self;

252
  VLIST_LOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253
254
255

  free(vlistptr);

256
257
258
  _vlistList[nsp][idx].next = _vlistAvail[nsp];
  _vlistList[nsp][idx].ptr  = 0;
  _vlistAvail[nsp]          = idx;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259

260
  VLIST_UNLOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
261
262

  if ( VLIST_Debug )
263
    Message("Removed idx %d from vlist list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
265
}

266
267
static
void vlist_initialize(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
{
  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);

282
  VLIST_LOCK();
283

Uwe Schulzweida's avatar
Uwe Schulzweida committed
284
285
  vlist_init_pointer();

286
  VLIST_UNLOCK();
287

Uwe Schulzweida's avatar
Uwe Schulzweida committed
288
289
290
  _vlist_init = TRUE;
}

291
292
static
void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
294
295
296
{
  int vlistID2;

  vlistID2 = vlistptr2->self;
297
  memcpy(vlistptr2, vlistptr1, sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
  vlistptr2->atts.nelems = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
299
300
301
  vlistptr2->self = vlistID2;
}

302
303
static
void vlist_check_ptr(const char *caller, vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
304
305
{
  if ( vlistptr == NULL )
306
    Errorc("vlist undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
307
308
309
}

/*
310
@Function  vlistCreate
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
312
@Title     Create a variable list

313
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
314

Uwe Schulzweida's avatar
Uwe Schulzweida committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
@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
332
333
@EndFunction
*/
334
int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336
{
  int vlistID = 0;
337
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
338

339
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
340
341

  vlistptr = vlist_new_entry();
342
  if ( ! vlistptr ) Error("No memory");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
344
345

  vlistID = vlistptr->self;

346
  if ( vlistID >= 0 ) vlistLock(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
348
349
350
351

  return (vlistID);
}

/*
352
@Function  vlistDestroy
353
@Title     Destroy a variable list
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354

355
@Prototype void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
356
@Parameter
357
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
358
359
360

@EndFunction
*/
361
void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
362
{
363
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
365
366

  vlistptr = vlist_to_pointer(vlistID);

367
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
369
370
371
372

  vlistUnlock(vlistID);

  if ( vlistptr->nlock > 0 )
    {
373
      Warning("vlist %d is locked!", vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374
375
376
377
    }
  else
    {
      int nvars = vlistptr->nvars;
378
      int varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379

380
      vlistDelAtts(vlistID, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
382

      for ( varID = 0; varID < nvars; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
383
	{
384
385
386
387
388
	  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
389
390
391

	  vlistDelAtts(vlistID, varID);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392
393
394
395
396
397
398

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

      vlist_delete_entry(vlistptr);
    }
}

399

Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
401
int vlistNlock(int vlistID)
{
402
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
404
405

  vlistptr = vlist_to_pointer(vlistID);

406
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
407
408
409
410

  return (vlistptr->nlock);
}

411

Uwe Schulzweida's avatar
Uwe Schulzweida committed
412
413
void vlistLock(int vlistID)
{
414
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
415
416
417

  vlistptr = vlist_to_pointer(vlistID);

418
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
420

  vlistptr->nlock++;
421
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
423
}

424

Uwe Schulzweida's avatar
Uwe Schulzweida committed
425
426
void vlistUnlock(int vlistID)
{
427
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
428
429
430

  vlistptr = vlist_to_pointer(vlistID);

431
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
432
433

  vlistptr->nlock--;
434
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
435
436
437
438
439
440
441
442
}

/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
443
444
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
445
446

@Description
447
The function @func{vlistCopy} copies all entries from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
448
449
450
451
452

@EndFunction
*/
void vlistCopy(int vlistID2, int vlistID1)
{
453
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
455
456
457

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

458
459
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
460
461
462

  vlist_copy(vlistptr2, vlistptr1);

463
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
464

Uwe Schulzweida's avatar
Uwe Schulzweida committed
465
466
467
468
469
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
      int nlevs, varID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
470
471
      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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487

      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
488
	  vlistptr2->vars[varID].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
489
	  memcpy(vlistptr2->vars[varID].levinfo,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
490
		 vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
491
492
493

	  vlistptr2->vars[varID].atts.nelems = 0;
	  vlistCopyVarAtts(vlistID1, varID, vlistID2, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
494
495
496
497
498
499
500
501
502
503
504
505
	}
    }

  vlistptr2->nlock = 0;
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

@Description
509
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
510
511

@Result
512
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
513
514
515
516
517
518

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  int vlistIDnew;
519
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
521
522

  vlistptr = vlist_to_pointer(vlistID);

523
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
524

525
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
527
528
529
530
531
532
533
534
535

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


void vlistClearFlag(int vlistID)
{
  int varID, levID;
536
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550

  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;
	}
    }
}


551
int vlistInqZaxis(int vlistID, int zaxistype, int nlevels, double *levels, int lbounds, double *levels2,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
552
553
554
555
556
557
558
		  int vctsize, const double *vct)
{
  int zaxisdefined;
  int nzaxis;
  int zaxisID = CDI_UNDEFID;
  int index;
  int zaxisglobdefined = 0;
559
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
560
561
562

  vlistptr = vlist_to_pointer(vlistID);

563
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
564
565
566
567
568
569
570
571

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

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

572
      if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
573
574
575
576
577
578
579
580
581
582
	{
	  zaxisdefined = 1;
	  break;
	}
    }

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
      for ( zaxisID = 0; zaxisID < nzaxis; zaxisID++ )
583
	if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
584
585
586
587
588
589
590
591
592
593
	  {
	    zaxisglobdefined = 1;
	    break;
	  }
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
	{
594
	  zaxisID = zaxisCreate(zaxistype, nlevels);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
595
596
597
598
599
600
601
	  zaxisDefLevels(zaxisID, levels);
	  if ( lbounds )
	    {
	      zaxisDefLbounds(zaxisID, levels);
	      zaxisDefUbounds(zaxisID, levels2);
	    }

602
	  if ( zaxistype == ZAXIS_HYBRID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
603
604
605
606
	    {
	      if ( vctsize > 0 )
		zaxisDefVct(zaxisID, vctsize, vct);
	      else
607
		Warning("VCT missing");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
	    }
	}

      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
625
626
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
627
628

@Description
629
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
630
631
632
633
634

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
635
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
636
637
638
639

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

640
641
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
642
643
644

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
  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
662
      if ( nvars2 > 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
663
	vlistptr2->vars  = (var_t *) malloc(nvars2*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
664
665
      else
	vlistptr2->vars  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
666
667
668
669
670
671
672
673
674

      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
675
	    memcpy(&vlistptr2->vars[varID2], &vlistptr1->vars[varID], sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
676
677
678
679

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

680
681
	    vlistptr2->vars[varID2].mvarID = varID2;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
	    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
699
	    vlistptr2->vars[varID2].levinfo = (levinfo_t *) malloc(nlevs2*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
700

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
	    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;
721
		      vlistptr1->vars[varID].levinfo[levID].mlevelID = levID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
722
723
724
725
726
727
728
729
730
731
		      levels[levID2++] = zaxisInqLevel(zaxisID, levID);
		    }

		zaxisType = zaxisInqType(zaxisID);

		if ( zaxisType == ZAXIS_HYBRID )
		  {
		    nvct = zaxisInqVctSize(zaxisID);
		    vct  = zaxisInqVctPtr(zaxisID);
		  }
732
733
		lbounds  = 0;     /* <------------ have to define */
		levels2  = NULL;  /* <------------ have to define */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
734
735
		zaxisID2 = vlistInqZaxis(vlistID2, zaxisType, nlevs2, levels, lbounds, levels2, nvct, vct);
		free(levels);
736
737
738
739
740
741
742
743
744
745
746

		{
		  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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
		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 )
761
762
763
764
765
		{
		  vlistptr2->vars[varID2].levinfo[levID2].flevelID = levID;
		  vlistptr2->vars[varID2].levinfo[levID2].mlevelID = levID;
		  levID2++;
		}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
766
767
768
769

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

770
	    if ( index == vlistptr2->ngrids )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
771
772
	      {
		vlistptr2->gridIDs[vlistptr2->ngrids++] = gridID;
773
		if (vlistptr2->ngrids >= MAX_GRIDS_PS )
774
		  Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
775
776
	      }

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

780
	    if ( index == vlistptr2->nzaxis )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
781
782
	      {
		vlistptr2->zaxisIDs[vlistptr2->nzaxis++] = zaxisID;
783
784
		if (vlistptr2->nzaxis >= MAX_ZAXES_PS )
		  Error("Internal Problem! More than %d zaxis.", MAX_ZAXES_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
785
786
787
788
789
790
791
792
793
794
795
796
797
	      }

	    varID2++;
	  }
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

@Prototype void vlistCat(int vlistID2, int vlistID1)
@Parameter
798
799
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
800
801
802
803
804
805
806
807
808
809
810

@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;
811
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
812
813
814
815

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

816
817
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
818
819
820
821
822
823

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
824
825
  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
826
827
828
829
830
831
832

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

833
834
835
      vlistptr1->vars[varID].mvarID = varID2;
      vlistptr2->vars[varID2].mvarID = varID;

836
837
838
839
840
841
842
843
      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
844
845
846
847
848
849
850
851
852
853
854
855
856
      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
857
858
      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
859

860
861
862
      vlistptr2->vars[varID2].atts.nelems = 0;
      vlistCopyVarAtts(vlistID1, varID, vlistID2, varID2);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
863
864
865
866
867
868
869
      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;
870
	  if ( vlistptr2->ngrids >= MAX_GRIDS_PS )
871
	    Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
872
873
874
875
876
877
878
879
880
	}

      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;
881
882
	  if ( vlistptr2->nzaxis >= MAX_ZAXES_PS )
	    Error("Internal Problem! More than %d zaxis.", MAX_ZAXES_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
883
884
885
886
887
888
889
890
891
892
	}
    }
}

/*
@Function  vlistMerge
@Title     Merge two variable lists

@Prototype void vlistMerge(int vlistID2, int vlistID1)
@Parameter
893
894
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
895
896

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
897
Merge the variable list vlistID1 to the variable list vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
898
899
900
901
902
903
904
905
906
907
908

@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;
909
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
910
911
912
913

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

914
915
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930

  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
	    {
931
	      if ( vlistptr1->vars[varID].param != vlistptr2->vars[varID].param )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
932
933
934
935
936
937
938
939
940
941
942
943
		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;

944
945
946
	  vlistptr1->vars[varID].mvarID = varID;
	  vlistptr2->vars[varID].mvarID = varID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
947
948
949
950
951
952
	  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
953
	  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
954
	  fprintf(stderr, "var %d %d %d %d %d\n", varID, nlevs1, nlevs2, nlevs, sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
955
	  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
956
	  vlistptr2->vars[varID].levinfo =
Uwe Schulzweida's avatar
Uwe Schulzweida committed
957
	    (levinfo_t *) realloc(vlistptr2->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
958

Uwe Schulzweida's avatar
Uwe Schulzweida committed
959
	  memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
960
		 vlistptr1->vars[varID].levinfo, nlevs1*sizeof(levinfo_t));
961
962
963
964
965

	  for ( levID = 0; levID < nlevs1; levID++ )
	    {
	      vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
	}

      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
983
984
985
	  /*
	  fprintf(stderr, "zaxis %d %d %d %d\n", zaxisID1, zaxisID2, nlevs1, nlevs2);
	  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
986
987
988
989
990
991
992
993
994
	  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
995
996
997
998
	  /*
	  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
999
1000
1001
1002
1003
1004
1005
1006
1007
	  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
1008
	  for ( varID2 = 0; varID2 < nvars2; varID2++ )
1009
	    if ( lvar[varID2] == FALSE && vlistptr2->vars[varID2].zaxisID == zaxisID2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
	      {
		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
1030
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1031
1032

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

@Result
1036
@func{vlistNvars} returns the number of variables in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1037
1038
1039
1040
1041

@EndFunction
*/
int vlistNvars(int vlistID)
{
1042
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1043
1044
1045

  vlistptr = vlist_to_pointer(vlistID);

1046
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1047
1048
1049
1050

  return (vlistptr->nvars);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1051

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1052
1053
1054
int vlistNrecs(int vlistID)
{
  int varID, nrecs = 0;
1055
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1056
1057
1058

  vlistptr = vlist_to_pointer(vlistID);

1059
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1060
1061
1062
1063
1064
1065
1066
1067

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

  return (nrecs);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
1068
1069
int vlistNumber(int vlistID)
{
1070
  int varID, number, number2, datatype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1071
1072
1073
1074
  vlist_t *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

1075
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1076

1077
1078
1079
1080
1081
1082
  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
1083
  for ( varID = 1; varID < vlistptr->nvars; varID++ )
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
    {
      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
1097
1098
1099
1100

  return (number);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1101
1102
1103
1104
1105
1106
/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

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

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

@Result
1113
@func{vlistNgrids} returns the number of grids in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1114
1115
1116
1117
1118

@EndFunction
*/
int vlistNgrids(int vlistID)
{
1119
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1120
1121
1122

  vlistptr = vlist_to_pointer(vlistID);

1123
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133

  return (vlistptr->ngrids);
}

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

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

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

@Result
1140
@func{vlistNzaxis} returns the number of zaxis in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1141
1142
1143
1144
1145

@EndFunction
*/
int vlistNzaxis(int vlistID)
{
1146
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1147
1148
1149

  vlistptr = vlist_to_pointer(vlistID);

1150
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1151
1152
1153
1154
1155
1156
1157

  return (vlistptr->nzaxis);
}


void vlistDefNtsteps(int vlistID, int nts)
{
1158
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1159
1160
1161

  vlistptr = vlist_to_pointer(vlistID);

1162
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1163
1164
1165
1166
1167
1168
1169

  vlistptr->ntsteps = nts;
}


int vlistNtsteps(int vlistID)
{
1170
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1171
1172
1173

  vlistptr = vlist_to_pointer(vlistID);

1174
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1175
1176
1177
1178
1179
1180
1181
1182

  return (vlistptr->ntsteps);
}


void vlistPrint(int vlistID)
{
  int nvars, flag, index;
1183
  int varID, fvarID, mvarID, flevID, mlevID, levID;
1184
  int param, gridID, zaxisID, timeID, nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1185
  int dtype;
1186
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1187
1188
  char *name, *longname, *units;
  double level;
1189
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1190
1191
1192

  vlistptr = vlist_to_pointer(vlistID);

1193
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210

  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 )
    {
1211
      printf(" varID param    gridID zaxisID timeID nlevel flag  name     longname\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1212
1213
      for ( varID = 0; varID < nvars; varID++ )
	{
1214
	  param    = vlistptr->vars[varID].param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1215
1216
1217
1218
1219
1220
1221
1222
	  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;
1223
1224
1225
1226

	  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
1227
1228
1229
1230
1231
1232
		 name ? name : "", longname ? longname : "");
	  if ( units ) printf("   [%s]", units);
	  printf("\n");
	}

      printf("\n");
1233
      printf(" varID  levID fvarID flevID mvarID mlevID  index  dtype  flag  level\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1234
1235
1236
1237
1238
      for ( varID = 0; varID < nvars; varID++ )
	{
	  nlevs    = vlistptr->vars[varID].nlevs;
	  zaxisID  = vlistptr->vars[varID].zaxisID;
	  fvarID   = vlistptr->vars[varID].fvarID;
1239
	  mvarID   = vlistptr->vars[varID].mvarID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1240
1241
1242
1243
	  dtype    = vlistptr->vars[varID].datatype;
	  for ( levID = 0; levID < nlevs; levID++ )
	    {
	      flevID = vlistptr->vars[varID].levinfo[levID].flevelID;
1244
	      mlevID = vlistptr->vars[varID].levinfo[levID].mlevelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1245
1246
1247
	      index  = vlistptr->vars[varID].levinfo[levID].index;
	      flag   = vlistptr->vars[varID].levinfo[levID].flag;
	      level  = zaxisInqLevel(zaxisID, levID);
1248
1249
	      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
1250
1251
1252
1253
1254
	    }
	}
    }
}

1255
1256
1257
1258
1259
1260
1261
/*
@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
1262
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
1263
1264
1265
1266
1267
1268

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

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1269
1270
void vlistDefTaxis(int vlistID, int taxisID)
{
1271
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1272
1273
1274

  vlistptr = vlist_to_pointer(vlistID);

1275
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1276
1277
1278
1279

  vlistptr->taxisID = taxisID;
}

1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
/*
@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
1296
1297
int vlistInqTaxis(int vlistID)
{
1298
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1299
1300
1301

  vlistptr = vlist_to_pointer(vlistID);

1302
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1303
1304
1305
1306

  return (vlistptr->taxisID);
}

1307

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1308
1309
void  vlistDefTable(int vlistID, int tableID)
{
1310
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1311
1312
1313

  vlistptr = vlist_to_pointer(vlistID);

1314
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1315
1316
1317
1318

  vlistptr->tableID = tableID;
}