tools/grib_ls.c
/*
 The options have been parsed and the structure
 grib_runtime_options* options has been loaded.
 Initialisation and startup can be done here
*/
The options have been parsed and the structure
grib_runtime_options* options has been loaded.
Initialisation and startup can be done here
The options have been parsed and the structure grib_runtime_options * options has been loaded . Initialisation and startup can be done here
int grib_tool_init(grib_runtime_options* options)
{
    char *theEnd = NULL, *end1 = NULL;
    size_t size = 4;
    int ret     = 0;
    double min = 0, max = 0;
    int i   = 0;
    char* p = NULL;
    if (options->latlon && grib_options_on("j")) {
        options->verbose = 0;
        json_latlon      = 1;
    }
    if (options->latlon) {
        lat = strtod(options->latlon, &theEnd);
        if (*theEnd != ',') {
            fprintf(stderr, "Error %s: wrong latitude value. Please use 'latitude,longitude'\n", tool_name);
            exit(1);
        }
        lon = strtod(++theEnd, &end1);
        mode = GRIB_NEAREST_SAME_POINT | GRIB_NEAREST_SAME_GRID;
        if (end1 && *end1 == ',') {
            end1++;
            if (*end1 != '0') {
                p = end1;
                while (*p != ',' && *p != '\0')
                    p++;
                if (*end1 == '4') {
                    options->latlon_mode = 4;
                }
                else if (*end1 == '1') {
                    options->latlon_mode = 1;
                }
                else {
                    fprintf(stderr, "Error %s: wrong mode given in option -l\n", tool_name);
                    exit(1);
                }
            }
            Assert(p);
            if (p && *p == ',') {
                p++;
                options->latlon_mask = strdup(p);
            }
        }
    }
    if (options->latlon && options->latlon_mask) {
        grib_handle* hh;
        int idx = 0, land_found = 0;
        double min_overall = 0.0;
        int idx_overall    = -1;
        FILE* f            = fopen(options->latlon_mask, "r");
        if (!f) {
            perror(options->latlon_mask);
            exit(1);
        }
        hh = grib_handle_new_from_file(0, f, &ret);
        fclose(f);
        GRIB_CHECK_NOLINE(ret, 0);
        nearest = grib_nearest_new(hh, &ret);
        GRIB_CHECK_NOLINE(ret, 0);
        GRIB_CHECK_NOLINE(grib_nearest_find(nearest, hh, lat, lon, mode,
                                            options->lats, options->lons, options->mask_values, options->distances, options->indexes, &size),
                          0);
        grib_nearest_delete(nearest);
        nearest = NULL;
        grib_handle_delete(hh);
        options->latlon_idx = -1;
        max                 = options->distances[0];
        for (i = 0; i < LATLON_SIZE; i++)
            if (max < options->distances[i]) {
                max = options->distances[i];
            }
        min         = max;
        min_overall = max;
        for (i = 0; i < LATLON_SIZE; i++) {
            if (min_overall >= options->distances[i]) { 
                min_overall = options->distances[i];
                idx_overall = i;
            }
            if ((min >= options->distances[i]) && (options->mask_values[i] >= 0.5)) {
                land_found = 1; 
                min        = options->distances[i];
                idx        = i;
            }
        }
        if (land_found) {
            options->latlon_idx = idx;
        }
        else {
            options->latlon_idx = idx_overall; 
        }
        if (options->latlon_idx < 0) {
            min                 = 0;
            options->latlon_idx = 0;
            for (i = 1; i < LATLON_SIZE; i++)
                if (min > options->distances[i]) {
                    min                 = options->distances[i];
                    options->latlon_idx = i;
                }
        }
    }
    if (json_latlon)
        printf("[\n");
    return 0;
}
int grib_tool_init ( grib_runtime_options * options ) { char * theEnd = NULL , * end1 = NULL ; size_t size = 4 ; int ret = 0 ; double min = 0 , max = 0 ; int i = 0 ; char * p = NULL ; if ( options -> latlon && grib_options_on ( " " ) ) { options -> verbose = 0 ; json_latlon = 1 ; } if ( options -> latlon ) { lat = strtod ( options -> latlon , & theEnd ) ; if ( * theEnd != ' ' ) { fprintf ( stderr , " \n " , tool_name ) ; exit ( 1 ) ; } lon = strtod ( ++ theEnd , & end1 ) ; mode = GRIB_NEAREST_SAME_POINT | GRIB_NEAREST_SAME_GRID ; if ( end1 && * end1 == ' ' ) { end1 ++ ; if ( * end1 != ' ' ) { p = end1 ; while ( * p != ' ' && * p != ' \0 ' ) p ++ ; if ( * end1 == ' ' ) { options -> latlon_mode = 4 ; } else if ( * end1 == ' ' ) { options -> latlon_mode = 1 ; } else { fprintf ( stderr , " \n " , tool_name ) ; exit ( 1 ) ; } } Assert ( p ) ; if ( p && * p == ' ' ) { p ++ ; options -> latlon_mask = strdup ( p ) ; } } } if ( options -> latlon && options -> latlon_mask ) { grib_handle * hh ; int idx = 0 , land_found = 0 ; double min_overall = 0.0 ; int idx_overall = -1 ; FILE * f = fopen ( options -> latlon_mask , " " ) ; if ( ! f ) { perror ( options -> latlon_mask ) ; exit ( 1 ) ; } hh = grib_handle_new_from_file ( 0 , f , & ret ) ; fclose ( f ) ; GRIB_CHECK_NOLINE ( ret , 0 ) ; nearest = grib_nearest_new ( hh , & ret ) ; GRIB_CHECK_NOLINE ( ret , 0 ) ; GRIB_CHECK_NOLINE ( grib_nearest_find ( nearest , hh , lat , lon , mode , options -> lats , options -> lons , options -> mask_values , options -> distances , options -> indexes , & size ) , 0 ) ; grib_nearest_delete ( nearest ) ; nearest = NULL ; grib_handle_delete ( hh ) ; options -> latlon_idx = -1 ; max = options -> distances [ 0 ] ; for ( i = 0 ; i < LATLON_SIZE ; i ++ ) if ( max < options -> distances [ i ] ) { max = options -> distances [ i ] ; } min = max ; min_overall = max ; for ( i = 0 ; i < LATLON_SIZE ; i ++ ) { if ( min_overall >= options -> distances [ i ] ) { min_overall = options -> distances [ i ] ; idx_overall = i ; } if ( ( min >= options -> distances [ i ] ) && ( options -> mask_values [ i ] >= 0.5 ) ) { land_found = 1 ; min = options -> distances [ i ] ; idx = i ; } } if ( land_found ) { options -> latlon_idx = idx ; } else { options -> latlon_idx = idx_overall ; } if ( options -> latlon_idx < 0 ) { min = 0 ; options -> latlon_idx = 0 ; for ( i = 1 ; i < LATLON_SIZE ; i ++ ) if ( min > options -> distances [ i ] ) { min = options -> distances [ i ] ; options -> latlon_idx = i ; } } } if ( json_latlon ) printf ( " \n " ) ; return 0 ; }
The options have been parsed and the structure
grib_runtime_options* options has been loaded.
The options have been parsed and the structure grib_runtime_options * options has been loaded .
/* See GRIB-213 */ /* find overall min and index ignoring mask */ /* found at least one point which is land */ /* all points were sea, so pick the min overall */