Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

import pandas as pd 

import numpy as np 

import datetime 

 

# plotting libraries 

from plotly.offline import init_notebook_mode, iplot 

import plotly.graph_objs as go 

import matplotlib.pyplot as plt 

 

# spatial libraries  

import pyproj 

import shapely.geometry 

from simplekml import Kml, Style 

 

import networkx as nx 

 

def vessel_planning(vessels, activities, colors, web=False): 

"""create a plot of the planning of vessels""" 

 

def get_segments(series, activity, y_val): 

"""extract 'start' and 'stop' of activities from log""" 

x = [] 

y = [] 

for i, v in series.iteritems(): 

if v == activity + ' start': 

start = i 

if v == activity + ' stop': 

x.extend((start, start, i, i, i)) 

y.extend((y_val, y_val, y_val, y_val, None)) 

return x, y 

 

# organise logdata into 'dataframes'  

dataframes = [] 

for vessel in vessels: 

df = pd.DataFrame( 

{'log_value': vessel.log["Value"], 'log_string': vessel.log["Message"]}, vessel.log["Timestamp"]) 

dataframes.append(df) 

df = dataframes[0] 

 

# prepare traces for each of the activities 

traces = [] 

for i, activity in enumerate(activities): 

x_combined = [] 

y_combined = [] 

for k, df in enumerate(dataframes): 

y_val = vessels[k].name 

x, y = get_segments( 

df['log_string'], activity=activity, y_val=y_val) 

x_combined.extend(x) 

y_combined.extend(y) 

traces.append(go.Scatter( 

name=activity, 

x=x_combined, 

y=y_combined, 

mode='lines', 

hoverinfo='y+name', 

line=dict(color=colors[i], width=10), 

connectgaps=False)) 

 

# prepare layout of figure 

layout = go.Layout( 

title='Vessel planning', 

hovermode='closest', 

legend=dict(x=0, y=-.2, orientation="h"), 

xaxis=dict( 

title='Time', 

titlefont=dict( 

family='Courier New, monospace', 

size=18, 

color='#7f7f7f'), 

range=[0, vessel.log["Timestamp"][-1]]), 

yaxis=dict( 

title='Vessels', 

titlefont=dict( 

family='Courier New, monospace', 

size=18, 

color='#7f7f7f'))) 

 

# plot figure 

init_notebook_mode(connected=True) 

fig = go.Figure(data=traces, layout=layout) 

return iplot(fig, filename='news-source') 

 

def vessel_kml(env, vessels, 

fname='vessel_movements.kml', 

icon='http://maps.google.com/mapfiles/kml/shapes/donut.png', 

size=1, 

scale=1, 

stepsize=120): 

"""Create a kml visualisation of vessels. Env variable needs to contain  

epoch to enable conversion of simulation time to real time. Vessels need 

logs that contain geometries in lat, lon as a function of time.""" 

 

# create a kml file containing the visualisation 

kml = Kml() 

fol = kml.newfolder(name="Vessels") 

 

shared_style = Style() 

shared_style.labelstyle.color = 'ffffffff' # White 

shared_style.labelstyle.scale = size 

shared_style.iconstyle.color = 'ffff0000' # Blue 

shared_style.iconstyle.scale = scale 

shared_style.iconstyle.icon.href = icon 

 

# each timestep will be represented as a single point 

for vessel in vessels: 

geom_x = [] 

geom_y = [] 

 

for geom in vessel.log["Geometry"]: 

geom_x.append(geom.x) 

geom_y.append(geom.y) 

 

vessel.log["Geometry - x"] = geom_x 

vessel.log["Geometry - y"] = geom_y 

 

time_stamp_min = min(vessel.log["Timestamp"]).timestamp() 

time_stamp_max = max(vessel.log["Timestamp"]).timestamp() 

 

steps = int(np.floor((time_stamp_max - time_stamp_min) / stepsize)) 

timestamps_t = np.linspace(time_stamp_min, time_stamp_max, steps) 

 

times = [] 

for t in vessel.log["Timestamp"]: 

times.append(t.timestamp()) 

 

vessel.log["timestamps_t"] = timestamps_t 

vessel.log["timestamps_x"] = np.interp(timestamps_t, times, vessel.log["Geometry - x"]) 

vessel.log["timestamps_y"] = np.interp(timestamps_t, times, vessel.log["Geometry - y"]) 

 

for log_index, value in enumerate(vessel.log["timestamps_t"][:-1]): 

 

begin = datetime.datetime.fromtimestamp(vessel.log["timestamps_t"][log_index]) 

end = datetime.datetime.fromtimestamp(vessel.log["timestamps_t"][log_index + 1]) 

 

pnt = fol.newpoint(name=vessel.name, coords=[(vessel.log["timestamps_x"][log_index], vessel.log["timestamps_y"][log_index])]) 

pnt.timespan.begin = begin.isoformat() 

pnt.timespan.end = end.isoformat() 

pnt.style = shared_style 

 

# include last point as well 

begin = datetime.datetime.fromtimestamp(vessel.log["timestamps_t"][log_index + 1]) 

end = datetime.datetime.fromtimestamp(vessel.log["timestamps_t"][log_index + 1]) 

 

pnt = fol.newpoint(name=vessel.name, coords=[(vessel.log["timestamps_x"][log_index + 1], vessel.log["timestamps_y"][log_index + 1])]) 

pnt.timespan.begin = begin.isoformat() 

pnt.timespan.end = end.isoformat() 

pnt.style = shared_style 

 

kml.save(fname) 

 

def graph_kml(env, 

fname='graph.kml', 

icon='http://maps.google.com/mapfiles/kml/shapes/donut.png', 

size=0.5, 

scale=0.5, 

width=5): 

"""Create a kml visualisation of graph. Env variable needs to contain  

graph.""" 

 

# create a kml file containing the visualisation 

kml = Kml() 

fol = kml.newfolder(name="Vessels") 

 

shared_style = Style() 

shared_style.labelstyle.color = 'ffffffff' # White 

shared_style.labelstyle.scale = size 

shared_style.iconstyle.color = 'ffffffff' # White 

shared_style.iconstyle.scale = scale 

shared_style.iconstyle.icon.href = icon 

shared_style.linestyle.color = 'ff0055ff' # Red 

shared_style.linestyle.width = width 

 

nodes = list(env.FG.nodes) 

 

# each timestep will be represented as a single point 

for log_index, value in enumerate(list(env.FG.nodes)[0:-1-1]): 

 

pnt = fol.newpoint(name='', 

coords=[(nx.get_node_attributes(env.FG, "Geometry")[nodes[log_index]].x, 

nx.get_node_attributes(env.FG, "Geometry")[nodes[log_index]].y)]) 

pnt.style = shared_style 

 

edges = list(env.FG.edges) 

for log_index, value in enumerate(list(env.FG.edges)[0:-1-1]): 

 

lne = fol.newlinestring(name='', 

coords = [(nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][0]].x, 

nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][0]].y), 

(nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][1]].x, 

nx.get_node_attributes(env.FG, "Geometry")[edges[log_index][1]].y)]) 

lne.style = shared_style 

 

kml.save(fname) 

 

def energy_use(vessel, testing = False): 

energy_use_loading = 0 # concumption between loading start and loading stop 

energy_use_sailing_full = 0 # concumption between sailing full start and sailing full stop 

energy_use_unloading = 0 # concumption between unloading start and unloading stop 

energy_use_sailing_empty = 0 # concumption between sailing empty start and sailing empty stop 

energy_use_waiting = 0 # concumption between waiting start and waiting stop 

 

for i in range(len(vessel.log["Message"])): 

if vessel.log["Message"][i] == "Energy use loading": 

energy_use_loading += vessel.log["Value"][i] 

 

elif vessel.log["Message"][i] == "Energy use sailing full": 

energy_use_sailing_full += vessel.log["Value"][i] 

 

elif vessel.log["Message"][i] == "Energy use unloading": 

energy_use_unloading += vessel.log["Value"][i] 

 

elif vessel.log["Message"][i] == "Energy use sailing empty": 

energy_use_sailing_empty += vessel.log["Value"][i] 

 

elif vessel.log["Message"][i] == "Energy use waiting": 

energy_use_waiting += vessel.log["Value"][i] 

 

# For the total plot 

fig, ax1 = plt.subplots(figsize = [15, 10]) 

 

# For the barchart 

height = [energy_use_loading, 

energy_use_unloading, 

energy_use_sailing_full, 

energy_use_sailing_empty, 

energy_use_waiting] 

labels = ["Loading", 

"Unloading", 

"Sailing full", 

"Sailing empty", 

"Waiting"] 

colors = [(55/255,126/255,184/255), 

(98/255, 192/255, 122/255), 

(255/255,150/255,0/255), 

(98/255, 141/255, 122/255), 

(124/255, 10/255, 2/255)] 

 

positions = np.arange(len(labels)) 

ax1.bar(positions, height, color = colors) 

 

# For the cumulative percentages 

total_use = sum([energy_use_loading, 

energy_use_unloading, 

energy_use_sailing_full, 

energy_use_sailing_empty, 

energy_use_waiting]) 

 

energy_use_unloading += energy_use_loading 

energy_use_sailing_full += energy_use_unloading 

energy_use_sailing_empty += energy_use_sailing_full 

energy_use_waiting += energy_use_sailing_empty 

y = [energy_use_loading, 

energy_use_unloading, 

energy_use_sailing_full, 

energy_use_sailing_empty, 

energy_use_waiting] 

n = [energy_use_loading / total_use, 

energy_use_unloading / total_use, 

energy_use_sailing_full / total_use, 

energy_use_sailing_empty / total_use, 

energy_use_waiting / total_use,] 

 

ax1.plot(positions, y, 'ko', markersize=10) 

ax1.plot(positions, y, 'k') 

 

for i, txt in enumerate(n): 

x_txt = positions[i] + 0.1 

y_txt = y[i] * 0.95 

ax1.annotate("{:02.1f}%".format(txt * 100), 

(x_txt, y_txt), size = 12) 

 

# Further markup 

plt.ylabel("Energy useage in KWH", size = 12) 

ax1.set_xticks(positions) 

ax1.set_xticklabels(labels, size = 12) 

plt.title("Energy use - {}".format(vessel.name), size = 15) 

 

if testing == False: 

plt.show()