NCEPLIBS-g2c 2.0.0
Loading...
Searching...
No Matches
gridtemplates.c
Go to the documentation of this file.
1
41#include "grib2_int.h"
42
46struct gridtemplate
47{
48 g2int template_num;
49 g2int mapgridlen;
50 g2int needext;
52};
53
57static 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
138static g2int
140{
141 g2int j, getgridindex = -1;
142
143 for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
144 {
145 if (number == templatesgrid[j].template_num)
146 {
147 getgridindex = j;
148 return (getgridindex);
149 }
150 }
151
152 return (getgridindex);
153}
154
176gtemplate *
178{
179 g2int index;
180 gtemplate *new;
181
182 index = getgridindex(number);
183
184 if (index != -1)
185 {
186 new = malloc(sizeof(gtemplate));
187 new->type = 3;
188 new->num = templatesgrid[index].template_num;
189 new->maplen = templatesgrid[index].mapgridlen;
190 new->needext = templatesgrid[index].needext;
191 new->map = (g2int *)templatesgrid[index].mapgrid;
192 new->extlen = 0;
193 new->ext = NULL;
194 return (new);
195 }
196 else
197 {
198 printf("getgridtemplate: GDT Template 3.%d not defined.\n", (int)number);
199 return (NULL);
200 }
201
202 return (NULL);
203}
204
224gtemplate *
225extgridtemplate(g2int number, g2int *template)
226{
227 gtemplate *new;
228 g2int index, i;
229
230 index = getgridindex(number);
231 if (index == -1)
232 return NULL;
233
234 new = getgridtemplate(number);
235
236 if (!new->needext)
237 return (new);
238
239 if (number == 120)
240 {
241 new->extlen = template[1] * 2;
242 new->ext = malloc(sizeof(g2int) * new->extlen);
243 for (i = 0; i < new->extlen; i++)
244 {
245 if (i % 2 == 0)
246 new->ext[i] = 2;
247 else
248 new->ext[i] = -2;
249 }
250 }
251 else if (number == 4 || number == 5)
252 {
253 /* The extension is of length template[7] + template[8]. The first
254 * template[7] values are 4, the next template[8] values are -4. */
255 new->extlen = template[7] + template[8];
256 new->ext = malloc(sizeof(g2int) * new->extlen);
257 for (i = 0; i < new->extlen; i++)
258 new->ext[i] = i < template[7] ? 4 : -4;
259 }
260 else if (number == 1000)
261 {
262 new->extlen = template[19];
263 new->ext = malloc(sizeof(g2int) * new->extlen);
264 for (i = 0; i < new->extlen; i++)
265 {
266 new->ext[i] = 4;
267 }
268 }
269 else if (number == 1200)
270 {
271 new->extlen = template[15];
272 new->ext = malloc(sizeof(g2int) * new->extlen);
273 for (i = 0; i < new->extlen; i++)
274 {
275 new->ext[i] = 4;
276 }
277 }
278
279 return (new);
280}
281
299int
300g2c_get_grid_template_extension(int grid_template_num, int *template,
301 int *extlen, int *ext)
302{
303 int j, t;
304
305 /* Check input. */
306 if (!template)
307 return G2C_EINVAL;
308
309 /* Look through the array of templates to find a matching one. */
310 for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
311 {
312 if (grid_template_num == templatesgrid[j].template_num)
313 {
314 /* Is there an extension to this template? */
315 if (templatesgrid[j].needext)
316 {
317 gtemplate *gt;
318 g2int *template8;
319 int e;
320
321 /* Copy templage to g2int for extgridtemplate() function. */
322 if (!(template8 = malloc(sizeof(g2int) * templatesgrid[j].mapgridlen)))
323 return G2C_ENOMEM;
324 for (t = 0; t < templatesgrid[j].mapgridlen; t++)
325 template8[t] = template[t];
326 if (!(gt = extgridtemplate(grid_template_num, template8)))
327 return G2C_ENOTEMPLATE;
328 free(template8);
329 if (extlen)
330 *extlen = gt->extlen;
331 if (ext)
332 for (e = 0; e < gt->extlen; e++)
333 ext[e] = gt->ext[e];
334 free(gt->ext);
335 free(gt);
336 }
337 else
338 {
339 if (extlen)
340 *extlen = 0;
341 }
342
343 /* Done. */
344 return G2C_NOERROR;
345 }
346 }
347
348 /* If we didn't find a template, return an error. */
349 return G2C_ENOTEMPLATE;
350}
351
374int
375g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *needext)
376{
377 int j, m;
378
379 /* Look through the array of templates to find a matching one. */
380 for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
381 {
382 if (grid_template_num == templatesgrid[j].template_num)
383 {
384 /* Copy maplen and map if the caller wants them. */
385 if (maplen)
386 *maplen = templatesgrid[j].mapgridlen;
387 if (map)
388 for (m = 0; m < templatesgrid[j].mapgridlen; m++)
389 map[m] = templatesgrid[j].mapgrid[m];
390 if (needext)
391 *needext = templatesgrid[j].needext;
392
393 /* Done. */
394 return G2C_NOERROR;
395 }
396 }
397
398 /* If we didn't find a template, return an error. */
399 return G2C_ENOTEMPLATE;
400}
#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:497
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:485
#define G2C_EINVAL
Invalid input.
Definition grib2.h:481
int64_t g2int
Long integer type.
Definition grib2.h:32
#define G2C_NOERROR
No error.
Definition grib2.h:476
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_grid_template(int grid_template_num, int *maplen, int *map, int *needext)
Get grid template information.