Ejemplo n.º 1
0
def regrid_data(lons, lats, target_lons, target_lats, array):
    """
    Regrids the irregular lons and lats produced by the reprojection so that
    imshow can handle it
    :param lons: an array of irregular longitude values produced by the
    reprojection function
    :param lats: an array of irregular latitude values produced by the
    reprojection function
    :param array: an array BTs or RGB values for each pixel
    :return: the array on an interpolated regular grid
    """

    # Define a target grid
    XI = target_lons
    YI = target_lats
    XI, YI = np.meshgrid(XI, YI)

    # Resample BT data
    def_a = SwathDefinition(lons=XI, lats=YI)
    def_b = SwathDefinition(lons=lons, lats=lats)
    interp_dat = resample_nearest(def_b,
                                  array,
                                  def_a,
                                  radius_of_influence=7000)

    return interp_dat
Ejemplo n.º 2
0
    def test_type_preserve(self):
        """Check that the type of resampled datasets is preserved."""
        from satpy.resample import resample_dataset
        import xarray as xr
        import dask.array as da
        import numpy as np
        from pyresample.geometry import SwathDefinition
        source_area = SwathDefinition(
            xr.DataArray(da.arange(4, chunks=5).reshape((2, 2)),
                         dims=['y', 'x']),
            xr.DataArray(da.arange(4, chunks=5).reshape((2, 2)),
                         dims=['y', 'x']))
        dest_area = SwathDefinition(
            xr.DataArray(da.arange(4, chunks=5).reshape((2, 2)) + .0001,
                         dims=['y', 'x']),
            xr.DataArray(da.arange(4, chunks=5).reshape((2, 2)) + .0001,
                         dims=['y', 'x']))
        expected_gap = np.array([[1, 2], [3, 255]])
        data = xr.DataArray(da.from_array(expected_gap, chunks=5),
                            dims=['y', 'x'])
        data.attrs['_FillValue'] = 255
        data.attrs['area'] = source_area
        res = resample_dataset(data, dest_area)
        self.assertEqual(res.dtype, data.dtype)
        self.assertTrue(np.all(res.values == expected_gap))

        expected_filled = np.array([[1, 2], [3, 3]])
        res = resample_dataset(data, dest_area, radius_of_influence=1000000)
        self.assertEqual(res.dtype, data.dtype)
        self.assertTrue(np.all(res.values == expected_filled))
Ejemplo n.º 3
0
 def _make_area_from_coords(self, coords):
     """Create an appropriate area with the given *coords*."""
     if len(coords) == 2:
         lon_sn = coords[0].attrs.get('standard_name')
         lat_sn = coords[1].attrs.get('standard_name')
         if lon_sn == 'longitude' and lat_sn == 'latitude':
             key = None
             try:
                 key = (coords[0].data.name, coords[1].data.name)
                 sdef = self.coords_cache.get(key)
             except AttributeError:
                 sdef = None
             if sdef is None:
                 sdef = SwathDefinition(*coords)
                 if key is not None:
                     self.coords_cache[key] = sdef
             sensor_str = '_'.join(self.info['sensors'])
             shape_str = '_'.join(map(str, coords[0].shape))
             sdef.name = "{}_{}_{}_{}".format(sensor_str, shape_str,
                                              coords[0].attrs['name'],
                                              coords[1].attrs['name'])
             return sdef
         else:
             raise ValueError(
                 'Coordinates info object missing standard_name key: ' +
                 str(coords))
     elif len(coords) != 0:
         raise NameError("Don't know what to do with coordinates " + str(
             coords))
Ejemplo n.º 4
0
    def test_numpy_basic_ewa(self, input_shape, input_dims,
                             maximum_weight_mode):
        """Test EWA with basic xarray DataArrays."""
        from pyresample.geometry import SwathDefinition
        output_shape = (200, 100)
        if len(input_shape) == 3:
            output_shape = (input_shape[0], output_shape[0], output_shape[1])
        swath_data, source_swath, target_area = get_test_data(
            input_shape=input_shape,
            output_shape=output_shape[-2:],
            input_dims=input_dims,
        )
        swath_data = swath_data.data.astype(np.float32).compute()
        source_swath = SwathDefinition(*source_swath.get_lonlats())

        resampler = DaskEWAResampler(source_swath, target_area)
        new_data = resampler.resample(swath_data,
                                      rows_per_scan=10,
                                      maximum_weight_mode=maximum_weight_mode)
        assert new_data.shape == output_shape
        assert new_data.dtype == np.float32
        assert isinstance(new_data, np.ndarray)

        # check how many valid pixels we have
        band_mult = 3 if len(output_shape) == 3 else 1
        assert np.count_nonzero(~np.isnan(new_data)) == 468 * band_mult
