#!/usr/bin/env python
# coding: utf-8

# # pytides example, reworked
# 
# We rework the example of https://github.com/sam-cox/pytides/wiki/Example-Pytides-Usage using a more current version of pytides from the fork at https://github.com/drf5n/pytides
# 
# This example uses the data from (https://tidesandcurrents.noaa.gov/waterlevels.html?id=8516945&units=standard&bdate=20000101&edate=20001231&timezone=GMT&datum=MLLW&interval=h&action=data)[the Kings Point data page]
# 
# 

import datetime 
from pytides.tide import Tide
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd  # to get CSV parsing of live URLs
import matplotlib as mpl

plt.rcParams['figure.figsize'] = [9.5, 6]


#Get the Kings Point data directly from the NOAA COOPS web services listed on the NOAA tidal predictions page

##Prepare our tide data
station_id = '8516945'

#Kings Point data
obs_2000 = pd.read_csv('https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=hourly_height&application=NOS.COOPS.TAC.WL&begin_date=20000101&end_date=20001231&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv')
obs_2001 = pd.read_csv('https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=hourly_height&application=NOS.COOPS.TAC.WL&begin_date=20010101&end_date=20011231&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv')

obs_2000['Date Time'] = pd.to_datetime(obs_2000['Date Time'])
obs_2001['Date Time'] = pd.to_datetime(obs_2001['Date Time'])



# Test data from 6 minute sample URLs from the web service:

# https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=predictions&application=NOS.COOPS.TAC.WL&begin_date=20130101&end_date=20130131&datum=MLLW&station=8516945&time_zone=GMT&units=english&interval=&format=csv
# https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=water_level&application=NOS.COOPS.TAC.WL&begin_date=20130101&end_date=20130131&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv

date_of_interest_begin = datetime.datetime.fromisoformat("2013-01-01 00:00")
date_of_interest_end = date_of_interest_begin + datetime.timedelta(days = 28)
print(f"Test data collected between {date_of_interest_begin} and {date_of_interest_end}")

begin_date = date_of_interest_begin.strftime("%Y%m%d")
end_date = date_of_interest_end.strftime("%Y%m%d")

pred_uri = f'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=predictions&application=NOS.COOPS.TAC.WL&begin_date={begin_date}&end_date={end_date}&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'
obs_uri  = f'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=water_level&application=NOS.COOPS.TAC.WL&begin_date={begin_date}&end_date={end_date}&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'

print(f"""from: \n {pred_uri} \n and \n{obs_uri}""")

noaa_pred_df = pd.read_csv(pred_uri)
noaa_obs_df = pd.read_csv(obs_uri)


# clean the test data :

noaa_obs_df['Date Time'] = pd.to_datetime(noaa_obs_df['Date Time'])
noaa_pred_df['Date Time'] = pd.to_datetime(noaa_pred_df['Date Time'])

#noaa_obs_df = noaa_obs_df
#noaa_pred_df = noaa_pred_df


# assemble the years and drop missing values

obs = pd.concat([obs_2000,obs_2001]).dropna()                            
print(obs)

#for col in obs.columns:
#    print(col)

# convert pandas series to pytides inputs:
heights = np.asarray(obs[' Water Level'])
t = obs['Date Time'].dt.to_pydatetime() # convert numpy timestamp_ns64 to datetime.datetime 

##Prepare a list of prediction datetimes, each 6 minutes apart, for a week.
prediction_t0 = datetime.datetime(2013,1,1)
hours = 0.1*np.arange(7 * 24 * 10)
times = Tide._times(prediction_t0, hours) # 

##Fit the tidal data to the harmonic model using Pytides
my_tide = Tide.decompose(heights, t)

##Predict the tides using the Pytides model.
my_prediction = my_tide.at(times)
mydf = pd.DataFrame(data = {'times': times, 'prediction': my_prediction })
print(mydf)

##Plot the results
plt.plot(times, my_prediction, label="Pytides")
plt.plot(mpl.dates.date2num(noaa_pred_df['Date Time']), noaa_pred_df[' Prediction'], label="NOAA Prediction")
plt.plot(mpl.dates.date2num(noaa_obs_df['Date Time']), noaa_obs_df[' Water Level'], label="NOAA Verified")
plt.legend()
plt.title('Comparison of Pytides and NOAA predictions for Station: ' + str(station_id))
plt.xlabel('Since ' + str(prediction_t0) + '(GMT)')
plt.ylabel('Metres')
plt.xticks(rotation=10)
plt.show()


