# -*- coding: utf-8 -*-
"""
Created on Wed Sep 16 12:25:31 2020

@author: aforsythe

Copied from "\\srv-ott3\Projects\11934.201 Newtown Creek TPP – Privileged and Confidential\06_Models\02_EFDC\01_Bathy\Bathy_Conversion.py"
_AJMR
"""
import pandas as pd
import cartopy.crs as ccrs
import contextily as ctx
import matplotlib.pyplot as plt


# Assign path
dataPath = '//srv-ott3/Projects/11934.201 Newtown Creek TPP – Privileged and Confidential/06_Models/02_EFDC/01_Bathy/'
mapbox = 'https://api.mapbox.com/styles/v1/alexander0042/ckemxgtk51fgp19nybfmdcb1e/tiles/256/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoiYWxleGFuZGVyMDA0MiIsImEiOiJjazVmdG4zbncwMHY4M2VrcThwZGUzZDFhIn0.w6oDHoo1eCeRlSBpwzwVtw'


#%%  Read in Source 1
S1 = pd.read_csv(dataPath + '2012_March_LiDAR_XYZ_V2.csv')

# Process Source 1
S1 = S1.rename(columns={'ELEV':'Z_FT'})
#delete all rows where elevation is above 0 from S1
PlusZero = S1[S1['Z_FT'] >= 0].index
S1 = S1.drop(PlusZero)
S1['Z_FT'] *=-1

#%%  Plot Source 1
# Create plot
fig, axes = plt.subplots(nrows=1, ncols=1,subplot_kw={'projection': ccrs.epsg(32118)}, figsize=(12, 12))
#axes.set_xlim(297579, 309365)
#axes.set_ylim(58211, 67565)
axes.set_xlim(305900, 306400)
axes.set_ylim(61500, 62200)

pltDat = axes.scatter(pd.to_numeric(S1.X)*0.3048, pd.to_numeric(S1.Y)*0.3048,
                       c=pd.to_numeric(S1.Z_FT)*0.3048, vmin=0, vmax=10, marker='.', s=40)
ctx.add_basemap(axes, source=mapbox, crs='EPSG:32118')

cbar = fig.colorbar(pltDat, ax=axes, shrink=0.95)
cbar.set_label('Elevation [mNAVD88]')

axes.set_title('2012_March_LiDAR_XYZ_V2')

plt.show()


#%%  Read in source 2, drop columns and rename
S2 = pd.read_csv(dataPath + 'H11353_2m_NAD83SP_NY_LongIsland3104_USFT_depth.csv')
S2 = S2.drop(['FID'], axis=1)
S2 = S2.rename(columns={'X_SP':'X','Y_SP':'Y'})

#%%  Plot Source 2
fig, axes = plt.subplots(nrows=1, ncols=1,subplot_kw={'projection': ccrs.epsg(32118)}, figsize=(12, 12))
axes.set_xlim(297579, 309365)
axes.set_ylim(58211, 67565)

pltDat = axes.scatter(pd.to_numeric(S2.X)*0.3048, pd.to_numeric(S2.Y)*0.3048,
                       c=pd.to_numeric(S2.Z_FT)*0.3048, vmin=0, vmax=40)
ctx.add_basemap(axes, source=mapbox, crs='EPSG:32118')

cbar = fig.colorbar(pltDat, ax=axes, shrink=0.95)
cbar.set_label('Elevation [mNAVD88]')

axes.set_title('H11353_2m_NAD83SP_NY_LongIsland3104_USFT_depth')

plt.show()

#%%  read in source 3 and rename
col_names = ["X","Y","Z_FT"]
S3 = pd.read_csv(dataPath + 'NC_2012_MBES_NAVD88_P2_average_2ft.csv', sep=' ',names=col_names)

#%%  Plot Source 3
fig, axes = plt.subplots(nrows=1, ncols=1,subplot_kw={'projection': ccrs.epsg(32118)}, figsize=(12, 12))
#axes.set_xlim(297579, 309365)
#axes.set_ylim(58211, 67565)
axes.set_xlim(305900, 306500)
axes.set_ylim(61500, 62200)

pltDat = axes.scatter(pd.to_numeric(S3.X)*0.3048, pd.to_numeric(S3.Y)*0.3048,
                       c=pd.to_numeric(S3.Z_FT)*0.3048, vmin=0, vmax=10, marker='.', s=40)
ctx.add_basemap(axes, source=mapbox, crs='EPSG:32118')

cbar = fig.colorbar(pltDat, ax=axes, shrink=0.95)
cbar.set_label('Elevation [mNAVD88]')

axes.set_title('NC_2012_MBES_NAVD88_P2_average_2ft')

plt.show()


#%%  read in source 4, drop columns and rename
S4 = pd.read_csv(dataPath + 'Harbour_Sounding_point_NAD83SP_NY_LongIsland3104_USFT.csv')
S4 = S4.drop(['FID'], axis=1)
S4 = S4.drop(['Zm'], axis=1)
S4 = S4.rename(columns={'Zft':'Z_FT'})


#%%  Plot Source 4
fig, axes = plt.subplots(nrows=1, ncols=1,subplot_kw={'projection': ccrs.epsg(32118)}, figsize=(12, 12))
axes.set_xlim(297579, 309365)
axes.set_ylim(58211, 67565)

pltDat = axes.scatter(pd.to_numeric(S4.X)*0.3048, pd.to_numeric(S4.Y)*0.3048,
                       c=pd.to_numeric(S4.Z_FT)*0.3048, vmin=0, vmax=40, marker='.', s=20)
ctx.add_basemap(axes, source=mapbox, crs='EPSG:32118')

cbar = fig.colorbar(pltDat, ax=axes, shrink=0.95)
cbar.set_label('Elevation [mNAVD88]')

axes.set_title('Harbour_Sounding_point_NAD83SP_NY_LongIsland3104_USFT')

plt.show()

#%%  read in source 5, drop columns and rename
S5 = pd.read_csv(dataPath + 'Harbour_Sounding_point_EastRiver.csv')
S5 = S5.drop(['FID'], axis=1)
S5 = S5.drop(['Zm'], axis=1)
S5 = S5.rename(columns={'Z_ft':'Z_FT'})


#%%  Plot Source 5
fig, axes = plt.subplots(nrows=1, ncols=1,subplot_kw={'projection': ccrs.epsg(32118)}, figsize=(12, 12))
axes.set_xlim(297579, 309365)
axes.set_ylim(58211, 67565)

pltDat = axes.scatter(pd.to_numeric(S5.X)*0.3048, pd.to_numeric(S5.Y)*0.3048,
                       c=pd.to_numeric(S5.Z_FT)*0.3048, vmin=0, vmax=40, marker='.', s=20)
ctx.add_basemap(axes, source=mapbox, crs='EPSG:32118')

cbar = fig.colorbar(pltDat, ax=axes, shrink=0.95)
cbar.set_label('Elevation [mNAVD88]')

axes.set_title('Harbour_Sounding_point_EastRiver')

plt.show()

#%%  Merged basin plot
fig, axes = plt.subplots(nrows=1, ncols=1,subplot_kw={'projection': ccrs.epsg(32118)}, figsize=(12, 12))
#axes.set_xlim(297579, 309365)
#axes.set_ylim(58211, 67565)
axes.set_xlim(305900, 306600)
axes.set_ylim(61500, 62200)

pltDat = axes.scatter(pd.to_numeric(S3.X)*0.3048, pd.to_numeric(S3.Y)*0.3048,
                       c=pd.to_numeric(S3.Z_FT)*0.3048, vmin=0, vmax=10, marker='.', s=40)

pltDat2 = axes.scatter(pd.to_numeric(S1.X)*0.3048, pd.to_numeric(S1.Y)*0.3048,
                       c=pd.to_numeric(S1.Z_FT)*0.3048, vmin=0, vmax=10, marker='o', s=100, edgecolors='k')

ctx.add_basemap(axes, source=mapbox, crs='EPSG:32118')

cbar = fig.colorbar(pltDat, ax=axes, shrink=0.95)
cbar.set_label('Elevation [mNAVD88]')

axes.set_title('Merged Basin Plot: S3 base, S1')

plt.show()

#%%  Merged Creek Mouth
fig, axes = plt.subplots(nrows=1, ncols=1,subplot_kw={'projection': ccrs.epsg(32118)}, figsize=(12, 12))
axes.set_xlim(302206, 304408)
axes.set_ylim(62354, 64085)


pltDat = axes.scatter(pd.to_numeric(S2.X)*0.3048, pd.to_numeric(S2.Y)*0.3048,
                       c=pd.to_numeric(S2.Z_FT)*0.3048, vmin=0, vmax=40, marker='.', s=40)

