zaxis.c 22.2 KB
Newer Older
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <string.h>
#include <math.h>
#include <float.h>

#include "dmemory.h"

#include "cdi.h"
#include "stream_int.h"


static struct {
  char *name;
  char *longname;
  char *units;
}
LevelTypeEntry[] = {
  {"sfc",     "surface",                  ""},
  {"lev",     "generic",                  "level"},
  {"lev",     "hybrid",                   "level"},
  {"lev",     "hybrid_half",              "level"},
  {"lev",     "pressure",                 "Pa"},
  {"height",  "height",                   "m"},
  {"depth",   "depth below sea",          "m"},
  {"depth",   "depth below land",         "cm"},
  {"lev",     "isentropic",               "K"},
  {"lev",     "trajectory",               ""},
  {"alt",     "altitude",                 "m"},
};

static int CDI_MaxLeveltype = sizeof(LevelTypeEntry) / sizeof(LevelTypeEntry[0]);


#define  LevelDown  0
#define  LevelUp    1

typedef struct {
  char     name[256];
  char     longname[256];
  char     units[256];
  double  *vals;
  double  *lbounds;
  double  *ubounds;
  double  *weights;
  int      prec;
  int      type;
  int      size;
  int      direction;
  int      used;
  int      vctsize;
  double  *vct;
}
ZAXIS;

static ZAXIS *zaxisTable;
static int    zaxisTableSize = 0;


static void zaxisTableInitEntry (int zaxisID)
{
  zaxisTable[zaxisID].name[0]     = 0;
  zaxisTable[zaxisID].longname[0] = 0;
  zaxisTable[zaxisID].units[0]    = 0;
  zaxisTable[zaxisID].vals        = NULL;
  zaxisTable[zaxisID].ubounds     = NULL;
  zaxisTable[zaxisID].lbounds     = NULL;
  zaxisTable[zaxisID].weights     = NULL;
  zaxisTable[zaxisID].type        = CDI_UNDEFID;
  zaxisTable[zaxisID].direction   = CDI_UNDEFID;
  zaxisTable[zaxisID].prec        = 0;
  zaxisTable[zaxisID].size        = 0;
  zaxisTable[zaxisID].vctsize     = 0;
  zaxisTable[zaxisID].vct         = NULL;
}


static int zaxisTableNewEntry (int zaxistype)
{
  static char func[] = "zaxisTableNewEntry";
  int zaxisID = 0;

  /*
    Look for a free slot in zaxisTable.
    (Create the table the first time through).
  */
  if ( ! zaxisTableSize )
    {
      int i;

      zaxisTableSize = 2;
      zaxisTable = (ZAXIS *) malloc(zaxisTableSize*sizeof(ZAXIS));
      if ( zaxisTable == NULL )
	{
          Message(func, "zaxisTableSize = %d", zaxisTableSize);
	  SysError(func, "Allocation of ZAXISTABLE failed");
	}

      for ( i = 0; i < zaxisTableSize; i++ )
	zaxisTableInitEntry(i);
    }
  else
    {
      while ( zaxisID < zaxisTableSize )
	{
	  if ( zaxisTable[zaxisID].type == CDI_UNDEFID ) break;
	  zaxisID++;
	}
    }
  /*
    If the table overflows, double its size.
  */
  if ( zaxisID == zaxisTableSize )
    {
      int i;

      zaxisTableSize = 2*zaxisTableSize;
      zaxisTable = (ZAXIS *) realloc(zaxisTable, zaxisTableSize*sizeof(ZAXIS));
      if ( zaxisTable == NULL )
	{
          Message(func, "zaxisTableSize = %d", zaxisTableSize);
	  SysError(func, "Reallocation of ZAXISTABLE failed");
	}

      zaxisID = zaxisTableSize/2;

      for ( i = zaxisID; i < zaxisTableSize; i++ )
	zaxisTableInitEntry(i);
    }

  zaxisTable[zaxisID].type = zaxistype;

  return (zaxisID);
}


void zaxisCheckID(const char *func, int zaxisID)
{
  if ( zaxisID < 0 || zaxisID >= zaxisTableSize )
      Error(func, "zaxisID %d undefined!", zaxisID);

  if ( zaxisTable[zaxisID].type == CDI_UNDEFID )
    Error(func, "zaxisID %d type undefined!", zaxisID);
}


