Commit 712fb501 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Replaced deprecated helper_functions.get_image_tileborders() with...

Replaced deprecated helper_functions.get_image_tileborders() with py_tools_ds.numeric.array.get_array_tilebounds(). Deleted deprecated helper_functions.cut_GMS_obj_into_blocks() and helper_functions.merge_GMS_tiles_to_GMS_obj()
Former-commit-id: efbd487e
Former-commit-id: 30ec45e8
parent de969c60
......@@ -2089,7 +2089,7 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3.0
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
......
......@@ -16,6 +16,7 @@ from tqdm import tqdm
from sklearn.cluster import k_means_ # noqa F401 # flake8 issue
from geoarray import GeoArray # noqa F401 # flake8 issue
from py_tools_ds.numeric.array import get_array_tilebounds
from ..config import GMS_config as CFG
from ..io.input_reader import SRF # noqa F401 # flake8 issue
......@@ -156,10 +157,11 @@ class SpectralResampler(object):
return np.array(spectrum_rsp)
def resample_image(self, image_cube, tiledims=(20, 20)):
# type: (Union[GeoArray, np.ndarray], int, bool) -> np.ndarray
# type: (Union[GeoArray, np.ndarray], tuple) -> np.ndarray
"""Resample the given spectral image cube according to the spectral response functions of the target instrument.
:param image_cube: image (3D array) containing the spectral information in the third dimension
:param tiledims: dimension of tiles to be used during computation
:return: resampled spectral image cube
"""
# input validation
......@@ -170,8 +172,7 @@ class SpectralResampler(object):
% image_cube.ndim)
assert image_cube.shape[2] == self.wvl_src_nm.size
from ..misc.helper_functions import get_image_tileborders
tilebounds = get_image_tileborders('block', tiledims, shape_fullArr=image_cube.shape)
tilebounds = get_array_tilebounds(array_shape=image_cube.shape, tile_shape=tiledims)
(R, C), B = image_cube.shape[:2], len(self.srf_tgt.bands)
image_rsp = np.zeros((R, C, B), dtype=image_cube.dtype)
......
......@@ -134,103 +134,6 @@ def convert_absPathArchive_to_GDALvsiPath(path_archive):
return os.path.join(gdal_prefix_dict[file_suffix], os.path.basename(path_archive))
def get_image_tileborders(target_tileShape, target_tileSize, path_GMS_file=None, shape_fullArr=None):
"""Calculates row/col bounds for image tiles according to the given parameters.
:param target_tileShape: <str> 'row','col','band','block','pixel' or 'custom'
:param target_tileSize: None or <tuple>. Only needed if target_tileShape=='block': (rows,cols)
:param path_GMS_file: <str> path to the *.gms file. Only needed if shape_fullArr is not given
:param shape_fullArr: <tuple> (rows,columns,bands)
"""
if shape_fullArr is None:
shape_fullArr = json.load(open(path_GMS_file))['shape_fullArr']
rows, cols = shape_fullArr[:2]
if target_tileShape == 'row':
return [i for i in range(rows)]
elif target_tileShape == 'col':
return [i for i in range(cols)]
elif target_tileShape == 'pixel':
return [[r, c] for r in range(rows) for c in range(cols)]
elif target_tileShape == 'block':
row_bounds = [0]
while row_bounds[-1] + target_tileSize[0] < rows:
row_bounds.append(row_bounds[-1] + target_tileSize[0] - 1)
row_bounds.append(row_bounds[-2] + target_tileSize[0])
else:
row_bounds.append(rows - 1)
col_bounds = [0]
while col_bounds[-1] + target_tileSize[1] < cols:
col_bounds.append(col_bounds[-1] + target_tileSize[1] - 1)
col_bounds.append(col_bounds[-2] + target_tileSize[1])
else:
col_bounds.append(cols - 1)
return [[tuple([row_bounds[r], row_bounds[r + 1]]), tuple([col_bounds[c], col_bounds[c + 1]])]
for r in range(0, len(row_bounds), 2) for c in range(0, len(col_bounds), 2)]
else:
raise ValueError("'%s' is not a valid target_tileShape. "
"Choose between 'row','col','band','block','pixel' or 'custom'!" % target_tileShape)
def cut_GMS_obj_into_blocks(tuple__In_obj__blocksize_RowsCols):
# type: (tuple) -> list
"""Cut a GMS object into tiles with respect to raster attributes as well as scene wide attributes.
:param tuple__In_obj__blocksize_RowsCols: a tuple with GMS_obj as first and [rows,cols] as second element"""
In_obj, blocksize_RowsCols = tuple__In_obj__blocksize_RowsCols
assert type(blocksize_RowsCols) in [list, tuple] and len(blocksize_RowsCols) == 2, \
"The argument 'blocksize_RowsCols' must represent a list of size 2."
# tilepos = get_image_tileborders('block', blocksize_RowsCols, shape_fullArr=In_obj.shape_fullArr)
return list(In_obj.to_tiles(blocksize=blocksize_RowsCols))
# NOTE: it's better to call get_subset_GMS_obj (also takes care of tile map infos)
# for tp in tilepos:
# (rS,rE),(cS,cE) = tp
# tile = parentObjDict[In_obj.proc_level](*initArgsDict[In_obj.proc_level])
# [setattr(tile, i, getattr(In_obj,i)) for i in In_obj.__dict__ \
# if not callable(getattr(In_obj,i)) and not isinstance(getattr(In_obj,i),(GeoArray, np.ndarray))]
# [setattr(tile, i, getattr(In_obj,i)[rS:rE+1, cS:cE+1]) \
# for i in In_obj.__dict__ \
# if not callable(getattr(In_obj,i)) and isinstance(getattr(In_obj,i),(GeoArray, np.ndarray))]
# tile.arr_shape = 'block'
# tile.arr_pos = tp
#
# yield tile
def merge_GMS_tiles_to_GMS_obj(list_GMS_tiles):
# type: (list) -> L1A_object
"""Merge separate GMS objects belonging to the same scene-ID to ONE GMS object
:param list_GMS_tiles: <list> of GMS objects that have been created by cut_GMS_obj_into_blocks()
"""
warnings.warn("'merge_GMS_tiles_to_GMS_obj' is deprecated. Use GMS_object.from_tiles() instead!",
DeprecationWarning)
if 'IMapUnorderedIterator' in str(type(list_GMS_tiles)):
list_GMS_tiles = list(list_GMS_tiles)
procLvl = list_GMS_tiles[0].proc_level
GMS_obj = parentObjDict[procLvl](*initArgsDict[procLvl])
[setattr(GMS_obj, i, getattr(list_GMS_tiles[0], i)) for i in list_GMS_tiles[0].__dict__
if not callable(getattr(list_GMS_tiles[0], )) and not isinstance(getattr(list_GMS_tiles[0], i), np.ndarray)]
GMS_obj.arr_shape = 'cube'
GMS_obj.arr_pos = None
list_arraynames = [i for i in list_GMS_tiles[0].__dict__ if not callable(getattr(list_GMS_tiles[0], i)) and
isinstance(getattr(list_GMS_tiles[0], i), np.ndarray)]
if list_arraynames:
GMS_obj = _numba_array_merger(GMS_obj, list_arraynames, list_GMS_tiles)
return GMS_obj
@jit
def _numba_array_merger(GMS_obj, list_arraynames, list_GMS_tiles):
"""private function, e.g. called by merge_GMS_tiles_to_GMS_obj() in order to fasten array merging"""
......
......@@ -12,6 +12,7 @@ from py_tools_ds.geo.projection import WKT2EPSG, get_UTMzone
from py_tools_ds.geo.coord_calc import calc_FullDataset_corner_positions
from py_tools_ds.geo.coord_trafo import pixelToLatLon, pixelToMapYX, imXY2mapXY
from py_tools_ds.geo.map_info import geotransform2mapinfo, mapinfo2geotransform
from py_tools_ds.numeric.array import get_array_tilebounds
from ..misc.logging import GMS_logger as DatasetLogger
from ..model.metadata import METADATA, get_LayerBandsAssignment
......@@ -432,8 +433,7 @@ class Dataset(object):
def get_tilepos(self, target_tileshape, target_tilesize):
self.tile_pos = [[target_tileshape, tb]
for tb in HLP_F.get_image_tileborders(target_tileshape, target_tilesize,
shape_fullArr=self.shape_fullArr)]
for tb in get_array_tilebounds(array_shape=self.shape_fullArr, tile_shape=target_tilesize)]
@staticmethod
def rescale_array(inArray, outScaleFactor, inScaleFactor=1):
......@@ -578,6 +578,8 @@ class Dataset(object):
# type: (tuple) -> self
"""Returns a generator object where items represent tiles of the given block size for the GMS object.
# NOTE: it's better to call get_subset_obj (also takes care of tile map infos)
:param blocksize: target dimensions of the generated block tile (rows, columns)
:return: <list> of GMS_object tiles
"""
......@@ -585,7 +587,7 @@ class Dataset(object):
assert type(blocksize) in [list, tuple] and len(blocksize) == 2, \
"The argument 'blocksize_RowsCols' must represent a tuple of size 2."
tilepos = HLP_F.get_image_tileborders('block', blocksize, shape_fullArr=self.shape_fullArr)
tilepos = get_array_tilebounds(array_shape=self.shape_fullArr, tile_shape=blocksize)
for tp in tilepos:
(xmin, xmax), (ymin, ymax) = tp # e.g. [(0, 1999), (0, 999)] at a blocksize of 2000*1000 (rowsxcols)
......
......@@ -28,6 +28,8 @@ from ..config import set_config, GMS_config
from .multiproc import MAP
from ..misc.definition_dicts import proc_chain, db_jobs_statistics_def
from py_tools_ds.numeric.array import get_array_tilebounds
if TYPE_CHECKING:
from collections import OrderedDict # noqa F401 # flake8 issue
......@@ -327,8 +329,8 @@ class process_controller(object):
else:
# define tile positions and size
def get_tilepos_list(GMSfile):
return HLP_F.get_image_tileborders('block', blocksize,
shape_fullArr=INP_R.GMSfile2dict(GMSfile)['shape_fullArr'])
return get_array_tilebounds(array_shape=INP_R.GMSfile2dict(GMSfile)['shape_fullArr'],
tile_shape=blocksize)
# get input parameters for creating GMS objects as blocks
work = [[GMSfile, ['block', tp]] for GMSfile in GMSfile_list_prevLvl_inDB
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment