vlist.c 34.7 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_max = MAX_VLISTS;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

static void vlist_initialize(void);

static int _vlist_init = FALSE;

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

static pthread_once_t _vlist_init_thread = PTHREAD_ONCE_INIT;
static pthread_mutex_t _vlist_mutex;

#  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);

#else

#  define VLIST_LOCK
#  define VLIST_UNLOCK
#  define VLIST_INIT                               \
   if ( _vlist_init == FALSE ) vlist_initialize();

#endif


typedef struct _vlistPtrToIdx {
  int idx;
  VLIST *ptr;
  struct _vlistPtrToIdx *next;
} vlistPtrToIdx;


Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
49
static vlistPtrToIdx *_vlistList  = NULL;
static vlistPtrToIdx *_vlistAvail = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
50
51
52
53
54
55


static void vlist_list_new(void)
{
  static char func[] = "vlist_list_new";

Uwe Schulzweida's avatar
Uwe Schulzweida committed
56
57
  assert(_vlistList == NULL);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  _vlistList = (vlistPtrToIdx *) malloc(_vlist_max*sizeof(vlistPtrToIdx));
}


static void vlist_list_delete(void)
{
  static char func[] = "vlist_list_delete";

  if ( _vlistList ) free(_vlistList);
}


static void vlist_init_pointer(void)
{
  int  i;
  
  for ( i = 0; i < _vlist_max; i++ )
    {
      _vlistList[i].next = _vlistList + i + 1;
      _vlistList[i].idx  = i;
      _vlistList[i].ptr  = 0;
    }

  _vlistList[_vlist_max-1].next = 0;

  _vlistAvail = _vlistList;
}


VLIST *vlist_to_pointer(int idx)
{
  static char func[] = "vlist_to_pointer";
  VLIST *vlistptr = NULL;

  VLIST_INIT

  if ( idx >= 0 && idx < _vlist_max )
    {
      VLIST_LOCK

      vlistptr = _vlistList[idx].ptr;

      VLIST_UNLOCK
    }
  else
    Error(func, "vlist index %d undefined!", idx);

  return (vlistptr);
}


/* Create an index from a pointer */
static int vlist_from_pointer(VLIST *ptr)
{
  static char func[] = "vlist_from_pointer";
  int      idx = -1;
  vlistPtrToIdx *newptr;

  if ( ptr )
    {
      VLIST_LOCK

      if ( _vlistAvail )
	{
	  newptr       = _vlistAvail;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
	  _vlistAvail  = _vlistAvail->next;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
125
126
127
128
129
130
131
	  newptr->next = 0;
	  idx	       = newptr->idx;
	  newptr->ptr  = ptr;
      
	  if ( VLIST_Debug )
	    Message(func, "Pointer %p has idx %d from vlist list", ptr, idx);
	}
      else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
	Warning(func, "Too many open vlists (limit is %d)!", _vlist_max);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

      VLIST_UNLOCK
    }
  else
    Error(func, "Internal problem (pointer %p undefined)", ptr);

  return (idx);
}


static void vlist_init_entry(VLIST *vlistptr)
{
  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;
158
  vlistptr->atts.nalloc    = MAX_ATTRIBUTES;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
  vlistptr->atts.nelems    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
}


static VLIST *vlist_new_entry(void)
{
  static char func[] = "vlist_new_entry";
  VLIST *vlistptr;

  vlistptr = (VLIST *) malloc(sizeof(VLIST));

  if ( vlistptr ) vlist_init_entry(vlistptr);

  return (vlistptr);
}


static void vlist_delete_entry(VLIST *vlistptr)
{
  static char func[] = "vlist_delete_entry";
  int idx;

  idx = vlistptr->self;

  VLIST_LOCK

  free(vlistptr);

  _vlistList[idx].next = _vlistAvail;
  _vlistList[idx].ptr  = 0;
  _vlistAvail          = &_vlistList[idx];

  VLIST_UNLOCK

  if ( VLIST_Debug )
    Message(func, "Removed idx %d from vlist list", idx);
}


static void vlist_initialize(void)
{
  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);

213
214
  VLIST_LOCK

Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
  vlist_init_pointer();

217
218
  VLIST_UNLOCK

Uwe Schulzweida's avatar
Uwe Schulzweida committed
219
220
221
222
223
224
225
226
227
228
  _vlist_init = TRUE;
}


static void vlist_copy(VLIST *vlistptr2, VLIST *vlistptr1)
{
  int vlistID2;

  vlistID2 = vlistptr2->self;
  memcpy(vlistptr2, vlistptr1, sizeof(VLIST));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
  vlistptr2->atts.nelems = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
230
231
232
233
234
235
236
237
238
239
240
241
  vlistptr2->self = vlistID2;
}


static void vlist_check_ptr(const char *func, VLIST *vlistptr)
{
  if ( vlistptr == NULL )
    Error(func, "vlist undefined!");
}


/*
242
@Function  vlistCreate
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
@Title     Create a variable list

245
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246

Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
@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
264
265
@EndFunction
*/
266
int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
267
{
268
  static char func[] = "vlistCreate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  int vlistID = 0;
  VLIST *vlistptr;

  VLIST_INIT

  vlistptr = vlist_new_entry();
  if ( ! vlistptr ) Error(func, "No memory");

  vlistID = vlistptr->self;

  vlistLock(vlistID);

  return (vlistID);
}


/*
286
@Function  vlistDestroy
287
@Title     Destroy a variable list
Uwe Schulzweida's avatar
Uwe Schulzweida committed
288

289
@Prototype void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290
@Parameter
291
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292
293
294

@EndFunction
*/
295
void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
296
{
297
  static char func[] = "vlistDestroy";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlist_check_ptr(func, vlistptr);

  vlistUnlock(vlistID);

  if ( vlistptr->nlock > 0 )
    {
      Warning(func, "vlist %d is locked!", vlistID);
    }
  else
    {
      int nvars = vlistptr->nvars;
313
      int varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
314

315
      vlistDelAtts(vlistID, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
317

      for ( varID = 0; varID < nvars; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
319
320
321
322
	{
	  if ( vlistptr->vars[varID].levinfo ) free(vlistptr->vars[varID].levinfo);

	  vlistDelAtts(vlistID, varID);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374

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

      vlist_delete_entry(vlistptr);
    }
}

int vlistNlock(int vlistID)
{
  static char func[] = "vlistNlock";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlist_check_ptr(func, vlistptr);

  return (vlistptr->nlock);
}

void vlistLock(int vlistID)
{
  static char func[] = "vlistLock";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlist_check_ptr(func, vlistptr);

  vlistptr->nlock++;
  /*  Message(func, "vlistID %d nlock %d", vlistID, vlistptr->nlock); */
}

void vlistUnlock(int vlistID)
{
  static char func[] = "vlistUnlock";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlist_check_ptr(func, vlistptr);

  vlistptr->nlock--;
  /*  Message(func, "vlistID %d nlock %d", vlistID, vlistptr->nlock); */
}


/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
375
376
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
377
378

@Description
379
The function @func{vlistCopy} copies all entries from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395

@EndFunction
*/
void vlistCopy(int vlistID2, int vlistID1)
{
  static char func[] = "vlistCopy";
  VLIST *vlistptr1, *vlistptr2;

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

  vlist_check_ptr(func, vlistptr1);
  vlist_check_ptr(func, vlistptr2);

  vlist_copy(vlistptr2, vlistptr1);

396
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
397

Uwe Schulzweida's avatar
Uwe Schulzweida committed
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
      int nlevs, varID;

      vlistptr2->vars = (VARINFO *) malloc(nvars*sizeof(VARINFO));
      memcpy(vlistptr2->vars, vlistptr1->vars, nvars*sizeof(VARINFO));

      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;
	  vlistptr2->vars[varID].levinfo = (LEVINFO *) malloc(nlevs*sizeof(LEVINFO));
	  memcpy(vlistptr2->vars[varID].levinfo,
		 vlistptr1->vars[varID].levinfo, nlevs*sizeof(LEVINFO));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
424
425
426

	  vlistptr2->vars[varID].atts.nelems = 0;
	  vlistCopyVarAtts(vlistID1, varID, vlistID2, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427
428
429
430
431
432
433
434
435
436
437
438
439
	}
    }

  vlistptr2->nlock = 0;
}


/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

@Description
443
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
444
445

@Result
446
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
447
448
449
450
451
452
453
454
455
456
457
458
459

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  static char func[] = "vlistDuplicate";
  int vlistIDnew;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlist_check_ptr(func, vlistptr);

460
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


void vlistClearFlag(int vlistID)
{
  int varID, levID;
  VLIST *vlistptr;

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


486
int vlistInqZaxis(int vlistID, int zaxistype, int nlevels, double *levels, int lbounds, double *levels2,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
		  int vctsize, const double *vct)
{
  static char func[] = "vlistInqZaxis";
  int zaxisdefined;
  int nzaxis;
  int zaxisID = CDI_UNDEFID;
  int index;
  int zaxisglobdefined = 0;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlist_check_ptr(func, vlistptr);

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

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

508
      if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
510
511
512
513
514
515
516
517
518
	{
	  zaxisdefined = 1;
	  break;
	}
    }

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
      for ( zaxisID = 0; zaxisID < nzaxis; zaxisID++ )
519
	if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
521
522
523
524
525
526
527
528
529
	  {
	    zaxisglobdefined = 1;
	    break;
	  }
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
	{
530
	  zaxisID = zaxisCreate(zaxistype, nlevels);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
532
533
534
535
536
537
	  zaxisDefLevels(zaxisID, levels);
	  if ( lbounds )
	    {
	      zaxisDefLbounds(zaxisID, levels);
	      zaxisDefUbounds(zaxisID, levels2);
	    }

538
	  if ( zaxistype == ZAXIS_HYBRID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
	    {
	      if ( vctsize > 0 )
		zaxisDefVct(zaxisID, vctsize, vct);
	      else
		Warning(func, "VCT missing");
	    }
	}

      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
562
563
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
564
565

@Description
566
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
  static char func[] = "vlistCopyFlag";
  VLIST *vlistptr1, *vlistptr2;

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

  vlist_check_ptr(func, vlistptr1);
  vlist_check_ptr(func, vlistptr2);

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
  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
600
601
602
603
      if ( nvars2 > 0 )
	vlistptr2->vars  = (VARINFO *) malloc(nvars2*sizeof(VARINFO));
      else
	vlistptr2->vars  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636

      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;

	    memcpy(&vlistptr2->vars[varID2], &vlistptr1->vars[varID], sizeof(VARINFO));

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

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

	    vlistptr2->vars[varID2].levinfo = (LEVINFO *) malloc(nlevs2*sizeof(LEVINFO));

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
	    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 )
		    {
656
		      vlistptr1->vars[varID].levinfo[levID].mlevelID = levID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
		      vlistptr1->vars[varID].levinfo[levID].flevelID = levID2;
		      levels[levID2++] = zaxisInqLevel(zaxisID, levID);
		    }

		zaxisType = zaxisInqType(zaxisID);

		if ( zaxisType == ZAXIS_HYBRID )
		  {
		    nvct = zaxisInqVctSize(zaxisID);
		    vct  = zaxisInqVctPtr(zaxisID);
		  }
		lbounds = 0;     /* <------------ have to define */
		levels2 = NULL;  /* <------------ have to define */
		zaxisID2 = vlistInqZaxis(vlistID2, zaxisType, nlevs2, levels, lbounds, levels2, nvct, vct);

		free(levels);
		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 )
		vlistptr2->vars[varID2].levinfo[levID2++].flevelID = levID;

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

	    if ( index ==vlistptr2->ngrids )
	      {
		vlistptr2->gridIDs[vlistptr2->ngrids++] = gridID;
695
696
		if (vlistptr2->ngrids >= MAX_GRIDS_PS )
		  Error(func, "Internal Problem! More than %d grids.", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
697
698
699
700
701
702
703
704
	      }

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

	    if ( index ==vlistptr2->nzaxis )
	      {
		vlistptr2->zaxisIDs[vlistptr2->nzaxis++] = zaxisID;
705
706
		if (vlistptr2->nzaxis >= MAX_ZAXIS_PS )
		  Error(func, "Internal Problem! More than %d zaxis.", MAX_ZAXIS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
707
708
709
710
711
712
713
714
715
716
717
718
719
720
	      }

	    varID2++;
	  }
    }
}


/*
@Function  vlistCat
@Title     Concatenate two variable lists

@Prototype void vlistCat(int vlistID2, int vlistID1)
@Parameter
721
722
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772

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

@EndFunction
*/
void vlistCat(int vlistID2, int vlistID1)
{
  static char func[] = "vlistCat";
  int nvars, nvars1, nvars2;
  int varID, varID2, nlevs;
  int index, gridID, zaxisID;
  VLIST *vlistptr1, *vlistptr2;

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

  vlist_check_ptr(func, vlistptr1);
  vlist_check_ptr(func, vlistptr2);

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

  vlistptr2->vars = (VARINFO *) realloc(vlistptr2->vars, nvars*sizeof(VARINFO));
  memcpy(vlistptr2->vars+nvars2, vlistptr1->vars, nvars1*sizeof(VARINFO));

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

      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;
      vlistptr2->vars[varID2].levinfo = (LEVINFO *) malloc(nlevs*sizeof(LEVINFO));
      memcpy(vlistptr2->vars[varID2].levinfo, vlistptr1->vars[varID].levinfo, nlevs*sizeof(LEVINFO));

773
774
775
      vlistptr2->vars[varID2].atts.nelems = 0;
      vlistCopyVarAtts(vlistID1, varID, vlistID2, varID2);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
776
777
778
779
780
781
782
      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;
783
784
	  if ( vlistptr2->ngrids >= MAX_GRIDS_PS )
	    Error(func, "Internal Problem! More than %d grids.", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
785
786
787
788
789
790
791
792
793
	}

      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;
794
795
	  if ( vlistptr2->nzaxis >= MAX_ZAXIS_PS )
	    Error(func, "Internal Problem! More than %d zaxis.", MAX_ZAXIS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
796
797
798
799
800
801
802
803
804
805
806
	}
    }
}


/*
@Function  vlistMerge
@Title     Merge two variable lists

@Prototype void vlistMerge(int vlistID2, int vlistID1)
@Parameter
807
808
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
809
810

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
811
Merge the variable list vlistID1 to the variable list vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864

@EndFunction
*/
void vlistMerge(int vlistID2, int vlistID1)
{
  static char func[] = "vlistMerge";
  int nvars1, nvars2;
  int varID = 0, varID2, levID, nlevs, nlevs1, nlevs2;
  int index, zaxisID;
  int zaxisID1, zaxisID2;
  int *lvar;
  double *levels;
  VLIST *vlistptr1, *vlistptr2;

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

  vlist_check_ptr(func, vlistptr1);
  vlist_check_ptr(func, vlistptr2);

  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
	    {
	      if ( vlistptr1->vars[varID].code != vlistptr2->vars[varID].code )
		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;

	  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
865
866
867
	  /*
	  fprintf(stderr, "var %d %d %d %d %d\n", varID, nlevs1, nlevs2, nlevs, sizeof(LEVINFO));
	  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
868
869
870
	  vlistptr2->vars[varID].levinfo =
	    (LEVINFO *) realloc(vlistptr2->vars[varID].levinfo, nlevs*sizeof(LEVINFO));

Uwe Schulzweida's avatar
Uwe Schulzweida committed
871
872
	  memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
		 vlistptr1->vars[varID].levinfo, nlevs1*sizeof(LEVINFO));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
	}

      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
890
891
892
	  /*
	  fprintf(stderr, "zaxis %d %d %d %d\n", zaxisID1, zaxisID2, nlevs1, nlevs2);
	  */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
893
894
895
896
897
898
899
900
901
	  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
902
903
904
905
	  /*
	  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
906
907
908
909
910
911
912
913
914
	  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
915
916
	  for ( varID2 = 0; varID2 < nvars2; varID2++ )
	    if ( lvar[varID2] = FALSE && vlistptr2->vars[varID2].zaxisID == zaxisID2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
	      {
		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
938
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
939
940

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

@Result
944
@func{vlistNvars} returns the number of variables in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982

@EndFunction
*/
int vlistNvars(int vlistID)
{
  static char func[] = "vlistNvars";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlist_check_ptr(func, vlistptr);

  return (vlistptr->nvars);
}

int vlistNrecs(int vlistID)
{
  static char func[] = "vlistNrecs";
  int varID, nrecs = 0;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlist_check_ptr(func, vlistptr);

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

  return (nrecs);
}


/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

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

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

@Result
989
@func{vlistNgrids} returns the number of grids in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
990
991
992
993
994
995
996
997
998
999
1000

@EndFunction
*/
int vlistNgrids(int vlistID)
{
  static char func[] = "vlistNgrids";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

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