Exemple #1
0
def reproj(nc_folder, band):
    os.chdir(nc_folder)
    full_direc = os.listdir(os.getcwd())
    nc_files = [ii for ii in full_direc if ii.endswith('.nc')]
    g16_data_file = nc_files[band]  # select .nc file
    g16nc = Dataset(g16_data_file, 'r')

    # GOES-R projection info and retrieving relevant constants
    area_id = 'GOES_R'
    description = 'FullDisk GOES-R Projection'
    proj_id = 'GOES_R'
    proj_info = g16nc.variables['goes_imager_projection']
    lon_origin = proj_info.longitude_of_projection_origin
    lat_origin = proj_info.latitude_of_projection_origin
    r_eq = proj_info.semi_major_axis
    r_pol = proj_info.semi_minor_axis
    sweep_angle = proj_info.sweep_angle_axis
    nsh = g16nc.variables['nominal_satellite_height'][:]
    projection = '+proj=geos +h={} +lat_0={} +lon_0={} +a={} +b={} +sweep={} +ellps=GRS80 +unit=m'.format(nsh*1000,lat_origin,lon_origin, r_eq, r_pol, sweep_angle)
    x = g16nc.variables['x'][:]
    y = g16nc.variables['y'][:]
    print(len(x), len(x))
    width = len(x)
    height = len(x)
    h = nsh*1000
    x_l = h * x[0]
    x_r = h * x[-1]
    y_l = h * y[-1]
    y_u = h * y[0]
    x_half = (x_r - x_l) / (len(x) - 1) / 2
    y_half = (y_l - y_u) / (len(x) - 1) / 2
    area_extent = (x_l-x_half, y_l-y_half, x_r+x_half, y_u+y_half)
    goesproj = AreaDefinition(area_id=area_id, description=description, proj_id=proj_id, projection=projection, width=width, height=height, area_extent=area_extent)

    area_id = 'IOA_D1'
    description = 'Dominio 1 Grupo IOA'
    proj_id = 'IOA_D1'
    projection = '+init=EPSG:4326'
    width = 2909
    height = 2058
    area_extent = (-123.3613, 4.1260, -74.8779, 38.4260)
    ioa1 = AreaDefinition(area_id=area_id, description=description, proj_id=proj_id, projection=projection, width=width, height=height, area_extent=area_extent)

    ioa_lon, ioa_lat = ioa1.get_lonlats()
    ioa_lon = ioa_lon[0, :]
    ioa_lat = ioa_lat[:, 0]

    return ioa_lat, ioa_lon, ioa1, goesproj
Exemple #2
0
class TestRBGradientSearchResamplerArea2Swath:
    """Test RBGradientSearchResampler for the Swath to Area case."""
    def setup(self):
        """Set up the test case."""
        chunks = 20

        self.src_area = AreaDefinition(
            'euro40', 'euro40', None, {
                'proj': 'stere',
                'lon_0': 14.0,
                'lat_0': 90.0,
                'lat_ts': 60.0,
                'ellps': 'bessel'
            }, 102, 102, (-2717181.7304994687, -5571048.14031214,
                          1378818.2695005313, -1475048.1403121399))

        self.dst_area = AreaDefinition(
            'omerc_otf', 'On-the-fly omerc area', None, {
                'alpha': '8.99811271718795',
                'ellps': 'sphere',
                'gamma': '0',
                'k': '1',
                'lat_0': '0',
                'lonc': '13.8096029486222',
                'proj': 'omerc',
                'units': 'm'
            }, 50, 100,
            (-1461111.3603, 3440088.0459, 1534864.0322, 9598335.0457))

        self.lons, self.lats = self.dst_area.get_lonlats(chunks=chunks)
        xrlons = xr.DataArray(self.lons.persist())
        xrlats = xr.DataArray(self.lats.persist())
        self.dst_swath = SwathDefinition(xrlons, xrlats)

    def test_resampling_to_swath_is_not_implemented(self):
        """Test that resampling to swath is not working yet."""
        from pyresample.gradient import ResampleBlocksGradientSearchResampler

        with pytest.raises(NotImplementedError):
            ResampleBlocksGradientSearchResampler(self.src_area,
                                                  self.dst_swath)
