grid.c 82.1 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
#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <string.h>

#include "dmemory.h"
#include "cdi.h"
#include "stream_int.h"
#include "grid.h"
#include "util.h"


#ifndef  RAD2DEG
#define  RAD2DEG  (180./M_PI)   /* conversion for rad to deg */
#endif

#ifndef  DEG2RAD
#define  DEG2RAD  (M_PI/180.)   /* conversion for deg to rad */
#endif


char *Grids[] = {
  "undefined",
  "generic",
  "gaussian",
  "gaussian reduced",
  "lonlat",
  "spectral",
  "fourier",
  "gme",
  "trajectory",
  "cell",
  "curvilinear",
35
  "lambert",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
36
37
38
39
40
};


static int  GRID_Debug = 0;   /* If set to 1, debugging */

41
static int _grid_max = MAX_GRIDS;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

static void grid_initialize(void);

static int _grid_init = FALSE;

#if  defined  (HAVE_LIBPTHREAD)
#  include <pthread.h>

static pthread_once_t _grid_init_thread = PTHREAD_ONCE_INIT;
static pthread_mutex_t _grid_mutex;

#  define GRID_LOCK           pthread_mutex_lock(&_grid_mutex);
#  define GRID_UNLOCK         pthread_mutex_unlock(&_grid_mutex);
#  define GRID_INIT                               \
   if ( _grid_init == FALSE ) pthread_once(&_grid_init_thread, grid_initialize);

#else

#  define GRID_LOCK
#  define GRID_UNLOCK
#  define GRID_INIT                               \
   if ( _grid_init == FALSE ) grid_initialize();

#endif


typedef struct _gridPtrToIdx {
  int idx;
  GRID *ptr;
  struct _gridPtrToIdx *next;
} gridPtrToIdx;


Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
76
static gridPtrToIdx *_gridList  = NULL;
static gridPtrToIdx *_gridAvail = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
78
79
80
81
82


static void grid_list_new(void)
{
  static char func[] = "grid_list_new";

Uwe Schulzweida's avatar
Uwe Schulzweida committed
83
84
  assert(_gridList == NULL);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
150
151
152
153
154
155
156
157
158
  _gridList = (gridPtrToIdx *) malloc(_grid_max*sizeof(gridPtrToIdx));
}


static void grid_list_delete(void)
{
  static char func[] = "grid_list_delete";

  if ( _gridList ) free(_gridList);
}


static void grid_init_pointer(void)
{
  int  i;
  
  for ( i = 0; i < _grid_max; i++ )
    {
      _gridList[i].next = _gridList + i + 1;
      _gridList[i].idx  = i;
      _gridList[i].ptr  = 0;
    }

  _gridList[_grid_max-1].next = 0;

  _gridAvail = _gridList;
}


GRID *grid_to_pointer(int idx)
{
  static char func[] = "grid_to_pointer";
  GRID *gridptr = NULL;

  GRID_INIT

  if ( idx >= 0 && idx < _grid_max )
    {
      GRID_LOCK

      gridptr = _gridList[idx].ptr;

      GRID_UNLOCK
    }
  else
    Error(func, "grid index %d undefined!", idx);

  return (gridptr);
}


/* Create an index from a pointer */
static int grid_from_pointer(GRID *ptr)
{
  static char func[] = "grid_from_pointer";
  int      idx = -1;
  gridPtrToIdx *newptr;

  if ( ptr )
    {
      GRID_LOCK

      if ( _gridAvail )
	{
	  newptr       = _gridAvail;
	  _gridAvail   = _gridAvail->next;
	  newptr->next = 0;
	  idx	       = newptr->idx;
	  newptr->ptr  = ptr;
      
	  if ( GRID_Debug )
	    Message(func, "Pointer %p has idx %d from grid list", ptr, idx);
	}
      else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
159
	Warning(func, "Too many open grids (limit is %d)!", _grid_max);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

      GRID_UNLOCK
    }
  else
    Error(func, "Internal problem (pointer %p undefined)", ptr);

  return (idx);
}


static void grid_init_entry(GRID *gridptr)
{
  gridptr->self         = grid_from_pointer(gridptr);

  gridptr->type         = CDI_UNDEFID;
  gridptr->mask         = NULL;
  gridptr->xvals        = NULL;
  gridptr->yvals        = NULL;
  gridptr->area         = NULL;
  gridptr->xbounds      = NULL;
  gridptr->ybounds      = NULL;
  gridptr->rowlon       = NULL;
  gridptr->nrowlon      = 0;
  gridptr->xinc         = 0.0;
  gridptr->yinc         = 0.0;
185
186
187
188
189
190
191
192
  gridptr->lam_originLon = 0.0;
  gridptr->lam_originLat = 0.0;
  gridptr->lam_lonParY  = 0.0;
  gridptr->lam_lat1     = 0.0;
  gridptr->lam_lat2     = 0.0;
  gridptr->lam_xinc     = 0.0;
  gridptr->lam_yinc     = 0.0;
  gridptr->lam_defined  = FALSE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
194
195
196
197
198
199
200
201
202
  gridptr->trunc        = 0;
  gridptr->nvertex      = 0;
  gridptr->nd           = 0;
  gridptr->ni           = 0;
  gridptr->ni2          = 0;
  gridptr->ni3          = 0;
  gridptr->prec         = 0;
  gridptr->size         = 0;
  gridptr->xsize        = 0;
  gridptr->ysize        = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
203
  gridptr->isCyclic     = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  gridptr->isRotated    = FALSE;
  gridptr->xpole        = 0.0;
  gridptr->ypole        = 0.0;
  gridptr->angle        = 0.0;
  gridptr->locked       = FALSE;
  gridptr->xname[0]     = 0;
  gridptr->yname[0]     = 0;
  gridptr->xlongname[0] = 0;
  gridptr->ylongname[0] = 0;
  gridptr->xunits[0]    = 0;
  gridptr->yunits[0]    = 0;
  gridptr->xstdname[0]  = 0;
  gridptr->ystdname[0]  = 0;
}


static GRID *grid_new_entry(void)
{
  static char func[] = "grid_new_entry";
  GRID *gridptr;

  gridptr = (GRID *) malloc(sizeof(GRID));

  if ( gridptr ) grid_init_entry(gridptr);

  return (gridptr);
}


