vlist.c 37.4 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
#  define VLIST_INIT()        \
24
  pthread_once(&_vlist_init_thread, vlist_initialize)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25
26
27

#else

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

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


35
36
37
/* 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
38
{
39
40
41
42
43
44
45
  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
46
47
}

48
49
static void
vlistPrintKernel(vlist_t *vlistptr);
50
#ifdef USE_MPI
51
static int  vlistGetSizeP ( void * vlistptr, MPI_Comm comm );
52
53
54
static void vlistPackP    ( void * vlistptr, void * buff, int size,
                            int *position, MPI_Comm comm );
#endif
55

56
57
resOps vlist_ops = {
  (valCompareFunc)vlist_compare,
58
  free,
59
60
61
  (valPrintFunc)vlistPrintKernel
#ifdef USE_MPI
  , vlistGetSizeP,
62
  vlistPackP
63
#endif
64
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
65
66


67
vlist_t *vlist_to_pointer(int code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
{
69
  VLIST_INIT();
70
  return reshGetVal(code, &vlist_ops );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
72
}

73
74
static
void vlist_init_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
{
76
  vlistptr->self           = reshPut(vlistptr, &vlist_ops);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
78
79
80
81
82

  vlistptr->nlock          = 0;
  vlistptr->nvars          = 0;
  vlistptr->vars           = NULL;
  vlistptr->ngrids         = 0;
  vlistptr->nzaxis         = 0;
83
  vlistptr->taxisID        = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
84
85
86
87
88
  vlistptr->instID         = cdiDefaultInstID;
  vlistptr->modelID        = cdiDefaultModelID;
  vlistptr->tableID        = cdiDefaultTableID;
  vlistptr->varsAllocated  = 0;
  vlistptr->ntsteps        = CDI_UNDEFID;
89
  vlistptr->atts.nalloc    = MAX_ATTRIBUTES;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
  vlistptr->atts.nelems    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
92
}

93
94
static
vlist_t *vlist_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
{
96
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97

98
  vlistptr = (vlist_t *)xmalloc(sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99

100
  vlist_init_entry(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101
102
103
104

  return (vlistptr);
}

105
106
static
void vlist_delete_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
{
108
109
110
  int idx, nsp;

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111
112
113

  idx = vlistptr->self;

114
  reshRemove(idx, &vlist_ops );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
115
116
117
118

  free(vlistptr);

  if ( VLIST_Debug )
119
    Message("Removed idx %d from vlist list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
121
}

122
123
static
void vlist_initialize(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
125
126
127
128
129
130
131
{
  char *env;

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

}

132
133
static
void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
136
137
{
  int vlistID2;

  vlistID2 = vlistptr2->self;
138
  memcpy(vlistptr2, vlistptr1, sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
  vlistptr2->atts.nelems = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
141
142
  vlistptr2->self = vlistID2;
}

143
144
static
void vlist_check_ptr(const char *caller, vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146
{
  if ( vlistptr == NULL )
147
    Errorc("vlist undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149
150
}

/*
151
@Function  vlistCreate
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
153
@Title     Create a variable list

154
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155

Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
@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
173
174
@EndFunction
*/
175
int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
{
  int vlistID = 0;
178
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
179

180
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
181
182
183
184
185

  vlistptr = vlist_new_entry();

  vlistID = vlistptr->self;

186
  if ( vlistID >= 0 ) vlistLock(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
187
188
189
190
191

  return (vlistID);
}

/*
192
@Function  vlistDestroy
193
@Title     Destroy a variable list
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194

195
@Prototype void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
@Parameter
197
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
198
199
200

@EndFunction
*/
201
void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
202
{
203
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
205
206

  vlistptr = vlist_to_pointer(vlistID);

207
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
208
209
210
211
212

  vlistUnlock(vlistID);

  if ( vlistptr->nlock > 0 )
    {
213
      Warning("vlist %d is locked!", vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
215
216
217
    }
  else
    {
      int nvars = vlistptr->nvars;
218
      int varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
219

220
      vlistDelAtts(vlistID, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
222

      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
223
224
225
226
227
228
        {
          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
229

Thomas Jahns's avatar
Thomas Jahns committed
230
231
          vlistDelAtts(vlistID, varID);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
234
235
236
237
238

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

      vlist_delete_entry(vlistptr);
    }
}

239

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

  vlistptr = vlist_to_pointer(vlistID);

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

  return (vlistptr->nlock);
}

251

Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
253
void vlistLock(int vlistID)
{
254
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
256
257

  vlistptr = vlist_to_pointer(vlistID);

258
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
260

  vlistptr->nlock++;
261
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
263
}

264

Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
266
void vlistUnlock(int vlistID)
{
267
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
268
269
270

  vlistptr = vlist_to_pointer(vlistID);

271
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
273

  vlistptr->nlock--;
274
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
275
276
277
278
279
280
281
282
}

/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
283
284
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285
286

@Description
287
The function @func{vlistCopy} copies all entries from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
288
289
290
291
292

@EndFunction
*/
void vlistCopy(int vlistID2, int vlistID1)
{
293
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
295
296
297

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

298
299
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
300
301
302

  vlist_copy(vlistptr2, vlistptr1);

303
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
304

Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
306
307
308
309
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
      int nlevs, varID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
311
      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
312
313

      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
314
315
316
        {
          if ( vlistptr1->vars[varID].name )
            vlistptr2->vars[varID].name = strdupx(vlistptr1->vars[varID].name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
317

Thomas Jahns's avatar
Thomas Jahns committed
318
319
          if ( vlistptr1->vars[varID].longname )
            vlistptr2->vars[varID].longname = strdupx(vlistptr1->vars[varID].longname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
320

Thomas Jahns's avatar
Thomas Jahns committed
321
322
          if ( vlistptr1->vars[varID].stdname )
            vlistptr2->vars[varID].stdname = strdupx(vlistptr1->vars[varID].stdname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
323

Thomas Jahns's avatar
Thomas Jahns committed
324
325
          if ( vlistptr1->vars[varID].units )
            vlistptr2->vars[varID].units = strdupx(vlistptr1->vars[varID].units);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
326

Thomas Jahns's avatar
Thomas Jahns committed
327
328
329
330
          nlevs = vlistptr1->vars[varID].nlevs;
          vlistptr2->vars[varID].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
          memcpy(vlistptr2->vars[varID].levinfo,
                 vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
331

Thomas Jahns's avatar
Thomas Jahns committed
332
333
334
          vlistptr2->vars[varID].atts.nelems = 0;
          vlistCopyVarAtts(vlistID1, varID, vlistID2, varID);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336
337
338
339
340
341
342
343
344
345
    }

  vlistptr2->nlock = 0;
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

@Description
349
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
350
351

@Result
352
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353
354
355
356
357
358

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  int vlistIDnew;
359
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360
361
362

  vlistptr = vlist_to_pointer(vlistID);

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

365
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
366
367
368
369
370
371
372
373
374
375

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


void vlistClearFlag(int vlistID)
{
  int varID, levID;
376
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
377
378
379
380
381
382
383

  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++ )
Thomas Jahns's avatar
Thomas Jahns committed
384
385
386
        {
          vlistptr->vars[varID].levinfo[levID].flag = FALSE;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
387
388
389
390
    }
}


391
int vlistInqZaxis(int vlistID, int zaxistype, int nlevels, double *levels, int lbounds, double *levels2,
Thomas Jahns's avatar
Thomas Jahns committed
392
                  int vctsize, const double *vct)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
394
395
396
397
398
{
  int zaxisdefined;
  int nzaxis;
  int zaxisID = CDI_UNDEFID;
  int index;
  int zaxisglobdefined = 0;
399
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
400
401
402

  vlistptr = vlist_to_pointer(vlistID);

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

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

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

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

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
      for ( zaxisID = 0; zaxisID < nzaxis; zaxisID++ )
Thomas Jahns's avatar
Thomas Jahns committed
423
424
425
426
427
        if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
          {
            zaxisglobdefined = 1;
            break;
          }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
428
429
430
431
432
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
Thomas Jahns's avatar
Thomas Jahns committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        {
          zaxisID = zaxisCreate(zaxistype, nlevels);
          zaxisDefLevels(zaxisID, levels);
          if ( lbounds )
            {
              zaxisDefLbounds(zaxisID, levels);
              zaxisDefUbounds(zaxisID, levels2);
            }

          if ( zaxistype == ZAXIS_HYBRID )
            {
              if ( vctsize > 0 )
                zaxisDefVct(zaxisID, vctsize, vct);
              else
                Warning("VCT missing");
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464

      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
465
466
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
467
468

@Description
469
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
470
471
472
473
474

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
475
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
476
477
478
479

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

480
481
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
482
483

  vlist_copy(vlistptr2, vlistptr1);
Thomas Jahns's avatar
Thomas Jahns committed
484

485
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
486

Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
488
489
490
491
492
493
494
495
496
497
498
  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++ )
Thomas Jahns's avatar
Thomas Jahns committed
499
        if ( vlistptr1->vars[varID].flag ) nvars2++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
500
501

      vlistptr2->nvars = nvars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
      if ( nvars2 > 0 )
Thomas Jahns's avatar
Thomas Jahns committed
503
        vlistptr2->vars  = (var_t *) malloc(nvars2*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
504
      else
Thomas Jahns's avatar
Thomas Jahns committed
505
        vlistptr2->vars  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507
508

      varID2 = 0;
      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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
        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(var_t));

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

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

            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_t *) malloc(nlevs2*sizeof(levinfo_t));

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

            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;
                      vlistptr1->vars[varID].levinfo[levID].mlevelID = 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);

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

                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;
                  vlistptr2->vars[varID2].levinfo[levID2].mlevelID = levID;
                  levID2++;
                }

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

            if ( index == vlistptr2->ngrids )
              {
                vlistptr2->gridIDs[vlistptr2->ngrids++] = gridID;
                if (vlistptr2->ngrids >= MAX_GRIDS_PS )
                  Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
              }

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

            if ( index == vlistptr2->nzaxis )
              {
                vlistptr2->zaxisIDs[vlistptr2->nzaxis++] = zaxisID;
                if (vlistptr2->nzaxis >= MAX_ZAXES_PS )
                  Error("Internal Problem! More than %d zaxis.", MAX_ZAXES_PS);
              }

            varID2++;
          }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
629
630
631
632
633
634
635
636
637
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

@Prototype void vlistCat(int vlistID2, int vlistID1)
@Parameter
638
639
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
640
641
642
643
644
645
646
647
648
649
650

@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;
651
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
652
653
654
655

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

656
657
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
658
659
660
661
662
663

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
664
665
  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
666
667
668
669
670
671
672

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

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

676
      if ( vlistptr1->vars[varID].param < 0 )
Thomas Jahns's avatar
Thomas Jahns committed
677
678
679
680
681
682
        {
          int pnum, pcat, pdis;
          cdiDecodeParam(vlistptr1->vars[varID].param, &pnum, &pcat, &pdis);
          pnum=-(varID2+1);
          vlistptr2->vars[varID2].param = cdiEncodeParam(pnum, pcat, pdis);
        }
683

Uwe Schulzweida's avatar
Uwe Schulzweida committed
684
      if ( vlistptr1->vars[varID].name )
Thomas Jahns's avatar
Thomas Jahns committed
685
        vlistptr2->vars[varID2].name = strdupx(vlistptr1->vars[varID].name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
686
687

      if ( vlistptr1->vars[varID].longname )
Thomas Jahns's avatar
Thomas Jahns committed
688
        vlistptr2->vars[varID2].longname = strdupx(vlistptr1->vars[varID].longname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
689
690

      if ( vlistptr1->vars[varID].stdname )
Thomas Jahns's avatar
Thomas Jahns committed
691
        vlistptr2->vars[varID2].stdname = strdupx(vlistptr1->vars[varID].stdname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
692
693

      if ( vlistptr1->vars[varID].units )
Thomas Jahns's avatar
Thomas Jahns committed
694
        vlistptr2->vars[varID2].units = strdupx(vlistptr1->vars[varID].units);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
695
696

      nlevs = vlistptr1->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
697
698
      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
699

700
701
702
      vlistptr2->vars[varID2].atts.nelems = 0;
      vlistCopyVarAtts(vlistID1, varID, vlistID2, varID2);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
703
704
      gridID = vlistptr1->vars[varID].gridID;
      for ( index = 0; index < vlistptr2->ngrids; index++ )
Thomas Jahns's avatar
Thomas Jahns committed
705
        if ( gridID == vlistptr2->gridIDs[index] ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
706
707

      if ( index == vlistptr2->ngrids )
Thomas Jahns's avatar
Thomas Jahns committed
708
709
710
711
712
        {
          vlistptr2->gridIDs[vlistptr2->ngrids++] = gridID;
          if ( vlistptr2->ngrids >= MAX_GRIDS_PS )
            Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
713
714
715

      zaxisID = vlistptr1->vars[varID].zaxisID;
      for ( index = 0; index < vlistptr2->nzaxis; index++ )
Thomas Jahns's avatar
Thomas Jahns committed
716
        if ( zaxisID == vlistptr2->zaxisIDs[index] ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
717
718

      if ( index == vlistptr2->nzaxis )
Thomas Jahns's avatar
Thomas Jahns committed
719
720
721
722
723
        {
          vlistptr2->zaxisIDs[vlistptr2->nzaxis++] = zaxisID;
          if ( vlistptr2->nzaxis >= MAX_ZAXES_PS )
            Error("Internal Problem! More than %d zaxis.", MAX_ZAXES_PS);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724
725
726
727
728
729
730
731
732
    }
}

/*
@Function  vlistMerge
@Title     Merge two variable lists

@Prototype void vlistMerge(int vlistID2, int vlistID1)
@Parameter
733
734
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
735
736

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
737
Merge the variable list vlistID1 to the variable list vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
738
739
740
741
742
743
744
745
746
747
748

@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;
749
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
750
751
752
753

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

754
755
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
757
758
759
760
761
762

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

  if ( nvars1 == nvars2 )
    {
      for ( varID = 0; varID < nvars2; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
763
764
765
766
767
768
769
770
771
772
773
774
        {
          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].param != vlistptr2->vars[varID].param )
                break;
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
775
776
777
778
779
    }

  if ( varID == nvars2 ) /* same variables in vlistID1 and vlistID2 */
    {
      for ( varID = 0; varID < nvars2; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
780
781
782
        {
          vlistptr1->vars[varID].fvarID = varID;
          vlistptr2->vars[varID].fvarID = varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
783

Thomas Jahns's avatar
Thomas Jahns committed
784
785
          vlistptr1->vars[varID].mvarID = varID;
          vlistptr2->vars[varID].mvarID = varID;
786

Thomas Jahns's avatar
Thomas Jahns committed
787
788
          nlevs1 = vlistptr1->vars[varID].nlevs;
          nlevs2 = vlistptr2->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
789

Thomas Jahns's avatar
Thomas Jahns committed
790
          nlevs = nlevs1 + nlevs2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
791

Thomas Jahns's avatar
Thomas Jahns committed
792
793
794
795
796
797
          vlistptr2->vars[varID].nlevs = nlevs;
          /*
          fprintf(stderr, "var %d %d %d %d %d\n", varID, nlevs1, nlevs2, nlevs, sizeof(levinfo_t));
          */
          vlistptr2->vars[varID].levinfo =
            (levinfo_t *) realloc(vlistptr2->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
798

Thomas Jahns's avatar
Thomas Jahns committed
799
800
          memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
                 vlistptr1->vars[varID].levinfo, nlevs1*sizeof(levinfo_t));
801

Thomas Jahns's avatar
Thomas Jahns committed
802
803
804
805
806
          for ( levID = 0; levID < nlevs1; levID++ )
            {
              vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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++ )
Thomas Jahns's avatar
Thomas Jahns 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
        {
          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);
          /*
          fprintf(stderr, "zaxis %d %d %d %d\n", zaxisID1, zaxisID2, nlevs1, nlevs2);
          */
          nlevs = nlevs1 + nlevs2;

          zaxisID = zaxisDuplicate(zaxisID2);

          zaxisResize(zaxisID, nlevs);

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

          zaxisInqLevels(zaxisID1, levels);
          /*
          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]);
          */
          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;

          for ( varID2 = 0; varID2 < nvars2; varID2++ )
            if ( lvar[varID2] == FALSE && vlistptr2->vars[varID2].zaxisID == zaxisID2 )
              {
                vlistptr2->vars[varID2].zaxisID = zaxisID;
                lvar[varID2] = TRUE;
              }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869

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

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

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

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

@Result
876
@func{vlistNvars} returns the number of variables in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
877
878
879
880
881

@EndFunction
*/
int vlistNvars(int vlistID)
{
882
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
883
884
885

  vlistptr = vlist_to_pointer(vlistID);

886
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
887
888
889
890

  return (vlistptr->nvars);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
891

Uwe Schulzweida's avatar
Uwe Schulzweida committed
892
893
894
int vlistNrecs(int vlistID)
{
  int varID, nrecs = 0;
895
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
896
897
898

  vlistptr = vlist_to_pointer(vlistID);

899
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
900
901
902
903
904
905
906
907

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

  return (nrecs);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
908
909
int vlistNumber(int vlistID)
{
910
  int varID, number, number2, datatype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
911
912
913
914
  vlist_t *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

915
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
916

917
918
919
920
921
922
  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
923
  for ( varID = 1; varID < vlistptr->nvars; varID++ )
924
925
926
    {
      datatype = vlistptr->vars[varID].datatype;
      if ( datatype == DATATYPE_CPX32 || datatype == DATATYPE_CPX64 )
Thomas Jahns's avatar
Thomas Jahns committed
927
        number2 = CDI_COMP;
928
      else
Thomas Jahns's avatar
Thomas Jahns committed
929
        number2 = CDI_REAL;
930
931

      if ( number2 != number )
Thomas Jahns's avatar
Thomas Jahns committed
932
933
934
935
        {
          number = CDI_BOTH;
          break;
        }
936
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
937
938
939
940

  return (number);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
941
942
943
944
945
946
/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

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

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

@Result
953
@func{vlistNgrids} returns the number of grids in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
954
955
956
957
958

@EndFunction
*/
int vlistNgrids(int vlistID)
{
959
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
960
961
962

  vlistptr = vlist_to_pointer(vlistID);

963
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
964
965
966
967
968
969
970
971
972
973

  return (vlistptr->ngrids);
}

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

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

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

@Result
980
@func{vlistNzaxis} returns the number of zaxis in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
981
982
983
984
985

@EndFunction
*/
int vlistNzaxis(int vlistID)
{
986
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
987
988
989

  vlistptr = vlist_to_pointer(vlistID);

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

  return (vlistptr->nzaxis);
}


void vlistDefNtsteps(int vlistID, int nts)
{
998
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
999
1000
1001

  vlistptr = vlist_to_pointer(vlistID);

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

  vlistptr->ntsteps = nts;
}


int vlistNtsteps(int vlistID)
{
1010
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1011
1012
1013

  vlistptr = vlist_to_pointer(vlistID);

1014
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1015
1016
1017
1018

  return (vlistptr->ntsteps);
}

1019
1020
static void
vlistPrintKernel(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1021
1022
{
  int nvars, flag, index;
1023
  int varID, fvarID, mvarID, flevID, mlevID, levID;
1024
  int param, gridID, zaxisID, timeID, nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1025
  int dtype;
1026
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1027
1028
1029
  char *name, *longname, *units;
  double level;

1030
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1031

1032
  printf("#\n# vlistID %d\n#\n", vlistptr->self);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045

  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 )
    {
1046
      printf(" varID param    gridID zaxisID timeID nlevel flag  name     longname\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1047
      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
        {
          param    = vlistptr->vars[varID].param;
          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;

          cdiParamToString(param, paramstr, sizeof(paramstr));
          printf("%6d %-8s %6d %6d %6d %6d %5d  %-8s %s",
                 varID, paramstr, gridID, zaxisID, timeID, nlevs, flag,
                 name ? name : "", longname ? longname : "");
          if ( units ) printf("   [%s]", units);
          printf("\n");
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1066
1067

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

1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101

void vlistPrint(int vlistID)
{
  vlist_t *vlistptr;

  if ( vlistID == CDI_UNDEFID ) return;

  vlistptr = vlist_to_pointer(vlistID);
  vlist_check_ptr(__func__, vlistptr);
  vlistPrintKernel(vlistptr);
}

1102
1103
1104
1105
1106
1107
1108
/*
@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
1109
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
1110
1111
1112
1113
1114
1115

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

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1116
1117
void vlistDefTaxis(int vlistID, int taxisID)
{
1118
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1119
1120
1121

  vlistptr = vlist_to_pointer(vlistID);

1122
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1123
1124
1125
1126

  vlistptr->taxisID = taxisID;
}

1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
/*
@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
1143
1144
int vlistInqTaxis(int vlistID)
{
1145
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1146
1147
1148

  vlistptr = vlist_to_pointer(vlistID);

1149
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1150
1151
1152
1153

  return (vlistptr->taxisID);
}

1154

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1155
1156
void  vlistDefTable(int vlistID, int tableID)
{
1157
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1158
1159
1160

  vlistptr = vlist_to_pointer(vlistID);

1161
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1162
1163
1164
1165

  vlistptr->tableID = tableID;
}

1166

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1167
1168
int vlistInqTable(int vlistID)
{
1169
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1170
1171
1172

  vlistptr = vlist_to_pointer(vlistID);

1173
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1174
1175
1176
1177

  return (vlistptr->tableID);
}

1178

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1179
1180
void vlistDefInstitut(int vlistID, int instID)
{
1181
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1182
1183
1184

  vlistptr = vlist_to_pointer(vlistID);

1185
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1186
1187
1188
1189

  vlistptr->instID = instID;
}

1190

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1191
1192
1193
int vlistInqInstitut(int vlistID)
{
  int varID, instID;
1194
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1195
1196
1197

  vlistptr = vlist_to_pointer(vlistID);

1198
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1199
1200
1201
1202
1203
1204
1205
1206

  instID = vlistptr->instID;

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

      for ( varID = 1; varID < vlistptr->nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
1207
1208
1209
1210
        if ( instID != vlistInqVarInstitut(vlistID, varID) )
          {
            instID = CDI_UNDEFID;
            break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1211
      }
Thomas Jahns's avatar
Thomas Jahns committed
1212

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1213
1214
1215
1216
1217
1218
      vlistDefInstitut(vlistID, instID);
    }

  return (instID);
}

1219

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1220
1221
void vlistDefModel(int vlistID, int modelID)
{
1222
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1223
1224
1225

  vlistptr = vlist_to_pointer(vlistID);

1226
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1227
1228
1229
1230

  vlistptr->modelID = modelID;
}

1231

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1232
1233
1234
int vlistInqModel(int vlistID)
{
  int varID, modelID;
1235
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1236
1237
1238

  vlistptr = vlist_to_pointer(vlistID);

1239
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1240
1241
1242
1243
1244
1245
1246
1247

  modelID = vlistptr->modelID;

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

      for ( varID = 1; varID < vlistptr->nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
1248
1249
1250
1251
1252
        if ( modelID != vlistInqVarModel(vlistID, varID) )
          {
            modelID = CDI_UNDEFID;
            break;
          }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1253
1254
1255
1256
1257
1258
1259

      vlistDefModel(vlistID, modelID);
    }

  return (modelID);
}

1260

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1261
1262
1263
1264
int vlistGridsizeMax(int vlistID)
{
  int gridsize, gridsizemax = 0;
  int gridID, index;
1265
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1266
1267
1268

  vlistptr = vlist_to_pointer(vlistID);

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

  for ( index = 0 ; index < vlistptr->ngrids ; index++ )
    {
      gridID = vlistptr->gridIDs[index];
      gridsize = gridInqSize(gridID);
      if ( gridsize > gridsizemax ) gridsizemax = gridsize;
    }
Thomas Jahns's avatar
Thomas Jahns committed
1277

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1278
1279
1280
  return (gridsizemax);
}

1281

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1282
1283
1284
int vlistGrid(int vlistID, int index)
{
  int gridID = CDI_UNDEFID;
1285
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1286
1287
1288

  vlistptr = vlist_to_pointer(vlistID);

1289
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1290
1291
1292

  if ( index < vlistptr->ngrids && index >= 0 )
    gridID = vlistptr->gridIDs[index];
Thomas Jahns's avatar
Thomas Jahns committed
1293

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1294
1295
1296
  return (gridID);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1297

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1298
1299
1300
int vlistGridIndex(int vlistID, int gridID)
{
  int index;
1301
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1302
1303
1304

  vlistptr = vlist_to_pointer(vlistID);

1305
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1306
1307
1308
1309
1310

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

  if ( index == vlistptr->ngrids ) index = -1;
Thomas Jahns's avatar
Thomas Jahns committed
1311

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1312
1313
1314
  return (index);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1315

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1316
1317
1318
1319
void vlistChangeGridIndex(int vlistID, int index, int gridID)
{
  int gridIDold;
  int varID, nvars;
1320
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1321
1322
1323

  vlistptr = vlist_to_pointer(vlistID);

1324
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334

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