Skip to content

Commit 20a9524

Browse files
authored
Merge pull request #85 from petebunting/main
dded a new tools module tools.math and the function round_to_nearest_val
2 parents 61cb6f5 + 34b93c8 commit 20a9524

File tree

8 files changed

+92
-11
lines changed

8 files changed

+92
-11
lines changed

doc/python/source/rsgislib_tools.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ RSGISLib Tools
1111
rsgislib_tools_projection
1212
rsgislib_tools_plotting
1313
rsgislib_tools_stats
14+
rsgislib_tools_maths
1415
rsgislib_tools_imagetools
1516
rsgislib_tools_sensors
1617
rsgislib_tools_sysprofile
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
RSGISLib Maths Tools
2+
=============================
3+
4+
Normalisation
5+
----------------
6+
.. autofunction:: rsgislib.tools.maths.round_to_nearest_val
7+
8+
9+
10+
* :ref:`genindex`
11+
* :ref:`modindex`
12+
* :ref:`search`

python/rsgislib/__init__.py

+10
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@
163163
* VAR_TYPE_CONTINUOUS = 1
164164
* VAR_TYPE_CATEGORICAL = 2
165165
166+
167+
Options for rounding numbers:
168+
* ROUND_NEAREST = 1
169+
* ROUND_UP = 2
170+
* ROUND_DOWN = 3
171+
166172
"""
167173
from __future__ import print_function
168174

@@ -301,6 +307,10 @@
301307
VAR_TYPE_CONTINUOUS = 1
302308
VAR_TYPE_CATEGORICAL = 2
303309

310+
ROUND_NEAREST = 1
311+
ROUND_UP = 2
312+
ROUND_DOWN = 3
313+
304314

305315
def get_install_base_path() -> pathlib.PurePath:
306316
"""

python/rsgislib/tools/ftptools.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,13 @@ def download_curl_ftp_file(
417417
)
418418
)
419419
success = True
420-
except:
420+
except pycurl.error as e:
421421
print(
422422
"An error occurred when downloading {}.".format(
423423
os.path.join(ftp_url, remote_file)
424424
)
425425
)
426+
print(e)
426427
success = False
427428
return success
428429

python/rsgislib/tools/mapping.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -698,19 +698,29 @@ def create_raster_img_map(
698698
"Manual stretch requires user parameters to be passed"
699699
)
700700
img_data_strch = rsgislib.tools.plotting.manual_stretch_np_arr(
701-
img_data, min_max_vals=stch_min_max_vals, no_data_val=img_no_data_val, no_data_clr=no_data_out_clr,
701+
img_data,
702+
min_max_vals=stch_min_max_vals,
703+
no_data_val=img_no_data_val,
704+
no_data_clr=no_data_out_clr,
702705
)
703706
elif img_stch == rsgislib.IMG_STRETCH_LINEAR:
704707
img_data_strch = rsgislib.tools.plotting.linear_stretch_np_arr(
705-
img_data, no_data_val=img_no_data_val, no_data_clr=no_data_out_clr,
708+
img_data,
709+
no_data_val=img_no_data_val,
710+
no_data_clr=no_data_out_clr,
706711
)
707712
elif img_stch == rsgislib.IMG_STRETCH_STDEV:
708713
img_data_strch = rsgislib.tools.plotting.stdev_stretch_np_arr(
709-
img_data, n_stdevs=stch_n_stdevs, no_data_val=img_no_data_val, no_data_clr=no_data_out_clr,
714+
img_data,
715+
n_stdevs=stch_n_stdevs,
716+
no_data_val=img_no_data_val,
717+
no_data_clr=no_data_out_clr,
710718
)
711719
elif img_stch == rsgislib.IMG_STRETCH_CUMULATIVE:
712720
img_data_strch = rsgislib.tools.plotting.cumulative_stretch_np_arr(
713-
img_data, no_data_val=img_no_data_val, no_data_clr=no_data_out_clr,
721+
img_data,
722+
no_data_val=img_no_data_val,
723+
no_data_clr=no_data_out_clr,
714724
)
715725
else:
716726
print("No stretch is being used - is this what you intended?!")

python/rsgislib/tools/maths.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
"""
3+
The tools.maths module contains some useful tools for maths operations which aren't
4+
easily available else where.
5+
6+
"""
7+
from typing import Union, List
8+
9+
import numpy
10+
11+
import rsgislib
12+
13+
14+
def round_to_nearest_val(
15+
input_val: Union[numpy.array, int, float, List[int], List[float]],
16+
round_val: int = 1,
17+
round_direction: int = rsgislib.ROUND_NEAREST,
18+
):
19+
"""
20+
Rounds elements of a NumPy array to the nearest multiple of the value specified,
21+
with options for rounding up, down or to the nearest value. For example, if a
22+
round_val of 5 is provided then the numbers will be rounded to intervals of 5.
23+
24+
:param input_val: A NumPy array of numbers.
25+
:param round_val: The value by which the input is to be rounded to.
26+
:param round_direction: Specify the round direction (rsgislib.ROUND_NEAREST,
27+
rsgislib.ROUND_UP, or rsgislib.ROUND_DOWN). The
28+
default is rsgislib.ROUND_NEAREST.
29+
:return: a single value or a numpy array of the rounded values.
30+
31+
"""
32+
if isinstance(input_val, list):
33+
input_val = numpy.array(input_val, dtype=float)
34+
35+
round_val_flt = float(round_val)
36+
round_val_int = int(round_val)
37+
if round_direction == rsgislib.ROUND_NEAREST:
38+
return numpy.round(input_val / round_val_flt) * round_val_int
39+
elif round_direction == rsgislib.ROUND_UP:
40+
return numpy.ceil(input_val / round_val_flt) * round_val_int
41+
elif round_direction == rsgislib.ROUND_DOWN:
42+
return numpy.floor(input_val / round_val_flt) * round_val_int
43+
else:
44+
raise rsgislib.RSGISPyException(
45+
f"Invalid round_direction: '{round_direction}'. Please use "
46+
f"rsgislib.ROUND_NEAREST, rsgislib.ROUND_UP, or rsgislib.ROUND_DOWN."
47+
)

python/rsgislib/tools/plotting.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ def linear_stretch_np_arr(
10441044
out_int_type: bool = False,
10451045
min_out_val: float = 0,
10461046
max_out_val: float = 1,
1047-
no_data_clr:Tuple[float, float, float] = None,
1047+
no_data_clr: Tuple[float, float, float] = None,
10481048
) -> numpy.array:
10491049
"""
10501050
A function which performs a linear stretch using the min-max values on a per
@@ -1145,7 +1145,7 @@ def cumulative_stretch_np_arr(
11451145
out_int_type: bool = False,
11461146
min_out_val: float = 0,
11471147
max_out_val: float = 1,
1148-
no_data_clr:Tuple[float, float, float] = None,
1148+
no_data_clr: Tuple[float, float, float] = None,
11491149
) -> numpy.array:
11501150
"""
11511151
A function which performs a cumulative stretch using an upper and lower
@@ -1245,7 +1245,7 @@ def stdev_stretch_np_arr(
12451245
out_int_type: bool = False,
12461246
min_out_val: float = 0,
12471247
max_out_val: float = 1,
1248-
no_data_clr:Tuple[float, float, float] = None,
1248+
no_data_clr: Tuple[float, float, float] = None,
12491249
) -> numpy.array:
12501250
"""
12511251
A function which performs a standard deviation stretch using an upper and lower
@@ -1369,7 +1369,7 @@ def manual_stretch_np_arr(
13691369
out_int_type: bool = False,
13701370
min_out_val: float = 0,
13711371
max_out_val: float = 1,
1372-
no_data_clr:Tuple[float, float, float] = None,
1372+
no_data_clr: Tuple[float, float, float] = None,
13731373
) -> numpy.array:
13741374
"""
13751375
A function which performs a linear stretch using the min-max values provided

python/rsgislib/tools/stats.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
"""
3-
The tools.stats module contains some useful tools for calculating useful statistics which aren't
4-
easily available else where.
3+
The tools.stats module contains some useful tools for calculating useful statistics
4+
which aren't easily available else where.
55
"""
66
from typing import Union, List
77

0 commit comments

Comments
 (0)