# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 - 2020 Karlsruhe Institute of Technology - Steinbuch Centre for Computing
# This code is distributed under the MIT License
# Please, see the LICENSE file
#
# @author: vykozlov
import o3api.config as cfg
import logging
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
# conigure python logger
logger = logging.getLogger('__name__') #o3api
logger.setLevel(cfg.log_level)
# configuration for netCDF
TIME = cfg.netCDF_conf['t_c']
LAT = cfg.netCDF_conf['lat_c']
# configuration for API
PTYPE = cfg.api_conf['plot_t']
MODEL = cfg.api_conf['model']
BEGIN = cfg.api_conf['begin']
END = cfg.api_conf['end']
MONTH = cfg.api_conf['month']
LAT_MIN = cfg.api_conf['lat_min']
LAT_MAX = cfg.api_conf['lat_max']
# configuration for plotting
plot_c = cfg.plot_conf
[docs]def clean_models(**kwargs):
"""Clean models from empty entries, spaces, and quotes
:param kwargs: The provided in the API call parameters
:return: models cleaned from empty entries, spaces, quotes
:rtype: list
"""
model_kwargs = list(filter(None, kwargs[MODEL])) # remove empty elements
# strip possible spaces in front and back, and then quotas
models = [ m.strip().strip('\"') for m in model_kwargs ]
return models
[docs]def get_date_range(ds):
"""Return the range of dates in the provided dataset
:param ds: xarray dataset to check
:return: date_min, date_max
"""
date_min = np.amin(ds.coords[TIME].values)
date_max = np.amax(ds.coords[TIME].values)
return date_min, date_max
[docs]def get_periodicity(pd_time):
"""Calculate periodicity in the provided data
:param pd_time: The time period
:type pd_time: pandas DatetimeIndex
:return: Calculated periodicity as the number of points per year
:rtype: int
"""
date_range = np.amax(pd_time) - np.amin(pd_time)
date_range = (date_range/np.timedelta64(1, 'D'))
periodicity = ((pd_time.size - 1) / date_range ) * 365.0
logger.debug("Periodicity calculated: {}".format(periodicity))
return int(round(periodicity, 0))
[docs]def set_plot_title(**kwargs):
"""Set plot title
:param kwargs: The provided in the API call parameters
:return: plot_title with added input parameters
:rtype: string
"""
plot_type = kwargs[PTYPE]
plot_title = ("requested: " + plot_type + ", " +
str(kwargs[BEGIN]) + ".." + str(kwargs[END]))
if len(kwargs[MONTH]) > 0:
plot_title += (", month: ")
for i in kwargs[MONTH]:
plot_title += str(i) + ","
#plot_title = plot_title[:-1] + ""
else:
plot_title += ", full_year,"
deg_sign= u'\N{DEGREE SIGN}'
plot_title += (" latitudes: " + str(kwargs[LAT_MIN]) + deg_sign + ".." +
str(kwargs[LAT_MAX]) + deg_sign)
return plot_title
[docs]def set_filename(**kwargs):
"""Set file name
:param kwargs: The provided in the API call parameters
:return: file_name with added input parameters (no extension given!)
:rtype: string
"""
print("api_conf = ", cfg.api_conf)
file_name = ''
for val in cfg.api_conf.values():
if val == MONTH and len(kwargs[val]) == 0:
par_str = 'full_year'
else:
par_str = str(kwargs[val])
file_name += par_str + "_" if val != MODEL else ''
# delete last "_"
file_name = file_name[:-1] + ''
return file_name