Beispiel #1
0
 def test_proj4_str_dict_conversion(self):
     from pyresample import utils
     proj_str = "+proj=lcc +ellps=WGS84 +lon_0=-95 +no_defs"
     proj_dict = utils.proj4_str_to_dict(proj_str)
     proj_str2 = utils.proj4_dict_to_str(proj_dict)
     proj_dict2 = utils.proj4_str_to_dict(proj_str2)
     self.assertDictEqual(proj_dict, proj_dict2)
Beispiel #2
0
 def test_basic_numbered_tiles(self):
     """Test creating a multiple numbered tiles"""
     from satpy.writers.scmi import SCMIWriter
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     w = SCMIWriter(base_dir=self.base_dir, compress=True)
     area_def = AreaDefinition(
         'test',
         'test',
         'test',
         proj_dict=proj4_str_to_dict(
             '+proj=lcc +datum=WGS84 +ellps=WGS84 +lon_0=-95. +lat_0=25 +lat_1=25 +units=m +no_defs'
         ),
         x_size=100,
         y_size=200,
         area_extent=(-1000., -1500., 1000., 1500.),
     )
     now = datetime.utcnow()
     ds = DataArray(np.linspace(0., 1., 20000, dtype=np.float32).reshape(
         (200, 100)),
                    attrs=dict(name='test_ds',
                               platform_name='PLAT',
                               sensor='SENSOR',
                               units='1',
                               area=area_def,
                               start_time=now,
                               end_time=now + timedelta(minutes=20)))
     fn = w.save_datasets([ds],
                          sector_id='TEST',
                          source_name="TESTS",
                          tile_count=(3, 3))
     # `fn` is currently the last file created
     self.assertTrue(os.path.isfile(fn))
     self.assertIn('T009', fn)
Beispiel #3
0
    def _get_test_one_dataset(self):
        """Helper function to create a single test dataset."""
        import xarray as xr
        import dask.array as da
        from datetime import datetime
        from pyresample.geometry import AreaDefinition
        from pyresample.utils import proj4_str_to_dict
        area_def = AreaDefinition(
            'test',
            'test',
            'test',
            proj_dict=proj4_str_to_dict('+proj=geos +datum=WGS84 +ellps=WGS84 \
            +lon_0=0. h=36000. +units=km'),
            x_size=100,
            y_size=200,
            area_extent=(-1000., -1500., 1000., 1500.),
        )

        ds1 = xr.DataArray(da.zeros((100, 200), chunks=50),
                           dims=('y', 'x'),
                           attrs={
                               'name': 'test',
                               'start_time': datetime.utcnow(),
                               'platform_name': "TEST_PLATFORM_NAME",
                               'sensor': 'TEST_SENSOR_NAME',
                               'area': area_def
                           })
        return ds1
Beispiel #4
0
 def test_basic1(self):
     from pyresample.ewa import ll2cr
     from pyresample.geometry import SwathDefinition, AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     lon_arr = create_test_longitude(-95.0, -75.0, (50, 100), dtype=np.float64)
     lat_arr = create_test_latitude(18.0, 40.0, (50, 100), dtype=np.float64)
     swath_def = SwathDefinition(lon_arr, lat_arr)
     grid_info = static_lcc.copy()
     cw = grid_info["cell_width"]
     ch = grid_info["cell_height"]
     ox = grid_info["origin_x"]
     oy = grid_info["origin_y"]
     w = grid_info["width"]
     h = grid_info["height"]
     half_w = abs(cw / 2.)
     half_h = abs(ch / 2.)
     extents = [
         ox - half_w, oy - h * abs(ch) - half_h,
         ox + w * abs(cw) + half_w, oy + half_h
     ]
     area = AreaDefinition('test_area', 'test_area', 'test_area',
                           proj4_str_to_dict(grid_info['proj4_definition']),
                           w, h, extents)
     points_in_grid, lon_res, lat_res, = ll2cr(swath_def, area,
                                               fill=np.nan, copy=False)
     self.assertEqual(points_in_grid, lon_arr.size, "all points should be contained in a dynamic grid")
     self.assertIs(lon_arr, lon_res)
     self.assertIs(lat_arr, lat_res)
     self.assertEqual(points_in_grid, lon_arr.size, "all these test points should fall in this grid")
Beispiel #5
0
    def __init__(self, projparams=None, preserve_units=True, **kwargs):
        # Copy dict-type arguments as they will be modified in-place
        if isinstance(projparams, dict):
            projparams = projparams.copy()

        # Pyproj<2 uses __new__ to initiate data and does not define its own __init__ method.
        if is_pyproj2():
            # If init is found in any of the data, override any other area parameters.
            if 'init' in kwargs:
                warnings.warn('init="EPSG:XXXX" is no longer supported. Use "EPSG:XXXX" as a proj string instead')
                projparams = kwargs.pop('init')
            # Proj takes params in projparams over the params in kwargs.
            if isinstance(projparams, (dict, str)) and 'init' in projparams:
                warn_msg = '{"init": "EPSG:XXXX"} is no longer supported. Use "EPSG:XXXX" as a proj string instead'
                if isinstance(projparams, str):
                    warn_msg = '"+init=EPSG:XXXX" is no longer supported. Use "EPSG:XXXX" as a proj string instead'
                    # Proj-dicts are cleaner to parse than strings.
                    projparams = proj4_str_to_dict(projparams)
                warnings.warn(warn_msg)
                projparams = projparams.pop('init')
            # New syntax 'EPSG:XXXX'
            if 'EPSG' in kwargs or (isinstance(projparams, dict) and 'EPSG' in projparams):
                if 'EPSG' in kwargs:
                    epsg_code = kwargs.pop('EPSG')
                else:
                    epsg_code = projparams.pop('EPSG')
                projparams = 'EPSG:{}'.format(epsg_code)

            super(BaseProj, self).__init__(projparams=projparams, preserve_units=preserve_units, **kwargs)
Beispiel #6
0
 def test_expand_dims_3d(self):
     """Test expanding native resampling with 3D data."""
     from satpy.resample import NativeResampler
     import numpy as np
     import dask.array as da
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     ds1 = DataArray(da.zeros((3, 100, 50), chunks=85),
                     dims=('bands', 'y', 'x'),
                     coords={
                         'bands': ['R', 'G', 'B'],
                         'y': da.arange(100, chunks=85),
                         'x': da.arange(50, chunks=85)
                     })
     proj_dict = proj4_str_to_dict('+proj=lcc +datum=WGS84 +ellps=WGS84 '
                                   '+lon_0=-95. +lat_0=25 +lat_1=25 '
                                   '+units=m +no_defs')
     target = AreaDefinition(
         'test',
         'test',
         'test',
         proj_dict,
         100,
         200,
         (-1000., -1500., 1000., 1500.),
     )
     # source geo def doesn't actually matter
     resampler = NativeResampler(None, target)
     new_arr = resampler.resample(ds1)
     self.assertEqual(new_arr.shape, (3, 200, 100))
     new_arr2 = resampler.resample(ds1.compute())
     self.assertTrue(np.all(new_arr == new_arr2))
Beispiel #7
0
    def __init__(self, projparams=None, preserve_units=True, **kwargs):
        # Copy dict-type arguments as they will be modified in-place
        if isinstance(projparams, dict):
            projparams = projparams.copy()

        # Pyproj<2 uses __new__ to initiate data and does not define its own __init__ method.
        if is_pyproj2():
            # If init is found in any of the data, override any other area parameters.
            if 'init' in kwargs:
                warnings.warn(
                    'init="EPSG:XXXX" is no longer supported. Use "EPSG:XXXX" as a proj string instead'
                )
                projparams = kwargs.pop('init')
            # Proj takes params in projparams over the params in kwargs.
            if isinstance(projparams, (dict, str)) and 'init' in projparams:
                warn_msg = '{"init": "EPSG:XXXX"} is no longer supported. Use "EPSG:XXXX" as a proj string instead'
                if isinstance(projparams, str):
                    warn_msg = '"+init=EPSG:XXXX" is no longer supported. Use "EPSG:XXXX" as a proj string instead'
                    # Proj-dicts are cleaner to parse than strings.
                    projparams = proj4_str_to_dict(projparams)
                warnings.warn(warn_msg)
                projparams = projparams.pop('init')
            # New syntax 'EPSG:XXXX'
            if 'EPSG' in kwargs or (isinstance(projparams, dict)
                                    and 'EPSG' in projparams):
                if 'EPSG' in kwargs:
                    epsg_code = kwargs.pop('EPSG')
                else:
                    epsg_code = projparams.pop('EPSG')
                projparams = 'EPSG:{}'.format(epsg_code)

            super(BaseProj, self).__init__(projparams=projparams,
                                           preserve_units=preserve_units,
                                           **kwargs)
Beispiel #8
0
    def _get_test_dataset_three_bands_two_prereq(self, bands=3):
        """Helper function to create a single test dataset."""
        import xarray as xr
        import dask.array as da
        from datetime import datetime
        from pyresample.geometry import AreaDefinition
        from pyresample.utils import proj4_str_to_dict
        from satpy import DatasetID
        area_def = AreaDefinition(
            'test',
            'test',
            'test',
            proj4_str_to_dict('+proj=stere +datum=WGS84 +ellps=WGS84 '
                              '+lon_0=0. +lat_0=90 +lat_ts=60 +units=km'),
            100,
            200,
            (-1000., -1500., 1000., 1500.),
        )

        ds1 = xr.DataArray(
            da.zeros((bands, 100, 200), chunks=50),
            coords=[['R', 'G', 'B'], list(range(100)), list(range(200))],
            dims=('bands', 'y', 'x'),
            attrs={'name': 'test',
                   'start_time': datetime.utcnow(),
                   'platform_name': "TEST_PLATFORM_NAME",
                   'sensor': 'TEST_SENSOR_NAME',
                   'area': area_def,
                   'prerequisites': [DatasetID(name='1', calibration='reflectance'),
                                     DatasetID(name='2', calibration='reflectance')]}
        )
        return ds1
Beispiel #9
0
    def get_area_def(self, dsid):
        """Get area definition."""
        if not self.is_geo:
            raise NotImplementedError("Don't know how to get the Area Definition for this file")

        platform = self.get_platform(self['/attr/Platform_Name'])
        res = self._calc_area_resolution(dsid.resolution)
        proj = self._get_proj(platform, float(self['/attr/Subsatellite_Longitude']))
        area_name = '{} {} Area at {}m'.format(
            platform,
            self.metadata.get('sector_id', ''),
            int(res))
        lon = self._load_nav('pixel_longitude')
        lat = self._load_nav('pixel_latitude')
        extents = self._get_extents(proj, res, lon, lat)
        area_def = geometry.AreaDefinition(
            area_name,
            area_name,
            area_name,
            proj4_str_to_dict(proj),
            lon.shape[1],
            lon.shape[0],
            area_extent=extents,
        )
        return area_def
Beispiel #10
0
 def test_basic1(self):
     from pyresample.ewa import ll2cr
     from pyresample.geometry import SwathDefinition, AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     lon_arr = create_test_longitude(-95.0,
                                     -75.0, (50, 100),
                                     dtype=np.float64)
     lat_arr = create_test_latitude(18.0, 40.0, (50, 100), dtype=np.float64)
     swath_def = SwathDefinition(lon_arr, lat_arr)
     grid_info = static_lcc.copy()
     cw = grid_info["cell_width"]
     ch = grid_info["cell_height"]
     ox = grid_info["origin_x"]
     oy = grid_info["origin_y"]
     w = grid_info["width"]
     h = grid_info["height"]
     half_w = abs(cw / 2.)
     half_h = abs(ch / 2.)
     extents = [
         ox - half_w, oy - h * abs(ch) - half_h, ox + w * abs(cw) + half_w,
         oy + half_h
     ]
     area = AreaDefinition('test_area', 'test_area', 'test_area',
                           proj4_str_to_dict(grid_info['proj4_definition']),
                           w, h, extents)
     points_in_grid, lon_res, lat_res, = ll2cr(swath_def,
                                               area,
                                               fill=np.nan,
                                               copy=False)
     self.assertEqual(points_in_grid, lon_arr.size,
                      "all points should be contained in a dynamic grid")
     self.assertIs(lon_arr, lon_res)
     self.assertIs(lat_arr, lat_res)
     self.assertEqual(points_in_grid, lon_arr.size,
                      "all these test points should fall in this grid")
Beispiel #11
0
def _get_proj_data(projection):
    """Takes a proj4_dict or proj4_string and returns a proj4_dict and a Proj function.

    There is special handling for the "EPSG:XXXX" case where "XXXX" is an
    EPSG number code. It can be provided as a string `"EPSG:XXXX"` or as a
    dictionary (when provided via YAML) as `{'EPSG': XXXX}`.
    If it is passed as a string ("EPSG:XXXX") then the rules of
    :func:`~pyresample.utils._proj.proj4_str_to_dict` are followed.
    If a dictionary and pyproj 2.0+ is installed then the string
    `"EPSG:XXXX"` is passed to ``proj4_str_to_dict``. If pyproj<2.0
    is installed then the string ``+init=EPSG:XXXX`` is passed to
    ``proj4_str_to_dict`` which provides limited information to area
    config operations.

    """
    if isinstance(projection, dict) and 'EPSG' in projection:
        projection = "EPSG:{}".format(projection['EPSG'])

    if isinstance(projection, str):
        proj_dict = proj4_str_to_dict(projection)
    elif isinstance(projection, dict):
        proj_dict = projection
    else:
        raise TypeError(
            'Wrong type for projection: {0}. Expected dict or string.'.format(
                type(projection)))
    return proj_dict
Beispiel #12
0
 def test_basic_numbered_tiles(self):
     """Test creating a multiple numbered tiles"""
     from satpy.writers.scmi import SCMIWriter
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     w = SCMIWriter(base_dir=self.base_dir, compress=True)
     area_def = AreaDefinition(
         'test',
         'test',
         'test',
         proj_dict=proj4_str_to_dict('+proj=lcc +datum=WGS84 +ellps=WGS84 +lon_0=-95. +lat_0=25 +lat_1=25 +units=m +no_defs'),
         x_size=100,
         y_size=200,
         area_extent=(-1000., -1500., 1000., 1500.),
     )
     now = datetime.utcnow()
     ds = DataArray(
         np.linspace(0., 1., 20000, dtype=np.float32).reshape((200, 100)),
         attrs=dict(
             name='test_ds',
             platform_name='PLAT',
             sensor='SENSOR',
             units='1',
             area=area_def,
             start_time=now,
             end_time=now + timedelta(minutes=20))
     )
     fn = w.save_datasets([ds],
                          sector_id='TEST',
                          source_name="TESTS",
                          tile_count=(3, 3))
     # `fn` is currently the last file created
     self.assertTrue(os.path.isfile(fn))
     self.assertIn('T009', fn)
Beispiel #13
0
    def _get_test_dataset_with_bad_values(self, bands=3):
        """Helper function to create a single test dataset."""
        import xarray as xr
        import numpy as np
        from datetime import datetime
        from pyresample.geometry import AreaDefinition
        from pyresample.utils import proj4_str_to_dict
        area_def = AreaDefinition(
            'test',
            'test',
            'test',
            proj4_str_to_dict('+proj=stere +datum=WGS84 +ellps=WGS84 '
                              '+lon_0=0. +lat_0=90 +lat_ts=60 +units=km'),
            100,
            200,
            (-1000., -1500., 1000., 1500.),
        )

        data = np.arange(-210, 790, 100).reshape((2, 5)) * 0.95
        data /= 5.605
        data[0, 0] = np.nan  # need a nan value
        data[0, 1] = 0.  # Need a 0 value

        rgb_data = np.stack([data, data, data])
        ds1 = xr.DataArray(rgb_data,
                           dims=('bands', 'y', 'x'),
                           attrs={
                               'name': 'test',
                               'start_time': datetime.utcnow(),
                               'platform_name': "TEST_PLATFORM_NAME",
                               'sensor': 'TEST_SENSOR_NAME',
                               'area': area_def,
                               'prerequisites': ['1', '2', '3']
                           })
        return ds1
Beispiel #14
0
 def test_expand_without_dims(self):
     from satpy.resample import NativeResampler
     import numpy as np
     import dask.array as da
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     ds1 = DataArray(da.zeros((100, 50), chunks=85))
     proj_dict = proj4_str_to_dict('+proj=lcc +datum=WGS84 +ellps=WGS84 '
                                   '+lon_0=-95. +lat_0=25 +lat_1=25 '
                                   '+units=m +no_defs')
     target = AreaDefinition(
         'test',
         'test',
         'test',
         proj_dict,
         x_size=100,
         y_size=200,
         area_extent=(-1000., -1500., 1000., 1500.),
     )
     # source geo def doesn't actually matter
     resampler = NativeResampler(None, target)
     new_arr = resampler.resample(ds1)
     self.assertEqual(new_arr.shape, (200, 100))
     new_arr2 = resampler.resample(ds1.compute())
     self.assertTrue(np.all(new_arr == new_arr2))
Beispiel #15
0
def get_generic_projection_from_proj4(lat, lon, proj4_srs):
    """Short summary.

    Parameters
    ----------
    lat : type
        Description of parameter `lat`.
    lon : type
        Description of parameter `lon`.
    proj4_srs : type
        Description of parameter `proj4_srs`.

    Returns
    -------
    type
        Description of returned object.

    """
    try:
        from pyresample.utils import proj4_str_to_dict
        from pyresample.geometry import SwathDefinition
    except ImportError:
        print('please install pyresample to use this functionality')
    swath = SwathDefinition(lats=lat, lons=lon)
    area = swath.compute_optimal_bb_area(proj4_str_to_dict(proj4_srs))
    return area
Beispiel #16
0
 def test_lettered_tiles_bad_filename(self):
     """Test creating a lettered grid with a bad filename"""
     from satpy.writers.scmi import SCMIWriter
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     w = SCMIWriter(base_dir=self.base_dir, compress=True, file_pattern="{Bad Key}.nc")
     area_def = AreaDefinition(
         'test',
         'test',
         'test',
         proj_dict=proj4_str_to_dict('+proj=lcc +datum=WGS84 +ellps=WGS84 +lon_0=-95. +lat_0=25 +lat_1=25 +units=m +no_defs'),
         x_size=1000,
         y_size=2000,
         area_extent=(-1000000., -1500000., 1000000., 1500000.),
     )
     now = datetime.utcnow()
     ds = DataArray(
         np.linspace(0., 1., 2000000, dtype=np.float32).reshape((2000, 1000)),
         attrs=dict(
             name='test_ds',
             platform_name='PLAT',
             sensor='SENSOR',
             units='1',
             area=area_def,
             start_time=now,
             end_time=now + timedelta(minutes=20))
     )
     self.assertRaises(KeyError, w.save_datasets,
                       [ds],
                       sector_id='LCC',
                       source_name="TESTS",
                       tile_count=(3, 3),
                       lettered_grid=True)
Beispiel #17
0
 def test_expand_without_dims(self):
     """Test expanding native resampling with no dimensions specified."""
     from satpy.resample import NativeResampler
     import numpy as np
     import dask.array as da
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     ds1 = DataArray(da.zeros((100, 50), chunks=85))
     proj_dict = proj4_str_to_dict('+proj=lcc +datum=WGS84 +ellps=WGS84 '
                                   '+lon_0=-95. +lat_0=25 +lat_1=25 '
                                   '+units=m +no_defs')
     target = AreaDefinition(
         'test',
         'test',
         'test',
         proj_dict,
         x_size=100,
         y_size=200,
         area_extent=(-1000., -1500., 1000., 1500.),
     )
     # source geo def doesn't actually matter
     resampler = NativeResampler(None, target)
     new_arr = resampler.resample(ds1)
     self.assertEqual(new_arr.shape, (200, 100))
     new_arr2 = resampler.resample(ds1.compute())
     self.assertTrue(np.all(new_arr == new_arr2))
Beispiel #18
0
 def test_lettered_tiles_no_valid_data(self):
     """Test creating a lettered grid with no valid data."""
     from satpy.writers.awips_tiled import AWIPSTiledWriter
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     w = AWIPSTiledWriter(base_dir=self.base_dir, compress=True)
     area_def = AreaDefinition(
         'test',
         'test',
         'test',
         proj4_str_to_dict('+proj=lcc +datum=WGS84 +ellps=WGS84 +lon_0=-95. '
                           '+lat_0=25 +lat_1=25 +units=m +no_defs'),
         1000,
         2000,
         (-1000000., -1500000., 1000000., 1500000.),
     )
     now = datetime(2018, 1, 1, 12, 0, 0)
     ds = DataArray(
         da.full((2000, 1000), np.nan, chunks=500, dtype=np.float32),
         attrs=dict(
             name='test_ds',
             platform_name='PLAT',
             sensor='SENSOR',
             units='1',
             area=area_def,
             start_time=now,
             end_time=now + timedelta(minutes=20))
     )
     w.save_datasets([ds], sector_id='LCC', source_name="TESTS", tile_count=(3, 3), lettered_grid=True)
     # No files created - all NaNs should result in no tiles being created
     all_files = glob(os.path.join(self.base_dir, 'TESTS_AII*.nc'))
     assert not all_files
Beispiel #19
0
 def _get_test_lcc_data(self):
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     area_def = AreaDefinition(
         'test',
         'test',
         'test',
         proj4_str_to_dict(
             '+proj=lcc +datum=WGS84 +ellps=WGS84 +lon_0=-95. '
             '+lat_0=25 +lat_1=25 +units=m +no_defs'),
         100,
         200,
         (-1000., -1500., 1000., 1500.),
     )
     now = datetime(2018, 1, 1, 12, 0, 0)
     data = np.linspace(0., 1., 20000, dtype=np.float32).reshape((200, 100))
     ds = DataArray(da.from_array(data, chunks=50),
                    attrs=dict(name='test_ds',
                               platform_name='PLAT',
                               sensor='SENSOR',
                               units='1',
                               area=area_def,
                               start_time=now,
                               end_time=now + timedelta(minutes=20)))
     return ds
