Download Vessel Counts by Day


In [2]:
import time
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib import colors,colorbar
%matplotlib inline
import csv 
import math
from math import radians, cos, sin, asin, sqrt
from scipy import stats
import math
import cPickle

In [5]:
import bq  
client = bq.Client.Get()

In [6]:
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 [12]:
def query_date(thedate):
    q = '''
    SELECT
      integer(FLOOR(first_lat*10)) lat_bin,
      integer(FLOOR(first_lon*10)) lon_bin,
      integer(FLOOR(avg_lat*10)) lat_bin_avg,
      integer(FLOOR(avg_lon*10)) lon_bin_avg,
      satellite_positions sat_positions,
      terrestrial_positions terrestrial_positions,
      positions_weighted,
      avg_speed,
      slow_pings,
      mmsi
    FROM
    (SELECT
      mmsi,
      SUM( CASE WHEN speed = 0 OR (speed<=2 AND type IN (18, 19)) THEN 180 
         WHEN (speed > 0 AND speed <14 AND type IN (1,2,3)AND turn = 0 )
             OR (speed>2 AND type IN (18,19)) THEN 10 
          when speed>0 and speed<14 and type in (1,2,3) and turn !=0 then 3.3
          when speed>=14 and speed<23 and type in (1,2,3) and turn = 0 then 6
          when type in (1,2,3) and (speed>=23 or (speed>=14 and turn !=0)) then 2
          END) positions_weighted,
       first(lat) first_lat,
       first(lon) first_lon,
       avg(lat) avg_lat,
       avg(lon) avg_lon,
       max(lat) max_lat,
       min(lat) min_lat,
       max(lon) max_lon,
       min(lon) min_lon,
       avg(speed) avg_speed,
       sum(if( (speed=0 and type in (1,2,3)) or (speed<2 and type in (18,19)),1,0 )) slow_pings,
       sum( if(REGEXP_REPLACE(tagblock_station, 'u', '') IN ('rORBCOMM000',
            'rORBCOMM01',
            'rORBCOMM008',
            'rORBCOMM009',
            'rORBCOMM010'),1,0)) terrestrial_positions,
       sum( if(REGEXP_REPLACE(tagblock_station, 'u', '') not IN ('rORBCOMM000',
            'rORBCOMM01',
            'rORBCOMM008',
            'rORBCOMM009',
            'rORBCOMM010'),1,0)) satellite_positions,       
    FROM
      [pipeline_normalize.'''+thedate+''']
    WHERE
      type IN (1,2,3,18,19) and lat is not null and lon is not null and speed is not null and turn is not null
    group by mmsi
    )
      where
      max_lat - min_lat <5
      AND (max_lon - min_lon < 10
        OR first_lon > 170
        OR first_lon < -170)
      AND mmsi IN (select mmsi from
    [scratch_david_gapanalysis.good_mmsi_2015_1000pings])
    '''

    positions = Query(q)
    cPickle.dump(positions, open('../../data/density/'+thedate+'_v2_vessels.p', 'wb'))

In [15]:
for i in range(1,32):
    thedate = "201503"
    if i<10:
        thedate += "0"+str(i)
    else:
        thedate += str(i)
    print thedate
    query_date(thedate)


