Skip to content

Commit 7dd185b

Browse files
fixed the implementation of a main function
1 parent e1343d5 commit 7dd185b

File tree

12 files changed

+326
-7
lines changed

12 files changed

+326
-7
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.micromamba
2-
data
2+
logs/*
3+
.snakemake/*
4+
.DS_Store

conf/config.yaml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
db:
2-
driver: mysql
3-
user: omry
4-
password: secret
1+
query:
2+
product_type: reanalysis
3+
variable: ["2m_dewpoint_temperature", "2m_temperature", "skin_temperature"]
4+
year: [2010, 2011]
5+
month: [1, 2, 3]
6+
day: [1, 2, 3, 4, 5]
7+
time: [0, 6, 12, 18]
8+
area: [0, 360, -90, 90]
9+
data_format: netcdf
10+
download_format: unarchived
511

612
hydra:
713
run:

conf/datapaths.yaml

Whitespace-only changes.

data/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*
2+
!*/
3+
!.gitignore
4+
!README.md
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
3.31 KB
Binary file not shown.

era5_sandbox/_modidx.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@
77
'lib_path': 'era5_sandbox'},
88
'syms': { 'era5_sandbox.core': { 'era5_sandbox.core._expand_path': ('core.html#_expand_path', 'era5_sandbox/core.py'),
99
'era5_sandbox.core.describe': ('core.html#describe', 'era5_sandbox/core.py'),
10-
'era5_sandbox.core.testAPI': ('core.html#testapi', 'era5_sandbox/core.py')}}}
10+
'era5_sandbox.core.testAPI': ('core.html#testapi', 'era5_sandbox/core.py')},
11+
'era5_sandbox.download': { 'era5_sandbox.download._validate_query': ( 'download_raw_data.html#_validate_query',
12+
'era5_sandbox/download.py'),
13+
'era5_sandbox.download.download': ('download_raw_data.html#download', 'era5_sandbox/download.py'),
14+
'era5_sandbox.download.fetch_MDG_GADM': ( 'download_raw_data.html#fetch_mdg_gadm',
15+
'era5_sandbox/download.py'),
16+
'era5_sandbox.download.main': ('download_raw_data.html#main', 'era5_sandbox/download.py')}}}

era5_sandbox/download.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""This module downloads the raw data from CDS and saves it in the local directory"""
2+
3+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../notes/01_download_raw_data.ipynb.
4+
5+
# %% auto 0
6+
__all__ = ['fetch_MDG_GADM', 'download', 'main']
7+
8+
# %% ../notes/01_download_raw_data.ipynb 4
9+
import os
10+
import hydra
11+
import cdsapi
12+
from fastcore.script import call_parse # for the console script
13+
14+
from omegaconf import DictConfig, OmegaConf
15+
16+
# %% ../notes/01_download_raw_data.ipynb 5
17+
def _validate_query(
18+
query_body: DictConfig
19+
)->bool:
20+
'''
21+
Check that the query is valid
22+
'''
23+
24+
required_keys = ['product_type', 'variable', 'year', 'month', 'day', 'time', 'area', 'data_format', 'download_format']
25+
if not all([key in query_body.keys() for key in required_keys]):
26+
print(f"Missing required key in query. Required keys are {required_keys}")
27+
print("Query validation failed")
28+
raise ValueError("Invalid query")
29+
30+
else:
31+
return query_body
32+
33+
# %% ../notes/01_download_raw_data.ipynb 6
34+
def fetch_MDG_GADM(
35+
output_file: str="gadm41_MDG.gpkg" # file path to save the GADM data
36+
)-> str:
37+
'''
38+
Fetch the GADM data for Madagascar
39+
https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_MDG.gpkg
40+
'''
41+
42+
output_file_path = os.path.join(os.getcwd(), output_file)
43+
if os.path.exists(output_file_path):
44+
print("GADM data already exists")
45+
return output_file_path
46+
47+
print("Fetching GADM bounding box data for Madagascar")
48+
os.system("curl --output {} https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_MDG.gpkg".format(output_file))
49+
print("GADM data fetched")
50+
51+
return output_file_path
52+
53+
# %% ../notes/01_download_raw_data.ipynb 7
54+
def download(
55+
cfg: DictConfig, # hydra configuration file
56+
output_dir: str = "data/input/", # output directory
57+
dataset: str = "reanalysis-era5-pressure-levels", # dataset to download
58+
testing: bool = True # testing mode
59+
)->None:
60+
'''
61+
Send the query to the API and download the data
62+
'''
63+
64+
client = cdsapi.Client()
65+
66+
query = _validate_query(cfg.query)
67+
68+
# Send the query to the client
69+
if not testing:
70+
client.retrieve(dataset, query).download(os.path.join(output_dir, "{}_{}.nc".format(query.year, query.month)))
71+
else:
72+
print(f"Testing mode. Not downloading data. Query is {query}")
73+
74+
print("Done")
75+
76+
# %% ../notes/01_download_raw_data.ipynb 8
77+
@hydra.main(config_path="../conf", config_name="config", version_base=None)
78+
def main(cfg: DictConfig) -> None:
79+
download(cfg=cfg)
80+
81+
# %% ../notes/01_download_raw_data.ipynb 9
82+
try: from nbdev.imports import IN_NOTEBOOK
83+
except: IN_NOTEBOOK=False
84+
85+
if __name__ == "__main__" and not IN_NOTEBOOK:
86+
print('Running from __main__ ...')
87+
88+
main()

notes/00_core.ipynb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,32 @@
186186
"describe(cfg)"
187187
]
188188
},
189+
{
190+
"cell_type": "markdown",
191+
"metadata": {},
192+
"source": [
193+
"### Importing the Main Function\n",
194+
"\n",
195+
"Important: using main in nbdev is a little bit tricky. We need to define the main function in the module and then when we export the notebook to script, we need to add the `nbdev.imports.IN_NOTEBOOK` variable. This way, the main function will only be executed when we run the notebook and not when we import the module.\n",
196+
"\n",
197+
"```python\n",
198+
"from nbdev.imports import IN_NOTEBOOK\n",
199+
"```\n",
200+
"\n",
201+
"You'll see this listed throughout the notebooks."
202+
]
203+
},
189204
{
190205
"cell_type": "code",
191206
"execution_count": null,
192207
"metadata": {},
193208
"outputs": [],
194209
"source": [
195210
"#export\n",
196-
"if __name__ == \"__main__\":\n",
211+
"try: from nbdev.imports import IN_NOTEBOOK\n",
212+
"except: IN_NOTEBOOK=False\n",
213+
"\n",
214+
"if __name__ == \"__main__\" and not IN_NOTEBOOK:\n",
197215
" # for testing\n",
198216
" describe()"
199217
]

0 commit comments

Comments
 (0)