Beispiel #20
0
    def _get_test_dataset(self, bands=3):
        """Helper function to create a single test dataset."""
        import xarray as xr
        import dask.array as da
        from datetime import datetime
        from pyresample.geometry import AreaDefinition
        from pyresample.utils import proj4_str_to_dict
        area_def = AreaDefinition(
            'test',
            'test',
            'test',
            proj4_str_to_dict('+proj=stere +datum=WGS84 +ellps=WGS84 '
                              '+lon_0=0. +lat_0=90 +lat_ts=60 +units=km'),
            100,
            200,
            (-1000., -1500., 1000., 1500.),
        )

        ds1 = xr.DataArray(da.zeros((bands, 100, 200), chunks=50),
                           dims=('bands', 'y', 'x'),
                           attrs={
                               'name': 'test',
                               'start_time': datetime.utcnow(),
                               'platform_name': "TEST_PLATFORM_NAME",
                               'sensor': 'TEST_SENSOR_NAME',
                               'area': area_def,
                               'prerequisites': ['1', '2', '3']
                           })
        return ds1
Beispiel #21
0
    def _get_test_dataset_calibration_one_dataset(self, bands=1):
        """Helper function to create a single test dataset."""
        import xarray as xr
        import dask.array as da
        from datetime import datetime
        from pyresample.geometry import AreaDefinition
        from pyresample.utils import proj4_str_to_dict
        from satpy import DatasetID
        from satpy.scene import Scene
        area_def = AreaDefinition(
            'test',
            'test',
            'test',
            proj4_str_to_dict('+proj=stere +datum=WGS84 +ellps=WGS84 '
                              '+lon_0=0. +lat_0=90 +lat_ts=60 +units=km'),
            100,
            200,
            (-1000., -1500., 1000., 1500.),
        )

        d = [DatasetID(name='4', calibration='brightness_temperature')]
        scene = Scene()
        scene["4"] = xr.DataArray(
            da.zeros((100, 200), chunks=50),
            dims=('y', 'x'),
            attrs={'calibration': 'brightness_temperature'})

        data = scene['4']
        calibration = []
        for p in scene:
            calibration.append(p.attrs['calibration'])
        new_attrs = {
            'name': 'datasets',
            'start_time': datetime.utcnow(),
            'platform_name': "TEST_PLATFORM_NAME",
            'sensor': 'test-sensor',
            'area': area_def,
            'prerequisites': d,
            'metadata_requirements': {
                'order': ['4'],
                'config': {
                    '4': {
                        'alias': 'BT',
                        'calibration': 'brightness_temperature',
                        'min-val': '-150',
                        'max-val': '50'
                    },
                },
                'translate': {
                    '4': '4',
                },
                'file_pattern': 'test-dataset-{start_time:%Y%m%d%H%M%S}.mitiff'
            }
        }
        ds1 = xr.DataArray(data=data.data,
                           attrs=new_attrs,
                           dims=data.dims,
                           coords=data.coords)
        return ds1
Beispiel #22
0
def _get_proj4_args(proj4_args):
    """Create dict from proj4 args."""
    from pyresample.utils._proj4 import convert_proj_floats
    if isinstance(proj4_args, (str, six.text_type)):
        proj_config = proj4_str_to_dict(str(proj4_args))
    else:
        from configobj import ConfigObj
        proj_config = ConfigObj(proj4_args)
    return convert_proj_floats(proj_config.items())
Beispiel #23
0
    def test_3d_ewa(self, ll2cr, fornav):
        """Test EWA with a 3D dataset."""
        import numpy as np
        import dask.array as da
        import xarray as xr
        from satpy.resample import resample_dataset
        from pyresample.geometry import SwathDefinition, AreaDefinition
        from pyresample.utils import proj4_str_to_dict
        lons = xr.DataArray(da.zeros((10, 10), chunks=5))
        lats = xr.DataArray(da.zeros((10, 10), chunks=5))
        ll2cr.return_value = (100, np.zeros(
            (10, 10), dtype=np.float32), np.zeros((10, 10), dtype=np.float32))
        fornav.return_value = ([100 * 200] * 3,
                               [np.zeros((200, 100), dtype=np.float32)] * 3)
        sgd = SwathDefinition(lons, lats)
        proj_dict = proj4_str_to_dict('+proj=lcc +datum=WGS84 +ellps=WGS84 '
                                      '+lon_0=-95. +lat_0=25 +lat_1=25 '
                                      '+units=m +no_defs')
        tgd = AreaDefinition(
            'test',
            'test',
            'test',
            proj_dict,
            100,
            200,
            (-1000., -1500., 1000., 1500.),
        )
        input_data = xr.DataArray(da.zeros((3, 10, 10),
                                           chunks=5,
                                           dtype=np.float32),
                                  dims=('bands', 'y', 'x'),
                                  attrs={
                                      'area': sgd,
                                      'test': 'test'
                                  })

        new_data = resample_dataset(input_data, tgd, resampler='ewa')
        self.assertTupleEqual(new_data.shape, (3, 200, 100))
        self.assertEqual(new_data.dtype, np.float32)
        self.assertEqual(new_data.attrs['test'], 'test')
        self.assertIs(new_data.attrs['area'], tgd)
        # make sure we can actually compute everything
        new_data.compute()
        previous_calls = ll2cr.call_count

        # resample a different dataset and make sure cache is used
        input_data = xr.DataArray(da.zeros((3, 10, 10),
                                           chunks=5,
                                           dtype=np.float32),
                                  dims=('bands', 'y', 'x'),
                                  attrs={
                                      'area': sgd,
                                      'test': 'test'
                                  })
        new_data = resample_dataset(input_data, tgd, resampler='ewa')
        self.assertEqual(ll2cr.call_count, previous_calls)
        new_data.compute()
