#!/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/>.
#
########################################################################
"""
Bibliothèque pyspc du projet pyspc - IO - Hydroportail - write
"""
import os.path
import pyspc.core.exception as _exception
from pyspc.core.samples import Sample
from pyspc.convention.hydroportail import VARNAMES
from pyspc.metadata.hydroportail import Sample as HP_Sample
VARNAMES_R = {v: k for k, v in VARNAMES.items()}
[docs]
def write_Hydroportail(data=None, datatype=None, dirname='.'):
"""
Ecrire un fichier au format Hydroportail.
Parameters
----------
data : pyspc.core.statistics.Samples
Echantillons.
datatype : str
Type du fichier Hydroportail.
dirname : str
Répertoire d'export
Returns
-------
filename : str
Fichier écrit
"""
# -------------------------------------------------------------------------
# 0- Contrôles
# -------------------------------------------------------------------------
_exception.check_str(dirname)
# -------------------------------------------------------------------------
# 1.1- Conversion - DONNEES - ECHANTILLON
# -------------------------------------------------------------------------
if datatype == 'hp_sample':
return _hp_sample(sample=data, dirname=dirname)
# -------------------------------------------------------------------------
# X- Conversion - CAS inconnu
# -------------------------------------------------------------------------
raise NotImplementedError
def _hp_sample(sample=None, dirname=None):
"""Conversion - META-DONNEES - ECHANTILLON."""
_exception.raise_valueerror(
not isinstance(sample, Sample),
"'data' doit être un échantillon 'Sample'"
)
filename = os.path.join(
dirname,
f"{VARNAMES_R.get(sample.spc_varname, 'Q-X')}_None_"
f"{sample.code}_{sample.name}_Echantillon.csv"
)
sample.sort(by='date')
writer = HP_Sample(filename=filename)
return writer.write(df=sample.df.copy(deep=True))