Beispiel #1
0
    def test_eraint(self):

        f = get_demo_file('era_interim_tibet.nc')
        d = GeoNetcdf(f)
        assert d.grid.origin == 'upper-left'

        stat_lon = 91.1
        stat_lat = 31.1
        with netCDF4.Dataset(f) as nc:
            nc.set_auto_mask(False)
            flon = nc.variables['longitude'][:]
            flat = nc.variables['latitude'][:]
            alon = np.argmin(np.abs(flon - stat_lon))
            alat = np.argmin(np.abs(flat - stat_lat))

            d.set_subset(corners=((stat_lon, stat_lat), (stat_lon, stat_lat)))
            slon, slat = d.grid.ll_coordinates
            assert_array_equal(flon[alon], slon)
            assert_allclose(flat[alat], slat)
            # Exotic subset
            assert_array_equal(flon[alon], d.get_vardata('longitude'))
            assert_allclose(flat[alat], d.get_vardata('latitude'))

            assert_allclose(nc.variables['t2m'][:, alat, alon],
                            np.squeeze(d.get_vardata('t2m')))

            d.set_period(t0='2012-06-01 06:00:00', t1='2012-06-01 12:00:00')
            assert_allclose(nc.variables['t2m'][1:3, alat, alon],
                            np.squeeze(d.get_vardata('t2m')))
Beispiel #2
0
    def test_har(self):

        # HAR
        hf = get_demo_file('har_d30km_y_2d_t2_2000.nc')
        d = GeoNetcdf(hf)
        reflon = np.squeeze(d.get_vardata('lon'))
        reflat = np.squeeze(d.get_vardata('lat'))
        mylon, mylat = d.grid.ll_coordinates
        np.testing.assert_allclose(reflon, mylon, atol=1e-5)
        np.testing.assert_allclose(reflat, mylat, atol=1e-5)
Beispiel #3
0
    def test_har(self):

        # HAR
        hf = get_demo_file("har_d30km_y_2d_t2_2000.nc")
        d = GeoNetcdf(hf)
        reflon = np.squeeze(d.get_vardata("lon"))
        reflat = np.squeeze(d.get_vardata("lat"))
        mylon, mylat = d.grid.ll_coordinates
        np.testing.assert_allclose(reflon, mylon, atol=1e-5)
        np.testing.assert_allclose(reflat, mylat, atol=1e-5)
Beispiel #4
0
    def test_eraint(self):

        f = get_demo_file("era_interim_tibet.nc")
        d = GeoNetcdf(f)
        assert d.grid.order == "ul"

        stat_lon = 91.1
        stat_lat = 31.1
        nc = netCDF4.Dataset(f)
        flon = nc.variables["longitude"][:]
        flat = nc.variables["latitude"][:]
        alon = np.argmin(np.abs(flon - stat_lon))
        alat = np.argmin(np.abs(flat - stat_lat))

        d.set_subset(corners=((stat_lon, stat_lat), (stat_lon, stat_lat)))
        slon, slat = d.grid.ll_coordinates
        assert_array_equal(flon[alon], slon)
        assert_allclose(flat[alat], slat)
        # Exotic subset
        assert_array_equal(flon[alon], d.get_vardata("longitude"))
        assert_allclose(flat[alat], d.get_vardata("latitude"))

        assert_allclose(nc.variables["t2m"][:, alat, alon], np.squeeze(d.get_vardata("t2m")))

        d.set_period(t0="2012-06-01 06:00:00", t1="2012-06-01 12:00:00")
        assert_allclose(nc.variables["t2m"][1:3, alat, alon], np.squeeze(d.get_vardata("t2m")))
Beispiel #5
0
    def test_diagnostic_vars(self):

        d = WRF(get_demo_file('wrf_tip_d1.nc'))
        d2 = GeoNetcdf(get_demo_file('wrf_tip_d2.nc'))
        self.assertTrue('T2C' in d.variables)

        ref = d.get_vardata('T2')
        tot = d.get_vardata('T2C') + 273.15
        np.testing.assert_allclose(ref, tot)

        d.set_roi(grid=d2.grid)
        d.set_subset(toroi=True)
        ref = d.get_vardata('T2')
        tot = d.get_vardata('T2C') + 273.15

        self.assertEqual(tot.shape[-1] * 3, d2.grid.nx)
        self.assertEqual(tot.shape[-2] * 3, d2.grid.ny)
        np.testing.assert_allclose(ref, tot)

        d = WRF(get_demo_file('wrf_tip_d1.nc'))
        ref = d.variables['T2'][:]
        d.set_subset(margin=-5)
        tot = d.get_vardata('T2')
        assert_array_equal(ref.shape[1] - 10, tot.shape[1])
        assert_array_equal(ref.shape[2] - 10, tot.shape[2])
        assert_array_equal(ref[:, 5:-5, 5:-5], tot)
