def get_linesample(lons, lats, source_area_def, nprocs=1): """Return index row and col arrays for resampling. Parameters ---------- lons : numpy array Lons. Dimensions must match lats lats : numpy array Lats. Dimensions must match lons source_area_def : object Source definition as AreaDefinition object nprocs : int, optional Number of processor cores to be used Returns ------- (row_indices, col_indices) : tuple of numpy arrays Arrays for resampling area by array indexing """ # Proj.4 definition of source area projection proj_kwargs = {} if nprocs > 1: source_proj = _spatial_mp.Proj_MP(**source_area_def.proj_dict) proj_kwargs["nprocs"] = nprocs else: source_proj = Proj(**source_area_def.proj_dict) # get cartesian projection values from longitude and latitude source_x, source_y = source_proj(lons, lats, **proj_kwargs) # Find corresponding pixels (element by element conversion of ndarrays) source_pixel_x = (source_area_def.pixel_offset_x + source_x / source_area_def.pixel_size_x).astype(np.int32) source_pixel_y = (source_area_def.pixel_offset_y - source_y / source_area_def.pixel_size_y).astype(np.int32) return source_pixel_y, source_pixel_x
def get_lonlats(self, nprocs=None, data_slice=None, cache=False, dtype=None): """Returns lon and lat arrays of area. Parameters ---------- nprocs : int, optional Number of processor cores to be used. Defaults to the nprocs set when instantiating object data_slice : slice object, optional Calculate only coordinates for specified slice cache : bool, optional Store result the result. Requires data_slice to be None Returns ------- (lons, lats) : tuple of numpy arrays Grids of area lons and and lats """ if dtype is None: dtype = self.dtype if self.lons is None or self.lats is None: #Data is not cached if nprocs is None: nprocs = self.nprocs # Proj.4 definition of target area projection if nprocs > 1: target_proj = _spatial_mp.Proj_MP(**self.proj_dict) else: target_proj = _spatial_mp.Proj(**self.proj_dict) # Get coordinates of local area as ndarrays target_x, target_y = self.get_proj_coords( data_slice=data_slice, dtype=dtype) # Get corresponding longitude and latitude values lons, lats = target_proj(target_x, target_y, inverse=True, nprocs=nprocs) lons = np.asanyarray(lons, dtype=dtype) lats = np.asanyarray(lats, dtype=dtype) if cache and data_slice is None: # Cache the result if requested self.lons = lons self.lats = lats # Free memory del(target_x) del(target_y) else: #Data is cached if data_slice is None: # Full slice lons = self.lons lats = self.lats else: lons = self.lons[data_slice] lats = self.lats[data_slice] return lons, lats