vlist.c 40.7 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"
Thomas Jahns's avatar
Thomas Jahns committed
13
#include "vlist_var.h"
14
#include "vlist_att.h"
15
#include "pio_rpc.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
16

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

static void vlist_initialize(void);

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

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

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

#else

31
static int vlistIsInitialized = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32

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


38
39
40
/* 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
41
{
42
43
44
45
46
47
  int diff;
  diff = (a->nvars != b->nvars) || (a->ngrids != b->ngrids)
    || (a->nzaxis != b->nzaxis) || (a->instID != b->instID)
    || (a->modelID != b->modelID) || (a->tableID != b->tableID)
    || (a->ntsteps != b->ntsteps);
  return diff;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
49
}

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

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


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

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

  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
{
  char *env;

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

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

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

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

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

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

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

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

  vlistptr = vlist_new_entry();

  vlistID = vlistptr->self;

  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;
Deike Kleberg's avatar
Deike Kleberg committed
204
205
  int nvars;
  int varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
206
207
208

  vlistptr = vlist_to_pointer(vlistID);

209
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210

Deike Kleberg's avatar
Deike Kleberg committed
211
  vlistDelAtts(vlistID, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212

Deike Kleberg's avatar
Deike Kleberg committed
213
  nvars = vlistptr->nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214

Deike Kleberg's avatar
Deike Kleberg committed
215
216
217
218
219
220
221
222
223
  for ( varID = 0; varID < nvars; varID++ )
    {
      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);
      
      vlistDelAtts(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
224
    }
Deike Kleberg's avatar
Deike Kleberg committed
225
226
  
  if ( vlistptr->vars ) free(vlistptr->vars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
227

Deike Kleberg's avatar
Deike Kleberg committed
228
  vlist_delete_entry(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
229
230
231
232
233
234
235
236
}

/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
237
238
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239
240

@Description
241
The function @func{vlistCopy} copies all entries from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
242
243
244
245
246

@EndFunction
*/
void vlistCopy(int vlistID2, int vlistID1)
{
247
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
248
249
250
251

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

252
253
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
254
255
256

  vlist_copy(vlistptr2, vlistptr1);

257
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
258

Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
260
261
262
263
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
      int nlevs, varID;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
265
      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
266
267

      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
268
269
270
        {
          if ( vlistptr1->vars[varID].name )
            vlistptr2->vars[varID].name = strdupx(vlistptr1->vars[varID].name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271

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

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

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

Thomas Jahns's avatar
Thomas Jahns committed
281
282
283
284
          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
285

Thomas Jahns's avatar
Thomas Jahns committed
286
287
288
          vlistptr2->vars[varID].atts.nelems = 0;
          vlistCopyVarAtts(vlistID1, varID, vlistID2, varID);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
290
291
292
293
294
295
296
297
    }
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

@Description
301
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
302
303

@Result
304
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
306
307
308
309
310

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  int vlistIDnew;
311
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
313
314

  vlistptr = vlist_to_pointer(vlistID);

315
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
316

317
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
319
320

  vlistCopy(vlistIDnew, vlistID);

321
322
323
  if ( ! namespaceHasLocalFile ( namespaceGetActive ()))
    reshSetStatus ( vlistIDnew, &vlist_ops, SUSPENDED );

Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
325
326
327
328
329
330
  return (vlistIDnew);
}


void vlistClearFlag(int vlistID)
{
  int varID, levID;
331
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
332
333
334
335
336
337
338

  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
339
340
341
        {
          vlistptr->vars[varID].levinfo[levID].flag = FALSE;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
342
343
344
345
    }
}


346
int vlistInqZaxis(int vlistID, int zaxistype, int nlevels, double *levels, int lbounds, double *levels2,
Thomas Jahns's avatar
Thomas Jahns committed
347
                  int vctsize, const double *vct)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
349
350
351
352
353
{
  int zaxisdefined;
  int nzaxis;
  int zaxisID = CDI_UNDEFID;
  int index;
  int zaxisglobdefined = 0;
354
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
356
357

  vlistptr = vlist_to_pointer(vlistID);

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

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

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

367
      if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
Thomas Jahns's avatar
Thomas Jahns committed
368
369
370
371
        {
          zaxisdefined = 1;
          break;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
373
374
375
376
377
    }

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
      for ( zaxisID = 0; zaxisID < nzaxis; zaxisID++ )
Thomas Jahns's avatar
Thomas Jahns committed
378
379
380
381
382
        if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, NULL, NULL, 0) == 0 )
          {
            zaxisglobdefined = 1;
            break;
          }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
383
384
385
386
387
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
Thomas Jahns's avatar
Thomas Jahns committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
        {
          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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419

      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
420
421
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
423

@Description
424
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
425
426
427
428
429

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
430
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
431
432
433
434

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

435
436
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
437
438

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

440
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
441

Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
443
444
445
446
447
448
449
450
451
452
453
  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
454
        if ( vlistptr1->vars[varID].flag ) nvars2++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
455
456

      vlistptr2->nvars = nvars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
457
      if ( nvars2 > 0 )
Thomas Jahns's avatar
Thomas Jahns committed
458
        vlistptr2->vars  = (var_t *) malloc(nvars2*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
459
      else
Thomas Jahns's avatar
Thomas Jahns committed
460
        vlistptr2->vars  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
461
462
463

      varID2 = 0;
      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
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
        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
584
585
586
587
588
589
590
591
592
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

@Prototype void vlistCat(int vlistID2, int vlistID1)
@Parameter
593
594
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
595
596
597
598
599
600
601
602
603
604
605

@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;
606
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
608
609
610

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

611
612
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
613
614
615
616
617
618

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
619
620
  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
621
622
623
624
625
626
627

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

628
629
630
      vlistptr1->vars[varID].mvarID = varID2;
      vlistptr2->vars[varID2].mvarID = varID;

631
      if ( vlistptr1->vars[varID].param < 0 )
Thomas Jahns's avatar
Thomas Jahns committed
632
633
634
635
636
637
        {
          int pnum, pcat, pdis;
          cdiDecodeParam(vlistptr1->vars[varID].param, &pnum, &pcat, &pdis);
          pnum=-(varID2+1);
          vlistptr2->vars[varID2].param = cdiEncodeParam(pnum, pcat, pdis);
        }
638

Uwe Schulzweida's avatar
Uwe Schulzweida committed
639
      if ( vlistptr1->vars[varID].name )
Thomas Jahns's avatar
Thomas Jahns committed
640
        vlistptr2->vars[varID2].name = strdupx(vlistptr1->vars[varID].name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
641
642

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

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

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

      nlevs = vlistptr1->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
652
653
      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
654

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
658
659
      gridID = vlistptr1->vars[varID].gridID;
      for ( index = 0; index < vlistptr2->ngrids; index++ )
Thomas Jahns's avatar
Thomas Jahns committed
660
        if ( gridID == vlistptr2->gridIDs[index] ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
661
662

      if ( index == vlistptr2->ngrids )
Thomas Jahns's avatar
Thomas Jahns committed
663
664
665
666
667
        {
          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
668
669
670

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

      if ( index == vlistptr2->nzaxis )
Thomas Jahns's avatar
Thomas Jahns committed
674
675
676
677
678
        {
          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
679
680
681
682
683
684
685
686
687
    }
}

/*
@Function  vlistMerge
@Title     Merge two variable lists

@Prototype void vlistMerge(int vlistID2, int vlistID1)
@Parameter
688
689
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
690
691

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
692
Merge the variable list vlistID1 to the variable list vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
693
694
695
696
697
698
699
700
701
702
703

@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;
704
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
705
706
707
708

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

709
710
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
711
712
713
714
715
716
717

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

  if ( nvars1 == nvars2 )
    {
      for ( varID = 0; varID < nvars2; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
718
719
720
721
722
723
724
725
726
727
728
729
        {
          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
730
731
732
733
734
    }

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

Thomas Jahns's avatar
Thomas Jahns committed
739
740
          vlistptr1->vars[varID].mvarID = varID;
          vlistptr2->vars[varID].mvarID = varID;
741

Thomas Jahns's avatar
Thomas Jahns committed
742
743
          nlevs1 = vlistptr1->vars[varID].nlevs;
          nlevs2 = vlistptr2->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
744

Thomas Jahns's avatar
Thomas Jahns committed
745
          nlevs = nlevs1 + nlevs2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
746

Thomas Jahns's avatar
Thomas Jahns committed
747
748
749
750
751
752
          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
753

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

Thomas Jahns's avatar
Thomas Jahns committed
757
758
759
760
761
          for ( levID = 0; levID < nlevs1; levID++ )
            {
              vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
762
763
764
765
766

      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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
        {
          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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824

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

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

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

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

@Result
831
@func{vlistNvars} returns the number of variables in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
832
833
834
835
836

@EndFunction
*/
int vlistNvars(int vlistID)
{
837
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
838
839
840

  vlistptr = vlist_to_pointer(vlistID);

841
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
842
843
844
845

  return (vlistptr->nvars);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
846

Uwe Schulzweida's avatar
Uwe Schulzweida committed
847
848
849
int vlistNrecs(int vlistID)
{
  int varID, nrecs = 0;
850
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
851
852
853

  vlistptr = vlist_to_pointer(vlistID);

854
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
855
856
857
858
859
860
861
862

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

  return (nrecs);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
863
864
int vlistNumber(int vlistID)
{
865
  int varID, number, number2, datatype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
866
867
868
869
  vlist_t *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

870
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
871

872
873
874
875
876
877
  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
878
  for ( varID = 1; varID < vlistptr->nvars; varID++ )
879
880
881
    {
      datatype = vlistptr->vars[varID].datatype;
      if ( datatype == DATATYPE_CPX32 || datatype == DATATYPE_CPX64 )
Thomas Jahns's avatar
Thomas Jahns committed
882
        number2 = CDI_COMP;
883
      else
Thomas Jahns's avatar
Thomas Jahns committed
884
        number2 = CDI_REAL;
885
886

      if ( number2 != number )
Thomas Jahns's avatar
Thomas Jahns committed
887
888
889
890
        {
          number = CDI_BOTH;
          break;
        }
891
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
892
893
894
895

  return (number);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
896
897
898
899
900
901
/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

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

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

@Result
908
@func{vlistNgrids} returns the number of grids in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
909
910
911
912
913

@EndFunction
*/
int vlistNgrids(int vlistID)
{
914
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
915
916
917

  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
926
927
928

  return (vlistptr->ngrids);
}

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

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

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

@Result
935
@func{vlistNzaxis} returns the number of zaxis in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
936
937
938
939
940

@EndFunction
*/
int vlistNzaxis(int vlistID)
{
941
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
942
943
944

  vlistptr = vlist_to_pointer(vlistID);

945
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
946
947
948
949
950
951
952

  return (vlistptr->nzaxis);
}


void vlistDefNtsteps(int vlistID, int nts)
{
953
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
954
955
956

  vlistptr = vlist_to_pointer(vlistID);

957
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
958

959
960
961
962
963
964
  if ( reshGetStatus ( vlistID, &vlist_ops ) == CLOSED )
    {
      xwarning ( "Operation not executed." );
      return;
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
965
966
967
968
969
970
  vlistptr->ntsteps = nts;
}


int vlistNtsteps(int vlistID)
{
971
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
972
973
974

  vlistptr = vlist_to_pointer(vlistID);

975
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
976
977
978
979

  return (vlistptr->ntsteps);
}

980
static void
981
vlistPrintKernel(vlist_t *vlistptr, FILE * fp )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
982
{
983
  int nvars, flag, index;
984
  int varID, fvarID, mvarID, flevID, mlevID, levID;
985
  int param, gridID, zaxisID, timeID, nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
986
  int dtype;
987
988
989

  int i, iorank, decoSize, size, rank, offset, chunk;

990
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
991
992
993
  char *name, *longname, *units;
  double level;

994
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
995

996
  fprintf ( fp, "#\n# vlistID %d\n#\n", vlistptr->self);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
997
998
999

  nvars = vlistptr->nvars;

1000
  fprintf ( fp, "nvars   %d\n", nvars);
For faster browsing, not all history is shown. View entire blame