Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
akarich73 committed Oct 15, 2024
1 parent 5f41928 commit 0f56603
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion barra2_dl/downloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
import calendar
from .helpers import list_months
from .globals import LatLonPoint, LatLonBBox, barra2_index
from .globals import LatLonPoint, LatLonBBox, barra2_aus11_index


def download_file(url: str,
Expand Down
2 changes: 1 addition & 1 deletion barra2_dl/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LatLonBBox(TypedDict):
"{var}_AUS-11_ERA5_historical_hres_BOM_BARRA-R2_v1_1hr_{year}{month:02d}-{year}{month:02d}.nc")

# index for barra2 used to join separate files
barra2_aus11__index = ['time', 'station', 'latitude[unit="degrees_north"]', 'longitude[unit="degrees_east"]']
barra2_aus11_index = ['time', 'station', 'latitude[unit="degrees_north"]', 'longitude[unit="degrees_east"]']

# BARRA2 wind speed variable pairs
barra2_aus11_wind_all = [('ua10m', 'va10m', '10m[unit="m s-1"]'),
Expand Down
38 changes: 30 additions & 8 deletions barra2_dl/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,19 @@ def process_csvs:


# todo add tests
def calculate_wind_speed(ua: Union[float, int, List[float], List[int]], va: Union[float, int, List[float], List[int]]) -> Union[float, List[float]]:
def calculate_wind_speed(u: Union[float, int], v: Union[float, int]) -> float:
"""
Args:
u: The u component of the wind vector, which can be a float or an int.
v: The v component of the wind vector, which can be a float or an int.
Returns:
Wind speed. If both u and v are zero, it returns 0.0.
"""
if u == 0 and v == 0:
return 0.0
return np.sqrt(u ** 2 + v ** 2)

def wind_components_to_speed(ua: Union[float, int, List[float], List[int]], va: Union[float, int, List[float], List[int]]) -> Union[float, List[float]]:
"""
Convert wind components ua and va to wind speed v.
Args:
Expand All @@ -252,19 +264,31 @@ def calculate_wind_speed(ua: Union[float, int, List[float], List[int]], va: Unio
if isinstance(ua, (float, int)) and isinstance(va, (float, int)):
if ua == 0 and va == 0:
return 0.0
return np.sqrt(ua ** 2 + va ** 2)
return calculate_wind_speed(ua, va)
elif isinstance(ua, list) and isinstance(va, list):
if not all(isinstance(num, (float, int)) for num in ua + va):
raise ValueError("All elements in both lists must be either float or int.")
if len(ua) != len(va):
raise ValueError("Both lists must be of the same length.")
return [0.0 if u == 0 and v == 0 else np.sqrt(u ** 2 + v ** 2) for u, v in zip(ua, va)]
return [calculate_wind_speed(u, v) for u, v in zip(ua, va)]
else:
raise ValueError("Both arguments must be either both float/int or both lists of float/int.")


# todo add tests
def calculate_wind_direction(ua: Union[float, int, List[float], List[int]], va: Union[float, int, List[float], List[int]]) -> Union[float, List[float]]:
def calculate_wind_direction(u: Union[float, int], v: Union[float, int]) -> float:
"""
Args:
u: The u component of the wind vector, which can be a float or an int.
v: The v component of the wind vector, which can be a float or an int.
Returns:
Wind direction in degrees. If both u and v are zero, it returns 0.0.
"""
if u == 0 and v == 0:
return 0.0
return np.mod(180 + np.rad2deg(np.arctan2(u, v)), 360)

def wind_components_to_direction(ua: Union[float, int, List[float], List[int]], va: Union[float, int, List[float], List[int]]) -> Union[float, List[float]]:
"""
Convert wind components ua and va to wind direction phi.
Args:
Expand All @@ -277,15 +301,13 @@ def calculate_wind_direction(ua: Union[float, int, List[float], List[int]], va:
"""

if isinstance(ua, (float, int)) and isinstance(va, (float, int)):
if ua == 0 and va == 0:
return 0.0
return np.mod(180 + np.rad2deg(np.arctan2(ua, va)), 360)
return calculate_wind_direction(ua, va)
elif isinstance(ua, list) and isinstance(va, list):
if not all(isinstance(num, (float, int)) for num in ua + va):
raise ValueError("All elements in both lists must be either float or int.")
if len(ua) != len(va):
raise ValueError("Both lists must be of the same length.")
return [0.0 if u == 0 and v == 0 else np.mod(180 + np.rad2deg(np.arctan2(u, v)), 360) for u, v in zip(ua, va)]
return [calculate_wind_direction(u, v) for u, v in zip(ua, va)]
else:
raise ValueError("Both arguments must be either both float/int or both lists of float/int.")

4 changes: 2 additions & 2 deletions scripts/barra2_downloader_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import barra2_dl
from datetime import datetime
from barra2_dl.globals import barra2_aus11_csv_url, barra2_var_wind_all, barra2_var_wind_50m, barra2_index
from barra2_dl.globals import barra2_aus11_csv_url, barra2_aus11_wind_all, barra2_var_wind_50m, barra2_aus11_index

# relative directory for caching downloaded files
# set directory for caching downloaded files
cache_dir = r'scripts\cache'
output_dir = r'scripts\output'

Expand Down

0 comments on commit 0f56603

Please sign in to comment.