def setUp(self): lat, lon = np.arange(180) - 90, np.arange(360) - 180 self.lats, self.lons = np.meshgrid(lat, lon) self.lats, self.lons = self.lats.flatten(), self.lons.flatten() self.cells = grids.lonlat2cell(self.lons, self.lats) self.subset = np.sort(np.random.choice(np.arange(self.lats.size), size=500, replace=False)) self.basic = grids.BasicGrid(self.lons, self.lats, subset=self.subset, shape=(360, 180)) self.basic_shape_gpis = grids.BasicGrid(self.lons, self.lats, gpis=np.arange(self.lats.size), subset=self.subset, shape=(360, 180)) self.basic_generated = grids.genreg_grid(1, 1) self.basic_irregular = grids.BasicGrid(np.random.random(360 * 180) * 360 - 180, np.random.random( 360 * 180) * 180 - 90, subset=self.subset) self.cellgrid = grids.CellGrid(self.lons, self.lats, self.cells, subset=self.subset) self.cellgrid_shape = grids.CellGrid(self.lons, self.lats, self.cells, subset=self.subset, shape=(360, 180)) self.testfile = tempfile.NamedTemporaryFile().name
def setUp(self): self.latdim = np.arange(90, -90, -2.5) self.londim = np.arange(-180, 180, 2.5) self.lon, self.lat = np.meshgrid(self.londim, self.latdim) self.grid = grids.BasicGrid(self.lon.flatten(), self.lat.flatten(), gpis=np.arange(self.lon.flatten().size), shape=(len(self.latdim), len(self.londim))) self.reverse_gpi_grid = grids.BasicGrid( self.lon.flatten(), self.lat.flatten(), gpis=np.arange(self.lon.flatten().size)[::-1], shape=(len(self.latdim), len(self.londim))) self.cellgrid = self.grid.to_cell_grid()
def setUp(self): self.latdim = np.arange(90, -90, -2.5) self.londim = np.arange(-180, 180, 2.5) self.lon, self.lat = np.meshgrid(self.londim, self.latdim) self.grid = grids.BasicGrid(self.lon.flatten(), self.lat.flatten(), shape=(len(self.latdim), len(self.londim)))
def setUp(self): """ Setup grid and shp_file to check if grid points fall in shp. """ lat = np.arange(-90, 90, 1) lon = np.arange(-180, 180, 1) self.lons, self.lats = np.meshgrid(lon, lat) self.grid = grids.BasicGrid(self.lon.flatten(), self.lat.flatten()) ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(14, 45) ring.AddPoint(14, 47) ring.AddPoint(16, 47) ring.AddPoint(16, 45) ring.AddPoint(14, 45) poly = ogr.Geometry(ogr.wkbPolygon) poly.AddGeometry(ring) self.fname = 'test.shp' drv = ogr.GetDriverByName('ESRI Shapefile') ds = drv.CreateDataSource(self.fname) srs = osr.SpatialReference() srs.ImportFromEPSG(4326) layer = ds.CreateLayer(self.fname, srs, ogr.wkbPolygon) featureDefn = layer.GetLayerDefn() feature = ogr.Feature(featureDefn) feature.SetGeometry(poly) layer.CreateFeature(feature) self.shp = feature.GetGeometryRef()
def setUp(self): self.latdim = np.arange(90, -90, -2.5) self.londim = np.arange(-180, 180, 2.5) self.lon, self.lat = np.meshgrid(self.londim, self.latdim) self.grid = grids.BasicGrid(self.lon.flatten(), self.lat.flatten(), gpis=np.arange(self.lon.flatten().size), shape=(len(self.londim), len(self.latdim)), subset=np.arange(self.lon.flatten().size / 2, dtype=np.int)) self.cellgrid = self.grid.to_cell_grid()
def setUp(self): """ Setup two grids with similar gpis but with different subset/gpi ordering. The lookup tables should still give the correct results. The gpi's of the two grids are identical. """ self.lats = np.array([1, 2, 3, 4]) self.lons = np.array([1, 2, 3, 4]) self.gpis = [0, 1, 2, 3] self.subset = [3, 2] self.lats2 = np.array([3, 4, 2, 1]) self.lons2 = np.array([3, 4, 2, 1]) self.gpis2 = [2, 3, 1, 0] self.subset2 = [0, 1] self.grid1 = grids.BasicGrid(self.lons, self.lats, gpis=self.gpis, subset=self.subset) self.grid2 = grids.BasicGrid(self.lons2, self.lats2, gpis=self.gpis2, subset=self.subset2)
def test_gpi2rowcol_custom_gpis(self): """ Test if gpi to row column lookup works correctly. """ self.custom_gpi_grid = grids.BasicGrid( self.lon.flatten(), self.lat.flatten(), shape=(len(self.latdim), len(self.londim)), gpis=np.arange(len(self.lat.flatten()))[::-1]) gpi = [200, 255] row_should = [70, 70] column_should = [87, 32] row, column = self.custom_gpi_grid.gpi2rowcol(gpi) assert np.all(row == row_should) assert np.all(column == column_should)
def test_store_load_regular_2D_grid(): """ Test the storing/loading of a 2D grid when the gpis are in a custom ordering. """ londim = np.arange(-180.0, 180.0, 60) latdim = np.arange(90.0, -90.0, -30) lons, lats = np.meshgrid(londim, latdim) gpis = np.arange(lons.flatten().size).reshape(lons.shape) grid = grids.BasicGrid(lons.flatten(), lats.flatten(), gpis.flatten(), shape=lons.shape) testfile = tempfile.NamedTemporaryFile().name grid_nc.save_grid(testfile, grid) grid_loaded = grid_nc.load_grid(testfile) assert grid == grid_loaded
def test_gpi2cell_custom_gpis(self): """ Test if gpi to row column lookup works correctly. """ self.custom_gpi_grid = \ grids.BasicGrid(self.lon.flatten(), self.lat.flatten(), shape=(len(self.londim), len(self.latdim)), gpis=np.arange(len(self.lat.flatten()))[::-1]) self.custom_gpi_cell_grid = self.custom_gpi_grid.to_cell_grid() gpi = [200, 255] cell = self.custom_gpi_cell_grid.gpi2cell(gpi) assert np.all(cell == [1549, 577]) gpi = 200 cell = self.custom_gpi_cell_grid.gpi2cell(gpi) assert cell == 1549
def test_nearest_neighbor_max_dist(self): # test with maxdist higher than nearest point gpi, dist = self.grid.find_nearest_gpi(14.3, 18.5, max_dist=100e3) assert gpi == 25754 assert len([dist]) == 1 lon, lat = self.grid.gpi2lonlat(gpi) assert lon == 14.5 assert lat == 18.5 # test with maxdist lower than nearest point gpi, dist = self.grid.find_nearest_gpi(14.3, 18.5, max_dist=10000) assert len(gpi) == 0 assert len(dist) == 0 # test with custom gpi, see issue #68 grid = grids.BasicGrid(lon=[16, 17], lat=[45, 46], gpis=[100, 200]) gpi, dist = grid.find_nearest_gpi(0, 0, max_dist=1000) assert len(gpi) == 0 assert len(dist) == 0
def setUp(self): """ Setup grid and shp_file to check if grid points fall in shp. """ lat = np.arange(-90, 90, 1) lon = np.arange(-180, 180, 1) self.lons, self.lats = np.meshgrid(lon, lat) self.grid = grids.BasicGrid(self.lons.flatten(), self.lats.flatten()) ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(14, 45) ring.AddPoint(14, 47) ring.AddPoint(16, 47) ring.AddPoint(16, 45) ring.AddPoint(14, 45) poly = ogr.Geometry(ogr.wkbPolygon) poly.AddGeometry(ring) self.shp = poly
def test_setup_grid_with_lists(): grid = grids.BasicGrid([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]) nptest.assert_allclose(grid.arrlon, np.array([1, 2, 3, 4, 5])) nptest.assert_allclose(grid.arrlat, np.array([1, 2, 3, 4, 5]))