Пример #1
0
def test_grib_nearest():
    gid = eccodes.codes_grib_new_from_samples("reduced_gg_ml_grib2")
    lat, lon = 30, -20
    nearest = eccodes.codes_grib_find_nearest(gid, lat, lon)[0]
    assert nearest.index == 1770
    lat, lon = 10, 0
    nearest = eccodes.codes_grib_find_nearest(gid, lat, lon)[0]
    assert nearest.index == 2545
    lat, lon = 10, 20
    nearest = eccodes.codes_grib_find_nearest(gid, lat, lon, False, 4)
    expected_indexes = (2553, 2552, 2425, 2424)
    returned_indexes = (
        nearest[0].index,
        nearest[1].index,
        nearest[2].index,
        nearest[3].index,
    )
    assert sorted(expected_indexes) == sorted(returned_indexes)
    # Cannot do more than 4 nearest neighbours
    with pytest.raises(ValueError):
        eccodes.codes_grib_find_nearest(gid, lat, lon, False, 5)
    eccodes.codes_release(gid)
Пример #2
0
def nearest_parallel_step(chunk, gid, mv):
    lat, lon, x, y = chunk

    idx = int_fill_value
    if not (lon <= -1.0e+10 or lon == mv):
        try:
            n_nearest = eccodes.codes_grib_find_nearest(
                gid, lat.item(), lon.item())
        except eccodes.GribInternalError:
            x = int_fill_value
            y = int_fill_value
        else:
            idx = n_nearest[0]['index']
    return int(x), int(y), idx
def cli(file_path):
    with open(file_path, 'rb') as f:
        handle = eccodes.codes_grib_new_from_file(f, headers_only=False)
        while handle is not None:
            date = eccodes.codes_get(handle, "dataDate")
            type_of_level = eccodes.codes_get(handle, "typeOfLevel")
            level = eccodes.codes_get(handle, "level")

            points = eccodes.codes_grib_find_nearest(handle, 39.92, 116.46,
                                                     False, 1)
            point = points[0]

            print(date, type_of_level, level, " :", point.lat, point.lon,
                  point.value, point.distance)

            eccodes.codes_release(handle)
            handle = eccodes.codes_grib_new_from_file(f, headers_only=False)
Пример #4
0
def invdist_parallel_step(chunk, gid, mv):
    lat, lon, x, y = chunk
    idx1 = idx2 = idx3 = idx4 = int_fill_value
    inv1 = inv2 = inv3 = inv4 = np.NaN
    if not (lon < -1.0e+10 or lon == mv):
        try:
            n_nearest = eccodes.codes_grib_find_nearest(gid,
                                                        lat.item(),
                                                        lon.item(),
                                                        npoints=4)
        except eccodes.GribInternalError:
            # tipically "out of grid" error
            x = int_fill_value
            y = int_fill_value
        else:
            inv1, inv2, inv3, inv4, idx1, idx2, idx3, idx4 = _compute_coeffs_and_idxs(
                n_nearest)
    return x, y, idx1, idx2, idx3, idx4, inv1, inv2, inv3, inv4
Пример #5
0
def grib_nearest(gid, target_lats, target_lons, mv):
    num_cells = target_lons.size
    indices = np.indices(target_lons.shape)
    valid_target_coords = (target_lons > -1.0e+10) & (target_lons != mv)
    xs = np.where(valid_target_coords, indices[0], int_fill_value).ravel()
    ys = np.where(valid_target_coords, indices[1], int_fill_value).ravel()
    idxs = empty(num_cells, fill_value=int_fill_value, dtype=int)

    back_char, progress_step = progress_step_and_backchar(num_cells)
    format_progress = '{}Nearest neighbour interpolation: {}/{}  [outs: {}] ({}%)'.format
    i = 0
    outs = 0
    stdout.write('Start interpolation: {}\n'.format(now_string()))
    stdout.write(format_progress(back_char, 0, num_cells, outs, 0))
    stdout.flush()

    for lat, lon in zip(target_lats.flat, target_lons.flat):
        if i % progress_step == 0:
            stdout.write(
                format_progress(back_char, i, num_cells, outs,
                                i * 100. / num_cells))
            stdout.flush()
        if not (lon <= -1.0e+10 or lon == mv):
            try:
                n_nearest = eccodes.codes_grib_find_nearest(
                    gid, lat.item(), lon.item())
            except eccodes.GribInternalError:
                outs += 1
                xs[i] = int_fill_value
                ys[i] = int_fill_value
            else:
                idxs[i] = n_nearest[0]['index']
        i += 1
    stdout.write('{}{:>100}'.format(back_char, ' '))
    stdout.write(format_progress(back_char, i, num_cells, outs, 100))
    stdout.write('End interpolation: {}\n\n'.format(now_string()))
    stdout.flush()
    return (xs[xs != int_fill_value], ys[ys != int_fill_value],
            idxs[idxs != int_fill_value])
