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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
dp_from_pressure(p, dim='lev')
Compute layer pressure thickness Δp, preserving extra dims and coord order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
DataArray
|
Pressure [Pa], with vertical dimension |
required |
dim
|
str
|
Name of the vertical coordinate. |
'lev'
|
Returns:
| Type | Description |
|---|---|
DataArray
|
xr.DataArray: Pressure thickness Δp [Pa], same shape and coords as |
Source code in isca_tools/utils/base.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
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
102 103 104 105 106 107 108 109 110 111 | |
has_out_of_range(val, min_range, max_range)
Check if any number within val is outside the range between min_range and max_range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
Union[List, Tuple, ndarray, float]
|
Numbers to check |
required |
min_range
|
float
|
Minimum allowed value. |
required |
max_range
|
float
|
Maximum allowed value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if there is a value outside the range between |
Source code in isca_tools/utils/base.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
insert_to_array(x_values, y_values, x_new, y_new)
Insert multiple (x, y) pairs into arrays while preserving the sort order of x (ascending or descending).
Works for both NumPy arrays and xarray.DataArray objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_values
|
Union[ndarray, DataArray]
|
Array of x-values (must be sorted, ascending or descending). |
required |
y_values
|
Union[ndarray, DataArray]
|
Array of corresponding y-values. |
required |
x_new
|
Union[ndarray, DataArray, List, float]
|
New x-values to insert. |
required |
y_new
|
Union[ndarray, DataArray, List, float]
|
Corresponding y-values to insert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x_updated |
Union[ndarray, DataArray]
|
|
y_updated |
Union[ndarray, DataArray]
|
|
Source code in isca_tools/utils/base.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
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
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
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
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 197 198 | |
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
34 35 36 37 38 39 40 41 42 43 44 45 | |
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
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 230 231 | |
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
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 98 | |
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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
top_n_peaks_ind(var, n=1, min_ind_spacing=0)
Return the indices of the N largest values of var, such that the indices of these values
are ≥min_ind_spacing apart.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
ndarray
|
1D array containing variable values. Assumed in an order |
required |
n
|
int
|
Number of peaks to select. |
1
|
min_ind_spacing
|
int
|
Minimum index spacing between selected peaks. |
0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Indices of |
Source code in isca_tools/utils/base.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
weighted_RMS(var, weight=None, dim=None)
Compute (weighted) RMS of a DataArray or numpy array along specified dimension(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var
|
Union[DataArray, ndarray]
|
Variable to compute RMS for (shape [...]). |
required |
weight
|
Optional[Union[DataArray, ndarray]]
|
Weights (same shape as var along |
None
|
dim
|
Optional[Union[str, int, List[Union[str, int]]]]
|
Dimension(s) to reduce over. - For xarray: names of dimensions. - For numpy: integer axis or list of axes. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
rms |
Union[DataArray, ndarray]
|
Same type as input, reduced along |
Source code in isca_tools/utils/base.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |