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
7
#include <float.h>  /* FLT_EPSILON */

Uwe Schulzweida's avatar
Uwe Schulzweida committed
8
9
#include "dmemory.h"
#include "cdi.h"
10
#include "cdi_int.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
11
#include "grid.h"
12
#include "gaussgrid.h"
13
#include "pio_util.h"
14
#include "resource_handle.h"
15
#include "resource_unpack.h"
16
#include "namespace.h"
17
#include "serialize.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
19
20
21
22
23
24
25
26
27
28

#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[] = {
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 */  "reference",
Deike Kleberg's avatar
Deike Kleberg committed
45
  /* 16 */  "projection",
Uwe Schulzweida's avatar
Uwe Schulzweida committed
46
47
48
};


49
50
51
static int    gridCompareP    ( void * gridptr1, void * gridptr2 );
static void   gridDestroyP    ( void * gridptr );
static void   gridPrintP      ( void * gridptr, FILE * fp );
52
static int    gridGetPackSize ( void * gridptr, void *context);
53
static void   gridPack        ( void * gridptr, void * buff, int size,
54
				int *position, void *context);
Deike Kleberg's avatar
minimal    
Deike Kleberg committed
55
static int    gridTxCode      ( void );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
56

57
resOps gridOps = { gridCompareP, gridDestroyP, gridPrintP
58
                   , gridGetPackSize, gridPack, gridTxCode
59
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
60
61


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


Deike Kleberg's avatar
Deike Kleberg committed
65
void grid_init(grid_t *gridptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
66
{
67
  gridptr->self         = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
68
  gridptr->type         = CDI_UNDEFID;
Deike Kleberg's avatar
Deike Kleberg committed
69
  gridptr->proj         = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
70
  gridptr->mask         = NULL;
71
  gridptr->mask_gme     = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
73
74
75
76
77
78
79
80
  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
81
82
83
84
85
86
87
  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;
88
89
  gridptr->lcc_projflag = 0;
  gridptr->lcc_scanflag = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
90
  gridptr->lcc_defined  = FALSE;
91
92
93
94
95
96
  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;
97
98
99
100
  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
101
102
103
104
105
106
  gridptr->trunc        = 0;
  gridptr->nvertex      = 0;
  gridptr->nd           = 0;
  gridptr->ni           = 0;
  gridptr->ni2          = 0;
  gridptr->ni3          = 0;
107
108
109
  gridptr->number       = 0;
  gridptr->position     = 0;
  gridptr->reference    = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
110
111
112
113
  gridptr->prec         = 0;
  gridptr->size         = 0;
  gridptr->xsize        = 0;
  gridptr->ysize        = 0;
114
  gridptr->np           = 0;
115
116
  gridptr->xdef         = 0;
  gridptr->ydef         = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
  gridptr->isCyclic     = CDI_UNDEFID;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
118
119
120
121
122
  gridptr->isRotated    = FALSE;
  gridptr->xpole        = 0.0;
  gridptr->ypole        = 0.0;
  gridptr->angle        = 0.0;
  gridptr->locked       = FALSE;
123
  gridptr->lcomplex     = 0;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
124
125
126
127
128
129
130
131
  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;
132
  gridptr->uuid[0]      = 0;
Deike Kleberg's avatar
Deike Kleberg committed
133
  gridptr->name         = NULL;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
134
135
}

136

Deike Kleberg's avatar
Deike Kleberg committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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);
}

153
static
154
grid_t *gridNewEntry ( void )
155
{
156
  grid_t *gridptr;
157

158
  gridptr = ( grid_t *) xmalloc ( sizeof ( grid_t ));
Deike Kleberg's avatar
Deike Kleberg committed
159
  grid_init ( gridptr );
160
  gridptr->self = reshPut (( void * ) gridptr, &gridOps );
161
162

  return gridptr;
163
164
}

165
static
166
void gridInit (void)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
167
{
168
169
  static int gridInitialized = 0;
  char *env;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
170

171
  if ( gridInitialized ) return;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
172

Thomas Jahns's avatar
Thomas Jahns committed
173
  gridInitialized = 1;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
174

175
176
  env = getenv("GRID_DEBUG");
  if ( env ) GRID_Debug = atoi(env);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
177
178
}

