# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:py38] *
#     language: python
#     name: conda-env-py38-py
# ---

# # Mooring Export -> Excel (each column/page is an instrument)

# +
from erddapy import ERDDAP
import pandas as pd
import numpy as np

server_url = 'http://ecofoci-field.pmel.noaa.gov:8080/erddap'

d = ERDDAP(server=server_url,
           protocol='tabledap',
           response='csv',
          )

d.dataset_id='datasets_Mooring_19bsm2a_preliminary'

d.variables =  [
 'timeseries_id',
 'temperature',
 'depth',
 'time',
 # 'Instrument_Identifier'
]


# +
df = d.to_pandas(
    index_col='time (UTC)',
    parse_dates=True,
).dropna()

df.columns = [x[1].split()[0] for x in enumerate(df.columns)]

# -

df

# + jupyter={"outputs_hidden": true} tags=[]
### for prawler export
df['samplenum'] = df.groupby('profile_id').cumcount()

dft = df.pivot(index='samplenum',columns='profile_id', values='Salinity (PSU)')

df.reset_index()

import openpyxl
with pd.ExcelWriter('19bs2c.xlsx') as writer:
    df.tz_localize(None).reset_index().pivot(index='samplenum',columns='profile_id', values='time (UTC)').to_excel(writer, sheet_name='Time')
    df.pivot(index='samplenum',columns='profile_id', values='Salinity (PSU)').to_excel(writer, sheet_name='Salinity')
    df.pivot(index='samplenum',columns='profile_id', values='depth (m)').to_excel(writer, sheet_name='depth')

# -

#all depths
import openpyxl
with pd.ExcelWriter('19bsm2a.xlsx') as writer:
    for instrument,dfg in df.groupby('timeseries_id'):
        dfg.tz_localize(None).to_excel(writer, sheet_name=instrument)

# ### Occasionally a filtered dataset is more appropriate so run the filter then export

import EcoFOCIpy.math.lanzcos as lanzcos #<- instrument specific

dfm= df.resample('1H').mean()
dfm['filt35hr'] = lanzcos.lanzcos(dfm['temperature'].values,1,35) + df['temperature'].mean()

dfm[['temperature','filt35hr']].plot()
dfm['filt35hr'][(dfm.index.hour == 0) | (dfm.index.hour == 6) | (dfm.index.hour == 12) | (dfm.index.hour == 18)].plot(marker='.',markersize=.1)

import openpyxl
with pd.ExcelWriter('19bsm2a_f35.xlsx') as writer:
    for instrument,dfg in df.groupby('timeseries_id'):
        dfm= dfg.resample('1H').mean()
        dfm['filt35hr'] = lanzcos.lanzcos(dfm['temperature'].values,1,35) + df['temperature'].mean()
        dfm['filt35hr'][(dfm.index.hour == 0) | (dfm.index.hour == 6) | (dfm.index.hour == 12) | (dfm.index.hour == 18)].tz_localize(None).to_excel(writer, sheet_name=instrument)


import openpyxl
with pd.ExcelWriter('19bsm2a_f35_3hr.xlsx') as writer:
    for instrument,dfg in df.groupby('timeseries_id'):
        if instrument in ['19bsm2a_sc_0006m']:
            dfm= dfg.resample('1H').mean().interpolate()
            dfm['filt35hr'] = lanzcos.lanzcos(dfm['temperature'].values,1,35) + df['temperature'].mean()
            dfm['filt35hr'][(dfm.index.hour == 0) | (dfm.index.hour == 6) | (dfm.index.hour == 12) | (dfm.index.hour == 18)].tz_localize(None).to_excel(writer, sheet_name=instrument)


for instrument,dfg in df.groupby('timeseries_id'):
    print(instrument)



StarcoderdataJupyterScriptsDedupFiltered