Beispiel #24
0
def _get_proj_data(projection):
    """Takes a proj4_dict or proj4_string and returns a proj4_dict and a Proj function."""
    if isinstance(projection, str):
        proj_dict = proj4_str_to_dict(projection)
    elif isinstance(projection, dict):
        proj_dict = projection
    else:
        raise TypeError('Wrong type for projection: {0}. Expected dict or string.'.format(type(projection)))
    return proj_dict
Beispiel #25
0
def _get_proj4_args(proj4_args):
    """Create dict from proj4 args."""
    from pyresample.utils._proj4 import convert_proj_floats
    if isinstance(proj4_args, (str, six.text_type)):
        proj_config = proj4_str_to_dict(str(proj4_args))
    else:
        from configobj import ConfigObj
        proj_config = ConfigObj(proj4_args)
    return convert_proj_floats(proj_config.items())
Beispiel #26
0
def _get_proj4_args(proj4_args):
    """Create dict from proj4 args."""
    from pyresample.utils._proj4 import convert_proj_floats
    if isinstance(proj4_args, str):
        # float conversion is done in `proj4_str_to_dict` already
        return proj4_str_to_dict(str(proj4_args))

    from configobj import ConfigObj
    proj_config = ConfigObj(proj4_args)
    return convert_proj_floats(proj_config.items())
Beispiel #27
0
    def __init__(self, proj4_terms, globe=None, bounds=None):
        terms = proj4_str_to_dict(proj4_terms)
        globe = _globe_from_proj4(terms) if globe is None else globe

        other_terms = []
        for term in terms.items():
            if term[0] not in _GLOBE_PARAMS:
                other_terms.append(term)
        super(_PROJ4Projection, self).__init__(other_terms, globe)

        self.bounds = bounds
Beispiel #28
0
 def test_basic_numbered_tiles_rgb(self):
     """Test creating a multiple numbered tiles with RGB."""
     from satpy.writers.awips_tiled import AWIPSTiledWriter
     import xarray as xr
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     w = AWIPSTiledWriter(base_dir=self.base_dir, compress=True)
     area_def = AreaDefinition(
         'test',
         'test',
         'test',
         proj4_str_to_dict(
             '+proj=lcc +datum=WGS84 +ellps=WGS84 +lon_0=-95. '
             '+lat_0=25 +lat_1=25 +units=m +no_defs'),
         100,
         200,
         (-1000., -1500., 1000., 1500.),
     )
     now = datetime(2018, 1, 1, 12, 0, 0)
     ds = DataArray(da.from_array(np.linspace(0.,
                                              1.,
                                              60000,
                                              dtype=np.float32).reshape(
                                                  (3, 200, 100)),
                                  chunks=50),
                    dims=('bands', 'y', 'x'),
                    coords={'bands': ['R', 'G', 'B']},
                    attrs=dict(name='test_ds',
                               platform_name='PLAT',
                               sensor='SENSOR',
                               units='1',
                               area=area_def,
                               start_time=now,
                               end_time=now + timedelta(minutes=20)))
     w.save_datasets([ds],
                     sector_id='TEST',
                     source_name="TESTS",
                     tile_count=(3, 3))
     chan_files = glob(
         os.path.join(self.base_dir, 'TESTS_AII*test_ds_R*.nc'))
     all_files = chan_files[:]
     assert len(chan_files) == 9
     chan_files = glob(
         os.path.join(self.base_dir, 'TESTS_AII*test_ds_G*.nc'))
     all_files.extend(chan_files)
     assert len(chan_files) == 9
     chan_files = glob(
         os.path.join(self.base_dir, 'TESTS_AII*test_ds_B*.nc'))
     assert len(chan_files) == 9
     all_files.extend(chan_files)
     for fn in all_files:
         ds = xr.open_dataset(fn, mask_and_scale=False)
         check_required_common_attributes(ds)
Beispiel #29
0
def _get_proj_data(projection):
    """Takes a proj4_dict or proj4_string and returns a proj4_dict and a Proj function."""
    if isinstance(projection, str):
        proj_dict = proj4_str_to_dict(projection)
    elif isinstance(projection, dict):
        proj_dict = projection
    else:
        raise TypeError(
            'Wrong type for projection: {0}. Expected dict or string.'.format(
                type(projection)))
    return proj_dict
Beispiel #30
0
    def test_nearest_neighbor_area_area(self):
        from pyresample import utils, geometry
        proj_str = "+proj=lcc +datum=WGS84 +ellps=WGS84 +lat_0=25 +lat_1=25 +lon_0=-95 +units=m +no_defs"
        proj_dict = utils.proj4_str_to_dict(proj_str)
        extents = [0, 0, 1000. * 5000, 1000. * 5000]
        area_def = geometry.AreaDefinition('CONUS', 'CONUS', 'CONUS',
                                           proj_dict, 400, 500, extents)

        extents2 = [-1000, -1000, 1000. * 4000, 1000. * 4000]
        area_def2 = geometry.AreaDefinition('CONUS', 'CONUS', 'CONUS',
                                           proj_dict, 600, 700, extents2)
        rows, cols = utils.generate_nearest_neighbour_linesample_arrays(area_def, area_def2, 12000.)
Beispiel #31
0
    def test_nearest_neighbor_grid_area(self):
        from pyresample import utils, geometry
        proj_str = "+proj=lcc +datum=WGS84 +ellps=WGS84 +lat_0=25 +lat_1=25 +lon_0=-95 +units=m +no_defs"
        proj_dict = utils.proj4_str_to_dict(proj_str)
        extents = [0, 0, 1000. * 2500., 1000. * 2000.]
        area_def = geometry.AreaDefinition('CONUS', 'CONUS', 'CONUS',
                                           proj_dict, 40, 50, extents)

        lon_arr = create_test_longitude(-100.0, -60.0, (550, 500), dtype=np.float64)
        lat_arr = create_test_latitude(20.0, 45.0, (550, 500), dtype=np.float64)
        grid = geometry.GridDefinition(lons=lon_arr, lats=lat_arr)
        rows, cols = utils.generate_nearest_neighbour_linesample_arrays(grid, area_def, 12000.)
Beispiel #32
0
def _create_test_area(proj_str=None, shape=DEFAULT_SHAPE, extents=None):
    """Create a test area definition."""
    from pyresample.geometry import AreaDefinition
    from pyresample.utils import proj4_str_to_dict
    if proj_str is None:
        proj_str = '+proj=lcc +datum=WGS84 +ellps=WGS84 +lon_0=-95. ' \
                   '+lat_0=25 +lat_1=25 +units=m +no_defs'
    proj_dict = proj4_str_to_dict(proj_str)
    extents = extents or (-1000., -1500., 1000., 1500.)

    return AreaDefinition('test', 'test', 'test', proj_dict, shape[1],
                          shape[0], extents)
Beispiel #33
0
def get_ioapi_pyresample_area_def(ds, proj4_srs):
    from pyresample import geometry, utils
    y_size = ds.NROWS
    x_size = ds.NCOLS
    projection = utils.proj4_str_to_dict(proj4_srs)
    proj_id = 'IOAPI_Dataset'
    description = 'IOAPI area_def for pyresample'
    area_id = 'MONET_Object_Grid'
    x_ll, y_ll = ds.XORIG + ds.XCELL * .5, ds.YORIG + ds.YCELL * .5
    x_ur, y_ur = ds.XORIG + (ds.NCOLS * ds.XCELL) + .5 * ds.XCELL, ds.YORIG + (
        ds.YCELL * ds.NROWS) + .5 * ds.YCELL
    area_extent = (x_ll, y_ll, x_ur, y_ur)
    area_def = geometry.AreaDefinition(area_id, description, proj_id,
                                       projection, x_size, y_size, area_extent)
    return area_def
Beispiel #34
0
    def test_3d_ewa(self, ll2cr, fornav):
        """Test EWA with a 3D dataset."""
        import numpy as np
        import dask.array as da
        import xarray as xr
        from satpy.resample import resample_dataset
        from pyresample.geometry import SwathDefinition, AreaDefinition
        from pyresample.utils import proj4_str_to_dict
        lons = xr.DataArray(da.zeros((10, 10), chunks=5))
        lats = xr.DataArray(da.zeros((10, 10), chunks=5))
        ll2cr.return_value = (100,
                              np.zeros((10, 10), dtype=np.float32),
                              np.zeros((10, 10), dtype=np.float32))
        fornav.return_value = ([100 * 200] * 3,
                               [np.zeros((200, 100), dtype=np.float32)] * 3)
        sgd = SwathDefinition(lons, lats)
        proj_dict = proj4_str_to_dict('+proj=lcc +datum=WGS84 +ellps=WGS84 '
                                      '+lon_0=-95. +lat_0=25 +lat_1=25 '
                                      '+units=m +no_defs')
        tgd = AreaDefinition(
            'test',
            'test',
            'test',
            proj_dict,
            x_size=100,
            y_size=200,
            area_extent=(-1000., -1500., 1000., 1500.),
        )
        input_data = xr.DataArray(
            da.zeros((3, 10, 10), chunks=5, dtype=np.float32),
            dims=('bands', 'y', 'x'), attrs={'area': sgd, 'test': 'test'})

        new_data = resample_dataset(input_data, tgd, resampler='ewa')
        self.assertTupleEqual(new_data.shape, (3, 200, 100))
        self.assertEqual(new_data.dtype, np.float32)
        self.assertEqual(new_data.attrs['test'], 'test')
        self.assertIs(new_data.attrs['area'], tgd)
        # make sure we can actually compute everything
        new_data.compute()
        previous_calls = ll2cr.call_count

        # resample a different dataset and make sure cache is used
        input_data = xr.DataArray(
            da.zeros((3, 10, 10), chunks=5, dtype=np.float32),
            dims=('bands', 'y', 'x'), attrs={'area': sgd, 'test': 'test'})
        new_data = resample_dataset(input_data, tgd, resampler='ewa')
        self.assertEqual(ll2cr.call_count, previous_calls)
        new_data.compute()
Beispiel #35
0
 def test_lettered_tiles_sector_ref(self):
     """Test creating a lettered grid using the sector as reference."""
     import xarray as xr
     from satpy.writers.scmi import SCMIWriter
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     w = SCMIWriter(base_dir=self.base_dir, compress=True)
     area_def = AreaDefinition(
         'test',
         'test',
         'test',
         proj4_str_to_dict(
             '+proj=lcc +datum=WGS84 +ellps=WGS84 +lon_0=-95. '
             '+lat_0=25 +lat_1=25 +units=m +no_defs'),
         1000,
         2000,
         (-1000000., -1500000., 1000000., 1500000.),
     )
     now = datetime(2018, 1, 1, 12, 0, 0)
     ds = DataArray(da.from_array(np.linspace(0.,
                                              1.,
                                              2000000,
                                              dtype=np.float32).reshape(
                                                  (2000, 1000)),
                                  chunks=500),
                    attrs=dict(name='test_ds',
                               platform_name='PLAT',
                               sensor='SENSOR',
                               units='1',
                               area=area_def,
                               start_time=now,
                               end_time=now + timedelta(minutes=20)))
     w.save_datasets([ds],
                     sector_id='LCC',
                     source_name="TESTS",
                     lettered_grid=True,
                     use_sector_reference=True,
                     use_end_time=True)
     all_files = glob(os.path.join(self.base_dir, 'TESTS_AII*.nc'))
     self.assertEqual(len(all_files), 16)
     for fn in all_files:
         nc = xr.open_dataset(fn, mask_and_scale=False)
         # geolocation coordinates should be monotonically increasing by 1
         np.testing.assert_equal(np.diff(nc['x']), 1)
         np.testing.assert_equal(np.diff(nc['y']), 1)
         assert nc.attrs['start_date_time'] == (
             now + timedelta(minutes=20)).strftime('%Y-%m-%dT%H:%M:%S')
Beispiel #36
0
def _get_test_target_area(output_shape, output_proj=None):
    from pyresample.geometry import AreaDefinition
    from pyresample.utils import proj4_str_to_dict
    if output_proj is None:
        output_proj = ('+proj=lcc +datum=WGS84 +ellps=WGS84 '
                       '+lon_0=-95. +lat_0=25 +lat_1=25 +units=m +no_defs')
    target = AreaDefinition(
        'test_target',
        'test_target',
        'test_target',
        proj4_str_to_dict(output_proj),
        output_shape[1],  # width
        output_shape[0],  # height
        (-100000., -150000., 100000., 150000.),
    )
    return target
Beispiel #37
0
 def test_basic_numbered_tiles_rgb(self):
     """Test creating a multiple numbered tiles with RGB."""
     from satpy.writers.scmi import SCMIWriter
     from xarray import DataArray
     from pyresample.geometry import AreaDefinition
     from pyresample.utils import proj4_str_to_dict
     w = SCMIWriter(base_dir=self.base_dir, compress=True)
     area_def = AreaDefinition(
         'test',
         'test',
         'test',
         proj_dict=proj4_str_to_dict(
             '+proj=lcc +datum=WGS84 +ellps=WGS84 +lon_0=-95. '
             '+lat_0=25 +lat_1=25 +units=m +no_defs'),
         x_size=100,
         y_size=200,
         area_extent=(-1000., -1500., 1000., 1500.),
     )
     now = datetime(2018, 1, 1, 12, 0, 0)
     ds = DataArray(da.from_array(np.linspace(0.,
                                              1.,
                                              60000,
                                              dtype=np.float32).reshape(
                                                  (3, 200, 100)),
                                  chunks=50),
                    dims=('bands', 'y', 'x'),
                    coords={'bands': ['R', 'G', 'B']},
                    attrs=dict(name='test_ds',
                               platform_name='PLAT',
                               sensor='SENSOR',
                               units='1',
                               area=area_def,
                               start_time=now,
                               end_time=now + timedelta(minutes=20)))
     w.save_datasets([ds],
                     sector_id='TEST',
                     source_name="TESTS",
                     tile_count=(3, 3))
     all_files = glob(os.path.join(self.base_dir,
                                   'TESTS_AII*test_ds_R*.nc'))
     self.assertEqual(len(all_files), 9)
     all_files = glob(os.path.join(self.base_dir,
                                   'TESTS_AII*test_ds_G*.nc'))
     self.assertEqual(len(all_files), 9)
     all_files = glob(os.path.join(self.base_dir,
                                   'TESTS_AII*test_ds_B*.nc'))
     self.assertEqual(len(all_files), 9)
Beispiel #38
0
 def __init__(self, projparams=None, preserve_units=True, **kwargs):
     # Pyproj<2 uses __new__ to initiate data and does not define its own __init__ method.
     if pyproj.__version__ >= '2':
         # If init is found in any of the data, override any other area parameters.
         if 'init' in kwargs:
             warnings.warn('init="EPSG:XXXX" is no longer supported. Use "EPSG:XXXX" as a proj string instead')
             projparams = kwargs.pop('init')
         # Proj takes params in projparams over the params in kwargs.
         if isinstance(projparams, (dict, str)) and 'init' in projparams:
             warn_msg = '{"init": "EPSG:XXXX"} is no longer supported. Use "EPSG:XXXX" as a proj string instead'
             if isinstance(projparams, str):
                 warn_msg = '"+init=EPSG:XXXX" is no longer supported. Use "EPSG:XXXX" as a proj string instead'
                 # Proj-dicts are cleaner to parse than strings.
                 projparams = proj4_str_to_dict(projparams)
             warnings.warn(warn_msg)
             projparams = projparams.pop('init')
         super(BaseProj, self).__init__(projparams=projparams, preserve_units=preserve_units, **kwargs)
Beispiel #39
0
    def get_area_def(self, dsid):
        if not self.is_geo:
            raise NotImplementedError("Don't know how to get the Area Definition for this file")

        platform = self.get_platform(self['/attr/Platform_Name'])
        res = self._calc_area_resolution(dsid.resolution)
        proj = self._get_proj(platform, float(self['/attr/Subsatellite_Longitude']))
        area_name = '{} {} Area at {}m'.format(
            platform,
            self.metadata.get('sector_id', ''),
            int(res))
        lon = self._load_nav('pixel_longitude')
        lat = self._load_nav('pixel_latitude')
        extents = self._get_extents(proj, res, lon, lat)
        area_def = geometry.AreaDefinition(
            area_name,
            area_name,
            area_name,
            proj_dict=proj4_str_to_dict(proj),
            x_size=lon.shape[1],
            y_size=lon.shape[0],
            area_extent=extents,
        )
        return area_def
Beispiel #40
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)