Skip to content

Commit a07fe6e

Browse files
api tester and core module
1 parent 23a7d79 commit a07fe6e

File tree

7 files changed

+198
-6
lines changed

7 files changed

+198
-6
lines changed

conf/config.yaml

Whitespace-only changes.
0 Bytes
Binary file not shown.
2.29 KB
Binary file not shown.

era5_sandbox/_modidx.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
'doc_host': 'https://TinasheMTapera.github.io',
66
'git_url': 'https://github.com/TinasheMTapera/era5_sandbox',
77
'lib_path': 'era5_sandbox'},
8-
'syms': {'era5_sandbox.core': {'era5_sandbox.core.describe': ('core.html#describe', 'era5_sandbox/core.py')}}}
8+
'syms': { 'era5_sandbox.core': { 'era5_sandbox.core._expand_path': ('core.html#_expand_path', 'era5_sandbox/core.py'),
9+
'era5_sandbox.core.describe': ('core.html#describe', 'era5_sandbox/core.py'),
10+
'era5_sandbox.core.testAPI': ('core.html#testapi', 'era5_sandbox/core.py')}}}

era5_sandbox/core.py

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,85 @@
1-
"""Here we define the core functions that will be used in ERA5 repo."""
1+
"""This is a core library for the ERA5 dataset pipeline. It defines"""
22

33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../notes/00_core.ipynb.
44

55
# %% auto 0
6-
__all__ = ['describe']
6+
__all__ = ['describe', 'testAPI']
77

88
# %% ../notes/00_core.ipynb 3
9+
import os
10+
import cdsapi
11+
import hydra
12+
13+
from omegaconf import DictConfig, OmegaConf
14+
from pyprojroot import here
15+
16+
# %% ../notes/00_core.ipynb 4
17+
def _expand_path(
18+
path: str # Path on user's machine
19+
)-> str: # Expanded path
20+
"Expand the path on the user's machine for cross compatibility"
21+
22+
# Expand ~ to the user's home directory
23+
path = os.path.expanduser(path)
24+
# Expand environment variables
25+
path = os.path.expandvars(path)
26+
# Convert to absolute path
27+
path = os.path.abspath(path)
28+
return path
29+
30+
# %% ../notes/00_core.ipynb 5
931
def describe():
1032
print("This package fetches ERA5 data.")
33+
34+
# %% ../notes/00_core.ipynb 6
35+
#@hydra.main(version_base=None, config_path=here() / "conf", config_name="config")
36+
def testAPI(
37+
output_path:str=None,
38+
dataset:str="reanalysis-era5-pressure-levels",
39+
remove:bool=False,
40+
cfg: DictConfig = None
41+
)-> bool:
42+
43+
#print(OmegaConf.to_yaml(cfg))
44+
45+
try:
46+
client = cdsapi.Client()
47+
48+
# check the path
49+
if output_path is None:
50+
output_path = here() / "data"
51+
else:
52+
output_path = _expand_path(output_path)
53+
54+
if not os.path.exists(output_path):
55+
os.makedirs(output_path)
56+
57+
# build request
58+
request = {
59+
'product_type': ['reanalysis'],
60+
'variable': ['geopotential'],
61+
'year': ['2024'],
62+
'month': ['03'],
63+
'day': ['01'],
64+
'time': ['13:00'],
65+
'pressure_level': ['1000'],
66+
'data_format': 'grib',
67+
}
68+
69+
target = output_path / 'download.grib'
70+
71+
print("Testing API connection by downloading a dummy dataset to {}...".format(output_path))
72+
73+
client.retrieve(dataset, request, target)
74+
75+
if remove:
76+
os.remove(target)
77+
78+
print("API connection test successful.")
79+
return True
80+
81+
except Exception as e:
82+
print("API connection test failed.")
83+
print("Did you set up your API key with CDS? If not, please visit https://cds.climate.copernicus.eu/api-how-to")
84+
print("Error: {}".format(e))
85+
return False

notes/00_core.ipynb

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"source": [
77
"# core\n",
88
"\n",
9-
"> Here we define the core functions that will be used in ERA5 repo."
9+
"> This is a core library for the ERA5 dataset pipeline. It defines\n",
10+
"a few helpful functions such as an API tester to test your API key and connection."
1011
]
1112
},
1213
{
@@ -28,6 +29,42 @@
2829
"from nbdev.showdoc import *"
2930
]
3031
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"#| export\n",
39+
"import os\n",
40+
"import cdsapi\n",
41+
"import hydra\n",
42+
"\n",
43+
"from omegaconf import DictConfig, OmegaConf\n",
44+
"from pyprojroot import here"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"#| exporti\n",
54+
"def _expand_path(\n",
55+
" path: str # Path on user's machine\n",
56+
" )-> str: # Expanded path\n",
57+
" \"Expand the path on the user's machine for cross compatibility\"\n",
58+
"\n",
59+
" # Expand ~ to the user's home directory\n",
60+
" path = os.path.expanduser(path)\n",
61+
" # Expand environment variables\n",
62+
" path = os.path.expandvars(path)\n",
63+
" # Convert to absolute path\n",
64+
" path = os.path.abspath(path)\n",
65+
" return path"
66+
]
67+
},
3168
{
3269
"cell_type": "code",
3370
"execution_count": null,
@@ -39,6 +76,80 @@
3976
" print(\"This package fetches ERA5 data.\")"
4077
]
4178
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"#| export\n",
86+
"#@hydra.main(version_base=None, config_path=here() / \"conf\", config_name=\"config\")\n",
87+
"def testAPI(\n",
88+
" output_path:str=None,\n",
89+
" dataset:str=\"reanalysis-era5-pressure-levels\",\n",
90+
" remove:bool=False,\n",
91+
" cfg: DictConfig = None\n",
92+
" )-> bool: \n",
93+
" \n",
94+
" #print(OmegaConf.to_yaml(cfg))\n",
95+
"\n",
96+
" try:\n",
97+
" client = cdsapi.Client()\n",
98+
"\n",
99+
" # check the path\n",
100+
" if output_path is None:\n",
101+
" output_path = here() / \"data\"\n",
102+
" else:\n",
103+
" output_path = _expand_path(output_path)\n",
104+
"\n",
105+
" if not os.path.exists(output_path):\n",
106+
" os.makedirs(output_path)\n",
107+
"\n",
108+
" # build request\n",
109+
" request = {\n",
110+
" 'product_type': ['reanalysis'],\n",
111+
" 'variable': ['geopotential'],\n",
112+
" 'year': ['2024'],\n",
113+
" 'month': ['03'],\n",
114+
" 'day': ['01'],\n",
115+
" 'time': ['13:00'],\n",
116+
" 'pressure_level': ['1000'],\n",
117+
" 'data_format': 'grib',\n",
118+
" }\n",
119+
"\n",
120+
" target = output_path / 'download.grib'\n",
121+
" \n",
122+
" print(\"Testing API connection by downloading a dummy dataset to {}...\".format(output_path))\n",
123+
"\n",
124+
" client.retrieve(dataset, request, target)\n",
125+
"\n",
126+
" if remove:\n",
127+
" os.remove(target)\n",
128+
" \n",
129+
" print(\"API connection test successful.\")\n",
130+
" return True\n",
131+
"\n",
132+
" except Exception as e:\n",
133+
" print(\"API connection test failed.\")\n",
134+
" print(\"Did you set up your API key with CDS? If not, please visit https://cds.climate.copernicus.eu/api-how-to\")\n",
135+
" print(\"Error: {}\".format(e))\n",
136+
" return False"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": null,
142+
"metadata": {},
143+
"outputs": [],
144+
"source": [
145+
"#export\n",
146+
"if __name__ == \"__main__\":\n",
147+
" describe()\n",
148+
" testAPI()\n",
149+
"else:\n",
150+
" pass"
151+
]
152+
},
42153
{
43154
"cell_type": "code",
44155
"execution_count": null,
@@ -52,9 +163,13 @@
52163
],
53164
"metadata": {
54165
"kernelspec": {
55-
"display_name": "python3",
166+
"display_name": "era5_sandbox",
56167
"language": "python",
57168
"name": "python3"
169+
},
170+
"language_info": {
171+
"name": "python",
172+
"version": "3.13.2"
58173
}
59174
},
60175
"nbformat": 4,

settings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ status = 3
3838
user = TinasheMTapera
3939

4040
### Optional ###
41-
# requirements = fastcore pandas
41+
requirements = fastcore pyprojroot hydra
4242
# dev_requirements =
4343
# console_scripts =
4444
# conda_user =

0 commit comments

Comments
 (0)