vlist.c 46.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"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
8
#include "vlist.h"
9
#include "zaxis.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
10
#include "varscan.h"
11
#include "namespace.h"
12
#include "pio_util.h"
13
#include "resource_handle.h"
Thomas Jahns's avatar
Thomas Jahns committed
14
#include "vlist_var.h"
15
#include "vlist_att.h"
16
#include "pio_rpc.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
17

18
19
20
21
22
23
#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

24
25
extern void zaxisGetIndexList ( int, int * );

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

static void vlist_initialize(void);

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

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 int vlistIsInitialized = 0;
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
49
/* 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
50
{
51
52
53
54
55
56
  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
57
58
}

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

64
#ifdef USE_MPI
65
static int  vlistGetSizeP ( void * vlistptr, MPI_Comm comm );
66
67
static void vlistPackP    ( void * vlistptr, void * buff, int size,
                            int *position, MPI_Comm comm );
68
static int  vlistTxCode   ( void );
69
#endif
70

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


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

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

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

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

113
  vlistptr = (vlist_t *)xmalloc(sizeof(vlist_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114

115
  vlist_init_entry(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
117
118
119

  return (vlistptr);
}

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

  idx = vlistptr->self;

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

  free(vlistptr);

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

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

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

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

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

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

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

169
@Prototype int vlistCreate(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170

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

195
196
  cdiInitialize();

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

  vlistptr = vlist_new_entry();

  vlistID = vlistptr->self;

  return (vlistID);
}

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

211
212
  int vlistID = vlistptr->self;

Deike Kleberg's avatar
Deike Kleberg committed
213
  vlistDelAtts(vlistID, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214

215
  int nvars = vlistptr->nvars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216

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

      if ( vlistptr->vars[varID].ensdata )  free(vlistptr->vars[varID].ensdata);
Thomas Jahns's avatar
Thomas Jahns committed
226
      if ( vlistptr->vars[varID].deco )     free(vlistptr->vars[varID].deco);
227

228
#if  defined  (HAVE_LIBGRIB_API)
229
230
231
232
233
234
235
236
237
      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]);
      }
238
#endif
239

Deike Kleberg's avatar
Deike Kleberg committed
240
      vlistDelAtts(vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
    }
242

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

Deike Kleberg's avatar
Deike Kleberg committed
245
  vlist_delete_entry(vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
247
}

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

/*
@Function  vlistDestroy
@Title     Destroy a variable list

@Prototype void vlistDestroy(int vlistID)
@Parameter
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}

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

  vlistptr = vlist_to_pointer(vlistID);

  vlist_delete(vlistptr);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
268
269
270
271
272
273
/*
@Function  vlistCopy
@Title     Copy a variable list

@Prototype void vlistCopy(int vlistID2, int vlistID1)
@Parameter
274
275
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
276
277

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

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

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

289
290
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
291
292
293

  vlist_copy(vlistptr2, vlistptr1);

294
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
295

Uwe Schulzweida's avatar
Uwe Schulzweida committed
296
297
298
299
300
  if ( vlistptr1->vars )
    {
      int nvars = vlistptr1->nvars;
      int nlevs, varID;

301
302
303
      //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
304
305

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

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

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

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

319
320
321
322
323
324
          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));
            }
325
326
327
328
329
330
331
          if ( vlistptr1->vars[varID].deco )
            {
              size_t decoSize = (size_t)vlistptr1->vars[varID].decoSize;
              vlistptr2->vars[varID].deco = malloc(decoSize * sizeof (deco_t));
              memcpy(vlistptr2->vars[varID].deco, vlistptr1->vars[varID].deco,
                     decoSize * sizeof (deco_t));
            }
332

333
#if  defined  (HAVE_LIBGRIB_API)
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
          /* ---------------------------------- */
          /* 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];
	    }
	  }
353
#endif
354

355
356
	  vlistptr2->vars[varID].atts.nelems = 0;
	  vlistCopyVarAtts(vlistID1, varID, vlistID2, varID);
357
358
359
360
361

          nlevs = vlistptr1->vars[varID].nlevs;
          vlistptr2->vars[varID].levinfo = (levinfo_t *) malloc(nlevs*sizeof(levinfo_t));
          memcpy(vlistptr2->vars[varID].levinfo,
                 vlistptr1->vars[varID].levinfo, nlevs*sizeof(levinfo_t));
362
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
363
364
365
366
367
368
369
370
371
    }
}

/*
@Function  vlistDuplicate
@Title     Duplicate a variable list

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

@Description
375
The function @func{vlistDuplicate} duplicates the variable list from vlistID1.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
377

@Result
378
@func{vlistDuplicate} returns an identifier to the duplicated variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
380
381
382
383
384

@EndFunction
*/
int vlistDuplicate(int vlistID)
{
  int vlistIDnew;
385
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
386
387
388

  vlistptr = vlist_to_pointer(vlistID);

389
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390

391
  vlistIDnew = vlistCreate();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392
393
394
395
396
397
398
399
400
401

  vlistCopy(vlistIDnew, vlistID);

  return (vlistIDnew);
}


void vlistClearFlag(int vlistID)
{
  int varID, levID;
402
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
403
404
405
406
407
408
409

  vlistptr = vlist_to_pointer(vlistID);

  for ( varID = 0; varID < vlistptr->nvars; varID++ )
    {
      vlistptr->vars[varID].flag = FALSE;
      for ( levID = 0; levID < vlistptr->vars[varID].nlevs; levID++ )
Thomas Jahns's avatar
Thomas Jahns committed
410
411
412
        {
          vlistptr->vars[varID].levinfo[levID].flag = FALSE;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413
414
415
    }
}

416
417
418
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
419
420
421
422
423
424
{
  int zaxisdefined;
  int nzaxis;
  int zaxisID = CDI_UNDEFID;
  int index;
  int zaxisglobdefined = 0;
425
  int has_bounds = FALSE;
426
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427
428
429

  vlistptr = vlist_to_pointer(vlistID);

430
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
431
432
433
434

  zaxisdefined = 0;
  nzaxis = vlistptr->nzaxis;

435
436
437
  if ( lbounds && ubounds ) has_bounds = TRUE;

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

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

  if ( ! zaxisdefined )
    {
      nzaxis = zaxisSize();
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
      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
467
468
469
470
471
    }

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

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

      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
504
505
    @Item  vlistID2  Target variable list ID
    @Item  vlistID1  Source variable list ID
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507

@Description
508
The function @func{vlistCopyFlag} copies all entries with a flag from vlistID1 to vlistID2.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
510
511
512
513

@EndFunction
*/
void vlistCopyFlag(int vlistID2, int vlistID1)
{
514
  vlist_t *vlistptr1, *vlistptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
515
516
517
518

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

519
520
  vlist_check_ptr(__func__, vlistptr1);
  vlist_check_ptr(__func__, vlistptr2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
521
522

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

524
  vlistCopyVarAtts(vlistID1, CDI_GLOBAL, vlistID2, CDI_GLOBAL);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
525

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

      vlistptr2->nvars = nvars2;
541
      vlistptr2->varsAllocated = nvars2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542
      if ( nvars2 > 0 )
Thomas Jahns's avatar
Thomas Jahns committed
543
        vlistptr2->vars  = (var_t *) malloc(nvars2*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
544
      else
Thomas Jahns's avatar
Thomas Jahns committed
545
        vlistptr2->vars  = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
546
547
548

      varID2 = 0;
      for ( varID = 0; varID < nvars; varID++ )
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
	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);

574
575
576
577
578
579
580
            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));
              }

581
#if  defined  (HAVE_LIBGRIB_API)
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
	    /* ---------------------------------- */
	    /* 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];
	      }
	    }
601
#endif
602
603
604
605

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

606
607
608
609
610
611
612
613
614
615
616
617
618
	    nlevs  = vlistptr1->vars[varID].nlevs;
	    nlevs2 = 0;
	    for ( levID = 0; levID < nlevs; levID++ )
	      if ( vlistptr1->vars[varID].levinfo[levID].flag ) nlevs2++;

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

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

		zaxisID = vlistptr1->vars[varID].zaxisID;
		levels = (double *) malloc(nlevs2*sizeof(double));
		levID2 = 0;
626
		for ( levID = 0; levID < nlevs; ++levID )
627
628
629
630
631
632
633
634
635
636
637
638
639
640
		  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);
		  }
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
667

                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);
668
		free(levels);
669
670
                if ( lbounds ) free(lbounds);
                if ( ubounds ) free(ubounds);
671

672
673
674
675
676
677
                zaxisInqName(zaxisID, ctemp);
                zaxisDefName(zaxisID2, ctemp);
                zaxisInqLongname(zaxisID, ctemp);
                zaxisDefLongname(zaxisID2, ctemp);
                zaxisInqUnits(zaxisID, ctemp);
                zaxisDefUnits(zaxisID2, ctemp);
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
719
720

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

	    for ( levID = 0; levID < nlevs2; levID++ )
	      {
		vlistptr2->vars[varID2].levinfo[levID].flag  = FALSE;
		vlistptr2->vars[varID2].levinfo[levID].index = -1;
	      }

	    levID2 = 0;
	    for ( levID = 0; levID < nlevs; levID++ )
	      if ( vlistptr1->vars[varID].levinfo[levID].flag )
		{
		  vlistptr2->vars[varID2].levinfo[levID2].flevelID = levID;
		  vlistptr2->vars[varID2].levinfo[levID2].mlevelID = levID;
		  levID2++;
		}

	    for ( index = 0; index <vlistptr2->ngrids; index++ )
	      if (vlistptr2->gridIDs[index] == gridID ) break;

	    if ( index == vlistptr2->ngrids )
	      {
		vlistptr2->gridIDs[vlistptr2->ngrids++] = gridID;
		if (vlistptr2->ngrids >= MAX_GRIDS_PS )
		  Error("Internal Problem! More than %d grids.", MAX_GRIDS_PS);
	      }

	    for ( index = 0; index < vlistptr2->nzaxis; index++ )
	      if ( vlistptr2->zaxisIDs[index] == zaxisID ) break;

	    if ( index == vlistptr2->nzaxis )
	      {
		vlistptr2->zaxisIDs[vlistptr2->nzaxis++] = zaxisID;
		if (vlistptr2->nzaxis >= MAX_ZAXES_PS )
		  Error("Internal Problem! More than %d zaxis.", MAX_ZAXES_PS);
	      }

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

/*
@Function  vlistCat
@Title     Concatenate two variable lists

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

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

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

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

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

756
757
758
759
760
  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
761
  memcpy(vlistptr2->vars+nvars2, vlistptr1->vars, nvars1*sizeof(var_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
762
763
764
765
766
767
768

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

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

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

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

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

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

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

      nlevs = vlistptr1->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
793
794
      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
795

796
797
798
799
800
801
      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));
        }

802
#if  defined  (HAVE_LIBGRIB_API)
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
      /* ---------------------------------- */
      /* 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];
	}
      }
822
#endif
823

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

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

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

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

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

/*
@Function  vlistMerge
@Title     Merge two variable lists

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

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

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

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

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

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

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

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

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

Thomas Jahns's avatar
Thomas Jahns committed
911
912
          nlevs1 = vlistptr1->vars[varID].nlevs;
          nlevs2 = vlistptr2->vars[varID].nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
913

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

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

923
924
	  memcpy(vlistptr2->vars[varID].levinfo+nlevs2,
		 vlistptr1->vars[varID].levinfo, nlevs1*sizeof(levinfo_t));
925

926
927
928
929
930
	  for ( levID = 0; levID < nlevs1; levID++ )
	    {
	      vlistptr1->vars[varID].levinfo[levID].mlevelID = nlevs2 + levID;
	    }
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
931
932
933
934
935

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

          zaxisID1 = vlistptr1->vars[varID].zaxisID;
          zaxisID2 = vlistptr2->vars[varID].zaxisID;
          /*
          nlevs1 = vlistptr1->vars[varID].nlevs;
          nlevs2 = vlistptr2->vars[varID].nlevs;
          */
          nlevs1 = zaxisInqSize(zaxisID1);
          nlevs2 = zaxisInqSize(zaxisID2);
          /*
          fprintf(stderr, "zaxis %d %d %d %d\n", zaxisID1, zaxisID2, nlevs1, nlevs2);
          */
          nlevs = nlevs1 + nlevs2;

          zaxisID = zaxisDuplicate(zaxisID2);

          zaxisResize(zaxisID, nlevs);

          levels = (double *) malloc(nlevs1*sizeof(double));

          zaxisInqLevels(zaxisID1, levels);
          /*
          for ( levID = 0; levID < nlevs1; levID++ )
            fprintf(stderr, "%d %d %d %d %d %g\n", varID, levID, nlevs1, nlevs2, vlistptr2->vars[varID].nlevs, levels[levID]);
          */
          for ( levID = 0; levID < nlevs1; levID++ )
            zaxisDefLevel(zaxisID, nlevs2+levID, levels[levID]);

          free(levels);

          for ( index = 0; index < vlistptr2->nzaxis; index++ )
            if ( vlistptr2->zaxisIDs[index] == zaxisID2 )
              vlistptr2->zaxisIDs[index] = zaxisID;

          for ( varID2 = 0; varID2 < nvars2; varID2++ )
            if ( lvar[varID2] == FALSE && vlistptr2->vars[varID2].zaxisID == zaxisID2 )
              {
                vlistptr2->vars[varID2].zaxisID = zaxisID;
                lvar[varID2] = TRUE;
              }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993

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

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

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

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

@Result
1000
@func{vlistNvars} returns the number of variables in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1001
1002
1003
1004
1005

@EndFunction
*/
int vlistNvars(int vlistID)
{
1006
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1007
1008
1009

  vlistptr = vlist_to_pointer(vlistID);

1010
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1011
1012
1013
1014

  return (vlistptr->nvars);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1015

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1016
1017
1018
int vlistNrecs(int vlistID)
{
  int varID, nrecs = 0;
1019
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1020
1021
1022

  vlistptr = vlist_to_pointer(vlistID);

1023
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1024
1025
1026
1027
1028
1029
1030
1031

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

  return (nrecs);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
1032
1033
int vlistNumber(int vlistID)
{
1034
  int varID, number, number2, datatype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1035
1036
1037
1038
  vlist_t *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

1039
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1040

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

      if ( number2 != number )
Thomas Jahns's avatar
Thomas Jahns committed
1056
1057
1058
1059
        {
          number = CDI_BOTH;
          break;
        }
1060
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1061
1062
1063
1064

  return (number);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1065
1066
1067
1068
1069
1070
/*
@Function  vlistNgrids
@Title     Number of grids in a variable list

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

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

@Result
1077
@func{vlistNgrids} returns the number of grids in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1078
1079
1080
1081
1082

@EndFunction
*/
int vlistNgrids(int vlistID)
{
1083
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1084
1085
1086

  vlistptr = vlist_to_pointer(vlistID);

1087
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097

  return (vlistptr->ngrids);
}

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

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

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

@Result
1104
@func{vlistNzaxis} returns the number of zaxis in a variable list.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1105
1106
1107
1108
1109

@EndFunction
*/
int vlistNzaxis(int vlistID)
{
1110
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1111
1112
1113

  vlistptr = vlist_to_pointer(vlistID);

1114
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1115
1116
1117
1118
1119
1120
1121

  return (vlistptr->nzaxis);
}


void vlistDefNtsteps(int vlistID, int nts)
{
1122
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1123
1124
1125

  vlistptr = vlist_to_pointer(vlistID);

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

1128
1129
  if ( reshGetStatus ( vlistID, &vlist_ops ) == CLOSED )
    {
1130
      xwarning("%s", "Operation not executed." );
1131
1132
1133
      return;
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1134
1135
1136
1137
1138
1139
  vlistptr->ntsteps = nts;
}


int vlistNtsteps(int vlistID)
{
1140
  vlist_t *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1141
1142
1143

  vlistptr = vlist_to_pointer(vlistID);

1144
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1145
1146
1147
1148

  return (vlistptr->ntsteps);
}

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

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

1159
  char paramstr[32];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1160
1161
1162
  char *name, *longname, *units;
  double level;

1163
  vlist_check_ptr(__func__, vlistptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1164

1165
  fprintf ( fp, "#\n# vlistID %d\n#\n", vlistptr->self);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1166
1167
1168

  nvars = vlistptr->nvars;

1169
1170
1171
1172
1173
1174
1175
  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
1176
1177
1178

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

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

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

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

1228
1229
      fprintf(fp, "\n");
      fprintf(fp, " varID  size iorank decosize corank offset  chunk\n");
1230
1231
1232
1233
1234
1235
      for ( varID = 0; varID < nvars; varID++ )
        {
          iorank    = vlistptr->vars[varID].iorank;
          decoSize  = vlistptr->vars[varID].decoSize;
          size      = vlistptr->vars[varID].nlevs *
            gridInqSize ( vlistptr->vars[varID].gridID );
1236

1237
1238
1239
1240
1241
1242
1243
1244
          for ( i = 0; i < decoSize; i++ )
            {
              rank   = vlistptr->vars[varID].deco[i].rank;
              offset = vlistptr->vars[varID].deco[i].offset;
              chunk  = vlistptr->vars[varID].deco[i].chunk;
              fprintf ( fp, "%3d %8d %6d %6d %6d %8d %6d\n",
                        varID, size, iorank, decoSize, rank, offset, chunk );
            }
1245
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1246
1247
1248
    }
}

1249
1250
1251
1252
1253
1254
1255
1256
1257

void vlistPrint(int vlistID)
{
  vlist_t *vlistptr;

  if ( vlistID == CDI_UNDEFID ) return;

  vlistptr = vlist_to_pointer(vlistID);
  vlist_check_ptr(__func__, vlistptr);
1258
  vlistPrintKernel(vlistptr, stdout);
1259
1260
}

1261
1262
1263
1264
1265
1266
1267
/*
@Function  vlistDefTaxis
@Title     Define the time axis

@Prototype void vlistDefTaxis(int vlistID, int taxisID)
@Parameter
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1268
    @Item  taxisID  Time axis ID, from a previous call to @fref{taxisCreate}
1269
1270
1271
1272
1273
1274

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

@EndFunction
*/