179
180
static
void grid_copy(grid_t *gridptr2, grid_t *gridptr1)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
181
182
183
184
{
  int gridID2;

  gridID2 = gridptr2->self;
185
  memcpy(gridptr2, gridptr1, sizeof(grid_t));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
186
187
188
  gridptr2->self = gridID2;
}

189
static
190
void gridCheckPtr(const char *caller, int gridID, grid_t *gridptr)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
191
192
{
  if ( gridptr == NULL )
193
    Errorc("grid %d undefined!", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
194
195
}

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

Uwe Schulzweida's avatar
Uwe Schulzweida committed
198

Uwe Schulzweida's avatar
Uwe Schulzweida committed
199
int gridSize(void)
Thomas Jahns's avatar
Thomas Jahns committed
200
{
201
  return reshCountType ( &gridOps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
202
203
204
205
206
}


void gridGenXvals(int xsize, double xfirst, double xlast, double xinc, double *xvals)
{
207
  if ( (! (fabs(xinc) > 0)) && xsize > 1 )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
208
209
    {
      if ( xfirst >= xlast )
Thomas Jahns's avatar
Thomas Jahns committed
210
211
212
213
        {
          while ( xfirst >= xlast ) xlast += 360;
          xinc = (xlast-xfirst)/(xsize);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
214
      else
Thomas Jahns's avatar
Thomas Jahns committed
215
216
217
        {
          xinc = (xlast-xfirst)/(xsize-1);
        }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
218
219
    }

220
  for ( int i = 0; i < xsize; ++i )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
221
222
223
    xvals[i] = xfirst + i*xinc;
}

224
225
static
void calc_gaussgrid(double *yvals, int ysize, double yfirst, double ylast)
226
227
{
  double *yw;
228
229
  long yhsize;
  long i;
230
231
232
233
234
235
236
237
238
239
240
241

  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++ )
Thomas Jahns's avatar
Thomas Jahns committed
242
243
244
245
246
        {
          ytmp = yvals[i];
          yvals[i] = yvals[ysize-i-1];
          yvals[ysize-i-1] = ytmp;
        }
247
248
249
250
    }
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
251
252
void gridGenYvals(int gridtype, int ysize, double yfirst, double ylast, double yinc, double *yvals)
{
253
  long i;
Deike Kleberg's avatar
Deike Kleberg committed
254
  double deleps = 0.002;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
255
256
257
258

  if ( gridtype == GRID_GAUSSIAN || gridtype == GRID_GAUSSIAN_REDUCED )
    {
      if ( ysize > 2 )
Deike Kleberg's avatar
Deike Kleberg committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
	{
	  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 )
	      {
		double yinc = fabs(ylast-yfirst)/(ysize-1);
		double *ytmp = NULL;
		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); */
		if ( ny > ysize && ny < 4096 )
		  {
		    ytmp = (double *) malloc(ny*sizeof(double));
		    calc_gaussgrid(ytmp, ny, yfirst, ylast);
		    for ( i = 0; i < (ny-ysize); i++ )
		      if ( fabs(ytmp[i] - yfirst) < deleps ) break;

		    nstart = i;

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

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

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

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

      for ( i = 0; i < ysize; i++ )
Thomas Jahns's avatar
Thomas Jahns committed
334
        yvals[i] = yfirst + i*yinc;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
335
336
337
    }
  /*
    else
338
    Error("unable to calculate values for %s grid!", gridNamePtr(gridtype));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
339
340
341
342
  */
}

/*
343
@Function  gridCreate
344
@Title     Create a horizontal Grid
Uwe Schulzweida's avatar
Uwe Schulzweida committed
345

346
@Prototype int gridCreate(int gridtype, int size)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
347
348
@Parameter
    @Item  gridtype  The type of the grid, one of the set of predefined CDI grid types.
349
                     The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
350
                     @func{GRID_LONLAT}, @func{GRID_LCC}, @func{GRID_SPECTRAL},
351
                     @func{GRID_GME}, @func{GRID_CURVILINEAR}, @func{GRID_UNSTRUCTURED} and
352
                     @func{GRID_REFERENCE}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
353
354
355
    @Item  size      Number of gridpoints.

@Description
356
The function @func{gridCreate} creates a horizontal Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
357
358

@Result
359
@func{gridCreate} returns an identifier to the Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
360
361

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

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

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

391
392
393
  gridInit ();

  gridptr = gridNewEntry();
394
  if ( ! gridptr ) Error("No memory");
Uwe Schulzweida's avatar
Uwe Schulzweida committed
395
396
397

  gridID = gridptr->self;

398
  if ( CDI_Debug ) Message("gridID: %d", gridID);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
399
400
401
402
403

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

  /*  if ( gridtype == GRID_GENERIC )     gridptr->xsize = size; */
404
405
  if ( gridtype == GRID_UNSTRUCTURED )  gridptr->xsize = size;
  if ( gridtype == GRID_CURVILINEAR  )  gridptr->nvertex = 4;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
406
407
408
409
410
411
412
413
414

  switch (gridtype)
    {
    case GRID_LONLAT:
    case GRID_GAUSSIAN:
    case GRID_GAUSSIAN_REDUCED:
    case GRID_CURVILINEAR:
    case GRID_TRAJECTORY:
      {
Thomas Jahns's avatar
Thomas Jahns committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
        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
446
447
      }
    case GRID_GME:
448
    case GRID_UNSTRUCTURED:
449
      {
Thomas Jahns's avatar
Thomas Jahns committed
450
451
452
453
454
455
456
        gridDefXname(gridID, "lon");
        gridDefYname(gridID, "lat");
        strcpy(gridptr->xstdname, "longitude");
        strcpy(gridptr->ystdname, "latitude");
        gridDefXunits(gridID, "degrees_east");
        gridDefYunits(gridID, "degrees_north");
        break;
457
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
458
459
    case GRID_GENERIC:
      {
Thomas Jahns's avatar
Thomas Jahns committed
460
461
462
463
464
465
466
        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
467
      }
468
    case GRID_LCC2:
469
    case GRID_SINUSOIDAL:
470
    case GRID_LAEA:
471
      {
Thomas Jahns's avatar
Thomas Jahns committed
472
473
474
475
476
477
478
        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;
479
      }
Uwe Schulzweida's avatar
Uwe Schulzweida committed
480
481
482
483
484
    }

  return (gridID);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
485
486
static
void gridDestroyKernel( grid_t * gridptr )
487
488
489
{
  int id;

Deike Kleberg's avatar
Deike Kleberg committed
490
  xassert ( gridptr );
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505

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

506
  reshRemove ( id, &gridOps );
507
}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
508

509
510
511
512
513
514
/*
@Function  gridDestroy
@Title     Destroy a horizontal Grid

@Prototype void gridDestroy(int gridID)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
515
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
516
517
518
519
520

@EndFunction
*/
void gridDestroy(int gridID)
{
521
522
  grid_t *gridptr;

523
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
524

525
  gridDestroyKernel ( gridptr );
526
527
}

528
void gridDestroyP ( void * gridptr )
529
{
530
  gridDestroyKernel (( grid_t * ) gridptr );
531
532
533
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
534
535
536
char *gridNamePtr(int gridtype)
{
  char *name;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
537
  int size = (int) (sizeof(Grids)/sizeof(char *));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558

  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
Deike Kleberg's avatar
Deike Kleberg committed
559
560
    @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
561
562

@Description
563
The function @func{gridDefXname} defines the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
564
565
566

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
567
void gridDefXname(int gridID, const char *xname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
568
{
569
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
570

571
572
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
573
      xwarning ("%s", "Operation not executed." );
574
575
576
      return;
    }

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

579
580
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
581
582
583
584
585
586
587
588
589
590
  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
Deike Kleberg's avatar
Deike Kleberg committed
591
592
    @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
593
594

@Description
595
The function @func{gridDefXlongname} defines the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
596
597
598

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
599
void gridDefXlongname(int gridID, const char *xlongname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
600
{
601
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
602

603
604
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
605
      xwarning ("%s", "Operation not executed." );
606
607
608
      return;
    }

609
  gridptr = ( grid_t *) reshGetVal ( gridID, &gridOps );
610

Uwe Schulzweida's avatar
Uwe Schulzweida committed
611
612
613
614
615
616
617
618
619
620
  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
Deike Kleberg's avatar
Deike Kleberg committed
621
622
    @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
623
624

@Description
625
The function @func{gridDefXunits} defines the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
626
627
628

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
629
void gridDefXunits(int gridID, const char *xunits)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
630
{
631
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
632

633
634
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
635
      xwarning("%s", "Operation not executed." );
636
637
638
      return;
    }

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

641
642
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
643
644
645
646
647
648
649
650
651
652
  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
Deike Kleberg's avatar
Deike Kleberg committed
653
654
    @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
655
656

@Description
657
The function @func{gridDefYname} defines the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
658
659
660

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
661
void gridDefYname(int gridID, const char *yname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
662
{
663
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
664

665
666
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
667
      xwarning("%s", "Operation not executed.");
668
669
670
      return;
    }

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

673
674
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
675
676
677
678
679
680
681
682
683
684
  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
Deike Kleberg's avatar
Deike Kleberg committed
685
686
    @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
687
688

@Description
689
The function @func{gridDefYlongname} defines the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
690
691
692

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
693
void gridDefYlongname(int gridID, const char *ylongname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
694
{
695
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
696

697
698
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
699
      xwarning("%s", "Operation not executed.");
700
701
702
      return;
    }

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

705
706
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
707
708
709
710
711
712
713
714
715
716
  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
Deike Kleberg's avatar
Deike Kleberg committed
717
718
    @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
719
720

@Description
721
The function @func{gridDefYunits} defines the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
722
723
724

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
725
void gridDefYunits(int gridID, const char *yunits)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
726
{
727
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
728

729
730
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
731
      xwarning("%s", "Operation not executed.");
732
733
734
      return;
    }

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

737
738
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
739
740
741
742
743
744
745
746
  if ( yunits )
    strcpy(gridptr->yunits, yunits);
}

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

Deike Kleberg's avatar
Deike Kleberg committed
747
@Prototype void gridInqXname(int gridID, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
748
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
749
750
751
    @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
752
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
753
754

@Description
755
The function @func{gridInqXname} returns the name of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
756
757

@Result
758
@func{gridInqXname} returns the name of the X-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
759
760
761
762
763

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

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

768
769
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
770
771
772
773
774
775
776
  strcpy(xname, gridptr->xname);
}

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

Deike Kleberg's avatar
Deike Kleberg committed
777
@Prototype void gridInqXlongname(int gridID, char *longname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
778
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
779
780
781
    @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
782
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
783
784

@Description
785
The function @func{gridInqXlongname} returns the longname of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
786
787

@Result
788
@func{gridInqXlongname} returns the longname of the X-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
789
790
791
792
793

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

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

798
799
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
800
801
802
803
804
805
806
  strcpy(xlongname, gridptr->xlongname);
}

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

Deike Kleberg's avatar
Deike Kleberg committed
807
@Prototype void gridInqXunits(int gridID, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
808
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
809
810
811
    @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
812
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
813
814

@Description
815
The function @func{gridInqXunits} returns the units of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
816
817

@Result
818
@func{gridInqXunits} returns the units of the X-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
819
820
821
822
823

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

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

828
829
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
830
831
832
833
834
835
  strcpy(xunits, gridptr->xunits);
}


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

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

840
841
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
842
843
844
845
846
847
848
  strcpy(xstdname, gridptr->xstdname);
}

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

Deike Kleberg's avatar
Deike Kleberg committed
849
@Prototype void gridInqYname(int gridID, char *name)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
850
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
851
852
853
    @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
854
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
855
856

@Description
857
The function @func{gridInqYname} returns the name of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
858
859

@Result
860
@func{gridInqYname} returns the name of the Y-axis to the parameter name.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
861
862
863
864
865

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

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

870
871
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
872
873
874
875
876
877
878
  strcpy(yname, gridptr->yname);
}

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

Deike Kleberg's avatar
Deike Kleberg committed
879
@Prototype void gridInqXlongname(int gridID, char *longname)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
880
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
881
882
883
    @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
884
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
885
886

@Description
887
The function @func{gridInqYlongname} returns the longname of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
888
889

@Result
890
@func{gridInqYlongname} returns the longname of the Y-axis to the parameter longname.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
891
892
893
894
895

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

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

900
901
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
902
903
904
905
906
907
908
  strcpy(ylongname, gridptr->ylongname);
}

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

Deike Kleberg's avatar
Deike Kleberg committed
909
@Prototype void gridInqYunits(int gridID, char *units)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
910
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
911
912
913
    @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
914
                    the string is given by the predefined constant @func{CDI_MAX_NAME}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
915
916

@Description
917
The function @func{gridInqYunits} returns the units of a Y-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
918
919

@Result
920
@func{gridInqYunits} returns the units of the Y-axis to the parameter units.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
921
922
923
924
925

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

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

930
931
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
932
933
934
935
936
  strcpy(yunits, gridptr->yunits);
}

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

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

941
942
  grid_check_ptr(gridID, gridptr);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
943
944
945
946
947
948
949
950
951
  strcpy(ystdname, gridptr->ystdname);
}

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

@Prototype int gridInqType(int gridID)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
952
    @Item  gridID   Grid ID, from a previous call to @fref{gridCreate}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
953
954

@Description
955
The function @func{gridInqType} returns the type of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
956
957

@Result
958
@func{gridInqType} returns the type of the grid,
Uwe Schulzweida's avatar
Uwe Schulzweida committed
959
one of the set of predefined CDI grid types.
960
The valid CDI grid types are @func{GRID_GENERIC}, @func{GRID_GAUSSIAN},
Uwe Schulzweida's avatar
Uwe Schulzweida committed
961
@func{GRID_LONLAT}, @func{GRID_LCC}, @func{GRID_SPECTRAL}, @func{GRID_GME},
962
@func{GRID_CURVILINEAR}, @func{GRID_UNSTRUCTURED} and @func{GRID_REFERENCE}.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
963
964
965
966
967

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

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

972
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
973
974
975
976
977
978
979
980
981
982
983

  return (gridptr->type);
}


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

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

@Description
987
The function @func{gridInqSize} returns the size of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
988
989

@Result
990
@func{gridInqSize} returns the number of grid points of a Grid.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
991
992
993
994
995
996

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

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

1001
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012

  size = gridptr->size;

  if ( ! size )
    {
      int xsize, ysize;

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

      if ( ysize )
Thomas Jahns's avatar
Thomas Jahns committed
1013
        size = xsize *ysize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1014
      else
Thomas Jahns's avatar
Thomas Jahns committed
1015
        size = xsize;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1016

Thomas Jahns's avatar
Thomas Jahns committed
1017
      gridptr->size = size;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1018
1019
1020
1021
1022
    }

  return (size);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1023
1024
static
int nsp2trunc(int nsp)
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1025
1026
1027
1028
1029
1030
1031
1032
1033
{
  /*  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
1034
  trunc = (int) (sqrt(nsp*4 + 1.) - 3) / 2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1035
1036
1037
1038
1039
1040
1041

  return (trunc);
}


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

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

1046
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1047
1048
1049
1050

  if ( gridptr->trunc == 0 )
    {
      if ( gridptr->type == GRID_SPECTRAL )
Thomas Jahns's avatar
Thomas Jahns committed
1051
        gridptr->trunc = nsp2trunc(gridptr->size);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1052
1053
      /*
      else if      ( gridptr->type == GRID_GAUSSIAN )
Thomas Jahns's avatar
Thomas Jahns committed
1054
        gridptr->trunc = nlat2trunc(gridptr->ysize);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1055
1056
1057
1058
1059
1060
      */
    }

  return (gridptr->trunc);
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1061

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1062
1063
void gridDefTrunc(int gridID, int trunc)
{
1064
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1065

1066
1067
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
1068
      xwarning("%s", "Operation not executed.");
1069
1070
1071
      return;
    }

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

1074
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1075
1076
1077
1078

  gridptr->trunc = trunc;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
1079
1080
1081
1082
1083
1084
/*
@Function  gridDefXsize
@Title     Define the number of values of a X-axis

@Prototype void gridDefXsize(int gridID, int xsize)
@Parameter
Deike Kleberg's avatar
Deike Kleberg committed
1085
1086
    @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
1087
1088

@Description
1089
The function @func{gridDefXsize} defines the number of values of a X-axis.
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1090
1091
1092

@EndFunction
*/
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1093
1094
void gridDefXsize(int gridID, int xsize)
{
1095
  grid_t *gridptr;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1096

1097
1098
  if ( reshGetStatus ( gridID, &gridOps ) == CLOSED )
    {
1099
      xwarning("%s", "Operation not executed.");
1100
1101
1102
      return;
    }

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

1105
  grid_check_ptr(gridID, gridptr);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1106
1107

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

1110
  if ( gridInqType(gridID) == GRID_UNSTRUCTURED && xsize != gridInqSize(gridID) )
1111
    Error("xsize %d must be equal to gridsize %d for gridtype: UNSTRUCTURED", xsize, gridInqSize(gridID));
Uwe Schulzweida's avatar
Uwe Schulzweida committed
1112
1113
1114

  gridptr->xsize = xsize;