Esempio n. 1
0
 def test_size(self):
     np.testing.assert_array_equal(
         [2, 3],
         gridpp.Grid([[0, 0, 0], [1, 1, 1]], [[0, 1, 2], [0, 1, 2]]).size())
     np.testing.assert_array_equal([0, 0],
                                   gridpp.Grid([[], []], [[], []]).size())
Esempio n. 2
0
 def test_get_box_almost_empty(self):
     """Check case where grid is 1x2 and no box exists"""
     grid = gridpp.Grid([[0, 0]], [[0, 1]])
     self.assertFalse(grid.get_box(0.4, 1.25)[0])
Esempio n. 3
0
 def test_get_box_empty(self):
     """Check case where grid is empty"""
     grid = gridpp.Grid()
     self.assertFalse(grid.get_box(0.4, 1.25)[0])
Esempio n. 4
0
def main():
    parser = argparse.ArgumentParser(
        description='Runs gridpp benchmarks for processing performance')
    parser.add_argument('-j',
                        type=int,
                        help='Run multiple cores',
                        dest='num_cores')
    parser.add_argument(
        '-s',
        type=int,
        default=1,
        help='Enlarge the inputs by this scaling factor to run a bigger test',
        dest='scaling')
    parser.add_argument('-n',
                        type=int,
                        default=1,
                        help='Number of iterations to average over',
                        dest='iterations')
    parser.add_argument('-t', help='Run only this function', dest="function")

    # if len(sys.argv) == 1:
    #     parser.print_help()
    #     sys.exit(1)

    args = parser.parse_args()

    input = dict()
    grids = dict()
    points = dict()
    np.random.seed(1000)

    for i in [10, 50, 100, 200, 500, 1000, 2000, 10000]:
        input[i] = np.random.rand(i * args.scaling, i) * 10
    for i in [10, 50, 100, 200, 500, 1000]:
        grids[i] = gridpp.Grid(*np.meshgrid(
            np.linspace(0, 1, i), np.linspace(0, 1, i * args.scaling)))
    for i in [1000, 100000]:
        # points[i] = gridpp.Points(np.linspace(0, 1, i), np.zeros(i))
        points[i] = gridpp.Points(
            np.random.rand(i) * 10,
            np.random.rand(i) * 10)
    structure = gridpp.BarnesStructure(10000)
    radius = 7
    quantile = 0.5
    thresholds = np.linspace(0, 1, 11)
    run = collections.OrderedDict()
    run[(gridpp.Grid, "1000²")] = {
        "expected":
        0.74,
        "args":
        np.meshgrid(np.linspace(0, 1, 1000 * args.scaling),
                    np.linspace(0, 1, 1000))
    }
    run[(gridpp.neighbourhood, "10000²")] = {
        "expected": 2.05,
        "args": (np.zeros([10000, 10000]), radius, gridpp.Mean)
    }
    run[(gridpp.neighbourhood, "2000² max")] = {
        "expected": 0.99,
        "args": (input[2000], radius, gridpp.Max)
    }
    run[(gridpp.neighbourhood_quantile_fast, "2000²")] = {
        "expected": 1.23,
        "args": (input[2000], quantile, radius, thresholds)
    }
    run[(gridpp.neighbourhood_quantile, "500²")] = {
        "expected": 1.70,
        "args": (input[500], quantile, radius)
    }
    run[(gridpp.bilinear, "1000²")] = {
        "expected": 1.68,
        "args": (grids[1000], grids[1000], input[1000])
    }
    run[(gridpp.bilinear, "1000² x 50")] = {
        "expected":
        4.42,
        "args": (grids[1000], grids[1000],
                 np.repeat(np.expand_dims(input[1000], 0), 50, axis=0))
    }
    run[(gridpp.nearest, "1000²")] = {
        "expected": 1.52,
        "args": (grids[1000], grids[1000], input[1000])
    }
    run[(gridpp.nearest, "1000² x 50")] = {
        "expected":
        2.30,
        "args": (grids[1000], grids[1000],
                 np.repeat(np.expand_dims(input[1000], 0), 50, axis=0))
    }
    run[(gridpp.optimal_interpolation, "1000² 1000")] = {
        "expected":
        1.57,
        "args": (grids[1000], input[1000], points[1000], np.zeros(1000),
                 np.ones(1000), np.ones(1000), structure, 20)
    }
    run[(gridpp.dewpoint, "1e7")] = {
        "expected": 0.53,
        "args": (np.zeros(10000000) + 273.15, np.zeros(10000000))
    }
    run[(gridpp.fill, "1e5")] = {
        "expected":
        0.52,
        "args": (grids[1000], np.zeros([1000, 1000]), points[100000],
                 np.ones(100000) * 5000, 1, False)
    }
    run[(gridpp.doping_square, "1e5")] = {
        "expected":
        0.16,
        "args": (grids[1000], np.zeros([1000, 1000]), points[100000],
                 np.ones(100000) * 1, np.ones(100000, 'int') * 5, False)
    }
    run[(gridpp.doping_circle, "1e5")] = {
        "expected":
        0.52,
        "args": (grids[1000], np.zeros([1000, 1000]), points[100000],
                 np.ones(100000) * 1, np.ones(100000) * 5000, False)
    }

    print("Gridpp version %s" % gridpp.version())
    if args.num_cores is not None:
        print(
            "Function                             Expected     Time     Diff    Scaling"
        )
    else:
        print(
            "Function                             Expected     Time     Diff")
    num_cores = [1]
    if args.num_cores is not None and args.num_cores != 1:
        num_cores += [args.num_cores]
    for key in run.keys():
        timings = dict()
        for num_core in num_cores:
            timings[num_core] = 0

        if isinstance(key, tuple):
            name = key[0].__name__ + " " + str(key[1])
            func = key[0]
        else:
            name = key.__name__
            func = key
        if args.function is not None:
            if func.__name__ != args.function:
                continue

        # Allow functions to fail (useful when benchmarking older versions of gridpp
        # where functions may not be defined).
        try:
            for num_core in num_cores:
                gridpp.set_omp_threads(num_core)
                for it in range(args.iterations):
                    s_time = time.time()
                    func(*run[key]["args"])
                    e_time = time.time()
                    curr_time = e_time - s_time
                    timings[num_core] += curr_time
        except Exception as e:
            print("Could not run %s" % key)
            continue

            # print("%s() Expected: %.2f s Time: %.2f s" % (func.__name__, run[key]["expected"], e_time - s_time))
        for num_core in num_cores:
            timings[num_core] /= args.iterations

        diff = (timings[1] - run[key]["expected"] * args.scaling) / (
            run[key]["expected"] * args.scaling) * 100
        string = "%-36s %8.2f %8.2f %8.2f %%" % (
            name, run[key]["expected"] * args.scaling, timings[1], diff)
        if args.num_cores is not None:
            scaling = timings[1] / timings[args.num_cores] / args.num_cores
            expected = timings[1] / args.num_cores
            scaling = 1 - (timings[args.num_cores] - expected) / (timings[1] -
                                                                  expected)
            # scaling = (1 - timings[args.num_cores] / timings[1]) * (args.num_cores + 1)

            string += " %8.2f %%" % (scaling * 100)
        print(string)