20150301
Waiting on bqjob_rc5d1d36a0673555_00000153a58cea75_85 ... (14s) Current status: DONE   
Query time: 34.4649701118 seconds.
20150302
Waiting on bqjob_r129120c3ab62cee1_00000153a58d78fb_86 ... (15s) Current status: DONE   
Query time: 52.4378290176 seconds.
20150303
Waiting on bqjob_r1fd315b1681a4fad_00000153a58e54d4_87 ... (15s) Current status: DONE   
Query time: 46.6302821636 seconds.
20150304
Waiting on bqjob_r50de679d8e60065a_00000153a58f19ae_88 ... (23s) Current status: DONE   
Query time: 59.0982401371 seconds.
20150305
Waiting on bqjob_r4240430d55b85765_00000153a5900f9c_89 ... (15s) Current status: DONE   
Query time: 47.1999361515 seconds.
20150306
Waiting on bqjob_r2e32496f8d58ab02_00000153a590d7dc_90 ... (14s) Current status: DONE   
Query time: 47.9087018967 seconds.
20150307
Waiting on bqjob_r4b5766a43c5a75f1_00000153a591a1c4_91 ... (14s) Current status: DONE   
Query time: 45.9500920773 seconds.
20150308
Waiting on bqjob_recf75325fc14252_00000153a5926412_92 ... (9s) Current status: DONE   
Query time: 39.3963389397 seconds.
20150309
Waiting on bqjob_r547ae8324c4e437b_00000153a5930c50_93 ... (15s) Current status: DONE   
Query time: 45.863850832 seconds.
20150310
Waiting on bqjob_r2213d2b35262c115_00000153a593ce07_94 ... (9s) Current status: DONE   
Query time: 41.4206161499 seconds.
20150311
Waiting on bqjob_r63f4d640acfe9c44_00000153a5947f6c_95 ... (15s) Current status: DONE   
Query time: 48.2355818748 seconds.
20150312
Waiting on bqjob_r3cba61b0d9b2e1ed_00000153a5954af7_96 ... (14s) Current status: DONE   
Query time: 50.681634903 seconds.
20150313
Waiting on bqjob_r29e67e552a1282e0_00000153a5961fd3_97 ... (9s) Current status: DONE   
Query time: 41.1331131458 seconds.
20150314
Waiting on bqjob_r1cdaf22456f938ab_00000153a596ce38_98 ... (14s) Current status: DONE   
Query time: 46.5095090866 seconds.
20150315
Waiting on bqjob_r511c3b29f5a7812c_00000153a59793c1_99 ... (9s) Current status: DONE   
Query time: 42.8878390789 seconds.
20150316
Waiting on bqjob_r3909d21ca7cc0b68_00000153a5984898_100 ... (9s) Current status: DONE   
Query time: 40.8973751068 seconds.
20150317
Waiting on bqjob_r1c2ee0975e833c77_00000153a598f6cc_101 ... (9s) Current status: DONE   
Query time: 41.1956939697 seconds.
20150318
Waiting on bqjob_rd71bbf6431d4a12_00000153a599a811_102 ... (23s) Current status: DONE   
Query time: 55.6452720165 seconds.
20150319
Waiting on bqjob_r5e31245da9309b94_00000153a59a9387_103 ... (14s) Current status: DONE   
Query time: 44.7022919655 seconds.
20150320
Waiting on bqjob_r64eea6c8034330ab_00000153a59b5476_104 ... (9s) Current status: DONE   
Query time: 39.9983961582 seconds.
20150321
Waiting on bqjob_r1926863dd570874d_00000153a59c06bd_105 ... (23s) Current status: DONE   
Query time: 53.2937009335 seconds.
20150322
Waiting on bqjob_r415ee3f2ba13943e_00000153a59ced1c_106 ... (23s) Current status: DONE   
Query time: 60.2540500164 seconds.
20150323
Waiting on bqjob_r2f2aa7e44fd42dcb_00000153a59e025b_107 ... (15s) Current status: DONE   
Query time: 54.9108281136 seconds.
20150324
Waiting on bqjob_r7174dc15140ac735_00000153a59f07ce_108 ... (10s) Current status: DONE   
Query time: 47.5314400196 seconds.
20150325
Waiting on bqjob_r1d00a8fe0604f6ce_00000153a59ff505_109 ... (15s) Current status: DONE   
Query time: 54.947081089 seconds.
20150326
Waiting on bqjob_r79f4b3d3bdf6e616_00000153a5a10ed1_110 ... (15s) Current status: DONE   
Query time: 56.4082648754 seconds.
20150327
Waiting on bqjob_r5c3af3c53381e63f_00000153a5a21e85_111 ... (10s) Current status: DONE   
Query time: 50.2303299904 seconds.
20150328
Waiting on bqjob_r3d4a7b70ab7c1c8c_00000153a5a324cf_112 ... (15s) Current status: DONE   
Query time: 72.321543932 seconds.
20150329
Waiting on bqjob_r6c6d6b7cfee9af6b_00000153a5a497aa_113 ... (15s) Current status: DONE   
Query time: 60.1116950512 seconds.
20150330
Waiting on bqjob_r46a832e11d1f3ec8_00000153a5a5bdf4_114 ... (15s) Current status: DONE   
Query time: 54.5713460445 seconds.
20150331
Waiting on bqjob_r588477d7222997e_00000153a5a6cdf3_115 ... (15s) Current status: DONE   
Query time: 55.8961420059 seconds.

