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 "cdi_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
static
int vlistvarNewEntry(int vlistID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
73
74
{
  int varID = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
76
77
  int vlistvarSize = vlistptr->varsAllocated;
  var_t *vlistvar = vlistptr->vars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
79
80
81
82
83
84
  /*
    Look for a free slot in vlistvar.
    (Create the table the first time through).
  */
  if ( ! vlistvarSize )
    {
      vlistvarSize = 2;
85
      vlistvar = (var_t *) Malloc((size_t)vlistvarSize * sizeof (var_t));
86
87
      for ( int i = 0; i < vlistvarSize; i++ )
	vlistvar[i].isUsed = false;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
88
89
90
    }
  else
    {
Thomas Jahns's avatar
Thomas Jahns committed
91
92
      while (varID < vlistvarSize && vlistvar[varID].isUsed)
        ++varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
94
95
96
97
98
    }
  /*
    If the table overflows, double its size.
  */
  if ( varID == vlistvarSize )
    {
99
      vlistvar = (var_t *) Realloc(vlistvar,
Thomas Jahns's avatar
Thomas Jahns committed
100
101
                                   (size_t)(vlistvarSize *= 2) * sizeof(var_t));
      for ( int i = varID; i < vlistvarSize; i++ )
102
	vlistvar[i].isUsed = false;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
104
105
106
107
108
109
    }

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

  vlistvarInitEntry(vlistID, varID);

110
  vlistptr->vars[varID].isUsed = true;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111

Uwe Schulzweida's avatar
Uwe Schulzweida committed
112
  return varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
113
114
}

115
void vlistCheckVarID(const char *caller, int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
116
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119

  if ( vlistptr == NULL )
120
    Errorc("vlist undefined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
122

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

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

129

130
131
132
133
134
135
136
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
137
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
138
  vlistptr->nvars++;
139
140
  vlistptr->vars[varID].gridID    = gridID;
  vlistptr->vars[varID].zaxisID   = zaxisID;
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  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
156
157

  return varID;
158
159
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
161
/*
@Function  vlistDefVar
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
@Title     Define a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163

164
@Prototype int vlistDefVar(int vlistID, int gridID, int zaxisID, int tsteptype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
@Parameter
166
167
168
169
    @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.
170
171
                     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
172
173

@Description
174
The function @func{vlistDefVar} adds a new variable to vlistID.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175
176

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

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

204
205
206
207
208
209
void
cdiVlistCreateVarLevInfo(vlist_t *vlistptr, int varID)
{
  xassert(varID >= 0 && varID < vlistptr->nvars
          && vlistptr->vars[varID].levinfo == NULL);
  int zaxisID = vlistptr->vars[varID].zaxisID;
210
  size_t nlevs = (size_t)zaxisInqSize(zaxisID);
211

212
  vlistptr->vars[varID].levinfo
213
    = (levinfo_t*) Malloc((size_t)nlevs * sizeof(levinfo_t));
214

215
216
  for (size_t levID = 0; levID < nlevs; levID++ )
    vlistptr->vars[varID].levinfo[levID] = DEFAULT_LEVINFO((int)levID);
217
218
}

219
220
221
222
223
224
/*
@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
225
226
227
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}.
    @Item  varID    Variable identifier.
    @Item  param    Parameter number.
228
229
230
231
232
233
234
235

@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
236
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
237

238
  vlistCheckVarID(__func__, vlistID, varID);
239

240
241
242
  if (vlistptr->vars[varID].param != param)
    {
      vlistptr->vars[varID].param = param;
Thomas Jahns's avatar
Thomas Jahns committed
243
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
244
    }
245
246
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
248
/*
@Function  vlistDefVarCode
Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
@Title     Define the code number of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
251
252

@Prototype void vlistDefVarCode(int vlistID, int varID, int code)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
253
254
255
    @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
256
257

@Description
258
The function @func{vlistDefVarCode} defines the code number of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259
260
261
262
263

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

266
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
267

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


280
void vlistInqVar(int vlistID, int varID, int *gridID, int *zaxisID, int *tsteptype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
281
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
282
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
283

284
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
285

286
287
288
  *gridID    = vlistptr->vars[varID].gridID;
  *zaxisID   = vlistptr->vars[varID].zaxisID;
  *tsteptype = vlistptr->vars[varID].tsteptype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
290
291
292

  return;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
293
294
295
296
297
298
/*
@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
299
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
300
    @Item  varID    Variable identifier.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
301
302

@Description
303
The function @func{vlistInqVarGrid} returns the grid ID of a Variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
304
305

@Result
306
@func{vlistInqVarGrid} returns the grid ID of the Variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
307
308
309

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
311
int vlistInqVarGrid(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
312
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313

314
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
315

Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
  return vlistptr->vars[varID].gridID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
318
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
319
320
321
322
323
324
/*
@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
325
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
326
    @Item  varID    Variable identifier.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
327
328
329
330
331
332
333
334
335

@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
336
337
int vlistInqVarZaxis(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
338
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
339

340
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341

Uwe Schulzweida's avatar
Uwe Schulzweida committed
342
  return vlistptr->vars[varID].zaxisID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
343
344
}

345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

/*
@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
363
364

  return vlistptr->vars[varID].subtypeID;
365
366
367
}


368
369
370
371
372
373
/*
@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
374
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
375
    @Item  varID    Variable identifier.
376
377
378
379
380
381
382
383
384
385
386

@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
387
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
388

389
  vlistCheckVarID(__func__, vlistID, varID);
390

Uwe Schulzweida's avatar
Uwe Schulzweida committed
391
  return vlistptr->vars[varID].param;
392
393
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
394
395
/*
@Function  vlistInqVarCode
Uwe Schulzweida's avatar
Uwe Schulzweida committed
396
@Title     Get the Code number of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
397
398
399

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

@Description
404
The function @func{vlistInqVarCode} returns the code number of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
405
406

@Result
407
@func{vlistInqVarCode} returns the code number of the variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
408
409
410
411
412

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

415
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
416

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

  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
428
  return code;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
430
431
}


432
const char *vlistInqVarNamePtr(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
433
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
434
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
435

436
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
437

Uwe Schulzweida's avatar
Uwe Schulzweida committed
438
  return vlistptr->vars[varID].name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
439
440
}

441

442
const char *vlistInqVarLongnamePtr(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
443
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
444
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
445

446
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
447

Uwe Schulzweida's avatar
Uwe Schulzweida committed
448
  return vlistptr->vars[varID].longname;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
449
450
}

451

452
const char *vlistInqVarStdnamePtr(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
453
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
455

456
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
457

Uwe Schulzweida's avatar
Uwe Schulzweida committed
458
  return vlistptr->vars[varID].stdname;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
459
460
}

461

462
const char *vlistInqVarUnitsPtr(int vlistID, int varID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
463
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
464
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
465

466
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
467

Uwe Schulzweida's avatar
Uwe Schulzweida committed
468
  return vlistptr->vars[varID].units;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
469
470
471
472
}

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

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

@Description
484
The function @func{vlistInqVarName} returns the name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486

@Result
487
488
@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
489
490
491
492
493

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

496
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
497
498
499

  if ( vlistptr->vars[varID].name == NULL )
    {
500
501
      int param = vlistptr->vars[varID].param;
      int pdis, pcat, pnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
      cdiDecodeParam(param, &pnum, &pcat, &pdis);
503
504
505
      if ( pdis == 255 )
	{
	  int code = pnum;
506
	  int tableID = vlistptr->vars[varID].tableID;
507
508
509
510
511
512
513
	  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
514
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
515
  else
516
    strcpy(name, vlistptr->vars[varID].name);   //FIXME: This may overrun the provided buffer.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
518
519
520

  return;
}

521
522
523
524
525
526
527
528
529
/*
@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.

530
@Return A pointer to a malloc'ed string. Must be cleaned up with Free().
531
532
533

@Description
This is the buffer overflow immune version of vlistInqVarName().
534
The memory for the returned string is allocated to fit the string via Malloc().
535
536
537
538
539
540
541
542
543
544

@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;
545
  if(name) return strdup(name);
546
547
548
549
550

  //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);
551
  char *result = NULL;
552
553
554
  if(discipline == 255)
    {
      int tableId = vlistptr->vars[varId].tableID;
555
556
557
558
      if(( name = tableInqParNamePtr(tableId, number) ))
        result = strdup(name);
      {
        //No luck, fall back to outputting a name of the format "var<num>".
559
        result = (char *) Malloc(3 + 3 * sizeof (int) * CHAR_BIT / 8 + 2);
560
561
562
563
564
        sprintf(result, "var%d", number);
      }
    }
  else
    {
565
      result = (char *) Malloc(5 + 2 + 3 * (3 * sizeof (int) * CHAR_BIT + 1) + 1);
566
      sprintf(result, "param%d.%d.%d", number, category, discipline);
567
568
    }
  //Finally, we fall back to outputting a name of the format "param<num>.<cat>.<dis>".
569
  return result;
570
571
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
572
573
/*
@Function  vlistInqVarLongname
Uwe Schulzweida's avatar
Uwe Schulzweida committed
574
@Title     Get the longname of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
575
576
577

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

@Description
585
586
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
587
588

@Result
589
@func{vlistInqVaeLongname} returns the longname of the variable to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
590
591
592
593
594

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

597
598
599
  vlistCheckVarID(__func__, vlistID, varID);

  longname[0] = '\0';
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600
601
602

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

  return;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
620
621
622
623
624
625
/*
@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
626
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate} or @fref{streamInqVlist}.
Deike Kleberg's avatar
Deike Kleberg committed
627
    @Item  varID    Variable identifier.
628
    @Item  stdname  Standard name of the variable. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
629
                    returned string. The maximum possible length, in characters, of
630
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
631
632

@Description
633
634
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
635
636

@Result
637
@func{vlistInqVarStdname} returns the standard name of the variable to the parameter stdname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
638
639
640

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
641
642
void vlistInqVarStdname(int vlistID, int varID, char *stdname)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
643
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
644

645
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
646
647
648

  if ( vlistptr->vars[varID].stdname == NULL )
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
649
      stdname[0] = '\0';
650
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
651
652
653
654
655
656
657
658
  else
    strcpy(stdname, vlistptr->vars[varID].stdname);

  return;
}

/*
@Function  vlistInqVarUnits
Uwe Schulzweida's avatar
Uwe Schulzweida committed
659
@Title     Get the units of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
660
661
662

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

@Description
670
671
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
672
673

@Result
674
@func{vlistInqVarUnits} returns the units of the variable to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
675
676
677
678
679

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

682
683
684
  vlistCheckVarID(__func__, vlistID, varID);

  units[0] = '\0';
Uwe Schulzweida's avatar
Uwe Schulzweida committed
685
686
687

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

  return;
}

705
/* used in MPIOM ! */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
706
int vlistInqVarID(int vlistID, int code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
707
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
708
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
709

710
  for ( int varID = 0; varID < vlistptr->nvars; varID++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
711
    {
712
713
      int param = vlistptr->vars[varID].param;
      int pdis, pcat, pnum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
714
      cdiDecodeParam(param, &pnum, &pcat, &pdis);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
715
      if ( pnum == code ) return varID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
716
717
    }

Uwe Schulzweida's avatar
Uwe Schulzweida committed
718
  return CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
719
}
720

Uwe Schulzweida's avatar
Uwe Schulzweida committed
721

Uwe Schulzweida's avatar
Uwe Schulzweida committed
722
723
int vlistInqVarSize(int vlistID, int varID)
{
724
  vlistCheckVarID(__func__, vlistID, varID);
725

Uwe Schulzweida's avatar
Uwe Schulzweida committed
726
  int zaxisID, gridID, tsteptype;
727
728
  vlistInqVar(vlistID, varID, &gridID, &zaxisID, &tsteptype);

729
  int nlevs = zaxisInqSize(zaxisID);
730

731
  int gridsize = gridInqSize(gridID);
732

733
  int size = gridsize*nlevs;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
734

Uwe Schulzweida's avatar
Uwe Schulzweida committed
735
  return size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
736
737
738
739
}

/*
@Function  vlistInqVarDatatype
Uwe Schulzweida's avatar
Uwe Schulzweida committed
740
@Title     Get the data type of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
741
742
743

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

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
748
The function @func{vlistInqVarDatatype} returns the data type of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
749
750

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

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

762
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
763

Uwe Schulzweida's avatar
Uwe Schulzweida committed
764
  return vlistptr->vars[varID].datatype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
765
766
767
}


768
769
int vlistInqVarNumber(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
770
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
771

772
  vlistCheckVarID(__func__, vlistID, varID);
773

774
  int number = CDI_REAL;
775
776
  if ( vlistptr->vars[varID].datatype == DATATYPE_CPX32 ||
       vlistptr->vars[varID].datatype == DATATYPE_CPX64 )
777
778
    number = CDI_COMP;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
779
  return number;
780
781
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
782
783
/*
@Function  vlistDefVarDatatype
Uwe Schulzweida's avatar
Uwe Schulzweida committed
784
@Title     Define the data type of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
785
786
787

@Prototype void vlistDefVarDatatype(int vlistID, int varID, int datatype)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
788
789
    @Item  vlistID  Variable list ID, from a previous call to @fref{vlistCreate}.
    @Item  varID    Variable identifier.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
790
    @Item  datatype The data type identifier.
791
                    The valid CDI data types are @func{DATATYPE_PACK8}, @func{DATATYPE_PACK16},
792
793
                    @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
794
795

@Description
796
The function @func{vlistDefVarDatatype} defines the data type of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
797
798
799
800
801

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

804
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
805

806
807
808
809
810
811
812
813
814
815
816
817
818
819
  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
820
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
821
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
822
823
}

824

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

835

Uwe Schulzweida's avatar
Uwe Schulzweida committed
836
837
int vlistInqVarInstitut(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
838
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
839
  return vlistptr->vars[varID].instID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
840
841
}

842

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

853

Uwe Schulzweida's avatar
Uwe Schulzweida committed
854
855
int vlistInqVarModel(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
856
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
857
  return vlistptr->vars[varID].modelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
858
859
}

860

Uwe Schulzweida's avatar
Uwe Schulzweida committed
861
862
void vlistDefVarTable(int vlistID, int varID, int tableID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
863
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
864

865
866
867
868
  if (vlistptr->vars[varID].tableID != tableID)
    {
      vlistptr->vars[varID].tableID = tableID;
      int tablenum = tableInqNum(tableID);
869

870
      int param = vlistptr->vars[varID].param;
871

872
873
874
      int pnum, pcat, pdis;
      cdiDecodeParam(param, &pnum, &pcat, &pdis);
      vlistptr->vars[varID].param = cdiEncodeParam(pnum, tablenum, pdis);
Thomas Jahns's avatar
Thomas Jahns committed
875
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
876
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
877
878
}

879

Uwe Schulzweida's avatar
Uwe Schulzweida committed
880
881
int vlistInqVarTable(int vlistID, int varID)
{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
882
  vlist_t *vlistptr = vlist_to_pointer(vlistID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
883
  return vlistptr->vars[varID].tableID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
884
885
886
887
}

/*
@Function  vlistDefVarName
Uwe Schulzweida's avatar
Uwe Schulzweida committed
888
@Title     Define the name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
889
890
891

@Prototype void vlistDefVarName(int vlistID, int varID, const char *name)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
892
893
894
    @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
895
896

@Description
897
The function @func{vlistDefVarName} defines the name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
898
899
900
901
902

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

905
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
906

907
  if ( name )
908
    {
909
910
      if ( vlistptr->vars[varID].name )
	{
911
	  Free(vlistptr->vars[varID].name);
912
913
	  vlistptr->vars[varID].name = NULL;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
914

915
      vlistptr->vars[varID].name = strdupx(name);
Thomas Jahns's avatar
Thomas Jahns committed
916
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
917
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
918
919
920
921
}

/*
@Function  vlistDefVarLongname
Uwe Schulzweida's avatar
Uwe Schulzweida committed
922
@Title     Define the long name of a Variable
Uwe Schulzweida's avatar
Uwe Schulzweida committed
923
924
925

@Prototype void vlistDefVarLongname(int vlistID, int varID, const char *longname)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
926
927
928
    @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
929
930

@Description
931
The function @func{vlistDefVarLongname} defines the long name of a variable.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
932
933
934
935
936

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

939
  vlistCheckVarID(__func__, vlistID, varID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
940

941
  if ( longname )
942
    {
943
944
      if ( vlistptr->vars[varID].longname )
	{
945
	  Free(vlistptr->vars[varID].longname);
946
947
	  vlistptr->vars[varID].longname = 0;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
948

949
      vlistptr->vars[varID].longname = strdupx(longname);
Thomas Jahns's avatar
Thomas Jahns committed
950
      reshSetStatus(vlistID, &vlistOps, RESH_DESYNC_IN_USE);
951
    }