# use the extrema generator to get the next few tides:
g = my_tide.extrema(t0=datetime.datetime.utcnow()) 
for x in range(6) :
    print(next(g))


print("Form number: ", my_tide.form_number())
print("Classification: ", my_tide.classify())

print(f"""Model for {station_id}
Constituent,Amplitude, Phase""")
for x in my_tide.model:
    #print(x)
    print(f'{x[0]}, {x[1]:8.5f}, {x[2]:7.6f}');



0b36c22d3faea63b6c61e3f1d7f459ac8f0a6341
<mask token>

<mask token>
print(
    f'Test data collected between {date_of_interest_begin} and {date_of_interest_end}'
    )
<mask token>
print(f"""from: 
 {pred_uri} 
 and 
{obs_uri}""")
<mask token>
print(obs)
<mask token>
print(mydf)
plt.plot(times, my_prediction, label='Pytides')
plt.plot(mpl.dates.date2num(noaa_pred_df['Date Time']), noaa_pred_df[
    ' Prediction'], label='NOAA Prediction')
plt.plot(mpl.dates.date2num(noaa_obs_df['Date Time']), noaa_obs_df[
    ' Water Level'], label='NOAA Verified')
plt.legend()
plt.title('Comparison of Pytides and NOAA predictions for Station: ' + str(
    station_id))
plt.xlabel('Since ' + str(prediction_t0) + '(GMT)')
plt.ylabel('Metres')
plt.xticks(rotation=10)
plt.show()
<mask token>
for x in range(6):
    print(next(g))
print('Form number: ', my_tide.form_number())
print('Classification: ', my_tide.classify())
print(f"""Model for {station_id}
Constituent,Amplitude, Phase""")
for x in my_tide.model:
    print(f'{x[0]}, {x[1]:8.5f}, {x[2]:7.6f}')

<mask token>
plt.rcParams['figure.figsize'] = [9.5, 6]
station_id = '8516945'
obs_2000 = pd.read_csv(
    'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=hourly_height&application=NOS.COOPS.TAC.WL&begin_date=20000101&end_date=20001231&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'
    )
obs_2001 = pd.read_csv(
    'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=hourly_height&application=NOS.COOPS.TAC.WL&begin_date=20010101&end_date=20011231&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'
    )
obs_2000['Date Time'] = pd.to_datetime(obs_2000['Date Time'])
obs_2001['Date Time'] = pd.to_datetime(obs_2001['Date Time'])
date_of_interest_begin = datetime.datetime.fromisoformat('2013-01-01 00:00')
date_of_interest_end = date_of_interest_begin + datetime.timedelta(days=28)
print(
    f'Test data collected between {date_of_interest_begin} and {date_of_interest_end}'
    )
begin_date = date_of_interest_begin.strftime('%Y%m%d')
end_date = date_of_interest_end.strftime('%Y%m%d')
pred_uri = (
    f'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=predictions&application=NOS.COOPS.TAC.WL&begin_date={begin_date}&end_date={end_date}&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'
    )
obs_uri = (
    f'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=water_level&application=NOS.COOPS.TAC.WL&begin_date={begin_date}&end_date={end_date}&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'
    )
print(f"""from: 
 {pred_uri} 
 and 
{obs_uri}""")
noaa_pred_df = pd.read_csv(pred_uri)
noaa_obs_df = pd.read_csv(obs_uri)
noaa_obs_df['Date Time'] = pd.to_datetime(noaa_obs_df['Date Time'])
noaa_pred_df['Date Time'] = pd.to_datetime(noaa_pred_df['Date Time'])
obs = pd.concat([obs_2000, obs_2001]).dropna()
print(obs)
heights = np.asarray(obs[' Water Level'])
t = obs['Date Time'].dt.to_pydatetime()
prediction_t0 = datetime.datetime(2013, 1, 1)
hours = 0.1 * np.arange(7 * 24 * 10)
times = Tide._times(prediction_t0, hours)
my_tide = Tide.decompose(heights, t)
my_prediction = my_tide.at(times)
mydf = pd.DataFrame(data={'times': times, 'prediction': my_prediction})
print(mydf)
plt.plot(times, my_prediction, label='Pytides')
plt.plot(mpl.dates.date2num(noaa_pred_df['Date Time']), noaa_pred_df[
    ' Prediction'], label='NOAA Prediction')
plt.plot(mpl.dates.date2num(noaa_obs_df['Date Time']), noaa_obs_df[
    ' Water Level'], label='NOAA Verified')
plt.legend()
plt.title('Comparison of Pytides and NOAA predictions for Station: ' + str(
    station_id))