Ejemplo n.º 5
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
Ejemplo n.º 6
0
 def _make_area_from_coords(self, coords):
     """Create an appropriate area with the given *coords*."""
     if len(coords) == 2:
         lon_sn = coords[0].attrs.get('standard_name')
         lat_sn = coords[1].attrs.get('standard_name')
         if lon_sn == 'longitude' and lat_sn == 'latitude':
             key = None
             try:
                 key = (coords[0].data.name, coords[1].data.name)
                 sdef = self.coords_cache.get(key)
             except AttributeError:
                 sdef = None
             if sdef is None:
                 sdef = SwathDefinition(*coords)
                 if key is not None:
                     self.coords_cache[key] = sdef
             sensor_str = '_'.join(self.info['sensors'])
             shape_str = '_'.join(map(str, coords[0].shape))
             sdef.name = "{}_{}_{}_{}".format(sensor_str, shape_str,
                                              coords[0].attrs['name'],
                                              coords[1].attrs['name'])
             return sdef
         else:
             raise ValueError(
                 'Coordinates info object missing standard_name key: ' +
                 str(coords))
     elif len(coords) != 0:
         raise NameError("Don't know what to do with coordinates " +
                         str(coords))
Ejemplo n.º 7
0
    def combine_info(self, all_infos):
        """Combine metadata for multiple datasets.

        When loading data from multiple files it can be non-trivial to combine
        things like start_time, end_time, start_orbit, end_orbit, etc.

        By default this method will produce a dictionary containing all values
        that were equal across **all** provided info dictionaries.

        Additionally it performs the logical comparisons to produce the
        following if they exist:

         - start_time
         - end_time
         - start_orbit
         - end_orbit
         - satellite_altitude
         - satellite_latitude
         - satellite_longitude
         - orbital_parameters

         Also, concatenate the areas.

        """
        combined_info = combine_metadata(*all_infos)

        new_dict = self._combine(all_infos, min, 'start_time', 'start_orbit')
        new_dict.update(self._combine(all_infos, max, 'end_time', 'end_orbit'))
        new_dict.update(self._combine(all_infos, np.mean,
                                      'satellite_longitude',
                                      'satellite_latitude',
                                      'satellite_altitude'))

        # Average orbital parameters
        orb_params = [info.get('orbital_parameters', {}) for info in all_infos]
        if all(orb_params):
            # Collect all available keys
            orb_params_comb = {}
            for d in orb_params:
                orb_params_comb.update(d)

            # Average known keys
            keys = ['projection_longitude', 'projection_latitude', 'projection_altitude',
                    'satellite_nominal_longitude', 'satellite_nominal_latitude',
                    'satellite_actual_longitude', 'satellite_actual_latitude', 'satellite_actual_altitude',
                    'nadir_longitude', 'nadir_latitude']
            orb_params_comb.update(self._combine(orb_params, np.mean, *keys))
            new_dict['orbital_parameters'] = orb_params_comb

        try:
            area = SwathDefinition(lons=np.ma.vstack([info['area'].lons for info in all_infos]),
                                   lats=np.ma.vstack([info['area'].lats for info in all_infos]))
            area.name = '_'.join([info['area'].name for info in all_infos])
            combined_info['area'] = area
        except KeyError:
            pass

        new_dict.update(combined_info)
        return new_dict
Ejemplo n.º 8
0
def resample(
    in_lat,
    in_lon,
    out_lat,
    out_lon,
    data,
    method="inv_square",
    neighbours=8,
    radius_of_influence=500000,
    nprocs=4,
):
    masked_lat = in_lat.view(np.ma.MaskedArray)
    masked_lon = in_lon.view(np.ma.MaskedArray)
    masked_lon.mask = masked_lat.mask = data.view(np.ma.MaskedArray).mask

    input_def = SwathDefinition(lons=masked_lon, lats=masked_lat)
    target_def = SwathDefinition(lons=out_lon, lats=out_lat)

    if method == "inv_square":
        res = resample_custom(
            input_def,
            data,
            target_def,
            radius_of_influence=radius_of_influence,
            neighbours=neighbours,
            weight_funcs=lambda r: 1 / np.clip(r, 0.0625,
                                               np.finfo(r.dtype).max)**2,
            fill_value=None,
            nprocs=nprocs,
        )
    elif method == "bilinear":
        res = resample_custom(
            input_def,
            data,
            target_def,
            radius_of_influence=radius_of_influence,
            neighbours=4,
            weight_funcs=lambda r: 1 / np.clip(r, 0.0625,
                                               np.finfo(r.dtype).max),
            fill_value=None,
            nprocs=nprocs,
        )
    elif method == "nn":
        res = resample_nearest(
            input_def,
            data,
            target_def,
            radius_of_influence=radius_of_influence,
            fill_value=None,
            nprocs=nprocs,
        )
    else:
        raise ValueError("Unknown resample method: %s", method)

    if type(res.mask) == bool:
        res.mask = np.tile(res.mask, len(res))
    return res
Ejemplo n.º 9
0
 def test_match_integers(self):
     from pyresample.geometry import SwathDefinition
     from pyresample.kd_tree import get_neighbour_info
     lon = np.array([0, 10, 25])
     source_def  = SwathDefinition(*(lon, lon))
     target_def = SwathDefinition(*(lon, lon))
     valid_in, valid_out, indices_int, distances = get_neighbour_info(source_def, target_def, 1000, neighbours=1)
     lon = np.array([0, 10, 25]).astype(np.float64)
     source_def  = SwathDefinition(*(lon, lon))
     target_def = SwathDefinition(*(lon, lon))
     valid_in, valid_out, indices_float, distances = get_neighbour_info(source_def, target_def, 1000, neighbours=1)
Ejemplo n.º 10
0
    def crop(self, st, et, delta):
        # Crop data every 'delta' and split into IC and CG
        scn = Scene(glob.glob(entln_path + 'LtgFlashPortions' +
                              st.strftime('%Y%m%d') + '.csv'),
                    reader='entln')
        vname = 'timestamp'  # any name in data is OK, because we just bin the counts
        scn.load([vname])

        # ---- loop through hour and delta interval ----- #
        for h in range(st.hour, et.hour):
            for m in range(0, 60, delta):
                # 1. -----Crop by delta----- #
                timestamp = scn[vname].timestamp.values.astype('datetime64[s]')
                if m + delta < 60:
                    cond = (timestamp >= st.replace(hour=h, minute=m)) & (
                        timestamp < st.replace(hour=h, minute=m + delta))
                else:
                    cond = (timestamp >= st.replace(hour=h, minute=m)) & (
                        timestamp < st.replace(hour=h + 1, minute=0))

                # 2. -----Crop by type ----- #
                self.ic = copy.deepcopy(scn)
                self.cg = copy.deepcopy(scn)
                cond_cg = (scn[vname].type != 1) & (cond)
                cond_ic = (scn[vname].type == 1) & (cond)

                self.cg[vname] = self.cg[vname][cond_cg]
                # if we only use CG data, IC is eaual to CG here
                #   and the constant ratio: IC/CG = iccg_ratio is used later
                if only_cg:
                    self.ic[vname] = self.ic[vname][cond_cg]
                else:
                    self.ic[vname] = self.ic[vname][cond_ic]

                # Correct attrs
                area_ic = SwathDefinition(lons=self.ic[vname].coords['longitude'], \
                                          lats=self.ic[vname].coords['latitude']
                                          )
                area_cg = SwathDefinition(lons=self.cg[vname].coords['longitude'], \
                                          lats=self.cg[vname].coords['latitude']
                                          )
                self.correct_attrs(self.ic, area_ic, vname)
                self.correct_attrs(self.cg, area_cg, vname)

                # 3. -----Crop by WRF_grid ----- #
                self.resample_WRF()
                if only_cg:
                    self.tl = (self.ic[vname] * iccg_ratio +
                               self.cg[vname]) / cg_de
                else:
                    self.tl = self.ic[vname] / ic_de + self.cg[vname] / cg_de
                self.save(vname, h, m)