static void grid_delete_entry(GRID *gridptr)
{
  static char func[] = "grid_delete_entry";
  int idx;

  idx = gridptr->self;

  GRID_LOCK

  free(gridptr);

  _gridList[idx].next = _gridAvail;
  _gridList[idx].ptr  = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
246
  _gridAvail          = &_gridList[idx];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

  GRID_UNLOCK

  if ( GRID_Debug )
    Message(func, "Removed idx %d from grid list", idx);
}


static void grid_initialize(void)
{
  char *env;

#if  defined  (HAVE_LIBPTHREAD)
  /* initialize global API mutex lock */
  pthread_mutex_init(&_grid_mutex, NULL);
#endif

  env = getenv("GRID_DEBUG");
  if ( env ) GRID_Debug = atoi(env);

  grid_list_new();
  atexit(grid_list_delete);

270
271
  GRID_LOCK

Uwe Schulzweida's avatar
Uwe Schulzweida committed
272
273
  grid_init_pointer();

274
275
  GRID_UNLOCK

Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
  _grid_init = TRUE;
}


static void grid_copy(GRID *gridptr2, GRID *gridptr1)
{
  int gridID2;

  gridID2 = gridptr2->self;
  memcpy(gridptr2, gridptr1, sizeof(GRID));
  gridptr2->self = gridID2;
}


static void grid_check_ptr(const char *func, GRID *gridptr)
{
  if ( gridptr == NULL )
    Error(func, "grid undefined!");
}


int gridSize(void)
{
  int gridsize = 0;
  int i;
  
  GRID_INIT

  GRID_LOCK

  for ( i = 0; i < _grid_max; i++ )
    if ( _gridList[i].ptr ) gridsize++;

  GRID_UNLOCK

  return (gridsize);
}


void gridGenXvals(int xsize, double xfirst, double xlast, double xinc, double *xvals)
{
  int i;

319
  if ( (! (fabs(xinc) > 0)) && xsize > 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
    {
      if ( xfirst >= xlast )
	{
	  while ( xfirst >= xlast ) xlast += 360;
	  xinc = (xlast-xfirst)/(xsize);
	}
      else
	{
	  xinc = (xlast-xfirst)/(xsize-1);
	}
    }

  for ( i = 0; i < xsize; i++ )
    xvals[i] = xfirst + i*xinc;
}


337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
void calc_gaussaw(double *yvals, int ysize, double yfirst, double ylast)
{
  static char func[] = "calc_gaussaw";
  double *yw;
  int yhsize;
  int i;

  yw = (double *) malloc(ysize*sizeof(double));
  gaussaw(yvals, yw, ysize);
  free(yw);
  for ( i = 0; i < ysize; i++ )
    yvals[i] = asin(yvals[i])/M_PI*180.0;

  if ( yfirst < ylast && yfirst > -90.0 && ylast < 90.0 )
    {
      double ytmp;
      yhsize = ysize/2;
      for ( i = 0; i < yhsize; i++ )
	{
	  ytmp = yvals[i];
	  yvals[i] = yvals[ysize-i-1];
	  yvals[ysize-i-1] = ytmp;
	}
    }
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
365
366
367
368
369
370
371
372
void gridGenYvals(int gridtype, int ysize, double yfirst, double ylast, double yinc, double *yvals)
{
  static char func[] = "gridGenYvals";
  int i;

  if ( gridtype == GRID_GAUSSIAN || gridtype == GRID_GAUSSIAN_REDUCED )
    {
      if ( ysize > 2 )
	{
373
	  calc_gaussaw(yvals, ysize, yfirst, ylast);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374

Uwe Schulzweida's avatar
Uwe Schulzweida committed
375
	  if ( ! (DBL_IS_EQUAL(yfirst, 0) && DBL_IS_EQUAL(ylast, 0)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
377
	    if ( fabs(yvals[0] - yfirst) > 0.001 || fabs(yvals[ysize-1] - ylast) > 0.001 )
	      {
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
		double yinc = fabs(ylast-yfirst)/(ysize-1);
		double *ytmp;
		int nstart, lfound = 0;
		int ny = (int) (180./yinc + 0.5);
		ny -= ny%2;
		/* printf("%g %g %g %g %g %d\n", ylast, yfirst, ylast-yfirst,yinc, 180/yinc, ny); */
		ytmp   = (double *) malloc(ny*sizeof(double));
		calc_gaussaw(ytmp, ny, yfirst, ylast);
		for ( i = 0; i < (ny-ysize); i++ )
		  if ( fabs(ytmp[i] - yfirst) < 0.001 ) break;

		nstart = i;

		if ( (nstart+ysize-1) < ny )
		  if ( fabs(ytmp[nstart+ysize-1] - ylast) < 0.001 ) lfound = 1;

		if ( lfound )
		  {
		    for ( i = 0; i < ysize; i++ ) yvals[i] = ytmp[i+nstart];
		  }
		else
		  {
400
		    Warning(func, "Cannot calculate gaussian latitudes for lat1 = %g latn = %g", yfirst, ylast);
401
402
403
404
405
406
		    for ( i = 0; i < ysize; i++ ) yvals[i] = 0;
		    yvals[0] = yfirst;
		    yvals[ysize-1] = ylast;
		  }

		free(ytmp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
407
408
409
410
411
412
413
414
415
416
417
	      }
	}
      else
	{
	  yvals[0] = yfirst;
	  yvals[ysize-1] = ylast;
	}
    }
  /*     else if ( gridtype == GRID_LONLAT || gridtype == GRID_GENERIC ) */
  else
    {
418
      if ( (! (fabs(yinc) > 0)) && ysize > 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
419
	{
Uwe Schulzweida's avatar
Uwe Schulzweida committed
420
	  if ( DBL_IS_EQUAL(yfirst, ylast) && !DBL_IS_EQUAL(yfirst, 0) ) ylast *= -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
471
472
473
474
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
535
536
537
538
539
540
541
542
543
544
545
546
547

	  if ( yfirst > ylast )
	    yinc = (yfirst-ylast)/(ysize-1);
	  else if ( yfirst < ylast )
	    yinc = (ylast-yfirst)/(ysize-1);
	  else
	    {
	      if ( ysize%2 != 0 )
		{
		  yinc = 180.0/(ysize-1);
		  yfirst = -90;
		}
	      else
		{
		  yinc = 180.0/ysize;
		  yfirst = -90 + yinc/2;
		}
	    }
	}

      if ( yfirst > ylast && yinc > 0 ) yinc = -yinc;

      for ( i = 0; i < ysize; i++ )
	yvals[i] = yfirst + i*yinc;
    }
  /*
    else
    Error(func, "unable to calculate values for %s grid!", gridNamePtr(gridtype));
  */
}
/*
static void defineXvals(int gridID)
{
  static char func[] = "defineXvals";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  if ( gridptr->type == GRID_CELL ) return;

  if ( gridptr->xvals == NULL )
    {
      double *xvals, xfirst, xlast, xinc;
      int i, xsize;

      xsize = gridptr->xsize;
      if ( xsize == 0 )
	Error(func, "xsize undefined for gridID = %d", gridID);

      xvals = (double *) malloc(xsize*sizeof(double));
      xfirst = gridptr->xfirst;
      xlast  = gridptr->xlast;
      xinc = gridptr->xinc;
      if ( ! fabs(xinc) )
	{
	  if ( xfirst > xlast )
	    xinc = (xfirst-xlast)/xsize;
	  else
	    xinc = (xlast-xfirst)/xsize;

	  gridptr->xinc = xinc;
	}
      if ( xfirst > xlast && xinc > 0 ) xinc = -xinc;
      for ( i = 0; i < xsize; i++ )
	xvals[i] = xfirst + i*xinc;

      gridptr->xvals = xvals;
    }
}
*/

static void defineYvals(int gridID)
{
  static char func[] = "defineYvals";
  int gridtype;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  gridtype = gridptr->type;

  if ( gridtype == GRID_CELL ) return;

  if ( gridptr->yvals == NULL )
    {
      double *yvals, yfirst, ylast, yinc;
      int i, ysize;

      ysize = gridptr->ysize;

      if ( gridtype == GRID_GAUSSIAN || gridtype == GRID_GAUSSIAN_REDUCED )
	{
	  double *yw;
	  int yhsize;

	  yvals = (double *) malloc(ysize*sizeof(double));
	  yw    = (double *) malloc(ysize*sizeof(double));
	  gaussaw(yvals, yw, ysize);
	  free(yw);
	  for ( i = 0; i < ysize; i++ )
	    yvals[i] = asin(yvals[i])/M_PI*180.0;

	  yfirst = gridptr->yfirst;
	  ylast  = gridptr->ylast;
	  if ( yfirst < ylast && yfirst > -90.0 && ylast < 90.0 )
	    {
	      double ytmp;
	      yhsize = ysize/2;
	      for ( i = 0; i < yhsize; i++ )
		{
		  ytmp = yvals[i];
		  yvals[i] = yvals[ysize-i-1];
		  yvals[ysize-i-1] = ytmp;
		}
	    }
	  gridptr->yvals = yvals;
	}
      /*     else if ( gridtype == GRID_LONLAT || gridtype == GRID_GENERIC ) */
      else
	{
	  if ( ysize == 0 )
	    Error(func, "ysize undefined for gridID = %d", gridID);

	  yvals = (double *) malloc(ysize*sizeof(double));
	  yfirst = gridptr->yfirst;
	  ylast  = gridptr->ylast;
	  yinc = gridptr->yinc;
548
	  if ( ! (fabs(yinc) > 0) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
	    {
	      if ( ysize > 1 )
		{
		  if ( yfirst > ylast )
		    yinc = (yfirst-ylast)/(ysize-1);
		  else
		    yinc = (ylast-yfirst)/(ysize-1);
		}

	      gridptr->yinc = yinc;
	    }
	  if ( yfirst > ylast && yinc > 0 ) yinc = -yinc;
	  for ( i = 0; i < ysize; i++ )
	    yvals[i] = yfirst + i*yinc;

	  gridptr->yvals = yvals;
	}
      /*
      else
	Error(func, "unable to calculate values for %s grid!", gridNamePtr(gridtype));
      */
    }
}


/*
575
@Function  gridCreate
576
@Title     Create a horizontal Grid
Uwe Schulzweida's avatar
Uwe Schulzweida committed
577

578
@Prototype int gridCreate(int gridtype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
579
580
@Parameter
    @Item  gridtype  The type of the grid, one of the set of predefined CDI grid types.
581
                     The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
582
583
                     @func{GRID_LONLAT}, @func{GRID_LAMBERT}, @func{GRID_SPECTRAL},
                     @func{GRID_GME}, @func{GRID_CURVILINEAR} and @func{GRID_CELL}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
584
585
586
    @Item  size      Number of gridpoints.

@Description
587
The function @func{gridCreate} creates a horizontal Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
588
589

@Result
590
@func{gridCreate} returns an identifier to the Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
591
592

@Example
593
Here is an example using @func{gridCreate} to create a regular lon/lat Grid:
Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
595

@Source
Uwe Schulzweida's avatar
Uwe Schulzweida committed
596
597
598
599
#include "cdi.h"
   ...
#define  NLON  12
#define  NLAT   6
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600
   ...
Uwe Schulzweida's avatar
Uwe Schulzweida committed
601
602
double lons[NLON] = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
double lats[NLAT] = {-75, -45, -15, 15, 45, 75};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
603
604
int gridID;
   ...
Uwe Schulzweida's avatar
Uwe Schulzweida committed
605
606
607
gridID = gridCreate(GRID_LONLAT, NLON*NLAT);
gridDefXsize(gridID, NLON);
gridDefYsize(gridID, NLAT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
608
609
610
611
gridDefXvals(gridID, lons);
gridDefYvals(gridID, lats);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
612
613
@EndFunction
*/
614
int gridCreate(int gridtype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
615
{
616
  static char func[] = "gridCreate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
646
647
648
649
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
685
686
687
688
689
  int gridID;
  GRID *gridptr;

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

  GRID_INIT

  gridptr = grid_new_entry();
  if ( ! gridptr ) Error(func, "No memory");

  gridID = gridptr->self;

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

  gridptr->type = gridtype;
  gridptr->size = size;

  /*  if ( gridtype == GRID_GENERIC )     gridptr->xsize = size; */
  if ( gridtype == GRID_CELL )        gridptr->xsize = size;
  if ( gridtype == GRID_CURVILINEAR ) gridptr->nvertex = 4;

  switch (gridtype)
    {
    case GRID_LONLAT:
    case GRID_GAUSSIAN:
    case GRID_GAUSSIAN_REDUCED:
    case GRID_CURVILINEAR:
    case GRID_TRAJECTORY:
      {
	if ( gridtype == GRID_TRAJECTORY )
	  {
	    gridDefXname(gridID, "tlon");
	    gridDefYname(gridID, "tlat");
	  }
	else
	  {
	    gridDefXname(gridID, "lon");
	    gridDefYname(gridID, "lat");
	  }
	gridDefXlongname(gridID, "longitude");
	gridDefYlongname(gridID, "latitude");
	if ( gridtype == GRID_CURVILINEAR )
	  {
	    strcpy(gridptr->xstdname, "grid_longitude");
	    strcpy(gridptr->ystdname, "grid_latitude");
	    gridDefXunits(gridID, "degrees");
	    gridDefYunits(gridID, "degrees");
	  }
	else
	  {
	    strcpy(gridptr->xstdname, "longitude");
	    strcpy(gridptr->ystdname, "latitude");
	    gridDefXunits(gridID, "degrees_east");
	    gridDefYunits(gridID, "degrees_north");
	  }

	break;
      }
    case GRID_GME:
    case GRID_CELL:
    case GRID_GENERIC:
      {
	gridDefXname(gridID, "x");
	gridDefYname(gridID, "y");
	break;
      }
    }

  return (gridID);
}


690
691
692
693
694
695
/*
@Function  gridDestroy
@Title     Destroy a horizontal Grid

@Prototype void gridDestroy(int gridID)
@Parameter
696
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
697
698
699
700
701
702
703
704

@EndFunction
*/
void gridDestroy(int gridID)
{
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
705
706
707
char *gridNamePtr(int gridtype)
{
  char *name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
708
  int size = (int) (sizeof(Grids)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730

  if ( gridtype >= 0 && gridtype < size )
    name = Grids[gridtype];
  else
    name = Grids[GRID_GENERIC];

  return (name);
}


void gridName(int gridtype, char *gridname)
{
  strcpy(gridname, gridNamePtr(gridtype));
}


/*
@Function  gridDefXname
@Title     Define the name of a X-axis

@Prototype void gridDefXname(int gridID, const char *name)
@Parameter
731
732
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  name     Name of the X-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
733
734

@Description
735
The function @func{gridDefXname} defines the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755

@EndFunction
*/
void gridDefXname(int gridID, char *xname)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  if ( xname )
    strcpy(gridptr->xname, xname);
}


/*
@Function  gridDefXlongname
@Title     Define the longname of a X-axis

@Prototype void gridDefXlongname(int gridID, const char *longname)
@Parameter
756
757
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  longname Longname of the X-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
758
759

@Description
760
The function @func{gridDefXlongname} defines the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780

@EndFunction
*/
void gridDefXlongname(int gridID, char *xlongname)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  if ( xlongname )
    strcpy(gridptr->xlongname, xlongname);
}


/*
@Function  gridDefXunits
@Title     Define the units of a X-axis

@Prototype void gridDefXunits(int gridID, const char *units)
@Parameter
781
782
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  units    Units of the X-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
783
784

@Description
785
The function @func{gridDefXunits} defines the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805

@EndFunction
*/
void gridDefXunits(int gridID, char *xunits)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  if ( xunits )
    strcpy(gridptr->xunits, xunits);
}


/*
@Function  gridDefYname
@Title     Define the name of a Y-axis

@Prototype void gridDefYname(int gridID, const char *name)
@Parameter
806
807
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  name     Name of the Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
808
809

@Description
810
The function @func{gridDefYname} defines the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830

@EndFunction
*/
void gridDefYname(int gridID, char *yname)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  if ( yname )
    strcpy(gridptr->yname, yname);
}


/*
@Function  gridDefYlongname
@Title     Define the longname of a Y-axis

@Prototype void gridDefYlongname(int gridID, const char *longname)
@Parameter
831
832
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  longname Longname of the Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
833
834

@Description
835
The function @func{gridDefYlongname} defines the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855

@EndFunction
*/
void gridDefYlongname(int gridID, char *ylongname)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  if ( ylongname )
    strcpy(gridptr->ylongname, ylongname);
}


/*
@Function  gridDefYunits
@Title     Define the units of a Y-axis

@Prototype void gridDefYunits(int gridID, const char *units)
@Parameter
856
857
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  units    Units of the Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
858
859

@Description
860
The function @func{gridDefYunits} defines the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880

@EndFunction
*/
void gridDefYunits(int gridID, char *yunits)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  if ( yunits )
    strcpy(gridptr->yunits, yunits);
}


/*
@Function  gridInqXname
@Title     Get the name of a X-axis

@Prototype void gridInqXname(int gridID, const char *name)
@Parameter
881
882
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  name     Name of the X-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
883
884

@Description
885
The function @func{gridInqXname} returns the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
886
887

@Result
888
@func{gridInqXname} returns the name of the X-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907

@EndFunction
*/
void gridInqXname(int gridID, char *xname)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  strcpy(xname, gridptr->xname);
}


/*
@Function  gridInqXlongname
@Title     Get the longname of a X-axis

@Prototype void gridInqXlongname(int gridID, const char *longname)
@Parameter
908
909
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  longname Longname of the X-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
910
911

@Description
912
The function @func{gridInqXlongname} returns the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
913
914

@Result
915
@func{gridInqXlongname} returns the longname of the X-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934

@EndFunction
*/
void gridInqXlongname(int gridID, char *xlongname)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  strcpy(xlongname, gridptr->xlongname);
}


/*
@Function  gridInqXunits
@Title     Get the units of a X-axis

@Prototype void gridInqXunits(int gridID, const char *units)
@Parameter
935
936
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  units    Units of the X-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
937
938

@Description
939
The function @func{gridInqXunits} returns the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
940
941

@Result
942
@func{gridInqXunits} returns the units of the X-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

@EndFunction
*/
void gridInqXunits(int gridID, char *xunits)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  strcpy(xunits, gridptr->xunits);
}


void gridInqXstdname(int gridID, char *xstdname)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  strcpy(xstdname, gridptr->xstdname);
}


