vlist.c 45.5 KB
Newer Older
1
2
#ifdef  HAVE_CONFIG_H
#include "config.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
3
4
5
6
#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 "cdi_att.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
16

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

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

26
static int VLIST_Debug = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28
29

static void vlist_initialize(void);

30
31
#ifdef  HAVE_LIBPTHREAD
#include <pthread.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
32

33
static pthread_once_t  _vlist_init_thread = PTHREAD_ONCE_INIT;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34

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

#else

40
static bool vlistIsInitialized = false;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
41

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


47
48
static int
vlist_compare(vlist_t *a, vlist_t *b)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
{
50
  int diff = (a->nvars != b->nvars) | (a->ngrids != b->ngrids)
Thomas Jahns's avatar
Thomas Jahns committed
51
52
53
    | (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);
54

Thomas Jahns's avatar
Thomas Jahns committed
55
56
57
  int nvars = a->nvars;
  for (int varID = 0; varID < nvars; ++varID)
    diff |= vlistVarCompare(a, varID, b, varID);
58

59
60
  size_t natts = a->atts.nelems;
  for (size_t attID = 0; attID < natts; ++attID)
61
62
    diff |= cdi_att_compare(&a->atts, &a->atts, (int)attID);

63
  return diff;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
64
65
}

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

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

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


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

93
94
static
void vlist_init_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
{
96
97
  vlistptr->immutable      = 0;
  vlistptr->internal       = 0;
98
  vlistptr->self           = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
100
101
102
  vlistptr->nvars          = 0;
  vlistptr->vars           = NULL;
  vlistptr->ngrids         = 0;
  vlistptr->nzaxis         = 0;
103
  vlistptr->taxisID        = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
104
105
106
107
108
  vlistptr->instID         = cdiDefaultInstID;
  vlistptr->modelID        = cdiDefaultModelID;
  vlistptr->tableID        = cdiDefaultTableID;
  vlistptr->varsAllocated  = 0;
  vlistptr->ntsteps        = CDI_UNDEFID;
109
110
  vlistptr->keys.nalloc    = MAX_KEYS;
  vlistptr->keys.nelems    = 0;
111
112
  for ( int i = 0; i < MAX_KEYS; ++i )
    vlistptr->keys.value[i].length = 0;
113
  vlistptr->atts.nalloc    = MAX_ATTRIBUTES;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
  vlistptr->atts.nelems    = 0;
115
  vlistptr->nsubtypes      = 0;
116
  for ( int i = 0; i < MAX_SUBTYPES_PS; i++ )
117
    vlistptr->subtypeIDs[i] = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
}

120
static
121
vlist_t *vlist_new_entry(cdiResH resH)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
{
123
  vlist_t *vlistptr = (vlist_t*) Malloc(sizeof(vlist_t));
124
  vlist_init_entry(vlistptr);
125
  if (resH == CDI_UNDEFID)
Thomas Jahns's avatar
Thomas Jahns committed
126
    vlistptr->self = reshPut(vlistptr, &vlistOps);
127
128
129
  else
    {
      vlistptr->self = resH;
Thomas Jahns's avatar
Thomas Jahns committed
130
      reshReplace(resH, vlistptr, &vlistOps);
131
    }
132
  return vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
133
134
}

135
136
static
void vlist_delete_entry(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
{
138
  int idx = vlistptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139

Thomas Jahns's avatar
Thomas Jahns committed
140
  reshRemove(idx, &vlistOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141

142
  Free(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
144

  if ( VLIST_Debug )
145
    Message("Removed idx %d from vlist list", idx);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
147
}

148
149
static
void vlist_initialize(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
150
{
151
  char *env = getenv("VLIST_DEBUG");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
  if ( env ) VLIST_Debug = atoi(env);
153
#ifndef HAVE_LIBPTHREAD
154
  vlistIsInitialized = true;
155
#endif
Uwe Schulzweida's avatar
Uwe Schulzweida committed
156
157
}

158
159
static
void vlist_copy(vlist_t *vlistptr2, vlist_t *vlistptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
{
161
  int vlistID2 = vlistptr2->self;
162
  int vlist2internal = vlistptr2->internal;
163
  memcpy(vlistptr2, vlistptr1, sizeof(vlist_t));
164
165
  vlistptr2->internal = vlist2internal;    //the question who's responsible to destroy the vlist is tied to its containing memory region, so we retain this flag
  vlistptr2->immutable = 0;    //this is a copy, so it's mutable, independent of whether the original is mutable or not
166
  vlistptr2->keys.nelems = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
  vlistptr2->atts.nelems = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
169
170
  vlistptr2->self = vlistID2;
}

171
void cdiVlistMakeInternal(int vlistID)
172
{
173
  vlist_to_pointer(vlistID)->internal = 1;
174
175
}

176
void cdiVlistMakeImmutable(int vlistID)
177
{
178
  vlist_to_pointer(vlistID)->immutable = 1;
179
180
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
181
/*
182
@Function  vlistCreate
Uwe Schulzweida's avatar
Uwe Schulzweida committed
183
184
@Title     Create a variable list

185
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186

Uwe Schulzweida's avatar
Uwe Schulzweida committed
187
188
@Example
Here is an example using @func{vlistCreate} to create a variable list
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189
and add a variable with @func{vlistDefVar}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
190
191
192
193
194
195
196

@Source
#include "cdi.h"
   ...
int vlistID, varID;
   ...
vlistID = vlistCreate();
197
varID = vlistDefVar(vlistID, gridID, zaxisID, TIME_VARYING);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
198
199
200
201
202
203
   ...
streamDefVlist(streamID, vlistID);
   ...
vlistDestroy(vlistID);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
205
@EndFunction
*/
206
int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
{
208
209
  cdiInitialize();

210
  VLIST_INIT();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
211

212
  vlist_t *vlistptr = vlist_new_entry(CDI_UNDEFID);
213
  if ( CDI_Debug ) Message("create vlistID = %d", vlistptr->self);
214
  return vlistptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
216
}

217
218
static
void vlist_delete(vlist_t *vlistptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
219
{
220
  int vlistID = vlistptr->self;
221
  if ( CDI_Debug ) Message("call to vlist_delete, vlistID = %d", vlistID);
222

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

226
  int nvars = vlistptr->nvars;
227
  var_t *vars = vlistptr->vars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
228

229
  for ( int varID = 0; varID < nvars; varID++ )
Deike Kleberg's avatar
Deike Kleberg committed
230
    {
231
232
233
234
235
      if ( vars[varID].levinfo )  Free(vars[varID].levinfo);
      if ( vars[varID].name )     Free(vars[varID].name);
      if ( vars[varID].longname ) Free(vars[varID].longname);
      if ( vars[varID].stdname )  Free(vars[varID].stdname);
      if ( vars[varID].units )    Free(vars[varID].units);
236

237
      if ( vlistptr->vars[varID].opt_grib_kvpair )
238
        {
239
240
241
242
243
          for ( int i = 0; i<vlistptr->vars[varID].opt_grib_nentries; i++ )
            {
              if ( vlistptr->vars[varID].opt_grib_kvpair[i].keyword )
                Free(vlistptr->vars[varID].opt_grib_kvpair[i].keyword);
            }
244
          Free(vlistptr->vars[varID].opt_grib_kvpair);
245
        }
246
247
248
      vlistptr->vars[varID].opt_grib_nentries    = 0;
      vlistptr->vars[varID].opt_grib_kvpair_size = 0;
      vlistptr->vars[varID].opt_grib_kvpair      = NULL;
249

250
251
      cdiDeleteKeys(vlistID, varID);
      cdiDeleteAtts(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
    }
253

254
  if ( vars ) Free(vars);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255

Deike Kleberg's avatar
Deike Kleberg committed
256
  vlist_delete_entry(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
257
258
}

259
260
261
262
263
264
265

/*
@Function  vlistDestroy
@Title     Destroy a variable list

@Prototype void vlistDestroy(int vlistID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
266
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}.
267
268
269
270
271

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

274
275
  if ( vlistptr->internal )
    Warning("Attempt to destroy an internal vlist object by the user (vlistID=%d).", vlistID);
276
277
  else
    vlist_delete(vlistptr);
278
279
}

280
281
282
283
284
285
286
287
288
289
290
291
292
// destroy an internal vlist object
void cdiVlistDestroy_(int vlistID)
{
  vlist_t *vlistptr = vlist_to_pointer(vlistID);

  if(!vlistptr->internal)
    Warning("Destroying a vlist object that is owned by the user.\n"
            "This is most likely because of a missing vlistDestroy() in the application code.\n"
            "If that's not the case, and you are absolutely certain about it, please report the bug.");

  vlist_delete(vlistptr);
}

293
294
295
296
297
298
299
static
void var_copy_entries(var_t *var2, var_t *var1)
{
  if ( var1->name )     var2->name     = strdupx(var1->name);
  if ( var1->longname ) var2->longname = strdupx(var1->longname);
  if ( var1->stdname )  var2->stdname  = strdupx(var1->stdname);
  if ( var1->units )    var2->units    = strdupx(var1->units);
300

301
302
303
304
  var2->opt_grib_kvpair_size = 0;
  var2->opt_grib_kvpair      = NULL;
  var2->opt_grib_nentries    = 0;

305
  resize_opt_grib_entries(var2, var1->opt_grib_nentries);
306
  var2->opt_grib_nentries = var1->opt_grib_nentries;
307
308
309
  if ((var2->opt_grib_nentries > 0) && CDI_Debug )
    Message("copy %d optional GRIB keywords", var2->opt_grib_nentries);

310
  for (int i=0; i<var1->opt_grib_nentries; i++) {
311
312
313
314
315
    if ( CDI_Debug )  Message("copy entry \"%s\" ...", var1->opt_grib_kvpair[i].keyword);
    var2->opt_grib_kvpair[i].keyword = NULL;
    if ( var1->opt_grib_kvpair[i].keyword != NULL ) {
      var2->opt_grib_kvpair[i]         = var1->opt_grib_kvpair[i];
      var2->opt_grib_kvpair[i].keyword = strdupx(var1->opt_grib_kvpair[i].keyword);
316
      var2->opt_grib_kvpair[i].update  = true;
317
318
319
320
      if ( CDI_Debug )  Message("done.");
    }
    else {
      if ( CDI_Debug )  Message("not done.");
321
    }
322
  }
323
324
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
325
326
327
328
329
330
/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
331
332
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
333
334

@Description
335
The function @func{vlistCopy} copies all entries from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
336
337
338
339
340

@EndFunction
*/
void vlistCopy(int vlistID2, int vlistID1)
{
341
342
  vlist_t *vlistptr1 = vlist_to_pointer(vlistID1);
  vlist_t *vlistptr2 = vlist_to_pointer(vlistID2);
343
  if ( CDI_Debug ) Message("call to vlistCopy, vlistIDs %d -> %d", vlistID1, vlistID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344

345
346
  var_t *vars1 = vlistptr1->vars;
  var_t *vars2 = vlistptr2->vars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
348
  vlist_copy(vlistptr2, vlistptr1);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
349
  vlistptr2->keys.nelems = 0;
350
  cdiCopyKeys(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
351
  vlistptr2->atts.nelems = 0;
352
  cdiCopyAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353

354
  if ( vars1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
355
356
    {
      int nvars = vlistptr1->nvars;
357
      //vlistptr2->varsAllocated = nvars;
358
359

      size_t n = (size_t)vlistptr2->varsAllocated;
360
      vars2 = (var_t *) Realloc(vars2, n*sizeof(var_t));
361
362
      memcpy(vars2, vars1, n*sizeof(var_t));
      vlistptr2->vars = vars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
363

364
      for ( int varID = 0; varID < nvars; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
365
        {
366
          var_copy_entries(&vars2[varID], &vars1[varID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
          vlistptr2->vars[varID].keys.nelems = 0;
368
	  cdiCopyKeys(vlistID1, varID, vlistID2, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
369

370
          vlistptr2->vars[varID].atts.nelems = 0;
371
	  cdiCopyAtts(vlistID1, varID, vlistID2, varID);
372

373
          if ( vars1[varID].levinfo )
374
            {
375
              n = (size_t)zaxisInqSize(vars1[varID].zaxisID);
376
              vars2[varID].levinfo = (levinfo_t *) Malloc(n*sizeof(levinfo_t));
377
              memcpy(vars2[varID].levinfo, vars1[varID].levinfo, n*sizeof(levinfo_t));
378
            }
379
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
380
381
382
383
384
385
386
387
388
    }
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

@Description
392
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
394

@Result
395
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
396
397
398
399
400

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
401
402
  if ( CDI_Debug ) Message("call to vlistDuplicate");

403
  int vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
404
  vlistCopy(vlistIDnew, vlistID);
405
  return vlistIDnew;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
407
408
409
410
}


void vlistClearFlag(int vlistID)
{
411
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
412

413
  for ( int varID = 0; varID < vlistptr->nvars; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
414
    {
415
      vlistptr->vars[varID].flag = false;
416
      if ( vlistptr->vars[varID].levinfo )
417
418
        {
          int nlevs = zaxisInqSize(vlistptr->vars[varID].zaxisID);
419
          for ( int levID = 0; levID < nlevs; levID++ )
420
            vlistptr->vars[varID].levinfo[levID].flag = false;
421
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
422
423
424
    }
}

Thomas Jahns's avatar
Thomas Jahns committed
425
426
427
428
429
430

struct vgzSearchState
{
  int resIDValue;
  int zaxistype;
  int nlevels;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
431
  bool lbounds;
Thomas Jahns's avatar
Thomas Jahns committed
432
433
434
435
436
437
438
439
440
  const double *levels;
};

static enum cdiApplyRet
vgzZAxisSearch(int id, void *res, void *data)
{
  struct vgzSearchState *state = (struct vgzSearchState *)data;
  (void)res;
  if (zaxisCompare(id, state->zaxistype, state->nlevels, state->lbounds,
441
                   state->levels, NULL, NULL, 0, -1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
442
      == false)
Thomas Jahns's avatar
Thomas Jahns committed
443
444
445
446
447
448
449
450
    {
      state->resIDValue = id;
      return CDI_APPLY_STOP;
    }
  else
    return CDI_APPLY_GO_ON;
}

451
static
Thomas Jahns's avatar
Thomas Jahns committed
452
int vlist_generate_zaxis(int vlistID, int zaxistype, int nlevels, const double *levels,
453
454
                         const double *lbounds, const double *ubounds, int vctsize, const double *vct,
                         const char **cvals, size_t clen)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
455
456
{
  int zaxisID = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
457
458
459
  bool zaxisdefined = false;
  bool zaxisglobdefined = false;
  bool has_bounds = false;
460
461
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
  int nzaxis = vlistptr->nzaxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
462

Uwe Schulzweida's avatar
Uwe Schulzweida committed
463
  if ( lbounds && ubounds ) has_bounds = true;
464

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

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

  if ( ! zaxisdefined )
    {
Thomas Jahns's avatar
Thomas Jahns committed
478
479
480
481
482
483
484
485
486
487
      struct vgzSearchState query;
      query.zaxistype = zaxistype;
      query.nlevels = nlevels;
      query.levels = levels;
      query.lbounds = has_bounds;

      if ((zaxisglobdefined
           = (cdiResHFilterApply(getZaxisOps(), vgzZAxisSearch, &query)
              == CDI_APPLY_STOP)))
        zaxisID = query.resIDValue;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
488
489
490
491
492
    }

  if ( ! zaxisdefined )
    {
      if ( ! zaxisglobdefined )
493
494
495
	{
	  zaxisID = zaxisCreate(zaxistype, nlevels);
	  zaxisDefLevels(zaxisID, levels);
496
497

          if ( zaxistype == ZAXIS_CHAR )
498
            zaxisDefCvals(zaxisID, cvals, (int)clen);
499

500
	  if ( has_bounds )
501
	    {
502
503
	      zaxisDefLbounds(zaxisID, lbounds);
	      zaxisDefUbounds(zaxisID, ubounds);
504
505
	    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507
	  if ( zaxistype == ZAXIS_HYBRID && vctsize > 0 )
            zaxisDefVct(zaxisID, vctsize, vct);
508
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
510
511
512
513
514

      nzaxis = vlistptr->nzaxis;
      vlistptr->zaxisIDs[nzaxis] = zaxisID;
      vlistptr->nzaxis++;
    }

515
  return zaxisID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
516
517
518
519
520
521
522
523
}

/*
@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
524
525
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
527

@Description
528
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
529
530
531
532
533

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
534
535
536
537
538
  vlist_t *vlistptr1 = vlist_to_pointer(vlistID1);
  vlist_t *vlistptr2 = vlist_to_pointer(vlistID2);
  var_t *vars1 = vlistptr1->vars;
  var_t *vars2 = vlistptr2->vars;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
539
  vlist_copy(vlistptr2, vlistptr1);
Thomas Jahns's avatar
Thomas Jahns committed
540

Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
  vlistptr2->keys.nelems = 0;
542
  cdiCopyKeys(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
  vlistptr2->atts.nelems = 0;
544
  cdiCopyAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
545

Uwe Schulzweida's avatar
Uwe Schulzweida committed
546
547
548
549
550
  if ( vlistptr1->vars )
    {
      vlistptr2->ngrids = 0;
      vlistptr2->nzaxis = 0;

551
552
      int nvars = vlistptr1->nvars;
      int nvars2 = 0;
553
      for ( int varID = 0; varID < nvars; varID++ )
554
        nvars2 += vars1[varID].flag;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
556

      vlistptr2->nvars = nvars2;
557
      vlistptr2->varsAllocated = nvars2;
558
      vars2 = (nvars2 > 0) ? (var_t *) Malloc((size_t)nvars2*sizeof(var_t)) : NULL;
559
560

      vlistptr2->vars = vars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
561

562
      int varID2 = 0;
563
      for ( int varID = 0; varID < nvars; varID++ )
564
	if ( vars1[varID].flag )
565
	  {
566
	    vlistptr2->vars[varID2].flag = false;
567
568
569
	    int zaxisID   = vlistptr1->vars[varID].zaxisID;
	    int gridID    = vlistptr1->vars[varID].gridID;
	    int subtypeID = vlistptr1->vars[varID].subtypeID;
570

571
	    memcpy(&vars2[varID2], &vars1[varID], sizeof(var_t));
572

573
574
	    vars1[varID].fvarID = varID2;
	    vars2[varID2].fvarID = varID;
575

576
	    vars2[varID2].mvarID = varID2;
577

578
            var_copy_entries(&vars2[varID2], &vars1[varID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
579
	    vlistptr2->vars[varID2].keys.nelems = 0;
580
            cdiCopyKeys(vlistID1, varID, vlistID2, varID2);
581

582
	    vlistptr2->vars[varID2].atts.nelems = 0;
583
	    cdiCopyAtts(vlistID1, varID, vlistID2, varID2);
584

585
	    int nlevs  = zaxisInqSize(vars1[varID].zaxisID);
586
	    int nlevs2 = 0;
587
            if ( vars1[varID].levinfo )
588
              for ( int levID = 0; levID < nlevs; levID++ )
589
                nlevs2 += vars1[varID].levinfo[levID].flag;
590

591
	    vars2[varID2].levinfo = (levinfo_t *) Malloc((size_t)nlevs2 * sizeof(levinfo_t));
592
593
594
595

	    if ( nlevs != nlevs2 )
	      {
		int nvct = 0;
596
                double *levels = NULL;
597
		double *lbounds = NULL, *ubounds = NULL;
598
		const double *vct = NULL;
599
                char ctemp[CDI_MAX_NAME];
600

601
602
                if ( !vars1[varID].levinfo ) cdiVlistCreateVarLevInfo(vlistptr1, varID);

603
		zaxisID = vars1[varID].zaxisID;
604
605
606
                int zaxisType = zaxisInqType(zaxisID);

                int levID2 = 0;
607
                for ( int levID = 0; levID < nlevs; levID++ )
608
609
610
611
612
613
                  if ( vars1[varID].levinfo[levID].flag )
                    {
                      vars1[varID].levinfo[levID].flevelID = levID2;
                      vars1[varID].levinfo[levID].mlevelID = levID2;
                    }

614

615
616
617
618
619
620
621
                if ( zaxisInqLevels(zaxisID, NULL) )
                  {
                    levels = (double *) Malloc((size_t)nlevs2 * sizeof (double));

                    levID2 = 0;
                    for ( int levID = 0; levID < nlevs; ++levID )
                      if ( vars1[varID].levinfo[levID].flag )
622
                        levels[levID2++] = zaxisInqLevel(zaxisID, levID);
623
624
625
626
627
628
629
                  }

                if ( zaxisType == ZAXIS_HYBRID )
                  {
                    nvct = zaxisInqVctSize(zaxisID);
                    vct  = zaxisInqVctPtr(zaxisID);
                  }
630

631
632
633
                size_t clen2 = 0;
                char **cvals2 = NULL;
#ifndef USE_MPI
634
635
                if ( zaxisType == ZAXIS_CHAR )
                  {
636
                    char **cvals1 = zaxisInqCValsPtr(zaxisID);
637
                    size_t clen1 = (size_t)zaxisInqCLen(zaxisID);
638
639
640
                    for ( int levID = 0; levID < nlevs; ++levID )
                      if ( vars1[varID].levinfo[levID].flag )
                          {
641
642
                            size_t testlen = clen1;
                            while ( cvals1[levID][testlen] == ' ' )
643
644
645
646
647
648
649
650
651
652
                              testlen--;
                            if ( clen2 < testlen )
                              clen2 = testlen;
                          }
                    cvals2 = (char **) Malloc((size_t)nlevs2 * sizeof (char *));
                    levID2 = 0;

                    for ( int levID = 0; levID < nlevs; ++levID )
                      if ( vars1[varID].levinfo[levID].flag )
                        {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
653
                          cvals2[levID2] = (char*) Malloc((size_t)(clen2) * sizeof(char));
654
                          memcpy(cvals2[levID2], cvals1[levID], clen2*sizeof(char));
655
656
657
                          levID2++;
                        }
                  }
658
#endif
659

660
661
                if ( zaxisInqLbounds(zaxisID, NULL) && zaxisInqUbounds(zaxisID, NULL) )
                  {
662
                    lbounds = (double *) Malloc(2 * (size_t)nlevs2 * sizeof (double));
663
664
                    ubounds = lbounds + nlevs2;

665
                    double *lbounds1 = (double *) Malloc(2 * (size_t)nlevs * sizeof (double)),
Uwe Schulzweida's avatar
Uwe Schulzweida committed
666
                           *ubounds1 = lbounds1 + nlevs;
667
668
669
670

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

671
                    levID2 = 0;
672
                    for ( int levID = 0; levID < nlevs; ++levID )
673
                      if ( vars1[varID].levinfo[levID].flag )
674
675
676
677
678
679
                        {
                          lbounds[levID2] = lbounds1[levID];
                          ubounds[levID2] = ubounds1[levID];
                          levID2++;
                        }

680
                    Free(lbounds1);
681
682
                  }

683
		int zaxisID2 = vlist_generate_zaxis(vlistID2, zaxisType, nlevs2, levels, lbounds, ubounds, nvct, vct, (const char **)cvals2, clen2);
684
685
		if ( levels )  Free(levels);
                if ( lbounds ) Free(lbounds);
686
687
688
689
690
691
                if ( cvals2 )
                  {
                    for ( int levID = 0; levID < nlevs2; ++levID )
                      Free(cvals2[levID]);
                    Free(cvals2);
                  }
692

693
                zaxisInqName(zaxisID, ctemp);
694
                cdiDefKeyString(zaxisID2, CDI_GLOBAL, CDI_KEY_NAME, ctemp);
695
696
697
698
                zaxisInqLongname(zaxisID, ctemp);
                zaxisDefLongname(zaxisID2, ctemp);
                zaxisInqUnits(zaxisID, ctemp);
                zaxisDefUnits(zaxisID2, ctemp);
699
                zaxisDefDatatype(zaxisID2, zaxisInqDatatype(zaxisID));
700
                zaxisDefPositive(zaxisID2, zaxisInqPositive(zaxisID));
701

702
703
                if ( zaxisType == ZAXIS_CHAR )
                  {
704
705
706
                    char dimname[CDI_MAX_NAME+3];
                    int length = sizeof(dimname);
                    cdiInqKeyString(zaxisID, CDI_GLOBAL, CDI_KEY_DIMNAME, dimname, &length);
707
                    if ( dimname[0] == 0 ) { memcpy(dimname, "area_type", 10); dimname[10] = 0; }
708
                    cdiDefKeyString(zaxisID2, CDI_GLOBAL, CDI_KEY_DIMNAME, dimname);
709
                  }
710

711
                if ( zaxisType == ZAXIS_GENERIC ) cdiCopyKey(zaxisID, CDI_GLOBAL, CDI_KEY_TYPEOFFIRSTFIXEDSURFACE, zaxisID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
712

713
714
                cdiCopyAtts(zaxisID, CDI_GLOBAL, zaxisID2, CDI_GLOBAL);

715
		zaxisID = zaxisID2;
716
		vars2[varID2].zaxisID = zaxisID2;
717
718
	      }

719
	    for ( int levID = 0; levID < nlevs2; levID++ )
720
	      {
721
		vars2[varID2].levinfo[levID].flag  = false;
722
		vars2[varID2].levinfo[levID].index = -1;
723
724
	      }

725
726
	    int levID2 = 0;
	    for ( int levID = 0; levID < nlevs; levID++ )
727
	      if ( vars1[varID].levinfo[levID].flag )
728
		{
729
		  vars2[varID2].levinfo[levID2].flevelID = levID;
730
		  vars2[varID2].levinfo[levID2].mlevelID = levID2;
731
732
733
		  levID2++;
		}

734
            vlistAdd2GridIDs(vlistptr2, gridID);
735
            vlistAdd2ZaxisIDs(vlistptr2, zaxisID);
736
            vlistAdd2SubtypeIDs(vlistptr2, subtypeID);
737
738
739

	    varID2++;
	  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
740
741
742
743
744
745
746
747
748
    }
}

/*
@Function  vlistCat
@Title     Concatenate two variable lists

@Prototype void vlistCat(int vlistID2, int vlistID1)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
749
750
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
751
752
753
754
755
756
757
758

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

@EndFunction
*/
void vlistCat(int vlistID2, int vlistID1)
{
759
760
761
762
  vlist_t *vlistptr1 = vlist_to_pointer(vlistID1);
  vlist_t *vlistptr2 = vlist_to_pointer(vlistID2);
  var_t *vars1 = vlistptr1->vars;
  var_t *vars2 = vlistptr2->vars;
763
764
765
  int nvars1 = vlistptr1->nvars;
  int nvars2 = vlistptr2->nvars;
  int nvars = nvars1 + nvars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
766
767
  vlistptr2->nvars = nvars;

768
769
770
  if ( nvars > vlistptr2->varsAllocated )
    {
      vlistptr2->varsAllocated = nvars;
771
      vars2 = (var_t *) Realloc(vars2, (size_t)nvars*sizeof(var_t));
772
      vlistptr2->vars = vars2;
773
    }
774
  memcpy(vars2+nvars2, vars1, (size_t)nvars1 * sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
775

776
  for ( int varID = 0; varID < nvars1; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
777
    {
778
      int varID2 = varID + nvars2;
779
780
      vars1[varID].fvarID = varID2;
      vars2[varID2].fvarID = varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
781

782
783
      vars1[varID].mvarID = varID2;
      vars2[varID2].mvarID = varID;
784

785
      if ( vars1[varID].param < 0 )
786
787
	{
	  int pnum, pcat, pdis;
788
	  cdiDecodeParam(vars1[varID].param, &pnum, &pcat, &pdis);
789
	  pnum = -(varID2+1);
790
	  vars2[varID2].param = cdiEncodeParam(pnum, pcat, pdis);
791
	}
792

793
      var_copy_entries(&vars2[varID2], &vars1[varID]);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
794
      vars2[varID2].keys.nelems = 0;
795
      cdiCopyKeys(vlistID1, varID, vlistID2, varID2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
796

797
      if ( vars1[varID].levinfo )
798
        {
Thomas Jahns's avatar
Thomas Jahns committed
799
800
          size_t nlevs = (size_t)zaxisInqSize(vars1[varID].zaxisID);
          vars2[varID2].levinfo = (levinfo_t *) Malloc(nlevs * sizeof(levinfo_t));
801
          memcpy(vars2[varID2].levinfo, vars1[varID].levinfo,
Thomas Jahns's avatar
Thomas Jahns committed
802
                 nlevs * sizeof(levinfo_t));
803
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
804

805
      vars2[varID2].atts.nelems = 0;
806
      cdiCopyAtts(vlistID1, varID, vlistID2, varID2);
807

808
809
      vlistAdd2GridIDs(vlistptr2, vars1[varID].gridID);
      vlistAdd2ZaxisIDs(vlistptr2, vars1[varID].zaxisID);
810
      vlistAdd2SubtypeIDs(vlistptr2, vars1[varID].subtypeID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
811
812
813
814
815
816
817
818
819
    }
}

/*
@Function  vlistMerge
@Title     Merge two variable lists

@Prototype void vlistMerge(int vlistID2, int vlistID1)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
820
821
    @Item  vlistID2  Target variable list ID.
    @Item  vlistID1  Source variable list ID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
822
823

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
824
Merge the variable list vlistID1 to the variable list vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
825
826
827
828
829

@EndFunction
*/
void vlistMerge(int vlistID2, int vlistID1)
{
830
  int varID = 0;
831
832
833
834
  vlist_t *vlistptr1 = vlist_to_pointer(vlistID1);
  vlist_t *vlistptr2 = vlist_to_pointer(vlistID2);
  var_t *vars1 = vlistptr1->vars;
  var_t *vars2 = vlistptr2->vars;
835
836
  int nvars1 = vlistptr1->nvars;
  int nvars2 = vlistptr2->nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
837
838
839
840

  if ( nvars1 == nvars2 )
    {
      for ( varID = 0; varID < nvars2; varID++ )
841
	{
842
843
          size_t ngp1 = gridInqSize(vars1[varID].gridID);
          size_t ngp2 = gridInqSize(vars2[varID].gridID);
844
845
846
          if ( ngp1 != ngp2 ) break;

	  if ( vars1[varID].name && vars2[varID].name )
847
	    {
848
	      if ( strcmp(vars1[varID].name, vars2[varID].name) != 0 ) break;
849
850
851
	    }
	  else
	    {
852
	      if ( vars1[varID].param != vars2[varID].param ) break;
853
854
	    }
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
855
856
857
858
859
    }

  if ( varID == nvars2 ) /* same variables in vlistID1 and vlistID2 */
    {
      for ( varID = 0; varID < nvars2; varID++ )
Thomas Jahns's avatar
Thomas Jahns committed
860
        {
861
862
          vars1[varID].fvarID = varID;
          vars2[varID].fvarID = varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
863

864
865
          vars1[varID].mvarID = varID;
          vars2[varID].mvarID = varID;
866

867
868
          int nlevs1 = zaxisInqSize(vars1[varID].zaxisID);
          int nlevs2 = zaxisInqSize(vars2[varID].zaxisID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
869

870
          int nlevs = nlevs1 + nlevs2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
871

Thomas Jahns's avatar