# Select project
prj_grid = nivapy.da.select_resa_projects(eng)
prj_grid

# Select project
prj_df = prj_grid.get_selected_df()
prj_df

# Get stations for these projects
stn_df = nivapy.da.select_resa_project_stations(prj_df, eng)
print (len(stn_df), 'stations associated with the selected projects.')
stn_df.head()

# Map
nivapy.spatial.quickmap(stn_df, 
                        popup='station_code',
                        aerial_imagery=True)

# Get available parameters
st_dt = '1970-01-01'
end_dt = '2019-01-01'
par_grid = nivapy.da.select_resa_station_parameters(stn_df,
                                                    st_dt,
                                                    end_dt,
                                                    eng)
par_grid

# Select relevant parameters
par_df = par_grid.get_selected_df()
par_df

# Get chem
wc_df, dup_df = nivapy.da.select_resa_water_chemistry(stn_df,
                                                      par_df,
                                                      st_dt,
                                                      end_dt,
                                                      eng,
                                                      drop_dups=True,
                                                      lod_flags=False)
wc_df.head()

# Save for later
wc_df.to_csv('icpw_norway_chem.csv', index=False, encoding='utf-8')

# Load saved data
wc_df = pd.read_csv('icpw_norway_chem.csv', encoding='utf-8')
wc_df['sample_date'] = pd.to_datetime(wc_df['sample_date'])

# Choose site
stn = 'BIE01'

# Resample to monthly for chosen site
df = wc_df.query('station_code == @stn')
df.index = df['sample_date']
df = df.resample('M').mean().reset_index()
df.head()

"""
Explanation: Test Prophet with ICPW data
A quick test of Facebook's Prophet algorithm, as applied to ICPW.
1. Get data
End of explanation
"""


# Get TOC
toc_df = df[['sample_date', 'TOC_mg C/l']].copy()
toc_df.columns = ['ds', 'y']
toc_df.head()

# Fit model
m = Prophet()
m.fit(toc_df)

# Predict the next 10 years, with uncertainty
future = m.make_future_dataframe(periods=120, freq='M')
fcst = m.predict(future)
fig = m.plot(fcst)
a = add_changepoints_to_plot(fig.gca(), m, fcst)
#plt.savefig(stn + '_TOC.png', dpi=300)

fig2 = m.plot_components(fcst)

"""
Explanation: 2. Trends in TOC
End of explanation
"""


# Get SO4
so4_df = df[['sample_date', 'SO4_mg/l']].copy()
so4_df.columns = ['ds', 'y']
so4_df.head()

# Fit model
so4_df['cap'] = so4_df['y'].max()
m = Prophet(growth='logistic')
m.fit(so4_df)

# Predict the next 10 years, with uncertainty
future = m.make_future_dataframe(periods=120, freq='M')
future['cap'] = so4_df['y'].max()
fcst = m.predict(future)
fig = m.plot(fcst)
a = add_changepoints_to_plot(fig.gca(), m, fcst)
plt.savefig(stn + '_SO4.png', dpi=300)

"""
Explanation: The data suggests that TOC increased rapidly from 1990 to around 2000, and then continued to increase more slowly.
3. Trends in SO4
Note: This is just raw SO4 for now (i.e. not corrected in any way).
End of explanation
"""