Beispiel #6
0
    def test_visible(self):
        import matplotlib as mpl

        x = [91.176036, 92.05, 88.880927]
        y = [29.649702, 31.483333, 29.264956]

        g = GoogleVisibleMap(x=x,
                             y=y,
                             size_x=400,
                             size_y=400,
                             maptype='terrain')
        img = g.get_vardata()[..., :3]

        i, j = g.grid.transform(x, y, nearest=True)

        for _i, _j in zip(i, j):
            img[_j - 3:_j + 4, _i - 3:_i + 4, 0] = 1
            img[_j - 3:_j + 4, _i - 3:_i + 4, 1:] = 0

        # from PIL import Image
        # Image.fromarray((img * 255).astype(np.uint8)).save(
        #     get_demo_file('hef_google_visible.png'))
        ref = mpl.image.imread(get_demo_file('hef_google_visible.png'))
        rmsd = np.sqrt(np.mean((ref - img)**2))
        self.assertTrue(rmsd < 1e-1)

        self.assertRaises(ValueError, GoogleVisibleMap, x=x, y=y, zoom=12)

        fw = get_demo_file('wrf_tip_d1.nc')
        d = GeoNetcdf(fw)
        i, j = d.grid.ij_coordinates
        g = GoogleVisibleMap(x=i, y=j, crs=d.grid, size_x=500, size_y=500)
        img = g.get_vardata()[..., :3]
        mask = g.grid.map_gridded_data(i * 0 + 1, d.grid)

        img[np.nonzero(mask)] = np.clip(img[np.nonzero(mask)] + 0.3, 0, 1)

        # from PIL import Image
        # Image.fromarray((img * 255).astype(np.uint8)).save(
        #     get_demo_file('hef_google_visible_grid.png'))
        ref = mpl.image.imread(get_demo_file('hef_google_visible_grid.png'))
        rmsd = np.sqrt(np.mean((ref - img)**2))
        self.assertTrue(rmsd < 5e-1)

        gm = GoogleVisibleMap(x=i, y=j, crs=d.grid, size_x=500, size_y=500)
        gm2 = GoogleVisibleMap(x=i,
                               y=j,
                               crs=d.grid,
                               scale=2,
                               size_x=500,
                               size_y=500)
        assert (gm.grid.nx * 2) == gm2.grid.nx
        assert gm.grid.extent == gm2.grid.extent

        # Test regression for non array inputs
        grid = mercator_grid(center_ll=(72.5, 30.), extent=(2.0e6, 2.0e6))
        GoogleVisibleMap(x=[0, grid.nx - 1], y=[0, grid.ny - 1], crs=grid)
Beispiel #7
0
    def test_longtime(self):
        """There was a bug with time"""

        fs = get_demo_file('test_longtime.nc')
        c = GeoNetcdf(fs)
        self.assertEqual(len(c.time), 2424)
        assert_array_equal(c.time[0:2], pd.to_datetime([datetime(1801, 10, 1),
                                                        datetime(1801, 11,
                                                                 1)]))
Beispiel #8
0
    def test_as_xarray(self):

        f = get_demo_file("era_interim_tibet.nc")
        d = GeoNetcdf(f)
        t2 = d.get_vardata("t2m", as_xarray=True)

        stat_lon = 91.1
        stat_lat = 31.1
        d.set_subset(corners=((stat_lon, stat_lat), (stat_lon, stat_lat)))
        t2_sub = d.get_vardata("t2m", as_xarray=True)
        np.testing.assert_allclose(t2_sub - t2, np.zeros(4).reshape((4, 1, 1)))

        d.set_period(t0="2012-06-01 06:00:00", t1="2012-06-01 12:00:00")
        t2_sub = d.get_vardata("t2m", as_xarray=True)
        np.testing.assert_allclose(t2_sub - t2, np.zeros(2).reshape((2, 1, 1)))

        wf = get_demo_file("wrf_cropped.nc")
        d = WRF(wf)
        tk = d.get_vardata("TK", as_xarray=True)
Beispiel #9
0
    def test_staggeredcoords(self):

        wf = get_demo_file('wrf_cropped.nc')
        nc = GeoNetcdf(wf)
        lon, lat = nc.grid.xstagg_ll_coordinates
        assert_allclose(np.squeeze(nc.variables['XLONG_U'][0, ...]), lon,
                        atol=1e-4)
        assert_allclose(np.squeeze(nc.variables['XLAT_U'][0, ...]), lat,
                        atol=1e-4)
        lon, lat = nc.grid.ystagg_ll_coordinates
        assert_allclose(np.squeeze(nc.variables['XLONG_V'][0, ...]), lon,
                        atol=1e-4)
        assert_allclose(np.squeeze(nc.variables['XLAT_V'][0, ...]), lat,
                        atol=1e-4)
Beispiel #10
0
    def test_wrf_polar(self):

        d = GeoNetcdf(get_demo_file('geo_em_d01_polarstereo.nc'))
        mylon, mylat = d.grid.ll_coordinates
        reflon = np.squeeze(d.get_vardata('XLONG_M'))
        reflat = np.squeeze(d.get_vardata('XLAT_M'))

        np.testing.assert_allclose(reflon, mylon, atol=5e-3)
        np.testing.assert_allclose(reflat, mylat, atol=5e-3)

        d = GeoNetcdf(get_demo_file('geo_em_d02_polarstereo.nc'))
        mylon, mylat = d.grid.ll_coordinates
        reflon = np.squeeze(d.get_vardata('XLONG_M'))
        reflat = np.squeeze(d.get_vardata('XLAT_M'))

        np.testing.assert_allclose(reflon, mylon, atol=1e-4)
        np.testing.assert_allclose(reflat, mylat, atol=1e-4)
Beispiel #11
0
    def test_visible(self):

        x = [91.176036, 92.05, 88.880927]
        y = [29.649702, 31.483333, 29.264956]

        g = GoogleVisibleMap(x=x,
                             y=y,
                             size_x=400,
                             size_y=400,
                             maptype='terrain')
        img = g.get_vardata()

        i, j = g.grid.transform(x, y, nearest=True)

        for _i, _j in zip(i, j):
            img[_j - 3:_j + 4, _i - 3:_i + 4, 0] = 1
            img[_j - 3:_j + 4, _i - 3:_i + 4, 1:] = 0

        # from scipy.misc import toimage
        # toimage(img).save(get_demo_file('hef_google_visible.png'))
        ref = mpl.image.imread(get_demo_file('hef_google_visible.png'))
        rmsd = np.sqrt(np.mean((ref - img)**2))
        self.assertTrue(rmsd < 1e-1)

        self.assertRaises(ValueError, GoogleVisibleMap, x=x, y=y, zoom=12)

        fw = get_demo_file('wrf_tip_d1.nc')
        d = GeoNetcdf(fw)
        i, j = d.grid.ij_coordinates
        g = GoogleVisibleMap(x=i, y=j, crs=d.grid, size_x=500, size_y=500)
        img = g.get_vardata()
        mask = g.grid.map_gridded_data(i * 0 + 1, d.grid)

        img[np.nonzero(mask)] = np.clip(img[np.nonzero(mask)] + 0.3, 0, 1)

        # from scipy.misc import toimage
        # toimage(img).save(get_demo_file('hef_google_visible_grid.png'))
        ref = mpl.image.imread(get_demo_file('hef_google_visible_grid.png'))
        rmsd = np.sqrt(np.mean((ref - img)**2))
        self.assertTrue(rmsd < 5e-1)
Beispiel #12
0
    def test_wrf(self):
        """Open WRF, do subsets and stuff"""

        fs = get_demo_file('chinabang.shp')

        for d in ['1', '2']:
            fw = get_demo_file('wrf_tip_d{}.nc'.format(d))
            d = GeoNetcdf(fw)
            self.assertTrue(isinstance(d, GeoDataset))
            mylon, mylat = d.grid.ll_coordinates
            reflon = d.get_vardata('XLONG')
            reflat = d.get_vardata('XLAT')
            np.testing.assert_allclose(reflon, mylon, rtol=0.000001)
            np.testing.assert_allclose(reflat, mylat, rtol=0.00001)

            d.set_roi(shape=fs)
            np.testing.assert_array_equal(d.get_vardata('roi'), d.roi)

        d1 = GeoNetcdf(get_demo_file('wrf_tip_d1.nc'))
        d2 = GeoNetcdf(get_demo_file('wrf_tip_d2.nc'))

        # Auto dimensions
        self.assertTrue(d1.t_dim == 'Time')
        self.assertTrue(d1.x_dim == 'west_east')
        self.assertTrue(d1.y_dim == 'south_north')
        self.assertTrue(d1.z_dim is None)

        #Time
        assert_array_equal(
            d1.time,
            pd.to_datetime([datetime(2005, 9, 21),
                            datetime(2005, 9, 21, 3)]))

        assert_array_equal(
            d2.time,
            pd.to_datetime([datetime(2005, 9, 21),
                            datetime(2005, 9, 21, 1)]))
        bef = d2.get_vardata('T2')
        d2.set_period(t0=datetime(2005, 9, 21, 1))
        assert_array_equal(bef[[1], ...], d2.get_vardata('T2'))
        d2.set_period()
        assert_array_equal(bef, d2.get_vardata('T2'))
        d2.set_period(t1=datetime(2005, 9, 21, 0))
        assert_array_equal(bef[[0], ...], d2.get_vardata('T2'))

        # ROIS
        d1.set_roi(grid=d2.grid)
        d1.set_subset(toroi=True)
        self.assertEqual(d1.grid.nx * 3, d2.grid.nx)
        self.assertEqual(d1.grid.ny * 3, d2.grid.ny)
        self.assertTrue(np.min(d1.roi) == 1)

        mylon, mylat = d1.grid.ll_coordinates
        reflon = d1.get_vardata('XLONG')
        reflat = d1.get_vardata('XLAT')
        np.testing.assert_allclose(reflon, mylon, atol=1e-4)
        np.testing.assert_allclose(reflat, mylat, atol=1e-4)

        reflon = d2.get_vardata('XLONG')[1::3, 1::3]
        reflat = d2.get_vardata('XLAT')[1::3, 1::3]
        np.testing.assert_allclose(reflon, mylon, atol=1e-4)
        np.testing.assert_allclose(reflat, mylat, atol=1e-4)

        # Mercator
        d = GeoNetcdf(get_demo_file('wrf_mercator.nc'))
        mylon, mylat = d.grid.ll_coordinates
        reflon = np.squeeze(d.get_vardata('XLONG'))
        reflat = np.squeeze(d.get_vardata('XLAT'))
        np.testing.assert_allclose(reflon, mylon, atol=1e-5)
        np.testing.assert_allclose(reflat, mylat, atol=1e-5)

        # Test xarray
        ds = xr.open_dataset(get_demo_file('wrf_mercator.nc'))
        mylon, mylat = ds.salem.grid.ll_coordinates
        np.testing.assert_allclose(reflon, mylon, atol=1e-5)
        np.testing.assert_allclose(reflat, mylat, atol=1e-5)
        d = GeoNetcdf(get_demo_file('wrf_tip_d1.nc'))
        reflon = np.squeeze(d.get_vardata('XLONG'))
        reflat = np.squeeze(d.get_vardata('XLAT'))
        ds = xr.open_dataset(get_demo_file('wrf_tip_d1.nc'))
        mylon, mylat = ds.salem.grid.ll_coordinates
        np.testing.assert_allclose(reflon, mylon, atol=1e-4)
        np.testing.assert_allclose(reflat, mylat, atol=1e-4)
Beispiel #13
0
    def test_as_xarray(self):

        f = get_demo_file('era_interim_tibet.nc')
        d = GeoNetcdf(f)
        t2 = d.get_vardata('t2m', as_xarray=True)

        stat_lon = 91.1
        stat_lat = 31.1
        d.set_subset(corners=((stat_lon, stat_lat), (stat_lon, stat_lat)))
        t2_sub = d.get_vardata('t2m', as_xarray=True)
        np.testing.assert_allclose(t2_sub - t2, np.zeros(4).reshape((4, 1, 1)))

        d.set_period(t0='2012-06-01 06:00:00', t1='2012-06-01 12:00:00')
        t2_sub = d.get_vardata('t2m', as_xarray=True)
        np.testing.assert_allclose(t2_sub - t2, np.zeros(2).reshape((2, 1, 1)))

        wf = get_demo_file('wrf_cropped.nc')
        d = WRF(wf)
        tk = d.get_vardata('TK', as_xarray=True)
