def test_wrap_and_check(self): from pyresample import utils lons1 = np.arange(-135., +135, 50.) lats = np.ones_like(lons1) * 70. new_lons, new_lats = utils.check_and_wrap(lons1, lats) self.assertIs(lats, new_lats) self.assertTrue(np.isclose(lons1, new_lons).all()) lons2 = np.where(lons1 < 0, lons1 + 360, lons1) new_lons, new_lats = utils.check_and_wrap(lons2, lats) self.assertIs(lats, new_lats) # after wrapping lons2 should look like lons1 self.assertTrue(np.isclose(lons1, new_lons).all()) lats2 = lats + 25. self.assertRaises(ValueError, utils.check_and_wrap, lons1, lats2)
def remap_data(self, dataarray, grid=None, **kwargs): """remaps from another grid to the current grid of self using pyresample. it assumes that the dimensions are ordered in ROW,COL,CHANNEL per pyresample docs Parameters ---------- grid : pyresample grid (SwathDefinition or AreaDefinition) Description of parameter `grid`. da : ndarray or xarray DataArray Description of parameter `dset`. radius_of_influence : float or integer radius of influcence for pyresample in meters. Returns ------- xarray.DataArray resampled object on current grid. """ from pyresample import utils from .util import resample from .util.interp_util import lonlat_to_swathdefinition as llsd # from .grids import get_generic_projection_from_proj4 # check to see if grid is supplied dataarray = _rename_to_monet_latlon(dataarray) lons_t, lats_t = utils.check_and_wrap(self.obj.longitude.values, self.obj.latitude.values) self.obj = _rename_to_monet_latlon(self.obj) lons_s, lats_s = utils.check_and_wrap(dataarray.longitude.values, dataarray.latitude.values) target = llsd(longitude=lons_t, latitude=lats_t) source = llsd(latitude=lats_s, longitude=lons_s) # target = get_generic_projection_from_proj4( # self.obj.latitude, self.object.longitude, self.obj.proj4_srs) # source_grid = get_generic_projection_from_proj4( # dataarray.latitude, dataarray.longitude, dataarray.proj4_srs) if grid is None: # grid is assumed to be in da.area out = resample.resample_dataset(dataarray, source, target, **kwargs) else: out = resample.resample_dataset(dataarray, grid, target, **kwargs) return out
def nearest_latlon(self, lat=None, lon=None, **kwargs): """Short summary. Parameters ---------- lat : type Description of parameter `lat`. lon : type Description of parameter `lon`. **kwargs : type Description of parameter `**kwargs`. Returns ------- type Description of returned object. """ try: from pyresample import utils from .util.interp_util import nearest_point_swathdefinition as npsd from .util.interp_util import lonlat_to_swathdefinition as llsd has_pyresample = True except ImportError: has_pyresample = False if has_pyresample: lons, lats = utils.check_and_wrap(self.obj.longitude.values, self.obj.latitude.values) swath = llsd(longitude=lons, latitude=lats) pswath = npsd(longitude=float(lon), latitude=float(lat)) row, col = utils.generate_nearest_neighbour_linesample_arrays( swath, pswath, float(1e6)) y, x = row[0][0], col[0][0] return self.obj.isel(x=x, y=y) else: vars = pd.Series(self.obj.variables) skip_keys = ['latitude', 'longitude', 'time', 'TFLAG'] loop_vars = vars.loc[~vars.isin(skip_keys)] kwargs = self._check_kwargs_and_set_defaults(**kwargs) kwargs['reuse_weights'] = True orig = self.obj[loop_vars.iloc[0]].monet.nearest_latlon( lat=lat, lon=lon, cleanup=False, **kwargs) dset = orig.to_dataset() dset.attrs = self.obj.attrs.copy() for i in loop_vars[1:-1].values: dset[i] = self.obj[i].monet.nearest_latlon(lat=lat, lon=lon, cleanup=False, **kwargs) i = loop_vars.values[-1] dset[i] = self.obj[i].monet.nearest_latlon(lat=lat, lon=lon, cleanup=True, **kwargs) return dset
def window(self, lat_min, lon_min, lat_max, lon_max): """Function to window, ie select a specific region, given the lower left latitude and longitude and the upper right latitude and longitude Parameters ---------- lat_min : float lower left latitude . lon_min : float lower left longitude. lat_max : float upper right latitude. lon_max : float upper right longitude. Returns ------- xr.DataSet returns the windowed object. """ try: from pyresample import utils from .util.interp_util import nearest_point_swathdefinition as npsd from .util.interp_util import lonlat_to_swathdefinition as llsd from numpy import concatenate has_pyresample = True except ImportError: has_pyresample = False try: if has_pyresample: lons, lats = utils.check_and_wrap(self.obj.longitude.values, self.obj.latitude.values) swath = llsd(longitude=lons, latitude=lats) pswath_ll = npsd(longitude=float(lon_min), latitude=float(lat_min)) pswath_ur = npsd(longitude=float(lon_max), latitude=float(lat_max)) row, col = utils.generate_nearest_neighbour_linesample_arrays( swath, pswath_ll, float(1e6)) y_ll, x_ll = row[0][0], col[0][0] row, col = utils.generate_nearest_neighbour_linesample_arrays( swath, pswath_ur, float(1e6)) y_ur, x_ur = row[0][0], col[0][0] if x_ur < x_ll: x1 = self.obj.x.where(self.obj.x >= x_ll, drop=True).values x2 = self.obj.x.where(self.obj.x <= x_ur, drop=True).values xrange = concatenate([x1, x2]).astype(int) self.obj['longitude'][:] = utils.wrap_longitudes( self.obj.longitude.values) # xrange = arange(float(x_ur), float(x_ll), dtype=int) else: xrange = slice(x_ll, x_ur) if y_ur < y_ll: y1 = self.obj.y.where(self.obj.y >= y_ll, drop=True).values y2 = self.obj.y.where(self.obj.y <= y_ur, drop=True).values yrange = concatenate([y1, y2]).astype(int) else: yrange = slice(y_ll, y_ur) return self.obj.sel(x=xrange, y=yrange) else: raise ImportError except ImportError: print('Window functionality is unavailable without pyresample')
def nearest_latlon(self, lat=None, lon=None, cleanup=True, esmf=False, **kwargs): """Uses xesmf to intepolate to a given latitude and longitude. Note that the conservative method is not available. Parameters ---------- lat : type Description of parameter `lat`. lon : type Description of parameter `lon`. **kwargs : type Description of parameter `**kwargs`. Returns ------- type Description of returned object. """ try: from pyresample import geometry, utils from .util.resample import resample_dataset from .util.interp_util import nearest_point_swathdefinition as npsd from .util.interp_util import lonlat_to_swathdefinition as llsd has_pyresample = True except ImportError: has_pyresample = False if esmf: has_pyresample = False from .util.interp_util import lonlat_to_xesmf from .util.resample import resample_xesmf try: if lat is None or lon is None: raise RuntimeError except RuntimeError: print('Must provide latitude and longitude') if has_pyresample: lons, lats = utils.check_and_wrap(self.obj.longitude.values, self.obj.latitude.values) swath = llsd(longitude=lons, latitude=lats) pswath = npsd(longitude=float(lon), latitude=float(lat)) row, col = utils.generate_nearest_neighbour_linesample_arrays( swath, pswath, float(1e6)) y, x = row[0][0], col[0][0] return self.obj.sel(x=x, y=y) else: kwargs = self._check_kwargs_and_set_defaults(**kwargs) self.obj = rename_latlon(self.obj) target = lonlat_to_xesmf(longitude=lon, latitude=lat) output = resample_xesmf(self.obj, target, **kwargs) if cleanup: output = resample_xesmf(self.obj, target, cleanup=True, **kwargs) return rename_latlon(output.squeeze())