Commit cb55e3aa authored by Niklas Selke's avatar Niklas Selke
Browse files

The 'check_metadata' function now raises a 'ValueError' if there is any...

The 'check_metadata' function now raises a 'ValueError' if there is any problem with the given 'metadata' argument.
parent 63e0a294
......@@ -164,7 +164,8 @@ def mark_required_arguments(sampling_method, stats_list, seasons_given):
def check_metadata(metadata_in, station_lat_in, station_lon_in,
station_climatic_zone_in):
station_climatic_zone_in, station_lat_req, station_lon_req,
station_climatic_zone_req):
"""Check the given metadata argument.
:param metadata_in: the given metadata argument
......@@ -172,11 +173,21 @@ def check_metadata(metadata_in, station_lat_in, station_lon_in,
:param station_lon_in: the given station longitude argument
:param station_climatic_zone_in: the given station climatic zone
argument
:return: The processed metadata argument or a namedtuple with None
values if there was a problem while processing the given
metadata argument
:param station_lat_req: boolean denoting whether the station
latitude is required
:param station_lon_req: boolean denoting whether the station
longitude is required
:param station_climatic_zone_req: boolean denoting whether the
station climatic zone is required
:raises ValueError: raised if any required argument is missing or
has a wrong value
:return: A namedtuple with the metadata information or None if no
metadata is needed
"""
if not any((station_lat_req, station_lon_req, station_climatic_zone_req)):
return None
metadata_dict = {"station_lat": station_lat_in,
"station_lon": station_lon_in,
"station_climatic_zone": station_climatic_zone_in}
......@@ -189,25 +200,45 @@ def check_metadata(metadata_in, station_lat_in, station_lon_in,
for key, val in metadata_dict.items()}
else:
metadata_dict.update(zip(metadata_dict, metadata_in))
if (not isinstance(metadata_dict["station_lat"], (float, int))
or not -90 <= metadata_dict["station_lat"] <= 90):
metadata_dict["station_lat"] = None
if (not isinstance(metadata_dict["station_lon"], (float, int))
or not -180 <= metadata_dict["station_lon"] <= 180):
metadata_dict["station_lon"] = None
if isinstance(metadata_dict["station_climatic_zone"], int):
metadata_dict["station_climatic_zone"] = CLIMATIC_ZONE.get(
metadata_dict["station_climatic_zone"]
)
elif isinstance(metadata_dict["station_climatic_zone"], str):
metadata_dict["station_climatic_zone"] = (
metadata_dict["station_climatic_zone"].strip()
)
if (metadata_dict["station_climatic_zone"]
not in CLIMATIC_ZONE.values()):
metadata_dict["station_climatic_zone"] = None
if station_lat_req:
if (not isinstance(metadata_dict["station_lat"], (float, int))
or not -90 <= metadata_dict["station_lat"] <= 90):
raise ValueError("Station latitude is required and must be between"
" -90 and 90")
else:
metadata_dict.pop("station_lat")
if station_lon_req:
if (not isinstance(metadata_dict["station_lon"], (float, int))
or not -180 <= metadata_dict["station_lon"] <= 180):
raise ValueError("Station longitude is required and must be"
" between -180 and 180")
else:
metadata_dict.pop("station_lon")
if station_climatic_zone_req:
if isinstance(metadata_dict["station_climatic_zone"], int):
try:
metadata_dict["station_climatic_zone"] = CLIMATIC_ZONE[
metadata_dict["station_climatic_zone"]
]
except KeyError:
raise ValueError("Station climatic zone is required and must"
" either be an integer or a string contained"
f" in {CLIMATIC_ZONE}")
elif isinstance(metadata_dict["station_climatic_zone"], str):
metadata_dict["station_climatic_zone"] = (
metadata_dict["station_climatic_zone"].strip()
)
if (metadata_dict["station_climatic_zone"]
not in CLIMATIC_ZONE.values()):
raise ValueError("Station climatic zone is required and must"
" either be an integer or a string contained"
f" in {CLIMATIC_ZONE}")
else:
raise ValueError("Station climatic zone is required and must"
" either be an integer or a string contained in"
f" {CLIMATIC_ZONE}")
else:
metadata_dict["station_climatic_zone"] = None
metadata_dict.pop("station_climatic_zone")
return namedtuple("Metadata", metadata_dict)(**metadata_dict)
......
......@@ -106,8 +106,10 @@ def calculate_statistics(
(station_lat_req, station_lon_req, station_climatic_zone_req, seasons_req,
crops_req) = mark_required_arguments(sampling_method, stats_list,
seasons is not None)
metadata_tuple = check_metadata(metadata, station_lat, station_lon,
station_climatic_zone)
metadata_tuple = check_metadata(
metadata, station_lat, station_lon, station_climatic_zone,
station_lat_req, station_lon_req, station_climatic_zone_req
)
seasons_list = check_seasons(seasons)
crops_list = check_crops(crops)
data_capture_value = check_data_capture(data_capture)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment