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

#include <ctype.h>
6
#include <stddef.h>
7
#include <string.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
8
9
10

#include "dmemory.h"
#include "cdi.h"
11
#include "cdi_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
12
13
14
15
16
17
18
19


/*int TableDefine = 0; */ /* Define new table also if the entry already exist */
                          /* This is needed for createtable */

#include "tablepar.h"
#include "table.h"

Uwe Schulzweida's avatar
Uwe Schulzweida committed
20
#define MAX_TABLE  256
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22
23
24
#define MAX_PARS   1024

typedef struct
{
25
  bool   used;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
27
28
29
  int    npars;
  int    modelID;
  int    number;
  char  *name;
30
  param_type *pars;
31
}
32
paramtab_type;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33

34
static paramtab_type parTable[MAX_TABLE];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
35
36
37
38
39
40
static int  parTableSize = MAX_TABLE;
static int  parTableNum  = 0;
static int  ParTableInit = 0;

static char *tablePath = NULL;

41
42
static void tableDefModelID(int tableID, int modelID);
static void tableDefNum(int tableID, int tablenum);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
43

44
45
static
void tableDefEntry(int tableID, int id, int ltype, const char *name,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
47
		   const char *longname, const char *units)
{
48
  if ( tableID >= 0 && tableID < MAX_TABLE && parTable[tableID].used) { } else
49
    Error("Invalid table ID %d", tableID);
50
51

  int item = parTable[tableID].npars++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
52
  parTable[tableID].pars[item].id       = id;
53
  parTable[tableID].pars[item].ltype    = ltype;
54
  parTable[tableID].pars[item].dupflags = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
56
57
58
  parTable[tableID].pars[item].name     = NULL;
  parTable[tableID].pars[item].longname = NULL;
  parTable[tableID].pars[item].units    = NULL;

59
  if ( name && name[0] )
60
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
61
      parTable[tableID].pars[item].name     = strdupx(name);
62
63
      parTable[tableID].pars[item].dupflags |= TABLE_DUP_NAME;
    }
64
  if ( longname && longname[0] )
65
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
      parTable[tableID].pars[item].longname = strdupx(longname);
67
68
      parTable[tableID].pars[item].dupflags |= TABLE_DUP_LONGNAME;
    }
69
  if ( units && units[0] )
70
    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
71
      parTable[tableID].pars[item].units    = strdupx(units);
72
73
      parTable[tableID].pars[item].dupflags |= TABLE_DUP_UNITS;
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
75
}

76
static void tableLink(int tableID, const param_type *pars, int npars)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
{
78
  for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80
    {
      parTable[tableID].pars[item].id       = pars[item].id;
81
      parTable[tableID].pars[item].dupflags = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
82
83
84
85
86
87
88
89
      parTable[tableID].pars[item].name     = pars[item].name;
      parTable[tableID].pars[item].longname = pars[item].longname;
      parTable[tableID].pars[item].units    = pars[item].units;
    }

  parTable[tableID].npars = npars;
}

90
static void parTableInitEntry(int tableID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
{
92
  parTable[tableID].used    = false;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
94
  parTable[tableID].pars    = NULL;
  parTable[tableID].npars   = 0;
95
96
  parTable[tableID].modelID = CDI_UNDEFID;
  parTable[tableID].number  = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
98
99
  parTable[tableID].name    = NULL;
}

100
static void tableGetPath(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
101
{
102
  char *path = getenv("TABLEPATH");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
103
104
105
106
107
108
109

  if ( path ) tablePath = strdupx(path);
  /*
  printf("tablePath = %s\n", tablePath);
  */
}

110
111
112
113
114
115
116
117
118
static void parTableFinalize(void)
{
  for (int tableID = 0; tableID < MAX_TABLE; ++tableID)
    if (parTable[tableID].used)
      {
        int npars = parTable[tableID].npars;
        for (int item = 0; item < npars; ++item)
          {
            if (parTable[tableID].pars[item].dupflags & TABLE_DUP_NAME)
119
              Free((void *)parTable[tableID].pars[item].name);
120
            if (parTable[tableID].pars[item].dupflags & TABLE_DUP_LONGNAME)
121
              Free((void *)parTable[tableID].pars[item].longname);
122
            if (parTable[tableID].pars[item].dupflags & TABLE_DUP_UNITS)
123
              Free((void *)parTable[tableID].pars[item].units);
124
          }
125
126
        Free(parTable[tableID].pars);
        Free(parTable[tableID].name);
127
128
129
      }
}

130
static void parTableInit(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
{
  ParTableInit = 1;

134
  atexit(parTableFinalize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
135
136
  if ( cdiPartabIntern )
    tableDefault();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
140

  tableGetPath();
}

141
static int tableNewEntry()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{
  int tableID = 0;
  static int init = 0;

  if ( ! init )
    {
      for ( tableID = 0; tableID < parTableSize; tableID++ )
	parTableInitEntry(tableID);
      init = 1;
    }

  /*
    Look for a free slot in parTable.
  */
  for ( tableID = 0; tableID < parTableSize; tableID++ )
    {
      if ( ! parTable[tableID].used ) break;
    }

  if ( tableID == parTableSize )
162
    Error("no more entries!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163

164
  parTable[tableID].used = true;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
166
  parTableNum++;

167
  return tableID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168
169
}

170
171
static int
decodeForm1(char *pline, char *name, char *longname, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
172
173
174
{
  char *pstart, *pend;

175
  /* FIXME: parse success isn't verified */
176
  /* long level =  */strtol(pline, &pline, 10);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
178
179
180
  while ( isspace((int) *pline) ) pline++;

  pstart = pline;
  while ( ! (isspace((int) *pline) || *pline == 0) ) pline++;
181
  size_t len = (size_t)(pline - pstart);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
183
  if ( len > 0 )
    {
184
      memcpy(name, pstart, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
185
186
187
      name[len] = 0;
    }
  else
188
    return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
189

190
  if ( pline[0] == 0 ) return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191

192
  /* Format 1 : code name add mult longname [units] */
193
  /* FIXME: successful parse isn't verified */
194
  /* double add  =  */strtod(pline, &pline);
195
  /* FIXME: successful parse isn't verified */
196
  /* double mult =  */strtod(pline, &pline);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
198
199
200

  while ( isspace((int) *pline) ) pline++;

  len = strlen(pline);
201
  if ( len > 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
202
203
204
    {
      pstart = pline;
      pend = strrchr(pline, '[');
205
206
      if ( pend == pstart )
        len = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
      else
208
209
210
211
212
213
214
215
        {
          if ( pend )
            pend--;
          else
            pend = pstart + len;
          while ( isspace((int) *pend) ) pend--;
          len = (size_t)(pend - pstart + 1);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216
217
      if ( len > 0 )
	{
218
	  memcpy(longname, pstart, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
219
220
221
222
223
224
225
226
	  longname[len] = 0;
	}
      pstart = strrchr(pline, '[');
      if ( pstart )
	{
	  pstart++;
	  while ( isspace((int) *pstart) ) pstart++;
	  pend = strchr(pstart, ']');
227
	  if ( ! pend ) return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
228
229
	  pend--;
	  while ( isspace((int) *pend) ) pend--;
230
	  len = (size_t)(pend - pstart + 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
231
232
	  if ( len > 0 )
	    {
233
	      memcpy(units, pstart, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
234
	      units[len] = 0;
235
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236
237
	}
    }
238

239
  return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
240
241
}

242
243
static int
decodeForm2(char *pline, char *name, char *longname, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
244
245
246
247
248
249
250
251
{
  /* Format 2 : code | name | longname | units */
  char *pend;

  pline = strchr(pline, '|');
  pline++;

  while ( isspace((int) *pline) ) pline++;
252
  if (*pline != '|')
Uwe Schulzweida's avatar
Uwe Schulzweida committed
253
    {
254
255
256
257
258
      pend = strchr(pline, '|');
      if ( ! pend )
        {
          pend = pline;
          while ( ! isspace((int) *pend) ) pend++;
Thomas Jahns's avatar
Thomas Jahns committed
259
          size_t len = (size_t)(pend - pline);
260
261
262
263
264
          if ( len > 0 )
            {
              memcpy(name, pline, len);
              name[len] = 0;
            }
265
          return 0;
266
267
268
269
270
        }
      else
        {
          pend--;
          while ( isspace((int) *pend) ) pend--;
Thomas Jahns's avatar
Thomas Jahns committed
271
          size_t len = (size_t)(pend - pline + 1);
272
273
274
275
276
277
          if ( len > 0 )
            {
              memcpy(name, pline, len);
              name[len] = 0;
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
278
279
    }
  else
280
    name[0] = '\0';
Uwe Schulzweida's avatar
Uwe Schulzweida committed
281
282
283
284
285
286
287
288

  pline = strchr(pline, '|');
  pline++;
  while ( isspace((int) *pline) ) pline++;
  pend = strchr(pline, '|');
  if ( !pend ) pend = strchr(pline, 0);
  pend--;
  while ( isspace((int) *pend) ) pend--;
Thomas Jahns's avatar
Thomas Jahns committed
289
290
291
292
293
294
295
296
  {
    size_t len = (size_t)(pend - pline + 1);
    if ( len > 0 )
      {
        memcpy(longname, pline, len);
        longname[len] = 0;
      }
  }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
297
298
299
300
301
302
303
304
305
306

  pline = strchr(pline, '|');
  if ( pline )
    {
      pline++;
      while ( isspace((int) *pline) ) pline++;
      pend = strchr(pline, '|');
      if ( !pend ) pend = strchr(pline, 0);
      pend--;
      while ( isspace((int) *pend) ) pend--;
307
      ptrdiff_t len = pend - pline + 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
308
      if ( len < 0 ) len = 0;
309
      memcpy(units, pline, (size_t)len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
310
311
312
      units[len] = 0;
    }

313
  return 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
314
315
}

316

Uwe Schulzweida's avatar
Uwe Schulzweida committed
317
318
319
320
321
322
int tableRead(const char *tablefile)
{
  char line[1024], *pline;
  int lnr = 0;
  char name[256], longname[256], units[256];
  int err;
323
  int tableID = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324

325
326
  FILE *tablefp = fopen(tablefile, "r");
  if ( tablefp == NULL ) return tableID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
327

328
  char *tablename = (char* )strrchr(tablefile, '/');
Uwe Schulzweida's avatar
Uwe Schulzweida committed
329
330
331
  if ( tablename == 0 ) tablename = (char *) tablefile;
  else                  tablename++;

332
  tableID = tableDef(-1, 0, tablename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
333
334
335

  while ( fgets(line, 1023, tablefp) )
    {
336
      size_t len = strlen(line);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
337
338
      if ( line[len-1] == '\n' ) line[len-1] = '\0';
      lnr++;
339
340
      int id      = CDI_UNDEFID;
      int ltype   = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
      name[0]     = 0;
      longname[0] = 0;
      units[0]    = 0;
      if ( line[0] == '#' ) continue;
      pline = line;

      len = strlen(pline);
      if ( len < 4 ) continue;
      while ( isspace((int) *pline) ) pline++;
      id = atoi(pline);
      /*
      if ( id > 255 ) id -= 256;
      */
      if ( id == 0 ) continue;

356
      while ( isdigit((int) *pline) ) pline++;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357

Uwe Schulzweida's avatar
Uwe Schulzweida committed
358
      if ( *pline == ';' || *pline == ':' )
359
360
361
362
363
        {
          pline++;
          ltype = atoi(pline);
          while ( isdigit((int) *pline) ) pline++;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
          if ( *pline == ';' || *pline == ':' )
365
366
367
368
369
370
371
372
            {
              pline++;
              while ( isdigit((int) *pline) ) pline++;
            }
        }

      while ( isdigit((int) *pline) ) pline++;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
374
375
376
377
378
379
      if ( strchr(pline, '|') )
	err = decodeForm2(pline, name, longname, units);
      else
	err = decodeForm1(pline, name, longname, units);

      if ( err ) continue;

380
      if ( name[0] == 0 ) sprintf(name, "var%d", id);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381

382
      tableDefEntry(tableID, id, ltype, name, longname, units);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
383
384
    }

385
  return tableID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
386
387
}

388

389
static int tableFromEnv(int modelID, int tablenum)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
391
{
  char tablename[256] = {'\0'};
Thomas Jahns's avatar
Thomas Jahns committed
392
393
  size_t tablenameLen = 0;
  int instID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
394

Thomas Jahns's avatar
Thomas Jahns committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
  const char *name2Use;
  {
    const char *modelName, *instName;
    if ( (modelName = modelInqNamePtr(modelID)) )
      name2Use = modelName;
    else if ( (instID = modelInqInstitut(modelID)) != CDI_UNDEFID
              && (instName = institutInqNamePtr(instID)) )
      name2Use = instName;
    else
      return CDI_UNDEFID;
  }
  tablenameLen = strlen(name2Use);
  memcpy(tablename, name2Use, tablenameLen);
  if ( tablenum )
    tablenameLen
      += (size_t)(sprintf(tablename+tablenameLen, "_%03d", tablenum));
  size_t lenp = 0, lenf = tablenameLen;
  if ( tablePath )
    lenp = strlen(tablePath);
  /* if (tablePath) printf("tablePath = %s\n", tablePath); */
  /* if (tablename) printf("tableName = %s\n", tablename); */
  char *tablefile = (char *) Malloc(lenp+lenf+3);
  if ( tablePath )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
418
    {
Thomas Jahns's avatar
Thomas Jahns committed
419
420
      strcpy(tablefile, tablePath);
      strcat(tablefile, "/");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
421
422
    }
  else
Thomas Jahns's avatar
Thomas Jahns committed
423
424
425
    tablefile[0] = '\0';
  strcat(tablefile, tablename);
  /* if (tablefile) printf("tableFile = %s\n", tablefile); */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
426

Thomas Jahns's avatar
Thomas Jahns committed
427
428
  int tableID = tableRead(tablefile);
  if ( tableID != CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
429
    {
Thomas Jahns's avatar
Thomas Jahns committed
430
431
      tableDefModelID(tableID, modelID);
      tableDefNum(tableID, tablenum);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
432
    }
Thomas Jahns's avatar
Thomas Jahns committed
433
434
  /* printf("tableID = %d %s\n", tableID, tablefile); */
  Free(tablefile);
435
436

  return tableID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
437
438
439
440
}

int tableInq(int modelID, int tablenum, const char *tablename)
{
441
442
  int tableID = CDI_UNDEFID;
  int modelID2 = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
  char tablefile[256] = {'\0'};

  if ( ! ParTableInit ) parTableInit();

  if ( tablename )
    {
      size_t len;
      strcpy(tablefile, tablename);
      /*
      printf("tableInq: tablefile = >%s<\n", tablefile);
      */
      /* search for internal table */
      for ( tableID = 0; tableID < MAX_TABLE; tableID++ )
	{
	  if ( parTable[tableID].used && parTable[tableID].name )
	    {
	      /* len = strlen(parTable[tableID].name); */
	      len = strlen(tablename);
461
	      if ( memcmp(parTable[tableID].name, tablename, len) == 0 ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
462
463
	    }
	}
464
      if ( tableID == MAX_TABLE ) tableID = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
465
      if ( CDI_Debug )
466
	Message("tableID = %d tablename = %s", tableID, tablename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
467
468
469
470
471
472
    }
  else
    {
      for ( tableID = 0; tableID < MAX_TABLE; tableID++ )
	{
	  if ( parTable[tableID].used )
473
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
474
475
476
477
	      if ( parTable[tableID].modelID == modelID &&
		   parTable[tableID].number  == tablenum ) break;
	    }
	}
478

479
      if ( tableID == MAX_TABLE ) tableID = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
480

481
      if ( tableID == CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
482
	{
483
	  if ( modelID != CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
484
	    {
485
486
              const char *modelName;
	      if ( (modelName = modelInqNamePtr(modelID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
		{
488
		  strcpy(tablefile, modelName);
489
490
		  size_t len = strlen(tablefile);
		  for ( size_t i = 0; i < len; i++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
491
492
493
494
		    if ( tablefile[i] == '.' ) tablefile[i] = '\0';
		  modelID2 = modelInq(-1, 0, tablefile);
		}
	    }
495
	  if ( modelID2 != CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
496
497
498
499
500
501
502
503
504
505
	    for ( tableID = 0; tableID < MAX_TABLE; tableID++ )
	      {
		if ( parTable[tableID].used )
		  {
		    if ( parTable[tableID].modelID == modelID2 &&
			 parTable[tableID].number  == tablenum ) break;
		  }
	      }
	}

506
      if ( tableID == MAX_TABLE ) tableID = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
507

508
      if ( tableID == CDI_UNDEFID && modelID != CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
510
511
512
	tableID = tableFromEnv(modelID, tablenum);

      if ( CDI_Debug )
	if ( tablename )
513
	  Message("tableID = %d tablename = %s", tableID, tablename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
514
515
    }

516
  return tableID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
518
519
520
}

int tableDef(int modelID, int tablenum, const char *tablename)
{
521
  int tableID = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
522
523
524

  if ( ! ParTableInit ) parTableInit();
  /*
525
  if ( ! (modelID == CDI_UNDEFID && tablenum == 0) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
526
527
    tableID = tableInq(modelID, tablenum, tablename);
    */
528
  if ( tableID == CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
529
530
531
532
533
    {
      tableID = tableNewEntry();

      parTable[tableID].modelID = modelID;
      parTable[tableID].number  = tablenum;
534
      if ( tablename )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
536
	parTable[tableID].name = strdupx(tablename);

537
      parTable[tableID].pars = (param_type *) Malloc(MAX_PARS * sizeof(param_type));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
538
539
    }

540
  return tableID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
542
}

543
544
static
void tableDefModelID(int tableID, int modelID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
545
546
547
548
{
  parTable[tableID].modelID = modelID;
}

549
550
static
void tableDefNum(int tableID, int tablenum)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551
552
553
554
{
  parTable[tableID].number  = tablenum;
}

555

Uwe Schulzweida's avatar
Uwe Schulzweida committed
556
557
558
559
560
561
562
int tableInqNum(int tableID)
{
  int number = 0;

  if ( tableID >= 0 && tableID < MAX_TABLE )
    number = parTable[tableID].number;

563
  return number;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
564
565
}

566

Uwe Schulzweida's avatar
Uwe Schulzweida committed
567
568
569
570
571
572
573
int tableInqModel(int tableID)
{
  int modelID = -1;

  if ( tableID >= 0 && tableID < MAX_TABLE )
    modelID = parTable[tableID].modelID;

574
  return modelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
575
576
}

577

578
static void partabCheckID(int item)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
579
580
{
  if ( item < 0 || item >= parTableSize )
581
    Error("item %d undefined!", item);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
582
583

  if ( ! parTable[item].name )
584
    Error("item %d name undefined!", item);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
585
586
}

587

588
const char *tableInqNamePtr(int tableID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
589
{
590
  const char *tablename = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
591
592

  if ( CDI_Debug )
593
    Message("tableID = %d", tableID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
595
596
597
598
599
600

  if ( ! ParTableInit ) parTableInit();

  if ( tableID >= 0 && tableID < parTableSize )
    if ( parTable[tableID].name )
      tablename = parTable[tableID].name;

601
  return tablename;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
602
603
}

604

Uwe Schulzweida's avatar
Uwe Schulzweida committed
605
606
void tableWrite(const char *ptfile, int tableID)
{
607
  size_t maxname = 4, maxlname = 10, maxunits = 2;
608
  int instID = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
609
  int center = 0, subcenter = 0;
610
  const char *instnameptr = NULL, *modelnameptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
611
612

  if ( CDI_Debug )
613
    Message("write parameter table %d to %s", tableID, ptfile);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
614

615
  if ( tableID == CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
616
    {
617
      Warning("parameter table ID undefined");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
618
619
620
621
622
      return;
    }

  partabCheckID(tableID);

623
  FILE *ptfp = fopen(ptfile, "w");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
624

625
  int npars = parTable[tableID].npars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
626

627
  for ( int item = 0; item < npars; item++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
628
    {
629
630
      if ( parTable[tableID].pars[item].name )
	{
631
	  size_t lenname = strlen(parTable[tableID].pars[item].name);
632
633
634
635
636
	  if ( lenname  > maxname )  maxname  = lenname;
	}

      if ( parTable[tableID].pars[item].longname )
	{
637
	  size_t lenlname = strlen(parTable[tableID].pars[item].longname);
638
639
640
641
642
	  if ( lenlname > maxlname ) maxlname = lenlname;
	}

      if ( parTable[tableID].pars[item].units )
	{
643
	  size_t lenunits = strlen(parTable[tableID].pars[item].units);
644
645
	  if ( lenunits > maxunits ) maxunits = lenunits;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
646
647
    }

648
649
  int tablenum = tableInqNum(tableID);
  int modelID = parTable[tableID].modelID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
  if ( modelID != CDI_UNDEFID )
    {
      modelnameptr = modelInqNamePtr(modelID);
      instID = modelInqInstitut(modelID);
    }
  if ( instID != CDI_UNDEFID )
    {
      center = institutInqCenter(instID);
      subcenter = institutInqSubcenter(instID);
      instnameptr = institutInqNamePtr(instID);
    }

  fprintf(ptfp, "# Parameter table\n");
  fprintf(ptfp, "#\n");
  if ( tablenum )
    fprintf(ptfp, "# TABLE_ID=%d\n", tablenum);
  fprintf(ptfp, "# TABLE_NAME=%s\n", parTable[tableID].name);
  if ( modelnameptr )
    fprintf(ptfp, "# TABLE_MODEL=%s\n", modelnameptr);
  if ( instnameptr )
    fprintf(ptfp, "# TABLE_INSTITUT=%s\n", instnameptr);
  if ( center )
    fprintf(ptfp, "# TABLE_CENTER=%d\n", center);
  if ( subcenter )
    fprintf(ptfp, "# TABLE_SUBCENTER=%d\n", subcenter);
  fprintf(ptfp, "#\n");
  fprintf(ptfp, "#\n");
  fprintf(ptfp, "# id       = parameter ID\n");
  fprintf(ptfp, "# name     = variable name\n");
  fprintf(ptfp, "# title    = long name (description)\n");
  fprintf(ptfp, "# units    = variable units\n");
  fprintf(ptfp, "#\n");
  fprintf(ptfp, "# The format of each record is:\n");
  fprintf(ptfp, "#\n");
  fprintf(ptfp, "# id | %-*s | %-*s | %-*s\n",
685
686
687
	  (int)maxname,  "name",
	  (int)maxlname, "title",
	  (int)maxunits, "units");
688
689

  for ( int item = 0; item < npars; item++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
690
    {
691
692
693
      const char *name = parTable[tableID].pars[item].name,
        *longname = parTable[tableID].pars[item].longname,
        *units = parTable[tableID].pars[item].units;
694
695
696
      if ( name == NULL ) name = " ";
      if ( longname == NULL ) longname = " ";
      if ( units == NULL ) units = " ";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
697
698
      fprintf(ptfp, "%4d | %-*s | %-*s | %-*s\n",
	      parTable[tableID].pars[item].id,
699
700
701
	      (int)maxname, name,
	      (int)maxlname, longname,
	      (int)maxunits, units);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
702
703
704
705
706
    }

  fclose(ptfp);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
707

708
void tableFWriteC(FILE *ptfp, int tableID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
709
{
710
  const char chelp[] = "";
711
  size_t maxname = 0, maxlname = 0, maxunits = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
712
713
714
  char tablename[256];


715
  if ( tableID == CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
716
    {
717
      Warning("parameter table ID undefined");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
718
719
720
721
722
      return;
    }

  partabCheckID(tableID);

723
  int npars = parTable[tableID].npars;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724

725
  for ( int item = 0; item < npars; item++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
726
    {
727
728
      if ( parTable[tableID].pars[item].name )
	{
729
	  size_t lenname = strlen(parTable[tableID].pars[item].name);
730
731
732
	  if ( lenname  > maxname )  maxname  = lenname;
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
733
      if ( parTable[tableID].pars[item].longname )
734
	{
735
	  size_t lenlname = strlen(parTable[tableID].pars[item].longname);
736
737
738
	  if ( lenlname > maxlname ) maxlname = lenlname;
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
739
      if ( parTable[tableID].pars[item].units )
740
	{
741
	  size_t lenunits = strlen(parTable[tableID].pars[item].units);
742
743
	  if ( lenunits > maxunits ) maxunits = lenunits;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
744
745
    }

Thomas Jahns's avatar
Thomas Jahns committed
746
747
  strncpy(tablename, parTable[tableID].name, sizeof (tablename));
  tablename[sizeof (tablename) - 1] = '\0';
748
749
750
751
752
  {
    size_t len = strlen(tablename);
    for (size_t i = 0; i < len; i++ )
      if ( tablename[i] == '.' ) tablename[i] = '_';
  }
753
  fprintf(ptfp, "static const param_type %s[] = {\n", tablename);
754

755
  for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
    {
757
758
759
760
761
      size_t len = strlen(parTable[tableID].pars[item].name),
        llen = parTable[tableID].pars[item].longname
        ? strlen(parTable[tableID].pars[item].longname) : 0,
        ulen = parTable[tableID].pars[item].units
        ? strlen(parTable[tableID].pars[item].units) : 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
762
      fprintf(ptfp, "  {%4d, -1, 0, \"%s\", %-*s%c%s%s, %-*s%c%s%s %-*s},\n",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
763
	      parTable[tableID].pars[item].id,
764
765
766
767
768
769
770
771
772
	      parTable[tableID].pars[item].name, (int)(maxname-len), chelp,
              llen?'"':' ',
              llen?parTable[tableID].pars[item].longname:"NULL",
              llen?"\"":"",
              (int)(maxlname-(llen?llen:3)), chelp,
              ulen?'"':' ',
              ulen?parTable[tableID].pars[item].units:"NULL",
              ulen?"\"":"",
              (int)(maxunits-(ulen?ulen:3)), chelp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
773
774
775
776
777
    }

  fprintf(ptfp, "};\n\n");
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
778

779
void tableInqEntry(int tableID, int id, int ltype, char *name, char *longname, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
780
{
781
782
  if ( ((tableID >= 0) & (tableID < MAX_TABLE)) | (tableID == CDI_UNDEFID) ) { } else
    Error("Invalid table ID %d", tableID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
783

784
  if ( tableID != CDI_UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
785
    {
786
787
      int npars = parTable[tableID].npars;
      for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
788
	{
789
790
791
	  if ( parTable[tableID].pars[item].id == id &&
               (parTable[tableID].pars[item].ltype == -1 || ltype == -1 ||
                parTable[tableID].pars[item].ltype == ltype) )
792
793
794
795
796
797
798
799
800
801
	    {
	      if ( name && parTable[tableID].pars[item].name )
		strcpy(name, parTable[tableID].pars[item].name);
	      if ( longname && parTable[tableID].pars[item].longname )
		strcpy(longname, parTable[tableID].pars[item].longname);
	      if ( units && parTable[tableID].pars[item].units )
		strcpy(units, parTable[tableID].pars[item].units);

	      break;
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
802
803
804
805
806
	}
    }
}


807
int tableInqParCode(int tableID, char *varname, int *code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
808
{
809
  int err = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
810

811
  if ( tableID != CDI_UNDEFID && varname != NULL )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
812
    {
813
814
      int npars = parTable[tableID].npars;
      for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
815
	{
816
817
818
819
	  if ( parTable[tableID].pars[item].name
               && strcmp(parTable[tableID].pars[item].name, varname) == 0 )
            {
              *code = parTable[tableID].pars[item].id;
820
              err = 0;
821
822
              break;
            }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
823
824
825
	}
    }

826
  return err;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
827
828
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
829

Uwe Schulzweida's avatar
Uwe Schulzweida committed
830
831
832
833
int tableInqNumber(void)
{
  if ( ! ParTableInit ) parTableInit();

834
  return parTableNum;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
835
}
836
837
838
839
840
841
842
843
844
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */