import hkvfewspy as fewspy
import matplotlib.pyplot as plt
import os
import pandas as pd

"""
Explanation: Test FewsTimeSeries and FewsTimeSeriesCollection
End of explanation
"""


fews_ts = fewspy.schemas.timeseries.FewsTimeSeries.from_pi_xml("./test_2_series.xml")

"""
Explanation: FewsTimeSeries
Import first series from Fews Pi-XML file. Even though file contains two series FewsTimeSeries will only import the first series in the file.
End of explanation
"""


fews_ts.plot(y="value")
plt.show()

"""
Explanation: Plot the series by calling plot on the FewsTimeSeries object (plot is passed on to the plot function in pandas for Series and DataFrames).
End of explanation
"""


fews_ts.header

fews_ts.to_pi_xml("./temp_1series.xml")

"""
Explanation: View metadata:
End of explanation
"""


fews_ts_collection = fewspy.schemas.timeseries.FewsTimeSeriesCollection.from_pi_xml("./test_2_series.xml")

"""
Explanation: FewsTimeSeriesCollection
To import all series from a Fews Pi-XML file, use FewsTimeSeriesCollection:
End of explanation
"""


fews_ts_collection.timeseries

"""
Explanation: Timeseries are collected in a DataFrame with header data as columns and the series in the events column:
End of explanation
"""


fig, ax = plt.subplots(1, 1)
for i in range(fews_ts_collection.timeseries.shape[0]):
    series = fews_ts_collection.timeseries.events.iloc[i]
    series.plot(y="value", ax=ax, label=series.header["stationName"])
    
plt.show()

"""
Explanation: Loop through timeseries to plot
End of explanation
"""


fews_ts_collection.timeseries.events[0]

"""
Explanation: Note that the objects stored in the timeseries DataFrame in the events column are actually FewsTimeSeries objects. This means that our earlier FewsTimeSeries object should be the same as the first timeseries in our FewsTimeSeriesCollection.
End of explanation
"""


# get start and end date from timeseries and update header
fews_ts_collection.timeseries.events[0]._update_header_dates()

"""
Explanation: We will now test whether the FewsTimeSeries in the collection is the same as the single timeseries we imported earlier. But first, it is important to realize that when we wrote our earlier FewsTimeSeries object fews_ts to XML the header (endDate and startDate) were automatically updated from the data. We will do the same for the first timeseries in the FewsTimeSeriesCollection object.
End of explanation
"""


fews_ts_collection.timeseries.events[0] == fews_ts

"""
Explanation: Now test for equality:
End of explanation
"""


help(fews_ts_collection.add_series)

"""
Explanation: It is possible to add data to the collection with the add_series method
End of explanation
"""


new_series = fews_ts.timeseries
new_metadata = fews_ts.header

fews_ts_collection.add_series(new_series, new_metadata)

"""
Explanation: Use data from first timeseries as "new" series:
End of explanation
"""


fews_ts_collection.timeseries

"""
Explanation: View our added timeseries in the DataFrame:
End of explanation
"""


fews_ts_collection.to_pi_xml("./temp_3series.xml")

"""
Explanation: We can write the FewsTimeSeriesCollection to file with the same command as before:
End of explanation
"""


# set client
pi = fewspy.pi
pi.setClient(wsdl='http://localhost:8081/FewsPiService/fewspiservice?wsdl') 

query = pi.setQueryParameters(prefill_defaults=True)

query.parameterIds(['P.meting.dagcal'])
query.moduleInstanceIds(['ImportCAW'])
query.locationIds(['66011cal'])
query.startTime(pd.datetime(2010,1,1))
query.endTime(pd.datetime(2018,7,1))
query.clientTimeZone('Europe/Amsterdam')
query.query["onlyManualEdits"] = False

query.query

fews_ts2_coll = pi.getFewsTimeSeries(queryParameters=query)

fews_ts2_coll

fews_ts2_coll.timeseries

"""
Explanation: Querying a FEWS database and returning FewsTimeSeries objects
End of explanation
"""


fews_ts2 = fews_ts2_coll.timeseries.events[0]

fews_ts2.header

fews_ts2.timeseries.head()

fews_ts2.plot(y="value")

"""
Explanation: We queried specifically to get one timeseries, so extract FewsTimeSeries object from the collection:
End of explanation
"""