/*
150
@Function  zaxisCreate
151
@Title     Create a vertical Z-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
152

153
@Prototype int zaxisCreate(int zaxistype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
154
155
156
157
158
159
160
161
@Parameter
    @Item  zaxistype  The type of the Z-axis, one of the set of predefined CDI Z-axis types.
                      The valid CDI Z-axis types are {\tt ZAXIS_SURFACE}, {\tt ZAXIS_HYBRID},
                      {\tt ZAXIS_HYBRID_HALF}, {\tt ZAXIS_PRESSURE}, {\tt ZAXIS_HEIGHT},
                      {\tt ZAXIS_DEPTH_BELOW_SEA} and {\tt ZAXIS_DEPTH_BELOW_LAND}.
    @Item  size       Number of levels

@Description
162
The function {\tt zaxisCreate} creates a vertical Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164

@Result
Uwe Schulzweida's avatar
Uwe Schulzweida committed
165
{\tt zaxisCreate} returns an identifier to the Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
166

Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
@Example
Here is an example using {\tt zaxisCreate} to create a pressure level Z-axis:

@Source
#include "cdi.h"
   ...
#define  NLEV    5
   ...
double levs[NLEV] = {101300, 92500, 85000, 50000, 20000};
int zaxisID;
   ...
zaxisID = zaxisCreate(ZAXIS_PRESSURE, NLEV);
zaxisDefLevels(zaxisID, levs);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
182
183
@EndFunction
*/
184
int zaxisCreate(int zaxistype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
185
{
186
  static char func[] = "zaxisCreate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  int ilev;
  int zaxisID;
  double *vals;

  if ( CDI_Debug )
    Message(func, "zaxistype: %d size: %d ", zaxistype, size);

  /* get next free level entry */
  zaxisID = zaxisTableNewEntry(zaxistype);

  zaxisTable[zaxisID].size = size;

  if ( zaxistype > CDI_MaxLeveltype )
    Error(func, "Internal problem! zaxistype > CDI_MaxLeveltype");

  zaxisDefName(zaxisID, LevelTypeEntry[zaxistype].name);
  zaxisDefLongname(zaxisID, LevelTypeEntry[zaxistype].longname);
  zaxisDefUnits(zaxisID, LevelTypeEntry[zaxistype].units);

  vals = (double *) malloc(size*sizeof(double));

  for ( ilev = 0; ilev < size; ilev++ )
    vals[ilev] = 0.0;

  zaxisTable[zaxisID].vals = vals;

  return (zaxisID);
}


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
@Function  zaxisDestroy
@Title     Destroy a vertical Z-axis

@Prototype void zaxisDestroy(int zaxisID)
@Parameter
    @Item  zaxisID Identifier, from a previous call to {\tt zaxisCreate}

@EndFunction
*/
void zaxisDestroy(int zaxisID)
{
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
char *zaxisNamePtr(int leveltype)
{
  char *name;

  if ( leveltype >= 0 && leveltype < CDI_MaxLeveltype )
    name = LevelTypeEntry[leveltype].longname;
  else
    name = LevelTypeEntry[ZAXIS_GENERIC].longname;

  return (name);
}


void zaxisName(int zaxistype, char *zaxisname)
{
  strcpy(zaxisname, zaxisNamePtr(zaxistype));
}


/*
@Function  zaxisDefName
@Title     Define the name of a Z-axis

@Prototype void zaxisDefName(int zaxisID, const char *name)
@Parameter
    @Item  zaxisID   Z-axis identifier
    @Item  name      Name of the Z-axis

@Description
The function {\tt zaxisDefName} defines the name of a Z-axis.

@EndFunction
*/
void zaxisDefName(int zaxisID, const char *name)
{
  if ( name )
    strcpy(zaxisTable[zaxisID].name, name);
}


/*
@Function  zaxisDefLongname
@Title     Define the longname of a Z-axis

@Prototype void zaxisDefLongname(int zaxisID, const char *longname)
@Parameter
    @Item  zaxisID   Z-axis identifier
    @Item  longname  Longname of the Z-axis

@Description
The function {\tt zaxisDefLongname} defines the longname of a Z-axis.

@EndFunction
*/
void zaxisDefLongname(int zaxisID, const char *longname)
{
  if ( longname )
    strcpy(zaxisTable[zaxisID].longname, longname);
}


/*
@Function  zaxisDefUnits
@Title     Define the units of a Z-axis

@Prototype void zaxisDefUnits(int zaxisID, const char *units)
@Parameter
    @Item  zaxisID   Z-axis identifier
    @Item  units     Units of the Z-axis

@Description
The function {\tt zaxisDefUnits} defines the units of a Z-axis.

@EndFunction
*/
void zaxisDefUnits(int zaxisID, const char *units)
{
  if ( units )
    strcpy(zaxisTable[zaxisID].units, units);
}


/*
@Function  zaxisInqName
@Title     Get the name of a Z-axis

@Prototype void zaxisInqName(int zaxisID, char *name)
@Parameter
    @Item  zaxisID     Z-axis identifier
    @Item  name        Name of the Z-axis

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
324
The function {\tt zaxisInqName} returns the name of a Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
325
326

@Result
Uwe Schulzweida's avatar
Uwe Schulzweida committed
327
{\tt zaxisInqName} returns the name of the Z-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

@EndFunction
*/
void zaxisInqName(int zaxisID, char *name)
{
  strcpy(name, zaxisTable[zaxisID].name);
}


/*
@Function  zaxisInqLongname
@Title     Get the longname of a Z-axis

@Prototype void zaxisInqLongname(int zaxisID, char *longname)
@Parameter
    @Item  zaxisID     Z-axis identifier
    @Item  longname    Longname of the Z-axis

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
The function {\tt zaxisInqLongname} returns the longname of a Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
348
349

@Result
Uwe Schulzweida's avatar
Uwe Schulzweida committed
350
{\tt zaxisInqLongname} returns the longname of the Z-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369

@EndFunction
*/
void zaxisInqLongname(int zaxisID, char *longname)
{
  strcpy(longname, zaxisTable[zaxisID].longname);
}


/*
@Function  zaxisInqUnits
@Title     Get the units of a Z-axis

@Prototype void zaxisInqUnits(int zaxisID, char *units)
@Parameter
    @Item  zaxisID     Z-axis identifier
    @Item  units       Units of the Z-axis

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
370
The function {\tt zaxisInqUnits} returns the units of a Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
371
372

@Result
Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
{\tt zaxisInqUnits} returns the units of the Z-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470

@EndFunction
*/
void zaxisInqUnits(int zaxisID, char *units)
{
  strcpy(units, zaxisTable[zaxisID].units);
}


void zaxisDefPrec(int zaxisID, int prec)
{
  static char func[] = "zaxisDefPrec";

  zaxisCheckID(func, zaxisID);

  zaxisTable[zaxisID].prec = prec;
}


int zaxisInqPrec(int zaxisID)
{
  static char func[] = "zaxisInqPrec";

  zaxisCheckID(func, zaxisID);

  return (zaxisTable[zaxisID].prec);
}


/*
@Function  zaxisDefLevels
@Title     Define the levels of a Z-axis

@Prototype void zaxisDefLevels(int zaxisID, double *levels)
@Parameter
    @Item  zaxisID   Z-axis identifier
    @Item  levels    All levels of the Z-axis

@Description
The function {\tt zaxisDefLevels} defines the levels of a Z-axis.

@EndFunction
*/
void zaxisDefLevels(int zaxisID, double *levels)
{
  static char func[] = "zaxisDefLevels";
  int ilev;
  int size;
  double *vals;

  zaxisCheckID(func, zaxisID);

  size = zaxisTable[zaxisID].size;

  vals = zaxisTable[zaxisID].vals;

  for ( ilev = 0; ilev < size; ilev++ )
    vals[ilev] = levels[ilev];
}


/*
@Function  zaxisDefLevel
@Title     Define one level of a Z-axis

@Prototype void zaxisDefLevel(int zaxisID, int levelID, double level)
@Parameter
    @Item  zaxisID   Z-axis identifier
    @Item  levelID   Level identifier
    @Item  level     Level

@Description
The function {\tt zaxisDefLevel} defines one level of a Z-axis.

@EndFunction
*/
void zaxisDefLevel(int zaxisID, int levelID, double level)
{
  static char func[] = "zaxisDefLevel";

  zaxisCheckID(func, zaxisID);

  if ( levelID >= 0 && levelID < zaxisTable[zaxisID].size )
    zaxisTable[zaxisID].vals[levelID] = level;
}


/*
@Function  zaxisInqLevel
@Title     Get one level of a Z-axis

@Prototype double zaxisInqLevel(int zaxisID, int levelID)
@Parameter
    @Item  zaxisID   Z-axis identifier
    @Item  levelID   Level index (range: 0 to nlevel-1)

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
471
The function {\tt zaxisInqLevel} returns one level of a Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
472
473

@Result
Uwe Schulzweida's avatar
Uwe Schulzweida committed
474
{\tt zaxisInqLevel} returns the level of a Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
@EndFunction
*/
double zaxisInqLevel(int zaxisID, int levelID)
{
  static char func[] = "zaxisInqLevel";
  double level = 0;

  zaxisCheckID(func, zaxisID);

  if ( levelID >= 0 && levelID < zaxisTable[zaxisID].size )
    level = zaxisTable[zaxisID].vals[levelID];

  return (level);
}


double zaxisInqLbound(int zaxisID, int index)
{
  static char func[] = "zaxisInqLbound";
  double level = 0;

  zaxisCheckID(func, zaxisID);

  if ( index >= 0 && index < zaxisTable[zaxisID].size )
    level = zaxisTable[zaxisID].lbounds[index];

  return (level);
}


double zaxisInqUbound(int zaxisID, int index)
{
  static char func[] = "zaxisInqUbound";
  double level = 0;

  zaxisCheckID(func, zaxisID);

  if ( index >= 0 && index < zaxisTable[zaxisID].size )
    level = zaxisTable[zaxisID].ubounds[index];

  return (level);
}


const double *zaxisInqLevelsPtr(int zaxisID)
{
  return ( zaxisTable[zaxisID].vals );
}


/*
@Function  zaxisInqLevels
@Title     Get all levels of a Z-axis

@Prototype void zaxisInqLevels(int zaxisID, double *levels)
@Parameter
    @Item  zaxisID   Z-axis identifier
    @Item  levels    Levels of the Z-axis
    
@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
535
The function {\tt zaxisInqLevels} returns all levels of a Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645

@Result
{\tt zaxisInqLevels} saves all levels to the parameter {\tt levels}.
@EndFunction
*/
void zaxisInqLevels(int zaxisID, double *levels)
{
  static char func[] = "zaxisInqLevels";
  int size;
  int i;

  zaxisCheckID(func, zaxisID);

  size = zaxisTable[zaxisID].size;
  for ( i = 0; i < size; i++ )
    levels[i] =  zaxisTable[zaxisID].vals[i];
}


int zaxisInqLbounds(int zaxisID, double *lbounds)
{
  static char func[] = "zaxisInqLbounds";
  int size = 0;
  int i;

  zaxisCheckID(func, zaxisID);

  if ( zaxisTable[zaxisID].lbounds )
    {
      size = zaxisTable[zaxisID].size;

      if ( lbounds )
	for ( i = 0; i < size; i++ )
	  lbounds[i] =  zaxisTable[zaxisID].lbounds[i];
    }

  return (size);
}


int zaxisInqUbounds(int zaxisID, double *ubounds)
{
  static char func[] = "zaxisInqUbounds";
  int size = 0;
  int i;

  zaxisCheckID(func, zaxisID);

  if ( zaxisTable[zaxisID].ubounds )
    {
      size = zaxisTable[zaxisID].size;

      if ( ubounds )
	for ( i = 0; i < size; i++ )
	  ubounds[i] =  zaxisTable[zaxisID].ubounds[i];
    }

  return (size);
}


int zaxisInqWeights(int zaxisID, double *weights)
{
  static char func[] = "zaxisInqWeights";
  int size = 0;
  int i;

  zaxisCheckID(func, zaxisID);

  if ( zaxisTable[zaxisID].weights )
    {
      size = zaxisTable[zaxisID].size;

      if ( weights )
	for ( i = 0; i < size; i++ )
	  weights[i] =  zaxisTable[zaxisID].weights[i];
    }

  return (size);
}


int zaxisInqLevelID(int zaxisID, double level)
{
  static char func[] = "zaxisInqLevelID";
  int size;
  int levelID = CDI_UNDEFID;
  int i;

  zaxisCheckID(func, zaxisID);

  size = zaxisTable[zaxisID].size;
  for ( i = 0; i < size; i++ )
    if ( fabs(level-zaxisTable[zaxisID].vals[i]) < DBL_EPSILON ) break;

  if ( i < size ) levelID = i;

  return (levelID);
}


/*
@Function  zaxisInqType
@Title     Get the type of a Z-axis

@Prototype int zaxisInqType(int zaxisID)
@Parameter
    @Item  zaxisID   Z-axis identifier

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
646
The function {\tt zaxisInqType} returns the type of a Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
647
648

@Result
Uwe Schulzweida's avatar
Uwe Schulzweida committed
649
{\tt zaxisInqType} returns the type of the Z-axis,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
650
651
one of the set of predefined CDI Z-axis types.
The valid CDI Z-axis types are {\tt ZAXIS_SURFACE}, {\tt ZAXIS_HYBRID},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
652
{\tt ZAXIS_HYBRID_HALF}, {\tt ZAXIS_PRESSURE}, {\tt ZAXIS_HEIGHT},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
{\tt ZAXIS_DEPTH_BELOW_SEA} and {\tt ZAXIS_DEPTH_BELOW_LAND}.

@EndFunction
*/
int zaxisInqType(int zaxisID)
{
  static char func[] = "zaxisInqType";

  zaxisCheckID(func, zaxisID);

  return (zaxisTable[zaxisID].type);
}


/*
@Function  zaxisInqSize
@Title     Get the size of a Z-axis

@Prototype int zaxisInqSize(int zaxisID)
@Parameter
    @Item  zaxisID  Z-axis identifier

@Description
Uwe Schulzweida's avatar
Uwe Schulzweida committed
676
The function {\tt zaxisInqSize} returns the size of a Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
677
678

@Result
Uwe Schulzweida's avatar
Uwe Schulzweida committed
679
{\tt zaxisInqSize} returns the number of levels of a Z-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881

@EndFunction
*/
int zaxisInqSize(int zaxisID)
{
  static char func[] = "zaxisInqSize";
  int size = 1;

  zaxisCheckID(func, zaxisID);

  size = zaxisTable[zaxisID].size;

  return (size);
}


void cdiCheckZaxis(int zaxisID)
{
  static char func[] = "cdiCheckZaxis";
  int size, i, found;
  
  zaxisCheckID(func, zaxisID);

  if ( zaxisInqType(zaxisID) == ZAXIS_GENERIC )
    {
      size = zaxisTable[zaxisID].size;
      if ( size > 1 )
	{
	  /* check direction */
	  if ( zaxisTable[zaxisID].direction == CDI_UNDEFID )
	    {
	      found = 0;
	      for ( i = 1; i < size; i++ )
		if ( zaxisTable[zaxisID].vals[i] > zaxisTable[zaxisID].vals[i-1] )
		  found++;
	      if ( found == size-1 )
		{
		  zaxisTable[zaxisID].direction = LevelUp;
		}
	      else
		{
		  found = 0;
		  for ( i = 1; i < size; i++ )
		    if ( zaxisTable[zaxisID].vals[i] < zaxisTable[zaxisID].vals[i-1] )
		      found++;
		  if ( found == size-1 )
		    {
		      zaxisTable[zaxisID].direction = LevelDown;
		    }
		}
	    }
	  /* check consistent */
	  if ( zaxisTable[zaxisID].direction == CDI_UNDEFID )
	    {
	      Warning(func, "direction undefined for zaxisID %d", zaxisID);
	    }
	}
    }
}


int zaxisSize(void)
{
  int zaxisID = 0;

  while ( zaxisID < zaxisTableSize )
    {
      if ( zaxisTable[zaxisID].type == CDI_UNDEFID ) break;
      zaxisID++;
    }

  return (zaxisID);
}


void zaxisDefVct(int zaxisID, int size, const double *vct)
{
  static char func[] = "zaxisDefVct";

  zaxisCheckID(func, zaxisID);

  if ( zaxisTable[zaxisID].vct == 0 )
    {
      zaxisTable[zaxisID].vctsize = size;
      zaxisTable[zaxisID].vct = (double *) malloc(size*sizeof(double));
      memcpy(zaxisTable[zaxisID].vct, vct, size*sizeof(double));
    }
  else
    if ( zaxisTable[zaxisID].vctsize != size )
      Warning(func, "VCT was allready defined");
}


int zaxisInqVctSize(int zaxisID)
{
  static char func[] = "zaxisInqVctSize";

  zaxisCheckID(func, zaxisID);

  return (zaxisTable[zaxisID].vctsize);
}


const double *zaxisInqVctPtr(int zaxisID)
{
  static char func[] = "zaxisInqVctPtr";

  zaxisCheckID(func, zaxisID);

  return (zaxisTable[zaxisID].vct);
}


void zaxisDefLbounds(int zaxisID, double *lbounds)
{
  static char func[] = "zaxisDefLbounds";
  size_t size;

  zaxisCheckID(func, zaxisID);

  size = zaxisTable[zaxisID].size;

  if ( zaxisTable[zaxisID].lbounds != NULL )
    Warning(func, "Lbounds allready defined for zaxisID = %d", zaxisID);

  zaxisTable[zaxisID].lbounds = (double *) malloc(size*sizeof(double));
  memcpy(zaxisTable[zaxisID].lbounds, lbounds, size*sizeof(double));
}


void zaxisDefUbounds(int zaxisID, double *ubounds)
{
  static char func[] = "zaxisDefUbounds";
  size_t size;

  zaxisCheckID(func, zaxisID);

  size = zaxisTable[zaxisID].size;

  if ( zaxisTable[zaxisID].ubounds != NULL )
    Warning(func, "Ubounds allready defined for zaxisID = %d", zaxisID);

  zaxisTable[zaxisID].ubounds = (double *) malloc(size*sizeof(double));
  memcpy(zaxisTable[zaxisID].ubounds, ubounds, size*sizeof(double));
}


void zaxisDefWeights(int zaxisID, double *weights)
{
  static char func[] = "zaxisDefWeights";
  size_t size;

  zaxisCheckID(func, zaxisID);

  size = zaxisTable[zaxisID].size;

  if ( zaxisTable[zaxisID].weights != NULL )
    Warning(func, "Weights allready defined for zaxisID = %d", zaxisID);

  zaxisTable[zaxisID].weights = (double *) malloc(size*sizeof(double));
  memcpy(zaxisTable[zaxisID].weights, weights, size*sizeof(double));
}


void zaxisChangeType(int zaxisID, int leveltype)
{
  static char func[] = "zaxisChangeType";

  zaxisCheckID(func, zaxisID);

  Message(func, "Change zaxis type from %s to %s\n",
	  zaxisNamePtr(zaxisTable[zaxisID].type),
	  zaxisNamePtr(leveltype));
  
  zaxisTable[zaxisID].type = leveltype;
}


void zaxisResize(int zaxisID, int size)
{
  static char func[] = "zaxisResize";

  zaxisCheckID(func, zaxisID);

  zaxisTable[zaxisID].size = size;

  if ( zaxisTable[zaxisID].vals )
    zaxisTable[zaxisID].vals =
      (double *) realloc(zaxisTable[zaxisID].vals, size*sizeof(double));
}


int zaxisDuplicate(int zaxisID)
{
  static char func[] = "zaxisDuplicate";
  int zaxisIDnew;
  int zaxistype, zaxissize;
  int size;

  zaxistype = zaxisInqType(zaxisID);
  zaxissize = zaxisInqSize(zaxisID);

882
  zaxisIDnew = zaxisCreate(zaxistype, zaxissize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

  memcpy(&zaxisTable[zaxisIDnew], &zaxisTable[zaxisID], sizeof(ZAXIS));

  strcpy(zaxisTable[zaxisIDnew].name, zaxisTable[zaxisID].name);
  strcpy(zaxisTable[zaxisIDnew].longname, zaxisTable[zaxisID].longname);
  strcpy(zaxisTable[zaxisIDnew].units, zaxisTable[zaxisID].units);

  if ( zaxisTable[zaxisID].vals != NULL )
    {
      size = zaxissize;

      zaxisTable[zaxisIDnew].vals = (double *) malloc(size*sizeof(double));
      memcpy(zaxisTable[zaxisIDnew].vals, zaxisTable[zaxisID].vals, size*sizeof(double));
    }

  if ( zaxisTable[zaxisIDnew].lbounds )
    {
      size = zaxissize;

      zaxisTable[zaxisIDnew].lbounds = (double *) malloc(size*sizeof(double));
      memcpy(zaxisTable[zaxisIDnew].lbounds, zaxisTable[zaxisID].lbounds, size*sizeof(double));
    }

  if ( zaxisTable[zaxisIDnew].ubounds )
    {
      size = zaxissize;

      zaxisTable[zaxisIDnew].ubounds = (double *) malloc(size*sizeof(double));
      memcpy(zaxisTable[zaxisIDnew].ubounds, zaxisTable[zaxisID].ubounds, size*sizeof(double));
    }

  if ( zaxisTable[zaxisID].vct != NULL )
    {
      size = zaxisTable[zaxisID].vctsize;

      if ( size )
	{
	  zaxisTable[zaxisID].vctsize = size;
	  zaxisTable[zaxisIDnew].vct = (double *) malloc(size*sizeof(double));
	  memcpy(zaxisTable[zaxisIDnew].vct, zaxisTable[zaxisID].vct, size*sizeof(double));
	}
    }

  return (zaxisIDnew);
}


void zaxisPrint(int zaxisID)
{
  FILE *fp = stdout;
  int type;
  int nlevels, levelID;
  int nbyte0, nbyte;
  double level;

  type    = zaxisInqType(zaxisID);
  nlevels = zaxisInqSize(zaxisID);

  nbyte0 = 0;
  fprintf(fp, "#\n");
  fprintf(fp, "# zaxisID %d\n", zaxisID);
  fprintf(fp, "#\n");
  fprintf(fp, "zaxistype : %s\n", zaxisNamePtr(type));
  fprintf(fp, "size      : %d\n", nlevels);
  if ( zaxisTable[zaxisID].name[0]     ) fprintf(fp, "name      : %s\n", zaxisTable[zaxisID].name);
  if ( zaxisTable[zaxisID].longname[0] ) fprintf(fp, "longname  : %s\n", zaxisTable[zaxisID].longname);
  if ( zaxisTable[zaxisID].units[0]    ) fprintf(fp, "units     : %s\n", zaxisTable[zaxisID].units);

  nbyte0 = fprintf(fp, "levels    : ");
  nbyte = nbyte0;
  for ( levelID = 0; levelID < nlevels; levelID++ )
    {
      if ( nbyte > 80 )
	{
	  fprintf(stdout, "\n");
	  fprintf(stdout, "%*s", nbyte0, "");
	  nbyte = nbyte0;
	}
      level = zaxisInqLevel(zaxisID, levelID);
      nbyte += fprintf(stdout, "%.9g ", level);
    }
  fprintf(stdout, "\n");

  if ( zaxisTable[zaxisID].lbounds && zaxisTable[zaxisID].ubounds )
    {
      double level1, level2;
      nbyte = nbyte0;
      nbyte0 = fprintf(stdout, "%32s : ", "bounds");
      for ( levelID = 0; levelID < nlevels; levelID++ )
	{
	  if ( nbyte > 80 )
	    {
	      fprintf(stdout, "\n");
	      fprintf(stdout, "%*s", nbyte0, "");
	      nbyte = nbyte0;
	    }
	  level1 = zaxisInqLbound(zaxisID, levelID);
	  level2 = zaxisInqUbound(zaxisID, levelID);
	  nbyte += fprintf(stdout, "%.9g-%.9g ", level1, level2);
	}
      fprintf(stdout, "\n");
    }

  if ( type == ZAXIS_HYBRID )
    {
      int i;
      int vctsize;
      const double *vct;

      vctsize = zaxisInqVctSize(zaxisID);
      vct     = zaxisInqVctPtr(zaxisID);
      fprintf(stdout, "vctsize   : %d\n", vctsize);
      if ( vctsize )
	{
	  nbyte0 = fprintf(stdout, "vct       : ");
	  nbyte = nbyte0;
	  for ( i = 0; i < vctsize; i++ )
	    {