vlist.c 45.7 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include "dmemory.h"
#include "cdi.h"
7
#include "cdi_int.h"
8
#include "error.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
9
#include "vlist.h"
10
#include "zaxis.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
11
#include "varscan.h"
12
#include "namespace.h"
13
#include "resource_handle.h"
Thomas Jahns's avatar
Thomas Jahns committed
14
#include "vlist_var.h"
15
#include "vlist_att.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
16

17
18
19
#include "resource_unpack.h"
#include "serialize.h"

20
21
22
23
24
25
#if  defined  (HAVE_LIBGRIB_API)
/* list of additional GRIB2 keywords which are read by the open process */
int    cdiNAdditionalGRIBKeys = 0;
char*  cdiAdditionalGRIBKeys[MAX_OPT_GRIB_ENTRIES];
#endif

26
27
extern void zaxisGetIndexList ( int, int * );

28
static int VLIST_Debug = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
29
30
31
32
33
34

static void vlist_initialize(void);

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

35
static pthread_once_t  _vlist_init_thread = PTHREAD_ONCE_INIT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36

37
#  define VLIST_INIT()        \
38
  pthread_once(&_vlist_init_thread, vlist_initialize)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
39
40
41

#else

42
static int vlistIsInitialized = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43

44
#  define VLIST_INIT()               \
45
  if ( !vlistIsInitialized ) vlist_initialize()
46
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
48


49
50
51
/* 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
52
{
53
54
55
56
57
58
  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
59
60
}

61
static void
62
vlistPrintKernel(vlist_t *vlistptr, FILE * fp );
63
64
65
static void
vlist_delete(vlist_t *vlistptr);

66
static int  vlistGetSizeP ( void * vlistptr, void *context);
67
static void vlistPackP    ( void * vlistptr, void * buff, int size,
68
                            int *position, void *context);
69
static int  vlistTxCode   ( void );
70

71
const resOps vlist_ops = {
72
  (valCompareFunc)vlist_compare,
73
  (valDestroyFunc)vlist_delete,
74
75
  (valPrintFunc)vlistPrintKernel
  , vlistGetSizeP,
76
77
  vlistPackP,
  vlistTxCode
78
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80


81
vlist_t *vlist_to_pointer(int code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
82
{
83
  VLIST_INIT();
84
  return reshGetVal(code, &vlist_ops );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
86
}

87
88
static
void vlist_init_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
89
{
90
  vlistptr->self           = reshPut(vlistptr, &vlist_ops);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
92
93
94
95

  vlistptr->nvars          = 0;
  vlistptr->vars           = NULL;
  vlistptr->ngrids         = 0;
  vlistptr->nzaxis         = 0;
96
  vlistptr->taxisID        = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
98
99
100
101
  vlistptr->instID         = cdiDefaultInstID;
  vlistptr->modelID        = cdiDefaultModelID;
  vlistptr->tableID        = cdiDefaultTableID;
  vlistptr->varsAllocated  = 0;
  vlistptr->ntsteps        = CDI_UNDEFID;
102
  vlistptr->atts.nalloc    = MAX_ATTRIBUTES;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
  vlistptr->atts.nelems    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
}

106
107
static
vlist_t *vlist_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
108
{
109
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
110

111
  vlistptr = (vlist_t *)xmalloc(sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
112

113
  vlist_init_entry(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
116
117

  return (vlistptr);
}

118
119
static
void vlist_delete_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
120
{
Thomas Jahns's avatar
Thomas Jahns committed
121
  int idx;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
123
124

  idx = vlistptr->self;

125
  reshRemove(idx, &vlist_ops );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127
128
129

  free(vlistptr);

  if ( VLIST_Debug )
130
    Message("Removed idx %d from vlist list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
}

133
134
static
void vlist_initialize(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
136
137
138
139
{
  char *env;

  env = getenv("VLIST_DEBUG");
  if ( env ) VLIST_Debug = atoi(env);
140
141
142
#ifndef HAVE_LIBPTHREAD
  vlistIsInitialized = TRUE;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
144
}

145
146
static
void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
147
148
149
150
{
  int vlistID2;

  vlistID2 = vlistptr2->self;
151
  memcpy(vlistptr2, vlistptr1, sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
  vlistptr2->atts.nelems = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
153
154
155
  vlistptr2->self = vlistID2;
}

156
157
static
void vlist_check_ptr(const char *caller, vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
159
{
  if ( vlistptr == NULL )
160
    Errorc("vlist undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161
162
163
}

/*
164
@Function  vlistCreate
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
@Title     Create a variable list

167
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168

Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
170
171
172
173
174
175
176
177
178
@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();
179
varID = vlistDefVar(vlistID, gridID, zaxisID, TSTEP_INSTANT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
180
181
182
183
184
185
   ...
streamDefVlist(streamID, vlistID);
   ...
vlistDestroy(vlistID);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
187
@EndFunction
*/
188
int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
190
{
  int vlistID = 0;
191
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
192

193
194
  cdiInitialize();

195
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
196
197
198
199
200
201
202
203

  vlistptr = vlist_new_entry();

  vlistID = vlistptr->self;

  return (vlistID);
}

204
205
static void
vlist_delete(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
206
{
207
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
208

209
210
  int vlistID = vlistptr->self;

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

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

215
  for (int varID = 0; varID < nvars; varID++ )
Deike Kleberg's avatar
Deike Kleberg committed
216
217
218
219
220
221
    {
      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);
222
223

      if ( vlistptr->vars[varID].ensdata )  free(vlistptr->vars[varID].ensdata);
224

225
#if  defined  (HAVE_LIBGRIB_API)
226
227
228
229
230
231
232
233
234
      int i;
      for (i=0; i<vlistptr->vars[varID].opt_grib_int_nentries; i++) {
	if ( vlistptr->vars[varID].opt_grib_int_keyword[i] )
	  free(vlistptr->vars[varID].opt_grib_int_keyword[i]);
      }
      for (i=0; i<vlistptr->vars[varID].opt_grib_dbl_nentries; i++) {
	if ( vlistptr->vars[varID].opt_grib_dbl_keyword[i] )
	  free(vlistptr->vars[varID].opt_grib_dbl_keyword[i]);
      }
235
#endif
236

Deike Kleberg's avatar
Deike Kleberg committed
237
      vlistDelAtts(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
238
    }
239

Deike Kleberg's avatar
Deike Kleberg committed
240
  if ( vlistptr->vars ) free(vlistptr->vars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241

Deike Kleberg's avatar
Deike Kleberg committed
242
  vlist_delete_entry(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
}

245
246
247
248
249
250
251

/*
@Function  vlistDestroy
@Title     Destroy a variable list

@Prototype void vlistDestroy(int vlistID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}.
253
254
255
256
257
258
259
260
261
262
263
264

@EndFunction
*/
void vlistDestroy(int vlistID)
{
  vlist_t *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlist_delete(vlistptr);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
265
266
267
268
269
270
/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
272
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273
274

@Description
275
The function @func{vlistCopy} copies all entries from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
276
277
278
279
280

@EndFunction
*/
void vlistCopy(int vlistID2, int vlistID1)
{
281
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
283
284
285

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

286
287
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
288
289
290

  vlist_copy(vlistptr2, vlistptr1);

291
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292

Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
294
295
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
296
      int varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
297

298
299
300
      //vlistptr2->varsAllocated = nvars;
      vlistptr2->vars = (var_t *) malloc(vlistptr2->varsAllocated*sizeof(var_t));
      memcpy(vlistptr2->vars, vlistptr1->vars, vlistptr2->varsAllocated*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
301
302

      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
303
304
305
        {
          if ( vlistptr1->vars[varID].name )
            vlistptr2->vars[varID].name = strdupx(vlistptr1->vars[varID].name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306

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

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

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

316
317
318
319
320
321
          if ( vlistptr1->vars[varID].ensdata )
            {
              vlistptr2->vars[varID].ensdata = (ensinfo_t *) malloc(sizeof(ensinfo_t));
              memcpy(vlistptr2->vars[varID].ensdata,
                     vlistptr1->vars[varID].ensdata, sizeof(ensinfo_t));
            }
322
#if  defined  (HAVE_LIBGRIB_API)
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
          /* ---------------------------------- */
          /* Local change: 2013-01-28, FP (DWD) */
          /* ---------------------------------- */

	  int i;
	  vlistptr2->vars[varID].opt_grib_int_nentries = vlistptr1->vars[varID].opt_grib_int_nentries;
	  for (i=0; i<vlistptr1->vars[varID].opt_grib_int_nentries; i++) {
	    if ( vlistptr1->vars[varID].opt_grib_int_keyword[i] ) {
	      vlistptr2->vars[varID].opt_grib_int_keyword[i] = strdupx(vlistptr1->vars[varID].opt_grib_int_keyword[i]);
	      vlistptr2->vars[varID].opt_grib_int_val[i]     = vlistptr1->vars[varID].opt_grib_int_val[i];
	    }
	  }
	  vlistptr2->vars[varID].opt_grib_dbl_nentries = vlistptr1->vars[varID].opt_grib_dbl_nentries;
	  for (i=0; i<vlistptr1->vars[varID].opt_grib_dbl_nentries; i++) {
	    if ( vlistptr1->vars[varID].opt_grib_dbl_keyword[i] ) {
	      vlistptr2->vars[varID].opt_grib_dbl_keyword[i] = strdupx(vlistptr1->vars[varID].opt_grib_dbl_keyword[i]);
	      vlistptr2->vars[varID].opt_grib_dbl_val[i]     = vlistptr1->vars[varID].opt_grib_dbl_val[i];
	    }
	  }
342
#endif
343

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

347
348
          if ( vlistptr1->vars[varID].levinfo )
            {
349
              int nlevs = zaxisInqSize(vlistptr1->vars[varID].zaxisID);
350
351
352
353
              vlistptr2->vars[varID].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
              memcpy(vlistptr2->vars[varID].levinfo,
                     vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
            }
354
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
356
357
358
359
360
361
362
363
    }
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

@Prototype int vlistDuplicate(int vlistID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
365
366

@Description
367
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
369

@Result
370
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371
372
373
374
375
376

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  int vlistIDnew;
377
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
378
379
380

  vlistptr = vlist_to_pointer(vlistID);

381
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
382

383
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
384
385
386
387
388
389
390
391
392
393

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


void vlistClearFlag(int vlistID)
{
  int varID, levID;
394
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
397
398
399
400

  vlistptr = vlist_to_pointer(vlistID);

  for ( varID = 0; varID < vlistptr->nvars; varID++ )
    {
      vlistptr->vars[varID].flag = FALSE;
401
      if ( vlistptr->vars[varID].levinfo )
402
403
404
405
406
407
408
        {
          int nlevs = zaxisInqSize(vlistptr->vars[varID].zaxisID);
          for ( levID = 0; levID < nlevs; levID++ )
            {
              vlistptr->vars[varID].levinfo[levID].flag = FALSE;
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409
410
411
    }
}

412
413
414
static
int vlist_generate_zaxis(int vlistID, int zaxistype, int nlevels, double *levels,
                         double *lbounds, double *ubounds, int vctsize, const double *vct)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
415
416
417
418
419
420
{
  int zaxisdefined;
  int nzaxis;
  int zaxisID = CDI_UNDEFID;
  int index;
  int zaxisglobdefined = 0;
421
  int has_bounds = FALSE;
422
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
423
424
425

  vlistptr = vlist_to_pointer(vlistID);

426
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427
428
429
430

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

431
432
433
  if ( lbounds && ubounds ) has_bounds = TRUE;

  for ( index = 0; index < nzaxis; ++index )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
434
435
436
    {
      zaxisID = vlistptr->zaxisIDs[index];

437
      if ( zaxisCompare(zaxisID, zaxistype, nlevels, has_bounds, levels, NULL, NULL, 0) == 0 )
Thomas Jahns's avatar
Thomas Jahns committed
438
439
440
441
        {
          zaxisdefined = 1;
          break;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
443
444
445
446
    }

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
      if ( nzaxis > 0 )
        {
          int *zaxisIndexList;
          zaxisIndexList = (int *) malloc ( nzaxis * sizeof ( int ));
          zaxisGetIndexList ( nzaxis, zaxisIndexList );
          for ( index = 0; index < nzaxis; ++index )
            {
              zaxisID = zaxisIndexList[index];
              if ( zaxisCompare(zaxisID, zaxistype, nlevels, has_bounds, levels, NULL, NULL, 0) == 0 )
                {
                  zaxisglobdefined = 1;
                  break;
                }
            }
          if ( zaxisIndexList ) free ( zaxisIndexList );
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
463
464
465
466
467
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
468
469
470
	{
	  zaxisID = zaxisCreate(zaxistype, nlevels);
	  zaxisDefLevels(zaxisID, levels);
471
	  if ( has_bounds )
472
	    {
473
474
	      zaxisDefLbounds(zaxisID, lbounds);
	      zaxisDefUbounds(zaxisID, ubounds);
475
476
477
478
479
480
481
482
483
484
	    }

	  if ( zaxistype == ZAXIS_HYBRID )
	    {
	      if ( vctsize > 0 )
		zaxisDefVct(zaxisID, vctsize, vct);
	      else
		Warning("VCT missing");
	    }
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

      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
Uwe Schulzweida's avatar
Uwe Schulzweida committed
500
501
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
503

@Description
504
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
505
506
507
508
509

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
510
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
511
512
513
514

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

515
516
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
518

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

520
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
521

Uwe Schulzweida's avatar
Uwe Schulzweida committed
522
523
524
525
526
527
528
529
530
531
532
533
  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
534
        if ( vlistptr1->vars[varID].flag ) nvars2++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
536

      vlistptr2->nvars = nvars2;
537
      vlistptr2->varsAllocated = nvars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
538
      if ( nvars2 > 0 )
Thomas Jahns's avatar
Thomas Jahns committed
539
        vlistptr2->vars  = (var_t *) malloc(nvars2*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
540
      else
Thomas Jahns's avatar
Thomas Jahns committed
541
        vlistptr2->vars  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
543
544

      varID2 = 0;
      for ( varID = 0; varID < nvars; varID++ )
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
	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);

570
571
572
573
574
575
576
            if ( vlistptr1->vars[varID].ensdata )
              {
                vlistptr2->vars[varID2].ensdata = (ensinfo_t *) malloc(sizeof(ensinfo_t));
                memcpy(vlistptr2->vars[varID2].ensdata,
                       vlistptr1->vars[varID].ensdata, sizeof(ensinfo_t));
              }

577
#if  defined  (HAVE_LIBGRIB_API)
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
	    /* ---------------------------------- */
	    /* Local change: 2013-01-28, FP (DWD) */
	    /* ---------------------------------- */

	    int i;
	    vlistptr2->vars[varID2].opt_grib_int_nentries = vlistptr1->vars[varID].opt_grib_int_nentries;
	    for (i=0; i<vlistptr1->vars[varID].opt_grib_int_nentries; i++) {
	      if ( vlistptr1->vars[varID].opt_grib_int_keyword[i] ) {
		vlistptr2->vars[varID2].opt_grib_int_keyword[i] = strdupx(vlistptr1->vars[varID].opt_grib_int_keyword[i]);
		vlistptr2->vars[varID2].opt_grib_int_val[i]     = vlistptr1->vars[varID].opt_grib_int_val[i];
	      }
	    }
	    vlistptr2->vars[varID2].opt_grib_dbl_nentries = vlistptr1->vars[varID].opt_grib_dbl_nentries;
	    for (i=0; i<vlistptr1->vars[varID].opt_grib_dbl_nentries; i++) {
	      if ( vlistptr1->vars[varID].opt_grib_dbl_keyword[i] ) {
		vlistptr2->vars[varID2].opt_grib_dbl_keyword[i] = strdupx(vlistptr1->vars[varID].opt_grib_dbl_keyword[i]);
		vlistptr2->vars[varID2].opt_grib_dbl_val[i]     = vlistptr1->vars[varID].opt_grib_dbl_val[i];
	      }
	    }
597
#endif
598
599
600
601

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

602
	    nlevs  = zaxisInqSize(vlistptr1->vars[varID].zaxisID);
603
	    nlevs2 = 0;
604
605
606
            if ( vlistptr1->vars[varID].levinfo )
              for ( levID = 0; levID < nlevs; levID++ )
                if ( vlistptr1->vars[varID].levinfo[levID].flag ) nlevs2++;
607
608
609
610
611
612
613
614
615

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

	    if ( nlevs != nlevs2 )
	      {
		int zaxisType;
		int zaxisID2;
		int nvct = 0;
		double *levels;
616
		double *lbounds = NULL, *ubounds = NULL;
617
		const double *vct = NULL;
618
                char ctemp[CDI_MAX_NAME];
619
620
621
622

		zaxisID = vlistptr1->vars[varID].zaxisID;
		levels = (double *) malloc(nlevs2*sizeof(double));
		levID2 = 0;
623
624
                if (!vlistptr1->vars[varID].levinfo)
                  cdiVlistCreateVarLevInfo(vlistptr1, varID);
625
		for ( levID = 0; levID < nlevs; ++levID )
626
627
628
629
630
631
632
633
634
635
636
637
638
639
		  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);
		  }
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666

                if ( zaxisInqLbounds(zaxisID, NULL) && zaxisInqUbounds(zaxisID, NULL) )
                  {
                    double *lbounds1, *ubounds1;
                    lbounds1 = (double *) malloc(nlevs*sizeof(double));
                    ubounds1 = (double *) malloc(nlevs*sizeof(double));

                    zaxisInqLbounds(zaxisID, lbounds1);
                    zaxisInqUbounds(zaxisID, ubounds1);

                    lbounds = (double *) malloc(nlevs2*sizeof(double));
                    ubounds = (double *) malloc(nlevs2*sizeof(double));

                    levID2 = 0;
                    for ( levID = 0; levID < nlevs; ++levID )
                      if ( vlistptr1->vars[varID].levinfo[levID].flag )
                        {
                          lbounds[levID2] = lbounds1[levID];
                          ubounds[levID2] = ubounds1[levID];
                          levID2++;
                        }

                    free(lbounds1);
                    free(ubounds1);
                  }

		zaxisID2 = vlist_generate_zaxis(vlistID2, zaxisType, nlevs2, levels, lbounds, ubounds, nvct, vct);
667
		free(levels);
668
669
                if ( lbounds ) free(lbounds);
                if ( ubounds ) free(ubounds);
670

671
672
673
674
675
676
                zaxisInqName(zaxisID, ctemp);
                zaxisDefName(zaxisID2, ctemp);
                zaxisInqLongname(zaxisID, ctemp);
                zaxisDefLongname(zaxisID2, ctemp);
                zaxisInqUnits(zaxisID, ctemp);
                zaxisDefUnits(zaxisID2, ctemp);
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718

		zaxisID = zaxisID2;
		vlistptr2->vars[varID2].zaxisID = zaxisID2;
	      }

	    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
719
720
721
722
723
724
725
726
727
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

@Prototype void vlistCat(int vlistID2, int vlistID1)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
728
729
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
730
731
732
733
734
735
736
737
738
739
740

@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;
741
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
742
743
744
745

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

746
747
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
748
749
750
751
752
753

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

754
755
756
757
758
  if ( nvars > vlistptr2->varsAllocated )
    {
      vlistptr2->varsAllocated = nvars;
      vlistptr2->vars = (var_t *) realloc(vlistptr2->vars, nvars*sizeof(var_t));
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
759
  memcpy(vlistptr2->vars+nvars2, vlistptr1->vars, nvars1*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
760
761
762
763
764
765
766

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

767
768
769
      vlistptr1->vars[varID].mvarID = varID2;
      vlistptr2->vars[varID2].mvarID = varID;

770
      if ( vlistptr1->vars[varID].param < 0 )
771
772
773
	{
	  int pnum, pcat, pdis;
	  cdiDecodeParam(vlistptr1->vars[varID].param, &pnum, &pcat, &pdis);
774
	  pnum = -(varID2+1);
775
776
	  vlistptr2->vars[varID2].param = cdiEncodeParam(pnum, pcat, pdis);
	}
777

Uwe Schulzweida's avatar
Uwe Schulzweida committed
778
      if ( vlistptr1->vars[varID].name )
Thomas Jahns's avatar
Thomas Jahns committed
779
        vlistptr2->vars[varID2].name = strdupx(vlistptr1->vars[varID].name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
780
781

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

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

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

790
      nlevs = zaxisInqSize(vlistptr1->vars[varID].zaxisID);
791
792
793
794
795
      if (vlistptr1->vars[varID].levinfo)
        {
          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
796

797
798
799
800
801
802
      if ( vlistptr1->vars[varID].ensdata )
        {
          vlistptr2->vars[varID2].ensdata = (ensinfo_t *) malloc(sizeof(ensinfo_t));
          memcpy(vlistptr2->vars[varID2].ensdata, vlistptr1->vars[varID].ensdata, sizeof(ensinfo_t));
        }

803
#if  defined  (HAVE_LIBGRIB_API)
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
      /* ---------------------------------- */
      /* Local change: 2013-01-28, FP (DWD) */
      /* ---------------------------------- */

      int i;
      vlistptr2->vars[varID2].opt_grib_int_nentries = vlistptr1->vars[varID].opt_grib_int_nentries;
      for (i=0; i<vlistptr1->vars[varID].opt_grib_int_nentries; i++) {
	if ( vlistptr1->vars[varID].opt_grib_int_keyword[i] ) {
	  vlistptr2->vars[varID2].opt_grib_int_keyword[i] = strdupx(vlistptr1->vars[varID].opt_grib_int_keyword[i]);
	  vlistptr2->vars[varID2].opt_grib_int_val[i]     = vlistptr1->vars[varID].opt_grib_int_val[i];
	}
      }
      vlistptr2->vars[varID2].opt_grib_dbl_nentries = vlistptr1->vars[varID].opt_grib_dbl_nentries;
      for (i=0; i<vlistptr1->vars[varID].opt_grib_dbl_nentries; i++) {
	if ( vlistptr1->vars[varID].opt_grib_dbl_keyword[i] ) {
	  vlistptr2->vars[varID2].opt_grib_dbl_keyword[i] = strdupx(vlistptr1->vars[varID].opt_grib_dbl_keyword[i]);
	  vlistptr2->vars[varID2].opt_grib_dbl_val[i]     = vlistptr1->vars[varID].opt_grib_dbl_val[i];
	}
      }
823
#endif
824

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
828
829
      gridID = vlistptr1->vars[varID].gridID;
      for ( index = 0; index < vlistptr2->ngrids; index++ )
Thomas Jahns's avatar
Thomas Jahns committed
830
        if ( gridID == vlistptr2->gridIDs[index] ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
831
832

      if ( index == vlistptr2->ngrids )
833
834
835
836
837
	{
	  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
838
839
840

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

      if ( index == vlistptr2->nzaxis )
844
845
846
847
848
	{
	  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
849
850
851
852
853
854
855
856
857
    }
}

/*
@Function  vlistMerge
@Title     Merge two variable lists

@Prototype void vlistMerge(int vlistID2, int vlistID1)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
858
859
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
860
861

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
862
Merge the variable list vlistID1 to the variable list vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
863
864
865
866
867
868
869
870
871
872
873

@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;
874
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
875
876
877
878

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

879
880
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
881
882
883
884
885
886
887

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

  if ( nvars1 == nvars2 )
    {
      for ( varID = 0; varID < nvars2; varID++ )
888
889
890
891
892
893
894
895
896
897
898
899
	{
	  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
900
901
902
903
904
    }

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

Thomas Jahns's avatar
Thomas Jahns committed
909
910
          vlistptr1->vars[varID].mvarID = varID;
          vlistptr2->vars[varID].mvarID = varID;
911

912
913
          nlevs1 = zaxisInqSize(vlistptr1->vars[varID].zaxisID);
          nlevs2 = zaxisInqSize(vlistptr2->vars[varID].zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
914

Thomas Jahns's avatar
Thomas Jahns committed
915
          nlevs = nlevs1 + nlevs2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
916

Thomas Jahns's avatar
Thomas Jahns committed
917
918
919
          /*
          fprintf(stderr, "var %d %d %d %d %d\n", varID, nlevs1, nlevs2, nlevs, sizeof(levinfo_t));
          */
920
921
922
923
          if (vlistptr1->vars[varID].levinfo)
            {
              vlistptr2->vars[varID].levinfo =
                xrealloc(vlistptr2->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
924

925
926
927
928
929
              memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
                     vlistptr1->vars[varID].levinfo, nlevs1*sizeof(levinfo_t));
            }
          else
            cdiVlistCreateVarLevInfo(vlistptr1, varID);
930
931
932
933
934
	  for ( levID = 0; levID < nlevs1; levID++ )
	    {
	      vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
	    }
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
935
936
937
938
939

      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
940
941
942
943
944
945
        {
          if ( lvar[varID] == TRUE ) continue;

          zaxisID1 = vlistptr1->vars[varID].zaxisID;
          zaxisID2 = vlistptr2->vars[varID].zaxisID;
          /*
946
947
          nlevs1 = zaxisInqSize(vlistptr1->vars[varID].zaxisID);
          nlevs2 = zaxisInqSize(vlistptr2->vars[varID].zaxisID);
Thomas Jahns's avatar
Thomas Jahns committed
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
          */
          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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997

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

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

@Prototype int vlistNvars(int vlistID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
998
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
999
1000

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

@Result
1004
@func{vlistNvars} returns the number of variables in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1005
1006
1007
1008
1009

@EndFunction
*/
int vlistNvars(int vlistID)
{
1010
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1011
1012
1013

  vlistptr = vlist_to_pointer(vlistID);

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

  return (vlistptr->nvars);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1019

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1020
1021
1022
int vlistNrecs(int vlistID)
{
  int varID, nrecs = 0;
1023
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1024
1025
1026

  vlistptr = vlist_to_pointer(vlistID);

1027
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1028
1029

  for ( varID = 0; varID < vlistptr->nvars; varID++ )
1030
    nrecs +=  zaxisInqSize(vlistptr->vars[varID].zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1031
1032
1033
1034
1035

  return (nrecs);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
1036
1037
int vlistNumber(int vlistID)
{
1038
  int varID, number, number2, datatype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1039
1040
1041
1042
  vlist_t *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

1043
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1044

1045
1046
1047
1048
1049
1050
  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
1051
  for ( varID = 1; varID < vlistptr->nvars; varID++ )
1052
1053
1054
    {
      datatype = vlistptr->vars[varID].datatype;
      if ( datatype == DATATYPE_CPX32 || datatype == DATATYPE_CPX64 )
Thomas Jahns's avatar
Thomas Jahns committed
1055
        number2 = CDI_COMP;
1056
      else
Thomas Jahns's avatar
Thomas Jahns committed
1057
        number2 = CDI_REAL;
1058
1059

      if ( number2 != number )
Thomas Jahns's avatar
Thomas Jahns committed
1060
1061
1062
1063
        {
          number = CDI_BOTH;
          break;
        }
1064
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1065
1066
1067
1068

  return (number);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1069
1070
1071
1072
1073
1074
/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

@Prototype int vlistNgrids(int vlistID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1075
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1076
1077

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

@Result
1081
@func{vlistNgrids} returns the number of grids in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1082
1083
1084
1085
1086

@EndFunction
*/
int vlistNgrids(int vlistID)
{
1087
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1088
1089
1090

  vlistptr = vlist_to_pointer(vlistID);

1091
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101

  return (vlistptr->ngrids);
}

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

@Prototype int vlistNzaxis(int vlistID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1102
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1103
1104

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

@Result
1108
@func{vlistNzaxis} returns the number of zaxis in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1109
1110
1111
1112
1113

@EndFunction
*/
int vlistNzaxis(int vlistID)
{
1114
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1115
1116
1117

  vlistptr = vlist_to_pointer(vlistID);

1118
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1119
1120
1121
1122
1123
1124
1125

  return (vlistptr->nzaxis);
}


void vlistDefNtsteps(int vlistID, int nts)
{
1126
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1127
1128
1129

  vlistptr = vlist_to_pointer(vlistID);

1130
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1131

1132
  if ( reshGetStatus ( vlistID, &vlist_ops ) == RESH_CLOSED )
1133
    {
1134
      Warning("%s", "Operation not executed." );
1135
1136
1137
      return;
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1138
1139
1140
1141
1142
1143
  vlistptr->ntsteps = nts;
}


int vlistNtsteps(int vlistID)
{
1144
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1145
1146
1147

  vlistptr = vlist_to_pointer(vlistID);

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

  return (vlistptr->ntsteps);
}

1153
static void
1154
vlistPrintKernel(vlist_t *vlistptr, FILE * fp )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1155
{
1156
  int nvars, flag, index;
1157
  int varID, fvarID, mvarID, flevID, mlevID, levID;
1158
  int param, gridID, zaxisID, tsteptype, nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1159
  int dtype;
1160

Thomas Jahns's avatar
Thomas Jahns committed
1161
  int iorank;
1162

1163
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1164
1165
1166
  char *name, *longname, *units;
  double level;

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

1169
  fprintf ( fp, "#\n# vlistID %d\n#\n", vlistptr->self);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1170
1171
1172

  nvars = vlistptr->nvars;

1173
1174
1175
1176
1177
1178
1179
  fprintf ( fp, "nvars   %d\n", nvars);
  fprintf ( fp, "ngrids  %d\n", vlistptr->ngrids);
  fprintf ( fp, "nzaxis  %d\n", vlistptr->nzaxis);
  fprintf ( fp, "taxisID %d\n", vlistptr->taxisID);
  fprintf ( fp, "instID  %d\n", vlistptr->instID);
  fprintf ( fp, "modelID %d\n", vlistptr->modelID);
  fprintf ( fp, "tableID %d\n", vlistptr->tableID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1180
1181
1182

  if ( nvars > 0 )
    {
1183
      fprintf(fp, " varID param    gridID zaxisID tsteptype flag "
1184
              " name     longname iorank\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1185
      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
1186
1187
1188
1189
        {
          param    = vlistptr->vars[varID].param;
          gridID   = vlistptr->vars[varID].gridID;
          zaxisID  = vlistptr->vars[varID].zaxisID;
1190
	  tsteptype= vlistptr->vars[varID].tsteptype;
Thomas Jahns's avatar
Thomas Jahns committed
1191
1192
1193
1194
          name     = vlistptr->vars[varID].name;
          longname = vlistptr->vars[varID].longname;
          units    = vlistptr->vars[varID].units;
          flag     = vlistptr->vars[varID].flag;
1195
          iorank   = vlistptr->vars[varID].iorank;
Thomas Jahns's avatar
Thomas Jahns committed
1196
1197

          cdiParamToString(param, paramstr, sizeof(paramstr));
1198
          fprintf(fp, "%6d %-8s %6d %6d %6d %5d %-8s"
1199
                  " %s %6d",
1200
                  varID, paramstr, gridID, zaxisID, tsteptype, flag,
1201
                  name ? name : "", longname ? longname : "",
1202
                  iorank);
1203

1204
1205
          if ( units ) fprintf ( fp, "   [%s]", units);
          fprintf ( fp, "\n");
Thomas Jahns's avatar
Thomas Jahns committed
1206
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1207

1208
1209
      fprintf(fp, "\n");
      fprintf(fp, " varID  levID fvarID flevID mvarID mlevID  index  dtype  flag  level\n");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1210
      for ( varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
1211
1212
        {
          zaxisID  = vlistptr->vars[varID].zaxisID;
1213
          nlevs    = zaxisInqSize(zaxisID);
Thomas Jahns's avatar
Thomas Jahns committed
1214
1215
1216
1217
1218
          fvarID   = vlistptr->vars[varID].fvarID;
          mvarID   = vlistptr->vars[varID].mvarID;
          dtype    = vlistptr->vars[varID].datatype;
          for ( levID = 0; levID < nlevs; levID++ )
            {
1219
1220
1221
1222
1223
1224
1225
1226
1227
              levinfo_t li;
              if (vlistptr->vars[varID].levinfo)
                li = vlistptr->vars[varID].levinfo[levID];
              else
                li = DEFAULT_LEVINFO(levID);
              flevID = li.flevelID;
              mlevID = li.mlevelID;
              index  = li.index;
              flag   = li.flag;
Thomas Jahns's avatar
Thomas Jahns committed
1228
              level  = zaxisInqLevel(zaxisID, levID);
1229

1230
1231
1232
              fprintf(fp, "%6d %6d %6d %6d %6d %6d %6d %6d %5d  %.9g\n",
                      varID, levID, fvarID, flevID, mvarID, mlevID, index,
                      dtype, flag, level);
Thomas Jahns's avatar
Thomas Jahns committed
1233
1234
            }
        }