ECC-240: bufr_dump should be able to dump a given subset

This commit is contained in:
Shahram Najm 2019-06-01 12:11:37 +01:00
parent 6aaef30888
commit 313175b018
1 changed files with 11 additions and 25 deletions

View File

@ -368,7 +368,6 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
long length=0;
int i,err=0;
long subsetNumber=0;
grib_accessor* a=NULL;
grib_accessors_list* al=NULL;
if (grib_get_long(h,"totalLength",&length) != GRIB_SUCCESS)
@ -384,49 +383,36 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
grib_set_flag(h,options->print_keys[i].name,GRIB_ACCESSOR_FLAG_DUMP);
if (grib_options_on("S:")) {
long val=0, numberOfSubsets=0;
char *endptr, *str;
errno = 0; /* To distinguish success/failure after call */
str = grib_options_get_option("S:");
val = strtol(str, &endptr, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) {
perror("strtol");
exit(1);
}
if (endptr == str) {
fprintf(stderr, "ERROR: -S option: No digits were found. Please specify a positive integer.\n");
exit(1);
}
subsetNumber = val;
long numberOfSubsets=0, subsetNumber=0;
char *str = grib_options_get_option("S:");
err = grib_get_long(h,"numberOfSubsets",&numberOfSubsets);
if (err) {
fprintf(stderr, "ERROR: Failed to get numberOfSubsets\n");
exit(1);
}
if (subsetNumber < 1 || subsetNumber > numberOfSubsets) {
fprintf(stderr, "ERROR: -S option: Please specify a subset number > 0 and < %ld\n", numberOfSubsets+1);
fprintf(stderr, "ERROR: Failed to get numberOfSubsets.\n");
exit(1);
}
if (subsetNumber) {
if (check_subset_number(str, numberOfSubsets, &subsetNumber) == GRIB_SUCCESS) {
grib_handle* new_handle = 0;
grib_handle *h2;
size_t size = 0;
const void *buffer = NULL;
/*Clone, unpack and extract that particular subset*/
/* Clone, unpack and extract that particular subset */
h2 = grib_handle_clone(h);
Assert(h2);
GRIB_CHECK_NOLINE(grib_set_long(h2,"unpack", 1), 0);
GRIB_CHECK_NOLINE(grib_set_long(h2,"extractSubset", subsetNumber), 0);
GRIB_CHECK_NOLINE(grib_set_long(h2,"doExtractSubsets",1), 0);
/*Put result into buffer then form new handle from it*/
/* Put result into buffer then form new handle from it */
GRIB_CHECK_NOLINE(grib_get_message(h2, &buffer, &size),0);
new_handle = grib_handle_new_from_message(0, buffer, size);
Assert(new_handle);
/*TODO: possible leak!*/
h = new_handle;
/* Replace handle with the new one which has only one subset */
h = new_handle; /*TODO: possible leak!*/
} else {
fprintf(stderr, "ERROR: -S option: Please specify a subset number > 0 and < %ld\n", numberOfSubsets+1);
exit(1);
}
}