plt.xlabel('Since ' + str(prediction_t0) + '(GMT)')
plt.ylabel('Metres')
plt.xticks(rotation=10)
plt.show()
g = my_tide.extrema(t0=datetime.datetime.utcnow())
for x in range(6):
    print(next(g))
print('Form number: ', my_tide.form_number())
print('Classification: ', my_tide.classify())
print(f"""Model for {station_id}
Constituent,Amplitude, Phase""")
for x in my_tide.model:
    print(f'{x[0]}, {x[1]:8.5f}, {x[2]:7.6f}')

import datetime
from pytides.tide import Tide
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib as mpl
plt.rcParams['figure.figsize'] = [9.5, 6]
station_id = '8516945'
obs_2000 = pd.read_csv(
    'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=hourly_height&application=NOS.COOPS.TAC.WL&begin_date=20000101&end_date=20001231&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'
    )
obs_2001 = pd.read_csv(
    'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=hourly_height&application=NOS.COOPS.TAC.WL&begin_date=20010101&end_date=20011231&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'
    )
obs_2000['Date Time'] = pd.to_datetime(obs_2000['Date Time'])
obs_2001['Date Time'] = pd.to_datetime(obs_2001['Date Time'])
date_of_interest_begin = datetime.datetime.fromisoformat('2013-01-01 00:00')
date_of_interest_end = date_of_interest_begin + datetime.timedelta(days=28)
print(
    f'Test data collected between {date_of_interest_begin} and {date_of_interest_end}'
    )
begin_date = date_of_interest_begin.strftime('%Y%m%d')
end_date = date_of_interest_end.strftime('%Y%m%d')
pred_uri = (
    f'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=predictions&application=NOS.COOPS.TAC.WL&begin_date={begin_date}&end_date={end_date}&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'
    )
obs_uri = (
    f'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=water_level&application=NOS.COOPS.TAC.WL&begin_date={begin_date}&end_date={end_date}&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'
    )
print(f"""from: 
 {pred_uri} 
 and 
{obs_uri}""")
noaa_pred_df = pd.read_csv(pred_uri)
noaa_obs_df = pd.read_csv(obs_uri)
noaa_obs_df['Date Time'] = pd.to_datetime(noaa_obs_df['Date Time'])
noaa_pred_df['Date Time'] = pd.to_datetime(noaa_pred_df['Date Time'])
obs = pd.concat([obs_2000, obs_2001]).dropna()
print(obs)
heights = np.asarray(obs[' Water Level'])
t = obs['Date Time'].dt.to_pydatetime()
prediction_t0 = datetime.datetime(2013, 1, 1)
hours = 0.1 * np.arange(7 * 24 * 10)
times = Tide._times(prediction_t0, hours)
my_tide = Tide.decompose(heights, t)
my_prediction = my_tide.at(times)
mydf = pd.DataFrame(data={'times': times, 'prediction': my_prediction})
print(mydf)
plt.plot(times, my_prediction, label='Pytides')
plt.plot(mpl.dates.date2num(noaa_pred_df['Date Time']), noaa_pred_df[
    ' Prediction'], label='NOAA Prediction')
plt.plot(mpl.dates.date2num(noaa_obs_df['Date Time']), noaa_obs_df[
    ' Water Level'], label='NOAA Verified')
plt.legend()
plt.title('Comparison of Pytides and NOAA predictions for Station: ' + str(
    station_id))
plt.xlabel('Since ' + str(prediction_t0) + '(GMT)')
plt.ylabel('Metres')
plt.xticks(rotation=10)
plt.show()
g = my_tide.extrema(t0=datetime.datetime.utcnow())
for x in range(6):
    print(next(g))
print('Form number: ', my_tide.form_number())
print('Classification: ', my_tide.classify())
print(f"""Model for {station_id}
Constituent,Amplitude, Phase""")
for x in my_tide.model:
    print(f'{x[0]}, {x[1]:8.5f}, {x[2]:7.6f}')

#!/usr/bin/env python
# coding: utf-8

# # pytides example, reworked
# 
# We rework the example of https://github.com/sam-cox/pytides/wiki/Example-Pytides-Usage using a more current version of pytides from the fork at https://github.com/drf5n/pytides
# 
# This example uses the data from (https://tidesandcurrents.noaa.gov/waterlevels.html?id=8516945&units=standard&bdate=20000101&edate=20001231&timezone=GMT&datum=MLLW&interval=h&action=data)[the Kings Point data page]
# 
# 

