mirror of https://github.com/ecmwf/eccodes.git
GRIB-947: grib_util_set_spec: For grib2 second order packing, data values are set twice
This commit is contained in:
parent
569bee5314
commit
acce5d56f4
|
@ -561,6 +561,29 @@ static const char* get_grid_type_name(const int spec_grid_type)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static int is_constant_field(grib_handle* h, const double* data_values, size_t data_values_count)
|
||||
{
|
||||
int ii=0;
|
||||
int constant=1;
|
||||
double missingValue=0;
|
||||
double value = missingValue;
|
||||
|
||||
grib_get_double(h,"missingValue",&missingValue);
|
||||
for (ii=0;ii<data_values_count;ii++) {
|
||||
if (data_values[ii]!=missingValue) {
|
||||
if (value==missingValue) {
|
||||
value=data_values[ii];
|
||||
} else {
|
||||
if (value!=data_values[ii]) {
|
||||
constant=0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return constant;
|
||||
}
|
||||
|
||||
grib_handle* grib_util_set_spec(grib_handle* h,
|
||||
const grib_util_grid_spec *spec,
|
||||
const grib_util_packing_spec *packing_spec,
|
||||
|
@ -1175,7 +1198,7 @@ grib_handle* grib_util_set_spec2(grib_handle* h,
|
|||
|
||||
if((*err = grib_set_values(outh,values,count)) != 0)
|
||||
{
|
||||
fprintf(stderr,"SET_GRID_DATA_DESCRIPTION: Cannot set values %s\n",grib_get_error_message(*err));
|
||||
fprintf(stderr,"SET_GRID_DATA_DESCRIPTION: Cannot set key values: %s\n",grib_get_error_message(*err));
|
||||
|
||||
for(i = 0; i < count; i++)
|
||||
if(values[i].error)
|
||||
|
@ -1183,39 +1206,45 @@ grib_handle* grib_util_set_spec2(grib_handle* h,
|
|||
goto cleanup;
|
||||
}
|
||||
|
||||
if((*err = grib_set_double_array(outh,"values",data_values,data_values_count)) != 0)
|
||||
/* See GRIB-947 */
|
||||
if (setSecondOrder && editionNumber != 1 && !is_constant_field(outh, data_values, data_values_count))
|
||||
{
|
||||
FILE* ferror;
|
||||
long i,lcount;
|
||||
grib_context* c=grib_context_get_default();
|
||||
/* Do not set the values at this point for this case. See later for 'setSecondOrder' */
|
||||
}
|
||||
else
|
||||
{
|
||||
if((*err = grib_set_double_array(outh,"values",data_values,data_values_count)) != 0)
|
||||
{
|
||||
FILE* ferror;
|
||||
long i,lcount;
|
||||
grib_context* c=grib_context_get_default();
|
||||
|
||||
ferror=fopen("error.data","w");
|
||||
lcount=0;
|
||||
fprintf(ferror,"# data_values_count=%ld\n",(long)data_values_count);
|
||||
fprintf(ferror,"set values={ ");
|
||||
for (i=0;i<data_values_count-1;i++) {
|
||||
fprintf(ferror,"%g, ",data_values[i]);
|
||||
if (lcount>10) {fprintf(ferror,"\n");lcount=0;}
|
||||
lcount++;
|
||||
ferror=fopen("error.data","w");
|
||||
lcount=0;
|
||||
fprintf(ferror,"# data_values_count=%ld\n",(long)data_values_count);
|
||||
fprintf(ferror,"set values={ ");
|
||||
for (i=0;i<data_values_count-1;i++) {
|
||||
fprintf(ferror,"%g, ",data_values[i]);
|
||||
if (lcount>10) {fprintf(ferror,"\n");lcount=0;}
|
||||
lcount++;
|
||||
}
|
||||
fprintf(ferror,"%g }",data_values[data_values_count-1]);
|
||||
fclose(ferror);
|
||||
if (c->write_on_fail)
|
||||
grib_write_message(outh,"error.grib","w");
|
||||
goto cleanup;
|
||||
}
|
||||
fprintf(ferror,"%g }",data_values[data_values_count-1]);
|
||||
fclose(ferror);
|
||||
if (c->write_on_fail)
|
||||
grib_write_message(outh,"error.grib","w");
|
||||
goto cleanup;
|
||||
}
|
||||
/* grib_write_message(outh,"h.grib","w"); */
|
||||
/* if the field is empty GRIBEX is packing as simple*/
|
||||
/*
|
||||
if (!strcmp(input_packing_type,"grid_simple_matrix")) {
|
||||
/* if (!strcmp(input_packing_type,"grid_simple_matrix")) {
|
||||
long numberOfValues;
|
||||
grib_get_long(outh,"numberOfValues",&numberOfValues);
|
||||
if (numberOfValues==0) {
|
||||
slen=11;
|
||||
grib_set_string(outh,"packingType","grid_simple",&slen);
|
||||
}
|
||||
}
|
||||
*/
|
||||
} */
|
||||
|
||||
if (grib1_high_resolution_fix) {
|
||||
/* GRIB-863: must set increments to MISSING */
|
||||
|
@ -1234,24 +1263,8 @@ grib_handle* grib_util_set_spec2(grib_handle* h,
|
|||
|
||||
/* convert to second_order if not constant field */
|
||||
if (setSecondOrder ) {
|
||||
int ii=0;
|
||||
int constant=1;
|
||||
double missingValue=0;
|
||||
double value=missingValue;
|
||||
int constant = is_constant_field(outh, data_values, data_values_count);
|
||||
|
||||
grib_get_double(outh,"missingValue",&missingValue);
|
||||
for (ii=0;ii<data_values_count;ii++) {
|
||||
if (data_values[ii]!=missingValue) {
|
||||
if (value==missingValue) {
|
||||
value=data_values[ii];
|
||||
} else {
|
||||
if (value!=data_values[ii]) {
|
||||
constant=0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!constant) {
|
||||
if (editionNumber == 1 ) {
|
||||
long numberOfGroups=0;
|
||||
|
@ -1272,12 +1285,16 @@ grib_handle* grib_util_set_spec2(grib_handle* h,
|
|||
} else {
|
||||
slen=17;
|
||||
grib_set_string(outh,"packingType","grid_second_order",&slen);
|
||||
grib_set_double_array(outh,"values",data_values,data_values_count);
|
||||
*err = grib_set_double_array(outh,"values", data_values, data_values_count);
|
||||
if (*err != GRIB_SUCCESS) {
|
||||
fprintf(stderr,"GRIB_UTIL_SET_SPEC: setting data values failed! %s\n", grib_get_error_message(*err));
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (outh->context->gribex_mode_on) {
|
||||
outh->context->gribex_mode_on=0;
|
||||
grib_set_double_array(outh,"values",data_values,data_values_count);
|
||||
grib_set_double_array(outh,"values", data_values, data_values_count);
|
||||
outh->context->gribex_mode_on=1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ void test_reduced_gg(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;
|
||||
size_t i=0, size=0;
|
||||
int set_spec_flags=0;
|
||||
double* values = NULL;
|
||||
FILE* in = NULL;
|
||||
|
@ -43,20 +43,23 @@ void test_reduced_gg(const char* input_filename, const char* output_filename)
|
|||
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);
|
||||
for(i=0; i<inlen; ++i) {
|
||||
values[i] *= 1.10;
|
||||
}
|
||||
|
||||
spec.grid_type = GRIB_UTIL_GRID_SPEC_REDUCED_GG;
|
||||
spec.N = 320; /* hardcoded for now */
|
||||
spec.N = 32; /* hardcoded for now */
|
||||
spec.Nj = 2 * spec.N;
|
||||
outlen = inlen;
|
||||
spec.iDirectionIncrementInDegrees = 1.5;
|
||||
spec.jDirectionIncrementInDegrees = 1.5;
|
||||
spec.latitudeOfFirstGridPointInDegrees = 89.785;
|
||||
spec.latitudeOfFirstGridPointInDegrees = 87.863799;
|
||||
spec.longitudeOfFirstGridPointInDegrees = 0.0;
|
||||
spec.latitudeOfLastGridPointInDegrees = -89.785;
|
||||
spec.longitudeOfLastGridPointInDegrees = 359.719;
|
||||
spec.latitudeOfLastGridPointInDegrees = -spec.latitudeOfFirstGridPointInDegrees;
|
||||
spec.longitudeOfLastGridPointInDegrees = 357.187500;
|
||||
spec.bitmapPresent = 0;
|
||||
|
||||
packing_spec.packing_type = GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE;
|
||||
packing_spec.packing_type = GRIB_UTIL_PACKING_TYPE_GRID_SECOND_ORDER;
|
||||
packing_spec.bitsPerValue = 24;
|
||||
packing_spec.accuracy=GRIB_UTIL_ACCURACY_USE_PROVIDED_BITS_PER_VALUES;
|
||||
packing_spec.packing=GRIB_UTIL_PACKING_USE_PROVIDED;
|
||||
|
|
|
@ -16,21 +16,37 @@ infile=../data/latlon.grib
|
|||
outfile=out.grib_util_set_spec.grib
|
||||
rm -f $outfile
|
||||
|
||||
${test_dir}/grib_util_set_spec $infile $outfile > /dev/null
|
||||
${test_dir}grib_util_set_spec $infile $outfile > /dev/null
|
||||
|
||||
res=`${tools_dir}grib_get -p Ni,Nj,numberOfValues,bitsPerValue $outfile`
|
||||
[ "$res" = "17 14 238 24" ]
|
||||
|
||||
# Check output file geometry
|
||||
${tools_dir}grib_get_data $outfile > /dev/null
|
||||
|
||||
### Reduced Gaussian Grid
|
||||
### Reduced Gaussian Grid N=32 second order packing
|
||||
###########################################
|
||||
infile=$ECCODES_SAMPLES_PATH/reduced_gg_pl_320_grib2.tmpl
|
||||
infile=../data/reduced_gaussian_model_level.grib2
|
||||
outfile=out.grib_util_set_spec.grib
|
||||
rm -f $outfile
|
||||
|
||||
${test_dir}/grib_util_set_spec $infile $outfile
|
||||
${test_dir}grib_util_set_spec $infile $outfile
|
||||
|
||||
# Check output file
|
||||
grib_check_key_equals $outfile packingType grid_second_order
|
||||
${tools_dir}grib_get_data $outfile > /dev/null
|
||||
CHECK_TOOL="${tools_dir}grib_check_gaussian_grid"
|
||||
if [ -x $CHECK_TOOL ]; then
|
||||
$CHECK_TOOL $outfile
|
||||
fi
|
||||
|
||||
### Constant field N=32
|
||||
###########################################
|
||||
infile=$ECCODES_SAMPLES_PATH/reduced_gg_pl_32_grib2.tmpl
|
||||
rm -f $outfile
|
||||
|
||||
${test_dir}grib_util_set_spec $infile $outfile
|
||||
grib_check_key_equals $outfile "packingType,const" "grid_simple 1"
|
||||
${tools_dir}grib_get_data $outfile > /dev/null
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue