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

5
6
#include <limits.h>

Uwe Schulzweida's avatar
Uwe Schulzweida committed
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "dmemory.h"
#include "cdi.h"
#include "stream_int.h"
#include "vlist.h"


static void vlistvarInitEntry(int vlistID, int varID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistptr->vars[varID].fvarID       = varID;
20
  vlistptr->vars[varID].mvarID       = varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22
23
24
  vlistptr->vars[varID].flag         = 0;
  vlistptr->vars[varID].code         = 0;
  vlistptr->vars[varID].timeID       = CDI_UNDEFID;
  vlistptr->vars[varID].datatype     = CDI_UNDEFID;
25
26
  vlistptr->vars[varID].timave       = 0;
  vlistptr->vars[varID].timaccu      = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  vlistptr->vars[varID].gridID       = CDI_UNDEFID;
  vlistptr->vars[varID].zaxisID      = CDI_UNDEFID;
  vlistptr->vars[varID].instID       = CDI_UNDEFID;
  vlistptr->vars[varID].modelID      = CDI_UNDEFID;
  vlistptr->vars[varID].tableID      = CDI_UNDEFID;
  vlistptr->vars[varID].missvalused  = FALSE;
  vlistptr->vars[varID].missval      = cdiDefaultMissval;
  vlistptr->vars[varID].addoffset    = 0.0;
  vlistptr->vars[varID].scalefactor  = 1.0;
  vlistptr->vars[varID].name         = NULL;
  vlistptr->vars[varID].longname     = NULL;
  vlistptr->vars[varID].stdname      = NULL;
  vlistptr->vars[varID].units        = NULL;
  vlistptr->vars[varID].nlevs        = 0;
  vlistptr->vars[varID].levinfo      = NULL;
42
43
  vlistptr->vars[varID].ztype        = COMPRESS_NONE;
  vlistptr->vars[varID].zlevel       = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
44
45
  vlistptr->vars[varID].atts.nalloc  = MAX_ATTRIBUTES;
  vlistptr->vars[varID].atts.nelems  = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
}

static int vlistvarNewEntry(int vlistID)
{
  static char func[] = "vlistvarNewEntry";
  int varID = 0;
  int vlistvarSize;
  VARINFO *vlistvar;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistvarSize = vlistptr->varsAllocated;
  vlistvar     = vlistptr->vars;
  /*
    Look for a free slot in vlistvar.
    (Create the table the first time through).
  */
  if ( ! vlistvarSize )
    {
      int i;

      vlistvarSize = 2;
      vlistvar = (VARINFO *) malloc(vlistvarSize*sizeof(VARINFO));
      if ( vlistvar == NULL )
	{
          Message(func, "vlistvarSize = %d", vlistvarSize);
	  SysError(func, "Allocation of VARINFO failed");
	}

      for ( i = 0; i < vlistvarSize; i++ )
	vlistvar[i].isUsed = FALSE;
    }
  else
    {
      while ( varID < vlistvarSize )
	{
	  if ( ! vlistvar[varID].isUsed ) break;
	  varID++;
	}
    }
  /*
    If the table overflows, double its size.
  */
  if ( varID == vlistvarSize )
    {
      int i;

      vlistvarSize = 2*vlistvarSize;
      vlistvar = (VARINFO *) realloc(vlistvar, vlistvarSize*sizeof(VARINFO));
      if ( vlistvar == NULL )
	{
          Message(func, "vlistvarSize = %d", vlistvarSize);
	  SysError(func, "Reallocation of VARINFO failed");
	}
      varID = vlistvarSize/2;

      for ( i = varID; i < vlistvarSize; i++ )
	vlistvar[i].isUsed = FALSE;
    }

  vlistptr->varsAllocated = vlistvarSize;
  vlistptr->vars          = vlistvar;

  vlistvarInitEntry(vlistID, varID);

  vlistptr->vars[varID].isUsed = TRUE;

  return (varID);
}

117
void vlistCheckVarID(char *func, int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  if ( vlistptr == NULL )
    Error(func, "vlist undefined!");

  if ( varID < 0 || varID >= vlistptr->nvars )
    Error(func, "varID %d undefined!", varID);

  if ( ! vlistptr->vars[varID].isUsed )
    Error(func, "varID %d undefined!", varID);
}


/*
@Function  vlistDefVar
Uwe Schulzweida's avatar
Uwe Schulzweida committed
136
@Title     Define a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139

@Prototype int vlistDefVar(int vlistID, int gridID, int zaxisID, int timeID)
@Parameter
140
141
142
143
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  zaxisID  Z-axis ID, from a previous call to @fref{zaxisCreate}
    @Item  timeID   One of the set of predefined CDI time identifiers.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
                    The valid CDI time identifiers are @func{TIME_CONSTANT} and @func{TIME_VARIABLE}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
145
146

@Description
147
The function @func{vlistDefVar} adds a new variable to vlistID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149

@Result
150
@func{vlistDefVar} returns an identifier to the new variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
151

Uwe Schulzweida's avatar
Uwe Schulzweida committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@Example
Here is an example using @func{vlistCreate} to create a variable list
and add a variable with @func{vlistDefVar}.

@Source
#include "cdi.h"
   ...
int vlistID, varID;
   ...
vlistID = vlistCreate();
varID = vlistDefVar(vlistID, gridID, zaxisID, TIME_VARIABLE);
   ...
streamDefVlist(streamID, vlistID);
   ...
vlistDestroy(vlistID);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
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
200
201
202
203
204
205
206
207
@EndFunction
*/
int vlistDefVar(int vlistID, int gridID, int zaxisID, int timeID)
{
  static char func[] = "vlistDefVar";
  int varID;
  int nlevs;
  int levID;
  int index;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  if ( CDI_Debug )
    Message(func, "gridID = %d  zaxisID = %d  timeID = %d", gridID, zaxisID, timeID);

  varID = vlistvarNewEntry(vlistID);

  vlistptr->nvars++;

  vlistptr->vars[varID].gridID  = gridID;
  vlistptr->vars[varID].zaxisID = zaxisID;
  vlistptr->vars[varID].timeID  = timeID;

  if ( timeID != TIME_VARIABLE && timeID != TIME_CONSTANT )
    {
      Message(func, "unexpected timeID %d. Set to TIME_VARIABLE", timeID);
      vlistptr->vars[varID].timeID = TIME_VARIABLE;	  
    }

  nlevs = zaxisInqSize(zaxisID);

  vlistptr->vars[varID].levinfo = (LEVINFO *) malloc(nlevs*sizeof(LEVINFO));

  for ( levID = 0; levID < nlevs; levID++ )
    {
      vlistptr->vars[varID].levinfo[levID].flag     = 0;
      vlistptr->vars[varID].levinfo[levID].index    = -1;
      vlistptr->vars[varID].levinfo[levID].flevelID = levID;
208
      vlistptr->vars[varID].levinfo[levID].mlevelID = levID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209
210
211
212
213
214
215
216
217
    }

  vlistptr->vars[varID].nlevs = nlevs;

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

  if ( index == vlistptr->ngrids )
    {
218
219
      if ( vlistptr->ngrids + 1 >= MAX_GRIDS_PS )
	Error(func, "Maximum of %d grids reached", MAX_GRIDS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
220
221
222
223
224
225
226
227
228
229

      vlistptr->gridIDs[vlistptr->ngrids] = gridID;
      vlistptr->ngrids++;
    }

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

  if ( index == vlistptr->nzaxis )
    {
230
231
      if ( vlistptr->nzaxis + 1 >= MAX_ZAXIS_PS )
	Error(func, "Maximum of %d zaxis reached", MAX_ZAXIS_PS);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
234
235
236
237
238
239
240
241
242
243
244

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

  vlistptr->vars[varID].code = -(varID + 1);

  return (varID);
}


/*
@Function  vlistDefVarCode
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
@Title     Define the code number of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
247
248

@Prototype void vlistDefVarCode(int vlistID, int varID, int code)
@Parameter
249
250
251
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
    @Item  code     Code number
Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
253

@Description
254
The function @func{vlistDefVarCode} defines the code number of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

@EndFunction
*/
void vlistDefVarCode(int vlistID, int varID, int code)
{
  static char func[] = "vlistDefVarCode";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  vlistptr->vars[varID].code = code;
}


void vlistInqVar(int vlistID, int varID, int *gridID, int *zaxisID, int *timeID)
{
  static char func[] = "vlistInqVar";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  *gridID  = vlistptr->vars[varID].gridID;
  *zaxisID = vlistptr->vars[varID].zaxisID;
  *timeID  = vlistptr->vars[varID].timeID;

  return;
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
@Function  vlistInqVarGrid
@Title     Get the Grid ID of a Variable

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

@Description
The function @func{vlistInqVarGrid} returns the grid ID of a variable.

@Result
@func{vlistInqVarGrid} returns the grid ID of the variable.

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
305
306
307
308
309
310
311
312
313
314
315
316
317
int vlistInqVarGrid(int vlistID, int varID)
{
  static char func[] = "vlistInqVarGrid";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].gridID);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
@Function  vlistInqVarZaxis
@Title     Get the Zaxis ID of a Variable

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

@Description
The function @func{vlistInqVarZaxis} returns the zaxis ID of a variable.

@Result
@func{vlistInqVarZaxis} returns the zaxis ID of the variable.

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
int vlistInqVarZaxis(int vlistID, int varID)
{
  static char func[] = "vlistInqVarZaxis";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].zaxisID);
}

int vlistInqVarTime(int vlistID, int varID)
{
  static char func[] = "vlistInqVarTime";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].timeID);
}


/*
@Function  vlistInqVarCode
Uwe Schulzweida's avatar
Uwe Schulzweida committed
362
@Title     Get the Code number of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
363
364
365

@Prototype int vlistInqVarCode(int vlistID, int varID)
@Parameter
366
367
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
369

@Description
370
The function @func{vlistInqVarCode} returns the code number of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371
372

@Result
373
@func{vlistInqVarCode} returns the code number of the variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374
375
376
377
378
379
380

@EndFunction
*/
int vlistInqVarCode(int vlistID, int varID)
{
  static char func[] = "vlistInqVarCode";
  VLIST *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
  int code;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
382
383
384
385
386

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
387
388
389
390
391
392
393
394
  code = vlistptr->vars[varID].code;

  if ( code < 0 && vlistptr->vars[varID].tableID != -1 && vlistptr->vars[varID].name != NULL )
    {
      tableInqParCode(vlistptr->vars[varID].tableID, vlistptr->vars[varID].name, &code);
    }

  return (code);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
}


char *vlistInqVarNamePtr(int vlistID, int varID)
{
  static char func[] = "vlistInqVarNamePtr";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].name);
}

char *vlistInqVarLongnamePtr(int vlistID, int varID)
{
  static char func[] = "vlistInqVarLongnamePtr";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].longname);
}

char *vlistInqVarStdnamePtr(int vlistID, int varID)
{
  static char func[] = "vlistInqVarStdnamePtr";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].stdname);
}

char *vlistInqVarUnitsPtr(int vlistID, int varID)
{
  static char func[] = "vlistInqVarUnitsPtr";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].units);
}


/*
@Function  vlistInqVarName
Uwe Schulzweida's avatar
Uwe Schulzweida committed
449
@Title     Get the name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
450
451
452

@Prototype void vlistInqVarName(int vlistID, int varID, char *name)
@Parameter
453
454
455
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
    @Item  name     Variable name
Uwe Schulzweida's avatar
Uwe Schulzweida committed
456
457

@Description
458
The function @func{vlistInqVarName} returns the name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
459
460

@Result
461
@func{vlistInqVarName} returns the name of the variable to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490

@EndFunction
*/
void vlistInqVarName(int vlistID, int varID, char *name)
{
  static char func[] = "vlistInqVarName";
  int code, tableID;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  if ( vlistptr->vars[varID].name == NULL )
    {
      code    = vlistptr->vars[varID].code;
      tableID = vlistptr->vars[varID].tableID;
      if ( tableInqParName(tableID, code, name) != 0 )
	sprintf(name, "var%d", code);
    }  
  else
    strcpy(name, vlistptr->vars[varID].name);

  return;
}


