%matplotlib inline
import pandas as pd
from math import pi
from bokeh.models import ColumnDataSource
from bokeh.models import HoverTool
from bokeh.plotting import figure, output_file, show
from bokeh.models import LinearAxis, Range1d
from bokeh.layouts import gridplot
from scipy.signal import detrend
print("# pandas version = ",pd.__version__)
import bokeh as bk
print("# bokeh version = ",bk.__version__)
import scipy as sp
print("# scipy version = ",sp.__version__)
# ftp directory
donet_dir = "http://ncedc.org/ftp/outgoing/taira/DONET"
# sts1v data
kis_sts1v_fi = donet_dir + "/kis_sts1v_0.00001-0.01hz.300sps.acc.ts.wh.out"
print(kis_sts1v_fi)
# pfo volumetric strain data. assuming poisson's ratio = 0.25
kis_evolume_fi = donet_dir + "/ez_volume_nu0.25_.ts.wh.out"
print(kis_evolume_fi)
# pfo gravitatity data
kis_grav_fi = donet_dir + "/KIS.grav.out.ts.wh.out"
# read file
# format
# time acc (nano m/s*2)
#2009-7-14T0:0:00 -53298
kis_sts1v =pd.read_csv(kis_sts1v_fi,
sep=" ",names=["time", "acc_nano"],
header=None, skiprows=1)
#print(hhz_psd_ts)
# convert "time" to datetime. need for plot
kis_sts1v['time'] = pd.to_datetime(kis_sts1v['time'])
len(kis_sts1v)
# check
kis_sts1v.head()
# check
kis_sts1v.tail()
# PFO strain data
# time vol ez0 (az=0deg) ez90 (az=90deg)
kis_evolume =pd.read_csv(kis_evolume_fi,
sep=",",names=["time", "vol","ez0","ez90"],
header=None, skiprows=1)
len(kis_evolume)
kis_evolume.head()
# time format conversion
kis_evolume['time'] = pd.to_datetime(kis_evolume['time'])
# PFO gravity data
# time gravity microgal, positive for decreasing
# microgal = microgal2. same data
kis_grav =pd.read_csv(kis_grav_fi,
sep=" ",names=["time", "microgal", "microgal2"],
header=None, skiprows=1)
len(kis_grav)
# microgal2 is Nan.... Not sure why but microgal2 is not used
kis_grav.head()
# time format conversion
kis_grav['time'] = pd.to_datetime(kis_grav['time'])
# check
kis_grav['time'].head()
# trim data
#kis_grav_select = kis_grav[("2016-01-01 00:00:00" <= kis_grav['time']) & (kis_grav['time'] <= "2016-04-01 00:00:00")]
kis_grav_select = kis_grav
# check
kis_grav_select.head()
# trim daat
#kis_evolume_select = kis_evolume[ ("2016-01-01 00:00:00" <= kis_evolume['time']) &
# (kis_evolume['time'] <= "2016-04-01 00:00:00") ]
kis_evolume_select=kis_evolume
# check
kis_evolume_select.head()
# dataframe to numpy matrix
evolume_matrix = kis_evolume_select.as_matrix()
# check to see evol
evolume_matrix[:,1] # 0 time, 1 evol # 2 ez0 #3 ez90
len(evolume_matrix)
len(kis_sts1v)
len(kis_evolume_select)
# now add evol data as pfo_sts1v['vol']
kis_sts1v['vol'] =evolume_matrix[:,1]
# check
kis_sts1v['vol'].head()
# check
kis_sts1v['time'].head()
# dataframe to numpy matrix
grav_matrix = kis_grav_select.as_matrix()
# check
grav_matrix[:,1] # microgal
# now add microgal data as pfo_sts1v['microgal']
kis_sts1v['microgal'] = grav_matrix[:,1]
# check
kis_sts1v['microgal'].head()
#TOOLS = "pan,wheel_zoom,box_zoom,undo,reset,save"
TOOLS = "pan,box_zoom,undo,reset,save"
#data = {'x_values': [1, 2, 3, 4, 5],
# 'y_values': [6, 7, 2, 3, 6]}
# make "data". all parameters need to be added
data = {
'time': kis_sts1v['time'],
'acc_mili': kis_sts1v['acc_nano']/1000/1000,
'microgal': kis_sts1v['microgal'],
'vol': kis_sts1v['vol'],
#'time': kis_evolume['time'],
}
# make source. needed
source = ColumnDataSource(data=data)
# x_axis is datetime. add tools. define size and add title
#p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=600, height=600, title = "kis")
p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1200, height=600, title = "KIS")
# labels
p.xaxis.axis_label = 'Time (UTC)'
p.yaxis.axis_label = 'Acceleration (mm/s*2) '
# label orientation
p.xaxis.major_label_orientation = pi/4
# grid line
p.grid.grid_line_alpha=1.0
# define Y-range
bottom, top = -5, 5
p.y_range=Range1d(bottom, top)
# plot STS1-v data
p.line(x='time', y='acc_mili', source=source,legend="STS1v")
# another Y-axis (right side)
p.extra_y_ranges = {"y2": Range1d(start=-160, end=60)}
# another Y-axis label
p.add_layout(LinearAxis(y_range_name="y2", axis_label='Nanostrain (positive extension)'), 'right')
# plot volumetric strain data with y2 axis
p.line(x='time', y='vol', y_range_name="y2", source=source,legend="Volumetric strain",line_color="black")
# font and etc
p.title.text_font="calibri"
p.title.text_font_size="20pt"
p.title.align="center"
p.xaxis.axis_label_text_font_size = "16pt"
p.xaxis.axis_label_text_font="calibri"
p.xaxis.major_label_text_font_size = "14pt"
p.xaxis.major_label_text_font="calibri"
p.yaxis.axis_label_text_font_size = "16pt"
p.yaxis.axis_label_text_font="calibri"
p.yaxis.major_label_text_font_size = "14pt"
p.yaxis.major_label_text_font="calibri"
p.xaxis.axis_label_text_font_style = 'normal'
p.yaxis.axis_label_text_font_style = 'normal'
p.xaxis[0].formatter.days = '%m/%d/%Y'
# save .html file
output_file("kis_ts.html", title="KIS")
# if you want to plot only "p"
#show(p) # open a browser
# two figure with "vertical"
#grid = gridplot([[p], [p2]])
# two figure with "horizontal"
#grid = gridplot([[p,p2]])
grid = gridplot([[p]])
# will open a browser
show(grid)