Make C, Fortran and Python examples ECC-15

This commit is contained in:
Sandor Kertesz 2015-02-03 16:31:51 +00:00
parent 4059749569
commit 156a2dcdd6
7 changed files with 230 additions and 87 deletions

View File

@ -32,7 +32,8 @@ list( APPEND test_bins
clone
check_gaussian_grid
ensemble_index
bufr_read_header
bufr_print_header
bufr_print_data
)
foreach( tool ${test_bins} )
ecbuild_add_executable( TARGET ${tool}
@ -61,7 +62,8 @@ list( APPEND tests
sections_copy
set_pv
check_gaussian_grids
bufr_read_header
bufr_print_header
bufr_print_data
)
foreach( test ${tests} )
ecbuild_add_test( TARGET c_${test}
@ -77,7 +79,7 @@ endforeach()
ecbuild_add_test( TARGET c_new_sample
SOURCES new_sample.c
LIBS grib_api
ARGS "out.new_sample.grib"
ARGS "out.grib"
ENVIRONMENT "ECCODES_SAMPLES_PATH=${PROJECT_SOURCE_DIR}/samples" "ECCODES_DEFINITION_PATH=${PROJECT_SOURCE_DIR}/definitions"
)

View File

@ -3,12 +3,12 @@ AM_CFLAGS = @WARN_PEDANTIC@ @WERROR@
TESTS = iterator.sh get.sh print_data.sh set.sh keys_iterator.sh multi.sh multi_write.sh \
precision.sh list.sh large_grib1.sh get_data.sh sections_copy.sh set_missing.sh clone.sh set_pv.sh \
check_gaussian_grids.sh bufr_read_header.sh
check_gaussian_grids.sh bufr_print_header.sh bufr_print_data.sh
noinst_PROGRAMS = nearest set_bitmap iterator get print_data set set_missing keys_iterator \
set_data mars_param values_check box multi multi2 multi_write precision \
set_pv list sections_copy large_grib1 get_data iterator_bitmap clone new_sample \
check_gaussian_grid ensemble_index points bufr_read_header
check_gaussian_grid ensemble_index points bufr_print_header bufr_print_data
#bin_PROGRAMS = points
box_SOURCES = box.c
@ -37,7 +37,8 @@ clone_SOURCES = clone.c
new_sample_SOURCES = new_sample.c
check_gaussian_grid_SOURCES = check_gaussian_grid.c
ensemble_index_SOURCES = ensemble_index.c
bufr_read_header_SOURCES = bufr_read_header.c
bufr_print_header_SOURCES = bufr_print_header.c
bufr_print_data_SOURCES = bufr_print_data.c
INCLUDES = -I$(top_builddir)/src

View File

@ -0,0 +1,81 @@
/*
* Copyright 2005-2015 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.
*/
/*
* C Implementation: bufr_print_data
*
* Description: how to read/print data values from synop BUFR messages.
*
*/
#include "eccodes.h"
void usage(char* prog) {
printf("usage: %s infile\n",prog);
exit(1);
}
int main(int argc,char* argv[])
{
char* filename = NULL;
FILE* in = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
double *values = NULL;
long *desc = NULL;
long longVal;
double doubleVal;
size_t values_len=0;
int err=0, i;
int cnt=0;
char* infile = "../../data/bufr/syno_multi.bufr";
in=fopen(infile,"r");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = bufr_new_from_file(NULL,in,&err)) != NULL || err != CODES_SUCCESS)
{
if (h == NULL) {
printf("Error: unable to create handle for message %s\n",cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
/* we need to instruct ecCodes to unpack the data values */
CODES_CHECK(codes_set_long(h,"unpack",1),0);
/* read and print some data values */
CODES_CHECK(codes_get_long(h,"blockNumber",&longVal),0);
printf("\tblockNumber: %ld\n",longVal);
CODES_CHECK(codes_get_long(h,"stationNumber",&longVal),0);
printf("\tstationNumber: %ld\n",longVal);
CODES_CHECK(codes_get_double(h,"airTemperatureAt2M",&doubleVal),0);
printf("\tairTemperatureAt2M %f\n",doubleVal);
/* delete handle */
codes_handle_delete(h);
cnt++;
}
fclose(in);
return 0;
}

39
examples/C/bufr_print_data.sh Executable file
View File

@ -0,0 +1,39 @@
#!/bin/sh
# Copyright 2005-2015 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
#Enter data dir
cd ${data_dir}/bufr
#Define a common label for all the tmp files
label="bufr_print_data_test"
#Define tmp file
fTmp=${label}".tmp.txt"
rm -f $fTmp
#-----------------------------------------------------
# Test reading the header only from a BUFR
# file with multiple messages
#----------------------------------------------------
f="syno_multi.bufr"
fRef=$f".header.ref"
REDIRECT=/dev/null
#Write the values into a file and compare with reference
${examples_dir}/bufr_print_data ${data_dir}/bufr/$f 2> $REDIRECT > $fTmp
#diff $fRef $fRes >$REDIRECT 2> $REDIRECT
#cat $fTmp
#Clean up
rm -f $fTmp | true

View File

@ -0,0 +1,98 @@
/*
* Copyright 2005-2015 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.
*/
/*
* C Implementation: bufr_print_header
*
* Description: how to read/print the header of BUFR messages.
*
*/
#include "eccodes.h"
void usage(char* prog) {
printf("usage: %s infile\n",prog);
exit(1);
}
int main(int argc,char* argv[])
{
char* filename = NULL;
FILE* in = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
char *typicalDate;
long *desc = NULL;
long longVal;
size_t len=0;
int err=0, i, cnt=0;
if (argc!=2) usage(argv[0]);
filename=argv[1];
in=fopen(filename,"r");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = bufr_new_from_file(NULL,in,&err)) != NULL || err != CODES_SUCCESS)
{
if (h == NULL) {
printf("Error: unable to create handle for message %s\n",cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
/* get and print some keys form the BUFR header */
CODES_CHECK(codes_get_long(h,"dataCategory",&longVal),0);
printf("\tdataCategory: %ld\n",longVal);
CODES_CHECK(codes_get_long(h,"dataSubCategory",&longVal),0);
printf("\tdataSubCategory: %ld\n",longVal);
/* string value */
CODES_CHECK(codes_get_length(h, "typicalDate", &len), 0);
typicalDate = (char*)malloc(len*sizeof(char));
grib_get_string(h, "typicalDate", typicalDate, &len);
printf("\ttypicalDate: %s\n", typicalDate);
free(typicalDate);
CODES_CHECK(codes_get_long(h,"centre",&longVal),0);
printf("\tcentre: %ld\n",longVal);
CODES_CHECK(codes_get_long(h,"subCentre",&longVal),0);
printf("\tsubCentre: %ld\n",longVal);
CODES_CHECK(codes_get_long(h,"masterTablesVersionNumber",&longVal),0);
printf("\tmasterTablesVersionNumber: %ld\n",longVal);
CODES_CHECK(codes_get_long(h,"localTablesVersionNumber",&longVal),0);
printf("\tlocalTablesVersionNumber: %ld\n",longVal);
CODES_CHECK(codes_get_long(h,"numberOfSubsets",&longVal),0);
printf("\tnumberOfSubsets: %ld\n",longVal);
/* delete handle */
codes_handle_delete(h);
cnt++;
}
fclose(in);
return 0;
}

View File

@ -23,7 +23,9 @@ fRes=$f".header.test"
REDIRECT=/dev/null
#Write the values into a file and compare with reference
${examples_dir}/bufr_read_header ${data_dir}/bufr/$f 2> $REDIRECT > $fRes
${examples_dir}/bufr_print_header ${data_dir}/bufr/$f 2> $REDIRECT > $fRes
#diff $fRef $fRes >$REDIRECT 2> $REDIRECT
#cat $fRes
rm -f $fRes

View File

@ -1,80 +0,0 @@
/*
* Copyright 2005-2015 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.
*/
/*
* C Implementation: bufr_read_header
*
* Description: how to read the header of BUFR messages.
*
*/
#include "eccodes.h"
void usage(char* prog) {
printf("usage: %s infile\n",prog);
exit(1);
}
int main(int argc,char* argv[])
{
char* filename = NULL;
FILE* in = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
double *values = NULL;
long *desc = NULL;
long longVal;
size_t values_len=0;
int err=0, i;
if (argc!=2) usage(argv[0]);
filename=argv[1];
in=fopen(filename,"r");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
/* create new handle from a message in a file*/
h=bufr_new_from_file(NULL,in,&err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n",filename);
}
/* check for errors after reading a message. */
if (err != CODES_SUCCESS) CODES_CHECK(err,0);
/* get and print some keys form the BUFR header */
CODES_CHECK(codes_get_long(h,"centre",&longVal),0);
printf("\tcentre: %ld\n",longVal);
CODES_CHECK(codes_get_long(h,"subCentre",&longVal),0);
printf("\tsubCentre: %ld\n",longVal);
CODES_CHECK(codes_get_long(h,"masterTablesVersionNumber",&longVal),0);
printf("\tmasterTablesVersionNumber: %ld\n",longVal);
CODES_CHECK(codes_get_long(h,"localTablesVersionNumber",&longVal),0);
printf("\tlocalTablesVersionNumber: %ld\n",longVal);
CODES_CHECK(codes_get_long(h,"numberOfSubsets",&longVal),0);
printf("\numberOfSubsets: %ld\n",longVal);
/* delete hanle */
codes_handle_delete(h);
fclose(in);
return 0;
}