/*
@Function  gridInqYname
@Title     Get the name of a Y-axis

@Prototype void gridInqYname(int gridID, const char *name)
@Parameter
972
973
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  name     Name of the Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
974
975

@Description
976
The function @func{gridInqYname} returns the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
977
978

@Result
979
@func{gridInqYname} returns the name of the Y-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998

@EndFunction
*/
void gridInqYname(int gridID, char *yname)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  strcpy(yname, gridptr->yname);
}


/*
@Function  gridInqYlongname
@Title     Get the longname of a Y-axis

@Prototype void gridInqXlongname(int gridID, const char *longname)
@Parameter
999
1000
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  longname Longname of the Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1001
1002

@Description
1003
The function @func{gridInqYlongname} returns the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1004
1005

@Result
1006
@func{gridInqYlongname} returns the longname of the Y-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025

@EndFunction
*/
void gridInqYlongname(int gridID, char *ylongname)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  strcpy(ylongname, gridptr->ylongname);
}


/*
@Function  gridInqYunits
@Title     Get the units of a Y-axis

@Prototype void gridInqYunits(int gridID, const char *units)
@Parameter
1026
1027
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  units    Units of the Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1028
1029

@Description
1030
The function @func{gridInqYunits} returns the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1031
1032

@Result
1033
@func{gridInqYunits} returns the units of the Y-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061

@EndFunction
*/
void gridInqYunits(int gridID, char *yunits)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  strcpy(yunits, gridptr->yunits);
}

void gridInqYstdname(int gridID, char *ystdname)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  strcpy(ystdname, gridptr->ystdname);
}


/*
@Function  gridInqType
@Title     Get the type of a Grid

@Prototype int gridInqType(int gridID)
@Parameter
1062
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1063
1064

@Description
1065
The function @func{gridInqType} returns the type of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1066
1067

@Result
1068
@func{gridInqType} returns the type of the grid,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1069
one of the set of predefined CDI grid types.
1070
The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1071
1072
@func{GRID_LONLAT}, @func{GRID_LAMBERT}, @func{GRID_SPECTRAL}, @func{GRID_GME},
@func{GRID_CURVILINEAR} and @func{GRID_CELL}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094

@EndFunction
*/
int gridInqType(int gridID)
{
  static char func[] = "gridInqType";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  return (gridptr->type);
}


/*
@Function  gridInqSize
@Title     Get the size of a Grid

@Prototype int gridInqSize(int gridID)
@Parameter
1095
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1096
1097

@Description
1098
The function @func{gridInqSize} returns the size of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1099
1100

@Result
1101
@func{gridInqSize} returns the number of grid points of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173

@EndFunction
*/
int gridInqSize(int gridID)
{
  static char func[] = "gridInqSize";
  int size = 0;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  size = gridptr->size;

  if ( ! size )
    {
      int xsize, ysize;

      xsize = gridptr->xsize;
      ysize = gridptr->ysize;

      if ( ysize )
	size = xsize *ysize;
      else
	size = xsize;

      gridptr->size = size;  
    }

  return (size);
}


static int nsp2trunc(int nsp)
{
  /*  nsp = (trunc+1)*(trunc+1)              */
  /*      => trunc^2 + 3*trunc - (x-2) = 0   */
  /*                                         */
  /*  with:  y^2 + p*y + q = 0               */
  /*         y = -p/2 +- sqrt((p/2)^2 - q)   */
  /*         p = 3 and q = - (x-2)           */
  int trunc;

  trunc = (int) (sqrt(nsp*4.0 + 1) - 3) / 2;

  return (trunc);
}