/*
@Function  vlistInqVarLongname
Uwe Schulzweida's avatar
Uwe Schulzweida committed
491
@Title     Get the longname of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
492
493
494

@Prototype void vlistInqVarLongname(int vlistID, int varID, char *longname)
@Parameter
495
496
497
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
    @Item  longname Variable description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
498
499

@Description
500
The function @func{vlistInqVarLongname} returns the longname of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
501
502

@Result
503
@func{vlistInqVaeLongname} returns the longname of the variable to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530

@EndFunction
*/
void vlistInqVarLongname(int vlistID, int varID, char *longname)
{
  static char func[] = "vlistInqVarLongname";
  int code, tableID;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  if ( vlistptr->vars[varID].longname == NULL )
    {
      code    = vlistptr->vars[varID].code;
      tableID = vlistptr->vars[varID].tableID;
      if ( tableInqParLongname(tableID, code, longname) != 0 )
	longname[0] = '\0';
    }  
  else
    strcpy(longname, vlistptr->vars[varID].longname);

  return;
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
@Function  vlistInqVarStdname
@Title     Get the standard name of a Variable

@Prototype void vlistInqVarStdname(int vlistID, int varID, char *stdname)
@Parameter
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
    @Item  stdname  Variable standard name

@Description
The function @func{vlistInqVarStdname} returns the standard name of a variable.

@Result
@func{vlistInqVarName} returns the standard name of the variable to the parameter stdname.

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
549
550
551
void vlistInqVarStdname(int vlistID, int varID, char *stdname)
{
  static char func[] = "vlistInqVarStdname";
552
  /*  int code, tableID; */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  if ( vlistptr->vars[varID].stdname == NULL )
    {
      /*
      code    = vlistptr->vars[varID].code;
      tableID = vlistptr->vars[varID].tableID;
      if ( tableInqParLongname(tableID, code, longname) != 0 )
      */
	stdname[0] = '\0';
    }  
  else
    strcpy(stdname, vlistptr->vars[varID].stdname);

  return;
}


/*
@Function  vlistInqVarUnits
Uwe Schulzweida's avatar
Uwe Schulzweida committed
577
@Title     Get the units of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
578
579
580

@Prototype void vlistInqVarUnits(int vlistID, int varID, char *units)
@Parameter
581
582
583
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
    @Item  units    Variable units
Uwe Schulzweida's avatar
Uwe Schulzweida committed
584
585

@Description
586
The function @func{vlistInqVarUnits} returns the units of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
587
588

@Result
589
@func{vlistInqVarUnits} returns the units of the variable to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663

@EndFunction
*/
void vlistInqVarUnits(int vlistID, int varID, char *units)
{
  static char func[] = "vlistInqVarUnits";
  int code, tableID;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  if ( vlistptr->vars[varID].units == NULL )
    {
      code    = vlistptr->vars[varID].code;
      tableID = vlistptr->vars[varID].tableID;
      if ( tableInqParUnits(tableID, code, units) != 0 )
	units[0] = '\0';
    }  
  else
    strcpy(units, vlistptr->vars[varID].units);

  return;
}

int vlistInqVarID(int vlistID, int code)
{
  int varID;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  for ( varID = 0; varID < vlistptr->nvars; varID++ )
    if ( vlistptr->vars[varID].code == code ) break;

  if ( varID == vlistptr->nvars )
    {
      /*
      Warning(func, "varID not found for code %d\n", code);
      */
      varID = CDI_UNDEFID;
    }

  return (varID);
}

int vlistInqVarSize(int vlistID, int varID)
{
  static char func[] = "vlistInqVarSize";
  int size;
  int zaxisID, gridID;
  int nlevs, gridsize;
  int timeID;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  vlistInqVar(vlistID, varID, &gridID, &zaxisID, &timeID);

  nlevs = zaxisInqSize(zaxisID);

  gridsize = gridInqSize(gridID);

  size = gridsize*nlevs;

  return (size);
}


/*
@Function  vlistInqVarDatatype
Uwe Schulzweida's avatar
Uwe Schulzweida committed
664
@Title     Get the data type of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
665
666
667

@Prototype int vlistInqVarDatatype(int vlistID, int varID)
@Parameter
668
669
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
Uwe Schulzweida's avatar
Uwe Schulzweida committed
670
671

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
672
The function @func{vlistInqVarDatatype} returns the data type of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
673
674

@Result
Uwe Schulzweida's avatar
Uwe Schulzweida committed
675
@func{vlistInqVarDatatype} returns an identifier to the data type of the variable.
676
677
The valid CDI data types are @func{DATATYPE_PACK8}, @func{DATATYPE_PACK16}, @func{DATATYPE_PACK24},
@func{DATATYPE_FLT32} and @func{DATATYPE_FLT64}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695

@EndFunction
*/
int vlistInqVarDatatype(int vlistID, int varID)
{
  static char func[] = "vlistInqVarDatatype";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].datatype);
}


