vlist_var.c 28.7 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
117
118
119
120
121
122
123
124
125
126
127
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#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;
  vlistptr->vars[varID].flag         = 0;
  vlistptr->vars[varID].code         = 0;
  vlistptr->vars[varID].timeID       = CDI_UNDEFID;
  vlistptr->vars[varID].datatype     = CDI_UNDEFID;
  vlistptr->vars[varID].average      = 0;
  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;
}

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

static void vlistCheckVarID(char func[], int vlistID, int varID)
{
  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
128
@Title     Define a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
130
131

@Prototype int vlistDefVar(int vlistID, int gridID, int zaxisID, int timeID)
@Parameter
132
133
134
135
    @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
136
                    The valid CDI time identifiers are @func{TIME_CONSTANT} and @func{TIME_VARIABLE}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138

@Description
139
The function @func{vlistDefVar} adds a new variable to vlistID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
141

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
@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
161
162
163
164
165
166
167
168
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
@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;
    }

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

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

  if ( index == vlistptr->ngrids )
    {
      if ( vlistptr->ngrids + 1 >= MAX_GRIDS )
	Error(func, "Maximum of %d grids reached", MAX_GRIDS);

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

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

  if ( index == vlistptr->nzaxis )
    {
      if ( vlistptr->nzaxis + 1 >= MAX_ZAXIS )
	Error(func, "Maximum of %d zaxis reached", MAX_ZAXIS);

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

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

  return (varID);
}


/*
@Function  vlistDefVarCode
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236
@Title     Define the code number of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
237
238
239

@Prototype void vlistDefVarCode(int vlistID, int varID, int code)
@Parameter
240
241
242
    @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
243
244

@Description
245
The function @func{vlistDefVarCode} defines the code number of a variable.
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
268
269
270
271
272
273
274
275
276
277
278

@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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
@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
296
297
298
299
300
301
302
303
304
305
306
307
308
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
@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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
353
@Title     Get the Code number of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
355
356

@Prototype int vlistInqVarCode(int vlistID, int varID)
@Parameter
357
358
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
Uwe Schulzweida's avatar
Uwe Schulzweida committed
359
360

@Description
361
The function @func{vlistInqVarCode} returns the code number of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
362
363

@Result
364
@func{vlistInqVarCode} returns the code number of the variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
365
366
367
368
369
370
371

@EndFunction
*/
int vlistInqVarCode(int vlistID, int varID)
{
  static char func[] = "vlistInqVarCode";
  VLIST *vlistptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
  int code;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
374
375
376
377

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
378
379
380
381
382
383
384
385
  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
386
387
388
389
390
391
392
393
394
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
}


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
440
@Title     Get the name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
441
442
443

@Prototype void vlistInqVarName(int vlistID, int varID, char *name)
@Parameter
444
445
446
    @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
447
448

@Description
449
The function @func{vlistInqVarName} returns the name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
450
451

@Result
452
@func{vlistInqVarName} returns the name of the variable to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

@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
482
@Title     Get the longname of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
483
484
485

@Prototype void vlistInqVarLongname(int vlistID, int varID, char *longname)
@Parameter
486
487
488
    @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
489
490

@Description
491
The function @func{vlistInqVarLongname} returns the longname of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
492
493

@Result
494
@func{vlistInqVaeLongname} returns the longname of the variable to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

@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;
}


