tools/grib_histogram.c
/*
 A new handle is available from the current input file and can be processed here.
 The handle available in this function is in the set of messages satisfying the constraint
 of the -w option. They are not to be skipped.
*/
A new handle is available from the current input file and can be processed here.
The handle available in this function is in the set of messages satisfying the constraint
of the -w option. They are not to be skipped.
A new handle is available from the current input file and can be processed here . The handle available in this function is in the set of messages satisfying the constraint of the - w option . They are not to be skipped .
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
    int i, j, err = 0;
    size_t last_size = 0;
    long missingValuesPresent;
    double delta;
    double* values = NULL;
    size_t size;
    double min, max;
    long count = 10;
    long intervals[10];
    const char* names[1024];
    int name_count = 0;
    for (i = 0; i < options->requested_print_keys_count; i++) {
        names[name_count++] = options->requested_print_keys[i].name;
    }
    if (!options->skip) {
        if (options->set_values_count != 0)
            err = grib_set_values(h, options->set_values, options->set_values_count);
        if (err != GRIB_SUCCESS && options->fail)
            exit(err);
    }
    GRIB_CHECK(grib_get_long(h, "missingValuesPresent", &missingValuesPresent), 0);
    GRIB_CHECK(grib_get_size(h, "values", &size), 0);
    if (size > last_size) {
        if (values)
            free(values);
        values = (double*)malloc(size * sizeof(double));
        if (!values) {
            fprintf(stderr, "Failed to allocate memory for values (%lu bytes)\n", size * sizeof(double));
            exit(1);
        }
    }
    GRIB_CHECK(grib_get_double_array(h, "values", values, &size), 0);
    for (j = 0; j < count; j++)
        intervals[j] = 0;
    if (missingValuesPresent) {
        double missing;
        GRIB_CHECK(grib_get_double(h, "missingValue", &missing), 0);
        min = max = missing;
        for (j = 0; j < size; j++) {
            if ((min == missing) || ((values[j] != missing) && (min > values[j])))
                min = values[j];
            if ((max == missing) || ((values[j] != missing) && (max < values[j])))
                max = values[j];
        }
        delta = max - min;
        if (delta == 0)
            delta = 1;
        for (j = 0; j < size; j++) {
            if (values[j] != missing) {
                int x = (values[j] - min) / delta * count;
                if (x == count)
                    x = x - 1; 
                intervals[x]++;
            }
        }
    }
    else {
        min = max = values[0];
        for (j = 1; j < size; j++) {
            if (min > values[j])
                min = values[j];
            if (max < values[j])
                max = values[j];
        }
        delta = max - min;
        if (delta == 0)
            delta = 1;
        for (j = 0; j < size; j++) {
            int x = (values[j] - min) / delta * count;
            if (x == count)
                x = x - 1; 
            intervals[x]++;
        }
    }
    for (j = 0; j < name_count; j++) {
        char v[1024];
        size_t s = sizeof(v);
        GRIB_CHECK(grib_get_string(h, names[j], v, &s), names[j]);
        printf(" %s", v);
    }
    printf("\nmin=%g max=%g size=%ld\n", min, max, (long)size);
    for (j = 0; j < count; j++)
        printf(" %g:%g", j * (max - min) / count + min, (j + 1) * (max - min) / count + min);
    printf("\n");
    for (j = 0; j < count; j++)
        printf(" %ld", intervals[j]);
    printf("\n");
    return 0;
}
int grib_tool_new_handle_action ( grib_runtime_options * options , grib_handle * h ) { int i , j , err = 0 ; size_t last_size = 0 ; long missingValuesPresent ; double delta ; double * values = NULL ; size_t size ; double min , max ; long count = 10 ; long intervals [ 10 ] ; const char * names [ 1024 ] ; int name_count = 0 ; for ( i = 0 ; i < options -> requested_print_keys_count ; i ++ ) { names [ name_count ++ ] = options -> requested_print_keys [ i ] . name ; } if ( ! options -> skip ) { if ( options -> set_values_count != 0 ) err = grib_set_values ( h , options -> set_values , options -> set_values_count ) ; if ( err != GRIB_SUCCESS && options -> fail ) exit ( err ) ; } GRIB_CHECK ( grib_get_long ( h , " " , & missingValuesPresent ) , 0 ) ; GRIB_CHECK ( grib_get_size ( h , " " , & size ) , 0 ) ; if ( size > last_size ) { if ( values ) free ( values ) ; values = ( double * ) malloc ( size * sizeof ( double ) ) ; if ( ! values ) { fprintf ( stderr , " \n " , size * sizeof ( double ) ) ; exit ( 1 ) ; } } GRIB_CHECK ( grib_get_double_array ( h , " " , values , & size ) , 0 ) ; for ( j = 0 ; j < count ; j ++ ) intervals [ j ] = 0 ; if ( missingValuesPresent ) { double missing ; GRIB_CHECK ( grib_get_double ( h , " " , & missing ) , 0 ) ; min = max = missing ; for ( j = 0 ; j < size ; j ++ ) { if ( ( min == missing ) || ( ( values [ j ] != missing ) && ( min > values [ j ] ) ) ) min = values [ j ] ; if ( ( max == missing ) || ( ( values [ j ] != missing ) && ( max < values [ j ] ) ) ) max = values [ j ] ; } delta = max - min ; if ( delta == 0 ) delta = 1 ; for ( j = 0 ; j < size ; j ++ ) { if ( values [ j ] != missing ) { int x = ( values [ j ] - min ) / delta * count ; if ( x == count ) x = x - 1 ; intervals [ x ] ++ ; } } } else { min = max = values [ 0 ] ; for ( j = 1 ; j < size ; j ++ ) { if ( min > values [ j ] ) min = values [ j ] ; if ( max < values [ j ] ) max = values [ j ] ; } delta = max - min ; if ( delta == 0 ) delta = 1 ; for ( j = 0 ; j < size ; j ++ ) { int x = ( values [ j ] - min ) / delta * count ; if ( x == count ) x = x - 1 ; intervals [ x ] ++ ; } } for ( j = 0 ; j < name_count ; j ++ ) { char v [ 1024 ] ; size_t s = sizeof ( v ) ; GRIB_CHECK ( grib_get_string ( h , names [ j ] , v , & s ) , names [ j ] ) ; printf ( " " , v ) ; } printf ( " \n \n " , min , max , ( long ) size ) ; for ( j = 0 ; j < count ; j ++ ) printf ( " " , j * ( max - min ) / count + min , ( j + 1 ) * ( max - min ) / count + min ) ; printf ( " \n " ) ; for ( j = 0 ; j < count ; j ++ ) printf ( " " , intervals [ j ] ) ; printf ( " \n " ) ; return 0 ; }
A new handle is available from the current input file and can be processed here.
A new handle is available from the current input file and can be processed here .
/*last_size = size;*/ /*handle the absolute  maximum */ /*handle the absolute  maximum */