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

#include <string.h>
6
#include <float.h>  /* FLT_EPSILON */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
7

8
9
10
11
#ifdef USE_MPI
#include <mpi.h>
#endif

Uwe Schulzweida's avatar
Uwe Schulzweida committed
12
13
#include "dmemory.h"
#include "cdi.h"
14
#include "cdi_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
15
#include "grid.h"
16
#include "gaussgrid.h"
17
18
19
20
#include "pio_util.h"
#include "resource_handle.h"
#include "pio_rpc.h"
#include "namespace.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
21
22
23
24
25
26
27
28
29
30
31

#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[] = {
32
33
34
35
36
37
38
39
40
  /*  0 */  "undefined",
  /*  1 */  "generic",
  /*  2 */  "gaussian",
  /*  3 */  "gaussian reduced",
  /*  4 */  "lonlat",
  /*  5 */  "spectral",
  /*  6 */  "fourier",
  /*  7 */  "gme",
  /*  8 */  "trajectory",
41
  /*  9 */  "unstructured",
42
43
  /* 10 */  "curvilinear",
  /* 11 */  "lcc",
44
45
46
  /* 12 */  "lcc2",
  /* 13 */  "laea",
  /* 14 */  "sinusoidal",
47
  /* 15 */  "reference",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
48
  /* 16 */  "projection",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
49
50
51
};


52
53
54
55
56
57
58
59
static int    gridCompareP    ( void * gridptr1, void * gridptr2 );
static void   gridDestroyP    ( void * gridptr );
static void   gridPrintP      ( void * gridptr, FILE * fp );
#ifdef USE_MPI
static int    gridGetPackSize ( void * gridptr, MPI_Comm comm );
static void   gridPack        ( void * gridptr, void * buff, int size,
				int *position, MPI_Comm comm );
static int    gridTxCode      ( void );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60
61
#endif

