

import xmltodict
import pandas as pd


class DovTimeSeries(object):

    def __init__(self, xml_doc):
        with open(xml_doc) as fd:
            doc = xmltodict.parse(fd.read())
            self.peilmetingen = self._get_peilmetingen_df(doc)
            self.observaties = self._get_observaties_df(doc)
            self.metadata_locatie = doc["ns2:dov-schema"]["grondwaterlocatie"]
            self.metadata_filters = self._get_filter_metadata(doc)

    @staticmethod
    def _get_filter_metadata(doc):
        """"""
        filter_info = {}
        for filterd in doc["ns2:dov-schema"]['filter']:
            filter_info[filterd["identificatie"]] = filterd
        return filter_info

    @staticmethod
    def get_peilmetingen(doc):
        """Generator to extract the individual measurements from the XML
        export"""
        for filterm in doc["ns2:dov-schema"]['filtermeting']:
            for meting in filterm['peilmeting']:
                yield (filterm['grondwaterlocatie'],
                       filterm['filter']['identificatie'],
                       meting['datum'],
                       meting['diepte'],
                       meting['methode'],
                       meting['betrouwbaarheid'])

    def _get_peilmetingen_df(self, doc):
        """"""
        doc_df = pd.DataFrame(list(self.get_peilmetingen(doc)),
                              columns=["grondwaterlocatie",
                                       "filternummer",
                                       "datum",
                                       "diepte",
                                       "methode",
                                       "betrouwbaarheid"])
        doc_df["datum"] = pd.to_datetime(doc_df["datum"])
        doc_df["diepte"] = pd.to_numeric(doc_df["diepte"])
        doc_df = doc_df.set_index("datum")
        return doc_df

    @staticmethod
    def get_observaties(doc):
        """Generator to extract the individual observations from the XML
        export"""
        for filterm in doc["ns2:dov-schema"]['filtermeting']:
            for watermonster in filterm['watermonster']:
                for observatie in watermonster['observatie']:
                    yield (filterm['grondwaterlocatie'],
                           filterm['filter']['identificatie'],
                           watermonster['identificatie'],
                           ' '.join([watermonster['monstername']['datum'],
                                     watermonster['monstername']['tijd']]),
                           observatie['parameter'],
                           observatie['waarde_numeriek'],
                           observatie['eenheid'],
                           observatie['betrouwbaarheid'])

    def _get_observaties_df(self, doc):
        """"""
        doc_df = pd.DataFrame(list(self.get_observaties(doc)),
                              columns=["grondwaterlocatie",
                                       "filternummer",
                                       "monsternummer",
                                       "datum",
                                       "parameter",
                                       "waarde",
                                       "eenheid",
                                       "betrouwbaarheid"])
        doc_df["datum"] = pd.to_datetime(doc_df["datum"])
        doc_df["waarde"] = pd.to_numeric(doc_df["waarde"])
        return doc_df


@@ -34,16 +34,62 @@
 as pd%0A%0A%0A
+class DOVVariableError(Exception):%0A    pass%0A%0A%0A
 class Do
@@ -3426,28 +3426,1009 @@
 e%22%5D)%0A        return doc_df%0A%0A
+    @property%0A    def variables(self):%0A        %22%22%22return list of variables currently stored%22%22%22%0A        return self.observaties%5B%22parameter%22%5D.unique()%0A%0A    def get_parameter_series(self, variable):%0A        %22%22%22select specific parameter%22%22%22%0A        if variable not in self.observaties%5B%22parameter%22%5D.unique():%0A            raise DOVVariableError(%22Variable name %7B%7D not represented in %22%0A                                   %22observations%22.format(variable))%0A%0A        vardata = self.observaties%5Bself.observaties%5B%22parameter%22%5D == variable%5D%0A        return vardata.pivot_table(%0A                            index=%22datum%22,%0A                            columns=%5B%22filternummer%22%5D,%0A                            values=%22waarde%22)%0A%0A    @property%0A    def peilmetingen_timeseries(self):%0A        return self.peilmetingen.reset_index().pivot_table(%0A                            index=%22datum%22,%0A                            columns=%5B%22grondwaterlocatie%22, %22filternummer%22%5D,%0A                            values=%22diepte%22)%0A%0A%0A

add convenient timeseries extractions