Esempio n. 5
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)
Esempio n. 6
0
from __future__ import print_function
import unittest
import gridpp
import numpy as np


lons, lats = np.meshgrid([0, 1, 2], [0, 1, 2])
grid = gridpp.Grid(lats, lons)
points = gridpp.Points([0, 1], [0, 1])


class Test(unittest.TestCase):
    def test_invalid_radii(self):
        values = np.zeros([3, 3])
        radii = [-1, -1]
        value = 1
        for outside in [False, True]:
            with self.assertRaises(Exception) as e:
                gridpp.fill(grid, values, points, radii, value, outside)

    def test_invalid_number_of_radii(self):
        values = np.zeros([3, 3])
        for outside in [False, True]:
            with self.assertRaises(Exception) as e:
                gridpp.fill(grid, values, points, [1], value, outside)

    def test_dimension_mismatch(self):
        values = np.zeros([3, 2])
        radii = [1, 1]
        value = 1
        for outside in [False, True]:
mpl.gca().set_aspect(2)
cb = mpl.colorbar()
cb.set_label(r"Temperature ($\degree C$)")
mpl.savefig("titan_sct.png", bbox_inches='tight', dpi=125)
mpl.clf()

# Read NWP background from file
import gridpp
import numpy as np

with netCDF4.Dataset('analysis.nc', 'r') as file:
    index_control = 0
    blats_2500m = file.variables['latitude'][:]
    blons_2500m = file.variables['longitude'][:]
    belevs_2500m = file.variables['surface_geopotential'][0, 0, index_control, :, :] / 9.81
    bgrid_2500m = gridpp.Grid(blats_2500m, blons_2500m, belevs_2500m)
    background_2500m = file.variables['air_temperature_2m'][0, 0, index_control, :, :]

with netCDF4.Dataset('template.nc', 'r') as file:
    blats = file.variables['latitude'][:]
    blons = file.variables['longitude'][:]
    belevs = file.variables['altitude'][:]
    bgrid = gridpp.Grid(blats, blons, belevs)

# Downscaling
gradient = -0.0065
background = gridpp.simple_gradient(bgrid_2500m, bgrid, background_2500m, gradient)

points = gridpp.Points(obs_lats[index_valid_obs], obs_lons[index_valid_obs], obs_elevs[index_valid_obs])
pbackground = gridpp.bilinear(bgrid, points, background)
Esempio n. 8
0
def make_grid(lats, lons):
    latlats, lonlons = np.meshgrid(lats, lons)
    ECMWF_grid = gridpp.Grid(latlats, lonlons)
    return ECMWF_grid