-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcustom_functions.py
More file actions
71 lines (52 loc) · 1.87 KB
/
custom_functions.py
File metadata and controls
71 lines (52 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""
Module for various plotting functions
"""
import logging
import uxarray as ux
logger = logging.getLogger(__name__)
# =====================
# User-defined derived functions
# =====================
def diff_prev_timestep(field: ux.UxDataArray, dim: str = "Time") -> ux.UxDataArray:
"""
Return timestep-to-timestep differences for input field.
First timestep is filled with zeros.
"""
# Compute differences along Time
result = field.diff(dim=dim, n=1)
return result
def sum_fields(x1, x2):
return x1 + x2
def vert_max(field: ux.UxDataArray, dim: str = "nVertLevels") -> ux.UxDataArray:
"""
Take a 3d input field and return a 2d field representing the maximum value for each vertical column.
The "dim" input is the name of the vertical dimension to operate over.
"""
if dim not in field.dims:
raise ValueError(
f"Vertical dimension '{dim}' not found in data array. "
f"Available dimensions: {list(field.dims)}"
)
# Compute the maximum using xarray’s reduction
vertmax = field.max(dim=dim, keep_attrs=True)
return vertmax
def vert_min(field: ux.UxDataArray, dim: str = "nVertLevels") -> ux.UxDataArray:
"""
Take a 3d input field and return a 2d field representing the minimum value for each vertical column.
The "dim" input is the name of the vertical dimension to operate over.
"""
if dim not in field.dims:
raise ValueError(
f"Vertical dimension '{dim}' not found in data array. "
f"Available dimensions: {list(field.dims)}"
)
# Compute the minimum using xarray’s reduction
vertmin = field.min(dim=dim, keep_attrs=True)
return vertmin
DERIVED_FUNCTIONS = {
"diff_prev_timestep": diff_prev_timestep,
"sum_fields": sum_fields,
"vert_max": vert_max,
"vert_min": vert_min,
}