Code source de pyspc.model.socose

#!/usr/bin/python3
# -*- coding: utf-8 -*-
########################################################################
#
# This file is part of python module <pyspc>.
# Copyright (C) 2013-2021  R. Marty
#   (renaud.marty@developpement-durable.gouv.fr)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see COPYING.txt).
# If not, see <http://www.gnu.org/licenses/>.
#
########################################################################
"""
Méthode SOCOSE
"""
import pandas as pnd
import warnings
import pyspc.core.exception as _exception

warnings.filterwarnings("ignore", message="numpy.dtype size changed")
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")


[docs] def socose(df=None): """ Déterminer les paramètrs D et RXD du modèle SOCOSE. - D : durée au-dessus du débit standardisé valant 0.5 (Q/Qp) - RXD : rapport du débit de pointe sur le débit moyen calculé sur la durée D. Parameters ---------- df : pandas.DataFrame Hydrogrammes synthétiques Returns ------- socose : dict Valeurs SOCOSE par colonne du tableau DataFrame - col: {'d': d, 'rxd': rxd} Examples -------- >>> df (K0000000, QH) -1 days +17:00:00 0.167761 -1 days +18:00:00 0.185996 -1 days +19:00:00 0.223195 -1 days +20:00:00 0.390956 -1 days +21:00:00 0.674690 -1 days +22:00:00 0.870897 -1 days +23:00:00 0.965718 00:00:00 1.000000 01:00:00 0.983953 02:00:00 0.945295 03:00:00 0.884026 04:00:00 0.814734 05:00:00 0.744712 06:00:00 0.683443 07:00:00 0.617068 08:00:00 0.558716 09:00:00 0.506929 10:00:00 0.466083 11:00:00 0.431072 12:00:00 0.401167 13:00:00 0.371991 14:00:00 0.350839 15:00:00 0.334063 16:00:00 0.317287 17:00:00 0.305616 >>> values = socose(df) >>> values {('K0000000', 'QH'): {'d': 13, 'rxd': 1.268270287129564}} """ _exception.check_dataframe(df) values = {} for c in df.columns: d = (df[c] >= 0.5).sum() rxd = df[c].max() / df[df[c] >= 0.5][c].mean() values.setdefault(c, {'d': d, 'rxd': rxd}) return values
[docs] def build_hydrograph(start=0, end=0, scale=None): """ Construire un hydrogramme unitaire. Parameters ---------- start : int Premier instant. end : int Dernier instant. scale : int Temps de réponse/montée. Returns ------- df : pandas.DataFrame Hydrogramme unitaire See Also -------- pyspc.model.socose.plot_hydrograph Examples -------- >>> from pyspc.model.socose import build_hydrograph >>> start = 0 >>> end = 24 >>> scale = 6 >>> df = build_hydrograph(start=start, end=end, scale=scale) >>> df SOCOSE 0 0.000000 1 0.001543 2 0.024688 3 0.124514 4 0.380226 5 0.782518 6 1.000000 7 0.835981 8 0.575225 9 0.380226 10 0.254918 11 0.175661 12 0.124514 13 0.090567 14 0.067395 15 0.051166 16 0.039535 17 0.031027 18 0.024688 19 0.019887 20 0.016199 21 0.013327 22 0.011064 23 0.009262 24 0.007812 """ _exception.check_numeric(start) _exception.check_numeric(end) _exception.raise_valueerror(start > end, "'start' et 'end' incorrects") _exception.check_int(scale) _exception.raise_valueerror(scale <= 0, "'scale' ne peut être négatif") index = list(range(start, end+1)) df = pnd.DataFrame( {'SOCOSE': [unit_hydrograph(t, scale) for t in index]}, index=index) # with pnd.option_context('display.max_rows', None, # 'display.max_columns', None): # print(df) # from scipy.integrate import quad # import numpy as np # integrale = quad(unit_hydrograph, 0, np.inf, args=(scale,)) # print(integrale) return df
[docs] def unit_hydrograph(t, tc): """Hydrogramme unitaire SOCOS.""" return 2 * (t / tc)**4 / (1 + (t/tc)**8)
[docs] def plot_hydrograph(df, filename='unit_hydrograph.png'): """ Tracer un hydrogramme unitaire. Parameters ---------- df : pandas.DataFrame Hydrogrammes unitaires filename : str Fichier à écrire Returns ------- filename : str Fichier à écrire See Also -------- pyspc.model.socose.build_hydrograph """ figsize = (11.67, 8.27) ax = df.plot( marker='o', title="Hydrogramme Unitaire SOCOSE", figsize=figsize) ax.set_xticks(df.index) ax.set_xticklabels(df.index.astype(str)) ylim = ax.get_ylim() ax.set_ylim((0, ylim[-1])) fig = ax.get_figure() fig.tight_layout() fig.savefig(filename, dpi=300) return filename