vlist_var.c 65 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
#include "dmemory.h"
#include "cdi.h"
9
#include "cdi_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
10
#include "vlist.h"
Deike Kleberg's avatar
Deike Kleberg committed
11
#include "vlist_var.h"
12
#include "resource_handle.h"
13
#include "vlist_att.h"
Deike Kleberg's avatar
Deike Kleberg committed
14
#include "namespace.h"
15
#include "serialize.h"
16
#include "error.h"
17

18
19
20
21
22
#if  defined  (HAVE_LIBGRIB_API)
#  include "file.h"
#  include <grib_api.h>
#endif

23

24
25
static
void vlistvarInitEntry(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
27
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28

Deike Kleberg's avatar
Deike Kleberg committed
29
30
31
32
33
34
35
36
  vlistptr->vars[varID].fvarID        = varID;
  vlistptr->vars[varID].mvarID        = varID;
  vlistptr->vars[varID].flag          = 0;
  vlistptr->vars[varID].param         = 0;
  vlistptr->vars[varID].datatype      = CDI_UNDEFID;
  vlistptr->vars[varID].tsteptype     = TSTEP_INSTANT;
  vlistptr->vars[varID].timave        = 0;
  vlistptr->vars[varID].timaccu       = 0;
37
38
  vlistptr->vars[varID].typeOfGeneratingProcess   = 0;
  vlistptr->vars[varID].productDefinitionTemplate = -1;
39
  vlistptr->vars[varID].chunktype     = cdiChunkType;
40
  vlistptr->vars[varID].xyz           = 321;
Deike Kleberg's avatar
Deike Kleberg committed
41
42
  vlistptr->vars[varID].gridID        = CDI_UNDEFID;
  vlistptr->vars[varID].zaxisID       = CDI_UNDEFID;
43
  vlistptr->vars[varID].subtypeID     = CDI_UNDEFID;
Deike Kleberg's avatar
Deike Kleberg committed
44
45
46
47
48
49
50
51
52
53
54
  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;
55
  vlistptr->vars[varID].extra         = NULL;
Deike Kleberg's avatar
Deike Kleberg committed
56
  vlistptr->vars[varID].levinfo       = NULL;
57
  vlistptr->vars[varID].comptype      = CDI_COMPRESS_NONE;
Deike Kleberg's avatar
Deike Kleberg committed
58
59
60
61
62
63
  vlistptr->vars[varID].complevel     = 1;
  vlistptr->vars[varID].atts.nalloc   = MAX_ATTRIBUTES;
  vlistptr->vars[varID].atts.nelems   = 0;
  vlistptr->vars[varID].lvalidrange   = 0;
  vlistptr->vars[varID].validrange[0] = VALIDMISS;
  vlistptr->vars[varID].validrange[1] = VALIDMISS;
64
  vlistptr->vars[varID].ensdata       = NULL;
65
  vlistptr->vars[varID].iorank        = CDI_UNDEFID;
66
  vlistptr->vars[varID].opt_grib_kvpair_size = 0;
67
  vlistptr->vars[varID].opt_grib_kvpair      = NULL;
68
  vlistptr->vars[varID].opt_grib_nentries    = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
69
70
}

71
72


73
74
static
int vlistvarNewEntry(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
76
77
{
  int varID = 0;
  int vlistvarSize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
  var_t *vlistvar;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
81
82
83
84
85
86
87
88
89

  vlistvarSize = vlistptr->varsAllocated;
  vlistvar     = vlistptr->vars;
  /*
    Look for a free slot in vlistvar.
    (Create the table the first time through).
  */
  if ( ! vlistvarSize )
    {
      vlistvarSize = 2;
90
      vlistvar = (var_t *) Malloc((size_t)vlistvarSize * sizeof (var_t));
91
      for (int i = 0; i < vlistvarSize; i++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
92
93
94
95
	vlistvar[i].isUsed = FALSE;
    }
  else
    {
Thomas Jahns's avatar
Thomas Jahns committed
96
97
      while (varID < vlistvarSize && vlistvar[varID].isUsed)
        ++varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
99
100
101
102
103
    }
  /*
    If the table overflows, double its size.
  */
  if ( varID == vlistvarSize )
    {
104
      vlistvar = (var_t *) Realloc(vlistvar,
Thomas Jahns's avatar
Thomas Jahns committed
105
106
                                   (size_t)(vlistvarSize *= 2) * sizeof(var_t));
      for ( int i = varID; i < vlistvarSize; i++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
108
109
110
111
112
113
114
115
116
	vlistvar[i].isUsed = FALSE;
    }

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

  vlistvarInitEntry(vlistID, varID);

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
  return varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
}

120
void vlistCheckVarID(const char *caller, int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
123
124

  if ( vlistptr == NULL )
125
    Errorc("vlist undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
126
127

  if ( varID < 0 || varID >= vlistptr->nvars )
128
    Errorc("varID %d undefined!", varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
129
130

  if ( ! vlistptr->vars[varID].isUsed )
131
    Errorc("varID %d undefined!", varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
133
}

134

135
136
137
138
139
140
141
int vlistDefVarTiles(int vlistID, int gridID, int zaxisID, int tsteptype, int tilesetID)
{
  if ( CDI_Debug )
    Message("gridID = %d  zaxisID = %d  tsteptype = %d", gridID, zaxisID, tsteptype);

  int varID = vlistvarNewEntry(vlistID);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
143
  vlistptr->nvars++;
144
145
  vlistptr->vars[varID].gridID    = gridID;
  vlistptr->vars[varID].zaxisID   = zaxisID;
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  vlistptr->vars[varID].tsteptype = tsteptype;
  vlistptr->vars[varID].subtypeID = tilesetID;

  if ( tsteptype < 0 )
    {
      Message("Unexpected tstep type %d, set to TSTEP_INSTANT!", tsteptype);
      vlistptr->vars[varID].tsteptype = TSTEP_INSTANT;
    }

  vlistAdd2GridIDs(vlistptr, gridID);
  vlistAdd2ZaxisIDs(vlistptr, zaxisID);
  vlistAdd2SubtypeIDs(vlistptr, tilesetID);

  vlistptr->vars[varID].param = cdiEncodeParam(-(varID + 1), 255, 255);
  reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
161
162

  return varID;
163
164
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
/*
@Function  vlistDefVar
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
@Title     Define a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168

169
@Prototype int vlistDefVar(int vlistID, int gridID, int zaxisID, int tsteptype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
@Parameter
171
172
173
174
    @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  tsteptype One of the set of predefined CDI timestep types.
175
176
                     The valid CDI timestep types are @func{TSTEP_CONSTANT}, @func{TSTEP_INSTANT},
                     @func{TSTEP_ACCUM}, @func{TSTEP_AVG}, @func{TSTEP_MAX}, @func{TSTEP_MIN} and @func{TSTEP_SD}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
178

@Description
179
The function @func{vlistDefVar} adds a new variable to vlistID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
180
181

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
184
185
186
187
188
189
190
191
192
193
@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();
194
varID = vlistDefVar(vlistID, gridID, zaxisID, TIME_INSTANT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
195
196
197
198
199
200
   ...
streamDefVlist(streamID, vlistID);
   ...
vlistDestroy(vlistID);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
202
@EndFunction
*/
203
int vlistDefVar(int vlistID, int gridID, int zaxisID, int tsteptype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
{
205
206
  /* call "vlistDefVarTiles" with a trivial tile index: */
  return vlistDefVarTiles(vlistID, gridID, zaxisID, tsteptype, CDI_UNDEFID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
208
}

209
210
211
212
213
214
void
cdiVlistCreateVarLevInfo(vlist_t *vlistptr, int varID)
{
  xassert(varID >= 0 && varID < vlistptr->nvars
          && vlistptr->vars[varID].levinfo == NULL);
  int zaxisID = vlistptr->vars[varID].zaxisID;
215
  size_t nlevs = (size_t)zaxisInqSize(zaxisID);
216

217
  vlistptr->vars[varID].levinfo
218
    = (levinfo_t*) Malloc((size_t)nlevs * sizeof(levinfo_t));
219

220
221
  for (size_t levID = 0; levID < nlevs; levID++ )
    vlistptr->vars[varID].levinfo[levID] = DEFAULT_LEVINFO((int)levID);
222
223
}

224
225
226
227
228
229
/*
@Function  vlistDefVarParam
@Title     Define the parameter number of a Variable

@Prototype void vlistDefVarParam(int vlistID, int varID, int param)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
230
231
232
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}.
    @Item  varID    Variable identifier.
    @Item  param    Parameter number.
233
234
235
236
237
238
239
240

@Description
The function @func{vlistDefVarParam} defines the parameter number of a variable.

@EndFunction
*/
void vlistDefVarParam(int vlistID, int varID, int param)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
242

243
  vlistCheckVarID(__func__, vlistID, varID);
244

245
246
247
  if (vlistptr->vars[varID].param != param)
    {
      vlistptr->vars[varID].param = param;
Thomas Jahns's avatar
Thomas Jahns committed
248
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
249
    }
250
251
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
252
253
/*
@Function  vlistDefVarCode
Uwe Schulzweida's avatar
Uwe Schulzweida committed
254
@Title     Define the code number of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
256
257

@Prototype void vlistDefVarCode(int vlistID, int varID, int code)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
258
259
260
    @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
261
262

@Description
263
The function @func{vlistDefVarCode} defines the code number of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
265
266
267
268

@EndFunction
*/
void vlistDefVarCode(int vlistID, int varID, int code)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
269
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
270

271
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
272

273
274
  int param = vlistptr->vars[varID].param;
  int pnum, pcat, pdis;
275
  cdiDecodeParam(param, &pnum, &pcat, &pdis);
276
277
278
279
  int newParam = cdiEncodeParam(code, pcat, pdis);
  if (vlistptr->vars[varID].param != newParam)
    {
      vlistptr->vars[varID].param = newParam;
Thomas Jahns's avatar
Thomas Jahns committed
280
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
281
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
283
284
}


285
void vlistInqVar(int vlistID, int varID, int *gridID, int *zaxisID, int *tsteptype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
287
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
288

289
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
290

291
292
293
  *gridID    = vlistptr->vars[varID].gridID;
  *zaxisID   = vlistptr->vars[varID].zaxisID;
  *tsteptype = vlistptr->vars[varID].tsteptype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
294
295
296
297

  return;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
299
300
301
302
303
/*
@Function  vlistInqVarGrid
@Title     Get the Grid ID of a Variable

@Prototype int vlistInqVarGrid(int vlistID, int varID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
304
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
305
    @Item  varID    Variable identifier.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
307

@Description
308
The function @func{vlistInqVarGrid} returns the grid ID of a Variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
309
310

@Result
311
@func{vlistInqVarGrid} returns the grid ID of the Variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
313
314

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
315
316
int vlistInqVarGrid(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
318

319
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
320

Uwe Schulzweida's avatar
Uwe Schulzweida committed
321
  return vlistptr->vars[varID].gridID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
322
323
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
325
326
327
328
329
/*
@Function  vlistInqVarZaxis
@Title     Get the Zaxis ID of a Variable

@Prototype int vlistInqVarZaxis(int vlistID, int varID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
330
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
331
    @Item  varID    Variable identifier.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
332
333
334
335
336
337
338
339
340

@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
341
342
int vlistInqVarZaxis(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
344

345
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
346

Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
  return vlistptr->vars[varID].zaxisID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
349
}

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

/*
@Function  vlistInqVarSubtype
@Title     Get the Subtype ID of a Variable

@Description
The function @func{vlistInqVarSubtype} returns the subtype ID of a variable.

@Result
@func{vlistInqVarSubtype} returns the subtype ID of the variable.

@EndFunction
*/
int vlistInqVarSubtype(int vlistID, int varID)
{
  vlist_t *vlistptr = vlist_to_pointer(vlistID);

  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
368
369

  return vlistptr->vars[varID].subtypeID;
370
371
372
}


373
374
375
376
377
378
/*
@Function  vlistInqVarParam
@Title     Get the parameter number of a Variable

@Prototype int vlistInqVarParam(int vlistID, int varID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
379
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
380
    @Item  varID    Variable identifier.
381
382
383
384
385
386
387
388
389
390
391

@Description
The function @func{vlistInqVarParam} returns the parameter number of a variable.

@Result
@func{vlistInqVarParam} returns the parameter number of the variable.

@EndFunction
*/
int vlistInqVarParam(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
392
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
393

394
  vlistCheckVarID(__func__, vlistID, varID);
395

Uwe Schulzweida's avatar
Uwe Schulzweida committed
396
  return vlistptr->vars[varID].param;
397
398
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
400
/*
@Function  vlistInqVarCode
Uwe Schulzweida's avatar
Uwe Schulzweida committed
401
@Title     Get the Code number of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
403
404

@Prototype int vlistInqVarCode(int vlistID, int varID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
405
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
406
    @Item  varID    Variable identifier.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
407
408

@Description
409
The function @func{vlistInqVarCode} returns the code number of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
410
411

@Result
412
@func{vlistInqVarCode} returns the code number of the variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413
414
415
416
417

@EndFunction
*/
int vlistInqVarCode(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
418
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419

420
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
421

422
423
  int param = vlistptr->vars[varID].param;
  int pdis, pcat, pnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
424
  cdiDecodeParam(param, &pnum, &pcat, &pdis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
425
426
  int code = pnum;
  if ( pdis != 255 ) code = -varID-1; // GRIB2 Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
427
428
429
430
431
432

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
433
  return code;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
434
435
436
}


437
const char *vlistInqVarNamePtr(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
438
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
439
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
440

441
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
442

Uwe Schulzweida's avatar
Uwe Schulzweida committed
443
  return vlistptr->vars[varID].name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
444
445
}

446

447
const char *vlistInqVarLongnamePtr(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
448
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
449
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
450

451
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
452

Uwe Schulzweida's avatar
Uwe Schulzweida committed
453
  return vlistptr->vars[varID].longname;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
455
}

456

457
const char *vlistInqVarStdnamePtr(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
458
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
459
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
460

461
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
462

Uwe Schulzweida's avatar
Uwe Schulzweida committed
463
  return vlistptr->vars[varID].stdname;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
464
465
}

466

467
const char *vlistInqVarUnitsPtr(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
468
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
469
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
470

471
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
472

Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
  return vlistptr->vars[varID].units;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
474
475
476
477
}

/*
@Function  vlistInqVarName
Uwe Schulzweida's avatar
Uwe Schulzweida committed
478
@Title     Get the name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
479

480
@Prototype void vlistInqVarName(int vlistID, int varID, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
481
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
482
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
483
    @Item  varID    Variable identifier.
484
    @Item  name     Returned variable name. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
485
                    returned string. The maximum possible length, in characters, of
486
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
488

@Description
489
The function @func{vlistInqVarName} returns the name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
490
491

@Result
492
493
@func{vlistInqVarName} returns the name of the variable to the parameter name if available,
otherwise the result is an empty string.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
494
495
496
497
498

@EndFunction
*/
void vlistInqVarName(int vlistID, int varID, char *name)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
499
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
500

501
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
503
504

  if ( vlistptr->vars[varID].name == NULL )
    {
505
506
      int param = vlistptr->vars[varID].param;
      int pdis, pcat, pnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
507
      cdiDecodeParam(param, &pnum, &pcat, &pdis);
508
509
510
      if ( pdis == 255 )
	{
	  int code = pnum;
511
	  int tableID = vlistptr->vars[varID].tableID;
512
513
514
515
516
517
518
	  if ( tableInqParName(tableID, code, name) != 0 )
	    sprintf(name, "var%d", code);
	}
      else
	{
	  sprintf(name, "param%d.%d.%d", pnum, pcat, pdis);
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
519
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
520
  else
521
    strcpy(name, vlistptr->vars[varID].name);   //FIXME: This may overrun the provided buffer.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
522
523
524
525

  return;
}

526
527
528
529
530
531
532
533
534
/*
@Function vlistCopyVarName
@Tatle    Get the name of a Variable in a safe way

@Prototype char* vlistCopyVarName(int vlistId, int varId)
@Parameter
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
    @Item  varID    Variable identifier.

535
@Return A pointer to a malloc'ed string. Must be cleaned up with Free().
536
537
538

@Description
This is the buffer overflow immune version of vlistInqVarName().
539
The memory for the returned string is allocated to fit the string via Malloc().
540
541
542
543
544
545
546
547
548
549

@EndFunction
*/
char* vlistCopyVarName(int vlistId, int varId)
{
  vlist_t* vlistptr = vlist_to_pointer(vlistId);
  vlistCheckVarID(__func__, vlistId, varId);

  //If a name is set in the variable description, use that.
  const char* name = vlistptr->vars[varId].name;
550
  if(name) return strdup(name);
551
552
553
554
555

  //Otherwise we check if we should use the table of parameter descriptions.
  int param = vlistptr->vars[varId].param;
  int discipline, category, number;
  cdiDecodeParam(param, &number, &category, &discipline);
556
  char *result = NULL;
557
558
559
  if(discipline == 255)
    {
      int tableId = vlistptr->vars[varId].tableID;
560
561
562
563
      if(( name = tableInqParNamePtr(tableId, number) ))
        result = strdup(name);
      {
        //No luck, fall back to outputting a name of the format "var<num>".
564
        result = (char *) Malloc(3 + 3 * sizeof (int) * CHAR_BIT / 8 + 2);
565
566
567
568
569
        sprintf(result, "var%d", number);
      }
    }
  else
    {
570
      result = (char *) Malloc(5 + 2 + 3 * (3 * sizeof (int) * CHAR_BIT + 1) + 1);
571
      sprintf(result, "param%d.%d.%d", number, category, discipline);
572
573
    }
  //Finally, we fall back to outputting a name of the format "param<num>.<cat>.<dis>".
574
  return result;
575
576
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
577
578
/*
@Function  vlistInqVarLongname
Uwe Schulzweida's avatar
Uwe Schulzweida committed
579
@Title     Get the longname of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
580
581
582

@Prototype void vlistInqVarLongname(int vlistID, int varID, char *longname)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
583
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
584
    @Item  varID    Variable identifier.
585
    @Item  longname Long name of the variable. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
586
                    returned string. The maximum possible length, in characters, of
587
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
588
589

@Description
590
591
The function @func{vlistInqVarLongname} returns the longname of a variable if available,
otherwise the result is an empty string.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
592
593

@Result
594
@func{vlistInqVaeLongname} returns the longname of the variable to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
595
596
597
598
599

@EndFunction
*/
void vlistInqVarLongname(int vlistID, int varID, char *longname)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
601

602
603
604
  vlistCheckVarID(__func__, vlistID, varID);

  longname[0] = '\0';
Uwe Schulzweida's avatar
Uwe Schulzweida committed
605
606
607

  if ( vlistptr->vars[varID].longname == NULL )
    {
608
609
      int param = vlistptr->vars[varID].param;
      int pdis, pcat, pnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
610
      cdiDecodeParam(param, &pnum, &pcat, &pdis);
611
612
613
      if ( pdis == 255 )
	{
	  int code = pnum;
614
          int tableID = vlistptr->vars[varID].tableID;
615
616
617
	  if ( tableInqParLongname(tableID, code, longname) != 0 )
	    longname[0] = '\0';
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
618
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
619
620
621
622
623
624
  else
    strcpy(longname, vlistptr->vars[varID].longname);

  return;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
625
626
627
628
629
630
/*
@Function  vlistInqVarStdname
@Title     Get the standard name of a Variable

@Prototype void vlistInqVarStdname(int vlistID, int varID, char *stdname)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
631
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
632
    @Item  varID    Variable identifier.
633
    @Item  stdname  Standard name of the variable. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
634
                    returned string. The maximum possible length, in characters, of
635
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
636
637

@Description
638
639
The function @func{vlistInqVarStdname} returns the standard name of a variable if available,
otherwise the result is an empty string.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
640
641

@Result
642
@func{vlistInqVarStdname} returns the standard name of the variable to the parameter stdname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
643
644
645

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
646
647
void vlistInqVarStdname(int vlistID, int varID, char *stdname)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
648
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
649

650
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
651
652
653

  if ( vlistptr->vars[varID].stdname == NULL )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
654
      stdname[0] = '\0';
655
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
656
657
658
659
660
661
662
663
  else
    strcpy(stdname, vlistptr->vars[varID].stdname);

  return;
}

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

@Prototype void vlistInqVarUnits(int vlistID, int varID, char *units)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
668
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
669
    @Item  varID    Variable identifier.
670
    @Item  units    Units of the variable. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
671
                    returned string. The maximum possible length, in characters, of
672
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
673
674

@Description
675
676
The function @func{vlistInqVarUnits} returns the units of a variable if available,
otherwise the result is an empty string.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
677
678

@Result
679
@func{vlistInqVarUnits} returns the units of the variable to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
680
681
682
683
684

@EndFunction
*/
void vlistInqVarUnits(int vlistID, int varID, char *units)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
685
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
686

687
688
689
  vlistCheckVarID(__func__, vlistID, varID);

  units[0] = '\0';
Uwe Schulzweida's avatar
Uwe Schulzweida committed
690
691
692

  if ( vlistptr->vars[varID].units == NULL )
    {
693
694
      int param = vlistptr->vars[varID].param;
      int pdis, pcat, pnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
695
      cdiDecodeParam(param, &pnum, &pcat, &pdis);
696
697
698
      if ( pdis == 255 )
	{
	  int code = pnum;
699
	  int tableID = vlistptr->vars[varID].tableID;
700
701
702
	  if ( tableInqParUnits(tableID, code, units) != 0 )
	    units[0] = '\0';
	}
703
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
704
705
706
707
708
709
  else
    strcpy(units, vlistptr->vars[varID].units);

  return;
}

710
/* used in MPIOM ! */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
711
int vlistInqVarID(int vlistID, int code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
712
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
713
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
714

715
  for ( int varID = 0; varID < vlistptr->nvars; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
716
    {
717
718
      int param = vlistptr->vars[varID].param;
      int pdis, pcat, pnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
719
      cdiDecodeParam(param, &pnum, &pcat, &pdis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
720
      if ( pnum == code ) return varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
721
722
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
723
  return CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724
}
725

Uwe Schulzweida's avatar
Uwe Schulzweida committed
726

Uwe Schulzweida's avatar
Uwe Schulzweida committed
727
728
int vlistInqVarSize(int vlistID, int varID)
{
729
  vlistCheckVarID(__func__, vlistID, varID);
730

Uwe Schulzweida's avatar
Uwe Schulzweida committed
731
  int zaxisID, gridID, tsteptype;
732
733
  vlistInqVar(vlistID, varID, &gridID, &zaxisID, &tsteptype);

734
  int nlevs = zaxisInqSize(zaxisID);
735

736
  int gridsize = gridInqSize(gridID);
737

738
  int size = gridsize*nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
739

Uwe Schulzweida's avatar
Uwe Schulzweida committed
740
  return size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
741
742
743
744
}

/*
@Function  vlistInqVarDatatype
Uwe Schulzweida's avatar
Uwe Schulzweida committed
745
@Title     Get the data type of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
746
747
748

@Prototype int vlistInqVarDatatype(int vlistID, int varID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
749
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
750
    @Item  varID    Variable identifier.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
751
752

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
753
The function @func{vlistInqVarDatatype} returns the data type of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
754
755

@Result
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
@func{vlistInqVarDatatype} returns an identifier to the data type of the variable.
757
The valid CDI data types are @func{DATATYPE_PACK8}, @func{DATATYPE_PACK16}, @func{DATATYPE_PACK24},
758
759
@func{DATATYPE_FLT32}, @func{DATATYPE_FLT64}, @func{DATATYPE_INT8}, @func{DATATYPE_INT16} and 
@func{DATATYPE_INT32}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
760
761
762
763
764

@EndFunction
*/
int vlistInqVarDatatype(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
765
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
766

767
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
768

Uwe Schulzweida's avatar
Uwe Schulzweida committed
769
  return vlistptr->vars[varID].datatype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
770
771
772
}


773
774
int vlistInqVarNumber(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
775
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
776

777
  vlistCheckVarID(__func__, vlistID, varID);
778

779
  int number = CDI_REAL;
780
781
  if ( vlistptr->vars[varID].datatype == DATATYPE_CPX32 ||
       vlistptr->vars[varID].datatype == DATATYPE_CPX64 )
782
783
    number = CDI_COMP;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
784
  return number;
785
786
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
787
788
/*
@Function  vlistDefVarDatatype
Uwe Schulzweida's avatar
Uwe Schulzweida committed
789
@Title     Define the data type of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
790
791
792

@Prototype void vlistDefVarDatatype(int vlistID, int varID, int datatype)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
793
794
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}.
    @Item  varID    Variable identifier.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
795
    @Item  datatype The data type identifier.
796
                    The valid CDI data types are @func{DATATYPE_PACK8}, @func{DATATYPE_PACK16},
797
798
                    @func{DATATYPE_PACK24}, @func{DATATYPE_FLT32}, @func{DATATYPE_FLT64},
                    @func{DATATYPE_INT8}, @func{DATATYPE_INT16} and @func{DATATYPE_INT32}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
799
800

@Description
801
The function @func{vlistDefVarDatatype} defines the data type of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
802
803
804
805
806

@EndFunction
*/
void vlistDefVarDatatype(int vlistID, int varID, int datatype)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
807
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
808

809
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
810

811
812
813
814
815
816
817
818
819
820
821
822
823
824
  if (vlistptr->vars[varID].datatype != datatype)
    {
      vlistptr->vars[varID].datatype = datatype;

      if ( vlistptr->vars[varID].missvalused == FALSE )
        switch (datatype)
          {
          case DATATYPE_INT8:   vlistptr->vars[varID].missval = -SCHAR_MAX; break;
          case DATATYPE_UINT8:  vlistptr->vars[varID].missval =  UCHAR_MAX; break;
          case DATATYPE_INT16:  vlistptr->vars[varID].missval = -SHRT_MAX;  break;
          case DATATYPE_UINT16: vlistptr->vars[varID].missval =  USHRT_MAX; break;
          case DATATYPE_INT32:  vlistptr->vars[varID].missval = -INT_MAX;   break;
          case DATATYPE_UINT32: vlistptr->vars[varID].missval =  UINT_MAX;  break;
          }
Thomas Jahns's avatar
Thomas Jahns committed
825
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
826
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
827
828
}

829

Uwe Schulzweida's avatar
Uwe Schulzweida committed
830
831
void vlistDefVarInstitut(int vlistID, int varID, int instID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
832
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
833
834
835
  if (vlistptr->vars[varID].instID != instID)
    {
      vlistptr->vars[varID].instID = instID;
Thomas Jahns's avatar
Thomas Jahns committed
836
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
837
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
838
839
}

840

Uwe Schulzweida's avatar
Uwe Schulzweida committed
841
842
int vlistInqVarInstitut(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
843
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
844
  return vlistptr->vars[varID].instID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
845
846
}

847

Uwe Schulzweida's avatar
Uwe Schulzweida committed
848
849
void vlistDefVarModel(int vlistID, int varID, int modelID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
850
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
851
852
853
  if (vlistptr->vars[varID].modelID != modelID)
    {
      vlistptr->vars[varID].modelID = modelID;
Thomas Jahns's avatar
Thomas Jahns committed
854
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
855
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
856
857
}

858

Uwe Schulzweida's avatar
Uwe Schulzweida committed
859
860
int vlistInqVarModel(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
861
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
862
  return vlistptr->vars[varID].modelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
863
864
}

865

Uwe Schulzweida's avatar
Uwe Schulzweida committed
866
867
void vlistDefVarTable(int vlistID, int varID, int tableID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
868
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
869

870
871
872
873
  if (vlistptr->vars[varID].tableID != tableID)
    {
      vlistptr->vars[varID].tableID = tableID;
      int tablenum = tableInqNum(tableID);
874

875
      int param = vlistptr->vars[varID].param;
876

877
878
879
      int pnum, pcat, pdis;
      cdiDecodeParam(param, &pnum, &pcat, &pdis);
      vlistptr->vars[varID].param = cdiEncodeParam(pnum, tablenum, pdis);
Thomas Jahns's avatar
Thomas Jahns committed
880
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
881
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
882
883
}

884

Uwe Schulzweida's avatar
Uwe Schulzweida committed
885
886
int vlistInqVarTable(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
887
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
888
  return vlistptr->vars[varID].tableID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
889
890
891
892
}

/*
@Function  vlistDefVarName
Uwe Schulzweida's avatar
Uwe Schulzweida committed
893
@Title     Define the name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
894
895
896

@Prototype void vlistDefVarName(int vlistID, int varID, const char *name)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
897
898
899
    @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
900
901

@Description
902
The function @func{vlistDefVarName} defines the name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
903
904
905
906
907

@EndFunction
*/
void vlistDefVarName(int vlistID, int varID, const char *name)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
908
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
909

910
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
911

912
  if ( name )
913
    {
914
915
      if ( vlistptr->vars[varID].name )
	{
916
	  Free(vlistptr->vars[varID].name);
917
918
	  vlistptr->vars[varID].name = NULL;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
919

920
      vlistptr->vars[varID].name = strdupx(name);
Thomas Jahns's avatar
Thomas Jahns committed
921
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
922
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
923
924
925
926
}

/*
@Function  vlistDefVarLongname
Uwe Schulzweida's avatar
Uwe Schulzweida committed
927
@Title     Define the long name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
928
929
930

@Prototype void vlistDefVarLongname(int vlistID, int varID, const char *longname)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
931
932
933
    @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
934
935

@Description
936
The function @func{vlistDefVarLongname} defines the long name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
937
938
939
940
941

@EndFunction
*/
void vlistDefVarLongname(int vlistID, int varID, const char *longname)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
942
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
943

944
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
945

946
  if ( longname )
947
    {
948
949
      if ( vlistptr->vars[varID].longname )
	{
950
	  Free(vlistptr->vars[varID].longname);
951
952
	  vlistptr->vars[varID].longname = 0;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
953

954
      vlistptr->vars[varID].longname = strdupx(longname);
Thomas Jahns's avatar
Thomas Jahns committed
955
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
956
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
957
958
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
959
960
961
962
963
964
/*
@Function  vlistDefVarStdname
@Title     Define the standard name of a Variable

@Prototype void vlistDefVarStdname(int vlistID, int varID, const char *stdname)
@Parameter