Skip to content

Commit

Permalink
Radar fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 26, 2020
1 parent 84760a5 commit 4fc20e4
Show file tree
Hide file tree
Showing 24 changed files with 144 additions and 130 deletions.
25 changes: 17 additions & 8 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,31 +245,40 @@ Other variants::
response = mosmix.read_mosmix_s_latest()


*******
RADOLAN
*******
*****
RADAR
*****

To use ``DWDRadolanRequest``, you have to provide a time resolution (either hourly or daily)
To use ``DWDRadarRequest``, you have to provide a time resolution (either hourly or daily)
and ``date_times`` (list of datetimes or strings) or a start date and end date. Datetimes
are rounded to HH:50min as the data is packaged for this minute step. Additionally,
you can provide a folder to store/restore RADOLAN data to/from the local filesystem.

This is a short snippet which should give you an idea
how to use ``DWDRadolanRequest`` together with ``wradlib``.
how to use ``DWDRadarRequest`` together with ``wradlib``.
For a more thorough example, please have a look at `example/radolan.py`_.

The subsystem implements access to various radar data like:

- https://opendata.dwd.de/climate_environment/CDC/grids_germany/5_minutes/radolan/
- https://opendata.dwd.de/climate_environment/CDC/grids_germany/daily/radolan/
- https://opendata.dwd.de/climate_environment/CDC/grids_germany/hourly/radolan/
- https://opendata.dwd.de/weather/radar/radolan/
- https://opendata.dwd.de/weather/radar/composit/
- https://opendata.dwd.de/weather/radar/sites/

.. code-block:: python
from wetterdienst import DWDRadolanRequest, TimeResolution
from wetterdienst import DWDRadarRequest, TimeResolution
import wradlib as wrl
radolan = DWDRadolanRequest(
radar = DWDRadarRequest(
TimeResolution.DAILY,
start_date="2020-09-04T12:00:00",
end_date="2020-09-04T12:00:00"
)
for item in radolan.collect_data():
for item in radar.collect_data():
# Decode item.
timestamp, buffer = item
Expand Down
4 changes: 2 additions & 2 deletions example/radolan.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
logging.basicConfig(level=logging.INFO)
log = logging.getLogger()

from wetterdienst import DWDRadolanRequest, TimeResolution
from wetterdienst import DWDRadarRequest, TimeResolution


def plot(data: np.ndarray, attributes: dict, label: str):
Expand Down Expand Up @@ -122,7 +122,7 @@ def radolan_example():
"""

log.info("Acquiring RADOLAN data")
radolan = DWDRadolanRequest(
radolan = DWDRadarRequest(
TimeResolution.DAILY,
start_date="2020-09-04T12:00:00",
end_date="2020-09-04T12:00:00",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path

from wetterdienst.dwd.metadata.time_resolution import TimeResolution
from wetterdienst.dwd.radolan.access import _collect_radolan_data
from wetterdienst.dwd.radar.access import _collect_radolan_data

HERE = Path(__file__).parent

Expand Down
9 changes: 5 additions & 4 deletions tests/dwd/radolan/test_api.py → tests/dwd/radar/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
from io import BytesIO
from pathlib import Path

from wetterdienst import DWDRadolanRequest, TimeResolution
from wetterdienst import DWDRadarRequest, TimeResolution

HERE = Path(__file__).parent


@pytest.mark.remote
def test_dwd_radolan_request():

with pytest.raises(ValueError):
DWDRadolanRequest(
DWDRadarRequest(
time_resolution=TimeResolution.MINUTE_1, date_times=["2019-08-08 00:50:00"]
)

request = DWDRadolanRequest(
request = DWDRadarRequest(
time_resolution=TimeResolution.HOURLY, date_times=["2019-08-08 00:50:00"]
)

assert request == DWDRadolanRequest(
assert request == DWDRadarRequest(
TimeResolution.HOURLY,
[datetime(year=2019, month=8, day=8, hour=0, minute=50, second=0)],
)
Expand Down
26 changes: 26 additions & 0 deletions tests/dwd/radar/test_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from wetterdienst import Parameter, TimeResolution
from wetterdienst.dwd.radar.metadata import RadarDataTypes
from wetterdienst.dwd.radar.sites import RadarSites
from wetterdienst.dwd.radar.index import _create_fileindex_radar


def test_radar_fileindex_sweep():

file_index = _create_fileindex_radar(
parameter=Parameter.SWEEP_VOL_VELOCITY_V,
time_resolution=TimeResolution.MINUTE_5,
radar_site=RadarSites.BOO,
radar_data_type=RadarDataTypes.HDF5
)

assert "sweep_vol_v/boo/hdf5" in file_index.iloc[0]["FILENAME"]


def test_radar_fileindex_reflectivity():

file_index = _create_fileindex_radar(
parameter=Parameter.PX250_REFLECTIVITY,
time_resolution=TimeResolution.MINUTE_5,
radar_site=RadarSites.BOO)

assert "px250/boo" in file_index.iloc[0]["FILENAME"]
15 changes: 15 additions & 0 deletions tests/dwd/radar/test_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from datetime import datetime
from wetterdienst import Parameter, TimeResolution
from wetterdienst.dwd.metadata.constants import DWD_FOLDER_MAIN
from wetterdienst.dwd.radar.store import _build_local_filepath


def test_build_local_filepath_for_radar():

radar_filepath = _build_local_filepath(
Parameter.DX_REFLECTIVITY,
datetime(2020, 1, 1, 12, 15),
DWD_FOLDER_MAIN,
TimeResolution.MINUTE_5)

assert "dx/5_minutes/dx_5_minutes_202001011215" in str(radar_filepath)
29 changes: 0 additions & 29 deletions tests/dwd/radolan/test_index.py

This file was deleted.

16 changes: 0 additions & 16 deletions tests/dwd/radolan/test_store.py

This file was deleted.

8 changes: 3 additions & 5 deletions tests/dwd/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ def test_fileindex():
file_index = _create_file_index_for_dwd_server(
Parameter.CLIMATE_SUMMARY,
TimeResolution.DAILY,
PeriodType.RECENT,
DWDCDCBase.CLIMATE_OBSERVATIONS,
period_type=PeriodType.RECENT)
)

test_split = file_index.iat[0, 0].split('/')
assert test_split[0] == 'daily'
assert test_split[1] == 'kl'
assert test_split[2] == 'recent'
assert "daily/kl/recent" in file_index.iloc[0]["FILENAME"]
3 changes: 3 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def test_cli_about_parameters(capsys):
assert "air_temperature" in response
assert "weather_phenomena" in response
assert "radolan" in response
assert "wx" in response
assert "rx" in response
assert "sweep_vol_z" in response


def test_cli_about_resolutions(capsys):
Expand Down
2 changes: 1 addition & 1 deletion wetterdienst/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
get_nearby_stations_by_number,
get_nearby_stations_by_distance,
)
from wetterdienst.dwd.radolan.api import DWDRadolanRequest
from wetterdienst.dwd.radar.api import DWDRadarRequest

# Single-sourcing the package version
# https://cjolowicz.github.io/posts/hypermodern-python-06-ci-cd/
Expand Down
7 changes: 0 additions & 7 deletions wetterdienst/dwd/metadata/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@

class DWDCDCBase(Enum):
CLIMATE_OBSERVATIONS = "observations_germany/climate/"
GRIDS_GERMANY = "grids_germany/"


class DWDWeatherBase(Enum):
PATH = "weather"
RADAR_COMPOSITE = "radar/composite"
RADAR_SITES = "radar/sites"


DWD_FOLDER_MAIN = "./dwd_data"
Expand Down
8 changes: 0 additions & 8 deletions wetterdienst/dwd/metadata/radar_data_types.py

This file was deleted.

1 change: 1 addition & 0 deletions wetterdienst/dwd/observations/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
DWD_FOLDER_STATION_DATA,
DWD_FILE_STATION_DATA,
DataFormat,
)


def store_climate_observations(
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from wetterdienst.dwd.metadata.constants import DWD_FOLDER_MAIN

from wetterdienst.dwd.network import download_file_from_dwd
from wetterdienst.dwd.radolan.index import create_file_index_for_radolan
from wetterdienst.dwd.radolan.store import restore_radolan_data, store_radolan_data
from wetterdienst.dwd.radar.index import create_file_index_for_radolan
from wetterdienst.dwd.radar.store import restore_radar_data, store_radar_data
from wetterdienst.dwd.metadata.column_names import DWDMetaColumns
from wetterdienst.dwd.metadata.datetime import DatetimeFormat
from wetterdienst.util.cache import payload_cache_twelve_hours
Expand Down Expand Up @@ -85,7 +85,7 @@ def _collect_radolan_data(
data.append(
(
date_time,
restore_radolan_data(Parameter.RADOLAN, date_time, time_resolution, folder),
restore_radar_data(Parameter.RADOLAN, date_time, time_resolution, folder),
)
)

Expand All @@ -100,6 +100,7 @@ def _collect_radolan_data(
remote_radolan_file_path = create_filepath_for_radolan(
date_time, time_resolution
)
print("remote_radolan_file_path:", remote_radolan_file_path)

if remote_radolan_file_path == "":
log.warning(f"RADOLAN not found for {str(date_time)}, will be skipped.")
Expand All @@ -110,7 +111,7 @@ def _collect_radolan_data(
data.append(date_time_and_file)

if write_file:
store_radolan_data(Parameter.RADOLAN, date_time_and_file, time_resolution, folder)
store_radar_data(Parameter.RADOLAN, date_time_and_file, time_resolution, folder)

return data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

import pandas as pd

from wetterdienst import TimeResolution
from wetterdienst import TimeResolution, Parameter
from wetterdienst.dwd.metadata.constants import DWD_FOLDER_MAIN
from wetterdienst.dwd.radolan.access import collect_radar_data
from wetterdienst.dwd.radar.access import collect_radar_data
from wetterdienst.dwd.metadata.column_names import DWDMetaColumns
from wetterdienst.dwd.radolan.index import create_file_index_for_radolan
from wetterdienst.dwd.radar.index import create_file_index_for_radolan
from wetterdienst.dwd.util import parse_enumeration_from_template


class DWDRadolanRequest:
class DWDRadarRequest:
"""
API for DWD RADOLAN data requests.
"""
Expand All @@ -31,15 +31,15 @@ def __init__(
"""
:param time_resolution: Time resolution enumeration, either hourly or daily
:param date_times: List of datetimes for which RADOLAN is requested.
:param date_times: List of datetimes for which radar data is requested.
Minutes have o be defined (HOUR:50), otherwise rounded
to 50 minutes as of its provision.
:param start_date: Alternative to datetimes, giving a start and end date
:param end_date: Alternative to datetimes, giving a start and end date
:param prefer_local: RADOLAN should rather be loaded from disk, for
:param prefer_local: Radar data should rather be loaded from disk, for
processing purposes
:param write_file: File should be stored on drive
:param folder: Folder where to store RADOLAN data
:param folder: Folder where to store radar data
:return: Nothing for now.
"""
Expand Down Expand Up @@ -100,6 +100,7 @@ def collect_data(self) -> Generator[Tuple[datetime, BytesIO], None, None]:
"""
for date_time in self.date_times:
_, file_in_bytes = collect_radar_data(
parameter=Parameter.RADOLAN,
time_resolution=self.time_resolution,
date_times=[date_time],
write_file=self.write_file,
Expand Down
Loading

0 comments on commit 4fc20e4

Please sign in to comment.