vlist.c 33.8 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"
12
#include "resource_handle.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
13

14
static int VLIST_Debug = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
15
16
17
18
19
20

static void vlist_initialize(void);

#if  defined  (HAVE_LIBPTHREAD)
#  include <pthread.h>

21
static pthread_once_t  _vlist_init_thread = PTHREAD_ONCE_INIT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
22
23
static pthread_mutex_t _vlist_mutex;

24
#  define VLIST_INIT()        \
25
  pthread_once(&_vlist_init_thread, vlist_initialize)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
27
28

#else

29
static int vlistIsInitialized = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
30

31
32
#  define VLIST_INIT()               \
  if ( _vlist_init == FALSE ) vlist_initialize()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33

34
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36


37
38
39
/* FIXME: implementation incomplete, fix once leaf nodes are complete */
static int
vlist_compare(vlist_t *a, vlist_t *b)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
40
{
41
42
43
44
45
46
47
  int diff;
  diff = (a->nvars != b->nvars) || (a->ngrids != b->ngrids)
    || (a->nzaxis != b->nzaxis) || (a->instID != b->instID)
    || (a->modelID != b->modelID) || (a->tableID != b->tableID)
    || (a->varsAllocated != b->varsAllocated)
    || (a->ntsteps != b->ntsteps);
  return diff;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
49
}

50
51
52
53
resOps vlist_ops = {
  (valCompareFunc)vlist_compare,
  free
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
55


56
vlist_t *vlist_to_pointer(int code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
{
58
  VLIST_INIT();
59
  return reshGetVal(code);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60
61
}

62
63
static
void vlist_init_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
{
65
  vlistptr->self           = reshPut(vlistptr, &vlist_ops);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
67
68
69
70
71

  vlistptr->nlock          = 0;
  vlistptr->nvars          = 0;
  vlistptr->vars           = NULL;
  vlistptr->ngrids         = 0;
  vlistptr->nzaxis         = 0;
72
  vlistptr->taxisID        = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
74
75
76
77
  vlistptr->instID         = cdiDefaultInstID;
  vlistptr->modelID        = cdiDefaultModelID;
  vlistptr->tableID        = cdiDefaultTableID;
  vlistptr->varsAllocated  = 0;
  vlistptr->ntsteps        = CDI_UNDEFID;
78
  vlistptr->atts.nalloc    = MAX_ATTRIBUTES;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
  vlistptr->atts.nelems    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
81
}

82
83
static
vlist_t *vlist_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84
{
85
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86

87
  vlistptr = (vlist_t *)xmalloc(sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88

89
  vlist_init_entry(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
91
92
93

  return (vlistptr);
}

94
95
static
void vlist_delete_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
{
97
98
99
  int idx, nsp;

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
101
102

  idx = vlistptr->self;

103
  reshRemove(idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
106
107

  free(vlistptr);

  if ( VLIST_Debug )
108
    Message("Removed idx %d from vlist list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
109
110
}

111
112
static
void vlist_initialize(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
114
115
116
117
118
119
120
{
  char *env;

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

}

121
122
static
void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
125
126
{
  int vlistID2;

  vlistID2 = vlistptr2->self;
127
  memcpy(vlistptr2, vlistptr1, sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
128
  vlistptr2->atts.nelems = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
130
131
  vlistptr2->self = vlistID2;
}

132
133
static
void vlist_check_ptr(const char *caller, vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
{
  if ( vlistptr == NULL )
136
    Errorc("vlist undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
}

/*
140
@Function  vlistCreate
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
@Title     Create a variable list

143
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144

Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
@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
162
163
@EndFunction
*/
164
int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
{
  int vlistID = 0;
167
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168

169
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
172
173
174

  vlistptr = vlist_new_entry();

  vlistID = vlistptr->self;

175
  if ( vlistID >= 0 ) vlistLock(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
178
179
180

  return (vlistID);
}

/*
181
@Function  vlistDestroy
182
@Title     Destroy a variable list
Uwe Schulzweida's avatar
Uwe Schulzweida committed
183

184
@Prototype void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
185
@Parameter
186
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
187
188
189

@EndFunction
*/
190
void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
{
192
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
194
195

  vlistptr = vlist_to_pointer(vlistID);

196
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
198
199
200
201

  vlistUnlock(vlistID);

  if ( vlistptr->nlock > 0 )
    {
202
      Warning("vlist %d is locked!", vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
204
205
206
    }
  else
    {
      int nvars = vlistptr->nvars;
207
      int varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
208

209
      vlistDelAtts(vlistID, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210
211

      for ( varID = 0; varID < nvars; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
	{
213
214
215
216
217
	  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
218
219
220

	  vlistDelAtts(vlistID, varID);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
222
223
224
225
226
227

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

      vlist_delete_entry(vlistptr);
    }
}

228

Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
230
int vlistNlock(int vlistID)
{
231
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
234

  vlistptr = vlist_to_pointer(vlistID);

235
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236
237
238
239

  return (vlistptr->nlock);
}

240

Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
242
void vlistLock(int vlistID)
{
243
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
245
246

  vlistptr = vlist_to_pointer(vlistID);

247
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
249

  vlistptr->nlock++;
250
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
252
}

253

Uwe Schulzweida's avatar
Uwe Schulzweida committed
254
255
void vlistUnlock(int vlistID)
{
256
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
257
258
259

  vlistptr = vlist_to_pointer(vlistID);

260
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
261
262

  vlistptr->nlock--;
263
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
265
266
267
268
269
270
271
}

/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
272
273
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
274
275

@Description
276
The function @func{vlistCopy} copies all entries from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
277
278
279
280
281

@EndFunction
*/
void vlistCopy(int vlistID2, int vlistID1)
{
282
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
283
284
285
286

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

287
288
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
290
291

  vlist_copy(vlistptr2, vlistptr1);

292
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
293

Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
295
296
297
298
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
      int nlevs, varID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
299
300
      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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316

      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
317
	  vlistptr2->vars[varID].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
	  memcpy(vlistptr2->vars[varID].levinfo,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
319
		 vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
320
321
322

	  vlistptr2->vars[varID].atts.nelems = 0;
	  vlistCopyVarAtts(vlistID1, varID, vlistID2, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
324
325
326
327
328
329
330
331
332
333
334
	}
    }

  vlistptr2->nlock = 0;
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

@Description
338
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
339
340

@Result
341
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
342
343
344
345
346
347

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  int vlistIDnew;
348
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
350
351

  vlistptr = vlist_to_pointer(vlistID);

352
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353

354
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
356
357
358
359
360
361
362
363
364

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


void vlistClearFlag(int vlistID)
{
  int varID, levID;
365
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
367
368
369
370
371
372
373
374
375
376
377
378
379

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


380
int vlistInqZaxis(int vlistID, int zaxistype, int nlevels, double *levels, int lbounds, double *levels2,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
382
383
384
385
386
387
		  int vctsize, const double *vct)
{
  int zaxisdefined;
  int nzaxis;
  int zaxisID = CDI_UNDEFID;
  int index;
  int zaxisglobdefined = 0;
388
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389
390
391

  vlistptr = vlist_to_pointer(vlistID);

392
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
394
395
396
397
398
399
400

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

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

401
      if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
403
404
405
406
407
408
409
410
411
	{
	  zaxisdefined = 1;
	  break;
	}
    }

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
      for ( zaxisID = 0; zaxisID < nzaxis; zaxisID++ )
412
	if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413
414
415
416
417
418
419
420
421
422
	  {
	    zaxisglobdefined = 1;
	    break;
	  }
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
	{
423
	  zaxisID = zaxisCreate(zaxistype, nlevels);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
424
425
426
427
428
429
430
	  zaxisDefLevels(zaxisID, levels);
	  if ( lbounds )
	    {
	      zaxisDefLbounds(zaxisID, levels);
	      zaxisDefUbounds(zaxisID, levels2);
	    }

431
	  if ( zaxistype == ZAXIS_HYBRID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
432
433
434
435
	    {
	      if ( vctsize > 0 )
		zaxisDefVct(zaxisID, vctsize, vct);
	      else
436
		Warning("VCT missing");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
	    }
	}

      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
454
455
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
456
457

@Description
458
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
459
460
461
462
463

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
464
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
465
466
467
468

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

469
470
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
471
472
473

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
  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
491
      if ( nvars2 > 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
492
	vlistptr2->vars  = (var_t *) malloc(nvars2*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
493
494
      else
	vlistptr2->vars  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
495
496
497
498
499
500
501
502
503

      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
504
	    memcpy(&vlistptr2->vars[varID2], &vlistptr1->vars[varID], sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
505
506
507
508

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

509
510
	    vlistptr2->vars[varID2].mvarID = varID2;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
	    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
528
	    vlistptr2->vars[varID2].levinfo = (levinfo_t *) malloc(nlevs2*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
529

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
	    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;
550
		      vlistptr1->vars[varID].levinfo[levID].mlevelID = levID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
552
553
554
555
556
557
558
559
560
		      levels[levID2++] = zaxisInqLevel(zaxisID, levID);
		    }

		zaxisType = zaxisInqType(zaxisID);

		if ( zaxisType == ZAXIS_HYBRID )
		  {
		    nvct = zaxisInqVctSize(zaxisID);
		    vct  = zaxisInqVctPtr(zaxisID);
		  }
561
562
		lbounds  = 0;     /* <------------ have to define */
		levels2  = NULL;  /* <------------ have to define */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
563
564
		zaxisID2 = vlistInqZaxis(vlistID2, zaxisType, nlevs2, levels, lbounds, levels2, nvct, vct);
		free(levels);
565
566
567
568
569
570
571
572
573
574
575

		{
		  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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
		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 )
590
591
592
593
594
		{
		  vlistptr2->vars[varID2].levinfo[levID2].flevelID = levID;
		  vlistptr2->vars[varID2].levinfo[levID2].mlevelID = levID;
		  levID2++;
		}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
595
596
597
598

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

599
	    if ( index == vlistptr2->ngrids )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600
601
	      {
		vlistptr2->gridIDs[vlistptr2->ngrids++] = gridID;
602
		if (vlistptr2->ngrids >= MAX_GRIDS_PS )
603
		  Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
604
605
	      }

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

609
	    if ( index == vlistptr2->nzaxis )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
610
611
	      {
		vlistptr2->zaxisIDs[vlistptr2->nzaxis++] = zaxisID;
612
613
		if (vlistptr2->nzaxis >= MAX_ZAXES_PS )
		  Error("Internal Problem! More than %d zaxis.", MAX_ZAXES_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
614
615
616
617
618
619
620
621
622
623
624
625
626
	      }

	    varID2++;
	  }
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

@Prototype void vlistCat(int vlistID2, int vlistID1)
@Parameter
627
628
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
629
630
631
632
633
634
635
636
637
638
639

@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;
640
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
641
642
643
644

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

645
646
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
647
648
649
650
651
652

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
653
654
  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
655
656
657
658
659
660
661

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

662
663
664
      vlistptr1->vars[varID].mvarID = varID2;
      vlistptr2->vars[varID2].mvarID = varID;

665
666
667
668
669
670
671
672
      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
673
674
675
676
677
678
679
680
681
682
683
684
685
      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
686
687
      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
688

689
690
691
      vlistptr2->vars[varID2].atts.nelems = 0;
      vlistCopyVarAtts(vlistID1, varID, vlistID2, varID2);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
692
693
694
695
696
697
698
      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;
699
	  if ( vlistptr2->ngrids >= MAX_GRIDS_PS )
700
	    Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
701
702
703
704
705
706
707
708
709
	}

      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;
710
711
	  if ( vlistptr2->nzaxis >= MAX_ZAXES_PS )
	    Error("Internal Problem! More than %d zaxis.", MAX_ZAXES_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
712
713
714
715
716
717
718
719
720
721
	}
    }
}

/*
@Function  vlistMerge
@Title     Merge two variable lists

@Prototype void vlistMerge(int vlistID2, int vlistID1)
@Parameter
722
723
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724
725

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
726
Merge the variable list vlistID1 to the variable list vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
727
728
729
730
731
732
733
734
735
736
737

@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;
738
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
739
740
741
742

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

743
744
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759

  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
	    {
760
	      if ( vlistptr1->vars[varID].param != vlistptr2->vars[varID].param )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
761
762
763
764
765
766
767
768
769
770
771
772
		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;

773
774
775
	  vlistptr1->vars[varID].mvarID = varID;
	  vlistptr2->vars[varID].mvarID = varID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
776
777
778
779
780
781
	  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
782
	  /*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
783
	  fprintf(stderr, "var %d %d %d %d %d\n", varID, nlevs1, nlevs2, nlevs, sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
784
	  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
785
	  vlistptr2->vars[varID].levinfo =
Uwe Schulzweida's avatar
Uwe Schulzweida committed
786
	    (levinfo_t *) realloc(vlistptr2->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
787

Uwe Schulzweida's avatar
Uwe Schulzweida committed
788
	  memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
789
		 vlistptr1->vars[varID].levinfo, nlevs1*sizeof(levinfo_t));
790
791
792
793
794

	  for ( levID = 0; levID < nlevs1; levID++ )
	    {
	      vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
	}

      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
812
813
814
	  /*
	  fprintf(stderr, "zaxis %d %d %d %d\n", zaxisID1, zaxisID2, nlevs1, nlevs2);
	  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
815
816
817
818
819
820
821
822
823
	  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
824
825
826
827
	  /*
	  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
828
829
830
831
832
833
834
835
836
	  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
837
	  for ( varID2 = 0; varID2 < nvars2; varID2++ )
838
	    if ( lvar[varID2] == FALSE && vlistptr2->vars[varID2].zaxisID == zaxisID2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
	      {
		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
859
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
860
861

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

@Result
865
@func{vlistNvars} returns the number of variables in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
866
867
868
869
870

@EndFunction
*/
int vlistNvars(int vlistID)
{
871
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
872
873
874

  vlistptr = vlist_to_pointer(vlistID);

875
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
876
877
878
879

  return (vlistptr->nvars);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
880

Uwe Schulzweida's avatar
Uwe Schulzweida committed
881
882
883
int vlistNrecs(int vlistID)
{
  int varID, nrecs = 0;
884
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
885
886
887

  vlistptr = vlist_to_pointer(vlistID);

888
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
889
890
891
892
893
894
895
896

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

  return (nrecs);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
897
898
int vlistNumber(int vlistID)
{
899
  int varID, number, number2, datatype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
900
901
902
903
  vlist_t *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

904
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
905

906
907
908
909
910
911
  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
912
  for ( varID = 1; varID < vlistptr->nvars; varID++ )
913
914
915
916
917
918
919
920
921
922
923
924
925
    {
      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
926
927
928
929

  return (number);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
930
931
932
933
934
935
/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

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

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

@Result
942
@func{vlistNgrids} returns the number of grids in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
943
944
945
946
947

@EndFunction
*/
int vlistNgrids(int vlistID)
{
948
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
949
950
951

  vlistptr = vlist_to_pointer(vlistID);

952
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
953
954
955
956
957
958
959
960
961
962

  return (vlistptr->ngrids);
}

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

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

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

@Result
969
@func{vlistNzaxis} returns the number of zaxis in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
970
971
972
973
974

@EndFunction
*/
int vlistNzaxis(int vlistID)
{
975
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
976
977
978

  vlistptr = vlist_to_pointer(vlistID);

979
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
980
981
982
983
984
985
986

  return (vlistptr->nzaxis);
}


void vlistDefNtsteps(int vlistID, int nts)
{
987
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
988
989
990

  vlistptr = vlist_to_pointer(vlistID);

991
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
992
993
994
995
996
997
998

  vlistptr->ntsteps = nts;
}


int vlistNtsteps(int vlistID)
{
999
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1000
1001
1002

  vlistptr = vlist_to_pointer(vlistID);

1003
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1004
1005
1006
1007
1008
1009
1010
1011

  return (vlistptr->ntsteps);
}


void vlistPrint(int vlistID)
{
  int nvars, flag, index;
1012
  int varID, fvarID, mvarID, flevID, mlevID, levID;
1013
  int param, gridID, zaxisID, timeID, nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1014
  int dtype;
1015
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1016
1017
  char *name, *longname, *units;
  double level;
1018
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1019
1020
1021

  vlistptr = vlist_to_pointer(vlistID);

1022
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039

  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 )
    {
1040
      printf(" varID param    gridID zaxisID timeID nlevel flag  name     longname\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1041
1042
      for ( varID = 0; varID < nvars; varID++ )
	{
1043
	  param    = vlistptr->vars[varID].param;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1044
1045
1046
1047
1048
1049
1050
1051
	  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;
1052
1053
1054
1055

	  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
1056
1057
1058
1059
1060
1061
		 name ? name : "", longname ? longname : "");
	  if ( units ) printf("   [%s]", units);
	  printf("\n");
	}

      printf("\n");
1062
      printf(" varID  levID fvarID flevID mvarID mlevID  index  dtype  flag  level\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1063
1064
1065
1066
1067
      for ( varID = 0; varID < nvars; varID++ )
	{
	  nlevs    = vlistptr->vars[varID].nlevs;
	  zaxisID  = vlistptr->vars[varID].zaxisID;
	  fvarID   = vlistptr->vars[varID].fvarID;
1068
	  mvarID   = vlistptr->vars[varID].mvarID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1069
1070
1071
1072
	  dtype    = vlistptr->vars[varID].datatype;
	  for ( levID = 0; levID < nlevs; levID++ )
	    {
	      flevID = vlistptr->vars[varID].levinfo[levID].flevelID;
1073
	      mlevID = vlistptr->vars[varID].levinfo[levID].mlevelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1074
1075
1076
	      index  = vlistptr->vars[varID].levinfo[levID].index;
	      flag   = vlistptr->vars[varID].levinfo[levID].flag;
	      level  = zaxisInqLevel(zaxisID, levID);
1077
1078
	      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
1079
1080
1081
1082
1083
	    }
	}
    }
}

1084
1085
1086
1087
1088
1089
1090
/*
@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
1091
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
1092
1093
1094
1095
1096
1097

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

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1098
1099
void vlistDefTaxis(int vlistID, int taxisID)
{
1100
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1101
1102
1103

  vlistptr = vlist_to_pointer(vlistID);

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

  vlistptr->taxisID = taxisID;
}

1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
/*
@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
1125
1126
int vlistInqTaxis(int vlistID)
{
1127
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1128
1129
1130

  vlistptr = vlist_to_pointer(vlistID);

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

  return (vlistptr->taxisID);
}

1136

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1137
1138
void  vlistDefTable(int vlistID, int tableID)
{
1139
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1140
1141
1142

  vlistptr = vlist_to_pointer(vlistID);

1143
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1144
1145
1146
1147

  vlistptr->tableID = tableID;
}

1148

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1149
1150
int vlistInqTable(int vlistID)
{
1151
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1152
1153
1154

  vlistptr = vlist_to_pointer(vlistID);

1155
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1156
1157
1158
1159

  return (vlistptr->tableID);
}

1160

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1161
1162
void vlistDefInstitut(int vlistID, int instID)
{
1163
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1164
1165
1166

  vlistptr = vlist_to_pointer(vlistID);

1167
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1168
1169
1170
1171

  vlistptr->instID = instID;
}

1172

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1173
1174
1175
int vlistInqInstitut(int vlistID)
{
  int varID, instID;
1176
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1177
1178
1179

  vlistptr = vlist_to_pointer(vlistID);

1180
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200

  instID = vlistptr->instID;

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

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

  return (instID);
}

1201

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1202
1203
void vlistDefModel(int vlistID, int modelID)
{
1204
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1205
1206
1207

  vlistptr = vlist_to_pointer(vlistID);

1208
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1209
1210
1211
1212

  vlistptr->modelID = modelID;
}

1213

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1214
1215
1216
int vlistInqModel(int vlistID)
{
  int varID, modelID;
1217
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1218
1219
1220

  vlistptr = vlist_to_pointer(vlistID);

1221
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241

  modelID = vlistptr->modelID;

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

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

      vlistDefModel(vlistID, modelID);
    }

  return (modelID);
}

1242

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1243
1244
1245
1246
int vlistGridsizeMax(int vlistID)
{
  int gridsize, gridsizemax = 0;
  int gridID, index;
1247
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1248
1249
1250

  vlistptr = vlist_to_pointer(vlistID);

1251
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262

  for ( index = 0 ; index < vlistptr->ngrids ; index++ )
    {
      gridID = vlistptr->gridIDs[index];
      gridsize = gridInqSize(gridID);
      if ( gridsize > gridsizemax ) gridsizemax = gridsize;
    }
  
  return (gridsizemax);
}

1263

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1264
1265
1266
int vlistGrid(int vlistID, int index)
{
  int gridID = CDI_UNDEFID;
1267
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1268
1269
1270

  vlistptr = vlist_to_pointer(vlistID);

1271
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1272
1273
1274
1275
1276
1277
1278

  if ( index < vlistptr->ngrids && index >= 0 )
    gridID = vlistptr->gridIDs[index];
  
  return (gridID);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1279

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1280
1281
1282
int vlistGridIndex(int vlistID, int gridID)
{
  int index;
1283
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1284
1285
1286

  vlistptr = vlist_to_pointer(vlistID);

1287
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1288
1289
1290
1291
1292
1293
1294
1295
1296

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

  if ( index == vlistptr->ngrids ) index = -1;
  
  return (index);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1297

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1298
1299
1300
1301
void vlistChangeGridIndex(int vlistID, int index, int gridID)
{
  int gridIDold;
  int varID, nvars;
1302
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1303
1304
1305

  vlistptr = vlist_to_pointer(vlistID);

1306
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316

  gridIDold = vlistptr->gridIDs[index];
  vlistptr->gridIDs[index] = gridID;

  nvars = vlistptr->nvars;
  for ( varID = 0; varID < nvars; varID++ )
    if ( vlistptr->vars[varID].gridID == gridIDold )
      vlistptr->vars[varID].gridID = gridID;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1317

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1318
1319
1320
1321
void vlistChangeGrid(int vlistID, int gridID1, int gridID2)
{
  int varID, nvars;
  int index, ngrids;
1322
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1323
1324
1325

  vlistptr = vlist_to_pointer(vlistID);

1326
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343

  ngrids = vlistptr->ngrids;
  for ( index = 0; index < ngrids; index++ )
    {
      if ( vlistptr->gridIDs[index] == gridID1 )
	{
	  vlistptr->gridIDs[index] = gridID2;
	  break;
	}
    }

  nvars = vlistptr->nvars;
  for ( varID = 0; varID < nvars; varID++ )
    if ( vlistptr->vars[varID].gridID == gridID1 )
      vlistptr->vars[varID].gridID = gridID2;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1344

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1345
1346
1347
int vlistZaxis(int vlistID, int index)
{
  int zaxisID = CDI_UNDEFID;
1348
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1349
1350
1351

  vlistptr = vlist_to_pointer(vlistID);

1352
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362

  if ( index < vlistptr->nzaxis && index >= 0 )
    zaxisID = vlistptr->zaxisIDs[index];
  
  return (zaxisID);
}

int vlistZaxisIndex(int vlistID, int zaxisID)
{
  int index;
Uwe Schulzweida's avatar