Skip to content

Commit b7a5b8b

Browse files
Adding some additional g2c interfaces (#584)
* add function g2c_get_pdt_len * create g2c_get_gdt_len * typo * Update grib2.h.in * Update tst_pdstemplates.c * Update tst_get_grid_template.c --------- Co-authored-by: Edward Hartnett <38856240+edwardhartnett@users.noreply.github.com>
1 parent 476105c commit b7a5b8b

5 files changed

Lines changed: 102 additions & 0 deletions

File tree

src/grib2.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ int g2c_get_grid_template_extension(int grid_template_num, int *g2c_template,
331331
int *extlen, int *ext);
332332
int g2c_get_pds_template_extension(int pds_template_num, int *g2c_template,
333333
int *extlen, int *ext);
334+
int g2c_get_gdt_len(int grid_template_num, int *maplen);
334335
int g2c_get_pds_template(int pds_template_num, int *maplen, int *map, int *needext);
336+
int g2c_get_pdt_len(int pds_template_num, int *maplen);
335337
int g2c_get_drs_template(int drs_template_num, int *maplen, int *map, int *needext);
336338

337339
/* Internal functions. */

src/gridtemplates.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,39 @@ g2c_get_grid_template(int grid_template_num, int *maplen, int *map, int *needext
414414
/* If we didn't find a template, return an error. */
415415
return G2C_ENOTEMPLATE;
416416
}
417+
418+
/**
419+
* Get initial length (number of entries) in static part of
420+
* Grid Definition Template.
421+
*
422+
* @param grid_template_num The Grid template number.
423+
* @param maplen Pointer that gets the length of the map. Ignored if
424+
* NULL.
425+
*
426+
* @return
427+
* - ::G2C_NOERROR No error.
428+
* - ::G2C_ENOTEMPLATE Template not found.
429+
*
430+
* @author Alyson Stahl @date 01/21/25
431+
*/
432+
int
433+
g2c_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+
}

src/pdstemplates.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,3 +1829,38 @@ g2c_get_pds_template(int pds_template_num, int *maplen, int *map, int *needext)
18291829
/* If we didn't find a template, return an error. */
18301830
return G2C_ENOTEMPLATE;
18311831
}
1832+
1833+
/**
1834+
* Get initial length (number of entries) in static part of
1835+
* PDS template.
1836+
*
1837+
* @param pds_template_num The PDS template number.
1838+
* @param maplen Pointer that gets the length of the map. Ignored if
1839+
* NULL.
1840+
*
1841+
* @return
1842+
* - ::G2C_NOERROR No error.
1843+
* - ::G2C_ENOTEMPLATE Template not found.
1844+
*
1845+
* @author Alyson Stahl @date 01/21/25
1846+
*/
1847+
int
1848+
g2c_get_pdt_len(int pds_template_num, int *maplen)
1849+
{
1850+
int j;
1851+
1852+
/* Look through the array of templates to find a matching one. */
1853+
for (j = 0; j < G2C_MAX_PDS_TEMPLATE; j++)
1854+
{
1855+
if (pds_template_num == templatespds[j].template_num)
1856+
{
1857+
if (maplen)
1858+
*maplen = templatespds[j].mappdslen;
1859+
1860+
return G2C_NOERROR;
1861+
}
1862+
}
1863+
1864+
/* If we didn't find a template, return an error. */
1865+
return G2C_ENOTEMPLATE;
1866+
}

tests/tst_get_grid_template.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@ main()
141141
}
142142
}
143143
printf("ok!\n");
144+
printf("Testing g2c_get_gdt_len()\n");
145+
{
146+
int template_number[NUM_TESTS] = {
147+
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,
148+
204, 32768, 32769, 1000, 1100, 1200, 13, 23, 33, 61, 62, 63, 150};
149+
int expected_maplen[NUM_TESTS] = {
150+
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,
151+
19, 21, 20, 28, 16, 23, 22, 26, 23, 23, 26, 13};
152+
int maplen, t, ret;
153+
154+
for (t = 0; t < NUM_TESTS; t++)
155+
{
156+
if ((ret = g2c_get_gdt_len(template_number[t], &maplen)))
157+
return ret;
158+
if (maplen != expected_maplen[t])
159+
return G2C_ERROR;
160+
}
161+
}
162+
printf("ok!\n");
144163
printf("SUCCESS!\n");
145164
return 0;
146165
}

tests/tst_pdstemplates.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,16 @@ main()
902902
free(tmpl);
903903
}
904904
printf("ok!\n");
905+
printf("\tchecking g2c_get_pdt_len() with template %d...", number[t]);
906+
{
907+
int maplen, ret;
908+
909+
if ((ret = g2c_get_pdt_len(number[t], &maplen)))
910+
return ret;
911+
if (maplen != expected_maplen[t])
912+
return G2C_ERROR;
913+
}
914+
printf("ok!\n");
905915
printf("\tchecking g2c_get_pds_template() with template %d...", number[t]);
906916
{
907917
int maplen, needext;

0 commit comments

Comments
 (0)