int gridInqTrunc(int gridID)
{
  static char func[] = "gridInqTrunc";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  if ( gridptr->trunc == 0 )
    {
      if ( gridptr->type == GRID_SPECTRAL )
	gridptr->trunc = nsp2trunc(gridptr->size);
      /*
      else if      ( gridptr->type == GRID_GAUSSIAN )
	gridptr->trunc = nlat2trunc(gridptr->ysize);
      */
    }

  return (gridptr->trunc);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1174

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
void gridDefTrunc(int gridID, int trunc)
{
  static char func[] = "gridDefTrunc";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  gridptr->trunc = trunc;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1187
1188
1189
1190
1191
1192
1193

/*
@Function  gridDefXsize
@Title     Define the number of values of a X-axis

@Prototype void gridDefXsize(int gridID, int xsize)
@Parameter
1194
1195
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  xsize    Number of values of a X-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1196
1197

@Description
1198
The function @func{gridDefXsize} defines the number of values of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1199
1200
1201

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
void gridDefXsize(int gridID, int xsize)
{
  static char func[] = "gridDefXsize";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  if ( xsize > gridInqSize(gridID) )
    Error(func, "xsize %d is greater then gridsize %d", xsize, gridInqSize(gridID));

  if ( gridInqType(gridID) == GRID_CELL && xsize != gridInqSize(gridID) )
    Error(func, "xsize %d must be equal gridsize %d for gridtype CELL", xsize, gridInqSize(gridID));

  gridptr->xsize = xsize;

  if ( gridInqType(gridID) != GRID_CELL && 
       gridptr->xsize*gridptr->ysize > gridInqSize(gridID) )
    Error(func, "inconsistent grid declaration! (xsize %d ysize %d gridsize %d)",
	  gridptr->xsize, gridptr->ysize, gridInqSize(gridID));
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridDefPrec(int gridID, int prec)
{
  static char func[] = "gridDefPrec";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  gridptr->prec = prec;
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
int gridInqPrec(int gridID)
{
  static char func[] = "gridInqPrec";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  return (gridptr->prec);
}


/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1273
1274
@Function  gridInqXsize
@Title     Get the number of values of a X-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1275

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1276
@Prototype void gridInqXsize(int gridID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1277
@Parameter
1278
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1279
1280

@Description
1281
The function @func{gridInqXsize} returns the number of values of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1282
1283

@Result
1284
@func{gridInqXsize} returns the number of values of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301

@EndFunction
*/
int gridInqXsize(int gridID)
{
  static char func[] = "gridInqXsize";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  return (gridptr->xsize);
}


/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1302
1303
@Function  gridDefYsize
@Title     Define the number of values of a Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1304

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1305
@Prototype void gridDefYsize(int gridID, int ysize)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1306
@Parameter
1307
1308
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  ysize    Number of values of a Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1309
1310

@Description
1311
The function @func{gridDefYsize} defines the number of values of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339

@EndFunction
*/
void gridDefYsize(int gridID, int ysize)
{
  static char func[] = "gridDefYsize";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  if ( ysize > gridInqSize(gridID) )
    Error(func, "ysize %d is greater then gridsize %d", ysize, gridInqSize(gridID));

  if ( gridInqType(gridID) == GRID_CELL && ysize != gridInqSize(gridID) )
    Error(func, "ysize %d must be equal gridsize %d for gridtype CELL", ysize, gridInqSize(gridID));

  gridptr->ysize = ysize;

  if ( gridInqType(gridID) != GRID_CELL && 
       gridptr->xsize*gridptr->ysize > gridInqSize(gridID) )
    Error(func, "inconsistent grid declaration! (xsize %d ysize %d gridsize %d)",
	  gridptr->xsize, gridptr->ysize, gridInqSize(gridID));
}


/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1340
1341
@Function  gridInqYsize
@Title     Get the number of values of a Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1342

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1343
@Prototype void gridInqYsize(int gridID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1344
@Parameter
1345
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1346
1347

@Description
1348
The function @func{gridInqYsize} returns the number of values of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1349
1350

@Result
1351
@func{gridInqYsize} returns the number of values of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485

@EndFunction
*/
int gridInqYsize(int gridID)
{
  static char func[] = "gridInqYsize";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  return (gridptr->ysize);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridDefRowlon(int gridID, int nrowlon, int *rowlon)
{
  static char func[] = "gridDefRowlon";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);
  
  grid_check_ptr(func, gridptr);

  gridptr->rowlon = (int *) malloc(nrowlon*sizeof(int));
  gridptr->nrowlon = nrowlon;

  memcpy(gridptr->rowlon, rowlon, nrowlon*sizeof(int));
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridInqRowlon(int gridID, int *rowlon)
{
  static char func[] = "gridInqRowlon";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  if ( gridptr->rowlon == 0 )  Error(func, "undefined pointer");

  memcpy(rowlon, gridptr->rowlon, gridptr->nrowlon*sizeof(int));
}


int gridInqMask(int gridID, int *mask)
{
  static char func[] = "gridInqMask";
  int size;
  int i;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  if ( gridptr->type == GRID_CURVILINEAR )
    size = gridptr->size;
  else
    size = gridptr->ysize;

  if ( CDI_Debug && size == 0 )
    Warning(func, "size undefined for gridID = %d", gridID);
    
  if ( mask && gridptr->mask )
    for ( i = 0; i < size; i++ )
      mask[i] =  gridptr->mask[i];

  if ( gridptr->mask == NULL ) size = 0;

  return (size);
}


void gridDefMask(int gridID, int *mask)
{
  static char func[] = "gridDefMask";
  int size;
  int gridtype;
  int i;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  gridtype = gridptr->type;

  if ( gridtype == GRID_CELL || gridtype == GRID_CURVILINEAR )
    size = gridptr->size;
  else
    size = gridptr->xsize;

  if ( size == 0 )
    Error(func, "size undefined for gridID = %d", gridID);
    
  if ( gridptr->mask == NULL )
    gridptr->mask = (int *) malloc(size*sizeof(int));
  else if ( CDI_Debug )
    Warning(func, "mask allready defined!");

  for ( i = 0; i < size; i++ )
    gridptr->mask[i] = mask[i];
}


/*
@Function  gridInqXvals
@Title     Get all values of a X-axis

@Prototype int gridInqXvals(int gridID, double *xvals)
@Parameter
1486
1487
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  xvals    X-values of the grid
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1488
1489

@Description
1490
The function @func{gridInqXvals} returns all values of the X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1491
1492

@Result
1493
1494
1495
Upon successful completion @func{gridInqXvals} returns the number of values and
the values are stored in @func{xvals}.
Otherwise, 0 is returned and @func{xvals} is empty.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533

@EndFunction
*/
int gridInqXvals(int gridID, double *xvals)
{
  static char func[] = "gridInqXvals";
  int size;
  int i;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  if ( gridptr->type == GRID_CURVILINEAR )
    size = gridptr->size;
  else
    size = gridptr->xsize;

  if ( CDI_Debug && size == 0 )
    Warning(func, "size undefined for gridID = %d", gridID);
    
  if ( xvals && gridptr->xvals )
    for ( i = 0; i < size; i++ )
      xvals[i] = gridptr->xvals[i];

  if ( gridptr->xvals == NULL ) size = 0;

  return (size);
}


/*
@Function  gridDefXvals
@Title     Define the values of a X-axis

@Prototype void gridDefXvals(int gridID, double *xvals)
@Parameter
1534
1535
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  xvals    X-values of the grid
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1536
1537

@Description
1538
The function @func{gridDefXvals} defines all values of the X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579

@EndFunction
*/
void gridDefXvals(int gridID, double *xvals)
{
  static char func[] = "gridDefXvals";
  int size;
  int gridtype;
  int i;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  gridtype = gridptr->type;

  if ( gridtype == GRID_CELL || gridtype == GRID_CURVILINEAR )
    size = gridptr->size;
  else
    size = gridptr->xsize;

  if ( size == 0 )
    Error(func, "size undefined for gridID = %d", gridID);
    
  if ( gridptr->xvals == NULL )
    gridptr->xvals = (double *) malloc(size*sizeof(double));
  else if ( CDI_Debug )
    Warning(func, "values allready defined!");

  for ( i = 0; i < size; i++ )
    gridptr->xvals[i] = xvals[i];
}


/*
@Function  gridInqYvals
@Title     Get all values of a Y-axis

@Prototype int gridInqYvals(int gridID, double *yvals)
@Parameter
1580
1581
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  yvals    Y-values of the grid
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1582
1583

@Description
1584
The function @func{gridInqYvals} returns all values of the Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1585
1586

@Result
1587
1588
1589
Upon successful completion @func{gridInqYvals} returns the number of values and
the values are stored in @func{yvals}.
Otherwise, 0 is returned and @func{yvals} is empty.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609

@EndFunction
*/
int gridInqYvals(int gridID, double *yvals)
{
  static char func[] = "gridInqYvals";
  int size;
  int i;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  if ( gridptr->type == GRID_CURVILINEAR )
    size = gridptr->size;
  else
    size = gridptr->ysize;

  if ( CDI_Debug && size == 0 )
1610
    Warning(func, "Size undefined for gridID = %d!", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
    
  if ( yvals && gridptr->yvals )
    for ( i = 0; i < size; i++ )
      yvals[i] =  gridptr->yvals[i];

  if ( gridptr->yvals == NULL ) size = 0;

  return (size);
}


/*
@Function  gridDefYvals
@Title     Define the values of a Y-axis

@Prototype void gridDefYvals(int gridID, double *yvals)
@Parameter
1628
1629
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
    @Item  yvals    Y-values of the grid
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1630
1631

@Description
1632
The function @func{gridDefYvals} defines all values of the Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655

@EndFunction
*/
void gridDefYvals(int gridID, double *yvals)
{
  static char func[] = "gridDefYvals";
  int size;
  int gridtype;
  int i;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  gridtype = gridptr->type;

  if ( gridtype == GRID_CELL || gridtype == GRID_CURVILINEAR )
    size = gridptr->size;
  else
    size = gridptr->ysize;

  if ( size == 0 )
1656
    Error(func, "Size undefined for gridID = %d!", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1657
1658
1659
1660
    
  if ( gridptr->yvals == NULL )
    gridptr->yvals = (double *) malloc(size*sizeof(double));
  else if ( CDI_Debug )
1661
    Warning(func, "Values allready defined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
    
  for ( i = 0; i < size; i++ )
    gridptr->yvals[i] = yvals[i];
}


double gridInqXval(int gridID, int index)
{
  double xval = 0;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  if ( gridptr->xvals )
    xval = gridptr->xvals[index];

  return (xval);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
double gridInqYval(int gridID, int index)
{
  double yval = 0;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  if ( gridptr->yvals )
    yval = gridptr->yvals[index];

  return (yval);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
double gridInqXinc(int gridID)
{
  double xinc;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  xinc = gridptr->xinc;

1725
  if ( (! (fabs(xinc) > 0)) && gridptr->xvals )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
    {
      int xsize;
      double *xvals;

      xsize = gridptr->xsize;
      xvals = gridptr->xvals;

      if ( xsize > 1 )
	{
	  int i;
	  xinc = fabs(xvals[0] - xvals[1]);
	  for ( i = 2; i < xsize; i++ )
	    if ( fabs(fabs(xvals[i-1] - xvals[i]) - xinc) > (xinc/1000) ) break;
		  
	  if ( i < xsize ) xinc = 0;
	}
    }

  return (xinc);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
double gridInqYinc(int gridID)
{
  double yinc;
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  yinc = gridptr->yinc;

1767
  if ( (! (fabs(yinc) > 0)) && gridptr->yvals )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
    {
      int ysize;
      double *yvals;

      ysize = gridptr->ysize;
      yvals = gridptr->yvals;

      if ( ysize > 1 )
	{
	  int i;
	  yinc = fabs(yvals[1] - yvals[0]);
	  for ( i = 2; i < ysize; i++ )
	    if ( fabs(fabs(yvals[i] - yvals[i-1]) - yinc) > (yinc/1000) ) break;
1781

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
	  if ( i < ysize ) yinc = 0;
	  else             yinc = yvals[1] - yvals[0];
	}
    }

  return (yinc);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
double gridInqXpole(int gridID)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  return (gridptr->xpole);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridDefXpole(int gridID, double xpole)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  gridptr->isRotated = TRUE;
  gridptr->xpole = xpole;
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
double gridInqYpole(int gridID)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  return (gridptr->ypole);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridDefYpole(int gridID, double ypole)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  gridptr->isRotated = TRUE;
  gridptr->ypole = ypole;
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
double gridInqAngle(int gridID)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  return (gridptr->angle);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridDefAngle(int gridID, double angle)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  gridptr->isRotated = TRUE;
  gridptr->angle = angle;
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
int gridInqGMEnd(int gridID)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  return (gridptr->nd);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridDefGMEnd(int gridID, int nd)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  gridptr->nd = nd;
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
int gridInqGMEni(int gridID)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  return (gridptr->ni);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridDefGMEni(int gridID, int ni)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  gridptr->ni = ni;
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
int gridInqGMEni2(int gridID)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  return (gridptr->ni2);
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridDefGMEni2(int gridID, int ni2)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  gridptr->ni2 = ni2;
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
int gridInqGMEni3(int gridID)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  return (gridptr->ni3);
}

void gridDefGMEni3(int gridID, int ni3)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  gridptr->ni3 = ni3;
}


/*
@Function  
@Title     

@Prototype 
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridChangeType(int gridID, int gridtype)
{
  static char func[] = "gridChangeType";
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  Message(func, "Change grid type from %s to %s\n",
	  gridNamePtr(gridptr->type),
	  gridNamePtr(gridtype));
  
  gridptr->type = gridtype;
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
static void grid_check_cyclic(GRID *gridptr)
{
  int xsize, ysize, gridsize;
  int i1, i2, j, k1, k2, nc;
  double xinc, x0;
  const double *xvals, *xbounds;

  gridptr->isCyclic = FALSE;

  gridsize = gridptr->size;
  xsize = gridptr->xsize;
  ysize = gridptr->ysize;
  xvals = gridptr->xvals;
  xbounds = gridptr->xbounds;

  if ( gridptr->type == GRID_GAUSSIAN || gridptr->type == GRID_LONLAT )
    {
      if ( xvals && xsize > 1 )
	{
	  xinc = xvals[1] - xvals[0];
	  x0 = 2*xvals[xsize-1]-xvals[xsize-2]-360;
	  if ( DBL_IS_EQUAL(x0, xvals[0]) ) gridptr->isCyclic = TRUE;
	}
    }
  else if ( gridptr->type == GRID_CURVILINEAR )
    {
      if ( xbounds && xsize > 1 )
	{
	  gridptr->isCyclic = TRUE;
	  for ( j = 0; j < ysize; ++j )
	    {
	      i1 = j*xsize*4;
	      i2 = j*xsize*4+(xsize-1)*4;
	      nc = 0;
	      for ( k1 = 0; k1 < 4; ++k1 )
		{
		  for ( k2 = 0; k2 < 4; ++k2 )
		    {
		      if ( DBL_IS_EQUAL(xbounds[i1+k1], xbounds[i2+k2]) ) nc++;
		    }
		}
	      if ( nc != 2 )
		{
		  gridptr->isCyclic = FALSE;
		  break;
		}
	    }
	}
    }
}


2140
int gridIsCircular(int gridID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  if ( gridptr->isCyclic == CDI_UNDEFID ) grid_check_cyclic(gridptr);

  return ( gridptr->isCyclic );
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
int gridIsRotated(int gridID)
{
  GRID *gridptr;

  gridptr = grid_to_pointer(gridID);

  return ( gridptr->isRotated );
}


int gridCompare(int gridID, GRID grid)
{
  int differ = 1;
  int xsize, ysize;

  xsize = grid.xsize;
  ysize = grid.ysize;

  if ( grid.type == gridInqType(gridID) || grid.type == GRID_GENERIC )
    {
      if ( grid.size == gridInqSize(gridID) )
	{
	  differ = 0;
	  if ( grid.type == GRID_LONLAT )
	    {
	      /*
	      printf("gridID      %d\n", gridID);
	      printf("grid.xsize  %d\n", grid.xsize);
	      printf("grid.ysize  %d\n", grid.ysize);
	      printf("grid.xfirst %f\n", grid.xfirst);
	      printf("grid.yfirst %f\n", grid.yfirst);
	      printf("grid.xfirst %f\n", gridInqXval(gridID, 0));
	      printf("grid.yfirst %f\n", gridInqYval(gridID, 0));
	      printf("grid.xinc   %f\n", grid.xinc);
	      printf("grid.yinc   %f\n", grid.yinc);
	      printf("grid.xinc   %f\n", gridInqXinc(gridID));
	      printf("grid.yinc   %f\n", gridInqYinc(gridID));
	      */
	      if ( grid.xsize == gridInqXsize(gridID) && grid.ysize == gridInqYsize(gridID) )
		{
		  if ( grid.xdef == 2 && grid.ydef == 2 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
2193
2194
2195
2196
2197
		    if ( ! (DBL_IS_EQUAL(grid.xfirst, 0) && DBL_IS_EQUAL(grid.xlast, 0) && DBL_IS_EQUAL(grid.xinc, 0)) &&
			 ! (DBL_IS_EQUAL(grid.yfirst, 0) && DBL_IS_EQUAL(grid.ylast, 0) && DBL_IS_EQUAL(grid.yinc, 0)) &&
			 !DBL_IS_EQUAL(grid.xfirst, grid.xlast) && !DBL_IS_EQUAL(grid.yfirst, grid.ylast) )
		      if ( !DBL_IS_EQUAL(grid.xfirst, gridInqXval(gridID, 0)) ||
			   !DBL_IS_EQUAL(grid.yfirst, gridInqYval(gridID, 0)) ||
Uwe Schulzweida's avatar
Uwe Schulzweida committed
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
			   fabs(fabs(grid.xinc) - fabs(gridInqXinc(gridID))) > fabs(grid.xinc/1000) ||
			   fabs(fabs(grid.yinc) - fabs(gridInqYinc(gridID))) > fabs(grid.yinc/1000) )
			{
			  differ = 1;
			}
		}
	      else
		differ = 1;		
	    }
	  else if ( grid.type == GRID_GAUSSIAN )
	    {
	      if ( grid.xsize == gridInqXsize(gridID) && grid.ysize == gridInqYsize(gridID) )
		{
		  if ( grid.xdef == 2 && grid.ydef == 2 )
		    {
Uwe Schulzweida's avatar
Uwe Schulzweida committed
2213
2214
		      if ( ! (DBL_IS_EQUAL(grid.xfirst, 0) && DBL_IS_EQUAL(grid.xlast, 0) && DBL_IS_EQUAL(grid.xinc, 0)) &&
			   ! (DBL_IS_EQUAL(grid.yfirst, 0) && DBL_IS_EQUAL(grid.ylast, 0)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
			if ( fabs(grid.xfirst - gridInqXval(gridID, 0)) > 0.001 ||
			     fabs(grid.yfirst - gridInqYval(gridID, 0)) > 0.001 ||
			     fabs(fabs(grid.xinc) - fabs(gridInqXinc(gridID))) > fabs(grid.xinc/1000))
			  {
			    differ = 1;
			  }
		    }
		  else
		    {
		      if ( grid.xvals && grid.yvals )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
2225
2226
			if ( !DBL_IS_EQUAL(grid.xvals[0], gridInqXval(gridID, 0)) ||
			     !DBL_IS_EQUAL(grid.yvals[0], gridInqYval(gridID, 0)) ||
Uwe Schulzweida's avatar
Uwe Schulzweida committed
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
			     fabs(fabs(grid.xvals[1]-grid.xvals[0]) - fabs(gridInqXinc(gridID)))
			     > fabs(grid.xinc/1000))
			  {
			    differ = 1;
			  }
		    }
		}
	      else
		differ = 1;		
	    }
	  else if ( grid.type == GRID_CURVILINEAR )
	    {
	      /*
	      printf("gridID      %d\n", gridID);
	      printf("grid.xsize  %d\n", grid.xsize);
	      printf("grid.ysize  %d\n", grid.ysize);
	      printf("grid.xfirst %f\n", grid.xvals[0]);
	      printf("grid.yfirst %f\n", grid.yvals[0]);
	      printf("grid.xfirst %f\n", gridInqXval(gridID, 0));
	      printf("grid.yfirst %f\n", gridInqYval(gridID, 0));
	      printf("grid.xlast  %f\n", grid.xvals[xsize-1]);
	      printf("grid.ylast  %f\n", grid.yvals[ysize-1]);
	      printf("grid.xlast  %f\n", gridInqXval(gridID, xsize-1));
	      printf("grid.ylast  %f\n", gridInqYval(gridID, ysize-1));
	      */
	      if ( grid.xsize == gridInqXsize(gridID) && grid.ysize == gridInqYsize(gridID) )
		if ( grid.xvals && gridInqXvalsPtr(gridID) )
		  {
		    if ( fabs(grid.xvals[0] - gridInqXval(gridID, 0)) > 1.e-9 ||
			 fabs(grid.xvals[xsize-1] - gridInqXval(gridID, xsize-1)) > 1.e-9 )
		      differ = 1;
		  }
		if ( grid.yvals && gridInqYvalsPtr(gridID) )
		  {
		    if ( fabs(grid.yvals[0] - gridInqYval(gridID, 0)) > 1.e-9 ||
			 fabs(grid.yvals[ysize-1] - gridInqYval