Xarray
convert_ds_dtypes(ds, verbose=False)
Convert all float variables to float32 and all int variables to int32 in an xarray Dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ds
|
Dataset
|
Input xarray Dataset. |
required |
verbose
|
bool
|
Whether to print out variables converted |
False
|
Returns:
Name | Type | Description |
---|---|---|
ds_out |
Dataset
|
Dataset with all float variables converted to float32 and all int variables to int32. |
Source code in isca_tools/utils/xarray.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 |
|
flatten_to_numpy(var, keep_dim=None)
Flattens var
to a numpy array with at most 2 dimensions.
Examples:
If var
has dims=(lat, lon, lev)
and keep_dim=lev
, it will return a numpy array of
size [n_lat*n_lon, n_lev]
.
If var
has dims=(lat, lon)
and keep_dim=None
, it will return a numpy array of
size [n_lat*n_lon]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
var
|
DataArray
|
Variable to flatten. |
required |
keep_dim
|
Optional[str]
|
Dimension along which not to flatten. |
None
|
Returns:
Name | Type | Description |
---|---|---|
var_flatten |
ndarray
|
Numpy array with flattened dimension first, and |
Source code in isca_tools/utils/xarray.py
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 |
|
print_ds_var_list(ds, phrase=None)
Prints all variables in ds
which contain phrase
in the variable name or variable long_name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ds
|
Dataset
|
Dataset to investigate variables of. |
required |
phrase
|
Optional[str]
|
Key phrase to search for in variable info. |
None
|
Source code in isca_tools/utils/xarray.py
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 |
|
set_attrs(var, overwrite=True, **kwargs)
Set attributes of a given variable.
Examples:
set_attrs(ds.plev, long_name='pressure', units='Pa')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
var
|
DataArray
|
Variable to set attributes of. |
required |
overwrite
|
bool
|
If |
True
|
**kwargs
|
str
|
Attributes to set. Common ones include |
{}
|
Returns:
Type | Description |
---|---|
DataArray
|
|
Source code in isca_tools/utils/xarray.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
unflatten_from_numpy(arr, var, keep_dim=None)
Reconstructs an xarray.DataArray from a flattened NumPy array created by flatten_to_numpy
.
Examples:
If var
had dims=(lat, lon, lev) and keep_dim='lev'
, and arr
has shape (n_lat*n_lon, n_lev),
this will return a DataArray with dims (lat, lon, lev).
If var
had dims=(lat, lon)and
keep_dim=None, and
arr` has shape (n_lat*n_lon),
this will return a DataArray with dims (lat, lon).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr
|
ndarray
|
Flattened NumPy array from |
required |
var
|
DataArray
|
The original DataArray used to determine dimension order, shape, and coordinates. |
required |
keep_dim
|
Optional[str]
|
Dimension that was kept unflattened in |
None
|
Returns:
Type | Description |
---|---|
DataArray
|
xr.DataArray: DataArray with the original dimensions and coordinates restored. |
Source code in isca_tools/utils/xarray.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|