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

more work on g2cdegrib2 handling of date/times and levels #454

Merged
merged 8 commits into from
Oct 23, 2023
Merged
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
66 changes: 58 additions & 8 deletions src/g2cdegrib2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -213,7 +216,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) */
Expand Down Expand Up @@ -252,6 +254,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)
Expand All @@ -271,7 +275,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]);
}

}
Expand Down Expand Up @@ -354,11 +359,14 @@ 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)
{
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" */
}
Expand All @@ -374,45 +382,74 @@ 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" */
}
/* 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" */
}
/* 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" */
}
/* 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" */
}
/* 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" */
Expand All @@ -424,6 +461,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) */
Expand All @@ -437,6 +479,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" */
Expand Down Expand Up @@ -565,6 +610,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, " %4d (Unknown Lvl)", (int)ipdtmpl[ipos]);
/* write(level_desc, fmt = '(1x,I4," (Unknown Lvl)")') ipdtmpl[ipos] */
}

Expand Down Expand Up @@ -641,10 +687,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)
Expand Down
11 changes: 7 additions & 4 deletions tests/tst_degrib2_ftp_all.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
}
Expand Down
43 changes: 29 additions & 14 deletions tests/tst_degrib2_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,54 @@
*/
#include "grib2_int.h"

#define NUM_PROD_TEST 5
#define NUM_PROD_TEST 12

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, 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},
{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},
{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},
{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"};
" Surface", "0 - .1 m DBLY", " 0 - .10 m DBLY", " Mean Sea Level", " 103 (Unknown Lvl)",
" 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};
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, 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",
"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 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",
"valid 0 hour after 2022111700:00:00"
};
int t;
int ret;
Expand All @@ -53,7 +68,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;
}

Expand All @@ -63,7 +78,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;
}
}
Expand Down
Loading