NCEPLIBS-g2c 2.2.0
Loading...
Searching...
No Matches
gridtemplates.c
Go to the documentation of this file.
1
42#include "grib2_int.h"
43
47struct gridtemplate
48{
49 g2int template_num;
50 g2int mapgridlen;
51 g2int needext;
53};
54
58static const struct gridtemplate templatesgrid[G2C_MAX_GDS_TEMPLATE] =
59 {
60 /* 3.0: Lat/Lon grid */
61 {0, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
62 /* 3.1: Rotated Lat/Lon grid */
63 {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}},
64 /* 3.2: Stretched Lat/Lon grid */
65 {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}},
66 /* 3.3: Stretched & Rotated Lat/Lon grid */
67 {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}},
68 /* Added GDT 3.4,3.5 (08/05/2013) */
69 /* 3.4: Variable resolution Latitude/Longitude */
70 {4, 13, 1, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, 1, 1}},
71 /* 3.5: Variable resolution rotate Latitude/Longitude */
72 {5, 16, 1, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, 1, 1, -4, 4, 4}},
73 /* 3.12: Transverse Mercator */
74 {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}},
75 /* 3.101: General unstructured grid */
76 {101, 4, 0, {1, 4, 1, -4}},
77 /* 3.140: Lambert Azimuthal Equal Area Projection */
78 {140, 17, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 4, 4, 1, 4, 4, 1}},
79
80 /* 3.10: Mercator */
81 {10, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, -4, 4, 1, 4, 4, 4}},
82 /* 3.20: Polar Stereographic Projection */
83 {20, 18, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, 1}},
84 /* 3.30: Lambert Conformal */
85 {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}},
86 /* 3.31: Albers equal area */
87 {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}},
88 /* 3.40: Guassian Lat/Lon */
89 {40, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
90 /* 3.41: Rotated Gaussian Lat/Lon */
91 {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}},
92 /* 3.42: Stretched Gaussian Lat/Lon */
93 {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}},
94 /* 3.43: Stretched and Rotated Gaussian Lat/Lon */
95 {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}},
96 /* 3.50: Spherical Harmonic Coefficients */
97 {50, 5, 0, {4, 4, 4, 1, 1}},
98 /* 3.51: Rotated Spherical Harmonic Coefficients */
99 {51, 8, 0, {4, 4, 4, 1, 1, -4, 4, 4}},
100 /* 3.52: Stretched Spherical Harmonic Coefficients */
101 {52, 8, 0, {4, 4, 4, 1, 1, -4, 4, -4}},
102 /* 3.53: Stretched and Rotated Spherical Harmonic Coefficients */
103 {53, 11, 0, {4, 4, 4, 1, 1, -4, 4, 4, -4, 4, -4}},
104 /* 3.90: Space View Perspective or orthographic */
105 {90, 21, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, 4, 4, 4, 4, 1, 4, 4, 4, 4}},
106 /* 3.100: Triangular grid based on an icosahedron */
107 {100, 11, 0, {1, 1, 2, 1, -4, 4, 4, 1, 1, 1, 4}},
108 /* 3.110: Equatorial Azimuthal equidistant */
109 {110, 16, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, 4, 4, 1, 1}},
110 /* 3.120: Azimuth-range projection */
111 {120, 7, 1, {4, 4, -4, 4, 4, 4, 1}},
112 /* 3.204: Curvilinear Orthogonal Grid */
113 {204, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
114 /* 3.32768: Rot Lat/Lon E-grid (Arakawa) */
115 {32768, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
116 /* 3.32769: Rot Lat/Lon Non-E Staggered grid (Arakawa) */
117 {32769, 21, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, 4, 4}},
118 /* 3.1000: Cross Section Grid */
119 {1000, 20, 1, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, -4, 4, 1, 4, 4, 1, 2, 1, 1, 2}},
120 /* 3.1100: Hovmoller Diagram Grid */
121 {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}},
122 /* 3.1200: Time Section Grid */
123 {1200, 16, 1, {4, 1, -4, 1, 1, -4, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2}},
124
125 /* 3.13: Mercator with modelling subdomains definition */
126 {13, 23, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, -4, 4, 1, 4, 4, 4, 4, 4, 4, 4}},
127 /* 3.23: Polar stereographic projection with modelling subdomains definition */
128 {23, 22, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, 1, 4, 4, 4, 4}},
129 /* 3.30: Lambert conformal with modelling subdomains definition */
130 {33, 26, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, 1, -4, -4, -4, 4, 4, 4, 4, 4}},
131 /* 3.61: Spectral Mercator with modelling subdomains definition */
132 {61, 23, 0, {1, 4, 4, 1, 8, 8, 8, 8, 8, 8, 1, 1, 4, 1, 4, 1, 4, -4, 4, -4, -4, 4, 4}},
133 /* 3.62: Spectral Polar Stereographic with modelling subdomains definition */
134 {62, 23, 0, {1, 4, 4, 1, 8, 8, 8, 8, 8, 8, 1, 1, 4, 1, 4, 1, 4, -4, 4, 1, -4, 4, 1}},
135 /* 3.63: Spectral Lambert Conformal with modelling subdomains definition */
136 {63, 26, 0, {1, 4, 4, 1, 8, 8, 8, 8, 8, 8, 1, 1, 4, 1, 4, 1, 4, -4, 4, -4, 4, 1, -4, -4, -4, 4}},
137 /* 3.150: Hierarchical Equal Area isoLatitude Pixelization grid (HEALPix) */
138 {150, 13, 0, {1, 1, 4, 1, 4, 1, 4, 1, 4, 4, 1, 1, 1}}};
139
154static g2int
156{
157 g2int j, getgridindex = -1;
158
159 for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
160 {
161 if (number == templatesgrid[j].template_num)
162 {
163 getgridindex = j;
164 return (getgridindex);
165 }
166 }
167
168 return (getgridindex);
169}
170
192gtemplate *
194{
195 g2int index;
196 gtemplate *new;
197
198 index = getgridindex(number);
199
200 if (index != -1)
201 {
202 new = malloc(sizeof(gtemplate));
203 new->type = 3;
204 new->num = templatesgrid[index].template_num;
205 new->maplen = templatesgrid[index].mapgridlen;
206 new->needext = templatesgrid[index].needext;
207 new->map = (g2int *)templatesgrid[index].mapgrid;
208 new->extlen = 0;
209 new->ext = NULL;
210 return (new);
211 }
212 else
213 {
214 printf("getgridtemplate: GDT Template 3.%d not defined.\n", (int)number);
215 return (NULL);
216 }
217
218 return (NULL);
219}
220
240gtemplate *
241extgridtemplate(g2int number, g2int *template)
242{
243 gtemplate *new;
244 g2int index, i;
245
246 index = getgridindex(number);
247 if (index == -1)
248 return NULL;
249
250 new = getgridtemplate(number);
251
252 if (!new->needext)
253 return (new);
254
255 if (number == 120)
256 {
257 new->extlen = template[1] * 2;
258 new->ext = malloc(sizeof(g2int) * new->extlen);
259 for (i = 0; i < new->extlen; i++)
260 {
261 if (i % 2 == 0)
262 new->ext[i] = 2;
263 else
264 new->ext[i] = -2;
265 }
266 }
267 else if (number == 4 || number == 5)
268 {
269 /* The extension is of length template[7] + template[8]. The first
270 * template[7] values are 4, the next template[8] values are -4. */
271 new->extlen = template[7] + template[8];
272 new->ext = malloc(sizeof(g2int) * new->extlen);
273 for (i = 0; i < new->extlen; i++)
274 new->ext[i] = i < template[7] ? 4 : -4;
275 }
276 else if (number == 1000)
277 {
278 new->extlen = template[19];
279 new->ext = malloc(sizeof(g2int) * new->extlen);
280 for (i = 0; i < new->extlen; i++)
281 {
282 new->ext[i] = 4;
283 }
284 }
285 else if (number == 1200)
286 {
287 new->extlen = template[15];
288 new->ext = malloc(sizeof(g2int) * new->extlen);
289 for (i = 0; i < new->extlen; i++)
290 {
291 new->ext[i] = 4;
292 }
293 }
294
295 return (new);
296}
297
315int
316g2c_get_grid_template_extension(int grid_template_num, int *template,
317 int *extlen, int *ext)
318{
319 int j, t;
320
321 /* Check input. */
322 if (!template)
323 return G2C_EINVAL;
324
325 /* Look through the array of templates to find a matching one. */
326 for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
327 {
328 if (grid_template_num == templatesgrid[j].template_num)
329 {
330 /* Is there an extension to this template? */
331 if (templatesgrid[j].needext)
332 {
333 gtemplate *gt;
334 g2int *template8;
335 int e;
336
337 /* Copy templage to g2int for extgridtemplate() function. */
338 if (!(template8 = malloc(sizeof(g2int) * templatesgrid[j].mapgridlen)))
339 return G2C_ENOMEM;
340 for (t = 0; t < templatesgrid[j].mapgridlen; t++)
341 template8[t] = template[t];
342 if (!(gt = extgridtemplate(grid_template_num, template8)))
343 return G2C_ENOTEMPLATE;
344 free(template8);
345 if (extlen)
346 *extlen = gt->extlen;
347 if (ext)
348 for (e = 0; e < gt->extlen; e++)
349 ext[e] = gt->ext[e];
350 free(gt->ext);
351 free(gt);
352 }
353 else
354 {
355 if (extlen)
356 *extlen = 0;
357 }
358
359 /* Done. */
360 return G2C_NOERROR;
361 }
362 }
363
364 /* If we didn't find a template, return an error. */
365 return G2C_ENOTEMPLATE;
366}
367
390int
391g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *needext)
392{
393 int j, m;
394
395 /* Look through the array of templates to find a matching one. */
396 for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
397 {
398 if (grid_template_num == templatesgrid[j].template_num)
399 {
400 /* Copy maplen and map if the caller wants them. */
401 if (maplen)
402 *maplen = templatesgrid[j].mapgridlen;
403 if (map)
404 for (m = 0; m < templatesgrid[j].mapgridlen; m++)
405 map[m] = templatesgrid[j].mapgrid[m];
406 if (needext)
407 *needext = templatesgrid[j].needext;
408
409 /* Done. */
410 return G2C_NOERROR;
411 }
412 }
413
414 /* If we didn't find a template, return an error. */
415 return G2C_ENOTEMPLATE;
416}
417
432int
433g2c_get_gdt_len(int grid_template_num, int *maplen)
434{
435 int j;
436
437 /* Look through the array of templates to find a matching one. */
438 for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
439 {
440 if (grid_template_num == templatesgrid[j].template_num)
441 {
442 if (maplen)
443 *maplen = templatesgrid[j].mapgridlen;
444
445 /* Done. */
446 return G2C_NOERROR;
447 }
448 }
449
450 /* If we didn't find a template, return an error. */
451 return G2C_ENOTEMPLATE;
452}
#define G2C_MAX_GDS_TEMPLATE
Maximum number of grid templates.
Definition grib2.h:292
#define G2C_MAX_GDS_TEMPLATE_MAPLEN
Maximum grid template map length.
Definition grib2.h:293
#define G2C_ENOTEMPLATE
Template not found.
Definition grib2.h:507
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:495
#define G2C_EINVAL
Invalid input.
Definition grib2.h:491
int64_t g2int
Long integer type.
Definition grib2.h:32
#define G2C_NOERROR
No error.
Definition grib2.h:486
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:302
g2int extlen
Number of entries in the template extension.
Definition grib2_int.h:298
Struct for GRIB template, returned by getgridtemplate().
Definition grib2_int.h:278
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.
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...
gtemplate * getgridtemplate(g2int number)
This subroutine returns grid template information for a specified Grid Definition Template for [Secti...
int g2c_get_gdt_len(int grid_template_num, int *maplen)
Get initial length (number of entries) in static part of Grid Definition Template.
int g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *needext)
Get grid template information.