grid.c 76 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
#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[] = {
24
25
26
27
28
29
30
31
32
33
34
35
  /*  0 */  "undefined",
  /*  1 */  "generic",
  /*  2 */  "gaussian",
  /*  3 */  "gaussian reduced",
  /*  4 */  "lonlat",
  /*  5 */  "spectral",
  /*  6 */  "fourier",
  /*  7 */  "gme",
  /*  8 */  "trajectory",
  /*  9 */  "cell",
  /* 10 */  "curvilinear",
  /* 11 */  "lcc",
36
37
38
  /* 12 */  "lcc2",
  /* 13 */  "laea",
  /* 14 */  "sinusoidal",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
39
40
41
42
43
};


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

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

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;
73
  grid_t *ptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
75
76
77
  struct _gridPtrToIdx *next;
} gridPtrToIdx;


Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
79
static gridPtrToIdx *_gridList  = NULL;
static gridPtrToIdx *_gridAvail = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
81
82
83
84
85


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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
87
  assert(_gridList == NULL);

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


117
grid_t *grid_to_pointer(int idx)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
{
  static char func[] = "grid_to_pointer";
120
  grid_t *gridptr = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

  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 */
140
static int grid_from_pointer(grid_t *ptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{
  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
162
	Warning(func, "Too many open grids (limit is %d)!", _grid_max);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
163
164
165
166
167
168
169
170
171
172

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

  return (idx);
}


173
static void grid_init_entry(grid_t *gridptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
{
  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;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
189
190
191
192
193
194
  gridptr->lcc_originLon = 0.0;
  gridptr->lcc_originLat = 0.0;
  gridptr->lcc_lonParY  = 0.0;
  gridptr->lcc_lat1     = 0.0;
  gridptr->lcc_lat2     = 0.0;
  gridptr->lcc_xinc     = 0.0;
  gridptr->lcc_yinc     = 0.0;
195
196
  gridptr->lcc_projflag = 0;
  gridptr->lcc_scanflag = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
197
  gridptr->lcc_defined  = FALSE;
198
199
200
201
202
203
  gridptr->lcc2_lon_0   = 0.0;
  gridptr->lcc2_lat_0   = 0.0;
  gridptr->lcc2_lat_1   = 0.0;
  gridptr->lcc2_lat_2   = 0.0;
  gridptr->lcc2_a       = 0.0;
  gridptr->lcc2_defined = FALSE;
204
205
206
207
  gridptr->laea_lon_0   = 0.0;
  gridptr->laea_lat_0   = 0.0;
  gridptr->laea_a       = 0.0;
  gridptr->laea_defined = FALSE;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
208
209
210
211
212
213
214
215
216
217
  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
218
  gridptr->isCyclic     = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  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;
}


235
static grid_t *grid_new_entry(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
236
237
{
  static char func[] = "grid_new_entry";
238
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
239

240
  gridptr = (grid_t *) malloc(sizeof(grid_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
242
243
244
245
246
247

  if ( gridptr ) grid_init_entry(gridptr);

  return (gridptr);
}


248
static void grid_delete_entry(grid_t *gridptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
249
250
251
252
253
254
255
256
257
258
259
260
{
  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
261
  _gridAvail          = &_gridList[idx];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

  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);

285
286
  GRID_LOCK

Uwe Schulzweida's avatar
Uwe Schulzweida committed
287
288
  grid_init_pointer();

289
290
  GRID_UNLOCK

Uwe Schulzweida's avatar
Uwe Schulzweida committed
291
292
293
294
  _grid_init = TRUE;
}


295
static void grid_copy(grid_t *gridptr2, grid_t *gridptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
296
297
298
299
{
  int gridID2;

  gridID2 = gridptr2->self;
300
  memcpy(gridptr2, gridptr1, sizeof(grid_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
301
302
303
304
  gridptr2->self = gridID2;
}


305
static void grid_check_ptr(const char *func, grid_t *gridptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
{
  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;

334
  if ( (! (fabs(xinc) > 0)) && xsize > 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
    {
      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;
}


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
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
379
380
381
382
383
384
385
386
387
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 )
	{
388
	  calc_gaussaw(yvals, ysize, yfirst, ylast);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
389

390
	  if ( ! (IS_EQUAL(yfirst, 0) && IS_EQUAL(ylast, 0)) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
391
392
	    if ( fabs(yvals[0] - yfirst) > 0.001 || fabs(yvals[ysize-1] - ylast) > 0.001 )
	      {
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
		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
		  {
415
		    Warning(func, "Cannot calculate gaussian latitudes for lat1 = %g latn = %g", yfirst, ylast);
416
417
418
419
420
421
		    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
422
423
424
425
426
427
428
429
430
431
432
	      }
	}
      else
	{
	  yvals[0] = yfirst;
	  yvals[ysize-1] = ylast;
	}
    }
  /*     else if ( gridtype == GRID_LONLAT || gridtype == GRID_GENERIC ) */
  else
    {
433
      if ( (! (fabs(yinc) > 0)) && ysize > 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
434
	{
435
	  if ( IS_EQUAL(yfirst, ylast) && IS_NOT_EQUAL(yfirst, 0) ) ylast *= -1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

	  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";
470
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

  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;
511
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562

  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;
563
	  if ( ! (fabs(yinc) > 0) )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
	    {
	      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));
      */
    }
}


/*
590
@Function  gridCreate
591
@Title     Create a horizontal Grid
Uwe Schulzweida's avatar
Uwe Schulzweida committed
592

593
@Prototype int gridCreate(int gridtype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
595
@Parameter
    @Item  gridtype  The type of the grid, one of the set of predefined CDI grid types.
596
                     The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
597
                     @func{GRID_LONLAT}, @func{GRID_LCC}, @func{GRID_SPECTRAL},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
598
                     @func{GRID_GME}, @func{GRID_CURVILINEAR} and @func{GRID_CELL}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
599
600
601
    @Item  size      Number of gridpoints.

@Description
602
The function @func{gridCreate} creates a horizontal Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
603
604

@Result
605
@func{gridCreate} returns an identifier to the Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
606
607

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

@Source
Uwe Schulzweida's avatar
Uwe Schulzweida committed
611
612
613
614
#include "cdi.h"
   ...
#define  NLON  12
#define  NLAT   6
Uwe Schulzweida's avatar
Uwe Schulzweida committed
615
   ...
Uwe Schulzweida's avatar
Uwe Schulzweida committed
616
617
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
618
619
int gridID;
   ...
Uwe Schulzweida's avatar
Uwe Schulzweida committed
620
621
622
gridID = gridCreate(GRID_LONLAT, NLON*NLAT);
gridDefXsize(gridID, NLON);
gridDefYsize(gridID, NLAT);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
623
624
625
626
gridDefXvals(gridID, lons);
gridDefYvals(gridID, lats);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
627
628
@EndFunction
*/
629
int gridCreate(int gridtype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
630
{
631
  static char func[] = "gridCreate";
Uwe Schulzweida's avatar
Uwe Schulzweida committed
632
  int gridID;
633
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

  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");
674
	/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
675
676
677
678
679
680
681
682
	if ( gridtype == GRID_CURVILINEAR )
	  {
	    strcpy(gridptr->xstdname, "grid_longitude");
	    strcpy(gridptr->ystdname, "grid_latitude");
	    gridDefXunits(gridID, "degrees");
	    gridDefYunits(gridID, "degrees");
	  }
	else
683
	*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
684
685
686
687
688
689
690
691
692
693
694
	  {
	    strcpy(gridptr->xstdname, "longitude");
	    strcpy(gridptr->ystdname, "latitude");
	    gridDefXunits(gridID, "degrees_east");
	    gridDefYunits(gridID, "degrees_north");
	  }

	break;
      }
    case GRID_GME:
    case GRID_CELL:
695
696
697
698
699
700
701
702
703
      {
	gridDefXname(gridID, "lon");
	gridDefYname(gridID, "lat");
	strcpy(gridptr->xstdname, "longitude");
	strcpy(gridptr->ystdname, "latitude");
	gridDefXunits(gridID, "degrees_east");
	gridDefYunits(gridID, "degrees_north");
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
704
705
706
707
    case GRID_GENERIC:
      {
	gridDefXname(gridID, "x");
	gridDefYname(gridID, "y");
708
709
710
711
	strcpy(gridptr->xstdname, "grid_longitude");
	strcpy(gridptr->ystdname, "grid_latitude");
	gridDefXunits(gridID, "degrees");
	gridDefYunits(gridID, "degrees");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
712
713
	break;
      }
714
    case GRID_LCC2:
715
    case GRID_SINUSOIDAL:
716
    case GRID_LAEA:
717
718
719
720
721
722
723
724
725
      {
	gridDefXname(gridID, "x");
	gridDefYname(gridID, "y");
	strcpy(gridptr->xstdname, "projection_x_coordinate");
	strcpy(gridptr->ystdname, "projection_y_coordinate");
	gridDefXunits(gridID, "m");
	gridDefYunits(gridID, "m");
	break;
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
726
727
728
729
730
731
    }

  return (gridID);
}


732
733
734
735
736
737
/*
@Function  gridDestroy
@Title     Destroy a horizontal Grid

@Prototype void gridDestroy(int gridID)
@Parameter
738
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
739
740
741
742
743
744
745
746

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


Uwe Schulzweida's avatar
Uwe Schulzweida committed
747
748
749
char *gridNamePtr(int gridtype)
{
  char *name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
750
  int size = (int) (sizeof(Grids)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772

  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
773
774
    @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
775
776

@Description
777
The function @func{gridDefXname} defines the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
778
779
780
781
782

@EndFunction
*/
void gridDefXname(int gridID, char *xname)
{
783
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
784
785
786
787
788
789
790
791
792
793
794
795
796
797

  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
798
799
    @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
800
801

@Description
802
The function @func{gridDefXlongname} defines the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
803
804
805
806
807

@EndFunction
*/
void gridDefXlongname(int gridID, char *xlongname)
{
808
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
809
810
811
812
813
814
815
816
817
818
819
820
821
822

  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
823
824
    @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
825
826

@Description
827
The function @func{gridDefXunits} defines the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
828
829
830
831
832

@EndFunction
*/
void gridDefXunits(int gridID, char *xunits)
{
833
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847

  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
848
849
    @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
850
851

@Description
852
The function @func{gridDefYname} defines the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
853
854
855
856
857

@EndFunction
*/
void gridDefYname(int gridID, char *yname)
{
858
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
859
860
861
862
863
864
865
866
867
868
869
870
871
872

  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
873
874
    @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
875
876

@Description
877
The function @func{gridDefYlongname} defines the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
878
879
880
881
882

@EndFunction
*/
void gridDefYlongname(int gridID, char *ylongname)
{
883
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
884
885
886
887
888
889
890
891
892
893
894
895
896
897

  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
898
899
    @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
900
901

@Description
902
The function @func{gridDefYunits} defines the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
903
904
905
906
907

@EndFunction
*/
void gridDefYunits(int gridID, char *yunits)
{
908
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
909
910
911
912
913
914
915
916
917
918
919
920
921
922

  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
923
924
    @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
925
926

@Description
927
The function @func{gridInqXname} returns the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
928
929

@Result
930
@func{gridInqXname} returns the name of the X-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
931
932
933
934
935

@EndFunction
*/
void gridInqXname(int gridID, char *xname)
{
936
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
937
938
939
940
941
942
943
944
945
946
947
948
949

  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
950
951
    @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
952
953

@Description
954
The function @func{gridInqXlongname} returns the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
955
956

@Result
957
@func{gridInqXlongname} returns the longname of the X-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
958
959
960
961
962

@EndFunction
*/
void gridInqXlongname(int gridID, char *xlongname)
{
963
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
964
965
966
967
968
969
970
971
972
973
974
975
976

  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
977
978
    @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
979
980

@Description
981
The function @func{gridInqXunits} returns the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
982
983

@Result
984
@func{gridInqXunits} returns the units of the X-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
985
986
987
988
989

@EndFunction
*/
void gridInqXunits(int gridID, char *xunits)
{
990
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
991
992
993
994
995
996
997
998
999

  gridptr = grid_to_pointer(gridID);

  strcpy(xunits, gridptr->xunits);
}


void gridInqXstdname(int gridID, char *xstdname)
{
1000
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013

  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
1014
1015
    @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
1016
1017

@Description
1018
The function @func{gridInqYname} returns the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1019
1020

@Result
1021
@func{gridInqYname} returns the name of the Y-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1022
1023
1024
1025
1026

@EndFunction
*/
void gridInqYname(int gridID, char *yname)
{
1027
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040

  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
1041
1042
    @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
1043
1044

@Description
1045
The function @func{gridInqYlongname} returns the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1046
1047

@Result
1048
@func{gridInqYlongname} returns the longname of the Y-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1049
1050
1051
1052
1053

@EndFunction
*/
void gridInqYlongname(int gridID, char *ylongname)
{
1054
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067

  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
1068
1069
    @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
1070
1071

@Description
1072
The function @func{gridInqYunits} returns the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1073
1074

@Result
1075
@func{gridInqYunits} returns the units of the Y-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1076
1077
1078
1079
1080

@EndFunction
*/
void gridInqYunits(int gridID, char *yunits)
{
1081
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1082
1083
1084
1085
1086
1087
1088
1089

  gridptr = grid_to_pointer(gridID);

  strcpy(yunits, gridptr->yunits);
}

void gridInqYstdname(int gridID, char *ystdname)
{
1090
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103

  gridptr = grid_to_pointer(gridID);

  strcpy(ystdname, gridptr->ystdname);
}


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

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

@Description
1107
The function @func{gridInqType} returns the type of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1108
1109

@Result
1110
@func{gridInqType} returns the type of the grid,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1111
one of the set of predefined CDI grid types.
1112
The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1113
@func{GRID_LONLAT}, @func{GRID_LCC}, @func{GRID_SPECTRAL}, @func{GRID_GME},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1114
@func{GRID_CURVILINEAR} and @func{GRID_CELL}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1115
1116
1117
1118
1119
1120

@EndFunction
*/
int gridInqType(int gridID)
{
  static char func[] = "gridInqType";
1121
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136

  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
1137
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1138
1139

@Description
1140
The function @func{gridInqSize} returns the size of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1141
1142

@Result
1143
@func{gridInqSize} returns the number of grid points of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1144
1145
1146
1147
1148
1149
1150

@EndFunction
*/
int gridInqSize(int gridID)
{
  static char func[] = "gridInqSize";
  int size = 0;
1151
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196

  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";
1197
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215

  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
1216

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1217
1218
1219
void gridDefTrunc(int gridID, int trunc)
{
  static char func[] = "gridDefTrunc";
1220
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1221
1222
1223
1224
1225
1226
1227
1228

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  gridptr->trunc = trunc;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1229
1230
1231
1232
1233
1234
1235

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

@Prototype void gridDefXsize(int gridID, int xsize)
@Parameter
1236
1237
    @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
1238
1239

@Description
1240
The function @func{gridDefXsize} defines the number of values of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1241
1242
1243

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1244
1245
1246
void gridDefXsize(int gridID, int xsize)
{
  static char func[] = "gridDefXsize";
1247
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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
1273
1274
1275
1276
1277
1278
1279
1280

  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";
1281
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303

  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";
1304
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  return (gridptr->prec);
}


/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1315
1316
@Function  gridInqXsize
@Title     Get the number of values of a X-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1317

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1318
@Prototype void gridInqXsize(int gridID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1319
@Parameter
1320
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1321
1322

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

@Result
1326
@func{gridInqXsize} returns the number of values of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1327
1328
1329
1330
1331
1332

@EndFunction
*/
int gridInqXsize(int gridID)
{
  static char func[] = "gridInqXsize";
1333
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  return (gridptr->xsize);
}


/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1344
1345
@Function  gridDefYsize
@Title     Define the number of values of a Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1346

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1347
@Prototype void gridDefYsize(int gridID, int ysize)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1348
@Parameter
1349
1350
    @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
1351
1352

@Description
1353
The function @func{gridDefYsize} defines the number of values of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1354
1355
1356
1357
1358
1359

@EndFunction
*/
void gridDefYsize(int gridID, int ysize)
{
  static char func[] = "gridDefYsize";
1360
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381

  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
1382
1383
@Function  gridInqYsize
@Title     Get the number of values of a Y-axis
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1384

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1385
@Prototype void gridInqYsize(int gridID)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1386
@Parameter
1387
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1388
1389

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

@Result
1393
@func{gridInqYsize} returns the number of values of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1394
1395
1396
1397
1398
1399

@EndFunction
*/
int gridInqYsize(int gridID)
{
  static char func[] = "gridInqYsize";
1400
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422

  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";
1423
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
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

  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";
1449
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465

  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;
1466
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1467
1468
1469
1470
1471

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

1472
  size = gridptr->size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1473
1474

  if ( CDI_Debug && size == 0 )
1475
    Warning(func, "Size undefined for gridID = %d", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
    
  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;
1493
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1494
1495
1496
1497
1498
1499
1500

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

  gridtype = gridptr->type;

1501
  size = gridptr->size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1502
1503

  if ( size == 0 )
1504
    Error(func, "Size undefined for gridID = %d", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1505
1506
1507
1508
    
  if ( gridptr->mask == NULL )
    gridptr->mask = (int *) malloc(size*sizeof(int));
  else if ( CDI_Debug )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1509
    Warning(func, "mask already defined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521

  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
1522
1523
    @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
1524
1525

@Description
1526
The function @func{gridInqXvals} returns all values of the X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1527
1528

@Result
1529
1530
1531
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
1532
1533
1534
1535
1536
1537
1538
1539

@EndFunction
*/
int gridInqXvals(int gridID, double *xvals)
{
  static char func[] = "gridInqXvals";
  int size;
  int i;
1540
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1541
1542
1543
1544
1545

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

1546
  if ( gridptr->type == GRID_CURVILINEAR || gridptr->type == GRID_CELL )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1547
1548
1549
1550
1551
    size = gridptr->size;
  else
    size = gridptr->xsize;

  if ( CDI_Debug && size == 0 )
1552
    Warning(func, "Size undefined for gridID = %d", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
    
  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
1570
1571
    @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
1572
1573

@Description
1574
The function @func{gridDefXvals} defines all values of the X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1575
1576
1577
1578
1579
1580
1581
1582
1583

@EndFunction
*/
void gridDefXvals(int gridID, double *xvals)
{
  static char func[] = "gridDefXvals";
  int size;
  int gridtype;
  int i;
1584
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597

  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 )
1598
    Error(func, "Size undefined for gridID = %d", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1599
1600
1601
1602
    
  if ( gridptr->xvals == NULL )
    gridptr->xvals = (double *) malloc(size*sizeof(double));
  else if ( CDI_Debug )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1603
    Warning(func, "values already defined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615

  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
1616
1617
    @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
1618
1619

@Description
1620
The function @func{gridInqYvals} returns all values of the Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1621
1622

@Result
1623
1624
1625
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
1626
1627
1628
1629
1630
1631
1632
1633

@EndFunction
*/
int gridInqYvals(int gridID, double *yvals)
{
  static char func[] = "gridInqYvals";
  int size;
  int i;
1634
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1635
1636
1637
1638
1639

  gridptr = grid_to_pointer(gridID);

  grid_check_ptr(func, gridptr);

1640
  if ( gridptr->type == GRID_CURVILINEAR || gridptr->type == GRID_CELL )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1641
1642
1643
1644
1645
    size = gridptr->size;
  else
    size = gridptr->ysize;

  if ( CDI_Debug && size == 0 )
1646
    Warning(func, "Size undefined for gridID = %d!", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
    
  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
1664
1665
    @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
1666
1667

@Description
1668
The function @func{gridDefYvals} defines all values of the Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1669
1670
1671
1672
1673
1674
1675
1676
1677

@EndFunction
*/
void gridDefYvals(int gridID, double *yvals)
{
  static char func[] = "gridDefYvals";
  int size;
  int gridtype;
  int i;
1678
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691

  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 )
1692
    Error(func, "Size undefined for gridID = %d!", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1693
1694
1695
1696
    
  if ( gridptr->yvals == NULL )
    gridptr->yvals = (double *) malloc(size*sizeof(double));
  else if ( CDI_Debug )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1697
    Warning(func, "Values already defined!");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1698
1699
1700
1701
1702
1703
1704
1705
1706
    
  for ( i = 0; i < size; i++ )
    gridptr->yvals[i] = yvals[i];
}


double gridInqXval(int gridID, int index)
{
  double xval = 0;
1707
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730

  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;
1731
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754

  gridptr = grid_to_pointer(gridID);

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

  return (yval);
}


/*</