Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better grid template handling with g2c_get_grid_template() #336

Merged
merged 12 commits into from
Oct 17, 2022
Merged
Prev Previous commit
Next Next commit
starting to test g2c_get_grid_template()
  • Loading branch information
edwardhartnett committed Oct 17, 2022
commit b3b570417c8452957c91acb58940f5d09f0be881
8 changes: 8 additions & 0 deletions src/grib2.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ g2int jpcunpack(unsigned char *cpack, g2int len, g2int *idrstmpl, g2int ndpts,
#define G2C_MAX_GRIB_CODE_LEN 20 /**< Maximum length of code. */
#define G2C_MAX_GRIB_TITLE_LEN 200 /**< Maximum length of code table title. */

/* Constants to help with templates. */
#define G2C_MAX_GRID_TEMPLATE 31 /**< Maximum number of grid templates. */
#define G2C_MAX_GRID_TEMPLATE_MAPLEN 200 /**< Maximum grid template map length. */

/* File handling functions. */
int g2c_open(const char *path, int mode, int *g2cid);
int g2c_close(int g2cid);
Expand All @@ -297,6 +301,10 @@ int g2c_close(int g2cid);
int g2c_get_prod(int g2cid, int msg_num, int prod_num, int *num_data_points,
float *data);

/* Templates. */
int g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *extlen,
int *ext);

/* Internal functions. */
int g2c_get_msg(int g2cid, size_t skip_bytes, size_t max_bytes, size_t *bytes_to_msg,
size_t *bytes_in_msg, unsigned char **cbuf);
Expand Down
35 changes: 22 additions & 13 deletions src/gridtemplates.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@
* 2009-01-14 | Vuong | Changed structure name template to gtemplate
* 2010-05-11 | Vuong | Added GDT 3.32769 Rotate Lat/Lon Non-E Staggered grid (Arakawa)
* 2013-08-06 | Vuong | Added GDT 3.4, 3.5, 3.12, 3.101, 3.140
* 2022-10-16 | Hartnett | Added g2c_get_grid_template().
*
* @author Stephen Gilbert @date 2001-06-28
*/
#include <stdlib.h>
#include "grib2_int.h"
#define MAXGRIDTEMP 31 /**< Maximum number of templates. */
#define MAXGRIDMAPLEN 200 /**< Maximum template map length. */

/**
* Struct for grid template.
Expand All @@ -50,13 +48,13 @@ struct gridtemplate
g2int template_num; /**< Template number. */
g2int mapgridlen; /**< The number of entries in the template. */
g2int needext; /**< Does template need extension? */
g2int mapgrid[MAXGRIDMAPLEN]; /**< Number of bytes for each template value. */
g2int mapgrid[G2C_MAX_GRID_TEMPLATE_MAPLEN]; /**< Number of bytes for each template value. */
};

/**
* Templates grid.
*/
static const struct gridtemplate templatesgrid[MAXGRIDTEMP] =
static const struct gridtemplate templatesgrid[G2C_MAX_GRID_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} },
Expand Down Expand Up @@ -143,7 +141,7 @@ getgridindex(g2int number)
{
g2int j, getgridindex = -1;

for (j = 0; j < MAXGRIDTEMP; j++)
for (j = 0; j < G2C_MAX_GRID_TEMPLATE; j++)
{
if (number == templatesgrid[j].template_num)
{
Expand Down Expand Up @@ -307,34 +305,43 @@ extgridtemplate(g2int number, g2int *list)
/**
* Get grid template information.
*
* The grid template consists of a template map, its length, and, for
* some templates, an extra extension map, and its length.
*
* @param grid_template_num The grid template number.
* @param maplen Pointer that gets the length of the map. Ignored if
* NULL.
* @param map Pointer that gets the map as an array of int. Memory
* must be allocated by caller. Ignored if NULL.
* @param extlen Pointer that gets the length of the extension. Ignored if NULL.
* @param ext Pointer that gets template extension, if there is one,
* or NULL if there is not one. Ignored if NULL.
* @param ext Pointer that gets template extension array, if there is
* one. Memory must be allocated by the caller. Ignored if NULL.
*
* @return
* - ::G2C_NOERROR No error.
* - ::G2C_ENOTEMPLATE Template not found.
*
* @author Ed Hartnett @date 10/16/22
*/
int
g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *extlen, int *ext)
g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *extlen,
int *ext)
{
int j, m, e;

for (j = 0; j < MAXGRIDTEMP; j++)

/* Look through the array of templates to find a matching one. */
for (j = 0; j < G2C_MAX_GRID_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];

/* Is there an extension to this template? */
if (templatesgrid[j].needext)
{
g2int *list = NULL;
Expand All @@ -354,11 +361,13 @@ g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *extlen,
{
if (extlen)
*extlen = 0;
if (ext)
ext = NULL;
}
}
}

/* If we didn't find a template, return an error. */
if (j == G2C_MAX_GRID_TEMPLATE)
return G2C_ENOTEMPLATE;

return G2C_NOERROR;
}