/*
@Function  vlistDefVarDatatype
Uwe Schulzweida's avatar
Uwe Schulzweida committed
696
@Title     Define the data type of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
697
698
699

@Prototype void vlistDefVarDatatype(int vlistID, int varID, int datatype)
@Parameter
700
701
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
Uwe Schulzweida's avatar
Uwe Schulzweida committed
702
    @Item  datatype The data type identifier.
703
704
                    The valid CDI data types are @func{DATATYPE_PACK8}, @func{DATATYPE_PACK16},
                    @func{DATATYPE_PACK24}, @func{DATATYPE_FLT32} and @func{DATATYPE_FLT64}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
705

Uwe Schulzweida's avatar
Uwe Schulzweida committed
706
707

@Description
708
The function @func{vlistDefVarDatatype} defines the data type of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
709
710
711
712
713
714
715
716
717
718
719
720

@EndFunction
*/
void vlistDefVarDatatype(int vlistID, int varID, int datatype)
{
  static char func[] = "vlistDefVarDatatype";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
  vlistptr->vars[varID].datatype = datatype;
  
  if ( vlistptr->vars[varID].missvalused == FALSE )
    {
      switch (datatype)
	{
	case DATATYPE_INT8:
	  vlistptr->vars[varID].missval =  SCHAR_MIN;
	  break;
	case DATATYPE_UINT8:
	  vlistptr->vars[varID].missval =  UCHAR_MAX;
	  break;
	case DATATYPE_INT16:
	  vlistptr->vars[varID].missval =  SHRT_MIN;
	  break;
	case DATATYPE_UINT16:
	  vlistptr->vars[varID].missval =  USHRT_MAX;
	  break;
	case DATATYPE_INT32:
	  vlistptr->vars[varID].missval =  INT_MIN;
	  break;
	case DATATYPE_UINT32:
	  vlistptr->vars[varID].missval =  UINT_MAX;
	  break;
	}
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
747
748
}

749

Uwe Schulzweida's avatar
Uwe Schulzweida committed
750
751
752
753
754
755
756
757
758
void vlistDefVarInstitut(int vlistID, int varID, int instID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistptr->vars[varID].instID = instID;
}

759

Uwe Schulzweida's avatar
Uwe Schulzweida committed
760
761
762
763
764
765
766
767
768
int vlistInqVarInstitut(int vlistID, int varID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  return (vlistptr->vars[varID].instID);
}

769

Uwe Schulzweida's avatar
Uwe Schulzweida committed
770
771
772
773
774
775
776
777
778
void vlistDefVarModel(int vlistID, int varID, int modelID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistptr->vars[varID].modelID = modelID;
}

779

Uwe Schulzweida's avatar
Uwe Schulzweida committed
780
781
782
783
784
785
786
787
788
int vlistInqVarModel(int vlistID, int varID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  return (vlistptr->vars[varID].modelID);
}

789

Uwe Schulzweida's avatar
Uwe Schulzweida committed
790
791
792
793
794
795
796
797
798
void vlistDefVarTable(int vlistID, int varID, int tableID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistptr->vars[varID].tableID = tableID;
}

799

Uwe Schulzweida's avatar
Uwe Schulzweida committed
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
int vlistInqVarTable(int vlistID, int varID)
{
  int tableID;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  tableID = vlistptr->vars[varID].tableID;

  return (tableID);
}


/*
@Function  vlistDefVarName
Uwe Schulzweida's avatar
Uwe Schulzweida committed
815
@Title     Define the name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
816
817
818

@Prototype void vlistDefVarName(int vlistID, int varID, const char *name)
@Parameter
819
820
821
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
    @Item  name     Name of the variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
822
823

@Description
824
The function @func{vlistDefVarName} defines the name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
825
826
827
828
829
830
831
832
833
834
835
836

@EndFunction
*/
void vlistDefVarName(int vlistID, int varID, const char *name)
{
  static char func[] = "vlistDefVarName";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

837
838
839
840
841
  if ( vlistptr->vars[varID].name )
    {
      free(vlistptr->vars[varID].name);
      vlistptr->vars[varID].name = 0;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
842
843
844
845
846
847
848

  if ( name ) vlistptr->vars[varID].name = strdupx(name);
}


/*
@Function  vlistDefVarLongname
Uwe Schulzweida's avatar
Uwe Schulzweida committed
849
@Title     Define the long name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
850
851
852

@Prototype void vlistDefVarLongname(int vlistID, int varID, const char *longname)
@Parameter
853
854
855
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
    @Item  longname Long name of the variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
856
857

@Description
858
The function @func{vlistDefVarLongname} defines the long name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
859
860
861
862
863
864
865
866
867
868
869
870

@EndFunction
*/
void vlistDefVarLongname(int vlistID, int varID, const char *longname)
{
  static char func[] = "vlistDefVarLongname";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

871
872
873
874
875
  if ( vlistptr->vars[varID].longname )
    {
      free(vlistptr->vars[varID].longname);
      vlistptr->vars[varID].longname = 0;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
876
877
878
879
880

  if ( longname ) vlistptr->vars[varID].longname = strdupx(longname);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
/*
@Function  vlistDefVarStdname
@Title     Define the standard name of a Variable

@Prototype void vlistDefVarStdname(int vlistID, int varID, const char *stdname)
@Parameter
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
    @Item  stdname  Standard name of the variable

@Description
The function @func{vlistDefVarStdname} defines the standard name of a variable.

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
896
897
898
899
900
901
902
903
904
void vlistDefVarStdname(int vlistID, int varID, const char *stdname)
{
  static char func[] = "vlistDefVarStdname";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

905
906
907
908
909
  if ( vlistptr->vars[varID].stdname )
    {
      free(vlistptr->vars[varID].stdname);
      vlistptr->vars[varID].stdname = 0;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
910
911
912
913
914
915
916

  if ( stdname ) vlistptr->vars[varID].stdname = strdupx(stdname);
}


/*
@Function  vlistDefVarUnits
Uwe Schulzweida's avatar
Uwe Schulzweida committed
917
@Title     Define the units of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
918
919
920

@Prototype void vlistDefVarUnits(int vlistID, int varID, const char *units)
@Parameter
921
922
923
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
    @Item  units    Units of the variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
924
925

@Description
926
The function @func{vlistDefVarUnits} defines the units of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
927
928
929
930
931
932
933
934
935
936
937
938

@EndFunction
*/
void vlistDefVarUnits(int vlistID, int varID, const char *units)
{
  static char func[] = "vlistDefVarUnits";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

939
940
941
942
943
  if ( vlistptr->vars[varID].units )
    {
      free(vlistptr->vars[varID].units);
      vlistptr->vars[varID].units = 0;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
944
945
946
947
948
949
950

  if ( units ) vlistptr->vars[varID].units = strdupx(units);
}


/*
@Function  vlistInqVarMissval
Uwe Schulzweida's avatar
Uwe Schulzweida committed
951
@Title     Get the missing value of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
952
953
954

@Prototype double vlistInqVarMissval(int vlistID, int varID)
@Parameter
955
956
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
Uwe Schulzweida's avatar
Uwe Schulzweida committed
957
958

@Description
959
The function @func{vlistInqVarMissval} returns the missing value of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
960
961

@Result
962
@func{vlistInqVarMissval} returns the missing value of the variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005

@EndFunction
*/
double vlistInqVarMissval(int vlistID, int varID)
{
  static char func[] = "vlistInqVarMissval";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].missval);
}


double vlistInqVarScalefactor(int vlistID, int varID)
{
  static char func[] = "vlistInqVarScalefactor";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].scalefactor);
}

double vlistInqVarAddoffset(int vlistID, int varID)
{
  static char func[] = "vlistInqVarAddoffset";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].addoffset);
}


