-
-
Notifications
You must be signed in to change notification settings - Fork 77
Issue Add support for Anemoi Datasets #102 #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FilippoContessa
wants to merge
6
commits into
openclimatefix:main
Choose a base branch
from
FilippoContessa:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ab91b59
Issue Add support for Anemoi Datasets #102
FilippoContessa f0165fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2216fa8
Update graph_weather/data/anemoi_dataloader.py
FilippoContessa 6280365
Update anemoi_dataloader.py
FilippoContessa 92e2684
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4b4c1e1
Merge branch 'openclimatefix:main' into main
FilippoContessa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import numpy as np | ||
import xarray as xr | ||
from torch.utils.data import Dataset | ||
from torchvision.transforms import ToTensor | ||
|
||
|
||
class AnemoiDataset(Dataset): | ||
""" | ||
Dataset for Anemoi weather datasets. | ||
|
||
Args: | ||
filepath: Path to the dataset in Zarr format. | ||
features: List of features to extract (e.g., "temperature", "geopotential"). | ||
start_year: Initial year to filter the data. Defaults to 2016. | ||
end_year: Ending year to filter the data. Defaults to 2022. | ||
resolution: Resolution of the dataset (e.g., "5.625deg"). | ||
land_sea_mask: Path to the land-sea mask dataset. | ||
orography: Path to the orography dataset. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
filepath: str, | ||
features: list, | ||
start_year: int = 2016, | ||
end_year: int = 2022, | ||
resolution: str = "5.625deg", | ||
land_sea_mask: str = None, | ||
orography: str = None, | ||
): | ||
super().__init__() | ||
|
||
# Check time range validity | ||
assert ( | ||
start_year <= end_year | ||
), f"start_year ({start_year}) cannot be greater than end_year ({end_year})." | ||
|
||
# Load the main dataset | ||
self.data = xr.open_zarr(filepath) | ||
self.data = self.data.sel(time=slice(str(start_year), str(end_year))) | ||
|
||
# Load additional datasets | ||
if land_sea_mask: | ||
self.land_sea_mask = xr.open_zarr(land_sea_mask) | ||
if orography: | ||
self.orography = xr.open_zarr(orography) | ||
|
||
self.features = features | ||
self.resolution = resolution | ||
|
||
# Precompute geographical features | ||
self._compute_geographical_features() | ||
|
||
def __len__(self): | ||
return len(self.data["time"]) | ||
|
||
def __getitem__(self, idx): | ||
start = self.data.isel(time=idx) | ||
|
||
# Extract features | ||
input_data = self._extract_features(start) | ||
|
||
# Add precomputed geographical features | ||
input_data = np.concatenate((input_data, self.geo_features), axis=-1) | ||
|
||
# Scale data between 0 and 1 | ||
input_data = np.clip(input_data, 0, 1) | ||
|
||
# Return tensor | ||
return ToTensor()(input_data) | ||
|
||
def _extract_features(self, data): | ||
""" | ||
Extract the specified features and stack them into a single array. | ||
""" | ||
feature_data = np.stack( | ||
[data[feature].values for feature in self.features], axis=-1 | ||
).astype(np.float32) | ||
|
||
return feature_data | ||
|
||
def _compute_geographical_features(self): | ||
""" | ||
Compute geographical features: sin(lat), cos(lat), sin(lon), cos(lon), day-of-year. | ||
""" | ||
lats = self.data["latitude"].values | ||
lons = self.data["longitude"].values | ||
|
||
sin_lat = np.sin(np.radians(lats)) | ||
cos_lat = np.cos(np.radians(lats)) | ||
sin_lon = np.sin(np.radians(lons)) | ||
cos_lon = np.cos(np.radians(lons)) | ||
|
||
# Add day-of-year feature | ||
times = self.data["time"].values | ||
days_of_year = (xr.DataArray(times).dt.dayofyear / 365.0).values | ||
FilippoContessa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
self.geo_features = np.stack( | ||
[sin_lat, cos_lat, sin_lon, cos_lon, days_of_year], axis=-1 | ||
).astype(np.float32) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.