Plot energy spectra below time series

master
Dan Howe 5 years ago
parent 75cb3d2192
commit 2f9edb1dff

@ -2,24 +2,28 @@ import io
import os import os
import base64 import base64
import datetime import datetime
import numpy as np
import pandas as pd import pandas as pd
import webbrowser as wb import webbrowser as wb
import dash import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc import dash_core_components as dcc
import dash_html_components as html import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.plotly as py import plotly.plotly as py
import plotly.graph_objs as go import plotly.graph_objs as go
import wafo.objects as wo
app = dash.Dash() app = dash.Dash()
app.title = 'daqviewer' app.title = 'daqviewer'
app.scripts.config.serve_locally = True app.scripts.config.serve_locally = True
app.layout = html.Div([ app.layout = html.Div([
dcc.Upload( dcc.Upload(
id='upload-data', id='upload-data',
children=html.Div([html.A('Drag and drop csv files, or click to select.')]), children=html.Div(
[html.A('Drag and drop csv files, or click to select.')]),
style={ style={
'width': '99%', 'width': '99%',
'height': '60px', 'height': '60px',
@ -45,8 +49,7 @@ def parse_contents(contents, filename, date):
inst_type = basename.split('_')[-1] inst_type = basename.split('_')[-1]
try: try:
if inst_type == 'WP': if inst_type == 'WP':
df = pd.read_csv( df = pd.read_csv(io.StringIO(decoded.decode('utf-8')),
io.StringIO(decoded.decode('utf-8')),
index_col=0, index_col=0,
header=5, header=5,
skiprows=[6]) skiprows=[6])
@ -60,8 +63,7 @@ def parse_contents(contents, filename, date):
col_names[i + j] = '{}-{}'.format(col, suffix) col_names[i + j] = '{}-{}'.format(col, suffix)
df.columns = col_names df.columns = col_names
else: else:
df = pd.read_csv( df = pd.read_csv(io.StringIO(decoded.decode('utf-8')),
io.StringIO(decoded.decode('utf-8')),
index_col=0, index_col=0,
header=3, header=3,
skiprows=[4]) skiprows=[4])
@ -73,19 +75,46 @@ def parse_contents(contents, filename, date):
# Zero time series based on first 5s # Zero time series based on first 5s
df -= df[:5].mean() df -= df[:5].mean()
data = [] ts = []
for col in df.columns: for col in df.columns:
trace = go.Scatter(x=df.index, y=df[col], name=col, opacity=0.8) trace = go.Scatter(x=df.index, y=df[col], name=col, opacity=0.8)
data.append(trace) ts.append(trace)
layout = dict(title=basename, xaxis=dict(rangeslider=dict())) layout = dict(title=basename, xaxis=dict(rangeslider=dict()))
fig = dict(data=data, layout=layout) timeseries = dict(data=ts, layout=layout)
# Specify wave statistics
var = [
'Hm0', 'Tm01', 'Tm02', 'Tm24', 'Tp', 'Ss', 'Sp', 'Ka', 'Tp1', 'alpha',
'eps2', 'eps4'
]
spec = []
for col in df.columns:
t = df.index.values[:, np.newaxis]
x = df[[col]].values
# Get wave statistics
xx = wo.mat2timeseries(np.hstack([t, x]))
S = xx.tospecdata()
S.freqtype = 'f'
values, _, keys = S.characteristic(var)
# Plot energy spectrum
trace = go.Scatter(x=S.args, y=S.data, name=col, opacity=0.8)
spec.append(trace)
energy = dict(data=spec)
elements = html.Div([
dcc.Graph(id='time-series', figure=timeseries),
dcc.Graph(id='energy-spectrum', figure=energy)
])
return html.Div([dcc.Graph(id='my-graph', figure=fig)]) return elements
@app.callback( @app.callback(Output('output-data-upload', 'children'), [
Output('output-data-upload', 'children'), [
Input('upload-data', 'contents'), Input('upload-data', 'contents'),
Input('upload-data', 'filename'), Input('upload-data', 'filename'),
Input('upload-data', 'last_modified') Input('upload-data', 'last_modified')
@ -102,7 +131,7 @@ def update_output(list_of_contents, list_of_names, list_of_dates):
def main(): def main():
port = 8050 port = 8050
wb.open('http://localhost:{}'.format(port)) wb.open('http://localhost:{}'.format(port))
app.run_server(port=port, debug=False) app.run_server(port=port, debug=True)
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save