Skip to content

Commit fb9a0f6

Browse files
committed
Add product argument and refactor network product structure
1 parent 505bd03 commit fb9a0f6

25 files changed

Lines changed: 291 additions & 139 deletions

CODE_OF_CONDUCT.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ representative at an online or offline event.
5858

5959
Instances of abusive, harassing, or otherwise unacceptable behavior may be
6060
reported to the community leaders responsible for enforcement at
61-
\[INSERT CONTACT METHOD\].
61+
[INSERT CONTACT METHOD].
6262
All complaints will be reviewed and investigated promptly and fairly.
6363

6464
All community leaders are obligated to respect the privacy and security of the

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
| Code Quality | [![Codefactor](https://www.codefactor.io/repository/github/ghiggi/radar_api/badge?style=flat)](https://www.codefactor.io/repository/github/ghiggi/radar_api) [![Codebeat](https://codebeat.co/badges/57498d71-f042-473f-bb8e-9b45e50572d8?style=flat)](https://codebeat.co/projects/github-com-ghiggi-radar_api-main) [![Codacy](https://app.codacy.com/project/badge/Grade/bee842cb10004ad8bb9288256f2fc8af?style=flat)](https://app.codacy.com/gh/ghiggi/radar_api/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade) [![Codescene](https://codescene.io/projects/63299/status-badges/average-code-health?style=flat)](https://codescene.io/projects/63299) |
1818
| License | [![License](https://img.shields.io/github/license/ghiggi/radar_api?style=flat)](https://github.com/ghiggi/radar_api/blob/main/LICENSE) |
1919
| Community | [![Discourse](https://img.shields.io/badge/Slack-radar_api-green.svg?logo=slack&style=flat)](https://openradar.discourse.group/) [![GitHub Discussions](https://img.shields.io/badge/GitHub-Discussions-green?logo=github&style=flat)](https://github.com/ghiggi/radar_api/discussions) |
20-
| Citation | [![DOI](https://zenodo.org/badge/922589509.svg?style=flat)](https://doi.org/10.5281/zenodo.14743651) </div> |
20+
| Citation | [![DOI](https://zenodo.org/badge/922589509.svg?style=flat)](https://doi.org/10.5281/zenodo.14743651) </div> |
2121

2222
[**Documentation: https://radar-api.readthedocs.io**](https://radar-api.readthedocs.io/)
2323

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,8 @@ ignore = [
186186
"B006", # {} defaults in function arguments
187187
"PT011", # pytest raised error must be checked if match the expected error msg
188188
"PERF203",
189-
"UP038",
189+
"PLC0415", # import not at the top-level file
190190
# "PD011", # suggest values --> to_numpy
191-
"PD901",
192191
"PD013", # suggest melt instead of stack
193192
"PLW2901",
194193
"PLW0603",

radar_api/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from radar_api.info import group_filepaths
4040
from radar_api.io import (
4141
available_networks,
42+
available_products,
4243
available_radars,
4344
)
4445
from radar_api.readers import (
@@ -52,17 +53,18 @@
5253

5354

5455
__all__ = [
55-
"available_radars",
5656
"available_networks",
57+
"available_products",
58+
"available_radars",
5759
"config",
5860
"define_configs",
59-
"read_configs",
61+
"download_files",
6062
"find_files",
6163
"group_filepaths",
62-
"open_datatree",
6364
"open_dataset",
65+
"open_datatree",
6466
"open_pyart",
65-
"download_files",
67+
"read_configs",
6668
]
6769

6870
# Get version

radar_api/checks.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import datetime
2828
import os
2929
import pathlib
30-
import sys
3130

3231
import numpy as np
3332

@@ -37,9 +36,7 @@
3736

3837
def get_current_utc_time():
3938
"""Return current UTC time."""
40-
if sys.version_info >= (3, 11):
41-
return datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
42-
return datetime.datetime.utcnow()
39+
return datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
4340

4441

4542
def check_protocol(protocol):
@@ -104,6 +101,23 @@ def check_network(network):
104101
return network
105102

106103

104+
def check_product(network, product=None):
105+
"""Check product validity.
106+
107+
If only one product available for that network, return that.
108+
"""
109+
from radar_api.io import available_products
110+
111+
valid_products = available_products(network, only_public=False)
112+
if product is None:
113+
if len(valid_products) == 1:
114+
return valid_products[0]
115+
raise ValueError(f"{network} has {valid_products} products available. 'product' must be specified.")
116+
if product not in valid_products:
117+
raise ValueError(f"Invalid product {product} for {network}. Available products: {valid_products}")
118+
return product
119+
120+
107121
def check_time(time):
108122
"""Check time validity.
109123

radar_api/configs.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"""RADAR-API configurations settings."""
2828
import os
2929

30+
import radar_api
3031
from radar_api.utils.yaml import read_yaml, write_yaml
3132

3233

@@ -75,6 +76,11 @@ def define_configs(
7576

7677
print(f"The RADAR-API config file has been {action_msg} successfully!")
7778

79+
# Now read the config file and set it as the active configuration
80+
# - This avoid the need to restart a python session to take effect !
81+
config_dict = read_configs()
82+
radar_api.config.update(config_dict)
83+
7884

7985
def read_configs() -> dict[str, str]:
8086
"""Reads the RADAR-API configuration file and returns a dictionary with the configuration settings.
@@ -110,8 +116,6 @@ def read_configs() -> dict[str, str]:
110116
####--------------------------------------------------------------------------.
111117
def _get_config_key(key):
112118
"""Return the config key."""
113-
import radar_api
114-
115119
value = radar_api.config.get(key, None)
116120
if value is None:
117121
raise ValueError(f"The '{key}' is not specified in the RADAR-API configuration file.")
@@ -120,8 +124,6 @@ def _get_config_key(key):
120124

121125
def get_base_dir(base_dir=None):
122126
"""Return the RADAR-API base directory."""
123-
import radar_api
124-
125127
if base_dir is None:
126128
base_dir = radar_api.config.get("base_dir")
127129
if base_dir is None:

radar_api/download.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
check_base_dir,
4242
check_download_protocol,
4343
check_network,
44+
check_product,
4445
check_radar,
4546
check_start_end_time,
4647
)
@@ -115,13 +116,13 @@ def _select_missing_fpaths(local_fpaths, bucket_fpaths):
115116
return local_fpaths, bucket_fpaths
116117

117118

118-
def define_local_filepath(filename, network, radar, base_dir=None):
119+
def define_local_filepath(filename, network, product, radar, base_dir=None):
119120
"""Define filepath where to save file locally on disk."""
120121
base_dir = get_base_dir(base_dir)
121122
base_dir = check_base_dir(base_dir)
122123
# Get directory pattern
123-
directory_pattern = get_directory_pattern(protocol="local", network=network)
124-
info_dict = get_info_from_filepath(filename, network=network)
124+
directory_pattern = get_directory_pattern(protocol="local", network=network, product=product)
125+
info_dict = get_info_from_filepath(filename, network=network, product=product)
125126
time = info_dict["start_time"]
126127
# Define local directory path
127128
parser = Parser(directory_pattern)
@@ -133,10 +134,16 @@ def define_local_filepath(filename, network, radar, base_dir=None):
133134
return filepath
134135

135136

136-
def _get_local_from_bucket_fpaths(base_dir, network, radar, bucket_fpaths):
137+
def _get_local_from_bucket_fpaths(base_dir, network, product, radar, bucket_fpaths):
137138
"""Convert cloud bucket filepaths to local storage filepaths."""
138139
fpaths = [
139-
define_local_filepath(filename=os.path.basename(fpath), network=network, radar=radar, base_dir=base_dir)
140+
define_local_filepath(
141+
filename=os.path.basename(fpath),
142+
network=network,
143+
product=product,
144+
radar=radar,
145+
base_dir=base_dir,
146+
)
140147
for fpath in bucket_fpaths
141148
]
142149
return fpaths
@@ -236,6 +243,7 @@ def download_files(
236243
radar,
237244
start_time,
238245
end_time,
246+
product=None,
239247
n_threads=20,
240248
force_download=False,
241249
check_data_integrity=True,
@@ -256,6 +264,11 @@ def download_files(
256264
network : str
257265
The name of the radar network.
258266
See `radar_api.available_network()` for available radar networks.
267+
product: str
268+
The product acronym. The default is None.
269+
It must be specified if for a given network, multiple products are available
270+
through radar_api.
271+
See `radar_api.available_products(network)` for available products.
259272
start_time : datetime.datetime
260273
The start (inclusive) time of the interval period for retrieving the filepaths.
261274
end_time : datetime.datetime
@@ -294,6 +307,7 @@ def download_files(
294307
base_dir = check_base_dir(base_dir)
295308
network = check_network(network)
296309
radar = check_radar(radar=radar, network=network)
310+
product = check_product(network=network, product=product)
297311
start_time, end_time = check_start_end_time(start_time, end_time)
298312

299313
# Initialize timing
@@ -323,6 +337,7 @@ def download_files(
323337
fs_args=fs_args,
324338
radar=radar,
325339
network=network,
340+
product=product,
326341
start_time=start_time,
327342
end_time=end_time,
328343
base_dir=None,
@@ -339,6 +354,7 @@ def download_files(
339354
base_dir=base_dir,
340355
network=network,
341356
radar=radar,
357+
product=product,
342358
bucket_fpaths=bucket_fpaths,
343359
)
344360

0 commit comments

Comments
 (0)