Exemple #3
0
class TestGradientResampler(unittest.TestCase):
    """Test case for the gradient resampling."""
    def setUp(self):
        """Set up the test case."""
        from pyresample.gradient import GradientSearchResampler
        self.src_area = AreaDefinition('dst', 'dst area', None, {
            'ellps': 'WGS84',
            'h': '35785831',
            'proj': 'geos'
        }, 100, 100, (5550000.0, 5550000.0, -5550000.0, -5550000.0))
        self.src_swath = SwathDefinition(*self.src_area.get_lonlats())
        self.dst_area = AreaDefinition(
            'euro40', 'euro40', None, {
                'proj': 'stere',
                'lon_0': 14.0,
                'lat_0': 90.0,
                'lat_ts': 60.0,
                'ellps': 'bessel'
            }, 102, 102, (-2717181.7304994687, -5571048.14031214,
                          1378818.2695005313, -1475048.1403121399))

        self.resampler = GradientSearchResampler(self.src_area, self.dst_area)
        self.swath_resampler = GradientSearchResampler(self.src_swath,
                                                       self.dst_area)

    def test_get_projection_coordinates_area_to_area(self):
        """Check that the coordinates are initialized, for area -> area."""
        assert self.resampler.prj is None
        self.resampler._get_projection_coordinates((10, 10))
        cdst_x = self.resampler.dst_x.compute()
        cdst_y = self.resampler.dst_y.compute()
        assert np.allclose(np.min(cdst_x), -2022632.1675016289)
        assert np.allclose(np.max(cdst_x), 2196052.591296284)
        assert np.allclose(np.min(cdst_y), 3517933.413092212)
        assert np.allclose(np.max(cdst_y), 5387038.893400168)
        assert self.resampler.use_input_coords
        assert self.resampler.prj is not None

    def test_get_projection_coordinates_swath_to_area(self):
        """Check that the coordinates are initialized, for swath -> area."""
        assert self.swath_resampler.prj is None
        self.swath_resampler._get_projection_coordinates((10, 10))
        cdst_x = self.swath_resampler.dst_x.compute()
        cdst_y = self.swath_resampler.dst_y.compute()
        assert np.allclose(np.min(cdst_x), -2697103.29912692)
        assert np.allclose(np.max(cdst_x), 1358739.8381279823)
        assert np.allclose(np.min(cdst_y), -5550969.708939591)
        assert np.allclose(np.max(cdst_y), -1495126.5716846888)
        assert self.swath_resampler.use_input_coords is False
        assert self.swath_resampler.prj is not None

    def test_get_gradients(self):
        """Test that coordinate gradients are computed correctly."""
        self.resampler._get_projection_coordinates((10, 10))
        assert self.resampler.src_gradient_xl is None
        self.resampler._get_gradients()
        assert self.resampler.src_gradient_xl.compute().max() == 0.0
        assert self.resampler.src_gradient_xp.compute().max() == -111000.0
        assert self.resampler.src_gradient_yl.compute().max() == 111000.0
        assert self.resampler.src_gradient_yp.compute().max() == 0.0

    def test_get_chunk_mappings(self):
        """Test that chunk overlap, and source and target slices are correct."""
        chunks = (10, 10)
        num_chunks = np.product(chunks)
        self.resampler._get_projection_coordinates(chunks)
        self.resampler._get_gradients()
        assert self.resampler.coverage_status is None
        self.resampler.get_chunk_mappings()
        # 8 source chunks overlap the target area
        covered_src_chunks = np.array([38, 39, 48, 49, 58, 59, 68, 69])
        res = np.where(self.resampler.coverage_status)[0]
        assert np.all(res == covered_src_chunks)
        # All *num_chunks* should have values in the lists
        assert len(self.resampler.coverage_status) == num_chunks
        assert len(self.resampler.src_slices) == num_chunks
        assert len(self.resampler.dst_slices) == num_chunks
        assert len(self.resampler.dst_mosaic_locations) == num_chunks
        # There's only one output chunk, and the covered source chunks
        # should have destination locations of (0, 0)
        res = np.array(self.resampler.dst_mosaic_locations)[covered_src_chunks]
        assert all([all(loc == (0, 0)) for loc in list(res)])

    def test_get_src_poly_area(self):
        """Test defining source chunk polygon for AreaDefinition."""
        chunks = (10, 10)
        self.resampler._get_projection_coordinates(chunks)
        self.resampler._get_gradients()
        poly = self.resampler._get_src_poly(0, 40, 0, 40)
        assert np.allclose(poly.area, 12365358458842.43)

    def test_get_src_poly_swath(self):
        """Test defining source chunk polygon for SwathDefinition."""
        chunks = (10, 10)
        self.swath_resampler._get_projection_coordinates(chunks)
        self.swath_resampler._get_gradients()
        # Swath area defs can't be sliced, so False is returned
        poly = self.swath_resampler._get_src_poly(0, 40, 0, 40)
        assert poly is False

    @mock.patch('pyresample.gradient.get_polygon')
    def test_get_dst_poly(self, get_polygon):
        """Test defining destination chunk polygon."""
        chunks = (10, 10)
        self.resampler._get_projection_coordinates(chunks)
        self.resampler._get_gradients()
        # First call should make a call to get_polygon()
        self.resampler._get_dst_poly('idx1', 0, 10, 0, 10)
        assert get_polygon.call_count == 1
        assert 'idx1' in self.resampler.dst_polys
        # The second call to the same index should come from cache
        self.resampler._get_dst_poly('idx1', 0, 10, 0, 10)
        assert get_polygon.call_count == 1

        # Swath defs raise AttributeError, and False is returned
        get_polygon.side_effect = AttributeError
        self.resampler._get_dst_poly('idx2', 0, 10, 0, 10)
        assert self.resampler.dst_polys['idx2'] is False

    def test_filter_data(self):
        """Test filtering chunks that do not overlap."""
        chunks = (10, 10)
        self.resampler._get_projection_coordinates(chunks)
        self.resampler._get_gradients()
        self.resampler.get_chunk_mappings()

        # Basic filtering.  There should be 8 dask arrays that each
        # have a shape of (10, 10)
        res = self.resampler._filter_data(self.resampler.src_x)
        valid = [itm for itm in res if itm is not None]
        assert len(valid) == 8
        shapes = [arr.shape for arr in valid]
        for shp in shapes:
            assert shp == (10, 10)

        # Destination x/y coordinate array filtering.  Again, 8 dask
        # arrays each with shape (102, 102)
        res = self.resampler._filter_data(self.resampler.dst_x, is_src=False)
        valid = [itm for itm in res if itm is not None]
        assert len(valid) == 8
        shapes = [arr.shape for arr in valid]
        for shp in shapes:
            assert shp == (102, 102)

        # Add a dimension to the given dataset
        data = da.random.random(self.src_area.shape)
        res = self.resampler._filter_data(data, add_dim=True)
        valid = [itm for itm in res if itm is not None]
        assert len(valid) == 8
        shapes = [arr.shape for arr in valid]
        for shp in shapes:
            assert shp == (1, 10, 10)

        # 1D and 3+D should raise NotImplementedError
        data = da.random.random((3, ))
        try:
            res = self.resampler._filter_data(data, add_dim=True)
            raise IndexError
        except NotImplementedError:
            pass
        data = da.random.random((3, 3, 3, 3))
        try:
            res = self.resampler._filter_data(data, add_dim=True)
            raise IndexError
        except NotImplementedError:
            pass

    def test_resample_area_to_area_2d(self):
        """Resample area to area, 2d."""
        data = xr.DataArray(da.ones(self.src_area.shape, dtype=np.float64),
                            dims=['y', 'x'])
        res = self.resampler.compute(
            data, method='bil').compute(scheduler='single-threaded')
        assert res.shape == self.dst_area.shape
        assert np.allclose(res, 1)

    def test_resample_area_to_area_2d_fill_value(self):
        """Resample area to area, 2d, use fill value."""
        data = xr.DataArray(da.full(self.src_area.shape,
                                    np.nan,
                                    dtype=np.float64),
                            dims=['y', 'x'])
        res = self.resampler.compute(
            data, method='bil',
            fill_value=2.0).compute(scheduler='single-threaded')
        assert res.shape == self.dst_area.shape
        assert np.allclose(res, 2.0)

    def test_resample_area_to_area_3d(self):
        """Resample area to area, 3d."""
        data = xr.DataArray(da.ones(
            (3, ) + self.src_area.shape, dtype=np.float64) *
                            np.array([1, 2, 3])[:, np.newaxis, np.newaxis],
                            dims=['bands', 'y', 'x'])
        res = self.resampler.compute(
            data, method='bil').compute(scheduler='single-threaded')
        assert res.shape == (3, ) + self.dst_area.shape
        assert np.allclose(res[0, :, :], 1.0)
        assert np.allclose(res[1, :, :], 2.0)
        assert np.allclose(res[2, :, :], 3.0)

    def test_resample_swath_to_area_2d(self):
        """Resample swath to area, 2d."""
        data = xr.DataArray(da.ones(self.src_swath.shape, dtype=np.float64),
                            dims=['y', 'x'])
        res = self.swath_resampler.compute(
            data, method='bil').compute(scheduler='single-threaded')
        assert res.shape == self.dst_area.shape
        assert not np.all(np.isnan(res))

    def test_resample_swath_to_area_3d(self):
        """Resample area to area, 3d."""
        data = xr.DataArray(da.ones(
            (3, ) + self.src_swath.shape, dtype=np.float64) *
                            np.array([1, 2, 3])[:, np.newaxis, np.newaxis],
                            dims=['bands', 'y', 'x'])
        res = self.swath_resampler.compute(
            data, method='bil').compute(scheduler='single-threaded')
        assert res.shape == (3, ) + self.dst_area.shape
        for i in range(res.shape[0]):
            arr = np.ravel(res[i, :, :])
            assert np.allclose(arr[np.isfinite(arr)], float(i + 1))
class TestGradientResampler(unittest.TestCase):
    """Test case for the gradient resampling."""

    def setUp(self):
        """Set up the test case."""
        from pyresample.gradient import GradientSearchResampler
        self.src_area = AreaDefinition('dst', 'dst area', None,
                                       {'ellps': 'WGS84', 'h': '35785831', 'proj': 'geos'},
                                       100, 100,
                                       (5550000.0, 5550000.0, -5550000.0, -5550000.0))
        self.src_swath = SwathDefinition(*self.src_area.get_lonlats())
        self.dst_area = AreaDefinition('nrMET3km', 'nrMET3km', None,
                                       {'proj': 'eqc', 'lon_0': 0.0, 'a': R},
                                       360, 180,
                                       (-np.pi * R, -np.pi / 2 * R, np.pi * R, np.pi / 2 * R))

        self.resampler = GradientSearchResampler(self.src_area, self.dst_area)
        self.swath_resampler = GradientSearchResampler(self.src_swath, self.dst_area)

    @mock.patch('pyresample.gradient.parallel_gradient_search')
    def test_coords_initialization(self, pgsoic):
        """Check that the coordinates get initialized correctly."""
        data = xr.DataArray(da.ones((100, 100), dtype=np.float64), dims=['y', 'x'])
        pgsoic.return_value = da.ones(self.dst_area.shape)
        self.resampler.compute(data, meth='bil')
        cdst_x = self.resampler.dst_x.compute()
        cdst_y = self.resampler.dst_y.compute()
        assert(np.isinf(cdst_x[0, 0]))
        assert(np.isinf(cdst_y[0, 0]))
        assert(cdst_y[90, 180] == -55285.59156767167)
        assert(cdst_x[90, 180] == 55656.13605425304)
        assert(self.resampler.use_input_coords)
        pgsoic.assert_called_once_with(data.data[:, :],
                                       self.resampler.src_x,
                                       self.resampler.src_y,
                                       self.resampler.dst_x,
                                       self.resampler.dst_y,
                                       meth='bil')

    def test_resample_area_to_area_2d(self):
        """Resample area to area, 2d."""
        data = xr.DataArray(da.ones((100, 100), dtype=np.float64), dims=['y', 'x'])
        res = self.resampler.compute(data, meth='bil').compute(scheduler='single-threaded')
        assert(res.shape == self.dst_area.shape)
        assert(not np.all(np.isnan(res)))

    def test_resample_area_to_area_3d(self):
        """Resample area to area, 3d."""
        data = xr.DataArray(da.ones((3, 100, 100), dtype=np.float64) *
                            np.array([1, 2, 3])[:, np.newaxis, np.newaxis], dims=['bands', 'y', 'x'])
        res = self.resampler.compute(data, meth='bil').compute(scheduler='single-threaded')
        assert(res.shape == (3, ) + self.dst_area.shape)
        assert(not np.all(np.isnan(res)))

    def test_resample_swath_to_area_2d(self):
        """Resample swath to area, 2d."""
        data = xr.DataArray(da.ones((100, 100), dtype=np.float64), dims=['y', 'x'])
        res = self.swath_resampler.compute(data, meth='bil').compute(scheduler='single-threaded')
        assert(res.shape == self.dst_area.shape)
        assert(not np.all(np.isnan(res)))

    def test_resample_swath_to_area_3d(self):
        """Resample area to area, 3d."""
        data = xr.DataArray(da.ones((3, 100, 100), dtype=np.float64) *
                            np.array([1, 2, 3])[:, np.newaxis, np.newaxis], dims=['bands', 'y', 'x'])
        res = self.swath_resampler.compute(data, meth='bil').compute(scheduler='single-threaded')
        assert(res.shape == (3, ) + self.dst_area.shape)
        assert(not np.all(np.isnan(res)))