コード例 #1
0
sza_vis_exp = xr.DataArray(np.array(
    [[45., 67.5, 90., np.nan], [22.5, 45., 67.5, np.nan],
     [0., 22.5, 45., np.nan], [np.nan, np.nan, np.nan, np.nan]],
    dtype=np.float32),
                           dims=('y', 'x'),
                           attrs=attrs_exp)
sza_ir_wv_exp = xr.DataArray(np.array([[45, 90], [0, 45]], dtype=np.float32),
                             dims=('y', 'x'),
                             attrs=attrs_exp)
area_vis_exp = AreaDefinition(
    area_id='geos_mviri_4x4',
    proj_id='geos_mviri_4x4',
    description='MVIRI Geostationary Projection',
    projection={
        'proj': 'geos',
        'lon_0': 57.0,
        'h': ALTITUDE,
        'a': EQUATOR_RADIUS,
        'b': POLE_RADIUS
    },
    width=4,
    height=4,
    area_extent=[5621229.74392, 5621229.74392, -5621229.74392, -5621229.74392])
area_ir_wv_exp = area_vis_exp.copy(area_id='geos_mviri_2x2',
                                   proj_id='geos_mviri_2x2',
                                   width=2,
                                   height=2)


@pytest.fixture(name='fake_dataset')
def fixture_fake_dataset():
    """Create fake dataset."""
コード例 #2
0
def _create_debug_array(sector_info, num_subtiles, font_path='Verdana.ttf'):
    from PIL import Image, ImageDraw, ImageFont
    from pkg_resources import resource_filename as get_resource_filename
    size = (1000, 1000)
    img = Image.new("L", size, 0)
    draw = ImageDraw.Draw(img)

    if ':' in font_path:
        # load from a python package
        font_path = get_resource_filename(*font_path.split(':'))
    font = ImageFont.truetype(font_path, 25)

    ll_extent = sector_info['lower_left_xy']
    ur_extent = sector_info['upper_right_xy']
    total_meters_x = ur_extent[0] - ll_extent[0]
    total_meters_y = ur_extent[1] - ll_extent[1]
    fcs_x = np.ceil(float(sector_info['resolution'][1]) / num_subtiles[1])
    fcs_y = np.ceil(float(sector_info['resolution'][0]) / num_subtiles[0])
    total_cells_x = np.ceil(total_meters_x / fcs_x)
    total_cells_y = np.ceil(total_meters_y / fcs_y)
    total_cells_x = np.ceil(total_cells_x / num_subtiles[1]) * num_subtiles[1]
    total_cells_y = np.ceil(total_cells_y / num_subtiles[0]) * num_subtiles[0]
    total_alpha_cells_x = int(total_cells_x / num_subtiles[1])
    total_alpha_cells_y = int(total_cells_y / num_subtiles[0])

    # "round" the total meters up to the number of alpha cells
    total_meters_x = total_cells_x * fcs_x
    total_meters_y = total_cells_y * fcs_y

    # Pixels per tile
    ppt_x = np.floor(float(size[0]) / total_cells_x)
    ppt_y = np.floor(float(size[1]) / total_cells_y)
    half_ppt_x = np.floor(ppt_x / 2.)
    half_ppt_y = np.floor(ppt_y / 2.)
    # Meters per pixel
    meters_ppx = fcs_x / ppt_x
    meters_ppy = fcs_y / ppt_y
    for idx, alpha in enumerate(string.ascii_uppercase):
        for i in range(4):
            st_x = i % num_subtiles[1]
            st_y = int(i / num_subtiles[1])
            t = "{}{:02d}".format(alpha, i + 1)
            t_size = font.getsize(t)
            cell_x = (idx * num_subtiles[1] + st_x) % total_cells_x
            cell_y = int(idx / (total_cells_x / num_subtiles[1])) * num_subtiles[0] + st_y
            if cell_x > total_cells_x:
                continue
            elif cell_y > total_cells_y:
                continue
            x = ppt_x * cell_x + half_ppt_x
            y = ppt_y * cell_y + half_ppt_y
            # draw box around the tile edge
            # PIL Documentation: "The second point is just outside the drawn rectangle."
            # we want to be just inside 0 and just inside the outer edge of the tile
            draw_rectangle(draw,
                           (x - half_ppt_x, y - half_ppt_y,
                            x + half_ppt_x, y + half_ppt_y), outline=255, fill=75, width=3)
            draw.text((x - t_size[0] / 2., y - t_size[1] / 2.), t, fill=255, font=font)

    img.save("test.png")

    from pyresample.utils import proj4_str_to_dict
    new_extents = (
        ll_extent[0],
        ur_extent[1] - 1001. * meters_ppy,
        ll_extent[0] + 1001. * meters_ppx,
        ur_extent[1],
    )
    grid_def = AreaDefinition(
        'debug_grid',
        'debug_grid',
        'debug_grid',
        proj4_str_to_dict(sector_info['projection']),
        1000,
        1000,
        new_extents
    )
    return grid_def, np.array(img)
コード例 #3
0
    def setUp(self):
        """Create temporary images to test on."""
        import tempfile
        from datetime import datetime

        from pyresample.geometry import AreaDefinition
        from satpy.scene import Scene

        self.date = datetime(2018, 1, 1)

        # Create area definition
        pcs_id = 'ETRS89 / LAEA Europe'
        proj4_dict = {'init': 'epsg:3035'}
        self.x_size = 100
        self.y_size = 100
        area_extent = (2426378.0132, 1528101.2618, 6293974.6215, 5446513.5222)
        self.area_def = AreaDefinition('geotiff_area', pcs_id, pcs_id,
                                       proj4_dict, self.x_size, self.y_size,
                                       area_extent)

        # Create datasets for L, LA, RGB and RGBA mode images
        r__ = da.random.randint(0,
                                256,
                                size=(self.y_size, self.x_size),
                                chunks=(50, 50)).astype(np.uint8)
        g__ = da.random.randint(0,
                                256,
                                size=(self.y_size, self.x_size),
                                chunks=(50, 50)).astype(np.uint8)
        b__ = da.random.randint(0,
                                256,
                                size=(self.y_size, self.x_size),
                                chunks=(50, 50)).astype(np.uint8)
        a__ = 255 * np.ones((self.y_size, self.x_size), dtype=np.uint8)
        a__[:10, :10] = 0
        a__ = da.from_array(a__, chunks=(50, 50))

        ds_l = xr.DataArray(da.stack([r__]),
                            dims=('bands', 'y', 'x'),
                            attrs={
                                'name': 'test_l',
                                'start_time': self.date
                            })
        ds_l['bands'] = ['L']
        ds_la = xr.DataArray(da.stack([r__, a__]),
                             dims=('bands', 'y', 'x'),
                             attrs={
                                 'name': 'test_la',
                                 'start_time': self.date
                             })
        ds_la['bands'] = ['L', 'A']
        ds_rgb = xr.DataArray(da.stack([r__, g__, b__]),
                              dims=('bands', 'y', 'x'),
                              attrs={
                                  'name': 'test_rgb',
                                  'start_time': self.date
                              })
        ds_rgb['bands'] = ['R', 'G', 'B']
        ds_rgba = xr.DataArray(da.stack([r__, g__, b__, a__]),
                               dims=('bands', 'y', 'x'),
                               attrs={
                                   'name': 'test_rgba',
                                   'start_time': self.date
                               })
        ds_rgba['bands'] = ['R', 'G', 'B', 'A']

        # Temp dir for the saved images
        self.base_dir = tempfile.mkdtemp()

        # Put the datasets to Scene for easy saving
        scn = Scene()
        scn['l'] = ds_l
        scn['l'].attrs['area'] = self.area_def
        scn['la'] = ds_la
        scn['la'].attrs['area'] = self.area_def
        scn['rgb'] = ds_rgb
        scn['rgb'].attrs['area'] = self.area_def
        scn['rgba'] = ds_rgba
        scn['rgba'].attrs['area'] = self.area_def

        # Save the images.  Two images in PNG and two in GeoTIFF
        scn.save_dataset('l',
                         os.path.join(self.base_dir, 'test_l.png'),
                         writer='simple_image')
        scn.save_dataset('la',
                         os.path.join(self.base_dir,
                                      '20180101_0000_test_la.png'),
                         writer='simple_image')
        scn.save_dataset('rgb',
                         os.path.join(self.base_dir,
                                      '20180101_0000_test_rgb.tif'),
                         writer='geotiff')
        scn.save_dataset('rgba',
                         os.path.join(self.base_dir, 'test_rgba.tif'),
                         writer='geotiff')

        self.scn = scn
コード例 #4
0
ファイル: test_bucket.py プロジェクト: tanjased/pyresample
class Test(unittest.TestCase):

    adef = AreaDefinition(
        'eurol', 'description', '', {
            'ellps': 'WGS84',
            'lat_0': '90.0',
            'lat_ts': '60.0',
            'lon_0': '0.0',
            'proj': 'stere'
        }, 2560, 2048, (-3780000.0, -7644000.0, 3900000.0, -1500000.0))
    chunks = 2
    lons = da.from_array(np.array([[25., 25.], [25., 25.]]), chunks=chunks)
    lats = da.from_array(np.array([[60., 60.00001], [60.2, 60.3]]),
                         chunks=chunks)

    def setUp(self):
        self.resampler = bucket.BucketResampler(self.adef, self.lons,
                                                self.lats)

    @patch('pyresample.bucket.Proj')
    @patch('pyresample.bucket.BucketResampler._get_indices')
    def test_init(self, get_indices, prj):
        resampler = bucket.BucketResampler(self.adef, self.lons, self.lats)
        get_indices.assert_called_once()
        prj.assert_called_once_with(self.adef.proj_dict)
        self.assertTrue(hasattr(resampler, 'target_area'))
        self.assertTrue(hasattr(resampler, 'source_lons'))
        self.assertTrue(hasattr(resampler, 'source_lats'))
        self.assertTrue(hasattr(resampler, 'x_idxs'))
        self.assertTrue(hasattr(resampler, 'y_idxs'))
        self.assertTrue(hasattr(resampler, 'idxs'))
        self.assertTrue(hasattr(resampler, 'get_sum'))
        self.assertTrue(hasattr(resampler, 'get_count'))
        self.assertTrue(hasattr(resampler, 'get_average'))
        self.assertTrue(hasattr(resampler, 'get_fractions'))
        self.assertIsNone(resampler.counts)

    def test_round_to_resolution(self):
        """Test rounding to given resolution"""
        # Scalar, integer resolution
        self.assertEqual(bucket.round_to_resolution(5.5, 2.), 6)
        # Scalar, non-integer resolution
        self.assertEqual(bucket.round_to_resolution(5.5, 1.7), 5.1)
        # List
        self.assertTrue(
            np.all(
                bucket.round_to_resolution([4.2, 5.6], 2) == np.array([4., 6.
                                                                       ])))
        # Numpy array
        self.assertTrue(
            np.all(
                bucket.round_to_resolution(np.array([4.2, 5.6]), 2) ==
                np.array([4., 6.])))
        # Dask array
        self.assertTrue(
            np.all(
                bucket.round_to_resolution(da.array([4.2, 5.6]), 2) ==
                np.array([4., 6.])))

    def test_get_proj_coordinates(self):
        """Test calculation of projection coordinates."""
        prj = MagicMock()
        prj.return_value = ([3.1, 3.1, 3.1], [4.8, 4.8, 4.8])
        lons = [1., 1., 1.]
        lats = [2., 2., 2.]
        self.resampler.prj = prj
        result = self.resampler._get_proj_coordinates(lons, lats)
        prj.assert_called_once_with(lons, lats)
        self.assertTrue(isinstance(result, np.ndarray))
        np.testing.assert_equal(result,
                                np.array([[3.1, 3.1, 3.1], [4.8, 4.8, 4.8]]))

    def test_get_bucket_indices(self):
        """Test calculation of array indices."""
        # Ensure nothing is calculated
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            self.resampler._get_indices()
        x_idxs, y_idxs = da.compute(self.resampler.x_idxs,
                                    self.resampler.y_idxs)
        np.testing.assert_equal(x_idxs, np.array([1710, 1710, 1707, 1705]))
        np.testing.assert_equal(y_idxs, np.array([465, 465, 459, 455]))

        # Additional small test case
        adef = create_area_def(area_id='test',
                               projection={'proj': 'latlong'},
                               width=2,
                               height=2,
                               center=(0, 0),
                               resolution=10)
        lons = da.from_array(np.array(
            [-10.0, -9.9, -0.1, 0, 0.1, 9.9, 10.0, -10.1, 0]),
                             chunks=2)
        lats = da.from_array(np.array(
            [-10.0, -9.9, -0.1, 0, 0.1, 9.9, 10.0, 0, 10.1]),
                             chunks=2)
        resampler = bucket.BucketResampler(source_lats=lats,
                                           source_lons=lons,
                                           target_area=adef)
        resampler._get_indices()
        np.testing.assert_equal(resampler.x_idxs,
                                np.array([-1, 0, 0, 1, 1, 1, -1, -1, -1]))
        np.testing.assert_equal(resampler.y_idxs,
                                np.array([-1, 1, 1, 1, 0, 0, -1, -1, -1]))

    def test_get_sum(self):
        """Test drop-in-a-bucket sum."""
        data = da.from_array(np.array([[2., 2.], [2., 2.]]),
                             chunks=self.chunks)
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_sum(data)

        result = result.compute()
        # One bin with two hits, so max value is 2.0
        self.assertTrue(np.max(result) == 4.)
        # Two bins with the same value
        self.assertEqual(np.sum(result == 2.), 2)
        # One bin with double the value
        self.assertEqual(np.sum(result == 4.), 1)
        self.assertEqual(result.shape, self.adef.shape)

        # Test that also Xarray.DataArrays work
        data = xr.DataArray(data)
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_sum(data)
        # One bin with two hits, so max value is 2.0
        self.assertTrue(np.max(result) == 4.)
        # Two bins with the same value
        self.assertEqual(np.sum(result == 2.), 2)
        # One bin with double the value
        self.assertEqual(np.sum(result == 4.), 1)
        self.assertEqual(result.shape, self.adef.shape)

        # Test masking all-NaN bins
        data = da.from_array(np.array([[np.nan, np.nan], [np.nan, np.nan]]),
                             chunks=self.chunks)
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_sum(data, mask_all_nan=True)
        self.assertTrue(np.all(np.isnan(result)))
        # By default all-NaN bins have a value of 0.0
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_sum(data)
        self.assertEqual(np.nanmax(result), 0.0)

    def test_get_count(self):
        """Test drop-in-a-bucket sum."""
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_count()
        result = result.compute()
        self.assertTrue(np.max(result) == 2)
        self.assertEqual(np.sum(result == 1), 2)
        self.assertEqual(np.sum(result == 2), 1)
        self.assertTrue(self.resampler.counts is not None)

    def test_get_average(self):
        """Test averaging bucket resampling."""
        data = da.from_array(np.array([[2., 4.], [3., np.nan]]),
                             chunks=self.chunks)
        # Without pre-calculated indices
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_average(data)
        result = result.compute()
        self.assertEqual(np.nanmax(result), 3.)
        self.assertTrue(np.any(np.isnan(result)))
        # Use a fill value other than np.nan
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_average(data, fill_value=-1)
        result = result.compute()
        self.assertEqual(np.max(result), 3.)
        self.assertEqual(np.min(result), -1)
        self.assertFalse(np.any(np.isnan(result)))

        # Test masking all-NaN bins
        data = da.from_array(np.array([[np.nan, np.nan], [np.nan, np.nan]]),
                             chunks=self.chunks)
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_average(data, mask_all_nan=True)
        self.assertTrue(np.all(np.isnan(result)))
        # By default all-NaN bins have a value of NaN
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_average(data)
        self.assertTrue(np.all(np.isnan(result)))

    def test_resample_bucket_fractions(self):
        """Test fraction calculations for categorical data."""
        data = da.from_array(np.array([[2, 4], [2, 2]]), chunks=self.chunks)
        categories = [1, 2, 3, 4]
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_fractions(data, categories=categories)
        self.assertEqual(set(categories), set(result.keys()))
        res = result[1].compute()
        self.assertTrue(np.nanmax(res) == 0.)
        res = result[2].compute()
        self.assertTrue(np.nanmax(res) == 1.)
        self.assertTrue(np.nanmin(res) == 0.5)
        res = result[3].compute()
        self.assertTrue(np.nanmax(res) == 0.)
        res = result[4].compute()
        self.assertTrue(np.nanmax(res) == 0.5)
        self.assertTrue(np.nanmin(res) == 0.)
        # There should be NaN values
        self.assertTrue(np.any(np.isnan(res)))

        # Use a fill value
        with dask.config.set(scheduler=CustomScheduler(max_computes=0)):
            result = self.resampler.get_fractions(data,
                                                  categories=categories,
                                                  fill_value=-1)

        # There should not be any NaN values
        for i in categories:
            res = result[i].compute()
            self.assertFalse(np.any(np.isnan(res)))
            self.assertTrue(np.min(res) == -1)

        # No categories given, need to compute the data once to get
        # the categories
        with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
            result = self.resampler.get_fractions(data, categories=None)
コード例 #5
0
def load(satscene, **kargs):
    """Reader for EUMETSATs Hydrology SAF (HSAF) h03 product
    h03 product is precipitation rate at the ground 
    by GEO(MSG)/Infrared supported by LEO/Microwave
    http://hsaf.meteoam.it/precipitation.php?tab=3
    """

    # Read config file content
    conf = ConfigParser()
    conf.read(os.path.join(CONFIG_PATH, satscene.fullname + ".cfg"))
    values = {
        "orbit": satscene.orbit,
        "satname": satscene.satname,
        "number": satscene.number,
        "instrument": satscene.instrument_name,
        "satellite": satscene.fullname
    }

    # end of scan time 12min after start
    end_time = satscene.time_slot + datetime.timedelta(minutes=12)

    filepath = end_time.strftime(conf.get("seviri-level2", "dir", raw=True))
    filepattern = end_time.strftime(
        conf.get("seviri-level2", "filename", raw=True)) % values
    filename = os.path.join(filepath, filepattern)

    print "... search for file: ", filename
    filenames = glob(str(filename))
    if len(filenames) == 0:
        print "*** Error, no file found"
        quit()
    elif len(filenames) > 1:
        print "*** Warning, more than 1 datafile found: "
        for filename in filenames:
            print "    ", filename

    # possible formats: h03_20150513_1557_rom.grb.gz, h03_20150513_1612_rom.grb, h03_20150513_1612_rom.nc
    fileformats = [filename.split(".")[-1] for filename in filenames]
    # try to find grb file

    if 'grb' in fileformats:
        # read grib
        data, fill_value, units, long_name = read_h03_grib(
            filenames[fileformats.index('grb')])
    elif 'nc' in fileformats:
        # read netCDF
        data, fill_value, units, long_name = read_h03_netCDF(
            filenames[fileformats.index('nc')])
    elif 'gz' in fileformats:
        # unzip
        from subprocess import call
        infile = filenames[fileformats.index('gz')]
        outfile = infile[:-3]
        print "    unizp ", infile
        # gunzip -c h03_20150513_1557_rom.grb.gz > h03_20150513_1557_rom.grb
        # call("/bin/gunzip "+ infile                +" 2>&1", shell=True) # dont keep gz file
        call("/bin/gunzip -c " + infile + " > " + outfile + " 2>&1",
             shell=True)  # keep gz file
        # check format of gunziped file
        if outfile.split(".")[-1] == 'grb':
            data, fill_value, units, long_name = read_h03_grib(outfile)
        elif outfile.split(".")[-1] == 'nc':
            data, fill_value, units, long_name = read_h03_netCDF(outfile)

    if units == "kg m**-2 s**-1" or units == "kg m-2s-1":
        data *= 3600
        units = "kg m-2 h-1"

    satscene['h03'] = data
    satscene['h03'].fill_value = fill_value
    satscene['h03'].units = units
    satscene['h03'].long_name = long_name
    satscene['h03'].product_name = 'h03'

    # personal communication with help desk
    # Each H03 grib file contains precipitation data of a 900x1900 pixel sub-area of the SEVIRI full disk area (3712x3712 pixels).
    # The first pixel  of H03 (pixel (1,1)) grib file corresponds to Seviri pixel (1095,85) if the Seviri pixel (1,1) is in the Nort-East.
    # I can confirm that only the prime satellite is used (position subsatellite longitude 0 degree East).
    # For the future we are thinking to disseminate the h03 outputs already corrected in parallax.

    # conversion of above information to correct AreaDefinition
    # full_disk = get_area_def("SeviriDiskFull")
    # from mpop.projector import get_area_def
    # import numpy as np
    # np.array(area_def.get_proj_coords(data_slice=(85+900,1095     ))) - 3000.40316582 / 2.
    #    array([-2284807.01076965,  2611850.9558437 ])
    # np.array(area_def.get_proj_coords(data_slice=(85    ,1095+1900))) + 3000.40316582 / 2.
    #                                        array([ 3418959.40744847,  5315214.20824482])
    # or
    # aex = full_disk.get_area_extent_for_subsets(985,1095,85,2995)

    proj = {
        'proj': 'geos',
        'a': '6378169.0',
        'b': '6356583.8',
        'h': '35785831.0',
        'lon_0': '0.0'
    }
    aex = (-2284807.01076965, 2611850.9558437, 3418959.40744847,
           5315214.20824482)

    from pyresample.geometry import AreaDefinition
    satscene.area = AreaDefinition("hsaf", "hsaf", "geos0", proj, 1900, 900,
                                   aex)
コード例 #6
0
ファイル: test_writers.py プロジェクト: ringsaturn/satpy
    def setUp(self):
        """Create test data and mock pycoast/pydecorate."""
        from trollimage.xrimage import XRImage
        from pyresample.geometry import AreaDefinition
        import xarray as xr
        import dask.array as da

        proj_dict = {
            'proj': 'lcc',
            'datum': 'WGS84',
            'ellps': 'WGS84',
            'lon_0': -95.,
            'lat_0': 25,
            'lat_1': 25,
            'units': 'm',
            'no_defs': True
        }
        self.area_def = AreaDefinition(
            'test',
            'test',
            'test',
            proj_dict,
            200,
            400,
            (-1000., -1500., 1000., 1500.),
        )
        self.orig_rgb_img = XRImage(
            xr.DataArray(da.arange(75., chunks=10).reshape(3, 5, 5) / 75.,
                         dims=('bands', 'y', 'x'),
                         coords={'bands': ['R', 'G', 'B']},
                         attrs={
                             'name': 'test_ds',
                             'area': self.area_def
                         }))
        self.orig_l_img = XRImage(
            xr.DataArray(da.arange(25., chunks=10).reshape(5, 5) / 75.,
                         dims=('y', 'x'),
                         attrs={
                             'name': 'test_ds',
                             'area': self.area_def
                         }))

        self.decorate = {
            'decorate': [{
                'logo': {
                    'logo_path': '',
                    'height': 143,
                    'bg': 'white',
                    'bg_opacity': 255
                }
            }, {
                'text': {
                    'txt': 'TEST',
                    'align': {
                        'top_bottom': 'bottom',
                        'left_right': 'right'
                    },
                    'font': '',
                    'font_size': 22,
                    'height': 30,
                    'bg': 'black',
                    'bg_opacity': 255,
                    'line': 'white'
                }
            }, {
                'scale': {
                    'colormap': greys,
                    'extend': False,
                    'width': 1670,
                    'height': 110,
                    'tick_marks': 5,
                    'minor_tick_marks': 1,
                    'cursor': [0, 0],
                    'bg': 'white',
                    'title': 'TEST TITLE OF SCALE',
                    'fontsize': 110,
                    'align': 'cc'
                }
            }]
        }

        import_mock = mock.MagicMock()
        modules = {
            'pycoast': import_mock.pycoast,
            'pydecorate': import_mock.pydecorate
        }
        self.module_patcher = mock.patch.dict('sys.modules', modules)
        self.module_patcher.start()
コード例 #7
0
ファイル: test_resample.py プロジェクト: JohannesSMHI/satpy
    def test_area_def_coordinates(self):
        """Test coordinates being added with an AreaDefinition."""
        import dask.array as da
        import numpy as np
        import xarray as xr
        from pyresample.geometry import AreaDefinition

        from satpy.resample import add_crs_xy_coords
        area_def = AreaDefinition('test', 'test', 'test', {
            'proj': 'lcc',
            'lat_1': 25,
            'lat_0': 25
        }, 100, 200, [-100, -100, 100, 100])
        data_arr = xr.DataArray(
            da.zeros((200, 100), chunks=50),
            attrs={'area': area_def},
            dims=('y', 'x'),
        )
        new_data_arr = add_crs_xy_coords(data_arr, area_def)
        self.assertIn('y', new_data_arr.coords)
        self.assertIn('x', new_data_arr.coords)

        self.assertIn('units', new_data_arr.coords['y'].attrs)
        self.assertEqual(new_data_arr.coords['y'].attrs['units'], 'meter')
        self.assertIn('units', new_data_arr.coords['x'].attrs)
        self.assertEqual(new_data_arr.coords['x'].attrs['units'], 'meter')
        self.assertIn('crs', new_data_arr.coords)
        self.assertIsInstance(new_data_arr.coords['crs'].item(), CRS)
        self.assertEqual(area_def.crs, new_data_arr.coords['crs'].item())

        # already has coords
        data_arr = xr.DataArray(da.zeros((200, 100), chunks=50),
                                attrs={'area': area_def},
                                dims=('y', 'x'),
                                coords={
                                    'y': np.arange(2, 202),
                                    'x': np.arange(100)
                                })
        new_data_arr = add_crs_xy_coords(data_arr, area_def)
        self.assertIn('y', new_data_arr.coords)
        self.assertNotIn('units', new_data_arr.coords['y'].attrs)
        self.assertIn('x', new_data_arr.coords)
        self.assertNotIn('units', new_data_arr.coords['x'].attrs)
        np.testing.assert_equal(new_data_arr.coords['y'], np.arange(2, 202))

        self.assertIn('crs', new_data_arr.coords)
        self.assertIsInstance(new_data_arr.coords['crs'].item(), CRS)
        self.assertEqual(area_def.crs, new_data_arr.coords['crs'].item())

        # lat/lon area
        area_def = AreaDefinition('test', 'test', 'test', {'proj': 'latlong'},
                                  100, 200, [-100, -100, 100, 100])
        data_arr = xr.DataArray(
            da.zeros((200, 100), chunks=50),
            attrs={'area': area_def},
            dims=('y', 'x'),
        )
        new_data_arr = add_crs_xy_coords(data_arr, area_def)
        self.assertIn('y', new_data_arr.coords)
        self.assertIn('x', new_data_arr.coords)

        self.assertIn('units', new_data_arr.coords['y'].attrs)
        self.assertEqual(new_data_arr.coords['y'].attrs['units'],
                         'degrees_north')
        self.assertIn('units', new_data_arr.coords['x'].attrs)
        self.assertEqual(new_data_arr.coords['x'].attrs['units'],
                         'degrees_east')
        self.assertIn('crs', new_data_arr.coords)
        self.assertIsInstance(new_data_arr.coords['crs'].item(), CRS)
        self.assertEqual(area_def.crs, new_data_arr.coords['crs'].item())
コード例 #8
0
ファイル: test_resample.py プロジェクト: JohannesSMHI/satpy
def get_test_data(input_shape=(100, 50),
                  output_shape=(200, 100),
                  output_proj=None,
                  input_dims=('y', 'x')):
    """Get common data objects used in testing.

    Returns:
        tuple:

        * input_data_on_area: DataArray with dimensions as if it is a gridded
          dataset.
        * input_area_def: AreaDefinition of the above DataArray
        * input_data_on_swath: DataArray with dimensions as if it is a swath.
        * input_swath: SwathDefinition of the above DataArray
        * target_area_def: AreaDefinition to be used as a target for resampling

    """
    import dask.array as da
    from pyresample.geometry import AreaDefinition, SwathDefinition
    from pyresample.utils import proj4_str_to_dict
    from xarray import DataArray
    ds1 = DataArray(da.zeros(input_shape, chunks=85),
                    dims=input_dims,
                    attrs={
                        'name': 'test_data_name',
                        'test': 'test'
                    })
    if input_dims and 'y' in input_dims:
        ds1 = ds1.assign_coords(y=da.arange(input_shape[-2], chunks=85))
    if input_dims and 'x' in input_dims:
        ds1 = ds1.assign_coords(x=da.arange(input_shape[-1], chunks=85))
    if input_dims and 'bands' in input_dims:
        ds1 = ds1.assign_coords(bands=list('RGBA'[:ds1.sizes['bands']]))

    input_proj_str = ('+proj=geos +lon_0=-95.0 +h=35786023.0 +a=6378137.0 '
                      '+b=6356752.31414 +sweep=x +units=m +no_defs')
    source = AreaDefinition(
        'test_target',
        'test_target',
        'test_target',
        proj4_str_to_dict(input_proj_str),
        input_shape[1],  # width
        input_shape[0],  # height
        (-1000., -1500., 1000., 1500.))
    ds1.attrs['area'] = source
    crs = CRS.from_string(input_proj_str)
    ds1 = ds1.assign_coords(crs=crs)

    ds2 = ds1.copy()
    input_area_shape = tuple(ds1.sizes[dim] for dim in ds1.dims
                             if dim in ['y', 'x'])
    geo_dims = ('y', 'x') if input_dims else None
    lons = da.random.random(input_area_shape, chunks=50)
    lats = da.random.random(input_area_shape, chunks=50)
    swath_def = SwathDefinition(DataArray(lons, dims=geo_dims),
                                DataArray(lats, dims=geo_dims))
    ds2.attrs['area'] = swath_def
    crs = CRS.from_string('+proj=latlong +datum=WGS84 +ellps=WGS84')
    ds2 = ds2.assign_coords(crs=crs)

    # set up target definition
    output_proj_str = ('+proj=lcc +datum=WGS84 +ellps=WGS84 '
                       '+lon_0=-95. +lat_0=25 +lat_1=25 +units=m +no_defs')
    output_proj_str = output_proj or output_proj_str
    target = AreaDefinition(
        'test_target',
        'test_target',
        'test_target',
        proj4_str_to_dict(output_proj_str),
        output_shape[1],  # width
        output_shape[0],  # height
        (-1000., -1500., 1000., 1500.),
    )
    return ds1, source, ds2, swath_def, target