Ejemplo n.º 11
0
    def get_dataset(self, key, info):
        """Get a dataset from the file."""

        if key.name in CHANNEL_NAMES:
            dataset = self.calibrate([key])[0]
        else:  # Get sun-sat angles
            if key.name in ANGLES:
                if isinstance(getattr(self, ANGLES[key.name]), np.ndarray):
                    dataset = Projectable(
                        getattr(self, ANGLES[key.name]),
                        copy=False)
                else:
                    dataset = self.get_angles(key.name)
            else:
                logger.exception(
                    "Not a supported sun-sensor viewing angle: %s", key.name)
                raise

        # TODO get metadata
        if self.lons is None or self.lats is None:
            self.navigate()
        if self.area is None:
            self.area = SwathDefinition(self.lons, self.lats)
            self.area.name = 'wla'

        if not self._shape:
            self._shape = dataset.shape

        dataset.info['area'] = self.area
        return dataset
Ejemplo n.º 12
0
    def test_call(self):
        """Test atmospherical correction."""
        from pyresample.geometry import SwathDefinition

        from satpy.modifiers import PSPAtmosphericalCorrection

        # Patch methods
        lons = np.zeros((5, 5))
        lons[1, 1] = np.inf
        lons = da.from_array(lons, chunks=5)
        lats = np.zeros((5, 5))
        lats[1, 1] = np.inf
        lats = da.from_array(lats, chunks=5)
        area = SwathDefinition(lons, lats)
        stime = datetime(2020, 1, 1, 12, 0, 0)
        orb_params = {
            "satellite_actual_altitude": 12345678,
            "nadir_longitude": 0.0,
            "nadir_latitude": 0.0,
        }
        band = xr.DataArray(da.zeros((5, 5)),
                            attrs={
                                'area': area,
                                'start_time': stime,
                                'name': 'name',
                                'platform_name': 'platform',
                                'sensor': 'sensor',
                                'orbital_parameters': orb_params
                            },
                            dims=('y', 'x'))

        # Perform atmospherical correction
        psp = PSPAtmosphericalCorrection(name='dummy')
        res = psp(projectables=[band])
        res.compute()
Ejemplo n.º 13
0
    def test_areas_pyproj(self):
        """Test all areas have valid projections with pyproj."""
        import pyproj
        from pyresample import parse_area_file
        from pyresample.geometry import SwathDefinition
        from satpy.resample import get_area_file
        import numpy as np
        import xarray as xr

        lons = np.array([[0, 0.1, 0.2], [0.05, 0.15, 0.25]])
        lats = np.array([[0, 0.1, 0.2], [0.05, 0.15, 0.25]])
        lons = xr.DataArray(lons)
        lats = xr.DataArray(lats)
        swath_def = SwathDefinition(lons, lats)
        all_areas = parse_area_file(get_area_file())
        for area_obj in all_areas:
            if hasattr(area_obj, 'freeze'):
                try:
                    area_obj = area_obj.freeze(lonslats=swath_def)
                except RuntimeError:
                    # we didn't provide enough info to freeze, hard to guess
                    # in a generic test so just skip this area
                    continue
            proj_dict = area_obj.proj_dict
            _ = pyproj.Proj(proj_dict)
Ejemplo n.º 14
0
def write_geotiff(lonm, latm, datam, FILE_NAME):
    # Define SwathDefinition.
    swathDef = SwathDefinition(lons=lonm, lats=latm)    

    # Define GridDefinition.
    cellSize = 1
    x0, xinc, y0, yinc = (-180, cellSize, 90, -cellSize)
    nx, ny = (360, 180)
    x = np.linspace(x0, x0 + xinc*nx, nx)
    y = np.linspace(y0, y0 + yinc*ny, ny)
    lon_g, lat_g = np.meshgrid(x, y)
    grid_def = GridDefinition(lons=lon_g, lats=lat_g)

    # Set radius_of_influence.
    ri = 50000
    result = resample_nearest(swathDef, datam, grid_def, 
                              radius_of_influence=ri, epsilon=0.5,
                              fill_value=np.nan)
    [cols, rows] = result.shape
    driver = gdal.GetDriverByName("GTiff")
    outdata = driver.Create(FILE_NAME+".tif", rows, cols, 1, gdal.GDT_Float32)
    
    geotransform = ([x0, 1, 0, y0, 0, -1 ])
    outdata.SetGeoTransform(geotransform)
    srs = osr.SpatialReference()
    res = srs.ImportFromEPSG(4326)
    if res != 0:
        logger.info('Could not import from EPSG')
        outdata.SetProjection(srs.ExportToWkt())
        outdata.GetRasterBand(1).SetNoDataValue(np.nan)        
        outdata.GetRasterBand(1).WriteArray(result)        

        # Save to disk.
        outdata.FlushCache()
        outdata = None
Ejemplo n.º 15
0
def reprojection_bucket(var,lats,lons):

    '''
    Larmbert conformal projection
    bucket method
    see more information in https://pyresample.readthedocs.io/en/latest/api/pyresample.bucket.html?highlight=bucket
    '''

    width  = 179
    height = 199

    area_id     = 'Lambert_conformal'
    description = 'Lambert_conformal over Australia'

    # make the projection
    proj_dict   = {'proj': 'lcc', 'lat_0': -26., 'lon_0': 135., 'lat_1' = -0.6, 'lat_2'= -38.0, 'a': 6371228.0, 'units': 'm'}
 
    # create WRF domain 
    area_def    = create_area_def(area_id, proj_dict, center=(0,0) ,shape= (width, height), resolution=25000.0 units="m", description=description)

    # create gridinfo domain
    swath_def   = SwathDefinition(lons=lons, lats=lats)
    result   = resample_nearest(swath_def, var, area_def, radius_of_influence=20000, fill_value=None)

    return result, area_def    
Ejemplo n.º 16
0
def lonlat_to_swathdefinition(longitude=None, latitude=None):
    """Short summary.

    Parameters
    ----------
    longitude : type
        Description of parameter `longitude`.
    latitude : type
        Description of parameter `latitude`.

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

    """
    from pyresample.geometry import SwathDefinition
    from numpy import vstack
    if len(longitude.shape) < 2:
        lons = vstack(longitude)
        lats = vstack(latitude)
    else:
        lons = longitude
        lats = latitude
    return SwathDefinition(lons=lons, lats=lats)
Ejemplo n.º 17
0
 def _make_area_from_coords(self, coords):
     """Create an apropriate area with the given *coords*."""
     if (len(coords) == 2
             and coords[0].info.get('standard_name') == 'longitude'
             and coords[1].info.get('standard_name') == 'latitude'):
         from pyresample.geometry import SwathDefinition
         sdef = SwathDefinition(*coords)
         sensor_str = sdef.name = '_'.join(self.info['sensors'])
         shape_str = '_'.join(map(str, coords[0].shape))
         sdef.name = "{}_{}_{}_{}".format(sensor_str, shape_str,
                                          coords[0].info['name'],
                                          coords[1].info['name'])
         return sdef
     elif len(coords) != 0:
         raise NameError("Don't know what to do with coordinates " +
                         str(coords))
Ejemplo n.º 18
0
def test_partial_filter(viirs_sdr_i01_scene):
    """Test that resampling completes even when coverage filters some datasets."""
    # make another DataArray that is shifted away from the target area
    # orig_lons, orig_lats = viirs_sdr_i01_scene['I01'].attrs['area'].get_lonlats()
    new_i01 = viirs_sdr_i01_scene["I01"].copy()
    orig_lons = new_i01.attrs["area"].lons
    orig_lats = new_i01.attrs["area"].lats
    new_lons = orig_lons + 180.0
    new_swath_def = SwathDefinition(new_lons, orig_lats)
    new_i01.attrs["name"] = "I01_2"
    new_i01.attrs["area"] = new_swath_def
    new_scn = Scene()
    new_scn["I01"] = viirs_sdr_i01_scene["I01"]
    new_scn["I01_2"] = new_i01

    with dask.config.set(scheduler=CustomScheduler(2)):
        scenes_to_save = resample_scene(
            new_scn,
            ["211e"],
            ["grids.conf"],
            None,
            is_polar2grid=True,
            grid_coverage=0.05,
        )
    assert len(scenes_to_save) == 1
    new_scn, data_ids = scenes_to_save[0]
    assert len(new_scn.keys()) == 1  # I01
Ejemplo n.º 19
0
    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)
Ejemplo n.º 20
0
Archivo: hrpt.py Proyecto: m4sth0/satpy
    def get_lonlats(self):
        if self.area is not None:
            return self.area
        from pyorbital.orbital import Orbital
        from pyorbital.geoloc import compute_pixels, get_lonlatalt
        from pyorbital.geoloc_instrument_definitions import avhrr

        if self.times is None:
            self.times = time_seconds(self._data["timecode"], self.year)
        scanline_nb = len(self.times)
        scan_points = np.arange(0, 2048, 32)
        # scan_points = np.arange(2048)

        sgeom = avhrr(scanline_nb, scan_points, apply_offset=False)
        # no attitude error
        rpy = [0, 0, 0]
        s_times = sgeom.times(self.times[:, np.newaxis]).ravel()
        # s_times = (np.tile(sgeom._times[0, :], (scanline_nb, 1)).astype(
        #    'timedelta64[s]') + self.times[:, np.newaxis]).ravel()

        orb = Orbital(self.platform_name)

        pixels_pos = compute_pixels(orb, sgeom, s_times, rpy)
        lons, lats, alts = get_lonlatalt(pixels_pos, s_times)
        self.lons, self.lats = geo_interpolate(
            lons.reshape((scanline_nb, -1)), lats.reshape((scanline_nb, -1)))

        self.area = SwathDefinition(self.lons, self.lats)
        self.area.name = '_'.join([self.platform_name, str(self.start_time()),
                                   str(self.end_time)])
        return self.area
Ejemplo n.º 21
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")
Ejemplo n.º 22
0
 def get_lonlats(self):
     if self.area is None:
         if self.lons is None or self.lats is None:
             self.lons, self.lats = self.get_full_lonlats()
         self.area = SwathDefinition(self.lons, self.lats)
         self.area.name = '_'.join([self.platform_name, str(self.start_time),
                                    str(self.end_time)])
     return self.area
Ejemplo n.º 23
0
 def get_lonlats(self):
     """Get lonlats."""
     if self.area is None:
         lons, lats = self.get_full_lonlats()
         self.area = SwathDefinition(lons, lats)
         self.area.name = '_'.join([self.platform_name, str(self.start_time),
                                    str(self.end_time)])
     return self.area
Ejemplo n.º 24
0
def _call_ll2cr(lons, lats, target_geo_def):
    """Wrap ll2cr() for handling dask delayed calls better."""
    new_src = SwathDefinition(lons, lats)
    swath_points_in_grid, cols, rows = ll2cr(new_src, target_geo_def)
    if swath_points_in_grid == 0:
        return (lons.shape, np.nan, lons.dtype), (lats.shape, np.nan,
                                                  lats.dtype)
    return np.stack([cols, rows], axis=0)
Ejemplo n.º 25
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()
Ejemplo n.º 26
0
def regrid_data_to_regular(lons, lats, array):
    """
    Regrids the irregular lons and lats produced by the reprojection so that
    imshow can handle it
    :param lons: an array of irregular longitude values produced by the
    reprojection function
    :param lats: an array of irregular latitude values produced by the
    reprojection function
    :param array: an array BTs or RGB values for each pixel
    :return: the array on an interpolated regular grid
    """

    # Define the average interval in the existing lons and lats
    intervals_lon = (np.max(lons) - np.min(lons)) / len(lons[0])
    intervals_lat = (np.max(lats) - np.min(lats)) / len(lats)

    # Define a regular grid
    XI = np.arange(np.min(lons), np.max(lons), intervals_lon)
    YI = np.arange(np.min(lats), np.max(lats), intervals_lat)
    XI, YI = np.meshgrid(XI, YI)

    # Resample BT data
    def_a = SwathDefinition(lons=XI, lats=YI)
    def_b = SwathDefinition(lons=lons, lats=lats)
    interp_dat_0 = resample_nearest(def_b,
                                    array[:, :, 0],
                                    def_a,
                                    radius_of_influence=7000)
    interp_dat_1 = resample_nearest(def_b,
                                    array[:, :, 1],
                                    def_a,
                                    radius_of_influence=7000)
    interp_dat_2 = resample_nearest(def_b,
                                    array[:, :, 2],
                                    def_a,
                                    radius_of_influence=7000)

    # Recombine into a single array
    interp_dat_BTs = array
    interp_dat_BTs[:, :, 0] = interp_dat_0
    interp_dat_BTs[:, :, 1] = interp_dat_1
    interp_dat_BTs[:, :, 2] = interp_dat_2

    return interp_dat_BTs
Ejemplo n.º 27
0
    def get_dataset(self, key, info):
        """Load a dataset
        """

        logger.debug('Reading %s.', key.name)

        if key.name in chans_dict:
            m_data = self.read_dataset(key, info)
        else:
            m_data = self.read_geo(key, info)
        m_data.info.update(info)
        if self.lons is None or self.lats is None:
            self.lons, self.lats = self.navigate(key)
        m_area_def = SwathDefinition(
            np.ma.masked_where(m_data.mask, self.lons, copy=False),
            np.ma.masked_where(m_data.mask, self.lats, copy=False))
        m_area_def.name = 'yo'
        m_data.info['area'] = m_area_def
        return m_data
Ejemplo n.º 28
0
 def _make_area_from_coords(self, coords):
     """Create an apropriate area with the given *coords*."""
     if (len(coords) == 2
             and coords[0].info.get('standard_name') == 'longitude'
             and coords[1].info.get('standard_name') == 'latitude'):
         from pyresample.geometry import SwathDefinition
         return SwathDefinition(*coords)
     elif len(coords) != 0:
         raise NameError("Don't know what to do with coordinates " +
                         str(coords))
Ejemplo n.º 29
0
    def combine_info(self, all_infos):
        """Combine metadata for multiple datasets.

        When loading data from multiple files it can be non-trivial to combine
        things like start_time, end_time, start_orbit, end_orbit, etc.

        By default this method will produce a dictionary containing all values
        that were equal across **all** provided info dictionaries.

        Additionally it performs the logical comparisons to produce the
        following if they exist:

         - start_time
         - end_time
         - start_orbit
         - end_orbit

         Also, concatenate the areas.

        """
        combined_info = combine_info(*all_infos)
        if 'start_time' not in combined_info and 'start_time' in all_infos[0]:
            combined_info['start_time'] = min(i['start_time']
                                              for i in all_infos)
        if 'end_time' not in combined_info and 'end_time' in all_infos[0]:
            combined_info['end_time'] = max(i['end_time'] for i in all_infos)
        if 'start_orbit' not in combined_info and 'start_orbit' in all_infos[0]:
            combined_info['start_orbit'] = min(i['start_orbit']
                                               for i in all_infos)
        if 'end_orbit' not in combined_info and 'end_orbit' in all_infos[0]:
            combined_info['end_orbit'] = max(i['end_orbit'] for i in all_infos)

        try:
            area = SwathDefinition(
                lons=np.ma.vstack([info['area'].lons for info in all_infos]),
                lats=np.ma.vstack([info['area'].lats for info in all_infos]))
            area.name = '_'.join([info['area'].name for info in all_infos])
            combined_info['area'] = area
        except KeyError:
            pass

        return combined_info
Ejemplo n.º 30
0
    def combine_info(self, all_infos):
        """Combine metadata for multiple datasets.

        When loading data from multiple files it can be non-trivial to combine
        things like start_time, end_time, start_orbit, end_orbit, etc.

        By default this method will produce a dictionary containing all values
        that were equal across **all** provided info dictionaries.

        Additionally it performs the logical comparisons to produce the
        following if they exist:

         - start_time
         - end_time
         - start_orbit
         - end_orbit
         - satellite_altitude
         - satellite_latitude
         - satellite_longitude

         Also, concatenate the areas.

        """
        combined_info = combine_metadata(*all_infos)

        new_dict = self._combine(all_infos, min, 'start_time', 'start_orbit')
        new_dict.update(self._combine(all_infos, max, 'end_time', 'end_orbit'))
        new_dict.update(
            self._combine(all_infos, np.mean, 'satellite_longitude',
                          'satellite_latitude', 'satellite_altitude'))

        try:
            area = SwathDefinition(
                lons=np.ma.vstack([info['area'].lons for info in all_infos]),
                lats=np.ma.vstack([info['area'].lats for info in all_infos]))
            area.name = '_'.join([info['area'].name for info in all_infos])
            combined_info['area'] = area
        except KeyError:
            pass

        new_dict.update(combined_info)
        return new_dict
Ejemplo n.º 31
0
 def _make_area_from_coords(self, coords):
     """Create an apropriate area with the given *coords*."""
     if len(coords) == 2:
         lon_sn = coords[0].info.get('standard_name')
         lat_sn = coords[1].info.get('standard_name')
         if lon_sn == 'longitude' and lat_sn == 'latitude':
             sdef = SwathDefinition(*coords)
             sensor_str = sdef.name = '_'.join(self.info['sensors'])
             shape_str = '_'.join(map(str, coords[0].shape))
             sdef.name = "{}_{}_{}_{}".format(sensor_str, shape_str,
                                              coords[0].info['name'],
                                              coords[1].info['name'])
             return sdef
         else:
             raise ValueError(
                 'Coordinates info object missing standard_name key: ' +
                 str(coords))
     elif len(coords) != 0:
         raise NameError("Don't know what to do with coordinates " +
                         str(coords))