Beispiel #14
0
    def test_wrf(self):
        """Open WRF, do subsets and stuff"""

        fs = get_demo_file("chinabang.shp")

        for d in ["1", "2"]:
            fw = get_demo_file("wrf_tip_d{}.nc".format(d))
            d = GeoNetcdf(fw)
            self.assertTrue(isinstance(d, GeoDataset))
            mylon, mylat = d.grid.ll_coordinates
            reflon = d.get_vardata("XLONG")
            reflat = d.get_vardata("XLAT")
            np.testing.assert_allclose(reflon, mylon, rtol=0.000001)
            np.testing.assert_allclose(reflat, mylat, rtol=0.00001)

            d.set_roi(shape=fs)
            np.testing.assert_array_equal(d.get_vardata("roi"), d.roi)

        d1 = GeoNetcdf(get_demo_file("wrf_tip_d1.nc"))
        d2 = GeoNetcdf(get_demo_file("wrf_tip_d2.nc"))

        # Auto dimensions
        self.assertTrue(d1.t_dim == "Time")
        self.assertTrue(d1.x_dim == "west_east")
        self.assertTrue(d1.y_dim == "south_north")
        self.assertTrue(d1.z_dim is None)

        # Time
        assert_array_equal(d1.time, pd.to_datetime([datetime(2005, 9, 21), datetime(2005, 9, 21, 3)]))

        assert_array_equal(d2.time, pd.to_datetime([datetime(2005, 9, 21), datetime(2005, 9, 21, 1)]))
        bef = d2.get_vardata("T2")
        d2.set_period(t0=datetime(2005, 9, 21, 1))
        assert_array_equal(bef[[1], ...], d2.get_vardata("T2"))
        d2.set_period()
        assert_array_equal(bef, d2.get_vardata("T2"))
        d2.set_period(t1=datetime(2005, 9, 21, 0))
        assert_array_equal(bef[[0], ...], d2.get_vardata("T2"))

        # ROIS
        d1.set_roi(grid=d2.grid)
        d1.set_subset(toroi=True)
        self.assertEqual(d1.grid.nx * 3, d2.grid.nx)
        self.assertEqual(d1.grid.ny * 3, d2.grid.ny)
        self.assertTrue(np.min(d1.roi) == 1)

        mylon, mylat = d1.grid.ll_coordinates
        reflon = d1.get_vardata("XLONG")
        reflat = d1.get_vardata("XLAT")
        np.testing.assert_allclose(reflon, mylon, atol=1e-4)
        np.testing.assert_allclose(reflat, mylat, atol=1e-4)

        reflon = d2.get_vardata("XLONG")[1::3, 1::3]
        reflat = d2.get_vardata("XLAT")[1::3, 1::3]
        np.testing.assert_allclose(reflon, mylon, atol=1e-4)
        np.testing.assert_allclose(reflat, mylat, atol=1e-4)

        # Mercator
        d = GeoNetcdf(get_demo_file("wrf_mercator.nc"))
        mylon, mylat = d.grid.ll_coordinates
        reflon = np.squeeze(d.get_vardata("XLONG"))
        reflat = np.squeeze(d.get_vardata("XLAT"))
        np.testing.assert_allclose(reflon, mylon, atol=1e-5)
        np.testing.assert_allclose(reflat, mylat, atol=1e-5)

        # Test xarray
        ds = xr.open_dataset(get_demo_file("wrf_mercator.nc"))
        mylon, mylat = ds.salem.grid.ll_coordinates
        np.testing.assert_allclose(reflon, mylon, atol=1e-5)
        np.testing.assert_allclose(reflat, mylat, atol=1e-5)
        d = GeoNetcdf(get_demo_file("wrf_tip_d1.nc"))
        reflon = np.squeeze(d.get_vardata("XLONG"))
        reflat = np.squeeze(d.get_vardata("XLAT"))
        ds = xr.open_dataset(get_demo_file("wrf_tip_d1.nc"))
        mylon, mylat = ds.salem.grid.ll_coordinates
        np.testing.assert_allclose(reflon, mylon, atol=1e-4)
        np.testing.assert_allclose(reflat, mylat, atol=1e-4)