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_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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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)