NCEPLIBS-g2c  1.8.0
gridtemplates.c
Go to the documentation of this file.
1 
41 #include "grib2_int.h"
42 
46 struct gridtemplate
47 {
48  g2int template_num;
49  g2int mapgridlen;
50  g2int needext;
52 };
53 
57 static const struct gridtemplate templatesgrid[G2C_MAX_GDS_TEMPLATE] =
58 {
59  /* 3.0: Lat/Lon grid */
60  {0, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
61  /* 3.1: Rotated Lat/Lon grid */
62  {1, 22, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, -4, 4, 4}},
63  /* 3.2: Stretched Lat/Lon grid */
64  {2, 22, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, -4, 4, -4}},
65  /* 3.3: Stretched & Rotated Lat/Lon grid */
66  {3, 25, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, -4, 4, 4, -4, 4, -4}},
67  /* Added GDT 3.4,3.5 (08/05/2013) */
68  /* 3.4: Variable resolution Latitude/Longitude */
69  {4, 13, 1, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, 1, 1}},
70  /* 3.5: Variable resolution rotate Latitude/Longitude */
71  {5, 16, 1, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, 1, 1, -4, 4, 4}},
72  /* 3.12: Transverse Mercator */
73  {12, 22, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, 4, 4, 1, 4, 4, -4, -4, -4, -4}},
74  /* 3.101: General unstructured grid */
75  {101, 4, 0, {1, 4, 1, -4}},
76  /* 3.140: Lambert Azimuthal Equal Area Projection */
77  {140, 17, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 4, 4, 1, 4, 4, 1}},
78 
79  /* 3.10: Mercator */
80  {10, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, -4, 4, 1, 4, 4, 4}},
81  /* 3.20: Polar Stereographic Projection */
82  {20, 18, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, 1}},
83  /* 3.30: Lambert Conformal */
84  {30, 22, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, 1, -4, -4, -4, 4}},
85  /* 3.31: Albers equal area */
86  {31, 22, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, 1, -4, -4, -4, 4}},
87  /* 3.40: Guassian Lat/Lon */
88  {40, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
89  /* 3.41: Rotated Gaussian Lat/Lon */
90  {41, 22, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, -4, 4, 4}},
91  /* 3.42: Stretched Gaussian Lat/Lon */
92  {42, 22, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, -4, 4, -4}},
93  /* 3.43: Stretched and Rotated Gaussian Lat/Lon */
94  {43, 25, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, -4, 4, 4, -4, 4, -4}},
95  /* 3.50: Spherical Harmonic Coefficients */
96  {50, 5, 0, {4, 4, 4, 1, 1}},
97  /* 3.51: Rotated Spherical Harmonic Coefficients */
98  {51, 8, 0, {4, 4, 4, 1, 1, -4, 4, 4}},
99  /* 3.52: Stretched Spherical Harmonic Coefficients */
100  {52, 8, 0, {4, 4, 4, 1, 1, -4, 4, -4}},
101  /* 3.53: Stretched and Rotated Spherical Harmonic Coefficients */
102  {53, 11, 0, {4, 4, 4, 1, 1, -4, 4, 4, -4, 4, -4}},
103  /* 3.90: Space View Perspective or orthographic */
104  {90, 21, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, 4, 4, 4, 4, 1, 4, 4, 4, 4}},
105  /* 3.100: Triangular grid based on an icosahedron */
106  {100, 11, 0, {1, 1, 2, 1, -4, 4, 4, 1, 1, 1, 4}},
107  /* 3.110: Equatorial Azimuthal equidistant */
108  {110, 16, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, 4, 4, 1, 1}},
109  /* 3.120: Azimuth-range projection */
110  {120, 7, 1, {4, 4, -4, 4, 4, 4, 1}},
111  /* 3.204: Curvilinear Orthogonal Grid */
112  {204, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
113  /* 3.32768: Rot Lat/Lon E-grid (Arakawa) */
114  {32768, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
115  /* 3.32769: Rot Lat/Lon Non-E Staggered grid (Arakawa) */
116  {32769, 21, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, 4, 4}},
117  /* 3.1000: Cross Section Grid */
118  {1000, 20, 1, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, -4, 4, 1, 4, 4, 1, 2, 1, 1, 2}},
119  /* 3.1100: Hovmoller Diagram Grid */
120  {1100, 28, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, -4, 4, 1, -4, 4, 1, 4, 1, -4, 1, 1, -4, 2, 1, 1, 1, 1, 1}},
121  /* 3.1200: Time Section Grid */
122  {1200, 16, 1, {4, 1, -4, 1, 1, -4, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2}}
123 } ;
124 
139 static g2int
141 {
142  g2int j, getgridindex = -1;
143 
144  for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
145  {
146  if (number == templatesgrid[j].template_num)
147  {
148  getgridindex = j;
149  return(getgridindex);
150  }
151  }
152 
153  return(getgridindex);
154 }
155 
177 gtemplate *
179 {
180  g2int index;
181  gtemplate *new;
182 
183  index = getgridindex(number);
184 
185  if (index != -1)
186  {
187  new = malloc(sizeof(gtemplate));
188  new->type = 3;
189  new->num = templatesgrid[index].template_num;
190  new->maplen = templatesgrid[index].mapgridlen;
191  new->needext = templatesgrid[index].needext;
192  new->map = (g2int *)templatesgrid[index].mapgrid;
193  new->extlen = 0;
194  new->ext = NULL;
195  return(new);
196  }
197  else
198  {
199  printf("getgridtemplate: GDT Template 3.%d not defined.\n", (int)number);
200  return(NULL);
201  }
202 
203  return(NULL);
204 }
205 
225 gtemplate *
226 extgridtemplate(g2int number, g2int *template)
227 {
228  gtemplate *new;
229  g2int index, i;
230 
231  index = getgridindex(number);
232  if (index == -1)
233  return NULL;
234 
235  new = getgridtemplate(number);
236 
237  if (!new->needext)
238  return(new);
239 
240  if (number == 120)
241  {
242  new->extlen = template[1] * 2;
243  new->ext = malloc(sizeof(g2int) * new->extlen);
244  for (i = 0; i < new->extlen; i++)
245  {
246  if (i % 2 == 0)
247  new->ext[i] = 2;
248  else
249  new->ext[i] = -2;
250  }
251  }
252  else if (number == 4 || number == 5)
253  {
254  /* The extension is of length template[7] + template[8]. The first
255  * template[7] values are 4, the next template[8] values are -4. */
256  new->extlen = template[7] + template[8];
257  new->ext = malloc(sizeof(g2int) * new->extlen);
258  for (i = 0; i < new->extlen; i++)
259  new->ext[i] = i < template[7] ? 4 : -4;
260  }
261  else if (number == 1000)
262  {
263  new->extlen = template[19];
264  new->ext = malloc(sizeof(g2int) * new->extlen);
265  for (i = 0; i < new->extlen; i++)
266  {
267  new->ext[i] = 4;
268  }
269  }
270  else if (number == 1200)
271  {
272  new->extlen = template[15];
273  new->ext = malloc(sizeof(g2int) * new->extlen);
274  for (i = 0; i < new->extlen; i++)
275  {
276  new->ext[i] = 4;
277  }
278  }
279 
280  return(new);
281 }
282 
300 int
301 g2c_get_grid_template_extension(int grid_template_num, int *template,
302  int *extlen, int *ext)
303 {
304  int j, t;
305 
306  /* Check input. */
307  if (!template)
308  return G2C_EINVAL;
309 
310  /* Look through the array of templates to find a matching one. */
311  for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
312  {
313  if (grid_template_num == templatesgrid[j].template_num)
314  {
315  /* Is there an extension to this template? */
316  if (templatesgrid[j].needext)
317  {
318  gtemplate *gt;
319  g2int *template8;
320  int e;
321 
322  /* Copy templage to g2int for extgridtemplate() function. */
323  if (!(template8 = malloc(sizeof(g2int) * templatesgrid[j].mapgridlen)))
324  return G2C_ENOMEM;
325  for (t = 0; t < templatesgrid[j].mapgridlen; t++)
326  template8[t] = template[t];
327  if (!(gt = extgridtemplate(grid_template_num, template8)))
328  return G2C_ENOTEMPLATE;
329  free(template8);
330  if (extlen)
331  *extlen = gt->extlen;
332  if (ext)
333  for (e = 0; e < gt->extlen; e++)
334  ext[e] = gt->ext[e];
335  free(gt->ext);
336  free(gt);
337  }
338  else
339  {
340  if (extlen)
341  *extlen = 0;
342  }
343 
344  /* Done. */
345  return G2C_NOERROR;
346  }
347  }
348 
349  /* If we didn't find a template, return an error. */
350  return G2C_ENOTEMPLATE;
351 }
352 
375 int
376 g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *needext)
377 {
378  int j, m;
379 
380  /* Look through the array of templates to find a matching one. */
381  for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
382  {
383  if (grid_template_num == templatesgrid[j].template_num)
384  {
385  /* Copy maplen and map if the caller wants them. */
386  if (maplen)
387  *maplen = templatesgrid[j].mapgridlen;
388  if (map)
389  for (m = 0; m < templatesgrid[j].mapgridlen; m++)
390  map[m] = templatesgrid[j].mapgrid[m];
391  if (needext)
392  *needext = templatesgrid[j].needext;
393 
394  /* Done. */
395  return G2C_NOERROR;
396  }
397  }
398 
399  /* If we didn't find a template, return an error. */
400  return G2C_ENOTEMPLATE;
401 }
402 
#define G2C_MAX_GDS_TEMPLATE
Maximum number of grid templates.
Definition: grib2.h:307
#define G2C_MAX_GDS_TEMPLATE_MAPLEN
Maximum grid template map length.
Definition: grib2.h:308
#define G2C_ENOTEMPLATE
Template not found.
Definition: grib2.h:512
#define G2C_ENOMEM
Out of memory.
Definition: grib2.h:500
#define G2C_EINVAL
Invalid input.
Definition: grib2.h:496
int64_t g2int
Long integer type.
Definition: grib2.h:33
#define G2C_NOERROR
No error.
Definition: grib2.h:491
Header file with internal function prototypes NCEPLIBS-g2c library.
g2int * ext
Number of octets of each entry in the extension part of the template.
Definition: grib2_int.h:300
g2int extlen
Number of entries in the template extension.
Definition: grib2_int.h:296
Struct for GRIB template, returned by getgridtemplate().
Definition: grib2_int.h:276
gtemplate * extgridtemplate(g2int number, g2int *template)
This subroutine generates the remaining octet map for a given Grid Definition Template,...
static const struct gridtemplate templatesgrid[G2C_MAX_GDS_TEMPLATE]
Templates grid.
Definition: gridtemplates.c:57
int g2c_get_grid_template_extension(int grid_template_num, int *template, int *extlen, int *ext)
Get grid template extension information.
static g2int getgridindex(g2int number)
This function returns the index of specified Grid Definition Template in array templates for [Section...
int g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *needext)
Get grid template information.
gtemplate * getgridtemplate(g2int number)
This subroutine returns grid template information for a specified Grid Definition Template for [Secti...