grid.c 137 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
  gridptr->self         = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
95
  gridptr->type         = CDI_UNDEFID;
Deike Kleberg's avatar
Deike Kleberg committed
96
  gridptr->proj         = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
  gridptr->mask         = NULL;
98
  gridptr->mask_gme     = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
99
100
101
102
103
104
105
  gridptr->xvals        = NULL;
  gridptr->yvals        = NULL;
  gridptr->area         = NULL;
  gridptr->xbounds      = NULL;
  gridptr->ybounds      = NULL;
  gridptr->rowlon       = NULL;
  gridptr->nrowlon      = 0;
106
107
  gridptr->xfirst       = 0.0;
  gridptr->xlast        = 0.0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
108
  gridptr->xinc         = 0.0;
109
110
  gridptr->yfirst       = 0.0;
  gridptr->ylast        = 0.0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
111
  gridptr->yinc         = 0.0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
112
113
114
115
116
117
118
  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;
119
120
  gridptr->lcc_projflag = 0;
  gridptr->lcc_scanflag = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
121
  gridptr->lcc_defined  = FALSE;
122
123
124
125
126
127
  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;
128
129
130
131
  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
132
133
134
135
136
137
  gridptr->trunc        = 0;
  gridptr->nvertex      = 0;
  gridptr->nd           = 0;
  gridptr->ni           = 0;
  gridptr->ni2          = 0;
  gridptr->ni3          = 0;
138
139
140
  gridptr->number       = 0;
  gridptr->position     = 0;
  gridptr->reference    = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
141
142
143
144
  gridptr->prec         = 0;
  gridptr->size         = 0;
  gridptr->xsize        = 0;
  gridptr->ysize        = 0;
145
  gridptr->np           = 0;
146
147
  gridptr->xdef         = 0;
  gridptr->ydef         = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
148
  gridptr->isCyclic     = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
149
150
151
152
  gridptr->isRotated    = FALSE;
  gridptr->xpole        = 0.0;
  gridptr->ypole        = 0.0;
  gridptr->angle        = 0.0;
153
  gridptr->lcomplex     = 0;
154
  gridptr->hasdims      = TRUE;
155
156
  gridptr->xdimname[0]  = 0;
  gridptr->ydimname[0]  = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
157
158
159
160
161
162
  gridptr->xname[0]     = 0;
  gridptr->yname[0]     = 0;
  gridptr->xlongname[0] = 0;
  gridptr->ylongname[0] = 0;
  gridptr->xunits[0]    = 0;
  gridptr->yunits[0]    = 0;
163
164
  gridptr->xstdname  = NULL;
  gridptr->ystdname  = NULL;
Thomas Jahns's avatar
Thomas Jahns committed
165
  memset(gridptr->uuid, 0, CDI_UUID_SIZE);
Deike Kleberg's avatar
Deike Kleberg committed
166
  gridptr->name         = NULL;
167
  gridptr->vtable       = &cdiGridVtable;
168
  gridptr->extraData    = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
169
170
}

171

172
173
static void
grid_free_components(grid_t *gridptr)
Deike Kleberg's avatar
Deike Kleberg committed
174
{
175
176
177
178
179
180
181
182
  void *p2free[] = { gridptr->mask, gridptr->mask_gme,
                   gridptr->xvals, gridptr->yvals,
                   gridptr->xbounds, gridptr->ybounds,
                   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
183

184
185
186
void grid_free(grid_t *gridptr)
{
  grid_free_components(gridptr);
Deike Kleberg's avatar
Deike Kleberg committed
187
188
189
  grid_init(gridptr);
}

190
191
static grid_t *
gridNewEntry(cdiResH resH)
192
{
193
  grid_t *gridptr = (grid_t*) Malloc(sizeof(grid_t));
194
195
196
197
198
199
200
201
  grid_init(gridptr);
  if (resH == CDI_UNDEFID)
    gridptr->self = reshPut(gridptr, &gridOps);
  else
    {
      gridptr->self = resH;
      reshReplace(resH, gridptr, &gridOps);
    }
202
  return gridptr;
203
204
}

205
static
206
void gridInit (void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
207
{
208
209
  static int gridInitialized = 0;
  char *env;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
210

211
  if ( gridInitialized ) return;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
212

Thomas Jahns's avatar
Thomas Jahns committed
213
  gridInitialized = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214

215
216
  env = getenv("GRID_DEBUG");
  if ( env ) GRID_Debug = atoi(env);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
217
218
}

Thomas Jahns's avatar
Thomas Jahns committed
219
220
static void
grid_copy_base_scalar_fields(grid_t *gridptrOrig, grid_t *gridptrDup)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
{
Thomas Jahns's avatar
Thomas Jahns committed
222
223
224
225
226
227
  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
228

Thomas Jahns's avatar
Thomas Jahns committed
229
230
231
232
233
234
235
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
236
237
}

238
unsigned cdiGridCount(void)
Thomas Jahns's avatar
Thomas Jahns committed
239
{
240
  return reshCountType(&gridOps);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
241
242
}

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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;
}

259
static inline void
260
gridSetName(char *gridstrname, const char *name)
261
{
262
263
  strncpy(gridstrname, name, CDI_MAX_NAME);
  gridstrname[CDI_MAX_NAME - 1] = 0;
264
265
}

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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 )
          {
284
285
            gridSetName(gridptr->xname, "tlon");
            gridSetName(gridptr->yname, "tlat");
286
287
288
          }
        else
          {
289
290
            gridSetName(gridptr->xname, "lon");
            gridSetName(gridptr->yname, "lat");
291
          }
292
293
        gridSetName(gridptr->xlongname, "longitude");
        gridSetName(gridptr->ylongname, "latitude");
294
295
296
297
298
299
300
301
302
303
304
305
306
307

        /*
        if ( gridtype == GRID_CURVILINEAR )
          {
            gridptr->xstdname = xystdname_tab[grid_xystdname_grid_latlon][0];
            gridptr->ystdname = xystdname_tab[grid_xystdname_grid_latlon][1];
            gridDefXunits(gridID, "degrees");
            gridDefYunits(gridID, "degrees");
          }
        else
        */
          {
            gridptr->xstdname = xystdname_tab[grid_xystdname_latlon][0];
            gridptr->ystdname = xystdname_tab[grid_xystdname_latlon][1];
308
309
            gridSetName(gridptr->xunits, "degrees_east");
            gridSetName(gridptr->yunits, "degrees_north");
310
311
312
313
314
315
316
317
318
          }

        break;
      }
    case GRID_UNSTRUCTURED:
      gridptr->xsize = size;
      /* Fall through */
    case GRID_GME:
      {
319
320
        gridSetName(gridptr->xname, "lon");
        gridSetName(gridptr->yname, "lat");
321
322
        gridptr->xstdname = xystdname_tab[grid_xystdname_latlon][0];
        gridptr->ystdname = xystdname_tab[grid_xystdname_latlon][1];
323
324
        gridSetName(gridptr->xunits, "degrees_east");
        gridSetName(gridptr->yunits, "degrees_north");
325
326
327
328
329
330
        break;
      }
    case GRID_GENERIC:
      {

        /* gridptr->xsize = size; */
331
332
        gridSetName(gridptr->xname, "x");
        gridSetName(gridptr->yname, "y");
333
334
335
336
337
338
339
340
341
342
343
344
345
346
        /*
        strcpy(gridptr->xstdname, "grid_longitude");
        strcpy(gridptr->ystdname, "grid_latitude");
        gridptr->xstdname = xystdname_tab[grid_xystdname_grid_latlon][0];
        gridptr->ystdname = xystdname_tab[grid_xystdname_grid_latlon][1];
        gridDefXunits(gridID, "degrees");
        gridDefYunits(gridID, "degrees");
        */
        break;
      }
    case GRID_LCC2:
    case GRID_SINUSOIDAL:
    case GRID_LAEA:
      {
347
348
        gridSetName(gridptr->xname, "x");
        gridSetName(gridptr->yname, "y");
349
350
        gridptr->xstdname = xystdname_tab[grid_xystdname_projection][0];
        gridptr->ystdname = xystdname_tab[grid_xystdname_projection][1];
351
352
        gridSetName(gridptr->xunits, "m");
        gridSetName(gridptr->yunits, "m");
353
354
355
356
357
358
359
        break;
      }
    }

}


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

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

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

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

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

  if ( gridtype == GRID_GAUSSIAN || gridtype == GRID_GAUSSIAN_REDUCED )
    {
      if ( ysize > 2 )
Deike Kleberg's avatar
Deike Kleberg committed
409
410
411
412
413
414
	{
	  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 )
	      {
415
		double *restrict ytmp = NULL;
Deike Kleberg's avatar
Deike Kleberg committed
416
		int nstart, lfound = 0;
417
		int ny = (int) (180./(fabs(ylast-yfirst)/(ysize-1)) + 0.5);
Deike Kleberg's avatar
Deike Kleberg committed
418
419
420
		ny -= ny%2;
		if ( ny > ysize && ny < 4096 )
		  {
421
		    ytmp = (double *) Malloc((size_t)ny * sizeof (double));
Deike Kleberg's avatar
Deike Kleberg committed
422
		    calc_gaussgrid(ytmp, ny, yfirst, ylast);
423
424
425
426
427
428
                    {
                      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
429

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

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

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

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

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

/*
493
@Function  gridCreate
494
@Title     Create a horizontal Grid
Uwe Schulzweida's avatar
Uwe Schulzweida committed
495

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

@Description
505
The function @func{gridCreate} creates a horizontal Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
506
507

@Result
508
@func{gridCreate} returns an identifier to the Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
509
510

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

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

536
  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
537

538
  gridInit();
539

Uwe Schulzweida's avatar
Uwe Schulzweida committed
540
  grid_t *gridptr = gridNewEntry(CDI_UNDEFID);
541
  if ( ! gridptr ) Error("No memory");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
542

Uwe Schulzweida's avatar
Uwe Schulzweida committed
543
  int gridID = gridptr->self;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
544

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

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

  return (gridID);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
552
553
static
void gridDestroyKernel( grid_t * gridptr )
554
555
556
{
  int id;

Deike Kleberg's avatar
Deike Kleberg committed
557
  xassert ( gridptr );
558
559
560

  id = gridptr->self;

561
  grid_free_components(gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
562
  Free( gridptr );
563

564
  reshRemove ( id, &gridOps );
565
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
566

567
568
569
570
571
572
/*
@Function  gridDestroy
@Title     Destroy a horizontal Grid

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

@EndFunction
*/
void gridDestroy(int gridID)
{
579
  grid_t *gridptr = gridID2Ptr(gridID);
580
  gridptr->vtable->destroy(gridptr);
581
582
}

583
void gridDestroyP ( void * gridptr )
584
{
585
  ((grid_t *)gridptr)->vtable->destroy((grid_t *)gridptr);
586
587
588
}


589
const char *gridNamePtr(int gridtype)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
590
{
591
  int size = (int) (sizeof(Grids)/sizeof(Grids[0]));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
592

593
  const char *name = gridtype >= 0 && gridtype < size ? Grids[gridtype] : Grids[GRID_GENERIC];
Uwe Schulzweida's avatar
Uwe Schulzweida committed
594
595
596
597
598
599
600
601
602
603

  return (name);
}


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

604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
@Function  cdiGridDefString
@Title     Define a CDI grid string value from a key

@Prototype int cdiGridDefString(int gridID, int key, int size, const char *mesg)
@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
The function @func{cdiGridDefString} defines a CDI grid string value from a key.

@Result
@func{cdiGridDefString} returns 0 if OK and integer value on error.

@EndFunction
*/
int cdiGridDefString(int gridID, int key, int size, const char *mesg)
{
  if ( size == 0 || mesg == NULL || *mesg == 0 ) return -1;

  grid_t *gridptr = gridID2Ptr(gridID);
  char *gridstring = NULL;

  switch (key)
    {
    case CDI_GRID_XDIMNAME: gridstring = gridptr->xdimname; break;
    default:
      Warning("CDI grid string key %d not supported!", key);
      return -1;
    }

  gridSetString(gridstring, mesg, size);
  gridMark4Update(gridID);

  return 0;
}

/*
@Function  cdiGridInqString
@Title     Get a CDI grid string value from a key

@Prototype int cdiGridInqString(int gridID, int key, int size, char *mesg)
@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
The function @func{cdiGridInqString} return a CDI grid string value from a key.

@Result
@func{cdiGridInqString} returns 0 if OK and integer value on error.

@EndFunction
*/
int cdiGridInqString(int gridID, int key, int size, char *mesg)
{
  if ( size == 0 || mesg == NULL || *mesg == 0 ) return -1;

  grid_t *gridptr = gridID2Ptr(gridID);
  const char *gridstring = NULL;

  switch (key)
    {
    case CDI_GRID_XDIMNAME: gridstring = gridptr->xdimname; break;
    default:
      Warning("CDI grid string key %d not supported!", key);
      return -1;
    }

  gridGetString(mesg, gridstring, size);

  return 0;
}

686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
@Function  gridDefXdimname
@Title     Define the dimension name of a X-axis

@Prototype void gridDefXdimname(int gridID, const char *dimname)
@Parameter
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
    @Item  dimname  Dimension name of the X-axis.

@Description
The function @func{gridDefXdimname} defines the dimension name of a X-axis.

@EndFunction
*/
void gridDefXdimname(int gridID, const char *xdimname)
{
  if ( xdimname && *xdimname )
    {
      grid_t *gridptr = gridID2Ptr(gridID);
      gridSetName(gridptr->xdimname, xdimname);
      gridMark4Update(gridID);
    }
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
710
711
712
713
714
715
/*
@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
716
717
    @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
718
719

@Description
720
The function @func{gridDefXname} defines the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
721
722
723

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724
void gridDefXname(int gridID, const char *xname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
725
{
726
  if ( xname && *xname )
727
    {
728
      grid_t *gridptr = gridID2Ptr(gridID);
729
      gridSetName(gridptr->xname, xname);
730
      gridMark4Update(gridID);
731
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
732
733
734
735
736
737
738
739
}

/*
@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
740
741
    @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
742
743

@Description
744
The function @func{gridDefXlongname} defines the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
745
746
747

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
748
void gridDefXlongname(int gridID, const char *xlongname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
749
{
750
751
  if ( xlongname )
    {
752
      grid_t *gridptr = gridID2Ptr(gridID);
753
      gridSetName(gridptr->xlongname, xlongname);
754
      gridMark4Update(gridID);
755
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
757
758
759
760
761
762
763
}

/*
@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
764
765
    @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
766
767

@Description
768
The function @func{gridDefXunits} defines the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
769
770
771

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
772
void gridDefXunits(int gridID, const char *xunits)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
773
{
774
775
  if ( xunits )
    {
776
      grid_t *gridptr = gridID2Ptr(gridID);
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
      gridSetName(gridptr->xunits, xunits);
      gridMark4Update(gridID);
    }
}

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

@Prototype void gridDefYdimname(int gridID, const char *dimname)
@Parameter
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
    @Item  dimname  Dimension name of the Y-axis.

@Description
The function @func{gridDefYdimname} defines the dimension name of a Y-axis.

@EndFunction
*/
void gridDefYdimname(int gridID, const char *ydimname)
{
  if ( ydimname && *ydimname )
    {
      grid_t *gridptr = gridID2Ptr(gridID);
      gridSetName(gridptr->ydimname, ydimname);
802
      gridMark4Update(gridID);
803
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
804
805
806
807
808
809
810
811
}

/*
@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
812
813
    @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
814
815

@Description
816
The function @func{gridDefYname} defines the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
817
818
819

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
820
void gridDefYname(int gridID, const char *yname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
821
{
822
  if ( yname && *yname )
823
    {
824
      grid_t *gridptr = gridID2Ptr(gridID);
825
      gridSetName(gridptr->yname, yname);
826
      gridMark4Update(gridID);
827
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
828
829
830
831
832
833
834
835
}

/*
@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
836
837
    @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
838
839

@Description
840
The function @func{gridDefYlongname} defines the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
841
842
843

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
844
void gridDefYlongname(int gridID, const char *ylongname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
845
{
846
847
  if ( ylongname )
    {
848
      grid_t *gridptr = gridID2Ptr(gridID);
849
      gridSetName(gridptr->ylongname, ylongname);
850
      gridMark4Update(gridID);
851
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
852
853
854
855
856
857
858
859
}

/*
@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
860
861
    @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
862
863

@Description
864
The function @func{gridDefYunits} defines the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
865
866
867

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
868
void gridDefYunits(int gridID, const char *yunits)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
869
{
870
871
  if ( yunits )
    {
872
      grid_t *gridptr = gridID2Ptr(gridID);
873
      gridSetName(gridptr->yunits, yunits);
874
      gridMark4Update(gridID);
875
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
876
877
}

878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/*
@Function  gridInqXdimname
@Title     Get the dimension name of a X-axis

@Prototype void gridInqXdimname(int gridID, char *dimname)
@Parameter
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate} or @fref{vlistInqVarGrid}.
    @Item  dimname  Dimension name of the X-axis. 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
The function @func{gridInqXdimname} returns the dimension name of a X-axis.

@Result
@func{gridInqXname} returns the dimension name of the X-axis to the parameter dimname.

@EndFunction
*/
void gridInqXdimname(int gridID, char *xdimname)
{
  grid_t *gridptr = gridID2Ptr(gridID);

  strcpy(xdimname, gridptr->xdimname);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
904
905
906
907
/*
@Function  gridInqXname
@Title     Get the name of a X-axis

Deike Kleberg's avatar
Deike Kleberg committed
908
@Prototype void gridInqXname(int gridID, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
909
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
910
911
    @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
912
                    returned string. The maximum possible length, in characters, of
913
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
914
915

@Description
916
The function @func{gridInqXname} returns the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
917
918

@Result
919
@func{gridInqXname} returns the name of the X-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
920
921
922
923
924

@EndFunction
*/
void gridInqXname(int gridID, char *xname)
{
925
  grid_t *gridptr = gridID2Ptr(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
926
927
928
929
930
931
932
933

  strcpy(xname, gridptr->xname);
}

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

Deike Kleberg's avatar
Deike Kleberg committed
934
@Prototype void gridInqXlongname(int gridID, char *longname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
935
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
936
937
    @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
938
                    returned string. The maximum possible length, in characters, of
939
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
940
941

@Description
942
The function @func{gridInqXlongname} returns the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
943
944

@Result
945
@func{gridInqXlongname} returns the longname of the X-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
946
947
948
949
950

@EndFunction
*/
void gridInqXlongname(int gridID, char *xlongname)
{
951
  grid_t *gridptr = gridID2Ptr(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
952
953
954
955
956
957
958
959

  strcpy(xlongname, gridptr->xlongname);
}

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

Deike Kleberg's avatar
Deike Kleberg committed
960
@Prototype void gridInqXunits(int gridID, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
961
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
962
963
    @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
964
                    returned string. The maximum possible length, in characters, of
965
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
966
967

@Description
968
The function @func{gridInqXunits} returns the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
969
970

@Result
971
@func{gridInqXunits} returns the units of the X-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
972
973
974
975
976

@EndFunction
*/
void gridInqXunits(int gridID, char *xunits)
{
977
  grid_t *gridptr = gridID2Ptr(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
978
979
980
981
982
983
984

  strcpy(xunits, gridptr->xunits);
}


void gridInqXstdname(int gridID, char *xstdname)
{
985
  grid_t *gridptr = gridID2Ptr(gridID);
986
987
988
989
  if ( gridptr->xstdname )
    strcpy(xstdname, gridptr->xstdname);
  else
    xstdname[0] = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
990
991
}

992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
/*
@Function  gridInqYdimname
@Title     Get the dimension name of a Y-axis

@Prototype void gridInqYdimname(int gridID, char *dimname)
@Parameter
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate} or @fref{vlistInqVarGrid}.
    @Item  dimname  Dimension name of the Y-axis. 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
The function @func{gridInqYdimname} returns the dimension name of a Y-axis.

@Result
@func{gridInqYname} returns the dimension name of the Y-axis to the parameter dimname.

@EndFunction
*/
void gridInqYdimname(int gridID, char *ydimname)
{
  grid_t *gridptr = gridID2Ptr(gridID);

  strcpy(ydimname, gridptr->ydimname);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1018
1019
1020
1021
/*
@Function  gridInqYname
@Title     Get the name of a Y-axis

Deike Kleberg's avatar
Deike Kleberg committed
1022
@Prototype void gridInqYname(int gridID, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1023
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1024
1025
    @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
1026
                    returned string. The maximum possible length, in characters, of
1027
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1028
1029

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

@Result
1033
@func{gridInqYname} returns the name of the Y-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1034
1035
1036
1037
1038

@EndFunction
*/
void gridInqYname(int gridID, char *yname)
{
1039
  grid_t *gridptr = gridID2Ptr(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1040
1041
1042
1043
1044
1045
1046
1047

  strcpy(yname, gridptr->yname);
}

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

Deike Kleberg's avatar
Deike Kleberg committed
1048
@Prototype void gridInqXlongname(int gridID, char *longname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1049
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1050
1051
    @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
1052
                    returned string. The maximum possible length, in characters, of
1053
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1054
1055

@Description
1056
The function @func{gridInqYlongname} returns the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1057
1058

@Result
1059
@func{gridInqYlongname} returns the longname of the Y-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1060
1061
1062
1063
1064

@EndFunction
*/
void gridInqYlongname(int gridID, char *ylongname)
{
1065
  grid_t *gridptr = gridID2Ptr(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1066
1067
1068
1069
1070
1071
1072
1073

  strcpy(ylongname, gridptr->ylongname);
}

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

Deike Kleberg's avatar
Deike Kleberg committed
1074
@Prototype void gridInqYunits(int gridID, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1075
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1076
1077
    @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
1078
                    returned string. The maximum possible length, in characters, of
1079
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1080
1081

@Description
1082
The function @func{gridInqYunits} returns the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1083
1084

@Result
1085
@func{gridInqYunits} returns the units of the Y-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1086
1087
1088
1089
1090

@EndFunction
*/
void gridInqYunits(int gridID, char *yunits)
{
1091
  grid_t *gridptr = gridID2Ptr(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1092
1093
1094
1095
1096
1097

  strcpy(yunits, gridptr->yunits);
}

void gridInqYstdname(int gridID, char *ystdname)
{
1098
  grid_t *gridptr = gridID2Ptr(gridID);
1099
1100
1101
1102
  if ( gridptr->ystdname )
    strcpy(ystdname, gridptr->ystdname);
  else
    ystdname[0] = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1103
1104
1105
1106
1107
1108
1109
1110
}

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

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

@Description
1114
The function @func{gridInqType} returns the type of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1115
1116

@Result
1117
@func{gridInqType} returns the type of the grid,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1118
one of the set of predefined CDI grid types.
1119
The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1120
@func{GRID_LONLAT}, @func{GRID_LCC}, @func{GRID_SPECTRAL}, @func{GRID_GME},
1121
@func{GRID_CURVILINEAR} and @func{GRID_UNSTRUCTURED}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1122
1123
1124
1125
1126

@EndFunction
*/
int gridInqType(int gridID)
{
1127
  grid_t *gridptr = gridID2Ptr(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138

  return (gridptr->type);
}


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

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

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

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

@EndFunction
*/
int gridInqSize(int gridID)
{
1151
  grid_t *gridptr = gridID2Ptr(gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1152

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1153
  int size = gridptr->size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1154
1155
1156
1157
1158
1159
1160
1161
1162

  if ( ! size )
    {
      int xsize, ysize;

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

      if ( ysize )
1163
        size = xsize * ysize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1164
      else
Thomas Jahns's avatar
Thomas Jahns committed
1165
        size = xsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1166

Thomas Jahns's avatar
Thomas Jahns committed
1167
      gridptr->size = size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1168
1169
1170
1171
1172
    }

  return (size);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1173
1174
static
int nsp2trunc(int nsp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1175