vlist.c 37 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 "pio_util.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
11
12
13
14


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

15
16
17
18
19
static int *vlist_list_size;

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

static void vlist_initialize(void);

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

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

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

#else

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

#endif


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


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

55
56
static
void vlist_list_new(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
{
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  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
72
73
}

74
75
static
void vlist_list_delete(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
76
{
77
78
79
80
81
82
83
84
85
86
87
88
  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
89
90
}

91
92
static
void vlist_init_pointer(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
{
94
95
96
97
98
99
100
  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
101
    {
102
103
104
      _vlistList[j][i].idx  = i;
      _vlistList[j][i].next = i + 1;
      _vlistList[j][i].ptr  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
106
    }

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

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

118
  nsp = namespaceGetActive ();
119

120
  assert(_vlistList[nsp]);
121

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

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

136
137
138
139
      _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;
140
141
142
    }
  else
    Warning("Too many open vlists (limit is %d)!", _vlist_max);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
144
145
}


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

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152

153
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154

155
  if ( idx >= 0 && idx < vlist_list_size[nsp] )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
    {
157
      VLIST_LOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158

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

161
      VLIST_UNLOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
163
    }
  else
164
    Error("vlist index %d undefined!", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
167
168
169

  return (vlistptr);
}

/* Create an index from a pointer */
170
171
static
int vlist_from_pointer(vlist_t *ptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
172
{
173
174
175
  int idx = -1, nsp;

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
178

  if ( ptr )
    {
179
180
      VLIST_LOCK();

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

183
      if ( _vlistAvail[nsp] >= 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
	{
185
186
	  vlistPtrToIdx *newptr;

187
188
	  newptr       = &_vlistList[nsp][_vlistAvail[nsp]];
	  _vlistAvail[nsp]  = newptr->next;
189
	  newptr->next = -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
191
	  idx	       = newptr->idx;
	  newptr->ptr  = ptr;
192

Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
	  if ( VLIST_Debug )
194
	    Message("Pointer %p has idx %d from vlist list", ptr, idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
195
196
	}

197
      VLIST_UNLOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
198
199
    }
  else
200
    Error("Internal problem (pointer %p undefined)", ptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
202
203
204

  return (idx);
}

205
206
static
void vlist_init_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
209
210
211
212
213
214
{
  vlistptr->self           = vlist_from_pointer(vlistptr);

  vlistptr->nlock          = 0;
  vlistptr->nvars          = 0;
  vlistptr->vars           = NULL;
  vlistptr->ngrids         = 0;
  vlistptr->nzaxis         = 0;
215
  vlistptr->taxisID        = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
217
218
219
220
  vlistptr->instID         = cdiDefaultInstID;
  vlistptr->modelID        = cdiDefaultModelID;
  vlistptr->tableID        = cdiDefaultTableID;
  vlistptr->varsAllocated  = 0;
  vlistptr->ntsteps        = CDI_UNDEFID;
221
  vlistptr->atts.nalloc    = MAX_ATTRIBUTES;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
222
  vlistptr->atts.nelems    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
223
224
}

225
226
static
vlist_t *vlist_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227
{
228
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229

230
  vlistptr = (vlist_t *)xmalloc(sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
231
232
233
234
235
236

  if ( vlistptr ) vlist_init_entry(vlistptr);

  return (vlistptr);
}

237
238
static
void vlist_delete_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239
{
240
241
242
  int idx, nsp;

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
245

  idx = vlistptr->self;

246
  VLIST_LOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
248
249

  free(vlistptr);

250
251
252
  _vlistList[nsp][idx].next = _vlistAvail[nsp];
  _vlistList[nsp][idx].ptr  = 0;
  _vlistAvail[nsp]          = idx;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253

254
  VLIST_UNLOCK();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
256

  if ( VLIST_Debug )
257
    Message("Removed idx %d from vlist list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
258
259
}

260
261
static
void vlist_initialize(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
{
  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);

276
  VLIST_LOCK();
277

Uwe Schulzweida's avatar
Uwe Schulzweida committed
278
279
  vlist_init_pointer();

280
  VLIST_UNLOCK();
281

Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
283
284
  _vlist_init = TRUE;
}

285
286
static
void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
287
288
289
290
{
  int vlistID2;

  vlistID2 = vlistptr2->self;
291
  memcpy(vlistptr2, vlistptr1, sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292
  vlistptr2->atts.nelems = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
294
295
  vlistptr2->self = vlistID2;
}

296
297
static
void vlist_check_ptr(const char *caller, vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
299
{
  if ( vlistptr == NULL )
300
    Errorc("vlist undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
301
302
303
}

/*
304
@Function  vlistCreate
Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
306
@Title     Create a variable list

307
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
308

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

333
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
334
335

  vlistptr = vlist_new_entry();
336
  if ( ! vlistptr ) Error("No memory");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
337
338
339

  vlistID = vlistptr->self;

340
  if ( vlistID >= 0 ) vlistLock(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
342
343
344
345

  return (vlistID);
}

/*
346
@Function  vlistDestroy
347
@Title     Destroy a variable list
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348

349
@Prototype void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
350
@Parameter
351
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
352
353
354

@EndFunction
*/
355
void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
356
{
357
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
358
359
360

  vlistptr = vlist_to_pointer(vlistID);

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

  vlistUnlock(vlistID);

  if ( vlistptr->nlock > 0 )
    {
367
      Warning("vlist %d is locked!", vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
369
370
371
    }
  else
    {
      int nvars = vlistptr->nvars;
372
      int varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
373

374
      vlistDelAtts(vlistID, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
375
376

      for ( varID = 0; varID < nvars; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
377
	{
378
379
380
381
382
	  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
383
384
385

	  vlistDelAtts(vlistID, varID);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
386
387
388
389
390
391
392

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

      vlist_delete_entry(vlistptr);
    }
}

393

Uwe Schulzweida's avatar
Uwe Schulzweida committed
394
395
int vlistNlock(int vlistID)
{
396
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
397
398
399

  vlistptr = vlist_to_pointer(vlistID);

400
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
401
402
403
404

  return (vlistptr->nlock);
}

405

Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
407
void vlistLock(int vlistID)
{
408
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409
410
411

  vlistptr = vlist_to_pointer(vlistID);

412
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413
414

  vlistptr->nlock++;
415
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
416
417
}

418

Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
420
void vlistUnlock(int vlistID)
{
421
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
423
424

  vlistptr = vlist_to_pointer(vlistID);

425
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
426
427

  vlistptr->nlock--;
428
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
430
431
432
433
434
435
436
}

/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
437
438
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
439
440

@Description
441
The function @func{vlistCopy} copies all entries from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
443
444
445
446

@EndFunction
*/
void vlistCopy(int vlistID2, int vlistID1)
{
447
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
448
449
450
451

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

452
453
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
455
456

  vlist_copy(vlistptr2, vlistptr1);

457
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
458

Uwe Schulzweida's avatar
Uwe Schulzweida committed
459
460
461
462
463
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
      int nlevs, varID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
464
465
      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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

      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
482
	  vlistptr2->vars[varID].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
483
	  memcpy(vlistptr2->vars[varID].levinfo,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
484
		 vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486
487

	  vlistptr2->vars[varID].atts.nelems = 0;
	  vlistCopyVarAtts(vlistID1, varID, vlistID2, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
488
489
490
491
492
493
494
495
496
497
498
499
	}
    }

  vlistptr2->nlock = 0;
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

@Description
503
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
504
505

@Result
506
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
507
508
509
510
511
512

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  int vlistIDnew;
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
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
521
522
523
524
525
526
527
528
529

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


void vlistClearFlag(int vlistID)
{
  int varID, levID;
530
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
532
533
534
535
536
537
538
539
540
541
542
543
544

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


545
int vlistInqZaxis(int vlistID, int zaxistype, int nlevels, double *levels, int lbounds, double *levels2,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
546
547
548
549
550
551
552
		  int vctsize, const double *vct)
{
  int zaxisdefined;
  int nzaxis;
  int zaxisID = CDI_UNDEFID;
  int index;
  int zaxisglobdefined = 0;
553
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
554
555
556

  vlistptr = vlist_to_pointer(vlistID);

557
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
558
559
560
561
562
563
564
565

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

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

566
      if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
567
568
569
570
571
572
573
574
575
576
	{
	  zaxisdefined = 1;
	  break;
	}
    }

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
      for ( zaxisID = 0; zaxisID < nzaxis; zaxisID++ )
577
	if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
578
579
580
581
582
583
584
585
586
587
	  {
	    zaxisglobdefined = 1;
	    break;
	  }
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
	{
588
	  zaxisID = zaxisCreate(zaxistype, nlevels);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
589
590
591
592
593
594
595
	  zaxisDefLevels(zaxisID, levels);
	  if ( lbounds )
	    {
	      zaxisDefLbounds(zaxisID, levels);
	      zaxisDefUbounds(zaxisID, levels2);
	    }

596
	  if ( zaxistype == ZAXIS_HYBRID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
597
598
599
600
	    {
	      if ( vctsize > 0 )
		zaxisDefVct(zaxisID, vctsize, vct);
	      else
601
		Warning("VCT missing");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
	    }
	}

      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
619
620
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
621
622

@Description
623
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
624
625
626
627
628

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
629
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
630
631
632
633

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

634
635
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
636
637
638

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

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

      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
669
	    memcpy(&vlistptr2->vars[varID2], &vlistptr1->vars[varID], sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
670
671
672
673

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

674
675
	    vlistptr2->vars[varID2].mvarID = varID2;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
	    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
693
	    vlistptr2->vars[varID2].levinfo = (levinfo_t *) malloc(nlevs2*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
694

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

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

		zaxisType = zaxisInqType(zaxisID);

		if ( zaxisType == ZAXIS_HYBRID )
		  {
		    nvct = zaxisInqVctSize(zaxisID);
		    vct  = zaxisInqVctPtr(zaxisID);
		  }
726
727
		lbounds  = 0;     /* <------------ have to define */
		levels2  = NULL;  /* <------------ have to define */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
728
729
		zaxisID2 = vlistInqZaxis(vlistID2, zaxisType, nlevs2, levels, lbounds, levels2, nvct, vct);
		free(levels);
730
731
732
733
734
735
736
737
738
739
740

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

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

764
	    if ( index == vlistptr2->ngrids )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
765
766
	      {
		vlistptr2->gridIDs[vlistptr2->ngrids++] = gridID;
767
		if (vlistptr2->ngrids >= MAX_GRIDS_PS )
768
		  Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
769
770
	      }

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

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

	    varID2++;
	  }
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

@Prototype void vlistCat(int vlistID2, int vlistID1)
@Parameter
792
793
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
794
795
796
797
798
799
800
801
802
803
804

@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;
805
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
806
807
808
809

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

810
811
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
812
813
814
815
816
817

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
818
819
  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
820
821
822
823
824
825
826

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

827
828
829
      vlistptr1->vars[varID].mvarID = varID2;
      vlistptr2->vars[varID2].mvarID = varID;

830
831
832
833
834
835
836
837
      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
838
839
840
841
842
843
844
845
846
847
848
849
850
      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
851
852
      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
853

854
855
856
      vlistptr2->vars[varID2].atts.nelems = 0;
      vlistCopyVarAtts(vlistID1, varID, vlistID2, varID2);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
857
858
859
860
861
862
863
      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;
864
	  if ( vlistptr2->ngrids >= MAX_GRIDS_PS )
865
	    Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
866
867
868
869
870
871
872
873
874
	}

      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;
875
876
	  if ( vlistptr2->nzaxis >= MAX_ZAXES_PS )
	    Error("Internal Problem! More than %d zaxis.", MAX_ZAXES_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
877
878
879
880
881
882
883
884
885
886
	}
    }
}

/*
@Function  vlistMerge
@Title     Merge two variable lists

@Prototype void vlistMerge(int vlistID2, int vlistID1)
@Parameter
887
888
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
889
890

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
891
Merge the variable list vlistID1 to the variable list vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
892
893
894
895
896
897
898
899
900
901
902

@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;
903
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
904
905
906
907

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

908
909
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924

  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
	    {
925
	      if ( vlistptr1->vars[varID].param != vlistptr2->vars[varID].param )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
926
927
928
929
930
931
932
933
934
935
936
937
		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;

938
939
940
	  vlistptr1->vars[varID].mvarID = varID;
	  vlistptr2->vars[varID].mvarID = varID;

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
953
	  memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
954
		 vlistptr1->vars[varID].levinfo, nlevs1*sizeof(levinfo_t));
955
956
957
958
959

	  for ( levID = 0; levID < nlevs1; levID++ )
	    {
	      vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
	}

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

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

@Result
1030
@func{vlistNvars} returns the number of variables in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1031
1032
1033
1034
1035

@EndFunction
*/
int vlistNvars(int vlistID)
{
1036
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1037
1038
1039

  vlistptr = vlist_to_pointer(vlistID);

1040
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1041
1042
1043
1044

  return (vlistptr->nvars);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1045

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1046
1047
1048
int vlistNrecs(int vlistID)
{
  int varID, nrecs = 0;
1049
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1050
1051
1052

  vlistptr = vlist_to_pointer(vlistID);

1053
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1054
1055
1056
1057
1058
1059
1060
1061

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

  return (nrecs);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
1062
1063
int vlistNumber(int vlistID)
{
1064
  int varID, number, number2, datatype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1065
1066
1067
1068
  vlist_t *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

1069
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1070

1071
1072
1073
1074
1075
1076
  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
1077
  for ( varID = 1; varID < vlistptr->nvars; varID++ )
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
    {
      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
1091
1092
1093
1094

  return (number);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1095
1096
1097
1098
1099
1100
/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

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

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

@Result
1107
@func{vlistNgrids} returns the number of grids in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1108
1109
1110
1111
1112

@EndFunction
*/
int vlistNgrids(int vlistID)
{
1113
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1114
1115
1116

  vlistptr = vlist_to_pointer(vlistID);

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

  return (vlistptr->ngrids);
}

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

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

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

@Result
1134
@func{vlistNzaxis} returns the number of zaxis in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1135
1136
1137
1138
1139

@EndFunction
*/
int vlistNzaxis(int vlistID)
{
1140
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1141
1142
1143

  vlistptr = vlist_to_pointer(vlistID);

1144
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1145
1146
1147
1148
1149
1150
1151

  return (vlistptr->nzaxis);
}


void vlistDefNtsteps(int vlistID, int nts)
{
1152
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1153
1154
1155

  vlistptr = vlist_to_pointer(vlistID);

1156
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1157
1158
1159
1160
1161
1162
1163

  vlistptr->ntsteps = nts;
}


int vlistNtsteps(int vlistID)
{
1164
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1165
1166
1167

  vlistptr = vlist_to_pointer(vlistID);

1168
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1169
1170
1171
1172
1173
1174
1175
1176

  return (vlistptr->ntsteps);
}


void vlistPrint(int vlistID)
{
  int nvars, flag, index;
1177
  int varID, fvarID, mvarID, flevID, mlevID, levID;
1178
  int param, gridID, zaxisID, timeID, nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1179
  int dtype;
1180
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1181
1182
  char *name, *longname, *units;
  double level;
1183
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1184
1185
1186

  vlistptr = vlist_to_pointer(vlistID);

1187
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204

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

	  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
1221
1222
1223
1224
1225
1226
		 name ? name : "", longname ? longname : "");
	  if ( units ) printf("   [%s]", units);
	  printf("\n");
	}

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

1249
1250
1251
1252
1253
1254
1255
/*
@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
1256
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
1257
1258
1259
1260
1261
1262

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

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1263
1264
void vlistDefTaxis(int vlistID, int taxisID)
{
1265
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1266
1267
1268

  vlistptr = vlist_to_pointer(vlistID);

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

  vlistptr->taxisID = taxisID;
}

1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
/*
@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
1290
1291
int vlistInqTaxis(int vlistID)
{
1292
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1293
1294
1295

  vlistptr = vlist_to_pointer(vlistID);

1296
  vlist_check_ptr(__func__, vlistptr);