#%% Scratch file to compare Delft3D input bounds
# Alexander Rey, Nay 16, 2023

#%% Import modules
import os
import numpy as np
import matplotlib.pyplot as plt
plt.close('all')
import dfm_tools as dfmt
import hydrolib.core.dflowfm as hcdfm
import pandas as pd
import datetime
from matplotlib import dates as mpl_dates

#%% Read in Salinity data
file_bc = r'\\ott-diomedes\Projects\11934.201_Newtown_Creek_TPP\Delft3D\Run169_2014HD_Sed_OrthoB_pliz_WorkingNoteNoMin_Wind.dsproj_data\FlowFM\input\Salinity.bc'

#Load .bc-file using HydroLib object ForcingModel.
m = hcdfm.ForcingModel(file_bc)
ForcingModel_object_out = hcdfm.ForcingModel()

# Create xarray object for SouthBound
salinityN_xr = dfmt.forcinglike_to_Dataset(m.forcing[0], convertnan=True)
salinityS_xr = dfmt.forcinglike_to_Dataset(m.forcing[2], convertnan=True)

#%% Read in Temperature data
file_bc = r'\\ott-diomedes\Projects\11934.201_Newtown_Creek_TPP\Delft3D\Run169_2014HD_Sed_OrthoB_pliz_WorkingNoteNoMin_Wind.dsproj_data\FlowFM\input\Temperature.bc'

#Load .bc-file using HydroLib object ForcingModel.
m = hcdfm.ForcingModel(file_bc)
ForcingModel_object_out = hcdfm.ForcingModel()

# Create xarray object for NorthBound and SouthBound
temperatureN_xr = dfmt.forcinglike_to_Dataset(m.forcing[0], convertnan=True)
temperatureS_xr = dfmt.forcinglike_to_Dataset(m.forcing[2], convertnan=True)


#%% Read in Water Level data
file_bc = r'\\ott-diomedes\Projects\11934.201_Newtown_Creek_TPP\Delft3D\Run169_2014HD_Sed_OrthoB_pliz_WorkingNoteNoMin_Wind.dsproj_data\FlowFM\input\WaterLevel.bc'

#Load .bc-file using HydroLib object ForcingModel.
m = hcdfm.ForcingModel(file_bc)
ForcingModel_object_out = hcdfm.ForcingModel()

# Create xarray object for NorthBound
waterlevelN_xr = dfmt.forcinglike_to_Dataset(m.forcing[0], convertnan=True)
waterlevelS_xr = dfmt.forcinglike_to_Dataset(m.forcing[2], convertnan=True)


#%% Plot Bound Data

# Set Time Bounds
dateMin = pd.to_datetime(datetime.datetime(2015, 7, 18, 0, 0, 0, 0))
dateMax = pd.to_datetime(datetime.datetime(2015, 7, 22, 0, 0, 0, 0))

fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(12, 6), sharex=True)

# Plot a line for salinity for all times for midpoint layer
salinityN_xr.salinitybnd.isel(depth=9).plot.line(ax=ax[0], linestyle='--', color='r', label='North Top')
salinityS_xr.salinitybnd.isel(depth=9).plot.line(ax=ax[0], linestyle='-', color='r', label='South Top')
salinityN_xr.salinitybnd.isel(depth=0).plot.line(ax=ax[0], linestyle='--', color='b', label='North Bottom')
salinityS_xr.salinitybnd.isel(depth=0).plot.line(ax=ax[0], linestyle='-', color='b', label='South Bottom')

temperatureN_xr.temperaturebnd.isel(depth=9).plot.line(ax=ax[1], linestyle='--', color='r')
temperatureS_xr.temperaturebnd.isel(depth=9).plot.line(ax=ax[1], linestyle='-', color='r')
temperatureN_xr.temperaturebnd.isel(depth=0).plot.line(ax=ax[1], linestyle='--', color='b')
temperatureS_xr.temperaturebnd.isel(depth=0).plot.line(ax=ax[1], linestyle='-', color='b')

waterlevelN_xr.waterlevelbnd.plot.line(ax=ax[2], linestyle='--', color='r')
waterlevelS_xr.waterlevelbnd.plot.line(ax=ax[2], linestyle='-', color='r')

# Set y axis limits
ax[0].set_ylim(12, 27)
ax[1].set_ylim(15, 25)
ax[2].set_ylim(-2, 2)

ax[2].set_xlim(dateMin, dateMax)
ax[2].xaxis.set_major_formatter(
    mpl_dates.ConciseDateFormatter(ax[2].xaxis.get_major_locator()))

ax[0].legend()

plt.show()
