table.c 23.1 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
20
21

#undef  UNDEFID
#define UNDEFID -1

/*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
22
#define MAX_TABLE  256
Uwe Schulzweida's avatar
Uwe Schulzweida committed
23
24
25
26
#define MAX_PARS   1024

typedef struct
{
27
  int    used;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
28
29
30
31
32
  PAR   *pars;
  int    npars;
  int    modelID;
  int    number;
  char  *name;
33
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34
35
36
37
38
39
40
41
42
PARTAB;

static PARTAB parTable[MAX_TABLE];
static int  parTableSize = MAX_TABLE;
static int  parTableNum  = 0;
static int  ParTableInit = 0;

static char *tablePath = NULL;

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

46

Uwe Schulzweida's avatar
Uwe Schulzweida committed
47
48
49
50
51
void tableDefEntry(int tableID, int id, const char *name,
		   const char *longname, const char *units)
{
  int item;

52
  if ( tableID >= 0 && tableID < MAX_TABLE && parTable[tableID].used) { } else
53
    Error("Invalid table ID %d", tableID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
54
55
  item = parTable[tableID].npars++;
  parTable[tableID].pars[item].id       = id;
56
  parTable[tableID].pars[item].dupflags = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
57
58
59
60
  parTable[tableID].pars[item].name     = NULL;
  parTable[tableID].pars[item].longname = NULL;
  parTable[tableID].pars[item].units    = NULL;

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

78
static void tableLink(int tableID, const PAR *pars, int npars)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80
81
82
83
84
{
  int item;

  for ( item = 0; item < npars; item++ )
    {
      parTable[tableID].pars[item].id       = pars[item].id;
85
      parTable[tableID].pars[item].dupflags = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
87
88
89
90
91
92
93
      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;
}

94
static void parTableInitEntry(int tableID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
96
97
98
99
100
101
102
103
{
  parTable[tableID].used    = 0;
  parTable[tableID].pars    = NULL;
  parTable[tableID].npars   = 0;
  parTable[tableID].modelID = UNDEFID;
  parTable[tableID].number  = UNDEFID;
  parTable[tableID].name    = NULL;
}

104
static void tableGetPath(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
105
106
107
108
109
110
111
112
113
114
115
{
  char *path;

  path = getenv("TABLEPATH");

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

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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)
              free((void *)parTable[tableID].pars[item].name);
            if (parTable[tableID].pars[item].dupflags & TABLE_DUP_LONGNAME)
              free((void *)parTable[tableID].pars[item].longname);
            if (parTable[tableID].pars[item].dupflags & TABLE_DUP_UNITS)
              free((void *)parTable[tableID].pars[item].units);
          }
        free(parTable[tableID].pars);
        free(parTable[tableID].name);
      }
}

136
static void parTableInit(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
137
138
139
{
  ParTableInit = 1;

140
  atexit(parTableFinalize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
  if ( cdiPartabIntern )
    tableDefault();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
143
144
145
146

  tableGetPath();
}

147
static int tableNewEntry()
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{
  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 )
168
    Error("no more entries!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
170
171
172
173
174
175

  parTable[tableID].used = 1;
  parTableNum++;

  return (tableID);
}

176
177
static int
decodeForm1(char *pline, char *name, char *longname, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
179
180
{
  char *pstart, *pend;

181
  /* FIXME: parse success isn't verified */
182
  /* long level =  */strtol(pline, &pline, 10);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
183
184
185
186
  while ( isspace((int) *pline) ) pline++;

  pstart = pline;
  while ( ! (isspace((int) *pline) || *pline == 0) ) pline++;
187
  size_t len = (size_t)(pline - pstart);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
