Merge branch 'feature/ExportResource' into develop

This commit is contained in:
Shahram Najm 2022-03-12 20:00:33 +00:00
commit 1141ac065c
6 changed files with 177 additions and 4 deletions

View File

@ -11,7 +11,7 @@
#include "grib_api_internal.h"
#ifdef HAVE_MEMFS
/* These two functions are implemented in the generated C file memfs.c in the build area */
/* These two functions are implemented in the generated C file memfs_gen_final.c in the build area */
/* See the memfs.py Python generator */
int codes_memfs_exists(const char* path);
FILE* codes_memfs_open(const char* path);
@ -20,7 +20,7 @@ FILE* codes_fopen(const char* name, const char* mode)
{
FILE* f;
if (strcmp(mode, "r") != 0) {
if (strcmp(mode, "r") != 0) { /* Not reading */
return fopen(name, mode);
}
@ -34,6 +34,7 @@ FILE* codes_fopen(const char* name, const char* mode)
int codes_access(const char* name, int mode)
{
/* F_OK tests for the existence of the file */
if (mode != F_OK) {
return access(name, mode);
}
@ -46,7 +47,7 @@ int codes_access(const char* name, int mode)
}
#else
/* No MEMFS */
FILE* codes_fopen(const char* name, const char* mode)
{
return fopen(name, mode);

View File

@ -111,7 +111,7 @@ static char* try_template_path(grib_context* c, const char* dir, const char* nam
sprintf(path, "%s/%s.tmpl", dir, name);
if (codes_access(path, R_OK) == 0) {
if (codes_access(path, F_OK) == 0) {
return grib_context_strdup(c, path);
}

View File

@ -400,6 +400,12 @@ if( HAVE_BUILD_TOOLS )
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/bufr_split_by_rdbSubtype.sh
TEST_DEPENDS eccodes_download_bufrs )
ecbuild_add_test( TARGET eccodes_t_codes_export_resource
TYPE SCRIPT
CONDITION ECCODES_INSTALL_EXTRA_TOOLS
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/codes_export_resource.sh )
# Performance test. Must have -DENABLE_TIMER=ON
ecbuild_add_test( TARGET eccodes_t_grib_ecc-386
TYPE SCRIPT

60
tests/codes_export_resource.sh Executable file
View File

@ -0,0 +1,60 @@
#!/bin/sh
# (C) Copyright 2005- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
#
. ./include.sh
label="codes_export_resource_test"
temp=temp.$label
# Sample files
# ----------------
f='GRIB2.tmpl'
${tools_dir}/codes_export_resource -s $f $temp
cmp $ECCODES_SAMPLES_PATH/$f $temp
# Use the shortened form
${tools_dir}/codes_export_resource -s GRIB2 $temp
cmp $ECCODES_SAMPLES_PATH/GRIB2.tmpl $temp
# IFS Samples
# ----------------
if [ $HAVE_MEMFS -eq 1 ]; then
ECCODES_SAMPLES_PATH=/MEMFS/ifs_samples/grib1_mlgrib2 ${tools_dir}/codes_export_resource -s gg_ml.tmpl $temp
fi
# Definition files
# ----------------
f='boot.def'
${tools_dir}/codes_export_resource -d $f $temp
cmp $ECCODES_DEFINITION_PATH/$f $temp
f='common/c-11.table'
${tools_dir}/codes_export_resource -d $f $temp
cmp $ECCODES_DEFINITION_PATH/$f $temp
# Failing cases
# ----------------
set +e
${tools_dir}/codes_export_resource -d nonexistent $temp
status=$?
set -e
[ $status -eq 1 ]
set +e
${tools_dir}/codes_export_resource -s nonexistent $temp
status=$?
set -e
[ $status -eq 1 ]
# Clean up
rm -f $temp

View File

@ -41,6 +41,7 @@ list( APPEND ecc_tools_binaries_extra
gg_sub_area_check
grib_repair
grib_to_json
codes_export_resource
grib_check_gaussian_grid
bufr_split_by_rdbSubtype )

View File

@ -0,0 +1,105 @@
/*
* (C) Copyright 2005- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
#include "grib_api_internal.h"
typedef enum ResourceType
{
UNKNOWN,
SAMPLE,
DEFINITION
} ResourceType;
static void usage(const char* prog)
{
printf("Usage: %s [-s | -d] resource_path out_file\n", prog);
exit(1);
}
#define SIZE (1024 * 1024)
int main(int argc, char* argv[])
{
char* resource_path = NULL;
char* resource_name = NULL;
ResourceType resource_type = UNKNOWN;
char* full_path = NULL;
char* out_file = NULL;
char* option = NULL;
grib_context* c = grib_context_get_default();
FILE* fin = NULL;
FILE* fout = NULL;
char buffer[SIZE] = {0,};
size_t bytes = 0;
if (argc != 4) usage(argv[0]);
option = argv[1];
resource_path = argv[2];
out_file = argv[3];
if (strcmp(option, "-s") == 0) {
resource_type = SAMPLE;
resource_name = "sample";
}
else if (strcmp(option, "-d") == 0) {
resource_type = DEFINITION;
resource_name = "definition";
}
else {
fprintf(stderr, "Invalid option: Specify either -s or -d\n");
return 1;
}
if (resource_type == SAMPLE) {
char* t = strstr(resource_path, ".tmpl");
if (t) {
*t = '\0'; // get rid of sample file extension (if there)
}
full_path = grib_external_template_path(c, resource_path);
}
else if (resource_type == DEFINITION) {
full_path = grib_context_full_defs_path(c, resource_path);
}
if (!full_path) {
fprintf(stderr, "Failed to access %s: '%s'\n", resource_name, resource_path);
return 1;
}
fout = fopen(out_file, "wb");
if (!fout) {
fprintf(stderr, "Failed to open output file '%s'\n", out_file);
return 1;
}
fin = codes_fopen(full_path, "r");
if (!fin) {
fprintf(stderr, "Failed to open resource '%s'\n", full_path);
return 1;
}
/* write resource bytes to fout */
while (0 < (bytes = fread(buffer, 1, sizeof(buffer), fin))) {
if (fwrite(buffer, 1, bytes, fout) != bytes) {
fprintf(stderr, "Failed to write out bytes\n");
return 1;
}
}
if (fclose(fin) != 0) {
fprintf(stderr, "Call to fclose failed (input)\n");
return 1;
}
if (fclose(fout) != 0) {
fprintf(stderr, "Call to fclose failed (output)\n");
return 1;
}
grib_context_free(c, full_path);
return 0;
}