pltDat2 = axes.scatter(pd.to_numeric(S4.X)*0.3048, pd.to_numeric(S4.Y)*0.3048,
                       c=pd.to_numeric(S4.Z_FT)*0.3048, vmin=0, vmax=40, marker='o', s=100, edgecolors='k')

ctx.add_basemap(axes, source=mapbox, crs='EPSG:32118')

cbar = fig.colorbar(pltDat, ax=axes, shrink=0.95)
cbar.set_label('Elevation [mNAVD88]')

axes.set_title('Merged NTC Mouth Plot: S2 base, S4')

plt.show()

#%%  Merged South End
fig, axes = plt.subplots(nrows=1, ncols=1,subplot_kw={'projection': ccrs.epsg(32118)}, figsize=(12, 12))
axes.set_xlim(301319, 303261)
axes.set_ylim(59067, 60555)

pltDat = axes.scatter(pd.to_numeric(S2.X)*0.3048, pd.to_numeric(S2.Y)*0.3048,
                       c=pd.to_numeric(S2.Z_FT)*0.3048, vmin=0, vmax=40, marker='.', s=40)

pltDat2 = axes.scatter(pd.to_numeric(S5.X)*0.3048, pd.to_numeric(S5.Y)*0.3048,
                       c=pd.to_numeric(S5.Z_FT)*0.3048, vmin=0, vmax=40, marker='o', s=100, edgecolors='k')

ctx.add_basemap(axes, source=mapbox, crs='EPSG:32118')

cbar = fig.colorbar(pltDat, ax=axes, shrink=0.95)
cbar.set_label('Elevation [mNAVD88]')

axes.set_title('Merged East River Plot: S2 base, S5')

plt.show()
#%%  Export

# Priority Order:
# S3: NC_2012_MBES_NAVD88_P2_average_2ft
# Complete set for creek
Out3 = S3

Out3.iloc[:,0] = Out3.iloc[:,0]*0.3048
Out3.iloc[:,1] = Out3.iloc[:,1]*0.3048
Out3.iloc[:,2] = Out3.iloc[:,2]*0.3048

Out3.to_csv('C:/Users/arey/files/Projects/Newtown/ModelBathy/S3_NC_2012_MBES_NAVD88_P2_average_2ft.xyz',sep=' ',header=False,index=False)
# S1: 2012_March_LiDAR_XYZ_V2
# Scattered points covering additional creek areas
Out1 = S1

Out1.iloc[:,0] = Out1.iloc[:,0]*0.3048
Out1.iloc[:,1] = Out1.iloc[:,1]*0.3048
Out1.iloc[:,2] = Out1.iloc[:,2]*0.3048

Out1.to_csv('C:/Users/arey/files/Projects/Newtown/ModelBathy/S1_2012_March_LiDAR_XYZ_V2.xyz',sep=' ',header=False,index=False)


# S2: H11353_2m_NAD83SP_NY_LongIsland3104_USFT_depth
# Complete East River Set
Out2 = S2

Out2.iloc[:,0] = Out2.iloc[:,0]*0.3048
Out2.iloc[:,1] = Out2.iloc[:,1]*0.3048
Out2.iloc[:,2] = Out2.iloc[:,2]*0.3048

Out2.to_csv('C:/Users/arey/files/Projects/Newtown/ModelBathy/S2_H11353_2m_NAD83SP_NY_LongIsland3104_USFT_depth.xyz',sep=' ',header=False,index=False)

# S4: Harbour_Sounding_point_NAD83SP_NY_LongIsland3104_USFT
# Sounding Points at mouth of river
Out4 = S4

Out4.iloc[:,0] = Out4.iloc[:,0]*0.3048
Out4.iloc[:,1] = Out4.iloc[:,1]*0.3048
Out4.iloc[:,2] = Out4.iloc[:,2]*0.3048

Out4.to_csv('C:/Users/arey/files/Projects/Newtown/ModelBathy/S4_Harbour_Sounding_point_NAD83SP_NY_LongIsland3104_USFT.xyz',sep=' ',header=False,index=False)

# S5: Harbour_Sounding_point_NAD83SP_NY_LongIsland3104_USFT
# Sounding Points in East River
Out5 = S5

Out5.iloc[:,0] = Out5.iloc[:,0]*0.3048
Out5.iloc[:,1] = Out5.iloc[:,1]*0.3048
Out5.iloc[:,2] = Out5.iloc[:,2]*0.3048

Out5.to_csv('C:/Users/arey/files/Projects/Newtown/ModelBathy/S5_Harbour_Sounding_point_EastRiver.xyz',sep=' ',header=False,index=False)