ECC-236: bufr_ls does not print the right value for unpacked data

This commit is contained in:
Shahram Najm 2016-09-16 16:23:03 +01:00
parent 3248dcbb40
commit 9bce800260
2 changed files with 146 additions and 85 deletions

View File

@ -47,5 +47,19 @@ ${tools_dir}/bufr_get -p totalLength,bufrHeaderCentre,bufrHeaderSubCentre,master
cat $fTmp | awk '{split($0,a," "); for (i=1; i<=8; i++) print a[i]}' > $res_get
diff $ref_get $res_get
#-------------------------------------------
# ECC-236
#-------------------------------------------
result=`${tools_dir}/bufr_get -s unpack=1 -p nonCoordinatePressure syno_1.bufr`
[ "$result" = "100910" ]
result=`${tools_dir}/bufr_get -s unpack=1 -p stationOrSiteName bssh_176.bufr`
[ "$result" = "Helgoland" ]
result=`${tools_dir}/bufr_get -s unpack=1 -p majorFrameCount aaen_55.bufr`
[ "$result" = "MISSING" ]
result=`${tools_dir}/bufr_get -s unpack=1 -p satelliteIdentifier wavb_134.bufr`
[ "$result" = "MISSING" ]
#Clean up
rm -f $fLog $fTmp $res_get

View File

@ -48,61 +48,61 @@ static int scan(grib_context* c,grib_runtime_options* options,const char* dir);
FILE* dump_file;
grib_runtime_options global_options={
0, /* verbose */
0, /* fail */
0, /* skip */
12, /* default_print_width */
0, /* print_header */
0, /* name_space */
0, /* print_number */
1, /* print_statistics */
0, /* verbose */
0, /* fail */
0, /* skip */
12, /* default_print_width */
0, /* print_header */
0, /* name_space */
0, /* print_number */
1, /* print_statistics */
{{0,},}, /* grib_values requested_print_keys[MAX_KEYS] */
0, /* requested_print_keys_count */
{{0,},}, /* grib_values print_keys[MAX_KEYS] */
0, /* print_keys_count */
0, /* strict */
0, /* multi_support */
0, /* set_values_count */
{{0,},}, /* grib_values set_values[MAX_KEYS] */
{{0,},}, /* grib_values constraints[MAX_KEYS] */
0, /* constraints_count */
{{0,},}, /* grib_values compare[MAX_KEYS] */
0, /* compare_count */
0, /* handle_count */
0, /* filter_handle_count */
0, /* file_count */
0, /* grib_tools_file infile_extra */
0, /* grib_tools_file current_infile */
0, /* grib_tools_file infile */
0, /*grib_tools_file outfile */
0, /* grib_action action */
0, /* grib_rule rules */
0, /* int dump_flags; */
0, /* char* dump_mode; */
0, /* repack */
0, /* error */
0, /* gts */
0, /* orderby */
0, /* latlon */
{0,},
{0,},
{0,},
{0,},
{0,},
4,
0,
-1,
{0,},
0, /* index */
0, /* index_on */
0, /* constant */
0, /* dump_filename*/
0, /* index */
0, /* random */
0, /* requested_print_keys_count */
{{0,},}, /* grib_values print_keys[MAX_KEYS] */
0, /* print_keys_count */
0, /* strict */
0, /* multi_support */
0, /* set_values_count */
{{0,},}, /* grib_values set_values[MAX_KEYS] */
{{0,},}, /* grib_values constraints[MAX_KEYS] */
0, /* constraints_count */
{{0,},}, /* grib_values compare[MAX_KEYS] */
0, /* compare_count */
0, /* handle_count */
0, /* filter_handle_count */
0, /* file_count */
0, /* grib_tools_file* infile_extra */
0, /* grib_tools_file* current_infile */
0, /* grib_tools_file* infile */
0, /* grib_tools_file* outfile */
0, /* grib_action* action */
0, /* grib_rule* rules */
0, /* int dump_flags; */
0, /* char* dump_mode; */
0, /* repack */
0, /* error */
0, /* gts */
0, /* orderby */
0, /* latlon */
{0,}, /* double lats[4] */
{0,}, /* double lons[4] */
{0,}, /* double values[4] */
{0,}, /* double distances[4] */
{0,}, /* int indexes[4] */
4, /* int latlon_mode */
0, /* char* latlon_mask */
-1, /* int latlon_idx */
{0,}, /* double mask_values[4] */
0, /* index */
0, /* index_on */
0, /* constant */
0, /* dump_filename*/
0, /* grib_fieldset* idx */
0, /* random */
0, /* format */
0, /* onlyfiles */
0, /* tolerance_count */
0, /* through_index */
0, /* through_index */
0, /* index1 */
0, /* index2 */
0, /* context */
@ -113,7 +113,7 @@ grib_runtime_options global_options={
{{0,},}, /* grib_values tolerance[MAX_KEYS] */
0 /* infile_offset */
};
};
static grib_handle* grib_handle_new_from_file_x(grib_context* c,FILE* f,int mode,int headers_only,int *err)
{
@ -384,23 +384,23 @@ static int grib_tool_without_orderby(grib_runtime_options* options)
static int navigate(grib_field_tree* fields,grib_runtime_options* options)
{
int err=0;
int message_type=0;
int message_type=0;
if (!fields || options->stop) return 0;
switch (options->mode) {
switch (options->mode) {
case MODE_GRIB:
message_type=CODES_GRIB;
break;
message_type=CODES_GRIB;
break;
case MODE_BUFR:
message_type=CODES_BUFR;
break;
message_type=CODES_BUFR;
break;
default :
Assert(0);
}
Assert(0);
}
if (fields->field) {
grib_handle* h=codes_index_get_handle(fields->field,message_type,&err);
grib_handle* h=codes_index_get_handle(fields->field,message_type,&err);
if (!options->index2->current)
options->index2->current=(grib_field_list*)grib_context_malloc_clear(options->context,sizeof(grib_field_list));
options->index2->current->field=fields->field;
@ -786,6 +786,21 @@ void grib_skip_check(grib_runtime_options* options,grib_handle* h)
}
}
static int string_value_is_missing(const char* val, size_t length)
{
/* For a string value to be missing, all its chars have to be */
/* all 1's i.e. -1 */
int i=0;
if (val == NULL || length == 0)
return 0;
for(i=0; i<length; ++i) {
if (val[i] != -1) {
return 0; /* not all bits set, so not missing */
}
}
return 1;
}
void grib_print_key_values(grib_runtime_options* options,grib_handle* h)
{
int i=0;
@ -803,31 +818,64 @@ void grib_print_key_values(grib_runtime_options* options,grib_handle* h)
size_t len=MAX_STRING_LEN;
ret=GRIB_SUCCESS;
if (grib_is_missing(h,options->print_keys[i].name,&ret) && ret==GRIB_SUCCESS)
sprintf(value,"MISSING");
else if ( ret == GRIB_SUCCESS ) {
if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED)
grib_get_native_type(h,options->print_keys[i].name,&(options->print_keys[i].type));
switch (options->print_keys[i].type) {
case GRIB_TYPE_STRING:
ret=grib_get_string( h,options->print_keys[i].name,value,&len);
break;
case GRIB_TYPE_DOUBLE:
ret=grib_get_double( h,options->print_keys[i].name,&dvalue);
sprintf(value,options->format,dvalue);
break;
case GRIB_TYPE_LONG:
ret=grib_get_long( h,options->print_keys[i].name,&lvalue);
sprintf(value,"%ld", lvalue);
break;
case GRIB_TYPE_BYTES:
ret=grib_get_string( h,options->print_keys[i].name,value,&len);
break;
default:
fprintf(dump_file,"invalid format option for %s\n",options->print_keys[i].name);
exit(1);
if (h->product_kind == PRODUCT_BUFR) {
/* ECC-236: Do not use grib_is_missing for BUFR */
if (!grib_is_defined(h, options->print_keys[i].name)) ret = GRIB_NOT_FOUND;
if (ret == GRIB_SUCCESS) {
if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED)
grib_get_native_type(h, options->print_keys[i].name, &(options->print_keys[i].type));
switch (options->print_keys[i].type) {
case GRIB_TYPE_STRING:
ret=grib_get_string(h, options->print_keys[i].name, value, &len);
if (string_value_is_missing(value, len)) sprintf(value,"MISSING");
break;
case GRIB_TYPE_DOUBLE:
ret=grib_get_double(h, options->print_keys[i].name, &dvalue);
if (dvalue == GRIB_MISSING_DOUBLE) sprintf(value,"MISSING");
else sprintf(value,options->format,dvalue);
break;
case GRIB_TYPE_LONG:
ret=grib_get_long(h, options->print_keys[i].name, &lvalue);
if (lvalue == GRIB_MISSING_LONG) sprintf(value,"MISSING");
else sprintf(value,"%ld", lvalue);
break;
case GRIB_TYPE_BYTES:
ret=grib_get_string(h, options->print_keys[i].name, value, &len);
break;
default:
fprintf(dump_file,"Could not determine type for %s\n", options->print_keys[i].name);
exit(1);
}
}
} else {
if (grib_is_missing(h,options->print_keys[i].name,&ret) && ret==GRIB_SUCCESS) {
sprintf(value,"MISSING");
}
else if ( ret == GRIB_SUCCESS ) {
if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED)
grib_get_native_type(h,options->print_keys[i].name,&(options->print_keys[i].type));
switch (options->print_keys[i].type) {
case GRIB_TYPE_STRING:
ret=grib_get_string( h,options->print_keys[i].name,value,&len);
break;
case GRIB_TYPE_DOUBLE:
ret=grib_get_double( h,options->print_keys[i].name,&dvalue);
sprintf(value,options->format,dvalue);
break;
case GRIB_TYPE_LONG:
ret=grib_get_long( h,options->print_keys[i].name,&lvalue);
sprintf(value,"%ld", lvalue);
break;
case GRIB_TYPE_BYTES:
ret=grib_get_string( h,options->print_keys[i].name,value,&len);
break;
default:
fprintf(dump_file,"invalid format option for %s\n",options->print_keys[i].name);
exit(1);
}
}
}
if (ret != GRIB_SUCCESS) {
if (options->fail) GRIB_CHECK_NOLINE(ret,options->print_keys[i].name);
if (ret == GRIB_NOT_FOUND) strcpy(value,notfound);
@ -1030,4 +1078,3 @@ void grib_tools_write_message(grib_runtime_options* options, grib_handle* h)
#endif
}