62
63
64
65
66
resOps gridOps = { gridCompareP, gridDestroyP, gridPrintP
#ifdef USE_MPI
                   , gridGetPackSize, gridPack, gridTxCode
#endif
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
67
68


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


Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
73
74
void grid_init(grid_t *gridptr)
{
  gridptr->self         = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
75
  gridptr->type         = CDI_UNDEFID;
76
  gridptr->proj         = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
77
  gridptr->mask         = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
78
  gridptr->mask_gme     = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
79
80
81
82
83
84
85
86
87
  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
88
89
90
91
92
93
94
  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;
95
96
  gridptr->lcc_projflag = 0;
  gridptr->lcc_scanflag = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
97
  gridptr->lcc_defined  = FALSE;
98
99
100
101
102
103
  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;
104
105
106
107
  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
108
109
110
111
112
113
  gridptr->trunc        = 0;
  gridptr->nvertex      = 0;
  gridptr->nd           = 0;
  gridptr->ni           = 0;
  gridptr->ni2          = 0;
  gridptr->ni3          = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
114
115
  gridptr->number       = 0;
  gridptr->position     = 0;
116
  gridptr->reference    = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
118
119
120
  gridptr->prec         = 0;
  gridptr->size         = 0;
  gridptr->xsize        = 0;
  gridptr->ysize        = 0;
121
  gridptr->np           = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
122
123
  gridptr->xdef         = 0;
  gridptr->ydef         = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
  gridptr->isCyclic     = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125
126
127
128
129
  gridptr->isRotated    = FALSE;
  gridptr->xpole        = 0.0;
  gridptr->ypole        = 0.0;
  gridptr->angle        = 0.0;
  gridptr->locked       = FALSE;
130
  gridptr->lcomplex     = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
131
132
133
134
135
136
137
138
  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;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
139
  gridptr->uuid[0]      = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  gridptr->name         = NULL;
}


void grid_free(grid_t *gridptr)
{
  if ( gridptr->mask      ) free(gridptr->mask);
  if ( gridptr->mask_gme  ) free(gridptr->mask_gme);
  if ( gridptr->xvals     ) free(gridptr->xvals);
  if ( gridptr->yvals     ) free(gridptr->yvals);
  if ( gridptr->area      ) free(gridptr->area);
  if ( gridptr->xbounds   ) free(gridptr->xbounds);
  if ( gridptr->ybounds   ) free(gridptr->ybounds);
  if ( gridptr->rowlon    ) free(gridptr->rowlon);
  if ( gridptr->reference ) free(gridptr->reference);
  if ( gridptr->name      ) free(gridptr->name);

  grid_init(gridptr);
}

static
161
grid_t *gridNewEntry ( void )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162
{
163
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
164

165
166
167
  gridptr = ( grid_t *) xmalloc ( sizeof ( grid_t ));
  grid_init ( gridptr );
  gridptr->self = reshPut (( void * ) gridptr, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168

169
  return gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170
171
}

172
static
173
void gridInit (void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174
{
175
  static int gridInitialized = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
176
177
  char *env;

178
179
180
  if ( gridInitialized ) return;

  gridInitialized = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
181
182
183
184
185

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

186
187
static
void grid_copy(grid_t *gridptr2, grid_t *gridptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
188
189
190
191
{
  int gridID2;

  gridID2 = gridptr2->self;
192
  memcpy(gridptr2, gridptr1, sizeof(grid_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
193
194
195
  gridptr2->self = gridID2;
}

196
static
197
void gridCheckPtr(const char *caller, int gridID, grid_t *gridptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
198
199
{
  if ( gridptr == NULL )
200
    Errorc("grid %d undefined!", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
201
202
}

203
#define  grid_check_ptr(gridID, gridptr)  gridCheckPtr(__func__, gridID, gridptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204

205

Uwe Schulzweida's avatar
Uwe Schulzweida committed
206
207
int gridSize(void)
{
208
  return reshCountType ( &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
209
210
211
212
213
}


void gridGenXvals(int xsize, double xfirst, double xlast, double xinc, double *xvals)
{
214
  if ( (! (fabs(xinc) > 0)) && xsize > 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
215
    {
216
      if ( xfirst >= xlast )
217
        {
218
219
          while ( xfirst >= xlast ) xlast += 360;
          xinc = (xlast-xfirst)/(xsize);
220
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
      else
222
        {
223
          xinc = (xlast-xfirst)/(xsize-1);
224
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
225
226
    }

227
  for ( int i = 0; i < xsize; ++i )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
228
229
230
    xvals[i] = xfirst + i*xinc;
}

231
232
static
void calc_gaussgrid(double *yvals, int ysize, double yfirst, double ylast)
233
234
{
  double *yw;
235
236
  long yhsize;
  long i;
237
238
239
240
241
242
243
244
245
246
247
248

  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++ )
249
250
251
252
253
        {
          ytmp = yvals[i];
          yvals[i] = yvals[ysize-i-1];
          yvals[ysize-i-1] = ytmp;
        }
254
255
256
257
    }
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
258
259
void gridGenYvals(int gridtype, int ysize, double yfirst, double ylast, double yinc, double *yvals)
{
260
  long i;
261
  double deleps = 0.002;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
262
263
264
265
266

  if ( gridtype == GRID_GAUSSIAN || gridtype == GRID_GAUSSIAN_REDUCED )
    {
      if ( ysize > 2 )
	{
267
	  calc_gaussgrid(yvals, ysize, yfirst, ylast);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
268

269
	  if ( ! (IS_EQUAL(yfirst, 0) && IS_EQUAL(ylast, 0)) )
270
	    if ( fabs(yvals[0] - yfirst) > deleps || fabs(yvals[ysize-1] - ylast) > deleps )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
271
	      {
272
		double yinc = fabs(ylast-yfirst)/(ysize-1);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
273
		double *ytmp = NULL;
274
275
276
277
		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); */
278
		if ( ny > ysize && ny < 4096 )
279
		  {
280
		    ytmp = (double *) malloc(ny*sizeof(double));
281
282
		    calc_gaussgrid(ytmp, ny, yfirst, ylast);
		    for ( i = 0; i < (ny-ysize); i++ )
283
		      if ( fabs(ytmp[i] - yfirst) < deleps ) break;
284

285
		    nstart = i;
286

287
		    if ( (nstart+ysize-1) < ny )
288
		      if ( fabs(ytmp[nstart+ysize-1] - ylast) < deleps ) lfound = 1;
289
		  }
290
291
292
293
294
295
296

		if ( lfound )
		  {
		    for ( i = 0; i < ysize; i++ ) yvals[i] = ytmp[i+nstart];
		  }
		else
		  {
297
		    Warning("Cannot calculate gaussian latitudes for lat1 = %g latn = %g!", yfirst, ylast);
298
299
300
301
302
		    for ( i = 0; i < ysize; i++ ) yvals[i] = 0;
		    yvals[0] = yfirst;
		    yvals[ysize-1] = ylast;
		  }

303
		if ( ytmp ) free(ytmp);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
304
305
306
	      }
	}
      else
307
308
309
310
        {
          yvals[0] = yfirst;
          yvals[ysize-1] = ylast;
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
311
312
    }
  /*     else if ( gridtype == GRID_LONLAT || gridtype == GRID_GENERIC ) */
313
  else
Uwe Schulzweida's avatar
Uwe Schulzweida committed
314
    {
315
      if ( (! (fabs(yinc) > 0)) && ysize > 1 )
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
        {
          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
337
338
339
340

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

      for ( i = 0; i < ysize; i++ )
341
        yvals[i] = yfirst + i*yinc;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
342
343
344
    }
  /*
    else
345
    Error("unable to calculate values for %s grid!", gridNamePtr(gridtype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
346
347
348
349
  */
}

/*
350
@Function  gridCreate
351
@Title     Create a horizontal Grid
Uwe Schulzweida's avatar
Uwe Schulzweida committed
352

353
@Prototype int gridCreate(int gridtype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
354
355
@Parameter
    @Item  gridtype  The type of the grid, one of the set of predefined CDI grid types.
356
                     The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
                     @func{GRID_LONLAT}, @func{GRID_LCC}, @func{GRID_SPECTRAL},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
358
                     @func{GRID_GME}, @func{GRID_CURVILINEAR}, @func{GRID_UNSTRUCTURED} and
Uwe Schulzweida's avatar
Uwe Schulzweida committed
359
                     @func{GRID_REFERENCE}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360
361
362
    @Item  size      Number of gridpoints.

@Description
363
The function @func{gridCreate} creates a horizontal Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
364
365

@Result
366
@func{gridCreate} returns an identifier to the Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
367
368

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

@Source
Uwe Schulzweida's avatar
Uwe Schulzweida committed
372
373
#include "cdi.h"
   ...
Uwe Schulzweida's avatar
Uwe Schulzweida committed
374
375
#define  nlon  12
#define  nlat   6
Uwe Schulzweida's avatar
Uwe Schulzweida committed
376
   ...
Uwe Schulzweida's avatar
Uwe Schulzweida committed
377
378
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
379
380
int gridID;
   ...
Uwe Schulzweida's avatar
Uwe Schulzweida committed
381
382
383
gridID = gridCreate(GRID_LONLAT, nlon*nlat);
gridDefXsize(gridID, nlon);
gridDefYsize(gridID, nlat);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
384
385
386
387
gridDefXvals(gridID, lons);
gridDefYvals(gridID, lats);
   ...
@EndSource
Uwe Schulzweida's avatar
Uwe Schulzweida committed
388
389
@EndFunction
*/
390
int gridCreate(int gridtype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
391
392
{
  int gridID;
393
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
394
395

  if ( CDI_Debug )
396
    Message("gridtype: %d size: %d", gridtype, size);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
397

398
  gridInit ();
Uwe Schulzweida's avatar
Uwe Schulzweida committed
399

400
  gridptr = gridNewEntry();
401
  if ( ! gridptr ) Error("No memory");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
402
403
404

  gridID = gridptr->self;

405
  if ( CDI_Debug ) Message("gridID: %d", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
407
408
409
410

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

  /*  if ( gridtype == GRID_GENERIC )     gridptr->xsize = size; */
Uwe Schulzweida's avatar
Uwe Schulzweida committed
411
412
  if ( gridtype == GRID_UNSTRUCTURED )  gridptr->xsize = size;
  if ( gridtype == GRID_CURVILINEAR  )  gridptr->nvertex = 4;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
413
414
415
416
417
418
419
420
421

  switch (gridtype)
    {
    case GRID_LONLAT:
    case GRID_GAUSSIAN:
    case GRID_GAUSSIAN_REDUCED:
    case GRID_CURVILINEAR:
    case GRID_TRAJECTORY:
      {
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
        if ( gridtype == GRID_TRAJECTORY )
          {
            gridDefXname(gridID, "tlon");
            gridDefYname(gridID, "tlat");
          }
        else
          {
            gridDefXname(gridID, "lon");
            gridDefYname(gridID, "lat");
          }
        gridDefXlongname(gridID, "longitude");
        gridDefYlongname(gridID, "latitude");

        /*
        if ( gridtype == GRID_CURVILINEAR )
          {
            strcpy(gridptr->xstdname, "grid_longitude");
            strcpy(gridptr->ystdname, "grid_latitude");
            gridDefXunits(gridID, "degrees");
            gridDefYunits(gridID, "degrees");
          }
        else
        */
          {
            strcpy(gridptr->xstdname, "longitude");
            strcpy(gridptr->ystdname, "latitude");
            gridDefXunits(gridID, "degrees_east");
            gridDefYunits(gridID, "degrees_north");
          }

        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
453
454
      }
    case GRID_GME:
455
    case GRID_UNSTRUCTURED:
456
      {
457
458
459
460
461
462
463
        gridDefXname(gridID, "lon");
        gridDefYname(gridID, "lat");
        strcpy(gridptr->xstdname, "longitude");
        strcpy(gridptr->ystdname, "latitude");
        gridDefXunits(gridID, "degrees_east");
        gridDefYunits(gridID, "degrees_north");
        break;
464
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
465
466
    case GRID_GENERIC:
      {
467
468
469
470
471
472
473
        gridDefXname(gridID, "x");
        gridDefYname(gridID, "y");
        strcpy(gridptr->xstdname, "grid_longitude");
        strcpy(gridptr->ystdname, "grid_latitude");
        gridDefXunits(gridID, "degrees");
        gridDefYunits(gridID, "degrees");
        break;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
474
      }
475
    case GRID_LCC2:
476
    case GRID_SINUSOIDAL:
477
    case GRID_LAEA:
478
      {
479
480
481
482
483
484
485
        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;
486
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
487
488
489
490
491
    }

  return (gridID);
}

492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
static
void gridDestroyKernel( grid_t * gridptr )
{
  int id;

  xassert ( gridptr );

  id = gridptr->self;

  if ( gridptr->mask      ) free(gridptr->mask);
  if ( gridptr->mask_gme  ) free(gridptr->mask_gme);
  if ( gridptr->xvals     ) free(gridptr->xvals);
  if ( gridptr->yvals     ) free(gridptr->yvals);
  if ( gridptr->area      ) free(gridptr->area);
  if ( gridptr->xbounds   ) free(gridptr->xbounds);
  if ( gridptr->ybounds   ) free(gridptr->ybounds);
  if ( gridptr->rowlon    ) free(gridptr->rowlon);
  if ( gridptr->reference ) free(gridptr->reference);

  free ( gridptr );

  reshRemove ( id, &gridOps );
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
515

516
517
518
519
520
521
/*
@Function  gridDestroy
@Title     Destroy a horizontal Grid

@Prototype void gridDestroy(int gridID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
522
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
523
524
525
526
527

@EndFunction
*/
void gridDestroy(int gridID)
{
528
529
  grid_t *gridptr;

530
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
531

532
533
  gridDestroyKernel ( gridptr );
}
534

535
536
537
void gridDestroyP ( void * gridptr )
{
  gridDestroyKernel (( grid_t * ) gridptr );
538
539
540
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
541
542
543
char *gridNamePtr(int gridtype)
{
  char *name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
544
  int size = (int) (sizeof(Grids)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565

  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
Uwe Schulzweida's avatar
Uwe Schulzweida committed
566
567
    @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
568
569

@Description
570
The function @func{gridDefXname} defines the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
571
572
573

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
574
void gridDefXname(int gridID, const char *xname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
575
{
576
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
577

578
579
580
581
582
583
584
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
      xwarning ("%s", "Operation not executed." );
      return;
    }

  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
585

586
587
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
588
589
590
591
592
593
594
595
596
597
  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
Uwe Schulzweida's avatar
Uwe Schulzweida committed
598
599
    @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
600
601

@Description
602
The function @func{gridDefXlongname} defines the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
603
604
605

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
606
void gridDefXlongname(int gridID, const char *xlongname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
607
{
608
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
609

610
611
612
613
614
615
616
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
      xwarning ("%s", "Operation not executed." );
      return;
    }

  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
617
618
619
620
621
622
623
624
625
626
627

  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
Uwe Schulzweida's avatar
Uwe Schulzweida committed
628
629
    @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
630
631

@Description
632
The function @func{gridDefXunits} defines the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
633
634
635

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
636
void gridDefXunits(int gridID, const char *xunits)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
637
{
638
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
639

640
641
642
643
644
645
646
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
      xwarning("%s", "Operation not executed." );
      return;
    }

  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
647

648
649
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
650
651
652
653
654
655
656
657
658
659
  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
Uwe Schulzweida's avatar
Uwe Schulzweida committed
660
661
    @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
662
663

@Description
664
The function @func{gridDefYname} defines the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
665
666
667

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
668
void gridDefYname(int gridID, const char *yname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
669
{
670
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
671

672
673
674
675
676
677
678
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
      xwarning("%s", "Operation not executed.");
      return;
    }

  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
679

680
681
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
682
683
684
685
686
687
688
689
690
691
  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
Uwe Schulzweida's avatar
Uwe Schulzweida committed
692
693
    @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
694
695

@Description
696
The function @func{gridDefYlongname} defines the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
697
698
699

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
700
void gridDefYlongname(int gridID, const char *ylongname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
701
{
702
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
703

704
705
706
707
708
709
710
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
      xwarning("%s", "Operation not executed.");
      return;
    }

  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
711

712
713
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
714
715
716
717
718
719
720
721
722
723
  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
Uwe Schulzweida's avatar
Uwe Schulzweida committed
724
725
    @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
726
727

@Description
728
The function @func{gridDefYunits} defines the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
729
730
731

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
732
void gridDefYunits(int gridID, const char *yunits)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
733
{
734
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
735

736
737
738
739
740
741
742
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
      xwarning("%s", "Operation not executed.");
      return;
    }

  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
743

744
745
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
746
747
748
749
750
751
752
753
  if ( yunits )
    strcpy(gridptr->yunits, yunits);
}

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
754
@Prototype void gridInqXname(int gridID, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
755
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
757
758
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
    @Item  name     Name of the X-axis. The caller must allocate space for the 
                    returned string. The maximum possible length, in characters, of
Uwe Schulzweida's avatar
Uwe Schulzweida committed
759
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
760
761

@Description
762
The function @func{gridInqXname} returns the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
763
764

@Result
765
@func{gridInqXname} returns the name of the X-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
766
767
768
769
770

@EndFunction
*/
void gridInqXname(int gridID, char *xname)
{
771
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
772

773
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
774

775
776
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
777
778
779
780
781
782
783
  strcpy(xname, gridptr->xname);
}

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
784
@Prototype void gridInqXlongname(int gridID, char *longname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
785
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
786
787
788
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
    @Item  longname Longname of the X-axis. The caller must allocate space for the 
                    returned string. The maximum possible length, in characters, of
Uwe Schulzweida's avatar
Uwe Schulzweida committed
789
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
790
791

@Description
792
The function @func{gridInqXlongname} returns the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
793
794

@Result
795
@func{gridInqXlongname} returns the longname of the X-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
796
797
798
799
800

@EndFunction
*/
void gridInqXlongname(int gridID, char *xlongname)
{
801
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
802

803
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
804

805
806
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
807
808
809
810
811
812
813
  strcpy(xlongname, gridptr->xlongname);
}

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
814
@Prototype void gridInqXunits(int gridID, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
815
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
816
817
818
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
    @Item  units    Units of the X-axis. The caller must allocate space for the 
                    returned string. The maximum possible length, in characters, of
Uwe Schulzweida's avatar
Uwe Schulzweida committed
819
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
820
821

@Description
822
The function @func{gridInqXunits} returns the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
823
824

@Result
825
@func{gridInqXunits} returns the units of the X-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
826
827
828
829
830

@EndFunction
*/
void gridInqXunits(int gridID, char *xunits)
{
831
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
832

833
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
834

835
836
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
837
838
839
840
841
842
  strcpy(xunits, gridptr->xunits);
}


void gridInqXstdname(int gridID, char *xstdname)
{
843
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
844

845
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
846

847
848
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
849
850
851
852
853
854
855
  strcpy(xstdname, gridptr->xstdname);
}

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
856
@Prototype void gridInqYname(int gridID, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
857
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
858
859
860
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
    @Item  name     Name of the Y-axis. The caller must allocate space for the 
                    returned string. The maximum possible length, in characters, of
Uwe Schulzweida's avatar
Uwe Schulzweida committed
861
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
862
863

@Description
864
The function @func{gridInqYname} returns the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
865
866

@Result
867
@func{gridInqYname} returns the name of the Y-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
868
869
870
871
872

@EndFunction
*/
void gridInqYname(int gridID, char *yname)
{
873
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
874

875
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
876

877
878
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
879
880
881
882
883
884
885
  strcpy(yname, gridptr->yname);
}

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
886
@Prototype void gridInqXlongname(int gridID, char *longname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
887
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
888
889
890
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
    @Item  longname Longname of the Y-axis. The caller must allocate space for the 
                    returned string. The maximum possible length, in characters, of
Uwe Schulzweida's avatar
Uwe Schulzweida committed
891
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
892
893

@Description
894
The function @func{gridInqYlongname} returns the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
895
896

@Result
897
@func{gridInqYlongname} returns the longname of the Y-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
898
899
900
901
902

@EndFunction
*/
void gridInqYlongname(int gridID, char *ylongname)
{
903
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
904

905
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
906

907
908
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
909
910
911
912
913
914
915
  strcpy(ylongname, gridptr->ylongname);
}

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
916
@Prototype void gridInqYunits(int gridID, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
917
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
918
919
920
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
    @Item  units    Units of the Y-axis. The caller must allocate space for the 
                    returned string. The maximum possible length, in characters, of
Uwe Schulzweida's avatar
Uwe Schulzweida committed
921
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
922
923

@Description
924
The function @func{gridInqYunits} returns the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
925
926

@Result
927
@func{gridInqYunits} returns the units of the Y-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
928
929
930
931
932

@EndFunction
*/
void gridInqYunits(int gridID, char *yunits)
{
933
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
934

935
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
936

937
938
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
939
940
941
942
943
  strcpy(yunits, gridptr->yunits);
}

void gridInqYstdname(int gridID, char *ystdname)
{
944
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
945

946
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
947

948
949
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
950
951
952
953
954
955
956
957
958
  strcpy(ystdname, gridptr->ystdname);
}

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

@Prototype int gridInqType(int gridID)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
959
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
960
961

@Description
962
The function @func{gridInqType} returns the type of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
963
964

@Result
965
@func{gridInqType} returns the type of the grid,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
966
one of the set of predefined CDI grid types.
967
The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
968
@func{GRID_LONLAT}, @func{GRID_LCC}, @func{GRID_SPECTRAL}, @func{GRID_GME},
969
@func{GRID_CURVILINEAR}, @func{GRID_UNSTRUCTURED} and @func{GRID_REFERENCE}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
970
971
972
973
974

@EndFunction
*/
int gridInqType(int gridID)
{
975
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
976

977
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
978

979
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
980
981
982
983
984
985
986
987
988
989
990

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

@Description
994
The function @func{gridInqSize} returns the size of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
995
996

@Result
997
@func{gridInqSize} returns the number of grid points of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
998
999
1000
1001
1002
1003

@EndFunction
*/
int gridInqSize(int gridID)
{
  int size = 0;
1004
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1005

1006
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1007

1008
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019

  size = gridptr->size;

  if ( ! size )
    {
      int xsize, ysize;

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

      if ( ysize )
1020
        size = xsize *ysize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1021
      else
1022
        size = xsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1023

1024
      gridptr->size = size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1025
1026
1027
1028
1029
    }

  return (size);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1030
1031
static
int nsp2trunc(int nsp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1032
1033
1034
1035
1036
1037
1038
1039
1040
{
  /*  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;

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1041
  trunc = (int) (sqrt(nsp*4 + 1.) - 3) / 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1042
1043
1044
1045
1046
1047
1048

  return (trunc);
}


int gridInqTrunc(int gridID)
{
1049
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1050

1051
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1052

1053
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1054
1055
1056
1057

  if ( gridptr->trunc == 0 )
    {
      if ( gridptr->type == GRID_SPECTRAL )
1058
        gridptr->trunc = nsp2trunc(gridptr->size);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1059
1060
      /*
      else if      ( gridptr->type == GRID_GAUSSIAN )
1061
        gridptr->trunc = nlat2trunc(gridptr->ysize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1062
1063
1064
1065
1066
1067
      */
    }

  return (gridptr->trunc);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1068

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1069
1070
void gridDefTrunc(int gridID, int trunc)
{
1071
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1072

1073
1074
1075
1076
1077
1078
1079
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
      xwarning("%s", "Operation not executed.");
      return;
    }

  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1080

1081
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1082
1083
1084
1085

  gridptr->trunc = trunc;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1086
1087
1088
1089
1090
1091
/*
@Function  gridDefXsize
@Title     Define the number of values of a X-axis

@Prototype void gridDefXsize(int gridID, int xsize)
@Parameter
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1092
1093
    @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
1094
1095

@Description
1096
The function @func{gridDefXsize} defines the number of values of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1097
1098
1099

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1100
1101
void gridDefXsize(int gridID, int xsize)
{
1102
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1103

1104
1105
1106
1107
1108
1109
1110
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
      xwarning("%s", "Operation not executed.");
      return;
    }

  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1111

1112
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1113
1114

  if ( xsize > gridInqSize(gridID) )
1115
    Error("xsize %d is greater then gridsize %d", xsize, gridInqSize(gridID));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1116

1117
  if ( gridInqType(gridID) == GRID_UNSTRUCTURED && xsize != gridInqSize(gridID) )
1118
    Error("xsize %d must be equal to gridsize %d for gridtype: UNSTRUCTURED", xsize, gridInqSize(gridID));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1119
1120
1121

  gridptr->xsize = xsize;

1122
1123
1124
1125
1126
1127
1128
  if ( gridInqType(gridID) != GRID_UNSTRUCTURED )
    {
      long gridsize = gridptr->xsize*gridptr->ysize;
      if ( gridsize > 0 && gridsize != gridInqSize(gridID) )
        Error("Inconsistent grid declaration! (xsize=%d ysize=%d gridsize=%d)",
              gridptr->xsize, gridptr->ysize, gridInqSize(gridID));
    }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1129
1130
1131
}

/*
1132
1133
@Function
@Title
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1134

1135
@Prototype
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1136
1137
1138
1139
1140
1141
1142
@Parameter
    @Item  Grid identifier

@EndFunction
*/
void gridDefPrec(int gridID, int prec)
{
1143
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1144

1145
1146
1147
1148
1149
1150
1151
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
      xwarning("%s", "Operation not executed.");
      return;
    }

  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1152

1153
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1154
1155
1156
1157
1158

  gridptr->prec = prec;
}

/*
1159
1160
@Function
@Title
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1161

1162
@Prototype
Uwe Schulzweida's avatar
Uwe Schulzweida committed