vlist.c 45.4 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
static int
vlist_compare(vlist_t *a, vlist_t *b)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
51
{
52
  int diff;
Thomas Jahns's avatar
Thomas Jahns committed
53
54
55
56
57
58
59
  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) | (a->atts.nelems != b->atts.nelems);
  int nvars = a->nvars;
  for (int varID = 0; varID < nvars; ++varID)
    diff |= vlistVarCompare(a, varID, b, varID);
60
61
62
  size_t natts = a->atts.nelems;
  for (size_t attID = 0; attID < natts; ++attID)
    diff |= vlist_att_compare(a, CDI_GLOBAL, b, CDI_GLOBAL, (int)attID);
63
  return diff;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
65
}

66
static void
67
vlistPrintKernel(vlist_t *vlistptr, FILE * fp );
68
69
70
static void
vlist_delete(vlist_t *vlistptr);

71
static int  vlistGetSizeP ( void * vlistptr, void *context);
72
static void vlistPackP    ( void * vlistptr, void * buff, int size,
73
                            int *position, void *context);
74
static int  vlistTxCode   ( void );
75

76
77
78
79
#if !defined(__cplusplus)
const
#endif
resOps vlist_ops = {
80
  (valCompareFunc)vlist_compare,
81
  (valDestroyFunc)vlist_delete,
82
83
  (valPrintFunc)vlistPrintKernel
  , vlistGetSizeP,
84
85
  vlistPackP,
  vlistTxCode
86
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
88


89
vlist_t *vlist_to_pointer(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
{
91
  VLIST_INIT();
92
  return (vlist_t*) reshGetVal(vlistID, &vlist_ops );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
94
}

95
96
static
void vlist_init_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
{
98
  vlistptr->locked         = 0;
99
  vlistptr->self           = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
100
101
102
103
  vlistptr->nvars          = 0;
  vlistptr->vars           = NULL;
  vlistptr->ngrids         = 0;
  vlistptr->nzaxis         = 0;
104
  vlistptr->taxisID        = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
106
107
108
109
  vlistptr->instID         = cdiDefaultInstID;
  vlistptr->modelID        = cdiDefaultModelID;
  vlistptr->tableID        = cdiDefaultTableID;
  vlistptr->varsAllocated  = 0;
  vlistptr->ntsteps        = CDI_UNDEFID;
110
  vlistptr->atts.nalloc    = MAX_ATTRIBUTES;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111
  vlistptr->atts.nelems    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
112
113
}

114
static
115
vlist_t *vlist_new_entry(cdiResH resH)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
  vlist_t *vlistptr = (vlist_t*) xmalloc(sizeof(vlist_t));
118
  vlist_init_entry(vlistptr);
119
120
121
122
123
124
125
  if (resH == CDI_UNDEFID)
    vlistptr->self = reshPut(vlistptr, &vlist_ops);
  else
    {
      vlistptr->self = resH;
      reshReplace(resH, vlistptr, &vlist_ops);
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127
128
  return (vlistptr);
}

129
130
static
void vlist_delete_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
{
Thomas Jahns's avatar
Thomas Jahns committed
132
  int idx;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
134
135

  idx = vlistptr->self;

136
  reshRemove(idx, &vlist_ops );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
140

  free(vlistptr);

  if ( VLIST_Debug )
141
    Message("Removed idx %d from vlist list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
}

144
145
static
void vlist_initialize(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
147
148
149
150
{
  char *env;

  env = getenv("VLIST_DEBUG");
  if ( env ) VLIST_Debug = atoi(env);
151
152
153
#ifndef HAVE_LIBPTHREAD
  vlistIsInitialized = TRUE;
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
155
}

156
157
static
void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
158
{
159
  int vlistID2 = vlistptr2->self;
160
  memcpy(vlistptr2, vlistptr1, sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161
  vlistptr2->atts.nelems = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
163
164
  vlistptr2->self = vlistID2;
}

165
166
static
void vlist_check_ptr(const char *caller, vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
168
{
  if ( vlistptr == NULL )
169
    Errorc("vlist undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
}

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

void vlist_lock(int vlistID)
{
  vlist_t *vlistptr = vlist_to_pointer(vlistID);

  vlist_check_ptr(__func__, vlistptr);

  if ( !vlistptr->locked )
    {
      vlistptr->locked = 1;
      reshSetStatus(vlistID, &vlist_ops, RESH_DESYNC_IN_USE);
    }
}


void vlist_unlock(int vlistID)
{
  vlist_t *vlistptr = vlist_to_pointer(vlistID);

  vlist_check_ptr(__func__, vlistptr);

  if ( vlistptr->locked )
    {
      vlistptr->locked = 0;
      reshSetStatus(vlistID, &vlist_ops, RESH_DESYNC_IN_USE);
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
200
/*
201
@Function  vlistCreate
Uwe Schulzweida's avatar
Uwe Schulzweida committed
202
203
@Title     Create a variable list

204
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
205

Uwe Schulzweida's avatar
Uwe Schulzweida committed
206
207
208
209
210
211
212
213
214
215
@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();
216
varID = vlistDefVar(vlistID, gridID, zaxisID, TSTEP_INSTANT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
217
218
219
220
221
222
   ...
streamDefVlist(streamID, vlistID);
   ...
vlistDestroy(vlistID);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
223
224
@EndFunction
*/
225
int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
226
{
227
228
  cdiInitialize();

229
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
230

231
232
  vlist_t *vlistptr = vlist_new_entry(CDI_UNDEFID);
  return (vlistptr->self);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
233
234
}

235
236
static void
vlist_delete(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
{
238
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239

240
241
  int vlistID = vlistptr->self;

Deike Kleberg's avatar
Deike Kleberg committed
242
  vlistDelAtts(vlistID, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243

244
  int nvars = vlistptr->nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245

246
  for (int varID = 0; varID < nvars; varID++ )
Deike Kleberg's avatar
Deike Kleberg committed
247
248
249
250
251
252
    {
      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);
253
254

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

256
#if  defined  (HAVE_LIBGRIB_API)
257
      for (int i=0; i<vlistptr->vars[varID].opt_grib_int_nentries; i++) {
258
259
260
	if ( vlistptr->vars[varID].opt_grib_int_keyword[i] )
	  free(vlistptr->vars[varID].opt_grib_int_keyword[i]);
      }
261
      for (int i=0; i<vlistptr->vars[varID].opt_grib_dbl_nentries; i++) {
262
263
264
	if ( vlistptr->vars[varID].opt_grib_dbl_keyword[i] )
	  free(vlistptr->vars[varID].opt_grib_dbl_keyword[i]);
      }
265
#endif
266

Deike Kleberg's avatar
Deike Kleberg committed
267
      vlistDelAtts(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
268
    }
269

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

Deike Kleberg's avatar
Deike Kleberg committed
272
  vlist_delete_entry(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273
274
}

275
276
277
278
279
280
281

/*
@Function  vlistDestroy
@Title     Destroy a variable list

@Prototype void vlistDestroy(int vlistID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}.
283
284
285
286
287

@EndFunction
*/
void vlistDestroy(int vlistID)
{
288
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
289

290
291
292
293
  if ( vlistptr->locked )
    Warning("Destroying of a locked object (vlistID=%d) failed!", vlistID);
  else
    vlist_delete(vlistptr);
294
295
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
296
297
298
299
300
301
/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
302
303
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
304
305

@Description
306
The function @func{vlistCopy} copies all entries from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
307
308
309
310
311

@EndFunction
*/
void vlistCopy(int vlistID2, int vlistID1)
{
312
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
314
315
316

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

317
318
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
319

Thomas Jahns's avatar
Thomas Jahns committed
320
  var_t *vlist2vars = vlistptr2->vars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
321
322
  vlist_copy(vlistptr2, vlistptr1);

323
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324

Uwe Schulzweida's avatar
Uwe Schulzweida committed
325
326
327
328
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;

329
      //vlistptr2->varsAllocated = nvars;
330
331
332
333
334
      vlistptr2->vars
        = xrealloc(vlist2vars,
                   (size_t)vlistptr2->varsAllocated * sizeof (var_t));
      memcpy(vlistptr2->vars, vlistptr1->vars,
             (size_t)vlistptr2->varsAllocated * sizeof (var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335

336
      for ( int varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
337
338
339
        {
          if ( vlistptr1->vars[varID].name )
            vlistptr2->vars[varID].name = strdupx(vlistptr1->vars[varID].name);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
340

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

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

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

350
351
352
353
354
355
          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));
            }
356
#if  defined  (HAVE_LIBGRIB_API)
357
358
359
360
361
          /* ---------------------------------- */
          /* Local change: 2013-01-28, FP (DWD) */
          /* ---------------------------------- */

	  vlistptr2->vars[varID].opt_grib_int_nentries = vlistptr1->vars[varID].opt_grib_int_nentries;
362
	  for (int i=0; i<vlistptr1->vars[varID].opt_grib_int_nentries; i++) {
363
364
365
	    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];
366
	      vlistptr2->vars[varID].opt_grib_int_update[i]  = TRUE;
367
368
369
	    }
	  }
	  vlistptr2->vars[varID].opt_grib_dbl_nentries = vlistptr1->vars[varID].opt_grib_dbl_nentries;
370
	  for (int i=0; i<vlistptr1->vars[varID].opt_grib_dbl_nentries; i++) {
371
372
373
	    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];
374
	      vlistptr2->vars[varID].opt_grib_dbl_update[i]  = TRUE;
375
376
	    }
	  }
377
#endif
378

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

382
383
          if ( vlistptr1->vars[varID].levinfo )
            {
384
385
386
387
              size_t nlevs
                = (size_t)zaxisInqSize(vlistptr1->vars[varID].zaxisID);
              vlistptr2->vars[varID].levinfo
                = xmalloc(nlevs * sizeof (levinfo_t));
388
              memcpy(vlistptr2->vars[varID].levinfo,
389
390
                     vlistptr1->vars[varID].levinfo,
                     nlevs * sizeof (levinfo_t));
391
            }
392
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
394
395
396
397
398
399
400
401
    }
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

@Description
405
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
407

@Result
408
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409
410
411
412
413
414

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  int vlistIDnew;
415
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
416

417
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
418

419
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
420
421
422
423
424
425
426
427
428
429

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


void vlistClearFlag(int vlistID)
{
  int varID, levID;
430
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
431
432
433
434

  for ( varID = 0; varID < vlistptr->nvars; varID++ )
    {
      vlistptr->vars[varID].flag = FALSE;
435
      if ( vlistptr->vars[varID].levinfo )
436
437
438
439
440
441
442
        {
          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
443
444
445
    }
}

446
447
448
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
449
450
451
{
  int zaxisID = CDI_UNDEFID;
  int zaxisglobdefined = 0;
452
  int has_bounds = FALSE;
453
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
454

455
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
456

457
458
  int zaxisdefined = 0;
  int nzaxis = vlistptr->nzaxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
459

460
461
  if ( lbounds && ubounds ) has_bounds = TRUE;

462
  for ( int index = 0; index < nzaxis; ++index )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
463
464
465
    {
      zaxisID = vlistptr->zaxisIDs[index];

466
      if ( zaxisCompare(zaxisID, zaxistype, nlevels, has_bounds, levels, NULL, NULL, 0) == 0 )
Thomas Jahns's avatar
Thomas Jahns committed
467
468
469
470
        {
          zaxisdefined = 1;
          break;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
471
472
473
474
475
    }

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
476
477
      if ( nzaxis > 0 )
        {
478
          int *zaxisIndexList = (int *)xmalloc((size_t)nzaxis * sizeof (int));
Thomas Jahns's avatar
Thomas Jahns committed
479
          reshLock();
480
          zaxisGetIndexList ( nzaxis, zaxisIndexList );
481
          for ( int index = 0; index < nzaxis; ++index )
482
483
484
485
486
487
488
489
            {
              zaxisID = zaxisIndexList[index];
              if ( zaxisCompare(zaxisID, zaxistype, nlevels, has_bounds, levels, NULL, NULL, 0) == 0 )
                {
                  zaxisglobdefined = 1;
                  break;
                }
            }
Thomas Jahns's avatar
Thomas Jahns committed
490
          reshUnlock();
491
          free(zaxisIndexList);
492
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
493
494
495
496
497
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
498
499
500
	{
	  zaxisID = zaxisCreate(zaxistype, nlevels);
	  zaxisDefLevels(zaxisID, levels);
501
	  if ( has_bounds )
502
	    {
503
504
	      zaxisDefLbounds(zaxisID, lbounds);
	      zaxisDefUbounds(zaxisID, ubounds);
505
506
507
508
509
510
511
512
513
514
	    }

	  if ( zaxistype == ZAXIS_HYBRID )
	    {
	      if ( vctsize > 0 )
		zaxisDefVct(zaxisID, vctsize, vct);
	      else
		Warning("VCT missing");
	    }
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529

      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
530
531
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
532
533

@Description
534
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
536
537
538
539

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
540
541
  vlist_t *vlistptr1 = vlist_to_pointer(vlistID1),
    *vlistptr2 = vlist_to_pointer(vlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542

543
544
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
545
546

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

548
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
549

Uwe Schulzweida's avatar
Uwe Schulzweida committed
550
551
552
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
553
554
      int nvars2 = 0;
      int varID2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
556
557
558

      vlistptr2->ngrids = 0;
      vlistptr2->nzaxis = 0;

559
560
      for ( int varID = 0; varID < nvars; varID++ )
        nvars2 += (vlistptr1->vars[varID].flag != 0);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
561
562

      vlistptr2->nvars = nvars2;
563
      vlistptr2->varsAllocated = nvars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
564
      if ( nvars2 > 0 )
565
        vlistptr2->vars  = (var_t *)xmalloc((size_t)nvars2*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
566
      else
Thomas Jahns's avatar
Thomas Jahns committed
567
        vlistptr2->vars  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
568
569

      varID2 = 0;
570
      for ( int varID = 0; varID < nvars; varID++ )
571
572
573
	if ( vlistptr1->vars[varID].flag )
	  {
	    vlistptr2->vars[varID2].flag = FALSE;
574
575
	    int zaxisID = vlistptr1->vars[varID].zaxisID;
	    int gridID  = vlistptr1->vars[varID].gridID;
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595

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

596
597
            if ( vlistptr1->vars[varID].ensdata )
              {
598
                vlistptr2->vars[varID2].ensdata = (ensinfo_t *)xmalloc(sizeof(ensinfo_t));
599
600
601
602
                memcpy(vlistptr2->vars[varID2].ensdata,
                       vlistptr1->vars[varID].ensdata, sizeof(ensinfo_t));
              }

603
#if  defined  (HAVE_LIBGRIB_API)
604
605
606
607
608
609
610
611
612
613
	    /* ---------------------------------- */
	    /* 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];
614
                vlistptr2->vars[varID2].opt_grib_int_update[i]  = TRUE;
615
616
617
618
619
620
621
	      }
	    }
	    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];
622
                vlistptr2->vars[varID2].opt_grib_dbl_update[i]  = TRUE;
623
624
	      }
	    }
625
#endif
626
627
628
629

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

630
631
	    int nlevs  = zaxisInqSize(vlistptr1->vars[varID].zaxisID);
	    int nlevs2 = 0;
632
            if ( vlistptr1->vars[varID].levinfo )
633
634
              for ( int levID = 0; levID < nlevs; levID++ )
                nlevs2 += (vlistptr1->vars[varID].levinfo[levID].flag != 0);
635

636
	    vlistptr2->vars[varID2].levinfo = (levinfo_t *)xmalloc((size_t)nlevs2 * sizeof (levinfo_t));
637
638
639
640

	    if ( nlevs != nlevs2 )
	      {
		int nvct = 0;
641
		double *lbounds = NULL, *ubounds = NULL;
642
		const double *vct = NULL;
643
                char ctemp[CDI_MAX_NAME];
644
645

		zaxisID = vlistptr1->vars[varID].zaxisID;
646
		double *levels = (double *)xmalloc((size_t)nlevs2 * sizeof (double));
647
                int levID2 = 0;
648
649
                if (!vlistptr1->vars[varID].levinfo)
                  cdiVlistCreateVarLevInfo(vlistptr1, varID);
650
651
652
653
654
655
656
657
                for ( int 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);
                    }
		int zaxisType = zaxisInqType(zaxisID);
658
659
660
661
662
663

		if ( zaxisType == ZAXIS_HYBRID )
		  {
		    nvct = zaxisInqVctSize(zaxisID);
		    vct  = zaxisInqVctPtr(zaxisID);
		  }
664
665
666

                if ( zaxisInqLbounds(zaxisID, NULL) && zaxisInqUbounds(zaxisID, NULL) )
                  {
667
                    lbounds = (double *)xmalloc(2 * (size_t)nlevs2 * sizeof (double));
668
669
                    ubounds = lbounds + nlevs2;

670
                    double *lbounds1 = (double *)xmalloc(2 * (size_t)nlevs * sizeof (double)),
671
                      *ubounds1 = lbounds1 + nlevs;
672
673
674
675

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

676
677
                    int levID2 = 0;
                    for ( int levID = 0; levID < nlevs; ++levID )
678
679
680
681
682
683
684
685
686
687
                      if ( vlistptr1->vars[varID].levinfo[levID].flag )
                        {
                          lbounds[levID2] = lbounds1[levID];
                          ubounds[levID2] = ubounds1[levID];
                          levID2++;
                        }

                    free(lbounds1);
                  }

688
		int zaxisID2 = vlist_generate_zaxis(vlistID2, zaxisType, nlevs2, levels, lbounds, ubounds, nvct, vct);
689
		free(levels);
690
                free(lbounds);
691

692
693
694
695
696
697
                zaxisInqName(zaxisID, ctemp);
                zaxisDefName(zaxisID2, ctemp);
                zaxisInqLongname(zaxisID, ctemp);
                zaxisDefLongname(zaxisID2, ctemp);
                zaxisInqUnits(zaxisID, ctemp);
                zaxisDefUnits(zaxisID2, ctemp);
698
699
700
701
702

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

703
	    for ( int levID = 0; levID < nlevs2; levID++ )
704
705
706
707
708
	      {
		vlistptr2->vars[varID2].levinfo[levID].flag  = FALSE;
		vlistptr2->vars[varID2].levinfo[levID].index = -1;
	      }

709
710
	    int levID2 = 0;
	    for ( int levID = 0; levID < nlevs; levID++ )
711
712
713
714
715
716
717
	      if ( vlistptr1->vars[varID].levinfo[levID].flag )
		{
		  vlistptr2->vars[varID2].levinfo[levID2].flevelID = levID;
		  vlistptr2->vars[varID2].levinfo[levID2].mlevelID = levID;
		  levID2++;
		}

718
            vlistAdd2GridIDs(vlistptr2, gridID);
719
            vlistAdd2ZaxisIDs(vlistptr2, zaxisID);
720
721
722

	    varID2++;
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
723
724
725
726
727
728
729
730
731
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

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

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

@EndFunction
*/
void vlistCat(int vlistID2, int vlistID1)
{
742
743
  vlist_t *vlistptr1 = vlist_to_pointer(vlistID1),
    *vlistptr2 = vlist_to_pointer(vlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
744

745
746
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
747

748
749
750
  int nvars1 = vlistptr1->nvars;
  int nvars2 = vlistptr2->nvars;
  int nvars = nvars1 + nvars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
751
752
  vlistptr2->nvars = nvars;

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

762
  for (int varID = 0; varID < nvars1; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
763
    {
764
      int varID2 = varID + nvars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
765
766
767
      vlistptr1->vars[varID].fvarID = varID2;
      vlistptr2->vars[varID2].fvarID = varID;

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

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

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

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

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

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

791
      int nlevs = zaxisInqSize(vlistptr1->vars[varID].zaxisID);
792
793
      if (vlistptr1->vars[varID].levinfo)
        {
794
795
796
797
798
          vlistptr2->vars[varID2].levinfo
            = (levinfo_t *)xmalloc((size_t)nlevs * sizeof (levinfo_t));
          memcpy(vlistptr2->vars[varID2].levinfo,
                 vlistptr1->vars[varID].levinfo,
                 (size_t)nlevs * sizeof (levinfo_t));
799
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
800

801
802
803
804
805
806
      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));
        }

807
#if  defined  (HAVE_LIBGRIB_API)
808
809
810
811
812
      /* ---------------------------------- */
      /* Local change: 2013-01-28, FP (DWD) */
      /* ---------------------------------- */

      vlistptr2->vars[varID2].opt_grib_int_nentries = vlistptr1->vars[varID].opt_grib_int_nentries;
813
814
      int n = vlistptr1->vars[varID].opt_grib_int_nentries;
      for (int i = 0; i < n; ++i) {
815
816
817
	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];
818
          vlistptr2->vars[varID2].opt_grib_int_update[i]  = TRUE;
819
820
821
	}
      }
      vlistptr2->vars[varID2].opt_grib_dbl_nentries = vlistptr1->vars[varID].opt_grib_dbl_nentries;
822
823
      n = vlistptr1->vars[varID].opt_grib_dbl_nentries;
      for (int i = 0; i < n; i++) {
824
825
826
	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];
827
          vlistptr2->vars[varID2].opt_grib_dbl_update[i]  = TRUE;
828
829
	}
      }
830
#endif
831

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

835
836
      vlistAdd2GridIDs(vlistptr2, vlistptr1->vars[varID].gridID);
      vlistAdd2ZaxisIDs(vlistptr2, vlistptr1->vars[varID].zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
837
838
839
840
841
842
843
844
845
    }
}

/*
@Function  vlistMerge
@Title     Merge two variable lists

@Prototype void vlistMerge(int vlistID2, int vlistID1)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
846
847
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
848
849

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
850
Merge the variable list vlistID1 to the variable list vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
851
852
853
854
855

@EndFunction
*/
void vlistMerge(int vlistID2, int vlistID1)
{
856
857
858
  int varID = 0;
  vlist_t *vlistptr1 = vlist_to_pointer(vlistID1),
    *vlistptr2 = vlist_to_pointer(vlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
859

860
861
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
862

863
864
  int nvars1 = vlistptr1->nvars;
  int nvars2 = vlistptr2->nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
865
866
867
868

  if ( nvars1 == nvars2 )
    {
      for ( varID = 0; varID < nvars2; varID++ )
869
870
871
872
873
874
875
876
877
878
879
880
	{
	  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
881
882
883
884
885
    }

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

Thomas Jahns's avatar
Thomas Jahns committed
890
891
          vlistptr1->vars[varID].mvarID = varID;
          vlistptr2->vars[varID].mvarID = varID;
892

893
894
          int nlevs1 = zaxisInqSize(vlistptr1->vars[varID].zaxisID);
          int nlevs2 = zaxisInqSize(vlistptr2->vars[varID].zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
895

896
          int nlevs = nlevs1 + nlevs2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
897

Thomas Jahns's avatar
Thomas Jahns committed
898
899
900
          /*
          fprintf(stderr, "var %d %d %d %d %d\n", varID, nlevs1, nlevs2, nlevs, sizeof(levinfo_t));
          */
901
902
          if (vlistptr1->vars[varID].levinfo)
            {
903
904
905
              vlistptr2->vars[varID].levinfo =
                (levinfo_t*)xrealloc(vlistptr2->vars[varID].levinfo,
                                     (size_t)nlevs * sizeof(levinfo_t));
906

907
              memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
908
909
                     vlistptr1->vars[varID].levinfo,
                     (size_t)nlevs1 * sizeof (levinfo_t));
910
911
912
            }
          else
            cdiVlistCreateVarLevInfo(vlistptr1, varID);
913
	  for ( int levID = 0; levID < nlevs1; levID++ )
914
915
916
917
	    {
	      vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
	    }
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
918

919
      int *lvar = (int *)xcalloc((size_t)nvars2, sizeof(int));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
920
921

      for ( varID = 0; varID < nvars2; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
922
923
924
        {
          if ( lvar[varID] == TRUE ) continue;

925
926
          int zaxisID1 = vlistptr1->vars[varID].zaxisID;
          int zaxisID2 = vlistptr2->vars[varID].zaxisID;
Thomas Jahns's avatar
Thomas Jahns committed
927
          /*
928
929
          nlevs1 = zaxisInqSize(vlistptr1->vars[varID].zaxisID);
          nlevs2 = zaxisInqSize(vlistptr2->vars[varID].zaxisID);
Thomas Jahns's avatar
Thomas Jahns committed
930
          */
931
932
          int nlevs1 = zaxisInqSize(zaxisID1);
          int nlevs2 = zaxisInqSize(zaxisID2);
Thomas Jahns's avatar
Thomas Jahns committed
933
934
935
          /*
          fprintf(stderr, "zaxis %d %d %d %d\n", zaxisID1, zaxisID2, nlevs1, nlevs2);
          */
936
          int nlevs = nlevs1 + nlevs2;
Thomas Jahns's avatar
Thomas Jahns committed
937

938
          int zaxisID = zaxisDuplicate(zaxisID2);
Thomas Jahns's avatar
Thomas Jahns committed
939
940
941

          zaxisResize(zaxisID, nlevs);

942
          double *levels = (double *)xmalloc((size_t)nlevs1 * sizeof(double));
Thomas Jahns's avatar
Thomas Jahns committed
943
944
945
946
947
948

          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]);
          */
949
          for ( int levID = 0; levID < nlevs1; levID++ )
Thomas Jahns's avatar
Thomas Jahns committed
950
951
952
953
            zaxisDefLevel(zaxisID, nlevs2+levID, levels[levID]);

          free(levels);

954
          for ( int index = 0; index < vlistptr2->nzaxis; index++ )
Thomas Jahns's avatar
Thomas Jahns committed
955
956
957
            if ( vlistptr2->zaxisIDs[index] == zaxisID2 )
              vlistptr2->zaxisIDs[index] = zaxisID;

958
          for ( int varID2 = 0; varID2 < nvars2; varID2++ )
Thomas Jahns's avatar
Thomas Jahns committed
959
960
961
962
963
964
            if ( lvar[varID2] == FALSE && vlistptr2->vars[varID2].zaxisID == zaxisID2 )
              {
                vlistptr2->vars[varID2].zaxisID = zaxisID;
                lvar[varID2] = TRUE;
              }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979

      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
980
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
981
982

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

@Result
986
@func{vlistNvars} returns the number of variables in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
987
988
989
990
991

@EndFunction
*/
int vlistNvars(int vlistID)
{
992
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
993

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

  return (vlistptr->nvars);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
999

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1000
1001
int vlistNrecs(int vlistID)
{
1002
1003
  int nrecs = 0;
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1004

1005
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1006

1007
  for ( int varID = 0; varID < vlistptr->nvars; varID++ )
1008
    nrecs +=  zaxisInqSize(vlistptr->vars[varID].zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1009
1010
1011
1012
1013

  return (nrecs);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
1014
1015
int vlistNumber(int vlistID)
{
1016
1017
  int number, number2, datatype;
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1018

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

1021
1022
1023
1024
1025
1026
  datatype = vlistptr->vars[0].datatype;
  if (  datatype== DATATYPE_CPX32 || datatype == DATATYPE_CPX64 )
    number = CDI_COMP;
  else
    number = CDI_REAL;

1027
  for ( int varID = 1; varID < vlistptr->nvars; varID++ )
1028
1029
1030
    {
      datatype = vlistptr->vars[varID].datatype;
      if ( datatype == DATATYPE_CPX32 || datatype == DATATYPE_CPX64 )
Thomas Jahns's avatar
Thomas Jahns committed
1031
        number2 = CDI_COMP;
1032
      else
Thomas Jahns's avatar
Thomas Jahns committed
1033
        number2 = CDI_REAL;
1034
1035

      if ( number2 != number )
Thomas Jahns's avatar
Thomas Jahns committed
1036
1037
1038
1039
        {
          number = CDI_BOTH;
          break;
        }
1040
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1041
1042
1043
1044

  return (number);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1045
1046
1047
1048
1049
1050
/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

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

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

@Result
1057
@func{vlistNgrids} returns the number of grids in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1058
1059
1060
1061
1062

@EndFunction
*/
int vlistNgrids(int vlistID)
{
1063
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1064

1065
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075

  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
1076
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1077
1078

@Description
1079
The function @func{vlistNzaxis} returns the number of zaxis in the variable list vlistID.