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

Added a check for the 'metadata' argument in 'interface.py'. The check...

Added a check for the 'metadata' argument in 'interface.py'. The check function is included in 'input_checks.py'.
parent c9ad31c4
......@@ -4,14 +4,17 @@ This module contains the following functions:
check_sampling - check the given sampling argument
check_statistics - check the given statistics argument
check_data - check the given data argument
check_metadata - check the given metadata argument
"""
from collections import namedtuple
import numpy as np
import pandas as pd
from toarstats import ozone_metrics
from toarstats import stats
from toarstats.stats_utils import RSTAGS
from toarstats.stats_utils import CLIMATIC_ZONE, RSTAGS
def check_sampling(sampling_in):
......@@ -101,3 +104,50 @@ def check_data(data_in, datetimes_in, values_in):
if index_out.tz:
index_out = index_out.tz_localize(None)
return pd.DataFrame({"values": values_out}, index=index_out)
def check_metadata(metadata_in, station_lat_in, station_lon_in,
station_climatic_zone_in):
"""Check the given metadata argument.
:param metadata_in: the given metadata argument
:param station_lat_in: the given station latitude argument
: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 None if there was a
problem while processing the given data argument
"""
metadata_dict = {"station_lat": station_lat_in,
"station_lon": station_lon_in,
"station_climatic_zone": station_climatic_zone_in}
if isinstance(metadata_in, dict):
metadata_dict = {key: metadata_in.get(key, val)
for key, val in metadata_dict.items()}
elif isinstance(metadata_in, (list, tuple)):
if hasattr(metadata_in, "_asdict"):
metadata_dict = {key: metadata_in._asdict().get(key, val)
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
else:
metadata_dict["station_climatic_zone"] = None
return namedtuple("Metadata", metadata_dict)(**metadata_dict)
......@@ -4,7 +4,8 @@ This module contains the following function:
calculate_statistics - calculate the requested statistics
"""
from toarstats.input_checks import check_data, check_sampling, check_statistics
from toarstats.input_checks import (check_data, check_metadata, check_sampling,
check_statistics)
def calculate_statistics(sampling, statistics, data=None, metadata=None,
......@@ -93,3 +94,5 @@ def calculate_statistics(sampling, statistics, data=None, metadata=None,
sampling_method = check_sampling(sampling)
stats_list = check_statistics(statistics)
data_frame = check_data(data, datetimes, values)
metadata_tuple = check_metadata(metadata, station_lat, station_lon,
station_climatic_zone)
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