Пример #6
0
def grib_invdist(gid, target_lats, target_lons, mv):
    num_cells = target_lons.size
    indices = np.indices(target_lons.shape)
    valid_target_coords = (target_lons > -1.0e+10) & (target_lons != mv)
    xs = np.where(valid_target_coords, indices[0], int_fill_value).ravel()
    ys = np.where(valid_target_coords, indices[1], int_fill_value).ravel()
    idxs1 = empty(num_cells, fill_value=int_fill_value, dtype=int)
    idxs2 = empty(num_cells, fill_value=int_fill_value, dtype=int)
    idxs3 = empty(num_cells, fill_value=int_fill_value, dtype=int)
    idxs4 = empty(num_cells, fill_value=int_fill_value, dtype=int)
    invs1 = empty(num_cells)
    invs2 = empty(num_cells)
    invs3 = empty(num_cells)
    invs4 = empty(num_cells)

    format_progress = '{}Inverse distance interpolation: {}/{}  [outs: {}] ({}%)'.format
    i = 0
    outs = 0
    back_char, progress_step = progress_step_and_backchar(num_cells)
    stdout.write('Start interpolation: {}\n'.format(now_string()))
    stdout.write(format_progress(back_char, 0, num_cells, outs, 0))
    stdout.flush()

    for lat, lon in zip(target_lats.flat, target_lons.flat):
        if i % progress_step == 0:
            stdout.write(
                format_progress(back_char, i, num_cells, outs,
                                i * 100. / num_cells))
            stdout.flush()
        if not (lon < -1.0e+10 or lon == mv):

            try:
                n_nearest = eccodes.codes_grib_find_nearest(gid,
                                                            lat.item(),
                                                            lon.item(),
                                                            npoints=4)
            except eccodes.GribInternalError:
                # tipically "out of grid" error
                outs += 1
                xs[i] = int_fill_value
                ys[i] = int_fill_value
            else:
                invs1[i], invs2[i], invs3[i], invs4[i], idxs1[i], idxs2[
                    i], idxs3[i], idxs4[i] = _compute_coeffs_and_idxs(
                        n_nearest)
        i += 1

    # variables seems unused but they are in numexpress expressions (see ne.evaluate())
    # DO NOT DELETE
    invs1 = invs1[~np.isnan(invs1)]
    invs2 = invs2[~np.isnan(invs2)]
    invs3 = invs3[~np.isnan(invs3)]
    invs4 = invs4[~np.isnan(invs4)]
    sums = ne.evaluate('invs1 + invs2 + invs3 + invs4')
    coeffs1 = ne.evaluate('invs1 / sums')
    coeffs2 = ne.evaluate('invs2 / sums')
    coeffs3 = ne.evaluate('invs3 / sums')
    coeffs4 = ne.evaluate('invs4 / sums')
    stdout.write('{}{:>100}'.format(back_char, ' '))
    stdout.write(format_progress(back_char, i, num_cells, outs, 100))
    stdout.write('End interpolation: {}\n\n'.format(now_string()))
    stdout.flush()
    return (xs[xs != int_fill_value], ys[ys != int_fill_value],
            idxs1[idxs1 != int_fill_value], idxs2[idxs2 != int_fill_value],
            idxs3[idxs3 != int_fill_value], idxs4[idxs4 != int_fill_value],
            coeffs1, coeffs2, coeffs3, coeffs4)
Пример #7
0
def nearest_value_func(gid, args):
    lat, lon = args
    nearest = codes_grib_find_nearest(gid, lat, lon)[0]
    return lat, lon, nearest.value