#%% Script to read LSU weather files and save as .bts
# AJMR, APril 11, 2022

# Set up
from stdio.bts import bts_write
from stdio.types  import STData
from stdio.csv import parse_field_name, identify_data_type, identify_angular_name, extract_angular_unit, identify_angular_unit, format_series, is_missing, is_missing_number, is_missing_text, csv_read
from pybut.gnx    import is_numeric, field_format
from pybut.dtx    import identify_time, extract_time

import pandas as pd
import numpy as np

import datetime as datetime

import matplotlib.pyplot as plt

import dataretrieval.nwis as nwis
import eeweather
#%% Read in data

# Initalize a list to hold the data
weatherPath = '//srv-mad3/Projects/13522.101 LSU Lakes/03_Data/02_Physical/02_Weather_Reports/'

# weatherDat = pd.read_excel(weatherPath + '2002-2018 weather_report.xls.xlsx',
#                                      engine='openpyxl', header=1)
weatherDat = pd.read_excel(weatherPath + '2005 weather_report.xlsx',
                                     engine='openpyxl', header=1)
weatherDat.rename(columns={'DateTimeCollected': 'DATETIME'}, inplace=True)


#%% Format for BTS
ts = STData()
ts.dims['time'] = []

for idx, name in enumerate(weatherDat.columns.values.tolist()):

    nameFormat, lame, unit = parse_field_name(name)
    dtype = identify_data_type(nameFormat, unit)  # NOTE: can not identify text at this point
    ts.var_add_empty(lame, name=nameFormat, unit=unit, dtype=dtype)

    # Everything should be string
    ts.series[idx] = np.array(weatherDat[name].values.astype(str))


# NOTE: will choke on missing data in time fields
#
tvar = identify_time(ts.series, ts.names, ts.units)
time, msg = extract_time(ts.series, tvar)

ts.dims['time'] = np.array(time)
for idx in range(len(ts.axes)):
    ts.axes[idx] = ['time']
    ts.ranks[idx] = 0

for idx in range(len(ts.series)):
    ts.types[idx], ts.forms[idx], ts.precs[idx], ts.series[idx] = format_series(ts.types[idx], ts.series[idx])


#%% Save as BTS
# bts_write(weatherPath + '2002-2018 weather_report.bts', ts)
bts_write(weatherPath + '2005 weather_report.bts', ts)

# %% Plot
import pandas as pd
import matplotlib.pyplot as plt

# Import as dataframe
weatherPath = '//srv-mad3/Projects/13522.101 LSU Lakes/03_Data/02_Physical/02_Weather_Reports/'
weatherDat = pd.read_excel(weatherPath + '2002-2018 weather_report.xls.xlsx',
                                     engine='openpyxl', header=1)

# Plot Dataframe

# Set up axes
fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(6, 6))

# Plot
# weatherDat.plot(x='DateTimeCollected', y='Max Air Temp (°F)', ax=axes, legend=True)
# axes.set_ylim(30, 120)
# axes.set_ylabel('Max Air Temp (°F)')

weatherDat.plot(x='DateTimeCollected', y='Rain (in)', ax=axes[0], legend=True)
axes[0].set_ylabel('Rain (in)')

weatherDat.plot(x='DateTimeCollected', y='Avg Wind Speed (mph)', ax=axes[1], legend=True)
axes[1].set_ylabel('Avg Wind Speed (mph)')

weatherDat.plot(x='DateTimeCollected', y='Avg Wind Direction (°)', ax=axes[2], legend=True)
axes[2].set_ylabel('Avg Wind Direction (°)')


# Set xlim
# axes.set_xlim(datetime.datetime(2005, 1, 1), datetime.datetime(2005, 12, 31))
axes[0].set_xlim(datetime.datetime(2005, 9, 21), datetime.datetime(2005, 10, 1))
axes[1].set_xlim(datetime.datetime(2005, 9, 21), datetime.datetime(2005, 10, 1))
axes[2].set_xlim(datetime.datetime(2005, 9, 21), datetime.datetime(2005, 10, 1))

axes[2].set_xlabel('Date')


# Show
plt.show()