In [3]:
import csv
for i in range(1,32):
    thedate = "201501"
    if i<10:
        thedate += "0"+str(i)
    else:
        thedate += str(i)
    print thedate   
    positions = cPickle.load(open('../../data/density/'+thedate+'_v2_vessels.p', 'rb'))
    with open('../../data/density/'+thedate+'_v2_vessels.csv', 'wb') as f:
        writer = csv.writer(f)
        writer.writerow(["lat_bin","lon_bin","lat_bin_avg","lon_bin_avg","sat_positions",
                         "terrestrial_positions","positions_weighted",
                        "avg_speed","slow_pings","mmsi"])
        writer.writerows(positions)


20150101
20150102
20150103
20150104
20150105
20150106
20150107
20150108
20150109
20150110
20150111
20150112
20150113
20150114
20150115
20150116
20150117
20150118
20150119
20150120
20150121
20150122
20150123
20150124
20150125
20150126
20150127
20150128
20150129
20150130
20150131

In [5]:
for i in range(1,29):
    thedate = "201502"
    if i<10:
        thedate += "0"+str(i)
    else:
        thedate += str(i)
    print thedate   
    positions = cPickle.load(open('../../data/density/'+thedate+'_v2_vessels.p', 'rb'))
    with open('../../data/density/'+thedate+'_v2_vessels.csv', 'wb') as f:
        writer = csv.writer(f)
        writer.writerow(["lat_bin","lon_bin","lat_bin_avg","lon_bin_avg","sat_positions",
                         "terrestrial_positions","positions_weighted",
                        "avg_speed","slow_pings","mmsi"])
        writer.writerows(positions)


20150201
20150202
20150203
20150204
20150205
20150206
20150207
20150208
20150209
20150210
20150211
20150212
20150213
20150214
20150215
20150216
20150217
20150218
20150219
20150220
20150221
20150222
20150223
20150224
20150225
20150226
20150227
20150228

In [4]:
for i in range(1,31):
    thedate = "201503"
    if i<10:
        thedate += "0"+str(i)
    else:
        thedate += str(i)
    print thedate   
    positions = cPickle.load(open('../../data/density/'+thedate+'_v2_vessels.p', 'rb'))
    with open('../../data/density/'+thedate+'_v2_vessels.csv', 'wb') as f:
        writer = csv.writer(f)
        writer.writerow(["lat_bin","lon_bin","lat_bin_avg","lon_bin_avg","sat_positions",
                         "terrestrial_positions","positions_weighted",
                        "avg_speed","slow_pings","mmsi"])
        writer.writerows(positions)


20150301
20150302
20150303
20150304
20150305
20150306
20150307
20150308
20150309
20150310
20150311
20150312
20150313
20150314
20150315
20150316
20150317
20150318
20150319
20150320
20150321
20150322
20150323
20150324
20150325
20150326
20150327
20150328
20150329
20150330

In [6]:
for i in range(31,32):
    thedate = "201503"
    if i<10:
        thedate += "0"+str(i)
    else:
        thedate += str(i)
    print thedate   
    positions = cPickle.load(open('../../data/density/'+thedate+'_v2_vessels.p', 'rb'))
    with open('../../data/density/'+thedate+'_v2_vessels.csv', 'wb') as f:
        writer = csv.writer(f)
        writer.writerow(["lat_bin","lon_bin","lat_bin_avg","lon_bin_avg","sat_positions",
                         "terrestrial_positions","positions_weighted",
                        "avg_speed","slow_pings","mmsi"])
        writer.writerows(positions)


20150331

In [ ]: