In [1]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import matplotlib
from matplotlib import colors,colorbar
import matplotlib
%matplotlib inline
import csv 
import math
import bq
import time

In [2]:
client = bq.Client.Get()
def Query(q):
    t0 = time.time()
    answer = client.ReadTableRows(client.Query(q)['configuration']['query']['destinationTable'])
    print 'Query time: ' + str(time.time() - t0) + ' seconds.'
    return answer

In [3]:
q = '''
select speed, 
count(*) count 
from 
(SELECT integer(avg(speed)) speed, 
mmsi FROM [pipeline_normalize.20160113] where speed is not null group by mmsi) 
group by speed, order by speed asc
'''
vessel_query = Query(q)


Waiting on bqjob_r3c7d7f7922512cbe_0000015314cbc332_1 ... (1s) Current status: DONE   
Query time: 3.80331301689 seconds.

In [22]:
speeds,counts = [],[]

for row in vessel_query:
    if float(row[0]) < 50 and float(row[0])>0: # no boats go faster than 50 knots for sustained time on the ocean
        speeds.append(float(row[0]))
        counts.append(float(row[1]))
        
y_pos = np.arange(len(speeds))
plt.bar(y_pos, counts, align='center', alpha=0.5)
plt.xticks(y_pos, speeds)
plt.ylabel('Number of Vessels')
plt.title('Average Daily Speed in Knots')
plt.show()



In [26]:
speeds,counts = [],[]

for row in vessel_query:
    if float(row[0])>0: # no boats go faster than 50 knots for sustained time on the ocean
        speeds.append(float(row[0]))
        counts.append(float(row[1]))

speeds = np.array(speeds)
counts = np.array(counts)

for i in range(len(speeds)):
    print int(speeds[i]), counts[0:i+1].sum() /counts.sum()


1 0.132400872942
2 0.246846923448
3 0.353513023269
4 0.449494090639
5 0.530723011082
6 0.601706204121
7 0.664739392909
8 0.719156534308
9 0.761684097157
10 0.801278235978
11 0.843579060737
12 0.884037638523
13 0.912620808888
14 0.930561460193
15 0.942040075957
16 0.950316016212
17 0.956763880622
18 0.961596236148
19 0.965167361052
20 0.966981265765
21 0.968044100558
22 0.968795170479
23 0.969276988918
24 0.969659609444
25 0.971317631721
26 0.971572712071
27 0.971912819205
28 0.972224584077
29 0.972635546864
30 0.972961482867
31 0.973485814698
32 0.974364424794
33 0.974647847405
34 0.975455601848
35 0.975809880112
36 0.97600827594
37 0.976093302724
38 0.976546778902
39 0.976674319077
40 0.976816030383
41 0.976901057166
42 0.97698608395
43 0.977085281864
44 0.977425388998
45 0.977581271434
46 0.977751325001
47 0.977963891959
48 0.978091432134
49 0.978162287787
50 0.978247314571
51 0.979905336848
52 0.980075390415
53 0.980259615112
54 0.980528866593
55 0.981180738599
56 0.981591701386
57 0.982286086784
58 0.982711220701
59 0.983278065924
60 0.983618173058
61 0.983745713233
62 0.983901595669
63 0.98417084715
64 0.984553467676
65 0.98466683672
66 0.984751863504
67 0.984836890287
68 0.984950259332
69 0.985077799507
70 0.985191168551
71 0.985304537596
72 0.985432077771
73 0.985531275685
74 0.985559617946
75 0.985729671513
76 0.986735821784
77 0.98689170422
78 0.987047586656
79 0.98714678457
80 0.98737352266
81 0.987642774141
82 0.988252132755
83 0.988776464586
84 0.98910240059
85 0.989584219029
86 0.989938497293
87 0.99009437973
88 0.990165235382
89 0.99034946008
90 0.990491171386
91 0.99060454043
92 0.990703738344
93 0.990859620781
94 0.990958818695
95 0.990987160956
96 0.992035824618
97 0.992220049316
98 0.992390102882
99 0.992602669841
100 0.992843579061
101 0.992999461497
102 0.999234758949
103 0.99926310121
106 0.999277272341
107 0.999319785733
108 0.999333956863
112 0.999348127994
118 0.999362299124
121 0.999376470255
122 0.999390641385
125 0.999404812516
126 0.999418983647
128 0.999433154777
133 0.999447325908
137 0.999461497038
138 0.999489839299
145 0.99950401043
146 0.999518181561
151 0.999532352691
160 0.999546523822
166 0.999574866083
170 0.999589037213
172 0.999674063997
173 0.999688235127
193 0.999702406258
218 0.999716577389
233 0.999730748519
269 0.99974491965
289 0.99975909078
343 0.999773261911
443 0.999787433041
447 0.999801604172
550 0.999815775303
606 0.999829946433
616 0.999844117564
830 0.999858288694
898 0.999872459825
951 0.999886630955
990 0.999900802086
1021 0.999914973217
1023 1.0

In [ ]: