Coverage for mlair/helpers/data_sources/toar_data.py: 63%
25 statements
« prev ^ index » next coverage.py v6.4.2, created at 2023-06-01 13:03 +0000
« prev ^ index » next coverage.py v6.4.2, created at 2023-06-01 13:03 +0000
1__author__ = "Lukas Leufen"
2__date__ = "2022-07-05"
4from . import join, toar_data_v2
6import requests
7import pandas as pd
8from .data_loader import EmptyQueryResult
11def download_toar(station, toar_stats, sampling, data_origin):
12 try:
13 # load data from toar-data (v2)
14 df_toar, meta_toar = toar_data_v2.download_toar(station, toar_stats, sampling=sampling, data_origin=data_origin)
15 except (AttributeError, EmptyQueryResult, KeyError, requests.ConnectionError, ValueError, IndexError):
16 df_toar, meta_toar = None, None
18 try:
19 # load join data (toar-data v1)
20 df_join, meta_join = join.download_join(station_name=station, stat_var=toar_stats, sampling=sampling,
21 data_origin=data_origin)
22 except (AttributeError, EmptyQueryResult, KeyError, requests.ConnectionError, ValueError, IndexError):
23 df_join, meta_join = None, None
25 # merge both data sources with priority on toar-data v2
26 if df_toar is not None and df_join is not None: 26 ↛ 27line 26 didn't jump to line 27, because the condition on line 26 was never true
27 df_merged = merge_toar_join(df_toar, df_join, sampling)
28 meta_merged = meta_toar
29 else:
30 df_merged = df_toar if df_toar is not None else df_join
31 meta_merged = meta_toar if df_toar is not None else meta_join
32 return df_merged, meta_merged
35def merge_toar_join(df_toar, df_join, sampling):
36 start_date = min([df_toar.index.min(), df_join.index.min()])
37 end_date = max([df_toar.index.max(), df_join.index.max()])
38 freq = {"hourly": "1H", "daily": "1d"}.get(sampling)
39 full_time = pd.date_range(start_date, end_date, freq=freq)
40 full_data = df_toar.reindex(full_time)
41 full_data.update(df_join, overwrite=False)
42 return full_data