Loading...
Searching...
No Matches
gridtemplates.c
Go to the documentation of this file.
#include "grib2_int.h"
struct gridtemplate
{
g2int template_num;
g2int mapgridlen;
g2int needext;
g2int mapgrid[G2C_MAX_GDS_TEMPLATE_MAPLEN];
};
static const struct gridtemplate templatesgrid[G2C_MAX_GDS_TEMPLATE] =
{
/* 3.0: Lat/Lon grid */
{0, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
/* 3.1: Rotated Lat/Lon grid */
{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}},
/* 3.2: Stretched Lat/Lon grid */
{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}},
/* 3.3: Stretched & Rotated Lat/Lon grid */
{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}},
/* Added GDT 3.4,3.5 (08/05/2013) */
/* 3.4: Variable resolution Latitude/Longitude */
{4, 13, 1, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, 1, 1}},
/* 3.5: Variable resolution rotate Latitude/Longitude */
{5, 16, 1, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, 1, 1, -4, 4, 4}},
/* 3.12: Transverse Mercator */
{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}},
/* 3.101: General unstructured grid */
{101, 4, 0, {1, 4, 1, -4}},
/* 3.140: Lambert Azimuthal Equal Area Projection */
{140, 17, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 4, 4, 1, 4, 4, 1}},
/* 3.10: Mercator */
{10, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, -4, 4, 1, 4, 4, 4}},
/* 3.20: Polar Stereographic Projection */
{20, 18, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, 1}},
/* 3.30: Lambert Conformal */
{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}},
/* 3.31: Albers equal area */
{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}},
/* 3.40: Guassian Lat/Lon */
{40, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
/* 3.41: Rotated Gaussian Lat/Lon */
{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}},
/* 3.42: Stretched Gaussian Lat/Lon */
{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}},
/* 3.43: Stretched and Rotated Gaussian Lat/Lon */
{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}},
/* 3.50: Spherical Harmonic Coefficients */
{50, 5, 0, {4, 4, 4, 1, 1}},
/* 3.51: Rotated Spherical Harmonic Coefficients */
{51, 8, 0, {4, 4, 4, 1, 1, -4, 4, 4}},
/* 3.52: Stretched Spherical Harmonic Coefficients */
{52, 8, 0, {4, 4, 4, 1, 1, -4, 4, -4}},
/* 3.53: Stretched and Rotated Spherical Harmonic Coefficients */
{53, 11, 0, {4, 4, 4, 1, 1, -4, 4, 4, -4, 4, -4}},
/* 3.90: Space View Perspective or orthographic */
{90, 21, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, 4, 4, 4, 4, 1, 4, 4, 4, 4}},
/* 3.100: Triangular grid based on an icosahedron */
{100, 11, 0, {1, 1, 2, 1, -4, 4, 4, 1, 1, 1, 4}},
/* 3.110: Equatorial Azimuthal equidistant */
{110, 16, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, -4, 4, 1, 4, 4, 1, 1}},
/* 3.120: Azimuth-range projection */
{120, 7, 1, {4, 4, -4, 4, 4, 4, 1}},
/* 3.204: Curvilinear Orthogonal Grid */
{204, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
/* 3.32768: Rot Lat/Lon E-grid (Arakawa) */
{32768, 19, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1}},
/* 3.32769: Rot Lat/Lon Non-E Staggered grid (Arakawa) */
{32769, 21, 0, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, 4, -4, 4, 1, -4, 4, 4, 4, 1, 4, 4}},
/* 3.1000: Cross Section Grid */
{1000, 20, 1, {1, 1, 4, 1, 4, 1, 4, 4, 4, 4, -4, 4, 1, 4, 4, 1, 2, 1, 1, 2}},
/* 3.1100: Hovmoller Diagram Grid */
{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}},
/* 3.1200: Time Section Grid */
{1200, 16, 1, {4, 1, -4, 1, 1, -4, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2}},
/* 3.13: Mercator with modelling subdomains definition */
{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}},
/* 3.23: Polar stereographic projection with modelling subdomains definition */
{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}},
/* 3.30: Lambert conformal with modelling subdomains definition */
{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}},
/* 3.61: Spectral Mercator with modelling subdomains definition */
{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}},
/* 3.62: Spectral Polar Stereographic with modelling subdomains definition */
{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}},
/* 3.63: Spectral Lambert Conformal with modelling subdomains definition */
{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}},
/* 3.150: Hierarchical Equal Area isoLatitude Pixelization grid (HEALPix) */
{150, 13, 0, {1, 1, 4, 1, 4, 1, 4, 1, 4, 4, 1, 1, 1}}};
static g2int
getgridindex(g2int number)
{
g2int j, getgridindex = -1;
for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
{
if (number == templatesgrid[j].template_num)
{
getgridindex = j;
return (getgridindex);
}
}
return (getgridindex);
}
gtemplate *
getgridtemplate(g2int number)
{
g2int index;
gtemplate *new;
index = getgridindex(number);
if (index != -1)
{
new = malloc(sizeof(gtemplate));
new->type = 3;
new->num = templatesgrid[index].template_num;
new->maplen = templatesgrid[index].mapgridlen;
new->needext = templatesgrid[index].needext;
new->map = (g2int *)templatesgrid[index].mapgrid;
new->extlen = 0;
new->ext = NULL;
return (new);
}
else
{
printf("getgridtemplate: GDT Template 3.%d not defined.\n", (int)number);
return (NULL);
}
return (NULL);
}
gtemplate *
extgridtemplate(g2int number, g2int *template)
{
gtemplate *new;
g2int index, i;
index = getgridindex(number);
if (index == -1)
return NULL;
new = getgridtemplate(number);
if (!new->needext)
return (new);
if (number == 120)
{
new->extlen = template[1] * 2;
new->ext = malloc(sizeof(g2int) * new->extlen);
for (i = 0; i < new->extlen; i++)
{
if (i % 2 == 0)
new->ext[i] = 2;
else
new->ext[i] = -2;
}
}
else if (number == 4 || number == 5)
{
/* The extension is of length template[7] + template[8]. The first
* template[7] values are 4, the next template[8] values are -4. */
new->extlen = template[7] + template[8];
new->ext = malloc(sizeof(g2int) * new->extlen);
for (i = 0; i < new->extlen; i++)
new->ext[i] = i < template[7] ? 4 : -4;
}
else if (number == 1000)
{
new->extlen = template[19];
new->ext = malloc(sizeof(g2int) * new->extlen);
for (i = 0; i < new->extlen; i++)
{
new->ext[i] = 4;
}
}
else if (number == 1200)
{
new->extlen = template[15];
new->ext = malloc(sizeof(g2int) * new->extlen);
for (i = 0; i < new->extlen; i++)
{
new->ext[i] = 4;
}
}
return (new);
}
int
g2c_get_grid_template_extension(int grid_template_num, int *template,
int *extlen, int *ext)
{
int j, t;
/* Check input. */
if (!template)
return G2C_EINVAL;
/* Look through the array of templates to find a matching one. */
for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
{
if (grid_template_num == templatesgrid[j].template_num)
{
/* Is there an extension to this template? */
if (templatesgrid[j].needext)
{
gtemplate *gt;
g2int *template8;
int e;
/* Copy templage to g2int for extgridtemplate() function. */
if (!(template8 = malloc(sizeof(g2int) * templatesgrid[j].mapgridlen)))
return G2C_ENOMEM;
for (t = 0; t < templatesgrid[j].mapgridlen; t++)
template8[t] = template[t];
if (!(gt = extgridtemplate(grid_template_num, template8)))
return G2C_ENOTEMPLATE;
free(template8);
if (extlen)
*extlen = gt->extlen;
if (ext)
for (e = 0; e < gt->extlen; e++)
ext[e] = gt->ext[e];
free(gt->ext);
free(gt);
}
else
{
if (extlen)
*extlen = 0;
}
/* Done. */
return G2C_NOERROR;
}
}
/* If we didn't find a template, return an error. */
return G2C_ENOTEMPLATE;
}
int
g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *needext)
{
int j, m;
/* Look through the array of templates to find a matching one. */
for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
{
if (grid_template_num == templatesgrid[j].template_num)
{
/* Copy maplen and map if the caller wants them. */
if (maplen)
*maplen = templatesgrid[j].mapgridlen;
if (map)
for (m = 0; m < templatesgrid[j].mapgridlen; m++)
map[m] = templatesgrid[j].mapgrid[m];
if (needext)
*needext = templatesgrid[j].needext;
/* Done. */
return G2C_NOERROR;
}
}
/* If we didn't find a template, return an error. */
return G2C_ENOTEMPLATE;
}
int
g2c_get_gdt_len(int grid_template_num, int *maplen)
{
int j;
/* Look through the array of templates to find a matching one. */
for (j = 0; j < G2C_MAX_GDS_TEMPLATE; j++)
{
if (grid_template_num == templatesgrid[j].template_num)
{
if (maplen)
*maplen = templatesgrid[j].mapgridlen;
/* Done. */
return G2C_NOERROR;
}
}
/* If we didn't find a template, return an error. */
return G2C_ENOTEMPLATE;
}
G2C_MAX_GDS_TEMPLATE
#define G2C_MAX_GDS_TEMPLATE
Maximum number of grid templates.
Definition grib2.h:292
G2C_MAX_GDS_TEMPLATE_MAPLEN
#define G2C_MAX_GDS_TEMPLATE_MAPLEN
Maximum grid template map length.
Definition grib2.h:293
G2C_ENOTEMPLATE
#define G2C_ENOTEMPLATE
Template not found.
Definition grib2.h:511
G2C_ENOMEM
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:499
G2C_EINVAL
#define G2C_EINVAL
Invalid input.
Definition grib2.h:495
g2int
int64_t g2int
Long integer type.
Definition grib2.h:32
G2C_NOERROR
#define G2C_NOERROR
No error.
Definition grib2.h:490
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
gtemplate::ext
g2int * ext
Number of octets of each entry in the extension part of the template.
Definition grib2_int.h:302
gtemplate::extlen
g2int extlen
Number of entries in the template extension.
Definition grib2_int.h:298
gtemplate
Struct for GRIB template, returned by getgridtemplate().
Definition grib2_int.h:278
extgridtemplate
gtemplate * extgridtemplate(g2int number, g2int *template)
This subroutine generates the remaining octet map for a given Grid Definition Template,...
Definition gridtemplates.c:241
templatesgrid
static const struct gridtemplate templatesgrid[G2C_MAX_GDS_TEMPLATE]
Templates grid.
Definition gridtemplates.c:58
g2c_get_grid_template_extension
int g2c_get_grid_template_extension(int grid_template_num, int *template, int *extlen, int *ext)
Get grid template extension information.
Definition gridtemplates.c:316
getgridindex
static g2int getgridindex(g2int number)
This function returns the index of specified Grid Definition Template in array templates for [Section...
Definition gridtemplates.c:155
getgridtemplate
gtemplate * getgridtemplate(g2int number)
This subroutine returns grid template information for a specified Grid Definition Template for [Secti...
Definition gridtemplates.c:193
g2c_get_gdt_len
int g2c_get_gdt_len(int grid_template_num, int *maplen)
Get initial length (number of entries) in static part of Grid Definition Template.
Definition gridtemplates.c:433
g2c_get_grid_template
int g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *needext)
Get grid template information.
Definition gridtemplates.c:391