/*
@Function  vlistDefVarMissval
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1006
@Title     Define the missing value of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1007
1008
1009

@Prototype void vlistDefVarMissval(int vlistID, int varID, double missval)
@Parameter
1010
1011
1012
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
    @Item  missval  Missing value
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1013
1014

@Description
1015
The function @func{vlistDefVarMissval} defines the missing value of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031

@EndFunction
*/
void vlistDefVarMissval(int vlistID, int varID, double missval)
{
  static char func[] = "vlistDefVarMissval";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  vlistptr->vars[varID].missval = missval;
  vlistptr->vars[varID].missvalused = TRUE;
}

1032

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
void vlistDefVarScalefactor(int vlistID, int varID, double scalefactor)
{
  static char func[] = "vlistDefVarScalefactor";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  vlistptr->vars[varID].scalefactor = scalefactor;
}

1045

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
void vlistDefVarAddoffset(int vlistID, int varID, double addoffset)
{
  static char func[] = "vlistDefVarAddoffset";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  vlistptr->vars[varID].addoffset = addoffset;
}

1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079

void vlistDefVarTimave(int vlistID, int varID, int timave)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistptr->vars[varID].timave = timave;
}


int vlistInqVarTimave(int vlistID, int varID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  return (vlistptr->vars[varID].timave);
}


void vlistDefVarTimaccu(int vlistID, int varID, int timaccu)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1080
1081
1082
1083
1084
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

1085
  vlistptr->vars[varID].timaccu = timaccu;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1086
1087
}

1088
1089

int vlistInqVarTimaccu(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1090
1091
1092
1093
1094
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

1095
  return (vlistptr->vars[varID].timaccu);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1096
1097
}

1098

1099
void vlistDestroyVarName(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1100
{
1101
  static char func[] = "vlistDestroyVarName";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  if ( vlistptr->vars[varID].name )
    {
      free(vlistptr->vars[varID].name);
      vlistptr->vars[varID].name = NULL;
    }
}

1113

1114
void vlistDestroyVarLongname(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1115
{
1116
  static char func[] = "vlistDestroyVarLongname";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  if ( vlistptr->vars[varID].longname )
    {
      free(vlistptr->vars[varID].longname);
      vlistptr->vars[varID].longname = NULL;
    }
}

1128

1129
void vlistDestroyVarStdname(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1130
{
1131
  static char func[] = "vlistDestroyVarStdname";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  if ( vlistptr->vars[varID].stdname )
    {
      free(vlistptr->vars[varID].stdname);
      vlistptr->vars[varID].stdname = NULL;
    }
}

1143

1144
void vlistDestroyVarUnits(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1145
{
1146
  static char func[] = "vlistDestroyVarUnits";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  if ( vlistptr->vars[varID].units )
    {
      free(vlistptr->vars[varID].units);
      vlistptr->vars[varID].units = NULL;
    }
}


void vlistDefVarTime(int vlistID, int varID, int timeID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistptr->vars[varID].timeID = timeID;
}


int vlistInqVarMissvalUsed(int vlistID, int varID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  return (vlistptr->vars[varID].missvalused);
}


void vlistDefFlag(int vlistID, int varID, int levID, int flag)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistptr->vars[varID].flag = flag;
  vlistptr->vars[varID].levinfo[levID].flag = flag;
}


int vlistInqFlag(int vlistID, int varID, int levID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  return (vlistptr->vars[varID].levinfo[levID].flag);
}


int vlistFindVar(int vlistID, int fvarID)
{
  static char func[] = "vlistFindVar";
  int varID;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  for ( varID = 0; varID < vlistptr->nvars; varID++ )
    {
      if ( vlistptr->vars[varID].fvarID == fvarID ) break;
    }

  if ( varID == vlistptr->nvars )
    {
      varID = -1;
      Message(func, "varID not found for fvarID %d in vlistID %d!", fvarID, vlistID);
    }

  return (varID);  
}


int vlistFindLevel(int vlistID, int fvarID, int flevelID)
{
  static char func[] = "vlistFindLevel";
  int varID;
  int levelID = -1;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  varID = vlistFindVar(vlistID, fvarID);

  if ( varID != -1 )
    {
      for ( levelID = 0; levelID < vlistptr->vars[varID].nlevs; levelID++ )
	{
	  if ( vlistptr->vars[varID].levinfo[levelID].flevelID == flevelID ) break;
	}

      if ( levelID == vlistptr->vars[varID].nlevs )
	{
	  levelID = -1;
	  Message(func, "levelID not found for fvarID %d and levelID %d in vlistID %d!",
		  fvarID, flevelID, vlistID);
	}
    }

  return (levelID);  
}


1253
int vlistMergedVar(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1254
1255
1256
1257
1258
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

1259
  return (vlistptr->vars[varID].mvarID);  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1260
1261
1262
}


1263
int vlistMergedLevel(int vlistID, int varID, int levelID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1264
1265
1266
1267
1268
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

1269
  return (vlistptr->vars[varID].levinfo[levelID].mlevelID);  
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
}


void vlistDefIndex(int vlistID, int varID, int levelID, int index)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistptr->vars[varID].levinfo[levelID].index = index;  
}


int vlistInqIndex(int vlistID, int varID, int levelID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  return (vlistptr->vars[varID].levinfo[levelID].index);  
}


void vlistChangeVarZaxis(int vlistID, int varID, int zaxisID)
{
  static char func[] = "vlistChangeVarZaxis";
  int nlevs1, nlevs2;
  int nvars, index;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  nlevs1 = zaxisInqSize(vlistptr->vars[varID].zaxisID);
  nlevs2 = zaxisInqSize(zaxisID);

  if ( nlevs1 != nlevs2 ) Error(func, "Number of levels must not change!");

  nvars = vlistptr->nvars;
  for ( index = 0; index < nvars; index++ )
    if ( index != varID )
      if ( vlistptr->vars[index].zaxisID == vlistptr->vars[varID].zaxisID ) break;

  if ( index == nvars )
    {
      for ( index = 0; index < vlistptr->nzaxis; index++ )
	if ( vlistptr->zaxisIDs[index] == vlistptr->vars[varID].zaxisID )
	  vlistptr->zaxisIDs[index] = zaxisID;
    }
  else
    {
1322
1323
      for ( index = 0; index < vlistptr->nzaxis; index++ )
	if ( vlistptr->zaxisIDs[index] == zaxisID ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1324

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1325
      if ( index == vlistptr->nzaxis )
1326
	{
1327
1328
	  if ( vlistptr->nzaxis + 1 >= MAX_ZAXIS_PS )
	    Error(func, "Maximum of %d zaxis reached", MAX_ZAXIS_PS);
1329
1330
1331
1332

	  vlistptr->zaxisIDs[vlistptr->nzaxis] = zaxisID;
	  vlistptr->nzaxis++;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1333
1334
1335
1336
    }
  
  vlistptr->vars[varID].zaxisID = zaxisID;
}
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364


void vlistChangeVarGrid(int vlistID, int varID, int gridID)
{
  static char func[] = "vlistChangeVarGrid";
  int nvars, index;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  nvars = vlistptr->nvars;
  for ( index = 0; index < nvars; index++ )
    if ( index != varID )
      if ( vlistptr->vars[index].gridID == vlistptr->vars[varID].gridID ) break;

  if ( index == nvars )
    {
      for ( index = 0; index < vlistptr->ngrids; index++ )
	if ( vlistptr->gridIDs[index] == vlistptr->vars[varID].gridID )
	  vlistptr->gridIDs[index] = gridID;
    }
  else
    {
      for ( index = 0; index < vlistptr->ngrids; index++ )
	if ( vlistptr->gridIDs[index] == gridID ) break;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1365
      if ( index == vlistptr->ngrids )
1366
	{
1367
1368
	  if ( vlistptr->ngrids + 1 >= MAX_GRIDS_PS )
	    Error(func, "Maximum of %d grids reached", MAX_GRIDS_PS);
1369
1370
1371
1372
1373
1374
1375
1376

	  vlistptr->gridIDs[vlistptr->ngrids] = gridID;
	  vlistptr->ngrids++;
	}
    }
  
  vlistptr->vars[varID].gridID = gridID;
}
1377
1378


1379
void vlistDefVarZtype(int vlistID, int varID, int ztype)
1380
{
1381
  static char func[] = "vlistDefVarZtype";
1382
1383
1384
1385
1386
1387
1388
  int nvars, index;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

1389
  vlistptr->vars[varID].ztype = ztype;
1390
1391
1392
}


1393
int vlistInqVarZtype(int vlistID, int varID)
1394
{
1395
  static char func[] = "vlistInqVarZtype";
1396
1397
1398
1399
1400
1401
1402
  int nvars, index;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

1403
  return (vlistptr->vars[varID].ztype);
1404
1405
}

1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432

void vlistDefVarZlevel(int vlistID, int varID, int zlevel)
{
  static char func[] = "vlistDefVarZlevel";
  int nvars, index;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  vlistptr->vars[varID].zlevel = zlevel;
}


int vlistInqVarZlevel(int vlistID, int varID)
{
  static char func[] = "vlistInqVarZlevel";
  int nvars, index;
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

  return (vlistptr->vars[varID].zlevel);
}