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

Adding some additional g2c interfaces #584

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/grib2.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ int g2c_get_grid_template_extension(int grid_template_num, int *g2c_template,
int *extlen, int *ext);
int g2c_get_pds_template_extension(int pds_template_num, int *g2c_template,
int *extlen, int *ext);
int g2c_get_gdt_len(int grid_template_num, int *maplen);
int g2c_get_pds_template(int pds_template_num, int *maplen, int *map, int *needext);
int g2c_get_pdt_len(int pds_template_num, int *maplen);
int g2c_get_drs_template(int drs_template_num, int *maplen, int *map, int *needext);

/* Internal functions. */
Expand Down
36 changes: 36 additions & 0 deletions src/gridtemplates.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,39 @@ g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *needext
/* If we didn't find a template, return an error. */
return G2C_ENOTEMPLATE;
}

/**
* Get initial length (number of entries) in static part of
* Grid Definition Template.
*
* @param grid_template_num The Grid template number.
* @param maplen Pointer that gets the length of the map. Ignored if
* NULL.
*
* @return
* - ::G2C_NOERROR No error.
* - ::G2C_ENOTEMPLATE Template not found.
*
* @author Alyson Stahl @date 01/21/25
*/
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;
}
35 changes: 35 additions & 0 deletions src/pdstemplates.c
Original file line number Diff line number Diff line change
Expand Up @@ -1829,3 +1829,38 @@ g2c_get_pds_template(int pds_template_num, int *maplen, int *map, int *needext)
/* If we didn't find a template, return an error. */
return G2C_ENOTEMPLATE;
}

/**
* Get initial length (number of entries) in static part of
* PDS template.
*
* @param pds_template_num The PDS template number.
* @param maplen Pointer that gets the length of the map. Ignored if
* NULL.
*
* @return
* - ::G2C_NOERROR No error.
* - ::G2C_ENOTEMPLATE Template not found.
*
* @author Alyson Stahl @date 01/21/25
*/
int
g2c_get_pdt_len(int pds_template_num, int *maplen)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no function that already does this?

{
int j;

/* Look through the array of templates to find a matching one. */
for (j = 0; j < G2C_MAX_PDS_TEMPLATE; j++)
{
if (pds_template_num == templatespds[j].template_num)
{
if (maplen)
*maplen = templatespds[j].mappdslen;

return G2C_NOERROR;
}
}

/* If we didn't find a template, return an error. */
return G2C_ENOTEMPLATE;
}
19 changes: 19 additions & 0 deletions tests/tst_get_grid_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ main()
}
}
printf("ok!\n");
printf("Testing g2c_get_gdt_len()\n");
{
int template_number[NUM_TESTS] = {
0, 1, 2, 3, 4, 5, 12, 101, 140, 10, 20, 30, 31, 40, 41, 42, 43, 50, 51, 52, 53, 90, 100, 110, 120,
204, 32768, 32769, 1000, 1100, 1200, 13, 23, 33, 61, 62, 63, 150};
int expected_maplen[NUM_TESTS] = {
19, 22, 22, 25, 13, 16, 22, 4, 17, 19, 18, 22, 22, 19, 22, 22, 25, 5, 8, 8, 11, 21, 11, 16, 7, 19,
19, 21, 20, 28, 16, 23, 22, 26, 23, 23, 26, 13};
int maplen, t, ret;

for (t = 0; t < NUM_TESTS; t++)
{
if ((ret = g2c_get_gdt_len(template_number[t], &maplen)))
return ret;
if (maplen != expected_maplen[t])
return G2C_ERROR;
}
}
printf("ok!\n");
printf("SUCCESS!\n");
return 0;
}
10 changes: 10 additions & 0 deletions tests/tst_pdstemplates.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,16 @@ main()
free(tmpl);
}
printf("ok!\n");
printf("\tchecking g2c_get_pdt_len() with template %d...", number[t]);
{
int maplen, ret;

if ((ret = g2c_get_pdt_len(number[t], &maplen)))
return ret;
if (maplen != expected_maplen[t])
return G2C_ERROR;
}
printf("ok!\n");
printf("\tchecking g2c_get_pds_template() with template %d...", number[t]);
{
int maplen, needext;
Expand Down
Loading