import datetime 
from pytides.tide import Tide
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd  # to get CSV parsing of live URLs
import matplotlib as mpl

plt.rcParams['figure.figsize'] = [9.5, 6]


#Get the Kings Point data directly from the NOAA COOPS web services listed on the NOAA tidal predictions page

##Prepare our tide data
station_id = '8516945'

#Kings Point data
obs_2000 = pd.read_csv('https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=hourly_height&application=NOS.COOPS.TAC.WL&begin_date=20000101&end_date=20001231&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv')
obs_2001 = pd.read_csv('https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=hourly_height&application=NOS.COOPS.TAC.WL&begin_date=20010101&end_date=20011231&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv')

obs_2000['Date Time'] = pd.to_datetime(obs_2000['Date Time'])
obs_2001['Date Time'] = pd.to_datetime(obs_2001['Date Time'])



# Test data from 6 minute sample URLs from the web service:

# https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=predictions&application=NOS.COOPS.TAC.WL&begin_date=20130101&end_date=20130131&datum=MLLW&station=8516945&time_zone=GMT&units=english&interval=&format=csv
# https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=water_level&application=NOS.COOPS.TAC.WL&begin_date=20130101&end_date=20130131&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv

date_of_interest_begin = datetime.datetime.fromisoformat("2013-01-01 00:00")
date_of_interest_end = date_of_interest_begin + datetime.timedelta(days = 28)
print(f"Test data collected between {date_of_interest_begin} and {date_of_interest_end}")

begin_date = date_of_interest_begin.strftime("%Y%m%d")
end_date = date_of_interest_end.strftime("%Y%m%d")

pred_uri = f'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=predictions&application=NOS.COOPS.TAC.WL&begin_date={begin_date}&end_date={end_date}&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'
obs_uri  = f'https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?product=water_level&application=NOS.COOPS.TAC.WL&begin_date={begin_date}&end_date={end_date}&datum=MLLW&station=8516945&time_zone=GMT&units=english&format=csv'

print(f"""from: \n {pred_uri} \n and \n{obs_uri}""")

noaa_pred_df = pd.read_csv(pred_uri)
noaa_obs_df = pd.read_csv(obs_uri)


# clean the test data :

noaa_obs_df['Date Time'] = pd.to_datetime(noaa_obs_df['Date Time'])
noaa_pred_df['Date Time'] = pd.to_datetime(noaa_pred_df['Date Time'])

#noaa_obs_df = noaa_obs_df
#noaa_pred_df = noaa_pred_df


# assemble the years and drop missing values

obs = pd.concat([obs_2000,obs_2001]).dropna()                            
print(obs)

#for col in obs.columns:
#    print(col)

# convert pandas series to pytides inputs:
heights = np.asarray(obs[' Water Level'])
t = obs['Date Time'].dt.to_pydatetime() # convert numpy timestamp_ns64 to datetime.datetime 

##Prepare a list of prediction datetimes, each 6 minutes apart, for a week.
prediction_t0 = datetime.datetime(2013,1,1)
hours = 0.1*np.arange(7 * 24 * 10)
times = Tide._times(prediction_t0, hours) # 

##Fit the tidal data to the harmonic model using Pytides
my_tide = Tide.decompose(heights, t)

##Predict the tides using the Pytides model.
my_prediction = my_tide.at(times)
mydf = pd.DataFrame(data = {'times': times, 'prediction': my_prediction })
print(mydf)

##Plot the results
plt.plot(times, my_prediction, label="Pytides")
plt.plot(mpl.dates.date2num(noaa_pred_df['Date Time']), noaa_pred_df[' Prediction'], label="NOAA Prediction")
plt.plot(mpl.dates.date2num(noaa_obs_df['Date Time']), noaa_obs_df[' Water Level'], label="NOAA Verified")
plt.legend()
plt.title('Comparison of Pytides and NOAA predictions for Station: ' + str(station_id))
plt.xlabel('Since ' + str(prediction_t0) + '(GMT)')
plt.ylabel('Metres')
plt.xticks(rotation=10)
plt.show()


# use the extrema generator to get the next few tides:
g = my_tide.extrema(t0=datetime.datetime.utcnow()) 
for x in range(6) :
    print(next(g))


print("Form number: ", my_tide.form_number())
print("Classification: ", my_tide.classify())

print(f"""Model for {station_id}
Constituent,Amplitude, Phase""")
for x in my_tide.model:
    #print(x)
    print(f'{x[0]}, {x[1]:8.5f}, {x[2]:7.6f}');


