diff --git a/examples/C/check_gaussian_grid.c b/examples/C/check_gaussian_grid.c index 85a19861f..e65b91fe7 100644 --- a/examples/C/check_gaussian_grid.c +++ b/examples/C/check_gaussian_grid.c @@ -138,13 +138,18 @@ int process_file(const char* filename) } if (is_reduced) { - int pl_sum = 0, max_pl = 0; + int pl_sum = 0, max_pl = 0, is_missing_Ni = 0, is_missing_Di = 0; size_t i = 0, pl_len = 0; long is_octahedral = 0; - int is_missing = codes_is_missing(h, "Ni", &err); + is_missing_Ni = codes_is_missing(h, "Ni", &err); assert(err == CODES_SUCCESS); - if (!is_missing) { - error("ERROR: Ni should be missing!\n"); + is_missing_Di = grib_is_missing(h, "iDirectionIncrement", &err); + assert(err == GRIB_SUCCESS); + if (!is_missing_Ni) { + error("ERROR: For a reduced gaussian grid Ni should be missing!\n"); + } + if (!is_missing_Di) { + error("ERROR: For a reduced gaussian grid iDirectionIncrement should be missing!\n"); } CODES_CHECK(codes_get_size(h, "pl", &pl_len),0); diff --git a/tests/grib_util_set_spec.c b/tests/grib_util_set_spec.c index 6766df152..829153348 100644 --- a/tests/grib_util_set_spec.c +++ b/tests/grib_util_set_spec.c @@ -10,18 +10,20 @@ #include "eccodes.h" #include -void test1(const char* input_filename, const char* output_filename) +#define STR_EQUAL(s1, s2) (strcmp((s1), (s2)) == 0) + +void test_reduced_gg(const char* input_filename, const char* output_filename) { /* based on copy_spec_from_ksec */ int err = 0; - size_t inlen = 0; - size_t outlen = 0; + size_t slen = 32, inlen = 0, outlen = 0; size_t size=0; int set_spec_flags=0; double* values = NULL; FILE* in = NULL; FILE* out = NULL; const void* buffer = NULL; + char gridType[128] = {0,}; codes_handle *handle = 0; codes_handle *finalh = 0; @@ -29,13 +31,89 @@ void test1(const char* input_filename, const char* output_filename) grib_util_packing_spec packing_spec={0,}; in = fopen(input_filename,"r"); assert(in); + handle = grib_handle_new_from_file(0,in,&err); assert(handle); + + CODES_CHECK(grib_get_string(handle, "gridType", gridType, &slen),0); + if (!STR_EQUAL(gridType, "reduced_gg")) { + grib_handle_delete(handle); + return; + } + out = fopen(output_filename,"w"); assert(out); + + CODES_CHECK(grib_get_size(handle,"values",&inlen), 0); + values = (double*)malloc(sizeof(double)*inlen); + CODES_CHECK(grib_get_double_array(handle, "values", values,&inlen), 0); + + spec.grid_type = GRIB_UTIL_GRID_SPEC_REDUCED_GG; + spec.N = 320; /* hardcoded for now */ + spec.Nj = 2 * spec.N; + outlen = inlen; + spec.iDirectionIncrementInDegrees = 1.5; + spec.jDirectionIncrementInDegrees = 1.5; + spec.latitudeOfFirstGridPointInDegrees = 89.785; + spec.longitudeOfFirstGridPointInDegrees = 0.0; + spec.latitudeOfLastGridPointInDegrees = -89.785; + spec.longitudeOfLastGridPointInDegrees = 359.719; + spec.bitmapPresent = 0; + + packing_spec.packing_type = GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE; + packing_spec.bitsPerValue = 24; + packing_spec.accuracy=GRIB_UTIL_ACCURACY_USE_PROVIDED_BITS_PER_VALUES; + packing_spec.packing=GRIB_UTIL_PACKING_USE_PROVIDED; + + finalh = grib_util_set_spec( + handle, + &spec, + &packing_spec, + set_spec_flags, + values, + outlen, + &err); + assert(finalh); + assert(err == 0); + + /* Write out the message to the output file */ + CODES_CHECK(grib_get_message(finalh, &buffer, &size),0); + if(fwrite(buffer,1,size,out) != size) { + assert(0); + } + grib_handle_delete(handle); + grib_handle_delete(finalh); + fclose(in); + fclose(out); +} + +void test_regular_ll(const char* input_filename, const char* output_filename) +{ + /* based on copy_spec_from_ksec */ + int err = 0; + size_t slen = 32, inlen = 0, outlen = 0; + size_t size=0; + int set_spec_flags=0; + double* values = NULL; + FILE* in = NULL; + FILE* out = NULL; + const void* buffer = NULL; + char gridType[128] = {0,}; + + codes_handle *handle = 0; + codes_handle *finalh = 0; + grib_util_grid_spec spec={0,}; + grib_util_packing_spec packing_spec={0,}; + + in = fopen(input_filename,"r"); assert(in); + handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); assert(handle); + + CODES_CHECK(grib_get_string(handle, "gridType", gridType, &slen),0); + if (!STR_EQUAL(gridType, "regular_ll")) { + grib_handle_delete(handle); + return; + } out = fopen(output_filename,"w"); assert(out); - handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); - assert(handle); CODES_CHECK(codes_get_size(handle,"values",&inlen), 0); values = (double*)malloc(sizeof(double)*inlen); - GRIB_CHECK(codes_get_double_array(handle, "values", values,&inlen), 0); + CODES_CHECK(codes_get_double_array(handle, "values", values,&inlen), 0); spec.grid_type = GRIB_UTIL_GRID_SPEC_REGULAR_LL; spec.Nj = 14; @@ -72,6 +150,8 @@ void test1(const char* input_filename, const char* output_filename) } codes_handle_delete(handle); codes_handle_delete(finalh); + fclose(in); + fclose(out); } int main(int argc, char *argv[]) @@ -79,7 +159,8 @@ int main(int argc, char *argv[]) const char *infile = argv[1]; const char *outfile = argv[2]; - test1(infile, outfile); + test_regular_ll(infile, outfile); + test_reduced_gg(infile, outfile); return 0; } diff --git a/tests/grib_util_set_spec.sh b/tests/grib_util_set_spec.sh index cf941fa07..e62c6af60 100755 --- a/tests/grib_util_set_spec.sh +++ b/tests/grib_util_set_spec.sh @@ -10,8 +10,11 @@ . ./include.sh +### Regular Lat/Lon Grid +########################################### infile=../data/latlon.grib outfile=out.grib_util_set_spec.grib +rm -f $outfile ${test_dir}/grib_util_set_spec $infile $outfile > /dev/null @@ -20,4 +23,16 @@ res=`${tools_dir}grib_get -p Ni,Nj,numberOfValues,bitsPerValue $outfile` ${tools_dir}grib_get_data $outfile > /dev/null +### Reduced Gaussian Grid +########################################### +infile=$ECCODES_SAMPLES_PATH/reduced_gg_pl_320_grib2.tmpl +outfile=out.grib_util_set_spec.grib +rm -f $outfile + +${test_dir}/grib_util_set_spec $infile $outfile + +${tools_dir}grib_get_data $outfile > /dev/null + + +### Clean up rm -f $outfile