Repository navigation
Expand file tree
/
Copy pathplotting_functions.py
More file actions
104 lines (79 loc) · 3.78 KB
/
Copy pathplotting_functions.py
File metadata and controls
104 lines (79 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import numpy as np
import pandas as pd
import cftime
import plotly.express as px
import plotly.graph_objects as go
import colorcet as cc
import sqlite3
import os
## Color Palette ############
color_dark_1 = '#1d191e'
color_dark_2 = '#1f2138'
color_dark_3 = '#101125'
color_light_1 = '#a4967a'
color_light_2 = '#bfb39b'
#############################
def get_cbar_range(data, force_range=None):
min_data_value = np.nanmin(data)
max_data_value = np.nanmax(data)
if not (force_range is None):
mid_data_value = np.nanmedian(data)
min_data_value = mid_data_value - 0.5*force_range
max_data_value = mid_data_value + 0.5*force_range
return (min_data_value, max_data_value)
def backward_diff(x):
dx = 0.0*x
dx[1:] = x[1:] - x[:-1]
dx[0] = dx[1]
return dx
def create_cruise_track_trace(lon, lat, fn):
## Figure out field campaign name.
campaign_name = fn[0].split('/')[0]
## Find Break Points: Where a new cruise (or cruise leg) has clearly started.
dlon = backward_diff(lon)
dlat = backward_diff(lat)
dlatlon = np.sqrt(np.power(dlon,2) + np.power(dlat,2))
break_point_indices = [0] + [x for x in range(len(dlatlon)) if dlatlon[x] > 1.0] + [len(dlatlon)]
tracks = []
for ii in range(len(break_point_indices)-1):
tracks += [go.Scattermapbox(lon=lon[break_point_indices[ii]:break_point_indices[ii+1]],
lat=lat[break_point_indices[ii]:break_point_indices[ii+1]],
mode='lines', line={'width':2.0, 'color':'white'}, name=campaign_name,
showlegend=False, hoverinfo='skip')]
return tracks
def create_data_markers_trace(lon, lat, T, Z, label, fn, skip=10):
## Figure out field campaign name.
campaign_name = fn[0].split('/')[0]
campaign_names = [x.split('/')[0] for x in fn]
cmin,cmax = get_cbar_range(Z)
if 'SST' in label:
cmap = 'jet'
elif 'WSPD' in label:
cmap = cc.CET_L20
else:
cmap = cc.CET_R1
return go.Scattermapbox(lon=lon[::skip], lat=lat[::skip],
marker=dict(color=Z[::skip], cmin=cmin, cmax=cmax, colorscale=cmap,
colorbar=dict(len=0.6, title=label, y=0.99, yanchor='top', x=0.99, xanchor='right',bgcolor=color_light_2)),
hovertemplate = '%{text}<br>lon: %{lon:.2f}<br>lat: %{lat:.2f}<br>'+label+': %{marker.color:.2f}<extra></extra>',
text= campaign_names[::skip],
showlegend=False)
def add_ndbc():
fn = 'data/activestations.xml'
df = pd.read_xml(fn)
# df = df[df['met'] != 'n'] # Tried to subset by met, but no success.
marker_sizes = 7+0.0*df['lon'].values
marker_symbols = ["star" for x in range(len(df['lon']))]
return go.Scattermapbox(lon=df['lon'].values, lat=df['lat'].values, text=df['name'].values,
marker = {'size': marker_sizes, 'symbol': marker_symbols, 'allowoverlap':True},
hovertemplate = '%{text}<extra></extra>')
def add_grid_lines(fig, dx=5, width=0.3, color='grey'):
for y in np.arange(-80,80+dx,dx):
fig.add_trace(go.Scattermapbox(lon=np.array([0,360]),lat=np.array([y,y]),
mode='lines', line={'width':width, 'color':color}, showlegend=False, legendgroup='llgrid',hoverinfo='skip'))
for y in [0.0,]:
fig.add_trace(go.Scattermapbox(lon=np.array([0,360]),lat=np.array([y,y]),
mode='lines', line={'width':width, 'color':color}, showlegend=False, legendgroup='llgrid',hoverinfo='skip'))
for x in np.arange(0,360+dx,dx):
fig.add_trace(go.Scattermapbox(lon=np.array([x,x]),lat=np.array([-90,90]),
mode='lines', line={'width':width, 'color':color}, showlegend=False, legendgroup='llgrid',hoverinfo='skip'))