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

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


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

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

static void vlist_initialize(void);

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

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

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

#else

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

#endif


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


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

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

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

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

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

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

  _vlistAvail = 0;
}

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

  assert(_vlistList != NULL);

  nvlists = _vlist_min + MIN_VLISTS;

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

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


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

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

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

      vlistptr = _vlistList[idx].ptr;

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

  return (vlistptr);
}

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

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

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

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

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

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

  return (idx);
}

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

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

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

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

  if ( vlistptr ) vlist_init_entry(vlistptr);

  return (vlistptr);
}

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

  idx = vlistptr->self;

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

  free(vlistptr);

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

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

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

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

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

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

  vlist_list_new();
  atexit(vlist_list_delete);

236
  VLIST_LOCK();
237

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

240
  VLIST_UNLOCK();
241

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

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

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

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

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

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

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

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

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

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

  vlistID = vlistptr->self;

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

  return (vlistID);
}

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

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

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

  vlistptr = vlist_to_pointer(vlistID);

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

  vlistUnlock(vlistID);

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

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

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

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

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

      vlist_delete_entry(vlistptr);
    }
}

353

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

  vlistptr = vlist_to_pointer(vlistID);

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

  return (vlistptr->nlock);
}

365

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

  vlistptr = vlist_to_pointer(vlistID);

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

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

378

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

  vlistptr = vlist_to_pointer(vlistID);

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

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

/*
@Function  vlistCopy
@Title     Copy a variable list

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

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

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

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

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

  vlist_copy(vlistptr2, vlistptr1);

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

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

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

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

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

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

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

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

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

  vlistptr2->nlock = 0;
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

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

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

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

  vlistptr = vlist_to_pointer(vlistID);

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

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

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


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

  vlistptr = vlist_to_pointer(vlistID);

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


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

  vlistptr = vlist_to_pointer(vlistID);

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

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

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

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

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

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

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

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

  return (zaxisID);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		zaxisType = zaxisInqType(zaxisID);

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

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

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

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

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

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

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

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

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

	    varID2++;
	  }
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/*
@Function  vlistMerge
@Title     Merge two variable lists

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

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

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

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

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

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

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

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

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

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

	  nlevs = nlevs1 + nlevs2;

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

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

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

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

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

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

	  zaxisID = zaxisDuplicate(zaxisID2);

	  zaxisResize(zaxisID, nlevs);

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

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

	  free(levels);

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

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

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

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

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

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

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

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

  vlistptr = vlist_to_pointer(vlistID);

1000
  vlist_check_ptr(__func__, vlistptr);
For faster browsing, not all history is shown. View entire blame