def create_grid(geobox, coord_fn, depth=7): """ Interpolates a `NumPy` array based on the input coordinate function `coord_fn`. :param geobox: An instance of an `GriddedGeoBox` object. :param coord_fn: A function that maps coordinates. :return: A `NumPy` array. """ # Define the transform funtions func = partial(coord_fn, geobox=geobox, centre=True) # Get some basic info about the image shape = geobox.get_shape_yx() # Initialise the array to contain the result arr = numpy.zeros(shape, dtype="float64") interpolate_grid(arr, func, depth=depth, origin=(0, 0), shape=shape) return arr
def test_small_grid(self): """ Test grid too small to calculate bilinear interpolation """ with self.assertRaises(ValueError): in_arr = np.zeros(1).reshape(1,1) eval_func = lambda y, x: y * 1 + x depth = 7 interpolate_grid(in_arr, eval_func, depth)
def test_interpolate_default_max_depth(self): """ Test that the wrapper defaults to max depth """ result = np.arange(64).reshape(8, 8) in_arr = result.copy() # Interpolation is performed in place eval_func = lambda y, x: 8 * y + x depth = 10 interpolate_grid(result, eval_func, depth) self.assertTrue(np.allclose(result, in_arr))
def test_interpolate_grid(self): """ Simple test case for interpolate grid """ result = np.arange(64).reshape(8, 8) in_arr = result.copy() # Interpolation is performed in place eval_func = lambda y, x: 8 * y + x depth = 3 interpolate_grid(result, eval_func, depth) self.assertTrue(np.allclose(result, in_arr))
def create_lon_lat_grids( acquisition, out_group=None, compression=H5CompressionFilter.LZF, filter_opts=None, depth=7, ): """ Creates 2 by 2D NumPy arrays containing longitude and latitude co-ordinates for each array element. :param acquisition: An instance of an `Acquisition` object. :param out_group: If set to None (default) then the results will be returned as an in-memory hdf5 file, i.e. the `core` driver. Otherwise, a writeable HDF5 `Group` object. The dataset names will be given by: * contants.DatasetName.LON.value * contants.DatasetName.LAT.value :param compression: The compression filter to use. Default is H5CompressionFilter.LZF :filter_opts: A dict of key value pairs available to the given configuration instance of H5CompressionFilter. For example H5CompressionFilter.LZF has the keywords *chunks* and *shuffle* available. Default is None, which will use the default settings for the chosen H5CompressionFilter instance. :return: An opened `h5py.File` object, that is either in-memory using the `core` driver, or on disk. """ geobox = acquisition.gridded_geo_box() # Define the lon and lat transform funtions lon_func = partial(get_lon_coordinate, geobox=geobox, centre=True) lat_func = partial(get_lat_coordinate, geobox=geobox, centre=True) # Get some basic info about the image shape = geobox.get_shape_yx() # Initialise the array to contain the result result = numpy.zeros(shape, dtype="float64") interpolate_grid(result, lon_func, depth=depth, origin=(0, 0), shape=shape) # Initialise the output files if out_group is None: fid = h5py.File("longitude-latitude.h5", "w", driver="core", backing_store=False) else: fid = out_group if GroupName.LON_LAT_GROUP.value not in fid: fid.create_group(GroupName.LON_LAT_GROUP.value) grp = fid[GroupName.LON_LAT_GROUP.value] # define some base attributes for the image datasets attrs = { "crs_wkt": geobox.crs.ExportToWkt(), "geotransform": geobox.transform.to_gdal(), "description": LON_DESC, } if filter_opts is None: filter_opts = {} filter_opts["chunks"] = acquisition.tile_size kwargs = compression.config(**filter_opts).dataset_compression_kwargs() lon_dset = grp.create_dataset(DatasetName.LON.value, data=result, **kwargs) attach_image_attributes(lon_dset, attrs) result = numpy.zeros(shape, dtype="float64") interpolate_grid(result, lat_func, depth=depth, origin=(0, 0), shape=shape) attrs["description"] = LAT_DESC lat_dset = grp.create_dataset(DatasetName.LAT.value, data=result, **kwargs) attach_image_attributes(lat_dset, attrs) return fid