Skip to content

Commit

Permalink
Fix util.py
Browse files Browse the repository at this point in the history
  • Loading branch information
a-lucic committed Aug 23, 2024
1 parent f30daf1 commit 4e6ceb0
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions aurora/util.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from pathlib import Path
import os
import requests
import time
from pathlib import Path

import requests


def try_download(url: Path, outpath: Path):
def try_download(url: str, outpath: Path):
"""
Download a file from a URL to a specified path. If the file already exists and is not empty, skip the download.
Args:
Args:
url (Path): URL to download the file from.
outpath (Path): Path to save the downloaded file to.
"""
Expand All @@ -33,34 +35,42 @@ def try_download(url: Path, outpath: Path):
print(f"Failed to download {url}. Error: {e}")


def download_hres_rda_surf(save_dir: Path, year: str, month: str, day: str, variable: str, var_dict: dict[str, str]):
def download_hres_rda_surf(
save_dir: Path, year: str, month: str, day: str, variable: str, var_dict: dict[str, str]
):
"""
Download IFS HRES 0.1 deg surface data from the RDA website: https://rda.ucar.edu/datasets/d113001/#
Download IFS HRES 0.1 deg surface data from the RDA website: https://rda.ucar.edu/datasets/d113001/#
Args:
save_dir (Path): Path to save the downloaded files to.
year (str): Year to download data for.
month (str): Month to download data for.
day (str): Day to download data for.
variable (str): Variable to download data for.
var_dict (dict[str, str]): Dictionary mapping variable names to their corresponding RDA variable numbers. See `docs/example_0.1deg.ipynb` for an example.
variable (str): Variable to download data for
var_dict (dict[str, str]): Dictionary mapping variable names to their corresponding RDA variable numbers. See `docs/example_0.1deg.ipynb` for an example.
"""
v_num = var_dict[variable]
print(f"Downloading {variable} for {year}-{month}-{day}...")


filename = f"ec.oper.an.sfc/{year}{month}/ec.oper.an.sfc.128_{v_num}_{variable}.regn1280sc.{year}{month}{day}.grb"

outpath = os.path.join(save_dir, f"{variable}_{year}_{month}_{day}.grb")
print(outpath)
outpath = save_dir / f"{variable}_{year}_{month}_{day}.grb"
url = "https://data.rda.ucar.edu/ds113.1/" + filename

start_time = time.time()
try_download(url, outpath)
print(f"Downloaded {filename} to {outpath} in {time.time() - start_time:.2f} seconds.")


def download_hres_rda_atmos(save_dir: Path, year: str, month: str, day: str, variable: str, var_dict: dict[str, str], timeofday: str):
def download_hres_rda_atmos(
save_dir: Path,
year: str,
month: str,
day: str,
variable: str,
var_dict: dict[str, str],
timeofday: str,
):
"""
Download IFS HRES 0.1 deg atmospheric data from the RDA website: https://rda.ucar.edu/datasets/d113001/#
Expand All @@ -70,7 +80,7 @@ def download_hres_rda_atmos(save_dir: Path, year: str, month: str, day: str, var
month (str): Month to download data for.
day (str): Day to download data for.
variable (str): Variable to download data for.
var_dict (dict[str, str]): Dictionary mapping variable names to their corresponding RDA variable numbers. See `docs/example_0.1deg.ipynb` for an example.
var_dict (dict[str, str]): Dictionary mapping variable names to their corresponding RDA variable numbers. See `docs/example_0.1deg.ipynb` for an example.
timeofday (str): Time of day to download data for. Format: "00", "06", "12", or "18".
"""
v_num = var_dict[variable]
Expand All @@ -82,8 +92,8 @@ def download_hres_rda_atmos(save_dir: Path, year: str, month: str, day: str, var
filename = f"ec.oper.an.pl/{year}{month}/ec.oper.an.pl.128_{v_num}_{variable}.regn1280uv.{year}{month}{day}{timeofday}.grb"

url = "https://data.rda.ucar.edu/ds113.1/" + filename
outpath = os.path.join(save_dir, f"{variable}_{year}_{month}_{day}_{timeofday}.grb")
outpath = save_dir / f"{variable}_{year}_{month}_{day}_{timeofday}.grb"

start_time = time.time()
try_download(url, outpath)
print(f"Downloaded {filename} to {outpath} in {time.time() - start_time:.2f} seconds.")
print(f"Downloaded {filename} to {outpath} in {time.time() - start_time:.2f} seconds.")

0 comments on commit 4e6ceb0

Please sign in to comment.