void vlistInqVarStdname(int vlistID, int varID, char *stdname)
{
  static char func[] = "vlistInqVarStdname";
525
  /*  int code, tableID; */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
  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
550
@Title     Get the units of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
552
553

@Prototype void vlistInqVarUnits(int vlistID, int varID, char *units)
@Parameter
554
555
556
    @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
557
558

@Description
559
The function @func{vlistInqVarUnits} returns the units of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
560
561

@Result
562
@func{vlistInqVarUnits} returns the units of the variable to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
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

@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
637
@Title     Get the data type of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
638
639
640

@Prototype int vlistInqVarDatatype(int vlistID, int varID)
@Parameter
641
642
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
Uwe Schulzweida's avatar
Uwe Schulzweida committed
643
644

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
645
The function @func{vlistInqVarDatatype} returns the data type of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
646
647

@Result
Uwe Schulzweida's avatar
Uwe Schulzweida committed
648
@func{vlistInqVarDatatype} returns an identifier to the data type of the variable.
649
650
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668

@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
669
@Title     Define the data type of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
670
671
672

@Prototype void vlistDefVarDatatype(int vlistID, int varID, int datatype)
@Parameter
673
674
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
Uwe Schulzweida's avatar
Uwe Schulzweida committed
675
    @Item  datatype The data type identifier.
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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
679
680

@Description
681
The function @func{vlistDefVarDatatype} defines the data type of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
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
747
748
749
750
751
752
753
754
755
756

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

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

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

void vlistDefVarInstitut(int vlistID, int varID, int instID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

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

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

  vlistptr = vlist_to_pointer(vlistID);

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

void vlistDefVarModel(int vlistID, int varID, int modelID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

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

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

  vlistptr = vlist_to_pointer(vlistID);

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

void vlistDefVarTable(int vlistID, int varID, int tableID)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

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

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
757
@Title     Define the name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
758
759
760

@Prototype void vlistDefVarName(int vlistID, int varID, const char *name)
@Parameter
761
762
763
    @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
764
765

@Description
766
The function @func{vlistDefVarName} defines the name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786

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

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

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


/*
@Function  vlistDefVarLongname
Uwe Schulzweida's avatar
Uwe Schulzweida committed
787
@Title     Define the long name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
788
789
790

@Prototype void vlistDefVarLongname(int vlistID, int varID, const char *longname)
@Parameter
791
792
793
    @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
794
795

@Description
796
The function @func{vlistDefVarLongname} defines the long name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831

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

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

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


void vlistDefVarStdname(int vlistID, int varID, const char *stdname)
{
  static char func[] = "vlistDefVarStdname";
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(func, vlistID, varID);

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

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


/*
@Function  vlistDefVarUnits
Uwe Schulzweida's avatar
Uwe Schulzweida committed
832
@Title     Define the units of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
833
834
835

@Prototype void vlistDefVarUnits(int vlistID, int varID, const char *units)
@Parameter
836
837
838
    @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
839
840

@Description
841
The function @func{vlistDefVarUnits} defines the units of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861

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

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

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


/*
@Function  vlistInqVarMissval
Uwe Schulzweida's avatar
Uwe Schulzweida committed
862
@Title     Get the missing value of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
863
864
865

@Prototype double vlistInqVarMissval(int vlistID, int varID)
@Parameter
866
867
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}
    @Item  varID    Variable identifier
Uwe Schulzweida's avatar
Uwe Schulzweida committed
868
869

@Description
870
The function @func{vlistInqVarMissval} returns the missing value of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
871
872

@Result
873
@func{vlistInqVarMissval} returns the missing value of the variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916

@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
917
@Title     Define the missing value of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
918
919
920

@Prototype void vlistDefVarMissval(int vlistID, int varID, double missval)
@Parameter
921
922
923
    @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
924
925

@Description
926
The function @func{vlistDefVarMissval} defines the missing value of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
927
928
929
930
931
932
933
934
935
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
979
980
981
982
983
984

@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;
}

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

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

void vlistDefVarAverage(int vlistID, int varID, int average)
{
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

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

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

  vlistptr = vlist_to_pointer(vlistID);

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

985
void vlistDestroyVarName(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
986
{
987
  static char func[] = "vlistDestroyVarName";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
988
989
990
991
992
993
994
995
996
997
998
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

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

999
void vlistDestroyVarLongname(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1000
{
1001
  static char func[] = "vlistDestroyVarLongname";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

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

1013
void vlistDestroyVarStdname(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1014
{
1015
  static char func[] = "vlistDestroyVarStdname";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
  VLIST *vlistptr;

  vlistptr = vlist_to_pointer(vlistID);

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

1027
void vlistDestroyVarUnits(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1028
{
1029
  static char func[] = "vlistDestroyVarUnits";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
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
  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);  
}


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

  vlistptr = vlist_to_pointer(vlistID);

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


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

  vlistptr = vlist_to_pointer(vlistID);

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


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
    {
1205
1206
      for ( index = 0; index < vlistptr->nzaxis; index++ )
	if ( vlistptr->zaxisIDs[index] == zaxisID ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1207

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1208
      if ( index == vlistptr->nzaxis )
1209
1210
1211
1212
1213
1214
1215
	{
	  if ( vlistptr->nzaxis + 1 >= MAX_ZAXIS )
	    Error(func, "Maximum of %d zaxis reached", MAX_ZAXIS);

	  vlistptr->zaxisIDs[vlistptr->nzaxis] = zaxisID;
	  vlistptr->nzaxis++;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1216
1217
1218
1219
    }
  
  vlistptr->vars[varID].zaxisID = zaxisID;
}
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


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
1248
      if ( index == vlistptr->ngrids )
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
	{
	  if ( vlistptr->ngrids + 1 >= MAX_GRIDS )
	    Error(func, "Maximum of %d grids reached", MAX_GRIDS);

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