Beispiel #1
0
    def __init__(self, operator, geo, dataset, grid_values, max_distance=5000):

        grid_lons = np.asarray(geo.lons)
        grid_lats = np.asarray(geo.lats)
        grid = gridpp.Grid(grid_lats, grid_lons)
        lons = dataset.lons
        lats = dataset.lats
        points = gridpp.Points(lats, lons)

        if operator == "nearest":
            obs_values = gridpp.nearest(grid, points, grid_values)
        elif operator == "bilinear":
            obs_values = gridpp.bilinear(grid, points, grid_values)
        else:
            raise NotImplementedError(operator)

        inside_grid = []
        # Check if they are in grid
        for i in range(0, len(obs_values)):
            lon = lons[i]
            lat = lats[i]
            nn = grid.get_num_neighbours(lat, lon, max_distance)
            # print(i, lons[i], lats[i], obs_values[i], nn)
            if nn == 0:
                inside_grid.append(False)
            else:
                inside_grid.append(True)

        self.inside_grid = inside_grid
        self.obs_values = obs_values
Beispiel #2
0
 def test_points_to_points(self):
     """Check that point to point interpolation works"""
     ipoints = gridpp.Points([0, 5, 10], [0, 5, 10])
     points = gridpp.Points([-1, 6], [-1, 6])
     values = [0, 1, 2]
     output = gridpp.nearest(ipoints, points, values)
     np.testing.assert_array_equal(output, [0, 1])
Beispiel #3
0
 def test_grid_to_points_3d(self):
     """Check that grid to point interpolation for 3D fields works"""
     lons, lats = np.meshgrid([0, 10, 20], [0, 10, 20])
     grid = gridpp.Grid(lats, lons)
     points = gridpp.Points([-1, 6], [-1, 6])
     values = np.reshape(range(18), [2, lons.shape[0], lons.shape[1]])
     output = gridpp.nearest(grid, points, values)
     np.testing.assert_array_equal(output, [[0, 4], [9, 13]])
Beispiel #4
0
 def test_grid_to_grid(self):
     """Check that grid to grid interpolation works"""
     lons1, lats1 = np.meshgrid([0, 10, 20], [30, 40, 50])
     lons2, lats2 = np.meshgrid([0, 20], [30, 50])
     grid1 = gridpp.Grid(lats1, lons1)
     grid2 = gridpp.Grid(lats2, lons2)
     values = np.reshape(range(9), lons1.shape)
     output = gridpp.nearest(grid1, grid2, values)
     np.testing.assert_array_equal(output, [[0, 2], [6, 8]])
Beispiel #5
0
 def test_one_row(self):
     gridpp.set_omp_threads(1)
     lons, lats = np.meshgrid([0], [30, 40, 50])
     grid = gridpp.Grid(lats, lons)
     values = np.zeros(lons.shape)
     values[:] = np.reshape(range(3), lons.shape)
     points = gridpp.Points([25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55], [-1, -1, -1, 0, 0, 0, 10, 10, 10, 20, 20, 20, 21, 21, 21])
     output = gridpp.nearest(grid, points, values)
     np.testing.assert_array_equal(output, (0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2))
Beispiel #6
0
 def test_grid_to_grid_3d(self):
     """Check that grid to grid interpolation for 3D fields works"""
     lons1, lats1 = np.meshgrid([0, 10, 20], [30, 40, 50])
     lons2, lats2 = np.meshgrid([0, 20], [30, 50])
     grid1 = gridpp.Grid(lats1, lons1)
     grid2 = gridpp.Grid(lats2, lons2)
     values = np.reshape(range(18), [2, lons1.shape[0], lons1.shape[1]])
     output = gridpp.nearest(grid1, grid2, values)
     np.testing.assert_array_equal(output,
                                   [[[0, 2], [6, 8]], [[9, 11], [15, 17]]])
Beispiel #7
0
    def test_cross_validation_grid(self):
        """ Check that the CV structure function works """
        np.random.seed(1000)
        y, x = np.meshgrid(np.arange(0, 3500, 500), np.arange(0, 3500, 500))
        Y = y.shape[0]
        X = y.shape[1]
        grid = gridpp.Grid(y, x, np.zeros(x.shape), np.zeros(x.shape),
                           gridpp.Cartesian)
        background = np.random.rand(Y, X) * 0

        obs = np.array([10, 20, 30])
        x_o = np.array([1000, 2000, 3000])
        y_o = np.array([1000, 2000, 3000])
        N = len(obs)
        points = gridpp.Points(y_o, x_o, np.zeros(N), np.zeros(N),
                               gridpp.Cartesian)

        background_o = gridpp.nearest(grid, points, background)

        ratios = np.ones(N)
        k = 0
        ii = np.arange(N).astype('int') != k
        points_cv = gridpp.Points(y_o[ii], x_o[ii], np.zeros(N - 1),
                                  np.zeros(N - 1), gridpp.Cartesian)
        structure = gridpp.BarnesStructure(1000, 0)
        structure_cv = gridpp.CrossValidation(structure, 750)

        analysis = gridpp.optimal_interpolation(grid, background, points_cv,
                                                obs[ii], ratios[ii],
                                                background_o[ii], structure,
                                                100)
        analysis_cv = gridpp.optimal_interpolation(points, background_o,
                                                   points, obs, ratios,
                                                   background_o, structure_cv,
                                                   100)

        self.assertAlmostEqual(
            gridpp.nearest(grid, points, analysis)[k], analysis_cv[k])
Beispiel #8
0
    def test_dimension_mismatch(self):
        lons, lats = np.meshgrid([0, 10, 20], [30, 40, 50])
        grid = gridpp.Grid(lats, lons)
        points = gridpp.Points([0, 1], [0, 1])
        values = np.zeros([3, 2])
        with self.assertRaises(Exception) as e:
            gridpp.nearest(grid, grid, values)

        with self.assertRaises(Exception) as e:
            gridpp.nearest(grid, points, values)

        values3 = np.zeros([3, 2, 3])
        with self.assertRaises(Exception) as e:
            gridpp.nearest(grid, grid, values3)

        with self.assertRaises(Exception) as e:
            gridpp.nearest(grid, points, values3)
Beispiel #9
0
 def test_neighbourhood(self):
     """Simple check
     50  6  7  8
     40  3  4  5
     30  0  1  2
         0 10 20
     """
     lons, lats = np.meshgrid([0, 10, 20], [30, 40, 50])
     grid = gridpp.Grid(lats, lons)
     values = np.zeros(lons.shape)
     values[:] = np.reshape(range(9), lons.shape)
     points = gridpp.Points([25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55], [-1, -1, -1, 0, 0, 0, 10, 10, 10, 20, 20, 20, 21, 21, 21])
     output = gridpp.nearest(grid, points, values)
     np.testing.assert_array_equal(output, (0, 3, 6, 0, 3, 6, 1, 4, 7, 2, 5, 8, 2, 5, 8))
     output = gridpp.bilinear(grid, points, values)
     np.testing.assert_array_equal(output, (0, 3, 6, 0, 3, 6, 1, 4, 7, 2, 5, 8, 2, 5, 8))
Beispiel #10
0
 def test_grid_to_point(self):
     """Check that grid to point interpolation works
     50  6  7  8
     40  3  4  5
     30  0  1  2
         0 10 20
     """
     for num_treads in [1, 2]:
         gridpp.set_omp_threads(num_treads)
         lons, lats = np.meshgrid([0, 10, 20], [30, 40, 50])
         grid = gridpp.Grid(lats, lons)
         values = np.reshape(range(9), lons.shape)
         points = gridpp.Points(
             [25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55, 25, 40, 55],
             [-1, -1, -1, 0, 0, 0, 10, 10, 10, 20, 20, 20, 21, 21, 21])
         output = gridpp.nearest(grid, points, values)
         np.testing.assert_array_equal(
             output, (0, 3, 6, 0, 3, 6, 1, 4, 7, 2, 5, 8, 2, 5, 8))
         output = gridpp.bilinear(grid, points, values)
         np.testing.assert_array_equal(
             output, (0, 3, 6, 0, 3, 6, 1, 4, 7, 2, 5, 8, 2, 5, 8))
