Normalize variables using the given scales and (optionally) locations.
(values: xarray.Dataset,
scales: xarray.Dataset,
locations: Optional[xarray.Dataset],
)
| 27 | |
| 28 | |
| 29 | def normalize(values: xarray.Dataset, |
| 30 | scales: xarray.Dataset, |
| 31 | locations: Optional[xarray.Dataset], |
| 32 | ) -> xarray.Dataset: |
| 33 | """Normalize variables using the given scales and (optionally) locations.""" |
| 34 | def normalize_array(array): |
| 35 | if array.name is None: |
| 36 | raise ValueError( |
| 37 | "Can't look up normalization constants because array has no name.") |
| 38 | if locations is not None: |
| 39 | if array.name in locations: |
| 40 | array = array - locations[array.name].astype(array.dtype) |
| 41 | else: |
| 42 | logging.warning('No normalization location found for %s', array.name) |
| 43 | if array.name in scales: |
| 44 | array = array / scales[array.name].astype(array.dtype) |
| 45 | else: |
| 46 | logging.warning('No normalization scale found for %s', array.name) |
| 47 | return array |
| 48 | return xarray_tree.map_structure(normalize_array, values) |
| 49 | |
| 50 | |
| 51 | def unnormalize(values: xarray.Dataset, |
no outgoing calls
no test coverage detected