Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple improvements on DWD Obs #1396

Merged
merged 3 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Development

### Refactor
- \[DWD Obs\] Make the download function more flexible using threadpool
- \[DWD Obs\] Cleanup parser function

### Fix
- \[DWD Obs\] Reduce unnecessary file index calls during retrieval of data for stations with multiple files

## 0.100.0 - 2025-01-06

### Feature
Expand Down
2 changes: 1 addition & 1 deletion tests/provider/dwd/observation/test_fileindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_create_file_list_for_dwd_server(default_settings):
station_id="00003",
dataset=DwdObservationMetadata.minute_10.temperature_air,
period=Period.HISTORICAL,
date_range="19930428_19991231",
date_ranges=["19930428_19991231"],
settings=default_settings,
).to_list()
assert remote_file_path == [
Expand Down
15 changes: 8 additions & 7 deletions wetterdienst/provider/dwd/observation/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from wetterdienst.metadata.period import Period
from wetterdienst.metadata.resolution import Resolution
from wetterdienst.provider.dwd.observation.download import (
download_climate_observations_data_parallel,
download_climate_observations_data,
)
from wetterdienst.provider.dwd.observation.fileindex import (
_create_file_index_for_dwd_server,
Expand Down Expand Up @@ -74,30 +74,31 @@ def _collect_station_parameter_or_dataset(
date_ranges = self._get_historical_date_ranges(
station_id, parameter_or_dataset, self.sr.stations.settings
)
for date_range in date_ranges:
periods_and_date_ranges.append((period, date_range))
periods_and_date_ranges.append((period, date_ranges))
else:
periods_and_date_ranges.append((period, None))

parameter_data = []

for period, date_range in periods_and_date_ranges:
for period, date_ranges in periods_and_date_ranges:
if period not in parameter_or_dataset.periods:
log.info(f"Skipping period {period} for {parameter_or_dataset.name}.")
continue
dataset_identifier = f"{parameter_or_dataset.resolution.value.name}/{parameter_or_dataset.name}/{station_id}/{period.value}/{date_range}" # noqa: E501
dataset_identifier = (
f"{parameter_or_dataset.resolution.value.name}/{parameter_or_dataset.name}/{station_id}/{period.value}" # noqa: E501
)
log.info(f"Acquiring observation data for {dataset_identifier}.")
remote_files = create_file_list_for_climate_observations(
station_id,
parameter_or_dataset,
period,
self.sr.stations.settings,
date_range,
date_ranges,
)
if remote_files.is_empty():
log.info(f"No files found for {dataset_identifier}. Station will be skipped.")
continue
filenames_and_files = download_climate_observations_data_parallel(remote_files, self.sr.stations.settings)
filenames_and_files = download_climate_observations_data(remote_files, self.sr.stations.settings)
period_df = parse_climate_observations_data(filenames_and_files, parameter_or_dataset, period)
parameter_data.append(period_df)

Expand Down
36 changes: 9 additions & 27 deletions wetterdienst/provider/dwd/observation/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,22 @@
log = logging.getLogger(__name__)


def download_climate_observations_data_parallel(
def download_climate_observations_data(
remote_files: pl.Series,
settings: Settings,
) -> list[tuple[str, BytesIO]]:
"""
Wrapper for ``_download_dwd_data`` to provide a multiprocessing feature.

:param remote_files: List of requested files
:return: List of downloaded files
"""
with ThreadPoolExecutor() as p:
files_in_bytes = p.map(
lambda file: _download_climate_observations_data(remote_file=file, settings=settings),
remote_files,
)

if len(remote_files) > 1:
with ThreadPoolExecutor() as p:
files_in_bytes = p.map(
lambda file: _download_climate_observations_data(remote_file=file, settings=settings),
remote_files,
)
else:
files_in_bytes = [_download_climate_observations_data(remote_file=remote_files[0], settings=settings)]
return list(zip(remote_files, files_in_bytes))


def _download_climate_observations_data(remote_file: str, settings: Settings) -> BytesIO:
"""
This function downloads the station data for which the link is
provided by the 'select_dwd' function. It checks the shortened filepath (just
the zipfile) for its parameters, creates the full filepath and downloads the
file(s) according to the set up folder.

Args:
remote_file: contains path to file that should be downloaded
and the path to the folder to store the files

Returns:
stores data on local file system

"""
return BytesIO(__download_climate_observations_data(remote_file=remote_file, settings=settings))


Expand Down
30 changes: 8 additions & 22 deletions wetterdienst/provider/dwd/observation/fileindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,18 @@ def create_file_list_for_climate_observations(
dataset: DatasetModel,
period: Period,
settings: Settings,
date_range: str | None = None,
date_ranges: list[str] | None = None,
) -> pl.Series:
"""
Function for selecting datafiles (links to archives) for given
station_ids, parameter, time_resolution and period_type under consideration of a
created list of files that are
available online.
Args:
station_id: station id for the weather station to ask for data
dataset: observation measure
resolution: frequency/granularity of measurement interval
period: recent or historical files
date_range:
Returns:
List of path's to file
"""Create a list of files for a given station id, dataset and period.

Date ranges are used to reduce the number of files to be downloaded based on the date range of the files.
This is useful for hourly or more fine-grained data, where the number of files can be very large.
"""
file_index = create_file_index_for_climate_observations(dataset, period, settings)

file_index = file_index.collect()

file_index = file_index.filter(pl.col("station_id").eq(station_id))

if date_range:
file_index = file_index.filter(pl.col("date_range").eq(date_range))

return file_index.get_column("filename")
if date_ranges:
file_index = file_index.filter(pl.col("date_range").is_in(date_ranges))
return file_index.collect().get_column("filename")


def create_file_index_for_climate_observations(
Expand Down
Loading
Loading