Beispiel #11
0
    model_version=mdl_vrsn,
    var_name_abbr=var_name_abbr,
    cast_type='cf',
    date_str=curr_date,
)

with open(os.path.join(config['BW_DIR'], 'sites.json')) as json_file:
    data_BW = pd.DataFrame(json.load(json_file))

BW_grid = gridpp.Points(data_BW.lat, data_BW.lon)

ECMWF_grid1_5deg = make_grid_object(SST_GRID1_5deg)

SST_1_5deg2point = gridpp.nearest(
    ECMWF_grid1_5deg,
    BW_grid,
    gridpp.fill_missing(np.transpose(SST_GRID1_5deg.sst[0, :, :].data))
)

print('test 1 med grid2points')
print(SST_1_5deg2point)

sst = SST_GRID1_5deg.sst[0, :, :].data.flatten()
valid_points = np.isnan(sst) == 0  # Ocean points

ECMWF_points = make_points_flatten(SST_GRID1_5deg.latitude.data, SST_GRID1_5deg.longitude.data, valid_points)

SST_points = gridpp.nearest(ECMWF_points, BW_grid, sst[valid_points])
print('test 2 med points2points')
print(SST_points)
Beispiel #12
0
def horizontal_oi(geo,
                  background,
                  observations,
                  gelevs,
                  glafs,
                  hlength=10000.,
                  vlength=10000.,
                  wlength=0.5,
                  elev_gradient=0,
                  structure_function="Barnes",
                  land_only=False,
                  max_locations=50,
                  epsilon=0.5,
                  minvalue=None,
                  maxvalue=None,
                  interpol="bilinear"):

    if gridpp is None:
        raise Exception("You need gridpp to perform OI")

    glats = geo.lats
    glons = geo.lons

    def obs2vectors(my_obs):
        return my_obs.lons, my_obs.lats, my_obs.stids, my_obs.elevs, \
               my_obs.values, my_obs.cis, my_obs.lafs

    vectors = np.vectorize(obs2vectors)
    lons, lats, stids, elevs, values, pci, lafs = vectors(observations)

    glats = np.transpose(glats)
    glons = np.transpose(glons)
    background = np.transpose(background)
    gelevs = np.transpose(gelevs)
    glafs = np.transpose(glafs)

    bgrid = gridpp.Grid(glats, glons, gelevs)
    points = gridpp.Points(lats, lons, elevs)
    if interpol == "bilinear":
        pbackground = gridpp.bilinear(bgrid, points, background)
    elif interpol == "nearest":
        pbackground = gridpp.nearest(bgrid, points, background, elev_gradient)
    else:
        raise NotImplementedError
    variance_ratios = np.full(points.size(), epsilon)

    if structure_function == "Barnes":
        # No land/sea weight when land_only = True
        if land_only:
            wlength = 0.
        structure = gridpp.BarnesStructure(hlength, vlength, wlength)
    else:
        raise NotImplementedError

    field = gridpp.optimal_interpolation(bgrid, background, points, values,
                                         variance_ratios, pbackground,
                                         structure, max_locations)
    field = np.asarray(field)
    if minvalue is not None:
        field[field < minvalue] = minvalue
    if maxvalue is not None:
        field[field > maxvalue] = maxvalue
    if land_only:
        no_land = np.where(glafs == 0)
        field[no_land] = background[no_land]
    return np.transpose(field)