From 4e6ceb0447af8559b50aeb8c73245b1bb1c78ff5 Mon Sep 17 00:00:00 2001 From: Ana Lucic Date: Fri, 23 Aug 2024 12:04:39 +0200 Subject: [PATCH] Fix util.py --- aurora/util.py | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/aurora/util.py b/aurora/util.py index b8d3042..2b30e40 100644 --- a/aurora/util.py +++ b/aurora/util.py @@ -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. """ @@ -33,26 +35,26 @@ 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() @@ -60,7 +62,15 @@ def download_hres_rda_surf(save_dir: Path, year: str, month: str, day: str, vari 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/# @@ -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] @@ -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.") \ No newline at end of file + print(f"Downloaded {filename} to {outpath} in {time.time() - start_time:.2f} seconds.")