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

Thomas Jahns's avatar
Thomas Jahns committed
5
#include <assert.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
#include <string.h>
7
#include <float.h>  /* FLT_EPSILON */
8
#include <limits.h> /* INT_MAX     */
9

Uwe Schulzweida's avatar
Uwe Schulzweida committed
10
11
#include "dmemory.h"
#include "cdi.h"
12
#include "cdi_cksum.h"
13
#include "cdi_int.h"
14
#include "cdi_uuid.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
15
#include "grid.h"
16
#include "gaussgrid.h"
17
#include "resource_handle.h"
18
#include "resource_unpack.h"
19
#include "namespace.h"
20
#include "serialize.h"
21
22
23
24
#include "vlist.h"

#undef  UNDEFID
#define UNDEFID -1
Uwe Schulzweida's avatar
Uwe Schulzweida committed
25

26
27
28
/* the value in the second pair of brackets must match the length of
 * the longest string (including terminating NUL) */
static const char Grids[][17] = {
29
30
31
32
33
34
35
36
37
  /*  0 */  "undefined",
  /*  1 */  "generic",
  /*  2 */  "gaussian",
  /*  3 */  "gaussian reduced",
  /*  4 */  "lonlat",
  /*  5 */  "spectral",
  /*  6 */  "fourier",
  /*  7 */  "gme",
  /*  8 */  "trajectory",
38
  /*  9 */  "unstructured",
39
40
  /* 10 */  "curvilinear",
  /* 11 */  "lcc",
41
42
43
  /* 12 */  "lcc2",
  /* 13 */  "laea",
  /* 14 */  "sinusoidal",
44
  /* 15 */  "projection",
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
/* must match table below */
enum xystdname_idx {
  grid_xystdname_grid_latlon,
  grid_xystdname_latlon,
  grid_xystdname_projection,
};
static const char xystdname_tab[][2][24] = {
  [grid_xystdname_grid_latlon] = { "grid_longitude",
                                   "grid_latitude" },
  [grid_xystdname_latlon] = { "longitude",
                              "latitude" },
  [grid_xystdname_projection] = { "projection_x_coordinate",
                                  "projection_y_coordinate" },

};


Uwe Schulzweida's avatar
Uwe Schulzweida committed
64

65
66
67
static int    gridCompareP    ( void * gridptr1, void * gridptr2 );
static void   gridDestroyP    ( void * gridptr );
static void   gridPrintP      ( void * gridptr, FILE * fp );
68
static int    gridGetPackSize ( void * gridptr, void *context);
69
static void   gridPack        ( void * gridptr, void * buff, int size,
70
				int *position, void *context);
Deike Kleberg's avatar
minimal    
Deike Kleberg committed
71
static int    gridTxCode      ( void );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72

73
static const resOps gridOps = {
74
75
76
77
78
79
  gridCompareP,
  gridDestroyP,
  gridPrintP,
  gridGetPackSize,
  gridPack,
  gridTxCode
80
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
81

82
static int  GRID_Debug = 0;   /* If set to 1, debugging */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
83

84
85
86
87
grid_t *gridID2Ptr(int gridID)
{
  return (grid_t *)reshGetVal(gridID, &gridOps);
}
88
#define gridID2Ptr(gridID) (grid_t *)reshGetVal(gridID, &gridOps)
89
#define gridMark4Update(gridID) reshSetStatus(gridID, &gridOps, RESH_DESYNC_IN_USE)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90

91

Deike Kleberg's avatar
Deike Kleberg committed
92
void grid_init(grid_t *gridptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
93
{
94
95
96
97
98
  gridptr->self          = CDI_UNDEFID;
  gridptr->type          = CDI_UNDEFID;
  gridptr->proj          = CDI_UNDEFID;
  gridptr->mask          = NULL;
  gridptr->mask_gme      = NULL;
99
100
  gridptr->x.vals        = NULL;
  gridptr->y.vals        = NULL;
101
  gridptr->area          = NULL;
102
103
  gridptr->x.bounds      = NULL;
  gridptr->y.bounds      = NULL;
104
105
106
107
108
109
110
111
  gridptr->rowlon        = NULL;
  gridptr->nrowlon       = 0;
  gridptr->xfirst        = 0.0;
  gridptr->xlast         = 0.0;
  gridptr->xinc          = 0.0;
  gridptr->yfirst        = 0.0;
  gridptr->ylast         = 0.0;
  gridptr->yinc          = 0.0;
112

113
114
115
116
117
118
119
120
121
122
  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;
  gridptr->lcc.projflag  = 0;
  gridptr->lcc.scanflag  = 0;
  gridptr->lcc.defined   = FALSE;
123
124
125
126
127
128
  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;
129

130
131
132
133
  gridptr->laea.lon_0    = 0.0;
  gridptr->laea.lat_0    = 0.0;
  gridptr->laea.a        = 0.0;
  gridptr->laea.defined  = FALSE;
134

135
136
137
138
  gridptr->gme.nd        = 0;
  gridptr->gme.ni        = 0;
  gridptr->gme.ni2       = 0;
  gridptr->gme.ni3       = 0;
139

140
141
142
143
144
145
146
  gridptr->trunc         = 0;
  gridptr->nvertex       = 0;
  gridptr->number        = 0;
  gridptr->position      = 0;
  gridptr->reference     = NULL;
  gridptr->prec          = 0;
  gridptr->size          = 0;
147
148
  gridptr->x.size        = 0;
  gridptr->y.size        = 0;
149
150
151
152
153
154
155
156
  gridptr->np            = 0;
  gridptr->xdef          = 0;
  gridptr->ydef          = 0;
  gridptr->isCyclic      = CDI_UNDEFID;
  gridptr->isRotated     = FALSE;
  gridptr->xpole         = 0.0;
  gridptr->ypole         = 0.0;
  gridptr->angle         = 0.0;
157
  gridptr->lcomplex      = false;
158
159
160
  gridptr->hasdims       = true;
  gridptr->x.dimname[0]  = 0;
  gridptr->y.dimname[0]  = 0;
161
  gridptr->vdimname[0]   = 0;
162
163
164
165
166
167
168
169
  gridptr->x.name[0]     = 0;
  gridptr->y.name[0]     = 0;
  gridptr->x.longname[0] = 0;
  gridptr->y.longname[0] = 0;
  gridptr->x.units[0]    = 0;
  gridptr->y.units[0]    = 0;
  gridptr->x.stdname     = NULL;
  gridptr->y.stdname     = NULL;
Thomas Jahns's avatar
Thomas Jahns committed
170
  memset(gridptr->uuid, 0, CDI_UUID_SIZE);
171
172
173
  gridptr->name          = NULL;
  gridptr->vtable        = &cdiGridVtable;
  gridptr->extraData     = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
175
}

176

177
178
static void
grid_free_components(grid_t *gridptr)
Deike Kleberg's avatar
Deike Kleberg committed
179
{
180
  void *p2free[] = { gridptr->mask, gridptr->mask_gme,
181
                   gridptr->x.vals, gridptr->y.vals,
182
                   gridptr->x.bounds, gridptr->y.bounds,
183
184
185
186
187
                   gridptr->rowlon, gridptr->area,
                   gridptr->reference, gridptr->name };
  for (size_t i = 0; i < sizeof (p2free) / sizeof (p2free[0]); ++i)
    if (p2free[i]) Free(p2free[i]);
}
Deike Kleberg's avatar
Deike Kleberg committed
188

189
190
191
void grid_free(grid_t *gridptr)
{
  grid_free_components(gridptr);
Deike Kleberg's avatar
Deike Kleberg committed
192
193
194
  grid_init(gridptr);
}

195
196
static grid_t *
gridNewEntry(cdiResH resH)
197
{
198
  grid_t *gridptr = (grid_t*) Malloc(sizeof(grid_t));
199
200
201
202
203
204
205
206
  grid_init(gridptr);
  if (resH == CDI_UNDEFID)
    gridptr->self = reshPut(gridptr, &gridOps);
  else
    {
      gridptr->self = resH;
      reshReplace(resH, gridptr, &gridOps);
    }
207
  return gridptr;
208
209
}

210
static
211
void gridInit(void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212
{
213
  static int gridInitialized = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214

215
  if ( gridInitialized ) return;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
216

Thomas Jahns's avatar
Thomas Jahns committed
217
  gridInitialized = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
218

Uwe Schulzweida's avatar
Uwe Schulzweida committed
219
  const char *env = getenv("GRID_DEBUG");
220
  if ( env ) GRID_Debug = atoi(env);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
222
}

Thomas Jahns's avatar
Thomas Jahns committed
223
224
static void
grid_copy_base_scalar_fields(grid_t *gridptrOrig, grid_t *gridptrDup)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
{
Thomas Jahns's avatar
Thomas Jahns committed
226
227
228
229
230
231
  memcpy(gridptrDup, gridptrOrig, sizeof(grid_t));
  gridptrDup->self = CDI_UNDEFID;
  if (gridptrOrig->reference)
    gridptrDup->reference = strdupx(gridptrOrig->reference);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
232

Thomas Jahns's avatar
Thomas Jahns committed
233
234
235
236
237
238
239
static grid_t *
grid_copy_base(grid_t *gridptrOrig)
{
  grid_t *gridptrDup = (grid_t *)Malloc(sizeof (*gridptrDup));
  gridptrOrig->vtable->copyScalarFields(gridptrOrig, gridptrDup);
  gridptrOrig->vtable->copyArrayFields(gridptrOrig, gridptrDup);
  return gridptrDup;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
240
241
}

242
unsigned cdiGridCount(void)
Thomas Jahns's avatar
Thomas Jahns committed
243
{
244
  return reshCountType(&gridOps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
245
246
}

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
static inline
void gridSetString(char *gridstrname, const char *name, size_t len)
{
  if ( len > CDI_MAX_NAME ) len = CDI_MAX_NAME;
  strncpy(gridstrname, name, len);
  gridstrname[len - 1] = 0;
}

static inline
void gridGetString(char *name, const char *gridstrname, size_t len)
{
  if ( len > CDI_MAX_NAME ) len = CDI_MAX_NAME;
  strncpy(name, gridstrname, len);
  name[len - 1] = 0;
}

263
static inline void
264
gridSetName(char *gridstrname, const char *name)
265
{
266
267
  strncpy(gridstrname, name, CDI_MAX_NAME);
  gridstrname[CDI_MAX_NAME - 1] = 0;
268
269
}

270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
void
cdiGridTypeInit(grid_t *gridptr, int gridtype, int size)
{
  gridptr->type = gridtype;
  gridptr->size = size;

  switch (gridtype)
    {
    case GRID_CURVILINEAR:
      gridptr->nvertex = 4;
      /* Fall through */
    case GRID_LONLAT:
    case GRID_GAUSSIAN:
    case GRID_GAUSSIAN_REDUCED:
    case GRID_TRAJECTORY:
      {
        if ( gridtype == GRID_TRAJECTORY )
          {
288
289
            if ( gridptr->x.name[0] == 0 ) gridSetName(gridptr->x.name, "tlon");
            if ( gridptr->y.name[0] == 0 ) gridSetName(gridptr->y.name, "tlat");
290
291
292
          }
        else
          {
293
294
            if ( gridptr->x.name[0] == 0 ) gridSetName(gridptr->x.name, "lon");
            if ( gridptr->y.name[0] == 0 ) gridSetName(gridptr->y.name, "lat");
295
          }
296
297
        gridSetName(gridptr->x.longname, "longitude");
        gridSetName(gridptr->y.longname, "latitude");
298
299
300
301

        /*
        if ( gridtype == GRID_CURVILINEAR )
          {
302
303
            gridptr->x.stdname = xystdname_tab[grid_xystdname_grid_latlon][0];
            gridptr->y.stdname = xystdname_tab[grid_xystdname_grid_latlon][1];
304
305
306
307
308
309
            gridDefXunits(gridID, "degrees");
            gridDefYunits(gridID, "degrees");
          }
        else
        */
          {
310
311
            gridptr->x.stdname = xystdname_tab[grid_xystdname_latlon][0];
            gridptr->y.stdname = xystdname_tab[grid_xystdname_latlon][1];
312
313
            gridSetName(gridptr->x.units, "degrees_east");
            gridSetName(gridptr->y.units, "degrees_north");
314
315
316
317
318
          }

        break;
      }
    case GRID_UNSTRUCTURED:
319
      gridptr->x.size = size;
320
321
322
      /* Fall through */
    case GRID_GME:
      {
323
324
        if ( gridptr->x.name[0] == 0 ) gridSetName(gridptr->x.name, "lon");
        if ( gridptr->y.name[0] == 0 ) gridSetName(gridptr->y.name, "lat");
325
326
        gridptr->x.stdname = xystdname_tab[grid_xystdname_latlon][0];
        gridptr->y.stdname = xystdname_tab[grid_xystdname_latlon][1];
327
328
        gridSetName(gridptr->x.units, "degrees_east");
        gridSetName(gridptr->y.units, "degrees_north");
329
330
331
332
333
        break;
      }
    case GRID_GENERIC:
      {

334
        /* gridptr->x.size = size; */
335
336
        if ( gridptr->x.name[0] == 0 ) gridSetName(gridptr->x.name, "x");
        if ( gridptr->y.name[0] == 0 ) gridSetName(gridptr->y.name, "y");
337
        /*
338
339
340
341
        strcpy(gridptr->x.stdname, "grid_longitude");
        strcpy(gridptr->y.stdname, "grid_latitude");
        gridptr->x.stdname = xystdname_tab[grid_xystdname_grid_latlon][0];
        gridptr->y.stdname = xystdname_tab[grid_xystdname_grid_latlon][1];
342
343
344
345
346
347
348
349
350
        gridDefXunits(gridID, "degrees");
        gridDefYunits(gridID, "degrees");
        */
        break;
      }
    case GRID_LCC2:
    case GRID_SINUSOIDAL:
    case GRID_LAEA:
      {
351
352
        if ( gridptr->x.name[0] == 0 ) gridSetName(gridptr->x.name, "x");
        if ( gridptr->y.name[0] == 0 ) gridSetName(gridptr->y.name, "y");
353
354
        gridptr->x.stdname = xystdname_tab[grid_xystdname_projection][0];
        gridptr->y.stdname = xystdname_tab[grid_xystdname_projection][1];
355
356
        gridSetName(gridptr->x.units, "m");
        gridSetName(gridptr->y.units, "m");
357
358
359
360
361
362
        break;
      }
    }
}


363
// used also in CDO
364
void gridGenXvals(int xsize, double xfirst, double xlast, double xinc, double *restrict xvals)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
365
{
366
  if ( (! (fabs(xinc) > 0)) && xsize > 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
368
    {
      if ( xfirst >= xlast )
Thomas Jahns's avatar
Thomas Jahns committed
369
370
371
372
        {
          while ( xfirst >= xlast ) xlast += 360;
          xinc = (xlast-xfirst)/(xsize);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
373
      else
Thomas Jahns's avatar
Thomas Jahns committed
374
375
376
        {
          xinc = (xlast-xfirst)/(xsize-1);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
377
378
    }

379
  for ( int i = 0; i < xsize; ++i )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
380
381
382
    xvals[i] = xfirst + i*xinc;
}

383
static
384
void calc_gaussgrid(double *restrict yvals, int ysize, double yfirst, double ylast)
385
{
386
  double *restrict yw = (double *) Malloc((size_t)ysize * sizeof(double));
387
  gaussaw(yvals, yw, (size_t)ysize);
388
  Free(yw);
389
  for (int i = 0; i < ysize; i++ )
390
391
392
393
    yvals[i] = asin(yvals[i])/M_PI*180.0;

  if ( yfirst < ylast && yfirst > -90.0 && ylast < 90.0 )
    {
394
395
      int yhsize = ysize/2;
      for (int i = 0; i < yhsize; i++ )
Thomas Jahns's avatar
Thomas Jahns committed
396
        {
397
          double ytmp = yvals[i];
Thomas Jahns's avatar
Thomas Jahns committed
398
399
400
          yvals[i] = yvals[ysize-i-1];
          yvals[ysize-i-1] = ytmp;
        }
401
402
403
    }
}

404
// used also in CDO
Thomas Jahns's avatar
Thomas Jahns committed
405
void gridGenYvals(int gridtype, int ysize, double yfirst, double ylast, double yinc, double *restrict yvals)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
{
407
  const double deleps = 0.002;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
408
409
410
411

  if ( gridtype == GRID_GAUSSIAN || gridtype == GRID_GAUSSIAN_REDUCED )
    {
      if ( ysize > 2 )
Deike Kleberg's avatar
Deike Kleberg committed
412
413
414
415
416
417
	{
	  calc_gaussgrid(yvals, ysize, yfirst, ylast);

	  if ( ! (IS_EQUAL(yfirst, 0) && IS_EQUAL(ylast, 0)) )
	    if ( fabs(yvals[0] - yfirst) > deleps || fabs(yvals[ysize-1] - ylast) > deleps )
	      {
418
		double *restrict ytmp = NULL;
Deike Kleberg's avatar
Deike Kleberg committed
419
		int nstart, lfound = 0;
420
		int ny = (int) (180./(fabs(ylast-yfirst)/(ysize-1)) + 0.5);
Deike Kleberg's avatar
Deike Kleberg committed
421
422
423
		ny -= ny%2;
		if ( ny > ysize && ny < 4096 )
		  {
424
		    ytmp = (double *) Malloc((size_t)ny * sizeof (double));
Deike Kleberg's avatar
Deike Kleberg committed
425
		    calc_gaussgrid(ytmp, ny, yfirst, ylast);
426
427
428
429
430
431
                    {
                      int i;
                      for ( i = 0; i < (ny-ysize); i++ )
                        if ( fabs(ytmp[i] - yfirst) < deleps ) break;
                      nstart = i;
                    }
Deike Kleberg's avatar
Deike Kleberg committed
432

433
434
		    lfound = (nstart+ysize-1) < ny
                      && fabs(ytmp[nstart+ysize-1] - ylast) < deleps;
435
436
437
438
                    if ( lfound )
                      {
                        for (int i = 0; i < ysize; i++) yvals[i] = ytmp[i+nstart];
                      }
Deike Kleberg's avatar
Deike Kleberg committed
439
440
		  }

441
		if ( !lfound )
Deike Kleberg's avatar
Deike Kleberg committed
442
		  {
443
		    Warning("Cannot calculate gaussian latitudes for lat1 = %g latn = %g!", yfirst, ylast);
444
		    for (int i = 0; i < ysize; i++ ) yvals[i] = 0;
Deike Kleberg's avatar
Deike Kleberg committed
445
446
447
448
		    yvals[0] = yfirst;
		    yvals[ysize-1] = ylast;
		  }

449
		if ( ytmp ) Free(ytmp);
Deike Kleberg's avatar
Deike Kleberg committed
450
451
	      }
	}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
452
      else
Thomas Jahns's avatar
Thomas Jahns committed
453
454
455
456
        {
          yvals[0] = yfirst;
          yvals[ysize-1] = ylast;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
457
458
459
460
    }
  /*     else if ( gridtype == GRID_LONLAT || gridtype == GRID_GENERIC ) */
  else
    {
461
      if ( (! (fabs(yinc) > 0)) && ysize > 1 )
Thomas Jahns's avatar
Thomas Jahns committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
        {
          if ( IS_EQUAL(yfirst, ylast) && IS_NOT_EQUAL(yfirst, 0) ) ylast *= -1;

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

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

486
      for (int i = 0; i < ysize; i++ )
Thomas Jahns's avatar
Thomas Jahns committed
487
        yvals[i] = yfirst + i*yinc;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
488
489
490
    }
  /*
    else
491
    Error("unable to calculate values for %s grid!", gridNamePtr(gridtype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
492
493
494
495
  */
}

/*
496
@Function  gridCreate
497
@Title     Create a horizontal Grid
Uwe Schulzweida's avatar
Uwe Schulzweida committed
498

499
@Prototype int gridCreate(int gridtype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
500
501
@Parameter
    @Item  gridtype  The type of the grid, one of the set of predefined CDI grid types.
502
                     The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
503
                     @func{GRID_LONLAT}, @func{GRID_LCC}, @func{GRID_SPECTRAL},
504
                     @func{GRID_GME}, @func{GRID_CURVILINEAR} and @func{GRID_UNSTRUCTURED}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
505
506
507
    @Item  size      Number of gridpoints.

@Description
508
The function @func{gridCreate} creates a horizontal Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
510

@Result
511
@func{gridCreate} returns an identifier to the Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
512
513

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

@Source
Uwe Schulzweida's avatar
Uwe Schulzweida committed
517
518
#include "cdi.h"
   ...
519
520
#define  nlon  12
#define  nlat   6
Uwe Schulzweida's avatar
Uwe Schulzweida committed
521
   ...
522
523
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
524
525
int gridID;
   ...
526
527
528
gridID = gridCreate(GRID_LONLAT, nlon*nlat);
gridDefXsize(gridID, nlon);
gridDefYsize(gridID, nlat);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
529
530
531
532
gridDefXvals(gridID, lons);
gridDefYvals(gridID, lats);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
533
534
@EndFunction
*/
535
int gridCreate(int gridtype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
536
{
537
  if ( CDI_Debug ) Message("gridtype=%s  size=%d", gridNamePtr(gridtype), size);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
538

539
  if ( size < 0 || size > INT_MAX ) Error("Grid size (%d) out of bounds (0 - %d)!", size, INT_MAX);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
540

541
  gridInit();
542

Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
  grid_t *gridptr = gridNewEntry(CDI_UNDEFID);
544
  if ( ! gridptr ) Error("No memory");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
545

Uwe Schulzweida's avatar
Uwe Schulzweida committed
546
  int gridID = gridptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
547

548
  if ( CDI_Debug ) Message("gridID: %d", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
549

550
  cdiGridTypeInit(gridptr, gridtype, size);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
551

Uwe Schulzweida's avatar
Uwe Schulzweida committed
552
  return gridID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
553
554
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
555
556
static
void gridDestroyKernel( grid_t * gridptr )
557
558
559
{
  int id;

Deike Kleberg's avatar
Deike Kleberg committed
560
  xassert ( gridptr );
561
562
563

  id = gridptr->self;

564
  grid_free_components(gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
565
  Free( gridptr );
566

567
  reshRemove ( id, &gridOps );
568
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
569

570
571
572
573
574
575
/*
@Function  gridDestroy
@Title     Destroy a horizontal Grid

@Prototype void gridDestroy(int gridID)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
576
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
577
578
579
580
581

@EndFunction
*/
void gridDestroy(int gridID)
{
582
  grid_t *gridptr = gridID2Ptr(gridID);
583
  gridptr->vtable->destroy(gridptr);
584
585
}

586

587
void gridDestroyP ( void * gridptr )
588
{
589
  ((grid_t *)gridptr)->vtable->destroy((grid_t *)gridptr);
590
591
592
}


593
const char *gridNamePtr(int gridtype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
{
595
  int size = (int) (sizeof(Grids)/sizeof(Grids[0]));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
596

597
  const char *name = gridtype >= 0 && gridtype < size ? Grids[gridtype] : Grids[GRID_GENERIC];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
598

Uwe Schulzweida's avatar
Uwe Schulzweida committed
599
  return name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600
601
602
603
604
605
606
607
}


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

608
static
609
void *grid_key_to_ptr(grid_t *gridptr, int key)
610
{
611
  void *keyptr = NULL;
612
613
614

  switch (key)
    {
615
616
617
618
619
620
621
622
623
    case CDI_KEY_XNAME:      keyptr = (void*)gridptr->x.name; break;
    case CDI_KEY_XLONGNAME:  keyptr = (void*)gridptr->x.longname; break;
    case CDI_KEY_XUNITS:     keyptr = (void*)gridptr->x.units; break;
    case CDI_KEY_YNAME:      keyptr = (void*)gridptr->y.name; break;
    case CDI_KEY_YLONGNAME:  keyptr = (void*)gridptr->y.longname; break;
    case CDI_KEY_YUNITS:     keyptr = (void*)gridptr->y.units; break;
    case CDI_KEY_XDIMNAME:   keyptr = (void*)gridptr->x.dimname; break;
    case CDI_KEY_YDIMNAME:   keyptr = (void*)gridptr->y.dimname; break;
    case CDI_KEY_VDIMNAME:   keyptr = (void*)gridptr->vdimname; break;
624
625
    }

626
  return keyptr;
627
628
}

629
/*
630
@Function  cdiGridDefKeyStr
631
632
@Title     Define a CDI grid string value from a key

633
@Prototype int cdiGridDefKeyStr(int gridID, int key, int size, const char *mesg)
634
635
636
637
638
639
640
@Parameter
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
    @Item  key      The key to be searched
    @Item  size     The allocated length of the string on input
    @Item  mesg     The address of a string where the data will be read

@Description
641
The function @func{cdiGridDefKeyStr} defines a CDI grid string value from a key.
642
643

@Result
644
@func{cdiGridDefKeyStr} returns 0 if OK and integer value on error.
645
646
647

@EndFunction
*/
648
int cdiGridDefKeyStr(int gridID, int key, int size, const char *mesg)
649
{
650
  if ( size < 1 || mesg == NULL || *mesg == 0 ) return -1;
651
652
653

  grid_t *gridptr = gridID2Ptr(gridID);

654
  char *gridstring = (char*)grid_key_to_ptr(gridptr, key);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
655
  if ( gridstring == NULL )
656
657
658
659
660
    {
      Warning("CDI grid string key %d not supported!", key);
      return -1;
    }

661
  gridSetString(gridstring, mesg, (size_t)size);
662
663
664
665
666
667
  gridMark4Update(gridID);

  return 0;
}

/*
668
@Function  cdiGridInqKeyStr
669
670
@Title     Get a CDI grid string value from a key

671
@Prototype int cdiGridInqKeyStr(int gridID, int key, int size, char *mesg)
672
673
674
675
676
677
678
679
680
681
@Parameter
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
    @Item  key      The key to be searched.
    @Item  size     The allocated length of the string on input.
    @Item  mesg     The address of a string where the data will be retrieved.
                    The caller must allocate space for the returned string.
                    The maximum possible length, in characters, of the string
                    is given by the predefined constant @func{CDI_MAX_NAME}.

@Description
682
The function @func{cdiGridInqKeyStr} return a CDI grid string value from a key.
683
684

@Result
685
@func{cdiGridInqKeyStr} returns 0 if OK and integer value on error.
686
687
688

@EndFunction
*/
689
int cdiGridInqKeyStr(int gridID, int key, int size, char *mesg)
690
{
691
  if ( size < 1 || mesg == NULL ) return -1;
692
693

  grid_t *gridptr = gridID2Ptr(gridID);
694
  const char *gridstring = (const char*)grid_key_to_ptr(gridptr, key);
695
  if ( gridstring == NULL)
696
697
698
699
700
    {
      Warning("CDI grid string key %d not supported!", key);
      return -1;
    }

701
  gridGetString(mesg, gridstring, (size_t)size);
702
703
704
705

  return 0;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
706
707
708
709
710
711
/*
@Function  gridDefXname
@Title     Define the name of a X-axis

@Prototype void gridDefXname(int gridID, const char *name)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
712
713
    @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
714
715

@Description
716
The function @func{gridDefXname} defines the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
717
718
719

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
720
void gridDefXname(int gridID, const char *xname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
721
{
722
  (void)cdiGridDefKeyStr(gridID, CDI_KEY_XNAME, CDI_MAX_NAME, xname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
723
724
725
726
727
728
729
730
}

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

@Prototype void gridDefXlongname(int gridID, const char *longname)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
731
732
    @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
733
734

@Description
735
The function @func{gridDefXlongname} defines the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
736
737
738

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
739
void gridDefXlongname(int gridID, const char *xlongname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
740
{
741
  (void)cdiGridDefKeyStr(gridID, CDI_KEY_XLONGNAME, CDI_MAX_NAME, xlongname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
742
743
744
745
746
747
748
749
}

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

@Prototype void gridDefXunits(int gridID, const char *units)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
750
751
    @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
752
753

@Description
754
The function @func{gridDefXunits} defines the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
755
756
757

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
758
void gridDefXunits(int gridID, const char *xunits)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
759
{
760
  (void)cdiGridDefKeyStr(gridID, CDI_KEY_XUNITS, CDI_MAX_NAME, xunits);
761
762
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
763
764
765
766
767
768
/*
@Function  gridDefYname
@Title     Define the name of a Y-axis

@Prototype void gridDefYname(int gridID, const char *name)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
769
770
    @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
771
772

@Description
773
The function @func{gridDefYname} defines the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
774
775
776

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
777
void gridDefYname(int gridID, const char *yname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
778
{
779
  (void)cdiGridDefKeyStr(gridID, CDI_KEY_YNAME, CDI_MAX_NAME, yname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
780
781
782
783
784
785
786
787
}

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

@Prototype void gridDefYlongname(int gridID, const char *longname)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
788
789
    @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
790
791

@Description
792
The function @func{gridDefYlongname} defines the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
793
794
795

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
796
void gridDefYlongname(int gridID, const char *ylongname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
797
{
798
  (void)cdiGridDefKeyStr(gridID, CDI_KEY_YLONGNAME, CDI_MAX_NAME, ylongname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
799
800
801
802
803
804
805
806
}

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

@Prototype void gridDefYunits(int gridID, const char *units)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
807
808
    @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
809
810

@Description
811
The function @func{gridDefYunits} defines the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
812
813
814

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
815
void gridDefYunits(int gridID, const char *yunits)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
816
{
817
  (void)cdiGridDefKeyStr(gridID, CDI_KEY_YUNITS, CDI_MAX_NAME, yunits);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
818
819
820
821
822
823
}

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

Deike Kleberg's avatar
Deike Kleberg committed
824
@Prototype void gridInqXname(int gridID, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
825
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
826
827
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate} or @fref{vlistInqVarGrid}.
    @Item  name     Name of the X-axis. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
828
                    returned string. The maximum possible length, in characters, of
829
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
830
831

@Description
832
The function @func{gridInqXname} returns the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
833
834

@Result
835
@func{gridInqXname} returns the name of the X-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
836
837
838
839
840

@EndFunction
*/
void gridInqXname(int gridID, char *xname)
{
841
  (void)cdiGridInqKeyStr(gridID, CDI_KEY_XNAME, CDI_MAX_NAME, xname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
842
843
}

844
845
846
const char *gridInqXnamePtr(int gridID)
{
  grid_t *gridptr = gridID2Ptr(gridID);
847
  return gridptr->x.name;
848
849
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
850
851
852
853
/*
@Function  gridInqXlongname
@Title     Get the longname of a X-axis

Deike Kleberg's avatar
Deike Kleberg committed
854
@Prototype void gridInqXlongname(int gridID, char *longname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
855
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
856
857
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate} or @fref{vlistInqVarGrid}.
    @Item  longname Longname of the X-axis. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
858
                    returned string. The maximum possible length, in characters, of
859
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
860
861

@Description
862
The function @func{gridInqXlongname} returns the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
863
864

@Result
865
@func{gridInqXlongname} returns the longname of the X-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
866
867
868
869
870

@EndFunction
*/
void gridInqXlongname(int gridID, char *xlongname)
{
871
  (void)cdiGridInqKeyStr(gridID, CDI_KEY_XLONGNAME, CDI_MAX_NAME, xlongname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
872
873
874
875
876
877
}

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

Deike Kleberg's avatar
Deike Kleberg committed
878
@Prototype void gridInqXunits(int gridID, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
879
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
880
881
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate} or @fref{vlistInqVarGrid}.
    @Item  units    Units of the X-axis. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
882
                    returned string. The maximum possible length, in characters, of
883
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
884
885

@Description
886
The function @func{gridInqXunits} returns the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
887
888

@Result
889
@func{gridInqXunits} returns the units of the X-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
890
891
892
893
894

@EndFunction
*/
void gridInqXunits(int gridID, char *xunits)
{
895
  (void)cdiGridInqKeyStr(gridID, CDI_KEY_XUNITS, CDI_MAX_NAME, xunits);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
896
897
898
899
900
}


void gridInqXstdname(int gridID, char *xstdname)
{
901
  grid_t *gridptr = gridID2Ptr(gridID);
902
903
  if ( gridptr->x.stdname )
    strcpy(xstdname, gridptr->x.stdname);
904
905
  else
    xstdname[0] = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
906
907
908
909
910
911
}

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

Deike Kleberg's avatar
Deike Kleberg committed
912
@Prototype void gridInqYname(int gridID, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
913
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
914
915
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate} or @fref{vlistInqVarGrid}.
    @Item  name     Name of the Y-axis. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
916
                    returned string. The maximum possible length, in characters, of
917
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
918
919

@Description
920
The function @func{gridInqYname} returns the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
921
922

@Result
923
@func{gridInqYname} returns the name of the Y-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
924
925
926
927
928

@EndFunction
*/
void gridInqYname(int gridID, char *yname)
{
929
  (void)cdiGridInqKeyStr(gridID, CDI_KEY_YNAME, CDI_MAX_NAME, yname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
930
931
}

932
933
934
const char *gridInqYnamePtr(int gridID)
{
  grid_t *gridptr = gridID2Ptr(gridID);
935
  return gridptr->y.name;
936
937
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
938
939
940
941
/*
@Function  gridInqYlongname
@Title     Get the longname of a Y-axis

Deike Kleberg's avatar
Deike Kleberg committed
942
@Prototype void gridInqXlongname(int gridID, char *longname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
943
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
944
945
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate} or @fref{vlistInqVarGrid}.
    @Item  longname Longname of the Y-axis. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
946
                    returned string. The maximum possible length, in characters, of
947
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
948
949

@Description
950
The function @func{gridInqYlongname} returns the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
951
952

@Result
953
@func{gridInqYlongname} returns the longname of the Y-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
954
955
956
957
958

@EndFunction
*/
void gridInqYlongname(int gridID, char *ylongname)
{
959
  (void)cdiGridInqKeyStr(gridID, CDI_KEY_YLONGNAME, CDI_MAX_NAME, ylongname);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
960
961
962
963
964
965
}

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

Deike Kleberg's avatar
Deike Kleberg committed
966
@Prototype void gridInqYunits(int gridID, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
967
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
968
969
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate} or @fref{vlistInqVarGrid}.
    @Item  units    Units of the Y-axis. The caller must allocate space for the
Deike Kleberg's avatar
Deike Kleberg committed
970
                    returned string. The maximum possible length, in characters, of
971
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
972
973

@Description
974
The function @func{gridInqYunits} returns the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
975
976

@Result
977
@func{gridInqYunits} returns the units of the Y-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
978
979
980
981
982

@EndFunction
*/
void gridInqYunits(int gridID, char *yunits)
{
983
  (void)cdiGridInqKeyStr(gridID, CDI_KEY_YUNITS, CDI_MAX_NAME, yunits);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
984
985
986
987
}

void gridInqYstdname(int gridID, char *ystdname)
{
988
  grid_t *gridptr = gridID2Ptr(gridID);
989
990
  if ( gridptr->y.stdname )
    strcpy(ystdname, gridptr->y.stdname);
991
992
  else
    ystdname[0] = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
993
994
995
996
997
998
999
1000
}

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

@Prototype int gridInqType(int gridID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1001
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate} or @fref{vlistInqVarGrid}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1002
1003

@Description
1004
The function @func{gridInqType} returns the type of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1005
1006

@Result
1007
@func{gridInqType} returns the type of the grid,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1008
one of the set of predefined CDI grid types.
1009
The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1010
@func{GRID_LONLAT}, @func{GRID_LCC}, @func{GRID_SPECTRAL}, @func{GRID_GME},
1011
@func{GRID_CURVILINEAR} and @func{GRID_UNSTRUCTURED}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1012
1013
1014
1015
1016

@EndFunction
*/
int gridInqType(int gridID)
{
1017
  grid_t *gridptr = gridID2Ptr(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1018

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1019
  return gridptr->type;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
}


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

@Prototype int gridInqSize(int gridID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1029
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate} or @fref{vlistInqVarGrid}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed