From f7ebfa75ec730483d35d0dfaec2dd1a419f8e4c8 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Mon, 23 Oct 2023 07:51:07 -0600 Subject: [PATCH 1/8] added failing test --- src/g2cdegrib2.c | 1 - tests/tst_degrib2_int.c | 20 ++++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/g2cdegrib2.c b/src/g2cdegrib2.c index 2d883390..696cbade 100644 --- a/src/g2cdegrib2.c +++ b/src/g2cdegrib2.c @@ -213,7 +213,6 @@ g2c_get_datetime(int ipdtn, long long int *ipdtmpl, short year, unsigned char mo else { is = ipos[ipdtn - 1] - 1; /* Continuous time interval. */ - printf("%d", is); sprintf(endtime, "%4.4d%2.2d%2.2d%2.2d:%2.2d:%2.2d", (int)ipdtmpl[is], (int)ipdtmpl[is + 1], (int)ipdtmpl[is + 2], (int)ipdtmpl[is + 3], (int)ipdtmpl[is + 4], (int)ipdtmpl[is + 5]); /* write(endtime, fmt = '(i4,3i2.2,":",i2.2,":",i2.2)') (ipdtmpl(j), j = is, is + 5) */ diff --git a/tests/tst_degrib2_int.c b/tests/tst_degrib2_int.c index 8a9878b8..47cbd440 100644 --- a/tests/tst_degrib2_int.c +++ b/tests/tst_degrib2_int.c @@ -7,26 +7,28 @@ */ #include "grib2_int.h" -#define NUM_PROD_TEST 5 +#define NUM_PROD_TEST 7 int main() { printf("Testing g2c degrib2 internal functions.\n"); - printf("Testing g2c_get_level_desc()..."); + printf("Testing g2c_get_level_desc()...\n"); { #define DATE_TIME_LEN 100 - int prod_template_num[NUM_PROD_TEST] = {0, 0, 0, 0, 8}; + int prod_template_num[NUM_PROD_TEST] = {0, 0, 0, 0, 8, 2, 0}; long long int prod_template_value[NUM_PROD_TEST][G2C_MAX_PDS_TEMPLATE_MAPLEN] = { {2, 1, 2, 0, 11, 0, 0, 1, 0, 1, 0, 1, 255, 0, 0}, {2, 10, 0, 0, 81, 0, 0, 1, 0, 100, 0, 80000, 255, 0, 0}, {0, 21, 2, 255, 104, 65535, 255, 1, 1, 103, 0, 2, 255, 0, 0}, {19, 238, 2, 255, 104, 65535, 255, 1, 1, 100, 0, 40000, 100, 0, 30000}, - {1, 228, 2, 255, 104, 65535, 255, 1, 0, 1, 0, 0, 255, 0, 0, 2022, 11, 17, 20, 0, 0, 1, 0, 1, 2, 1, 1, 1, 0} + {1, 228, 2, 255, 104, 65535, 255, 1, 0, 1, 0, 0, 255, 0, 0, 2022, 11, 17, 20, 0, 0, 1, 0, 1, 2, 1, 1, 1, 0}, + {0, 192, 4, 70, 70, 0, 0, 1, 0, 106, 0, 0, 106, 1, 1, 0, 20}, + {0, 192, 2, 0, 98, 0, 0, 1, 0, 106, 2, 0, 106, 2, 10} }; char expected_level_desc[NUM_PROD_TEST][G2C_MAX_GRIB_LEVEL_DESC_LEN + 1] = { " Surface", " 800 mb", "2 m above ground", " 400 - 300 mb", - " Surface"}; + " Surface", "0 - .1 m DBLY", " 0 - .10 m DBLY"}; char level_desc[G2C_MAX_GRIB_LEVEL_DESC_LEN + 1]; short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022}; unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11}; @@ -39,7 +41,9 @@ main() "valid 0 hour after 2022110612:00:00", "valid 1 hour after 2022110612:00:00", "valid 1 hour after 2022110612:00:00", - "(0 -1 hr) valid 0 hour after 2022111719:00:00 to 2022111720:00:00" + "(0 -1 hr) valid 0 hour after 2022111719:00:00 to 2022111720:00:00", + "valid 0 hour after 2022111719:00:00", + "valid 0 hour after 2022111719:00:00" }; int t; int ret; @@ -53,7 +57,7 @@ main() return ret; if (strcmp(level_desc, expected_level_desc[t])) { - printf("level_desc %s expected %s\n", level_desc, expected_level_desc[t]); + printf("level_desc %s \nexpected %s\n", level_desc, expected_level_desc[t]); return G2C_ERROR; } @@ -63,7 +67,7 @@ main() return ret; if (strcmp(date_time, expected_date_time[t])) { - printf("date_time %s expected %s\n", date_time, expected_date_time[t]); + printf("date_time %s \nexpected %s\n", date_time, expected_date_time[t]); return G2C_ERROR; } } From 88a0d8fd481aca3cf697379c6c513c553b17b31d Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Mon, 23 Oct 2023 11:01:26 -0600 Subject: [PATCH 2/8] more improvement of g2cdegrib2, testing --- src/g2cdegrib2.c | 10 +++++++++- tests/tst_degrib2_ftp_all.c | 11 +++++++---- tests/tst_degrib2_int.c | 16 ++++++++-------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/g2cdegrib2.c b/src/g2cdegrib2.c index 696cbade..1c12e0d9 100644 --- a/src/g2cdegrib2.c +++ b/src/g2cdegrib2.c @@ -251,6 +251,8 @@ g2c_get_datetime(int ipdtn, long long int *ipdtmpl, short year, unsigned char mo static int format_level(char *cbuf, int ival, int scale) { + char tmpcbuf[37]; + assert(cbuf); if (!scale) @@ -270,7 +272,8 @@ format_level(char *cbuf, int ival, int scale) { char fmt[37]; sprintf(fmt, "%s%d%s", "%.", (int)abs(scale), "f"); - sprintf(cbuf, fmt, rval); + sprintf(tmpcbuf, fmt, rval); + strcpy(cbuf, &tmpcbuf[1]); } } @@ -412,6 +415,11 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) /* Depth Below Land Sfc Layer. */ else if (ipdtmpl[ipos] == 106 && ipdtmpl[ipos + 3] == 106) { + if ((ret = format_level(tmpval1, ipdtmpl[ipos + 2], ipdtmpl[ipos + 1]))) + return ret; + if ((ret = format_level(tmpval2, ipdtmpl[ipos + 5], ipdtmpl[ipos + 4]))) + return ret; + sprintf(level_desc, "%s - %s m DBLY", tmpval1, tmpval2); /* call frmt(tmpval1, ipdtmpl(ipos + 2), ipdtmpl(ipos + 1)) */ /* call frmt(tmpval2, ipdtmpl(ipos + 5), ipdtmpl(ipos + 4)) */ /* level_desc = trim(tmpval1)//" - "//trim(tmpval2)//" m DBLY" */ diff --git a/tests/tst_degrib2_ftp_all.c b/tests/tst_degrib2_ftp_all.c index c3c6f480..71de8bb5 100644 --- a/tests/tst_degrib2_ftp_all.c +++ b/tests/tst_degrib2_ftp_all.c @@ -39,7 +39,7 @@ main() for (t = 0; t < 1; t++) { char degrib2_file[MAX_FILENAME_LEN + 9]; - /* char ref_degrib2_file[MAX_FILENAME_LEN + 20]; */ + char ref_degrib2_file[MAX_FILENAME_LEN + 20]; /* Open the data file with and without the index file. */ if (t) @@ -73,9 +73,12 @@ main() return ret; /* Compare the degrib2 output to our reference file. */ - /* sprintf(ref_degrib2_file, "data/ref_%s.degrib2", basename(file[f])); */ - /* if ((ret = compare_degrib2_files2(degrib2_file, ref_degrib2_file))) */ - /* return ret; */ + sprintf(ref_degrib2_file, "data/ref_%s.degrib2", basename(file[f])); + if ((ret = compare_degrib2_files2(degrib2_file, ref_degrib2_file))) + { + printf("compare failed!\n"); + /* return ret; */ + } } printf("\tok!\n"); } diff --git a/tests/tst_degrib2_int.c b/tests/tst_degrib2_int.c index 47cbd440..b482c3b8 100644 --- a/tests/tst_degrib2_int.c +++ b/tests/tst_degrib2_int.c @@ -30,20 +30,20 @@ main() " Surface", " 800 mb", "2 m above ground", " 400 - 300 mb", " Surface", "0 - .1 m DBLY", " 0 - .10 m DBLY"}; char level_desc[G2C_MAX_GRIB_LEVEL_DESC_LEN + 1]; - short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022}; - unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11}; - unsigned char day[NUM_PROD_TEST] = {30, 6, 6, 6, 17}; - unsigned char hour[NUM_PROD_TEST] = {0, 12, 12, 12, 19}; - unsigned char minute[NUM_PROD_TEST] = {0, 0, 0, 0, 0}; - unsigned char second[NUM_PROD_TEST] = {0, 0, 0, 0, 0}; + short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022, 2022, 2022}; + unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11, 11, 11}; + unsigned char day[NUM_PROD_TEST] = {30, 6, 6, 6, 17, 17, 17}; + unsigned char hour[NUM_PROD_TEST] = {0, 12, 12, 12, 19, 12, 12}; + unsigned char minute[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0}; + unsigned char second[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0}; char expected_date_time[NUM_PROD_TEST][DATE_TIME_LEN + 1] = { "valid 0 hour after 2021113000:00:00", "valid 0 hour after 2022110612:00:00", "valid 1 hour after 2022110612:00:00", "valid 1 hour after 2022110612:00:00", "(0 -1 hr) valid 0 hour after 2022111719:00:00 to 2022111720:00:00", - "valid 0 hour after 2022111719:00:00", - "valid 0 hour after 2022111719:00:00" + "valid 0 hour after 2022111712:00:00", + "valid 0 hour after 2022111712:00:00" }; int t; int ret; From fd6d96a3304b75fce19f950a5e5db77c1e2e8840 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Mon, 23 Oct 2023 15:13:41 -0600 Subject: [PATCH 3/8] more tests --- src/g2cdegrib2.c | 2 +- tests/tst_degrib2_int.c | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/g2cdegrib2.c b/src/g2cdegrib2.c index 1c12e0d9..4ed43241 100644 --- a/src/g2cdegrib2.c +++ b/src/g2cdegrib2.c @@ -356,7 +356,7 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) } else if (ipdtmpl[ipos] == 101) { - strcpy(level_desc, "Mean Sea Level "); + strcpy(level_desc, " Mean Sea Level"); } /* Altitude above MSL. */ else if (ipdtmpl[ipos] == 102 && ipdtmpl[ipos + 3] == 255) diff --git a/tests/tst_degrib2_int.c b/tests/tst_degrib2_int.c index b482c3b8..4621ce15 100644 --- a/tests/tst_degrib2_int.c +++ b/tests/tst_degrib2_int.c @@ -7,7 +7,7 @@ */ #include "grib2_int.h" -#define NUM_PROD_TEST 7 +#define NUM_PROD_TEST 8 int main() @@ -16,7 +16,7 @@ main() printf("Testing g2c_get_level_desc()...\n"); { #define DATE_TIME_LEN 100 - int prod_template_num[NUM_PROD_TEST] = {0, 0, 0, 0, 8, 2, 0}; + int prod_template_num[NUM_PROD_TEST] = {0, 0, 0, 0, 8, 2, 0, 2}; long long int prod_template_value[NUM_PROD_TEST][G2C_MAX_PDS_TEMPLATE_MAPLEN] = { {2, 1, 2, 0, 11, 0, 0, 1, 0, 1, 0, 1, 255, 0, 0}, {2, 10, 0, 0, 81, 0, 0, 1, 0, 100, 0, 80000, 255, 0, 0}, @@ -24,18 +24,19 @@ main() {19, 238, 2, 255, 104, 65535, 255, 1, 1, 100, 0, 40000, 100, 0, 30000}, {1, 228, 2, 255, 104, 65535, 255, 1, 0, 1, 0, 0, 255, 0, 0, 2022, 11, 17, 20, 0, 0, 1, 0, 1, 2, 1, 1, 1, 0}, {0, 192, 4, 70, 70, 0, 0, 1, 0, 106, 0, 0, 106, 1, 1, 0, 20}, - {0, 192, 2, 0, 98, 0, 0, 1, 0, 106, 2, 0, 106, 2, 10} + {0, 192, 2, 0, 98, 0, 0, 1, 0, 106, 2, 0, 106, 2, 10}, + {3, 1, 4, 70, 70, 0, 0, 1, 0, 101, 0, 0, 255, 0, 0, 0, 20} }; char expected_level_desc[NUM_PROD_TEST][G2C_MAX_GRIB_LEVEL_DESC_LEN + 1] = { " Surface", " 800 mb", "2 m above ground", " 400 - 300 mb", - " Surface", "0 - .1 m DBLY", " 0 - .10 m DBLY"}; + " Surface", "0 - .1 m DBLY", " 0 - .10 m DBLY", " Mean Sea Level"}; char level_desc[G2C_MAX_GRIB_LEVEL_DESC_LEN + 1]; - short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022, 2022, 2022}; - unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11, 11, 11}; - unsigned char day[NUM_PROD_TEST] = {30, 6, 6, 6, 17, 17, 17}; - unsigned char hour[NUM_PROD_TEST] = {0, 12, 12, 12, 19, 12, 12}; - unsigned char minute[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0}; - unsigned char second[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0}; + short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022}; + unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11, 11, 11, 11}; + unsigned char day[NUM_PROD_TEST] = {30, 6, 6, 6, 17, 17, 17, 17}; + unsigned char hour[NUM_PROD_TEST] = {0, 12, 12, 12, 19, 12, 12, 12}; + unsigned char minute[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0}; + unsigned char second[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0}; char expected_date_time[NUM_PROD_TEST][DATE_TIME_LEN + 1] = { "valid 0 hour after 2021113000:00:00", "valid 0 hour after 2022110612:00:00", @@ -43,6 +44,7 @@ main() "valid 1 hour after 2022110612:00:00", "(0 -1 hr) valid 0 hour after 2022111719:00:00 to 2022111720:00:00", "valid 0 hour after 2022111712:00:00", + "valid 0 hour after 2022111712:00:00", "valid 0 hour after 2022111712:00:00" }; int t; @@ -57,7 +59,7 @@ main() return ret; if (strcmp(level_desc, expected_level_desc[t])) { - printf("level_desc %s \nexpected %s\n", level_desc, expected_level_desc[t]); + printf("level_desc |%s| \nexpected |%s|\n", level_desc, expected_level_desc[t]); return G2C_ERROR; } From 5fbcef102d080f99d80d81fffd80441ba291cb69 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Mon, 23 Oct 2023 15:35:57 -0600 Subject: [PATCH 4/8] more handling of level descriptions --- src/g2cdegrib2.c | 1 + tests/tst_degrib2_int.c | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/g2cdegrib2.c b/src/g2cdegrib2.c index 4ed43241..d8190731 100644 --- a/src/g2cdegrib2.c +++ b/src/g2cdegrib2.c @@ -572,6 +572,7 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) strcpy(level_desc, " highest top lvl sup" ); else { + sprintf(level_desc, " %d (Unknown Lvl)", (int)ipdtmpl[ipos]); /* write(level_desc, fmt = '(1x,I4," (Unknown Lvl)")') ipdtmpl[ipos] */ } diff --git a/tests/tst_degrib2_int.c b/tests/tst_degrib2_int.c index 4621ce15..f685d7e1 100644 --- a/tests/tst_degrib2_int.c +++ b/tests/tst_degrib2_int.c @@ -7,7 +7,7 @@ */ #include "grib2_int.h" -#define NUM_PROD_TEST 8 +#define NUM_PROD_TEST 9 int main() @@ -16,7 +16,7 @@ main() printf("Testing g2c_get_level_desc()...\n"); { #define DATE_TIME_LEN 100 - int prod_template_num[NUM_PROD_TEST] = {0, 0, 0, 0, 8, 2, 0, 2}; + int prod_template_num[NUM_PROD_TEST] = {0, 0, 0, 0, 8, 2, 0, 2, 15}; long long int prod_template_value[NUM_PROD_TEST][G2C_MAX_PDS_TEMPLATE_MAPLEN] = { {2, 1, 2, 0, 11, 0, 0, 1, 0, 1, 0, 1, 255, 0, 0}, {2, 10, 0, 0, 81, 0, 0, 1, 0, 100, 0, 80000, 255, 0, 0}, @@ -25,18 +25,19 @@ main() {1, 228, 2, 255, 104, 65535, 255, 1, 0, 1, 0, 0, 255, 0, 0, 2022, 11, 17, 20, 0, 0, 1, 0, 1, 2, 1, 1, 1, 0}, {0, 192, 4, 70, 70, 0, 0, 1, 0, 106, 0, 0, 106, 1, 1, 0, 20}, {0, 192, 2, 0, 98, 0, 0, 1, 0, 106, 2, 0, 106, 2, 10}, - {3, 1, 4, 70, 70, 0, 0, 1, 0, 101, 0, 0, 255, 0, 0, 0, 20} + {3, 1, 4, 70, 70, 0, 0, 1, 0, 101, 0, 0, 255, 0, 0, 0, 20}, + {0, 27, 2, 255, 104, 65535, 255, 1, 1, 103, 0, 610, 100, 0, 40000, 241, 241, 241} }; char expected_level_desc[NUM_PROD_TEST][G2C_MAX_GRIB_LEVEL_DESC_LEN + 1] = { " Surface", " 800 mb", "2 m above ground", " 400 - 300 mb", - " Surface", "0 - .1 m DBLY", " 0 - .10 m DBLY", " Mean Sea Level"}; + " Surface", "0 - .1 m DBLY", " 0 - .10 m DBLY", " Mean Sea Level", " 103 (Unknown Lvl)"}; char level_desc[G2C_MAX_GRIB_LEVEL_DESC_LEN + 1]; - short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022}; - unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11, 11, 11, 11}; - unsigned char day[NUM_PROD_TEST] = {30, 6, 6, 6, 17, 17, 17, 17}; - unsigned char hour[NUM_PROD_TEST] = {0, 12, 12, 12, 19, 12, 12, 12}; - unsigned char minute[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0}; - unsigned char second[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0}; + short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022}; + unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11, 11, 11, 11, 11}; + unsigned char day[NUM_PROD_TEST] = {30, 6, 6, 6, 17, 17, 17, 17, 17}; + unsigned char hour[NUM_PROD_TEST] = {0, 12, 12, 12, 19, 12, 12, 12, 19}; + unsigned char minute[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + unsigned char second[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; char expected_date_time[NUM_PROD_TEST][DATE_TIME_LEN + 1] = { "valid 0 hour after 2021113000:00:00", "valid 0 hour after 2022110612:00:00", @@ -45,7 +46,8 @@ main() "(0 -1 hr) valid 0 hour after 2022111719:00:00 to 2022111720:00:00", "valid 0 hour after 2022111712:00:00", "valid 0 hour after 2022111712:00:00", - "valid 0 hour after 2022111712:00:00" + "valid 0 hour after 2022111712:00:00", + "valid 1 hour after 2022111719:00:00" }; int t; int ret; From 08535615f78973bd69488bb51c5002fa02bca834 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Mon, 23 Oct 2023 16:03:48 -0600 Subject: [PATCH 5/8] more testing, better handling of degrib2 level --- src/g2cdegrib2.c | 5 ++++- tests/tst_degrib2_int.c | 25 +++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/g2cdegrib2.c b/src/g2cdegrib2.c index d8190731..f6b821e1 100644 --- a/src/g2cdegrib2.c +++ b/src/g2cdegrib2.c @@ -157,7 +157,7 @@ g2c_get_datetime(int ipdtn, long long int *ipdtmpl, short year, unsigned char mo } /* Determine second unit of time range. */ - iutpos2 = ipos2[ipdtn + 1]; + iutpos2 = ipos2[ipdtn - 1]; switch (ipdtmpl[iutpos2]) { case 0: @@ -361,6 +361,9 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) /* Altitude above MSL. */ else if (ipdtmpl[ipos] == 102 && ipdtmpl[ipos + 3] == 255) { + if ((ret = format_level(tmpval1, ipdtmpl[ipos + 2], ipdtmpl[ipos + 1]))) + return ret; + sprintf(level_desc, "%s %s", tmpval1, "m above MSL"); /* call frmt(tmpval1, ipdtmpl(ipos + 2), ipdtmpl(ipos + 1)) */ /* level_desc = trim(tmpval1)//" m above MSL" */ } diff --git a/tests/tst_degrib2_int.c b/tests/tst_degrib2_int.c index f685d7e1..4ba3876e 100644 --- a/tests/tst_degrib2_int.c +++ b/tests/tst_degrib2_int.c @@ -7,7 +7,7 @@ */ #include "grib2_int.h" -#define NUM_PROD_TEST 9 +#define NUM_PROD_TEST 11 int main() @@ -16,7 +16,7 @@ main() printf("Testing g2c_get_level_desc()...\n"); { #define DATE_TIME_LEN 100 - int prod_template_num[NUM_PROD_TEST] = {0, 0, 0, 0, 8, 2, 0, 2, 15}; + int prod_template_num[NUM_PROD_TEST] = {0, 0, 0, 0, 8, 2, 0, 2, 15, 9, 0}; long long int prod_template_value[NUM_PROD_TEST][G2C_MAX_PDS_TEMPLATE_MAPLEN] = { {2, 1, 2, 0, 11, 0, 0, 1, 0, 1, 0, 1, 255, 0, 0}, {2, 10, 0, 0, 81, 0, 0, 1, 0, 100, 0, 80000, 255, 0, 0}, @@ -26,18 +26,21 @@ main() {0, 192, 4, 70, 70, 0, 0, 1, 0, 106, 0, 0, 106, 1, 1, 0, 20}, {0, 192, 2, 0, 98, 0, 0, 1, 0, 106, 2, 0, 106, 2, 10}, {3, 1, 4, 70, 70, 0, 0, 1, 0, 101, 0, 0, 255, 0, 0, 0, 20}, - {0, 27, 2, 255, 104, 65535, 255, 1, 1, 103, 0, 610, 100, 0, 40000, 241, 241, 241} + {0, 27, 2, 255, 104, 65535, 255, 1, 1, 103, 0, 610, 100, 0, 40000, 241, 241, 241}, + {1, 8, 2, 255, 104, 65535, 255, 1, 0, 1, 0, 0, 255, 0, 0, 255, 255, 1, -127, 255, 3, 254, 2022, 11, 17, 20, 0, 0, 1, 0, 1, 2, 1, 1, 1, 0}, + {19, 236, 2, 255, 104, 65535, 255, 1, 1, 102, 0, 0, 255, 0, 0} }; char expected_level_desc[NUM_PROD_TEST][G2C_MAX_GRIB_LEVEL_DESC_LEN + 1] = { " Surface", " 800 mb", "2 m above ground", " 400 - 300 mb", - " Surface", "0 - .1 m DBLY", " 0 - .10 m DBLY", " Mean Sea Level", " 103 (Unknown Lvl)"}; + " Surface", "0 - .1 m DBLY", " 0 - .10 m DBLY", " Mean Sea Level", " 103 (Unknown Lvl)", + " Surface", "0 m above MSL"}; char level_desc[G2C_MAX_GRIB_LEVEL_DESC_LEN + 1]; - short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022}; - unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11, 11, 11, 11, 11}; - unsigned char day[NUM_PROD_TEST] = {30, 6, 6, 6, 17, 17, 17, 17, 17}; - unsigned char hour[NUM_PROD_TEST] = {0, 12, 12, 12, 19, 12, 12, 12, 19}; - unsigned char minute[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; - unsigned char second[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022}; + unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}; + unsigned char day[NUM_PROD_TEST] = {30, 6, 6, 6, 17, 17, 17, 17, 17, 17, 17}; + unsigned char hour[NUM_PROD_TEST] = {0, 12, 12, 12, 19, 12, 12, 12, 19, 19, 19}; + unsigned char minute[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + unsigned char second[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; char expected_date_time[NUM_PROD_TEST][DATE_TIME_LEN + 1] = { "valid 0 hour after 2021113000:00:00", "valid 0 hour after 2022110612:00:00", @@ -47,6 +50,8 @@ main() "valid 0 hour after 2022111712:00:00", "valid 0 hour after 2022111712:00:00", "valid 0 hour after 2022111712:00:00", + "valid 1 hour after 2022111719:00:00", + "(0 -1 hr) valid 0 hour after 2022111719:00:00 to 2022111720:00:00", "valid 1 hour after 2022111719:00:00" }; int t; From 049b9bb2fecbdeddab7e670cb643e7faa9a2cca9 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Mon, 23 Oct 2023 16:09:11 -0600 Subject: [PATCH 6/8] improve g2cdegrib2 level handling --- src/g2cdegrib2.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/g2cdegrib2.c b/src/g2cdegrib2.c index f6b821e1..12f1b9d9 100644 --- a/src/g2cdegrib2.c +++ b/src/g2cdegrib2.c @@ -379,6 +379,11 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) /* Height above Ground. */ else if (ipdtmpl[ipos] == 103 && ipdtmpl[ipos + 3] == 103) { + if ((ret = format_level(tmpval1, ipdtmpl[ipos + 2], ipdtmpl[ipos + 1]))) + return ret; + if ((ret = format_level(tmpval2, ipdtmpl[ipos + 5], ipdtmpl[ipos + 4]))) + return ret; + sprintf(level_desc, "%s - %s m AGL", tmpval1, tmpval2); /* call frmt(tmpval1, ipdtmpl(ipos + 2), ipdtmpl(ipos + 1)) */ /* call frmt(tmpval2, ipdtmpl(ipos + 5), ipdtmpl(ipos + 4)) */ /* level_desc = trim(tmpval1)//" - "//trim(tmpval2)//" m AGL" */ @@ -386,12 +391,20 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) /* Sigma Level. */ else if (ipdtmpl[ipos] == 104 && ipdtmpl[ipos + 3] == 255) { + if ((ret = format_level(tmpval1, ipdtmpl[ipos + 2], ipdtmpl[ipos + 1]))) + return ret; + sprintf(level_desc, "%s %s", tmpval1, "sigma"); /* call frmt(tmpval1, ipdtmpl(ipos + 2), ipdtmpl(ipos + 1)) */ /* level_desc = trim(tmpval1)//" sigma" */ } /* Sigma Layer. */ else if (ipdtmpl[ipos] == 104 && ipdtmpl[ipos + 3] == 104) { + if ((ret = format_level(tmpval1, ipdtmpl[ipos + 2], ipdtmpl[ipos + 1]))) + return ret; + if ((ret = format_level(tmpval2, ipdtmpl[ipos + 5], ipdtmpl[ipos + 4]))) + return ret; + sprintf(level_desc, "%s - %s sigma", tmpval1, tmpval2); /* call frmt(tmpval1, ipdtmpl(ipos + 2), ipdtmpl(ipos + 1)) */ /* call frmt(tmpval2, ipdtmpl(ipos + 5), ipdtmpl(ipos + 4)) */ /* level_desc = trim(tmpval1)//" - "//trim(tmpval2)//" sigma" */ @@ -399,12 +412,20 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) /* Hybrid Level. */ else if (ipdtmpl[ipos] == 105 && ipdtmpl[ipos + 3] == 255) { + if ((ret = format_level(tmpval1, ipdtmpl[ipos + 2], ipdtmpl[ipos + 1]))) + return ret; + sprintf(level_desc, "%s %s", tmpval1, "hybrid lvl"); /* call frmt(tmpval1, ipdtmpl(ipos + 2), ipdtmpl(ipos + 1)) */ /* level_desc = trim(tmpval1)//" hybrid lvl" */ } /* Hybrid Level. */ else if (ipdtmpl[ipos] == 105 && ipdtmpl[ipos + 3] == 105) { + if ((ret = format_level(tmpval1, ipdtmpl[ipos + 2], ipdtmpl[ipos + 1]))) + return ret; + if ((ret = format_level(tmpval2, ipdtmpl[ipos + 5], ipdtmpl[ipos + 4]))) + return ret; + sprintf(level_desc, "%s - %s hybrid lvl", tmpval1, tmpval2); /* call frmt(tmpval1, ipdtmpl(ipos + 2), ipdtmpl(ipos + 1)) */ /* call frmt(tmpval2, ipdtmpl(ipos + 5), ipdtmpl(ipos + 4)) */ /* level_desc = trim(tmpval1)//" - "//trim(tmpval2)//" hybrid lvl" */ @@ -412,6 +433,9 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) /* Depth Below Land Sfc. */ else if (ipdtmpl[ipos] == 106 && ipdtmpl[ipos + 3] == 255) { + if ((ret = format_level(tmpval1, ipdtmpl[ipos + 2], ipdtmpl[ipos + 1]))) + return ret; + sprintf(level_desc, "%s %s", tmpval1, "m below land"); /* call frmt(tmpval1, ipdtmpl(ipos + 2), ipdtmpl(ipos + 1)) */ /* level_desc = trim(tmpval1)//" m below land" */ } @@ -447,6 +471,9 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) /* Potential Vorticity Sfc. */ else if (ipdtmpl[ipos] == 109 && ipdtmpl[ipos + 3] == 255) { + if ((ret = format_level(tmpval1, ipdtmpl[ipos + 2], ipdtmpl[ipos + 1]))) + return ret; + sprintf(level_desc, "%s %s", tmpval1, "pv surface"); /* write(tmpval1, *) ipdtmpl(ipos + 2). */ /* call frmt(tmpval1, ipdtmpl(ipos + 2), ipdtmpl(ipos + 1)-6) */ /* level_desc = trim(tmpval1)//" pv surface" */ From 2f5a86d3b3cd1db1a8bccafcd0b80741dc9911e0 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Mon, 23 Oct 2023 16:42:13 -0600 Subject: [PATCH 7/8] more g2cdegrib2 fixes and tests --- src/g2cdegrib2.c | 19 ++++++++++++++----- tests/tst_degrib2_ftp_all.c | 2 +- tests/tst_degrib2_int.c | 24 +++++++++++++----------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/g2cdegrib2.c b/src/g2cdegrib2.c index 12f1b9d9..903adfcc 100644 --- a/src/g2cdegrib2.c +++ b/src/g2cdegrib2.c @@ -458,6 +458,11 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) /* Press Diff from Ground Layer. */ else if (ipdtmpl[ipos] == 108 && ipdtmpl[ipos + 3] == 108) { + if ((ret = format_level(tmpval1, ipdtmpl[ipos + 2], ipdtmpl[ipos + 1] + 2))) + return ret; + if ((ret = format_level(tmpval2, ipdtmpl[ipos + 5], ipdtmpl[ipos + 4] + 2))) + return ret; + sprintf(level_desc, "%s - %s mb SPDY", tmpval1, tmpval2); /* write(tmpval1, *) ipdtmpl(ipos + 2)/100. */ /* write(tmpval2, *) ipdtmpl(ipos + 5)/100. */ /* call frmt(tmpval1, ipdtmpl(ipos + 2), ipdtmpl(ipos + 1) + 2) */ @@ -602,7 +607,7 @@ g2c_get_level_desc(int ipdtn, long long int *ipdtmpl, char *level_desc) strcpy(level_desc, " highest top lvl sup" ); else { - sprintf(level_desc, " %d (Unknown Lvl)", (int)ipdtmpl[ipos]); + sprintf(level_desc, " %4d (Unknown Lvl)", (int)ipdtmpl[ipos]); /* write(level_desc, fmt = '(1x,I4," (Unknown Lvl)")') ipdtmpl[ipos] */ } @@ -679,10 +684,14 @@ g2c_degrib2(int g2cid, const char *fileout) fprintf(f, "\n"); fprintf(f, " FIELD %d\n", fld + 1); - fprintf(f, " SECTION 0: %d 2\n", msg->discipline); - fprintf(f, " SECTION 1: %d %d %d %d %d %d %d %d %d %d %d %d %d\n", msg->center, msg->subcenter, - msg->master_version, msg->local_version, msg->sig_ref_time, msg->year, msg->month, msg->day, - msg->hour, msg->minute, msg->second, msg->status, msg->type); + /* Only print section 0 and 1 data for the first field. */ + if (fld == 0) + { + fprintf(f, " SECTION 0: %d 2\n", msg->discipline); + fprintf(f, " SECTION 1: %d %d %d %d %d %d %d %d %d %d %d %d %d\n", msg->center, msg->subcenter, + msg->master_version, msg->local_version, msg->sig_ref_time, msg->year, msg->month, msg->day, + msg->hour, msg->minute, msg->second, msg->status, msg->type); + } /* Find this field (a.k.a. product, a.k.a. section 4). */ for (sec = msg->sec; sec; sec = sec->next) diff --git a/tests/tst_degrib2_ftp_all.c b/tests/tst_degrib2_ftp_all.c index 71de8bb5..9867798a 100644 --- a/tests/tst_degrib2_ftp_all.c +++ b/tests/tst_degrib2_ftp_all.c @@ -77,7 +77,7 @@ main() if ((ret = compare_degrib2_files2(degrib2_file, ref_degrib2_file))) { printf("compare failed!\n"); - /* return ret; */ + return ret; } } printf("\tok!\n"); diff --git a/tests/tst_degrib2_int.c b/tests/tst_degrib2_int.c index 4ba3876e..36f41539 100644 --- a/tests/tst_degrib2_int.c +++ b/tests/tst_degrib2_int.c @@ -7,7 +7,7 @@ */ #include "grib2_int.h" -#define NUM_PROD_TEST 11 +#define NUM_PROD_TEST 12 int main() @@ -16,7 +16,7 @@ main() printf("Testing g2c_get_level_desc()...\n"); { #define DATE_TIME_LEN 100 - int prod_template_num[NUM_PROD_TEST] = {0, 0, 0, 0, 8, 2, 0, 2, 15, 9, 0}; + int prod_template_num[NUM_PROD_TEST] = {0, 0, 0, 0, 8, 2, 0, 2, 15, 9, 0, 0}; long long int prod_template_value[NUM_PROD_TEST][G2C_MAX_PDS_TEMPLATE_MAPLEN] = { {2, 1, 2, 0, 11, 0, 0, 1, 0, 1, 0, 1, 255, 0, 0}, {2, 10, 0, 0, 81, 0, 0, 1, 0, 100, 0, 80000, 255, 0, 0}, @@ -28,19 +28,20 @@ main() {3, 1, 4, 70, 70, 0, 0, 1, 0, 101, 0, 0, 255, 0, 0, 0, 20}, {0, 27, 2, 255, 104, 65535, 255, 1, 1, 103, 0, 610, 100, 0, 40000, 241, 241, 241}, {1, 8, 2, 255, 104, 65535, 255, 1, 0, 1, 0, 0, 255, 0, 0, 255, 255, 1, -127, 255, 3, 254, 2022, 11, 17, 20, 0, 0, 1, 0, 1, 2, 1, 1, 1, 0}, - {19, 236, 2, 255, 104, 65535, 255, 1, 1, 102, 0, 0, 255, 0, 0} + {19, 236, 2, 255, 104, 65535, 255, 1, 1, 102, 0, 0, 255, 0, 0}, + {0, 0, 2, 0, 116, 0, 0, 1, 0, 108, 0, 3000, 108, 0, 0} }; char expected_level_desc[NUM_PROD_TEST][G2C_MAX_GRIB_LEVEL_DESC_LEN + 1] = { " Surface", " 800 mb", "2 m above ground", " 400 - 300 mb", " Surface", "0 - .1 m DBLY", " 0 - .10 m DBLY", " Mean Sea Level", " 103 (Unknown Lvl)", - " Surface", "0 m above MSL"}; + " Surface", "0 m above MSL", " 30 - 0 mb SPDY"}; char level_desc[G2C_MAX_GRIB_LEVEL_DESC_LEN + 1]; - short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022}; - unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}; - unsigned char day[NUM_PROD_TEST] = {30, 6, 6, 6, 17, 17, 17, 17, 17, 17, 17}; - unsigned char hour[NUM_PROD_TEST] = {0, 12, 12, 12, 19, 12, 12, 12, 19, 19, 19}; - unsigned char minute[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - unsigned char second[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + short year[NUM_PROD_TEST] = {2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022}; + unsigned char month[NUM_PROD_TEST] = {11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}; + unsigned char day[NUM_PROD_TEST] = {30, 6, 6, 6, 17, 17, 17, 17, 17, 17, 17, 17}; + unsigned char hour[NUM_PROD_TEST] = {0, 12, 12, 12, 19, 12, 12, 12, 19, 19, 19, 0}; + unsigned char minute[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + unsigned char second[NUM_PROD_TEST] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; char expected_date_time[NUM_PROD_TEST][DATE_TIME_LEN + 1] = { "valid 0 hour after 2021113000:00:00", "valid 0 hour after 2022110612:00:00", @@ -52,7 +53,8 @@ main() "valid 0 hour after 2022111712:00:00", "valid 1 hour after 2022111719:00:00", "(0 -1 hr) valid 0 hour after 2022111719:00:00 to 2022111720:00:00", - "valid 1 hour after 2022111719:00:00" + "valid 1 hour after 2022111719:00:00", + "valid 0 hour after 2022111700:00:00" }; int t; int ret; From 20f8d4aa22fbc530cd432a13e7949fa9c1d8d043 Mon Sep 17 00:00:00 2001 From: Edward Hartnett Date: Mon, 23 Oct 2023 16:47:50 -0600 Subject: [PATCH 8/8] fixed memory problem --- src/g2cdegrib2.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/g2cdegrib2.c b/src/g2cdegrib2.c index 903adfcc..fa6b1305 100644 --- a/src/g2cdegrib2.c +++ b/src/g2cdegrib2.c @@ -157,7 +157,10 @@ g2c_get_datetime(int ipdtn, long long int *ipdtmpl, short year, unsigned char mo } /* Determine second unit of time range. */ - iutpos2 = ipos2[ipdtn - 1]; + if (ipdtn > 0) + iutpos2 = ipos2[ipdtn - 1]; + else + iutpos2 = 0; switch (ipdtmpl[iutpos2]) { case 0: