Skip to content

Commit

Permalink
committing progress on working code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlysonStahl-NOAA committed Aug 7, 2024
1 parent 53444de commit 99c9eeb
Show file tree
Hide file tree
Showing 8 changed files with 7,124 additions and 17 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ else()
endif()

# Find libxml2.
if(BUILD_G2C)
find_package(LibXml2 2.9.0 REQUIRED)
endif()
#if(BUILD_G2C)
# find_package(LibXml2 2.9.0 REQUIRED)
#endif()

# Set the compiler flags.
if(CMAKE_C_COMPILER_ID MATCHES "^(Intel|IntelLLVM)$")
Expand Down
10 changes: 6 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ reduce.c seekgb.c simpack.c simunpack.c specpack.c
specunpack.c util.c)

if(BUILD_G2C)
set(src_files ${src_files} g2cutil.c g2cxml.c g2cparams.c g2cfile.c
set(src_files ${src_files} g2cutil.c g2ccsv.c g2cparams.c g2cfile.c
g2cdegrib2.c g2cprod.c g2cinq.c g2cindex.c g2cio.c g2ccompare.c)
endif()

Expand Down Expand Up @@ -50,6 +50,7 @@ if(BUILD_SHARED_LIBS)
endif()

# Link to libxml2.
#[===[
if(BUILD_G2C)
message(STATUS "libxml2 include directories ${LIBXML2_INCLUDE_DIR}")
target_link_libraries(${lib_name}_objlib PRIVATE ${LIBXML2_LIBRARIES})
Expand All @@ -63,6 +64,7 @@ if(BUILD_G2C)
target_include_directories(${lib_name}_static PRIVATE "${LIBXML2_INCLUDE_DIR}")
endif()
endif()
]===]

# Build with PNG.
if(PNG_FOUND)
Expand Down Expand Up @@ -229,9 +231,9 @@ install(EXPORT ${PROJECT_NAME}Exports
FILE ${PROJECT_NAME}-targets.cmake
DESTINATION ${CONFIG_INSTALL_DESTINATION})

# XML data files.
# CSV data files.
if(BUILD_G2C)
set(XML_FILES Template.xml CodeFlag.xml)
install(FILES ${XML_FILES}
set(CSV_FILES Template.txt CodeFlag.txt)
install(FILES ${CSV_FILES}
DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
2,960 changes: 2,960 additions & 0 deletions src/CodeFlag.txt

Large diffs are not rendered by default.

3,862 changes: 3,862 additions & 0 deletions src/Template.txt

Large diffs are not rendered by default.

258 changes: 258 additions & 0 deletions src/g2ccsv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
/** @file
*
* @brief This file reads the GRIB2 CSV files.
* @author Ed Hartnett @date 8/25/22
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <grib2_int.h>

/** Contains the parsed CSV document. */
FILE* doc;

/** Pointer to the list of code tables. */
G2C_CODE_TABLE_T *g2c_table = NULL;

/** Print the table data.
*
* @author Ed Hartnett @date 8/28/22
*/
void
g2c_print_tables()
{
G2C_CODE_TABLE_T *t;

for (t = g2c_table; t; t = t->next)
{
G2C_CODE_ENTRY_T *e;

printf("%s\n", t->title);
for (e = t->entry; e; e = e->next)
printf("code %s desc %s status %s\n", e->code, e->desc, e->status);

}
}

/** Free table memory.
*
* @author Ed Hartnett @date 8/28/22
*/
void
g2c_free_tables()
{
G2C_CODE_TABLE_T *t, *the_next = NULL;

/* If g2c_table is NULL, then tables have already been
* freed. */
if (!g2c_table)
return;

/* Free each table. */
for (t = g2c_table; t; t = the_next)
{
G2C_CODE_ENTRY_T *e;
G2C_CODE_ENTRY_T *e_next;

/* Free each entry in the table. */
the_next = t->next;
for (e = t->entry; e; e = e_next)
{
e_next = e->next;
free(e);
}

free(t);
}

/* Set to NULL so we all know g2c_table has been freed. */
g2c_table = NULL;
}

/** Given a table title and a code, find a description.
*
* @param title Title of table.
* @param code Code to search for.
* @param desc Pointer that gets a copy of the description. Must be
* allocated to ::G2C_MAX_GRIB_DESC_LEN + 1.
*
* @author Ed Hartnett @date 8/28/22
*
* @return 0 for success, error code otherwise.
*/
int
g2c_find_desc_str(char *title, char *code, char *desc)
{
G2C_CODE_TABLE_T *t = NULL;
int found = 0;

/* Check inputs. */
if (!title || strlen(title) > G2C_MAX_GRIB_TITLE_LEN
|| !code || strlen(code) > G2C_MAX_GRIB_CODE_LEN || !desc)
return G2C_EINVAL;

/* Find table. */
for (t = g2c_table; !found && t; t = t->next)
{
if (!strncmp(title, t->title, strlen(title)))
{
G2C_CODE_ENTRY_T *e = NULL;

/* Find entry. */
for (e = t->entry; e; e = e->next)
{
if (!strncmp(code, e->code, strlen(code)))
{
strcpy(desc, e->desc);
found++;
break;
}
}
}
}

if (!found)
return G2C_ENOTFOUND;

return G2C_NOERROR;
}

/** Given a table title and an integer code, find a description.
*
* @param title Title of table.
* @param code Code to search for as an int.
* @param desc Pointer that gets a copy of the description. Must be
* allocated to ::G2C_MAX_GRIB_DESC_LEN + 1.
*
* @author Ed Hartnett @date 8/28/22
*
* @return 0 for success, error code otherwise.
*/
int
g2c_find_desc(char *title, int code, char *desc)
{
char str_code[G2C_MAX_GRIB_CODE_LEN + 1];

sprintf(str_code, "%d", code);
return g2c_find_desc_str(title, str_code, desc);
}

/** Find a table given a key.
*
* @param key The table title to find.
*
* @author Ed Hartnett @date 8/28/22
*
* @return a pointer to the matching code table, or NULL if not found.
*/
G2C_CODE_TABLE_T *
g2c_find_table(char *key)
{
G2C_CODE_TABLE_T *g;

for (g = g2c_table; g; g = g->next)
if (!strncmp(key, g->title, G2C_MAX_GRIB_TITLE_LEN))
return g;

return NULL;
}

/** Find an entry in a table given a description.
*
* @param desc The description of the entry to find.
* @param table A pointer to the table to search.
*
* @author Ed Hartnett @date 8/29/22
*
* @return a pointer to the matching entry, or NULL if not found.
*/
G2C_CODE_ENTRY_T *
g2c_find_entry(char *desc, G2C_CODE_TABLE_T *table)
{
G2C_CODE_ENTRY_T *e;

for (e = table->entry; e; e = e->next)
if (!strncmp(desc, e->desc, G2C_MAX_GRIB_DESC_LEN))
return e;

return NULL;
}

/**
* Init.
*
* @return
* - ::G2C_NOERROR No error.
*
* @author Alyson Stahl @date 8/2/24
*/
int
g2c_csv_init()
{
size_t max_line_size = 500;
const int num_columns = 9;
int i,j;
char *buf, *tmp, *key;
char line[max_line_size];
G2C_CODE_TABLE_T *my_table = NULL;
G2C_CODE_ENTRY_T *new_entry = NULL;

/* If g2c_table is not NULL, then tables have already been
* initialized. */
if (g2c_table)
return G2C_NOERROR;

/* Ingest the CSV document. */
if (!(doc = fopen("CodeFlag.txt", "r")))
return G2C_ECSV;

/* Skip header line */
fgets(line,max_line_size,doc);
j = 1;
while((fgets(line,max_line_size,doc)) != NULL && j < 2)
{
buf = strdup(line);
i = 0;
while(buf != NULL && i < num_columns)
{
G2C_CODE_TABLE_T *new_table = NULL;

if (*buf == '\"')
{
tmp = strsep(&buf,"\"");
tmp = strsep(&buf,"\"");
key = strdup((const char*)tmp);
tmp = strsep(&buf,",");
}
else
{
tmp = strsep(&buf,",");
key = strdup((const char*)tmp);
}

if (i==0)
{
if (strlen(key) > G2C_MAX_GRIB_TITLE_LEN)
return G2C_ENAMETOOLONG;
if (!(my_table = g2c_find_table(key)))
{
if (!(new_table = calloc(1,sizeof(G2C_CODE_TABLE_T))))
return G2C_ENOMEM;
strncpy(new_table->title, key,G2C_MAX_GRIB_TITLE_LEN);
my_table = new_table;
}
}

free(key);
i++;
}

j++;
}

fclose(doc);

return G2C_NOERROR;
}

1 change: 1 addition & 0 deletions src/grib2.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -515,5 +515,6 @@ int g2c_param_all(int param_idx, int *g1ver, int *g1val, int *g2disc, int *g2cat
#define G2C_ENOPRODUCT (-72) /**< Product not found. */
#define G2C_EBADTYPE (-73) /**< Type not found. */
#define G2C_EAEC (-74) /**< Error encoding/decoding AEC data. */
#define G2C_ECSV (-75) /**< CSV error. */

#endif /* _grib2_H */
20 changes: 10 additions & 10 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,30 @@ foreach(THE_FILE IN LISTS REF_FILES)
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data)
endforeach()

# These are the XML files from the WMO with GRIB2 code data.
# These are the CSV files from the WMO with GRIB2 code data.
if(BUILD_G2C)
FILE(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../src/CodeFlag.xml
FILE(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../src/CodeFlag.txt
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/)
FILE(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../src/Template.xml
FILE(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../src/Template.txt
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/)
endif()

# Build a C program test.
function(g2c_build_test name)
add_executable(${name} ${name}.c g2c_test_util.c)
target_link_libraries(${name} PUBLIC g2c::g2c)
if(BUILD_G2C)
target_link_libraries(${name} PRIVATE ${LIBXML2_LIBRARIES})
target_include_directories(${name} PRIVATE "${LIBXML2_INCLUDE_DIR}")
endif()
#if(BUILD_G2C)
# target_link_libraries(${name} PRIVATE ${LIBXML2_LIBRARIES})
# target_include_directories(${name} PRIVATE "${LIBXML2_INCLUDE_DIR}")
#endif()
endfunction()

# Build a C program test.
function(g2c_build_test name)
add_executable(${name} ${name}.c g2c_test_util.c)
target_link_libraries(${name} PUBLIC g2c::g2c)
target_link_libraries(${name} PRIVATE ${LIBXML2_LIBRARIES})
target_include_directories(${name} PRIVATE "${LIBXML2_INCLUDE_DIR}")
#target_link_libraries(${name} PRIVATE ${LIBXML2_LIBRARIES})
#target_include_directories(${name} PRIVATE "${LIBXML2_INCLUDE_DIR}")
endfunction()

# Run a C program test.
Expand Down Expand Up @@ -203,7 +203,7 @@ g2c_test(tst_seekgb)
# Run these tests if the g2c API has been built.
if(BUILD_G2C)
g2c_test(tst_index1)
g2c_test(tst_xml)
g2c_test(tst_csv)
g2c_test(tst_error)
g2c_test(tst_params)
g2c_test(tst_params2)
Expand Down
24 changes: 24 additions & 0 deletions tests/tst_csv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* This is a test for the NCEPLIBS-g2c project. This test is for
* g2ccsv.c.
*
* Ed Hartnett, Alyson Stahl 8/5/24
*/

#include <stdio.h>
#include <stdlib.h>
#include "grib2_int.h"

int
main()
{
int ret;
char desc[G2C_MAX_GRIB_DESC_LEN + 1];

printf("Testing CSV ingestion...\n");
if (g2c_csv_init())
return G2C_ERROR;

printf("SUCCESS!!!\n");
return G2C_NOERROR;
}

0 comments on commit 99c9eeb

Please sign in to comment.