189
  if ( len > 0 )
    {
190
      memcpy(name, pstart, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
192
193
194
195
196
197
198
      name[len] = 0;
    }
  else
    return (0);

  len = strlen(pline);
  if ( len == 0 ) return (0);

199
  /* Format 1 : code name add mult longname [units] */
200
  /* FIXME: successful parse isn't verified */
201
  /* double add  =  */strtod(pline, &pline);
202
  /* FIXME: successful parse isn't verified */
203
  /* double mult =  */strtod(pline, &pline);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
205
206
207

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

  len = strlen(pline);
208
  if ( len > 0 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209
210
211
    {
      pstart = pline;
      pend = strrchr(pline, '[');
212
213
      if ( pend == pstart )
        len = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
      else
215
216
217
218
219
220
221
222
        {
          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
223
224
      if ( len > 0 )
	{
225
	  memcpy(longname, pstart, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
226
227
228
229
230
231
232
233
234
235
236
	  longname[len] = 0;
	}
      pstart = strrchr(pline, '[');
      if ( pstart )
	{
	  pstart++;
	  while ( isspace((int) *pstart) ) pstart++;
	  pend = strchr(pstart, ']');
	  if ( ! pend ) return (0);
	  pend--;
	  while ( isspace((int) *pend) ) pend--;
237
	  len = (size_t)(pend - pstart + 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
238
239
	  if ( len > 0 )
	    {
240
	      memcpy(units, pstart, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
	      units[len] = 0;
242
	    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
243
244
	}
    }
245

Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
247
248
  return (0);
}

249
250
static int
decodeForm2(char *pline, char *name, char *longname, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
252
253
{
  /* Format 2 : code | name | longname | units */
  char *pend;
254
  size_t len;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
256
257
258
259

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

  while ( isspace((int) *pline) ) pline++;
260
  if (*pline != '|')
Uwe Schulzweida's avatar
Uwe Schulzweida committed
261
    {
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
      pend = strchr(pline, '|');
      if ( ! pend )
        {
          pend = pline;
          while ( ! isspace((int) *pend) ) pend++;
          len = (size_t)(pend - pline);
          if ( len > 0 )
            {
              memcpy(name, pline, len);
              name[len] = 0;
            }
          return (0);
        }
      else
        {
          pend--;
          while ( isspace((int) *pend) ) pend--;
          len = (size_t)(pend - pline + 1);
          if ( len > 0 )
            {
              memcpy(name, pline, len);
              name[len] = 0;
            }
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
286
287
    }
  else
288
    name[0] = '\0';
Uwe Schulzweida's avatar
Uwe Schulzweida committed
289
290
291
292
293
294
295
296

  pline = strchr(pline, '|');
  pline++;
  while ( isspace((int) *pline) ) pline++;
  pend = strchr(pline, '|');
  if ( !pend ) pend = strchr(pline, 0);
  pend--;
  while ( isspace((int) *pend) ) pend--;
297
  len = (size_t)(pend - pline + 1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
298
299
  if ( len > 0 )
    {
300
      memcpy(longname, pline, len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
301
302
303
304
305
306
307
308
309
310
311
312
      longname[len] = 0;
    }

  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--;
313
      ptrdiff_t len = pend - pline + 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
314
      if ( len < 0 ) len = 0;
315
      memcpy(units, pline, (size_t)len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
      units[len] = 0;
    }

  return (0);
}

int tableRead(const char *tablefile)
{
  char line[1024], *pline;
  int lnr = 0;
  int id;
  char name[256], longname[256], units[256];
  int tableID = UNDEFID;
  int err;
  char *tablename;
  FILE *tablefp;

  tablefp = fopen(tablefile, "r");
  if ( tablefp == NULL ) return (tableID);

  tablename = strrchr(tablefile, '/');
  if ( tablename == 0 ) tablename = (char *) tablefile;
  else                  tablename++;

  tableID = tableDef(-1, 0, tablename);

  while ( fgets(line, 1023, tablefp) )
    {
344
      size_t len = strlen(line);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
      if ( line[len-1] == '\n' ) line[len-1] = '\0';
      lnr++;
      id       = CDI_UNDEFID;
      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;

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

      if ( strchr(pline, '|') )
	err = decodeForm2(pline, name, longname, units);
      else
	err = decodeForm1(pline, name, longname, units);

      if ( err ) continue;

      if ( strlen(name) == 0 ) sprintf(name, "var%d", id);

      tableDefEntry(tableID, id, name, longname, units);
    }

  return (tableID);
}

380
static int tableFromEnv(int modelID, int tablenum)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
382
383
384
385
{
  int tableID = UNDEFID;
  char tablename[256] = {'\0'};
  int tablenamefound = 0;

386
387
  const char *modelName;
  if ( (modelName = modelInqNamePtr(modelID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388
    {
389
      strcpy(tablename, modelName);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
390
391
      if ( tablenum )
	{
392
	  size_t len = strlen(tablename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
393
394
395
396
397
398
399
400
401
	  sprintf(tablename+len, "_%03d", tablenum);
	}
      tablenamefound = 1;
    }
  else
    {
      int instID = modelInqInstitut(modelID);
      if ( instID != UNDEFID )
	{
402
403
          const char *instName;
	  if ( (instName = institutInqNamePtr(instID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
404
	    {
405
	      strcpy(tablename, instName);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
407
	      if ( tablenum )
		{
408
		  size_t len = strlen(tablename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
409
410
411
412
413
414
415
416
417
		  sprintf(tablename+len, "_%03d", tablenum);
		}
	      tablenamefound = 1;
	    }
	}
    }

  if ( tablenamefound )
    {
418
      size_t lenp = 0, lenf;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
      char *tablefile = NULL;
      if ( tablePath )
	lenp = strlen(tablePath);
      lenf = strlen(tablename);
      /* if (tablePath) printf("tablePath = %s\n", tablePath); */
      /* if (tablename) printf("tableName = %s\n", tablename); */
      tablefile = (char *) malloc(lenp+lenf+3);
      if ( tablePath )
	{
	  strcpy(tablefile, tablePath);
	  strcat(tablefile, "/");
	}
      else
	tablefile[0] = '\0';
      strcat(tablefile, tablename);
      /* if (tablefile) printf("tableFile = %s\n", tablefile); */

      tableID = tableRead(tablefile);
      if ( tableID != UNDEFID )
	{
	  tableDefModelID(tableID, modelID);
	  tableDefNum(tableID, tablenum);
	}
      /* printf("tableID = %d %s\n", tableID, tablefile); */

      free(tablefile);
    }

  return (tableID);
}

int tableInq(int modelID, int tablenum, const char *tablename)
{
  int tableID = UNDEFID;
453
  int modelID2 = UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  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);
472
	      if ( memcmp(parTable[tableID].name, tablename, len) == 0 ) break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
473
474
475
476
	    }
	}
      if ( tableID == MAX_TABLE ) tableID = UNDEFID;
      if ( CDI_Debug )
477
	Message("tableID = %d tablename = %s", tableID, tablename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
478
479
480
481
482
483
    }
  else
    {
      for ( tableID = 0; tableID < MAX_TABLE; tableID++ )
	{
	  if ( parTable[tableID].used )
484
	    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486
487
488
	      if ( parTable[tableID].modelID == modelID &&
		   parTable[tableID].number  == tablenum ) break;
	    }
	}
489

Uwe Schulzweida's avatar
Uwe Schulzweida committed
490
491
492
493
494
495
      if ( tableID == MAX_TABLE ) tableID = UNDEFID;

      if ( tableID == UNDEFID )
	{
	  if ( modelID != UNDEFID )
	    {
496
497
              const char *modelName;
	      if ( (modelName = modelInqNamePtr(modelID)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
498
		{
499
		  strcpy(tablefile, modelName);
500
501
		  size_t len = strlen(tablefile);
		  for ( size_t i = 0; i < len; i++)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
		    if ( tablefile[i] == '.' ) tablefile[i] = '\0';
		  modelID2 = modelInq(-1, 0, tablefile);
		}
	    }
	  if ( modelID2 != UNDEFID )
	    for ( tableID = 0; tableID < MAX_TABLE; tableID++ )
	      {
		if ( parTable[tableID].used )
		  {
		    if ( parTable[tableID].modelID == modelID2 &&
			 parTable[tableID].number  == tablenum ) break;
		  }
	      }
	}

      if ( tableID == MAX_TABLE ) tableID = UNDEFID;

      if ( tableID == UNDEFID && modelID != UNDEFID )
	tableID = tableFromEnv(modelID, tablenum);

      if ( CDI_Debug )
	if ( tablename )
524
	  Message("tableID = %d tablename = %s", tableID, tablename);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
    }

  return (tableID);
}

int tableDef(int modelID, int tablenum, const char *tablename)
{
  int tableID = UNDEFID;

  if ( ! ParTableInit ) parTableInit();
  /*
  if ( ! (modelID == UNDEFID && tablenum == 0) )
    tableID = tableInq(modelID, tablenum, tablename);
    */
  if ( tableID == UNDEFID )
    {
      tableID = tableNewEntry();

      parTable[tableID].modelID = modelID;
      parTable[tableID].number  = tablenum;
545
      if ( tablename )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
546
547
548
549
550
551
552
553
	parTable[tableID].name = strdupx(tablename);

      parTable[tableID].pars = (PAR *) malloc(MAX_PARS * sizeof(PAR));
    }

  return (tableID);
}

554
static void tableDefModelID(int tableID, int modelID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
556
557
558
{
  parTable[tableID].modelID = modelID;
}

559
static void tableDefNum(int tableID, int tablenum)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
{
  parTable[tableID].number  = tablenum;
}

int tableInqNum(int tableID)
{
  int number = 0;

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

  return (number);
}

int tableInqModel(int tableID)
{
  int modelID = -1;

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

  return (modelID);
}

584
static void partabCheckID(int item)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
585
586
{
  if ( item < 0 || item >= parTableSize )
587
    Error("item %d undefined!", item);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
588
589

  if ( ! parTable[item].name )
590
    Error("item %d name undefined!", item);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
591
592
593
594
595
596
597
}

char *tableInqNamePtr(int tableID)
{
  char *tablename = NULL;

  if ( CDI_Debug )
598
    Message("tableID = %d", tableID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
599
600
601
602
603
604
605
606
607
608
609
610
611

  if ( ! ParTableInit ) parTableInit();

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

  return (tablename);
}

void tableWrite(const char *ptfile, int tableID)
{
  int item, npars;
612
  size_t maxname = 4, maxlname = 10, maxunits = 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
613
614
615
  FILE *ptfp;
  int tablenum, modelID, instID = CDI_UNDEFID;
  int center = 0, subcenter = 0;
616
  const char *instnameptr = NULL, *modelnameptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
617
618

  if ( CDI_Debug )
619
    Message("write parameter table %d to %s", tableID, ptfile);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
620
621
622

  if ( tableID == UNDEFID )
    {
623
      Warning("parameter table ID undefined");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
624
625
626
627
628
629
630
631
632
633
634
      return;
    }

  partabCheckID(tableID);

  ptfp = fopen(ptfile, "w");

  npars = parTable[tableID].npars;

  for ( item = 0; item < npars; item++)
    {
635
636
      if ( parTable[tableID].pars[item].name )
	{
637
	  size_t lenname = strlen(parTable[tableID].pars[item].name);
638
639
640
641
642
	  if ( lenname  > maxname )  maxname  = lenname;
	}

      if ( parTable[tableID].pars[item].longname )
	{
643
	  size_t lenlname = strlen(parTable[tableID].pars[item].longname);
644
645
646
647
648
	  if ( lenlname > maxlname ) maxlname = lenlname;
	}

      if ( parTable[tableID].pars[item].units )
	{
649
	  size_t lenunits = strlen(parTable[tableID].pars[item].units);
650
651
	  if ( lenunits > maxunits ) maxunits = lenunits;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
685
686
687
688
689
690
    }

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

  fclose(ptfp);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
713

Uwe Schulzweida's avatar
Uwe Schulzweida committed
714
void tableWriteC(const char *filename, int tableID)
715
716
717
718
719
720
721
722
723
724
725
{
  FILE *ptfp = fopen(filename, "w");
  if (!ptfp)
    Error("failed to open file \"%s\"!", filename);
  if ( CDI_Debug )
    Message("write parameter table %d to %s", tableID, filename);
  tableFWriteC(ptfp, tableID);
  fclose(ptfp);
}

void tableFWriteC(FILE *ptfp, int tableID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
726
{
727
  const char chelp[] = "";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
728
  int item, npars;
729
  size_t maxname = 0, maxlname = 0, maxunits = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
730
731
732
733
734
  char tablename[256];


  if ( tableID == UNDEFID )
    {
735
      Warning("parameter table ID undefined");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
736
737
738
739
740
741
742
743
744
      return;
    }

  partabCheckID(tableID);

  npars = parTable[tableID].npars;

  for ( item = 0; item < npars; item++)
    {
745
746
      if ( parTable[tableID].pars[item].name )
	{
747
	  size_t lenname = strlen(parTable[tableID].pars[item].name);
748
749
750
	  if ( lenname  > maxname )  maxname  = lenname;
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
751
      if ( parTable[tableID].pars[item].longname )
752
	{
753
	  size_t lenlname = strlen(parTable[tableID].pars[item].longname);
754
755
756
	  if ( lenlname > maxlname ) maxlname = lenlname;
	}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
757
      if ( parTable[tableID].pars[item].units )
758
	{
759
	  size_t lenunits = strlen(parTable[tableID].pars[item].units);
760
761
	  if ( lenunits > maxunits ) maxunits = lenunits;
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
762
763
    }

Thomas Jahns's avatar
Thomas Jahns committed
764
765
  strncpy(tablename, parTable[tableID].name, sizeof (tablename));
  tablename[sizeof (tablename) - 1] = '\0';
766
767
768
769
770
  {
    size_t len = strlen(tablename);
    for (size_t i = 0; i < len; i++ )
      if ( tablename[i] == '.' ) tablename[i] = '_';
  }
771
  fprintf(ptfp, "static const PAR %s[] = {\n", tablename);
772

Uwe Schulzweida's avatar
Uwe Schulzweida committed
773
774
  for ( item = 0; item < npars; item++ )
    {
775
776
777
778
779
780
      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;
      fprintf(ptfp, "  {%4d, 0, \"%s\", %-*s%c%s%s, %-*s%c%s%s %-*s},\n",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
781
	      parTable[tableID].pars[item].id,
782
783
784
785
786
787
788
789
790
	      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
791
792
793
794
795
    }

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
796
797
798

int tableInqParCode(int tableID, char *varname, int *code)
{
799
  int err = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
800

801
  if ( tableID != UNDEFID && varname != NULL )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
802
    {
803
804
      int npars = parTable[tableID].npars;
      for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
805
	{
806
807
808
809
810
811
812
	  if ( parTable[tableID].pars[item].name
               && strcmp(parTable[tableID].pars[item].name, varname) == 0 )
            {
              *code = parTable[tableID].pars[item].id;
              err = 0;
              break;
            }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
813
814
815
816
817
818
819
	}
    }

  return (err);
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
820
821
int tableInqParName(int tableID, int code, char *varname)
{
822
  int err = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
823

824
  if ( tableID != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
825
    {
826
827
      int npars = parTable[tableID].npars;
      for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
828
829
830
831
	{
	  if ( parTable[tableID].pars[item].id == code )
	    {
	      if ( parTable[tableID].pars[item].name )
832
		strcpy(varname, parTable[tableID].pars[item].name);     //FIXME: This may overrun the supplied buffer!
833
              err = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
834
835
836
837
838
839
840
841
	      break;
	    }
	}
    }

  return (err);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
842

843
const char *tableInqParNamePtr(int tableID, int code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
844
{
845
  const char *name = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
846
847
848

  if ( tableID != UNDEFID )
    {
849
850
      int npars = parTable[tableID].npars;
      for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
851
852
853
854
855
856
857
858
859
860
861
862
	{
	  if ( parTable[tableID].pars[item].id == code )
	    {
	      name = parTable[tableID].pars[item].name;
	      break;
	    }
	}
    }

  return (name);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
863

864
const char *tableInqParLongnamePtr(int tableID, int code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
865
{
866
  const char *longname = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
867
868
869

  if ( tableID != UNDEFID )
    {
870
871
      int npars = parTable[tableID].npars;
      for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
872
873
874
875
876
877
878
879
880
881
882
883
	{
	  if ( parTable[tableID].pars[item].id == code )
	    {
	      longname = parTable[tableID].pars[item].longname;
	      break;
	    }
	}
    }

  return (longname);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
884

885
const char *tableInqParUnitsPtr(int tableID, int code)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
886
{
887
  const char *units = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
888
889
890

  if ( tableID != UNDEFID )
    {
891
892
      int npars = parTable[tableID].npars;
      for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
893
894
895
896
897
898
899
900
901
902
903
904
	{
	  if ( parTable[tableID].pars[item].id == code )
	    {
	      units = parTable[tableID].pars[item].units;
	      break;
	    }
	}
    }

  return (units);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
905

Uwe Schulzweida's avatar
Uwe Schulzweida committed
906
907
int tableInqParLongname(int tableID, int code, char *longname)
{
908
  if ( ((tableID >= 0) & (tableID < MAX_TABLE)) | (tableID == UNDEFID) ) { } else
909
910
    Error("Invalid table ID %d", tableID);

911
912
913
  int err = 1;

  if ( tableID != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
914
    {
915
916
      int npars = parTable[tableID].npars;
      for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
917
918
919
920
921
	{
	  if ( parTable[tableID].pars[item].id == code )
	    {
	      if ( parTable[tableID].pars[item].longname )
		strcpy(longname, parTable[tableID].pars[item].longname);
922
              err = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
923
924
925
926
927
928
929
930
	      break;
	    }
	}
    }

  return (err);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
931

Uwe Schulzweida's avatar
Uwe Schulzweida committed
932
933
934
int tableInqParUnits(int tableID, int code, char *units)
{

935
  if ( ((tableID >= 0) & (tableID < MAX_TABLE)) | (tableID == UNDEFID) ) { } else
936
937
    Error("Invalid table ID %d", tableID);

938
939
940
  int err = 1;

  if ( tableID != UNDEFID )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
941
    {
942
943
      int npars = parTable[tableID].npars;
      for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
944
945
946
947
948
	{
	  if ( parTable[tableID].pars[item].id == code )
	    {
	      if ( parTable[tableID].pars[item].units )
		strcpy(units, parTable[tableID].pars[item].units);
949
              err = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
950
951
952
953
954
955
956
957
	      break;
	    }
	}
    }

  return (err);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
958

Uwe Schulzweida's avatar
Uwe Schulzweida committed
959
960
961
void tableInqPar(int tableID, int code, char *name, char *longname, char *units)
{

962
963
  if ( ((tableID >= 0) & (tableID < MAX_TABLE)) | (tableID == UNDEFID) ) { } else
    Error("Invalid table ID %d", tableID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
964

965
966
967
  int npars = parTable[tableID].npars;

  for ( int item = 0; item < npars; item++ )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
    {
      if ( parTable[tableID].pars[item].id == code )
	{
	  if ( parTable[tableID].pars[item].name )
	    strcpy(name, parTable[tableID].pars[item].name);
	  if ( parTable[tableID].pars[item].longname )
	    strcpy(longname, parTable[tableID].pars[item].longname);
	  if ( parTable[tableID].pars[item].units )
	    strcpy(units, parTable[tableID].pars[item].units);
	  break;
	}
    }
}

int tableInqNumber(void)
{
  if ( ! ParTableInit ) parTableInit();

  return (parTableNum);
}
988
989
990
991
992
993
994
995
996
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */