Base
area_weighting(var)
Apply area weighting to the variable var
using the cosine
of latitude: \(\cos (\phi)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
var
|
DataArray
|
Variable to weight e.g. |
required |
Returns:
Type | Description |
---|---|
DataArrayWeighted
|
Area weighted version of |
Source code in isca_tools/utils/base.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
get_memory_usage()
Get current process’s memory in MB.
Returns:
Name | Type | Description |
---|---|---|
mem_mb |
float
|
Memory usage in MB |
Source code in isca_tools/utils/base.py
101 102 103 104 105 106 107 108 109 110 |
|
len_safe(x)
Return length of x
which can have multiple values, or just be a number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Variable to return length of. |
required |
Returns:
Type | Description |
---|---|
int
|
Number of elements in |
Source code in isca_tools/utils/base.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
parse_int_list(value, format_func=lambda x: str(x), all_values=None)
Takes in a value or list of values e.g. [1, 2, 3]
and converts it into a list of strings where
each string has the format given by format_func
e.g. ['1', '2', '3']
for the default case.
There are three string options for value
:
* value='x:y'
, will return all integers between x
and y
inclusive.
* value='firstX'
will return first X values of all_values
.
* value='firstY'
will return first Y values of all_values
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
Union[str, int, List]
|
Variable to convert into list of strings |
required |
format_func
|
Callable
|
How to format each integer within the string. |
lambda x: str(x)
|
all_values
|
Optional[List]
|
List of all possible integers, must be provided if |
None
|
Returns:
Type | Description |
---|---|
List
|
List, where each integer in |
Source code in isca_tools/utils/base.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
print_log(text, logger=None)
Quick function to add to log if log exists, otherwise print it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
Text to be printed. |
required |
logger
|
Optional[Logger]
|
|
None
|
Returns:
Source code in isca_tools/utils/base.py
33 34 35 36 37 38 39 40 41 42 43 44 |
|
round_any(x, base, round_type='round')
Rounds x
to the nearest multiple of base
with the rounding done according to round_type
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Union[float, ndarray]
|
Number or array to round. |
required |
base
|
float
|
Rounds |
required |
round_type
|
str
|
One of the following, indicating how to round
|
'round'
|
Returns:
Type | Description |
---|---|
Union[float, ndarray]
|
Rounded version of |
Example
round_any(3, 5) = 5
round_any(3, 5, 'floor') = 0
Source code in isca_tools/utils/base.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
run_func_loop(func, max_wait_time=300, wait_interval=20, func_check=None, logger=None)
Safe way to run a function, such that if hit error, will try again every wait_interval
seconds up to a
maximum of max_wait_time
seconds.
If func_check
is given and returns True
at any point, it will exit the loop without executing func
.
Most obvious usage is for creating a directory e.g. os.makedirs
, especially to a server where connection
cuts in and out.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable
|
Function to run. Must have no arguments. |
required |
max_wait_time
|
int
|
Maximum number of seconds to try and run |
300
|
wait_interval
|
int
|
Interval in seconds to wait between running |
20
|
func_check
|
Optional[Callable]
|
Function that returns a boolean. If it returns |
None
|
logger
|
Optional[Logger]
|
Logger to record information |
None
|
Returns:
Type | Description |
---|---|
Whatever |
Source code in isca_tools/utils/base.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
split_list_max_n(lst, n)
Split lst
into balanced chunks with at most n
elements each.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lst
|
Union[List, ndarray]
|
List to split. |
required |
n
|
int
|
Maximum number of elements in each chunk of |
required |
Returns:
Type | Description |
---|---|
List
|
List of |
Source code in isca_tools/utils/base.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|