Skip to content

Commit

Permalink
make mypy happy and add variables list
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang committed Jul 11, 2023
1 parent cf04f55 commit 2d2825c
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ To download the following datasets, users need access to CDS via cdsapi:
- ERA5 land
- LAI

Before submitting any request with `zampy`, please configure your `.cdsapirc` file following the instructions on https://pypi.org/project/cdsapi/.
Before submitting any request with `zampy`, please configure your `.cdsapirc` file following the instructions on https://cds.climate.copernicus.eu/api-how-to. For the first time, it is **necessary to agree to the Terms of Use of every datasets that you intend to download**. This can only be done via web API.
96 changes: 96 additions & 0 deletions demo/era5_dataset_demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Handle ERA5 dataset with Zampy\n",
"Demo notebook for developers."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from zampy.datasets import ERA5\n",
"from zampy.datasets.dataset_protocol import TimeBounds, SpatialBounds\n",
"from pathlib import Path"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"work_dir = Path(\"/home/yangliu/EcoExtreML/temp\")\n",
"download_dir = work_dir / \"download\"\n",
"ingest_dir = work_dir / \"ingest\"\n",
"times = TimeBounds(np.datetime64(\"2010-01-01\"), np.datetime64(\"2010-01-31\"))\n",
"bbox_demo = SpatialBounds(54, 56, 1, 3)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"File 'era5_10m_v_component_of_wind_2010-1.nc' already exists, skipping...\n",
"File 'era5_surface_solar_radiation_downwards_2010-1.nc' already exists, skipping...\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"era5_dataset = ERA5()\n",
"era5_dataset.download(\n",
" download_dir=download_dir,\n",
" time_bounds=times,\n",
" spatial_bounds=bbox_demo,\n",
" variable_names=[\"10m_v_component_of_wind\",\n",
" \"surface_solar_radiation_downwards\",\n",
" \"mean_total_precipitation_rate\",\n",
" \"surface_pressure\",],\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ecoextreml",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
File renamed without changes.
80 changes: 55 additions & 25 deletions src/zampy/datasets/era5.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,74 @@
"""ERA5 dataset."""

from pathlib import Path
from typing import List
from typing import Union
import numpy as np
import xarray as xr
# from zampy.datasets import converter
from zampy.datasets import utils
# from zampy.datasets import validation
from zampy.datasets import validation
from zampy.datasets.dataset_protocol import Dataset
from zampy.datasets.dataset_protocol import SpatialBounds
from zampy.datasets.dataset_protocol import TimeBounds
# from zampy.datasets.dataset_protocol import Variable
# from zampy.datasets.dataset_protocol import copy_properties_file
from zampy.datasets.dataset_protocol import Variable
from zampy.datasets.dataset_protocol import write_properties_file
from zampy.reference.variables import unit_registry


## Ignore missing class/method docstrings: they are implemented in the Dataset class.
# ruff: noqa: D102


class ERA5(Dataset): # noqa: D101
name = "era5"
time_bounds = TimeBounds(np.datetime64("1940-01-01"), np.datetime64("2023-06-30"))
spatial_bounds = SpatialBounds(90, 180, -90, -180)

raw_variables = (
Variable(name="mtpr", unit=unit_registry.precipitation),
Variable(name="strd", unit=unit_registry.radiation),
Variable(name="ssrd", unit=unit_registry.radiation),
Variable(name="sp", unit=unit_registry.pascal),
Variable(name="u10", unit=unit_registry.velocity),
Variable(name="v10", unit=unit_registry.velocity),
)

variable_names = (
"mean_total_precipitation_rate",
"surface_thermal_radiation_downwards",
"surface_solar_radiation_downwards",
"surface_pressure",
"10m_u_component_of_wind",
"10m_v_component_of_wind",
)

license = "cc-by-4.0"
bib = """
@article{hersbach2020era5,
title={The ERA5 global reanalysis},
author={Hersbach, Hans et al.},
journal={Quarterly Journal of the Royal Meteorological Society},
volume={146},
number={730},
pages={1999--2049},
year={2020},
publisher={Wiley Online Library}
}
"""

def download( # noqa: PLR0913
self,
download_dir: Path,
time_bounds: TimeBounds,
spatial_bounds: SpatialBounds,
variable_names: List[str],
variable_names: list[str],
overwrite: bool = False,
) -> bool:
# validation.validate_download_request(
# self,
# download_dir,
# time_bounds,
# spatial_bounds,
# variable_names,
# )
# call the cds_request function
validation.validate_download_request(
self,
download_dir,
time_bounds,
spatial_bounds,
variable_names,
)

download_folder = download_dir / self.name
download_folder.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -66,16 +96,16 @@ def ingest(
) -> bool:
return True

def load( # noqa: PLR0913
self,
ingest_dir: Path,
time_bounds: TimeBounds,
spatial_bounds: SpatialBounds,
resolution: float,
regrid_method: str,
variable_names: List[str],
):
pass
# def load( # noqa: PLR0913
# self,
# ingest_dir: Path,
# time_bounds: TimeBounds,
# spatial_bounds: SpatialBounds,
# resolution: float,
# regrid_method: str,
# variable_names: List[str],
# ) -> None:
# pass

def convert(
self,
Expand Down
Loading

0 comments on commit 2d2825c

Please sign in to comment.