File size: 13,316 Bytes
9d66a40 | 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 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 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 145 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 197 198 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 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 279 280 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 325 326 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 | """Copyright (c) Microsoft Corporation. Licensed under the MIT license."""
import dataclasses
from datetime import datetime
from functools import partial
from pathlib import Path
from typing import Callable, List
import numpy as np
import torch
from scipy.interpolate import RegularGridInterpolator as RGI
from aurora.normalisation import (
normalise_atmos_var,
normalise_surf_var,
unnormalise_atmos_var,
unnormalise_surf_var,
)
__all__ = ["Metadata", "Batch"]
@dataclasses.dataclass
class Metadata:
"""Metadata in a batch.
Args:
lat (:class:`torch.Tensor`): Latitudes.
lon (:class:`torch.Tensor`): Longitudes.
time (tuple[datetime, ...]): For every batch element, the time.
atmos_levels (tuple[int | float, ...]): Pressure levels for the atmospheric variables in
hPa.
rollout_step (int, optional): How many roll-out steps were used to produce this prediction.
If equal to `0`, which is the default, then this means that this is not a prediction,
but actual data. This field is automatically populated by the model and used to use a
separate LoRA for every roll-out step. Generally, you are safe to ignore this field.
"""
lat: torch.Tensor
lon: torch.Tensor
time: tuple[datetime, ...]
atmos_levels: tuple[int | float, ...]
rollout_step: int = 0
def __post_init__(self):
if not (torch.all(self.lat <= 90) and torch.all(self.lat >= -90)):
raise ValueError("Latitudes must be in the range [-90, 90].")
if not (torch.all(self.lon >= 0) and torch.all(self.lon < 360)):
raise ValueError("Longitudes must be in the range [0, 360).")
# Validate vector-valued latitudes and longitudes:
if self.lat.dim() == self.lon.dim() == 1:
if not torch.all(self.lat[1:] - self.lat[:-1] < 0):
raise ValueError("Latitudes must be strictly decreasing.")
if not torch.all(self.lon[1:] - self.lon[:-1] > 0):
raise ValueError("Longitudes must be strictly increasing.")
# Validate matrix-valued latitudes and longitudes:
elif self.lat.dim() == self.lon.dim() == 2:
if not torch.all(self.lat[1:, :] - self.lat[:-1, :]):
raise ValueError("Latitudes must be strictly decreasing along every column.")
if not torch.all(self.lon[:, 1:] - self.lon[:, :-1] > 0):
raise ValueError("Longitudes must be strictly increasing along every row.")
else:
raise ValueError(
"The latitudes and longitudes must either both be vectors or both be matrices."
)
@dataclasses.dataclass
class Batch:
"""A batch of data.
Args:
surf_vars (dict[str, :class:`torch.Tensor`]): Surface-level variables with shape
`(b, t, h, w)`.
static_vars (dict[str, :class:`torch.Tensor`]): Static variables with shape `(h, w)`.
atmos_vars (dict[str, :class:`torch.Tensor`]): Atmospheric variables with shape
`(b, t, c, h, w)`.
metadata (:class:`Metadata`): Metadata associated to this batch.
"""
surf_vars: dict[str, torch.Tensor]
static_vars: dict[str, torch.Tensor]
atmos_vars: dict[str, torch.Tensor]
metadata: Metadata
@property
def spatial_shape(self) -> tuple[int, int]:
"""Get the spatial shape from an arbitrary surface-level variable."""
return next(iter(self.surf_vars.values())).shape[-2:]
def normalise(self, surf_stats: dict[str, tuple[float, float]]) -> "Batch":
"""Normalise all variables in the batch.
Args:
surf_stats (dict[str, tuple[float, float]]): For these surface-level variables, adjust
the normalisation to the given tuple consisting of a new location and scale.
Returns:
:class:`.Batch`: Normalised batch.
"""
return Batch(
surf_vars={
k: normalise_surf_var(v, k, stats=surf_stats) for k, v in self.surf_vars.items()
},
static_vars={
k: normalise_surf_var(v, k, stats=surf_stats) for k, v in self.static_vars.items()
},
atmos_vars={
k: normalise_atmos_var(v, k, self.metadata.atmos_levels)
for k, v in self.atmos_vars.items()
},
metadata=self.metadata,
)
def unnormalise(self, surf_stats: dict[str, tuple[float, float]]) -> "Batch":
"""Unnormalise all variables in the batch.
Args:
surf_stats (dict[str, tuple[float, float]]): For these surface-level variables, adjust
the normalisation to the given tuple consisting of a new location and scale.
Returns:
:class:`.Batch`: Unnormalised batch.
"""
return Batch(
surf_vars={
k: unnormalise_surf_var(v, k, stats=surf_stats) for k, v in self.surf_vars.items()
},
static_vars={
k: unnormalise_surf_var(v, k, stats=surf_stats) for k, v in self.static_vars.items()
},
atmos_vars={
k: unnormalise_atmos_var(v, k, self.metadata.atmos_levels)
for k, v in self.atmos_vars.items()
},
metadata=self.metadata,
)
def crop(self, patch_size: int) -> "Batch":
"""Crop the variables in the batch to patch size `patch_size`."""
h, w = self.spatial_shape
if w % patch_size != 0:
raise ValueError("Width of the data must be a multiple of the patch size.")
if h % patch_size == 0:
return self
elif h % patch_size == 1:
return Batch(
surf_vars={k: v[..., :-1, :] for k, v in self.surf_vars.items()},
static_vars={k: v[..., :-1, :] for k, v in self.static_vars.items()},
atmos_vars={k: v[..., :-1, :] for k, v in self.atmos_vars.items()},
metadata=Metadata(
lat=self.metadata.lat[:-1],
lon=self.metadata.lon,
atmos_levels=self.metadata.atmos_levels,
time=self.metadata.time,
rollout_step=self.metadata.rollout_step,
),
)
else:
raise ValueError(
f"There can at most be one latitude too many, "
f"but there are {h % patch_size} too many."
)
def _fmap(self, f: Callable[[torch.Tensor], torch.Tensor]) -> "Batch":
return Batch(
surf_vars={k: f(v) for k, v in self.surf_vars.items()},
static_vars={k: f(v) for k, v in self.static_vars.items()},
atmos_vars={k: f(v) for k, v in self.atmos_vars.items()},
metadata=Metadata(
lat=f(self.metadata.lat),
lon=f(self.metadata.lon),
atmos_levels=self.metadata.atmos_levels,
time=self.metadata.time,
rollout_step=self.metadata.rollout_step,
),
)
def to(self, device: str | torch.device) -> "Batch":
"""Move the batch to another device."""
return self._fmap(lambda x: x.to(device))
def type(self, t: type) -> "Batch":
"""Convert everything to type `t`."""
return self._fmap(lambda x: x.type(t))
def regrid(self, res: float) -> "Batch":
"""Regrid the batch to a `res` degrees resolution.
This results in `float32` data on the CPU.
This function is not optimised for either speed or accuracy. Use at your own risk.
"""
shape = (round(180 / res) + 1, round(360 / res))
lat_new = torch.from_numpy(np.linspace(90, -90, shape[0]))
lon_new = torch.from_numpy(np.linspace(0, 360, shape[1], endpoint=False))
interpolate_res = partial(
interpolate,
lat=self.metadata.lat,
lon=self.metadata.lon,
lat_new=lat_new,
lon_new=lon_new,
)
return Batch(
surf_vars={k: interpolate_res(v) for k, v in self.surf_vars.items()},
static_vars={k: interpolate_res(v) for k, v in self.static_vars.items()},
atmos_vars={k: interpolate_res(v) for k, v in self.atmos_vars.items()},
metadata=Metadata(
lat=lat_new,
lon=lon_new,
atmos_levels=self.metadata.atmos_levels,
time=self.metadata.time,
rollout_step=self.metadata.rollout_step,
),
)
def to_netcdf(self, path: str | Path) -> None:
"""Write the batch to a file.
This requires `xarray` and `netcdf4` to be installed.
"""
try:
import xarray as xr
except ImportError as e:
raise RuntimeError("`xarray` must be installed.") from e
ds = xr.Dataset(
{
**{
f"surf_{k}": (("batch", "history", "latitude", "longitude"), _np(v))
for k, v in self.surf_vars.items()
},
**{
f"static_{k}": (("latitude", "longitude"), _np(v))
for k, v in self.static_vars.items()
},
**{
f"atmos_{k}": (("batch", "history", "level", "latitude", "longitude"), _np(v))
for k, v in self.atmos_vars.items()
},
},
coords={
"latitude": _np(self.metadata.lat),
"longitude": _np(self.metadata.lon),
"time": list(self.metadata.time),
"level": list(self.metadata.atmos_levels),
"rollout_step": self.metadata.rollout_step,
},
)
ds.to_netcdf(path)
@classmethod
def from_netcdf(cls, path: str | Path) -> "Batch":
"""Load a batch from a file."""
try:
import xarray as xr
except ImportError as e:
raise RuntimeError("`xarray` must be installed.") from e
ds = xr.load_dataset(path, engine="netcdf4")
surf_vars: List[str] = []
static_vars: List[str] = []
atmos_vars: List[str] = []
for k in ds:
if k.startswith("surf_"):
surf_vars.append(k.removeprefix("surf_"))
elif k.startswith("static_"):
static_vars.append(k.removeprefix("static_"))
elif k.startswith("atmos_"):
atmos_vars.append(k.removeprefix("atmos_"))
return Batch(
surf_vars={k: torch.from_numpy(ds[f"surf_{k}"].values) for k in surf_vars},
static_vars={k: torch.from_numpy(ds[f"static_{k}"].values) for k in static_vars},
atmos_vars={k: torch.from_numpy(ds[f"atmos_{k}"].values) for k in atmos_vars},
metadata=Metadata(
lat=torch.from_numpy(ds.latitude.values),
lon=torch.from_numpy(ds.longitude.values),
time=tuple(ds.time.values.astype("datetime64[s]").tolist()),
atmos_levels=tuple(ds.level.values),
rollout_step=int(ds.rollout_step.values),
),
)
def _np(x: torch.Tensor) -> np.ndarray:
return x.detach().cpu().numpy()
def interpolate(
v: torch.Tensor,
lat: torch.Tensor,
lon: torch.Tensor,
lat_new: torch.Tensor,
lon_new: torch.Tensor,
) -> torch.Tensor:
"""Interpolate a variable `v` with latitudes `lat` and longitudes `lon` to new latitudes
`lat_new` and new longitudes `lon_new`."""
# Perform the interpolation in double precision.
return torch.from_numpy(
interpolate_numpy(
v.double().numpy(),
lat.double().numpy(),
lon.double().numpy(),
lat_new.double().numpy(),
lon_new.double().numpy(),
)
).float()
def interpolate_numpy(
v: np.ndarray,
lat: np.ndarray,
lon: np.ndarray,
lat_new: np.ndarray,
lon_new: np.ndarray,
) -> np.ndarray:
"""Like :func:`.interpolate`, but for NumPy tensors."""
# Implement periodic longitudes in `lon`.
assert (np.diff(lon) > 0).all()
lon = np.concatenate((lon[-1:] - 360, lon, lon[:1] + 360))
# Merge all batch dimensions into one.
batch_shape = v.shape[:-2]
v = v.reshape(-1, *v.shape[-2:])
# Loop over all batch elements.
vs_regridded = []
for vi in v:
# Implement periodic longitudes in `vi`.
vi = np.concatenate((vi[:, -1:], vi, vi[:, :1]), axis=1)
rgi = RGI(
(lat, lon),
vi,
method="linear",
bounds_error=False, # Allow out of bounds, for the latitudes.
fill_value=None, # Extrapolate latitudes if they are out of bounds.
)
lat_new_grid, lon_new_grid = np.meshgrid(
lat_new,
lon_new,
indexing="ij",
sparse=True,
)
vs_regridded.append(rgi((lat_new_grid, lon_new_grid)))
# Recreate the batch dimensions.
v_regridded = np.stack(vs_regridded, axis=0)
v_regridded = v_regridded.reshape(*batch_shape, lat_new.shape[0], lon_new.shape[0])
return v_regridded
|