vlist.c 37.5 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"
13
#include "pio_rpc.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
14

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

static void vlist_initialize(void);

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

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

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

#else

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

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


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

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

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


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

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

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

96
97
static
vlist_t *vlist_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
{
99
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100

101
  vlistptr = (vlist_t *)xmalloc(sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
102

103
  vlist_init_entry(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
106
107

  return (vlistptr);
}

108
109
static
void vlist_delete_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
110
{
111
112
113
  int idx, nsp;

  nsp = namespaceGetActive ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
116

  idx = vlistptr->self;

117
  reshRemove(idx, &vlist_ops );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
120
121

  free(vlistptr);

  if ( VLIST_Debug )
122
    Message("Removed idx %d from vlist list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124
}

125
126
static
void vlist_initialize(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
127
128
129
130
131
132
133
134
{
  char *env;

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

}

135
136
static
void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
140
{
  int vlistID2;

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

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

/*
154
@Function  vlistCreate
Uwe Schulzweida's avatar
Uwe Schulzweida committed
155
156
@Title     Create a variable list

157
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158

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

183
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
185
186
187
188

  vlistptr = vlist_new_entry();

  vlistID = vlistptr->self;

189
  if ( vlistID >= 0 ) vlistLock(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
191
192
193
194

  return (vlistID);
}

/*
195
@Function  vlistDestroy
196
@Title     Destroy a variable list
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197

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

@EndFunction
*/
204
void vlistDestroy(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
{
206
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
209

  vlistptr = vlist_to_pointer(vlistID);

210
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211
212
213
214
215

  vlistUnlock(vlistID);

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

223
      vlistDelAtts(vlistID, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
224
225

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

Thomas Jahns's avatar
Thomas Jahns committed
233
234
          vlistDelAtts(vlistID, varID);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
235
236
237
238
239
240
241

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

      vlist_delete_entry(vlistptr);
    }
}

242

Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
int vlistNlock(int vlistID)
{
245
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
247
248

  vlistptr = vlist_to_pointer(vlistID);

249
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
251
252
253

  return (vlistptr->nlock);
}

254

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

  vlistptr = vlist_to_pointer(vlistID);

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

  vlistptr->nlock++;
264
  /*  Message("vlistID %d nlock %d", vlistID, vlistptr->nlock); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
266
}

267

Uwe Schulzweida's avatar
Uwe Schulzweida committed
268
269
void vlistUnlock(int vlistID)
{
270
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
272
273

  vlistptr = vlist_to_pointer(vlistID);

274
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
275
276

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

/*
@Function  vlistCopy
@Title     Copy a variable list

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

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

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

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

301
302
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
303
304
305

  vlist_copy(vlistptr2, vlistptr1);

306
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
307

Uwe Schulzweida's avatar
Uwe Schulzweida committed
308
309
310
311
312
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
      int nlevs, varID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
314
      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
315
316

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

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

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

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

Thomas Jahns's avatar
Thomas Jahns committed
330
331
332
333
          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
334

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

  vlistptr2->nlock = 0;
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

@Description
352
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353
354

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

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  int vlistIDnew;
362
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
363
364
365

  vlistptr = vlist_to_pointer(vlistID);

366
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367

368
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369
370
371
372
373
374
375
376
377
378

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


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

  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
387
388
389
        {
          vlistptr->vars[varID].levinfo[levID].flag = FALSE;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
391
392
393
    }
}


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

  vlistptr = vlist_to_pointer(vlistID);

406
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
407
408
409
410
411
412
413
414

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

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

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

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

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
Thomas Jahns's avatar
Thomas Jahns committed
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
        {
          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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467

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

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

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

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

483
484
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486

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

488
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
489

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

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

      varID2 = 0;
      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
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
629
630
631
        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
632
633
634
635
636
637
638
639
640
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

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

@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;
654
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
655
656
657
658

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

659
660
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
661
662
663
664
665
666

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
667
668
  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
669
670
671
672
673
674
675

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

676
677
678
      vlistptr1->vars[varID].mvarID = varID2;
      vlistptr2->vars[varID2].mvarID = varID;

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

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

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

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

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

      nlevs = vlistptr1->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
700
701
      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
702

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

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

      if ( index == vlistptr2->ngrids )
Thomas Jahns's avatar
Thomas Jahns committed
711
712
713
714
715
        {
          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
716
717
718

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

      if ( index == vlistptr2->nzaxis )
Thomas Jahns's avatar
Thomas Jahns committed
722
723
724
725
726
        {
          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
727
728
729
730
731
732
733
734
735
    }
}

/*
@Function  vlistMerge
@Title     Merge two variable lists

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

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

@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;
752
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
753
754
755
756

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

757
758
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
759
760
761
762
763
764
765

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

  if ( nvars1 == nvars2 )
    {
      for ( varID = 0; varID < nvars2; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
766
767
768
769
770
771
772
773
774
775
776
777
        {
          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
778
779
780
781
782
    }

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

Thomas Jahns's avatar
Thomas Jahns committed
787
788
          vlistptr1->vars[varID].mvarID = varID;
          vlistptr2->vars[varID].mvarID = varID;
789

Thomas Jahns's avatar
Thomas Jahns committed
790
791
          nlevs1 = vlistptr1->vars[varID].nlevs;
          nlevs2 = vlistptr2->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
792

Thomas Jahns's avatar
Thomas Jahns committed
793
          nlevs = nlevs1 + nlevs2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
794

Thomas Jahns's avatar
Thomas Jahns committed
795
796
797
798
799
800
          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
801

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

Thomas Jahns's avatar
Thomas Jahns committed
805
806
807
808
809
          for ( levID = 0; levID < nlevs1; levID++ )
            {
              vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
810
811
812
813
814

      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
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
        {
          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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872

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

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

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

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

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

@EndFunction
*/
int vlistNvars(int vlistID)
{
885
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
886
887
888

  vlistptr = vlist_to_pointer(vlistID);

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

  return (vlistptr->nvars);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
894

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

  vlistptr = vlist_to_pointer(vlistID);

902
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
903
904
905
906
907
908
909
910

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

  return (nrecs);
}


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

  vlistptr = vlist_to_pointer(vlistID);

918
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
919

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

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

  return (number);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
944
945
946
947
948
949
/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

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

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

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

@EndFunction
*/
int vlistNgrids(int vlistID)
{
962
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
963
964
965

  vlistptr = vlist_to_pointer(vlistID);

966
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
967
968
969
970
971
972
973
974
975
976

  return (vlistptr->ngrids);
}

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

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

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

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

@EndFunction
*/
int vlistNzaxis(int vlistID)
{
989
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
990
991
992

  vlistptr = vlist_to_pointer(vlistID);

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

  return (vlistptr->nzaxis);
}


void vlistDefNtsteps(int vlistID, int nts)
{
1001
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1002
1003
1004

  vlistptr = vlist_to_pointer(vlistID);

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

  vlistptr->ntsteps = nts;
}


int vlistNtsteps(int vlistID)
{
1013
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1014
1015
1016

  vlistptr = vlist_to_pointer(vlistID);

1017
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1018
1019
1020
1021

  return (vlistptr->ntsteps);
}

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

1033
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1034

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

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

      printf("\n");
1071
      printf(" varID  levID fvarID flevID mvarID mlevID  index  dtype  flag  level\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1072
      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
        {
          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
1090
1091
1092
    }
}

1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104

void vlistPrint(int vlistID)
{
  vlist_t *vlistptr;

  if ( vlistID == CDI_UNDEFID ) return;

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

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

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

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

  vlistptr = vlist_to_pointer(vlistID);

1125
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1126
1127
1128
1129

  vlistptr->taxisID = taxisID;
}

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

  vlistptr = vlist_to_pointer(vlistID);

1152
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1153
1154
1155
1156

  return (vlistptr->taxisID);
}

1157

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1158
1159
void  vlistDefTable(int vlistID, int tableID)
{
1160
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1161
1162
1163

  vlistptr = vlist_to_pointer(vlistID);

1164
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1165
1166
1167
1168

  vlistptr->tableID = tableID;
}

1169

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1170
1171
int vlistInqTable(int vlistID)
{
1172
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1173
1174
1175

  vlistptr = vlist_to_pointer(vlistID);

1176
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1177
1178
1179
1180

  return (vlistptr->tableID);
}

1181

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1182
1183
void vlistDefInstitut(int vlistID, int instID)
{
1184
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1185
1186
1187

  vlistptr = vlist_to_pointer(vlistID);

1188
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1189
1190
1191
1192

  vlistptr->instID = instID;
}

1193

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1194
1195
1196
int vlistInqInstitut(int vlistID)
{
  int varID, instID;
1197
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1198
1199
1200

  vlistptr = vlist_to_pointer(vlistID);

1201
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1202
1203
1204
1205
1206
1207
1208
1209

  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
1210
1211
1212
1213
        if ( instID != vlistInqVarInstitut(vlistID, varID) )
          {
            instID = CDI_UNDEFID;
            break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1214
      }
Thomas Jahns's avatar
Thomas Jahns committed
1215

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1216
1217
1218
1219
1220
1221
      vlistDefInstitut(vlistID, instID);
    }

  return (instID);
}

1222

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1223
1224
void vlistDefModel(int vlistID, int modelID)
{
1225
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1226
1227
1228

  vlistptr = vlist_to_pointer(vlistID);

1229
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1230
1231
1232
1233

  vlistptr->modelID = modelID;
}

1234

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1235
1236
1237
int vlistInqModel(int vlistID)
{
  int varID, modelID;
1238
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1239
1240
1241

  vlistptr = vlist_to_pointer(vlistID);

1242
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1243
1244
1245
1246
1247
1248
1249
1250

  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
1251
1252
1253
1254
1255
        if ( modelID != vlistInqVarModel(vlistID, varID) )
          {
            modelID = CDI_UNDEFID;
            break;
          }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1256
1257
1258
1259
1260
1261
1262

      vlistDefModel(vlistID, modelID);
    }

  return (modelID);
}

1263

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

  vlistptr = vlist_to_pointer(vlistID);

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

  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
1280

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1281
1282
1283
  return (gridsizemax);
}

1284

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

  vlistptr = vlist_to_pointer(vlistID);

1292
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1293
1294
1295

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1297
1298
1299
  return (gridID);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1300

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1301
1302
1303
int vlistGridIndex(int vlistID, int gridID)
{
  int index;
1304
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1305
1306
1307

  vlistptr = vlist_to_pointer(vlistID);

1308
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1309
1310
1311
1312
1313

  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
1314

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1315
1316
1317
  return (index);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1318

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

  vlistptr = vlist_to_pointer(vlistID);