Fishing Effort in the Mascarene Plateau


In [3]:
import bq  
import time
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
from math import radians, cos, sin, asin, sqrt
from scipy import stats
import matplotlib.dates as mdates

client = bq.Client.Get()

In [4]:
# create a bounding box:
max_lat = -5
min_lat = -15
max_lon = 65
min_lon = 55

In [5]:
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 [6]:
q = '''
select count(distinct mmsi) 
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > '''+str(min_lat)+'''
  AND latitude <'''+str(max_lat)+'''
  AND longitude > '''+str(min_lon)+'''
  AND longitude < '''+str(max_lon)+'''
  AND weight >=.5'''

number_of_mmsi = Query(q)


Waiting on bqjob_r40791676e6648beb_000001533d2e6970_1 ... (0s) Current status: DONE   
Query time: 2.70184087753 seconds.

In [7]:
print "Number of unique MMSI:",int(number_of_mmsi[0][0])


Number of unique MMSI: 190

190 MMSI were fishing in this region from January 2014 to July 2015


In [10]:
q = '''
select a.mmsi mmsi,
a.number number,
if(b.country is null, "invalid mmsi",b.country) country,
b.continent continent
from
(select mmsi, 
integer(if(length(string(mmsi))= 9,LEFT(STRING(mmsi),3), '0')) code,
count(*) number from
(SELECT
  mmsi,
  date(timestamp) date,
  first(latitude) lat,
  first(longitude) lon
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > -15
  AND latitude <-5
  AND longitude > 55
  AND longitude <65
  AND weight >=.5
group by mmsi, date) 
group by mmsi, code 
) 
a
left join [scratch_roan.country_code] b
on a.code = b.code 
order by number desc'''

days_by_mmsi = Query(q)


Waiting on bqjob_r223af54691adfe22_000001533d2f95ac_3 ... (4s) Current status: DONE   
Query time: 6.90521597862 seconds.

The List of MMSI that were fishing are below


In [11]:
for r in days_by_mmsi:
    if "Taiwan" in r[2]:
        r[2] = "Taiwan"
    print "mmsi:", r[0],"  fishing days:", r[1], "  country: ", r[2].split(" (")[0]


mmsi: 416000000   fishing days: 257   country:  Taiwan
mmsi: 416237800   fishing days: 115   country:  Taiwan
mmsi: 416002566   fishing days: 105   country:  Taiwan
mmsi: 416002655   fishing days: 97   country:  Taiwan
mmsi: 416214600   fishing days: 96   country:  Taiwan
mmsi: 416241800   fishing days: 96   country:  Taiwan
mmsi: 416001838   fishing days: 96   country:  Taiwan
mmsi: 416239800   fishing days: 91   country:  Taiwan
mmsi: 525000000   fishing days: 79   country:  Indonesia
mmsi: 416004367   fishing days: 77   country:  Taiwan
mmsi: 416236600   fishing days: 74   country:  Taiwan
mmsi: 416002039   fishing days: 70   country:  Taiwan
mmsi: 416229900   fishing days: 69   country:  Taiwan
mmsi: 416002767   fishing days: 69   country:  Taiwan
mmsi: 416004187   fishing days: 66   country:  Taiwan
mmsi: 416002508   fishing days: 65   country:  Taiwan
mmsi: 416004773   fishing days: 59   country:  Taiwan
mmsi: 533333001   fishing days: 55   country:  Malaysia
mmsi: 416244700   fishing days: 55   country:  Taiwan
mmsi: 416002061   fishing days: 54   country:  Taiwan
mmsi: 416000818   fishing days: 52   country:  Taiwan
mmsi: 416002732   fishing days: 48   country:  Taiwan
mmsi: 416243500   fishing days: 47   country:  Taiwan
mmsi: 416011800   fishing days: 43   country:  Taiwan
mmsi: 416004624   fishing days: 42   country:  Taiwan
mmsi: 416003484   fishing days: 41   country:  Taiwan
mmsi: 416000464   fishing days: 39   country:  Taiwan
mmsi: 645378000   fishing days: 38   country:  Mauritius
mmsi: 416004342   fishing days: 38   country:  Taiwan
mmsi: 416223700   fishing days: 38   country:  Taiwan
mmsi: 416004168   fishing days: 38   country:  Taiwan
mmsi: 416002839   fishing days: 38   country:  Taiwan
mmsi: 416002646   fishing days: 37   country:  Taiwan
mmsi: 416803000   fishing days: 35   country:  Taiwan
mmsi: 416002269   fishing days: 35   country:  Taiwan
mmsi: 416004485   fishing days: 34   country:  Taiwan
mmsi: 416002876   fishing days: 33   country:  Taiwan
mmsi: 416004245   fishing days: 33   country:  Taiwan
mmsi: 660002700   fishing days: 33   country:  Reunion
mmsi: 533333009   fishing days: 33   country:  Malaysia
mmsi: 416308000   fishing days: 32   country:  Taiwan
mmsi: 416002151   fishing days: 32   country:  Taiwan
mmsi: 416002235   fishing days: 31   country:  Taiwan
mmsi: 664583000   fishing days: 31   country:  Seychelles
mmsi: 416225500   fishing days: 30   country:  Taiwan
mmsi: 416002407   fishing days: 30   country:  Taiwan
mmsi: 416003800   fishing days: 30   country:  Taiwan
mmsi: 312888000   fishing days: 29   country:  Belize
mmsi: 416002659   fishing days: 29   country:  Taiwan
mmsi: 533333005   fishing days: 29   country:  Malaysia
mmsi: 370599000   fishing days: 27   country:  Panama
mmsi: 664544000   fishing days: 26   country:  Seychelles
mmsi: 416124600   fishing days: 26   country:  Taiwan
mmsi: 416768000   fishing days: 26   country:  Taiwan
mmsi: 416005500   fishing days: 25   country:  Taiwan
mmsi: 416004507   fishing days: 24   country:  Taiwan
mmsi: 416004463   fishing days: 24   country:  Taiwan
mmsi: 416002885   fishing days: 23   country:  Taiwan
mmsi: 664582000   fishing days: 23   country:  Seychelles
mmsi: 416067700   fishing days: 22   country:  Taiwan
mmsi: 533333003   fishing days: 22   country:  Malaysia
mmsi: 664137000   fishing days: 22   country:  Seychelles
mmsi: 416002875   fishing days: 22   country:  Taiwan
mmsi: 533333007   fishing days: 21   country:  Malaysia
mmsi: 416000303   fishing days: 21   country:  Taiwan
mmsi: 461000062   fishing days: 21   country:  Oman
mmsi: 440822000   fishing days: 20   country:  Korea
mmsi: 416000154   fishing days: 19   country:  Taiwan
mmsi: 416004797   fishing days: 19   country:  Taiwan
mmsi: 416004790   fishing days: 19   country:  Taiwan
mmsi: 416002948   fishing days: 19   country:  Taiwan
mmsi: 416004368   fishing days: 19   country:  Taiwan
mmsi: 416002198   fishing days: 18   country:  Taiwan
mmsi: 416004427   fishing days: 18   country:  Taiwan
mmsi: 416002841   fishing days: 18   country:  Taiwan
mmsi: 416002926   fishing days: 17   country:  Taiwan
mmsi: 247354400   fishing days: 17   country:  Italy
mmsi: 416002447   fishing days: 17   country:  Taiwan
mmsi: 660004300   fishing days: 17   country:  Reunion
mmsi: 416002232   fishing days: 16   country:  Taiwan
mmsi: 416702000   fishing days: 16   country:  Taiwan
mmsi: 441734000   fishing days: 16   country:  Korea
mmsi: 416841000   fishing days: 16   country:  Taiwan
mmsi: 416002632   fishing days: 16   country:  Taiwan
mmsi: 441865000   fishing days: 15   country:  Korea
mmsi: 416004378   fishing days: 15   country:  Taiwan
mmsi: 431602690   fishing days: 15   country:  Japan
mmsi: 416094500   fishing days: 14   country:  Taiwan
mmsi: 312729000   fishing days: 14   country:  Belize
mmsi: 412695710   fishing days: 14   country:  China
mmsi: 412695690   fishing days: 13   country:  China
mmsi: 416826000   fishing days: 13   country:  Taiwan
mmsi: 416004772   fishing days: 13   country:  Taiwan
mmsi: 416004675   fishing days: 13   country:  Taiwan
mmsi: 660004900   fishing days: 12   country:  Reunion
mmsi: 416002764   fishing days: 12   country:  Taiwan
mmsi: 224464000   fishing days: 12   country:  Spain
mmsi: 416003953   fishing days: 12   country:  Taiwan
mmsi: 416167900   fishing days: 11   country:  Taiwan
mmsi: 416002325   fishing days: 11   country:  Taiwan
mmsi: 548055100   fishing days: 11   country:  Philippines
mmsi: 660005100   fishing days: 11   country:  Reunion
mmsi: 224069690   fishing days: 11   country:  Spain
mmsi: 416002287   fishing days: 11   country:  Taiwan
mmsi: 416333000   fishing days: 10   country:  Taiwan
mmsi: 416556000   fishing days: 10   country:  Taiwan
mmsi: 577132000   fishing days: 10   country:  Vanuatu
mmsi: 432298000   fishing days: 10   country:  Japan
mmsi: 416002515   fishing days: 10   country:  Taiwan
mmsi: 416002826   fishing days: 9   country:  Taiwan
mmsi: 416002496   fishing days: 9   country:  Taiwan
mmsi: 416004236   fishing days: 9   country:  Taiwan
mmsi: 416001054   fishing days: 9   country:  Taiwan
mmsi: 660001900   fishing days: 8   country:  Reunion
mmsi: 660003800   fishing days: 8   country:  Reunion
mmsi: 416003048   fishing days: 8   country:  Taiwan
mmsi: 416002794   fishing days: 8   country:  Taiwan
mmsi: 416002779   fishing days: 8   country:  Taiwan
mmsi: 312422000   fishing days: 8   country:  Belize
mmsi: 416001900   fishing days: 7   country:  Taiwan
mmsi: 416004095   fishing days: 7   country:  Taiwan
mmsi: 416004718   fishing days: 7   country:  Taiwan
mmsi: 440316000   fishing days: 7   country:  Korea
mmsi: 226312000   fishing days: 7   country:  France
mmsi: 416055600   fishing days: 7   country:  Taiwan
mmsi: 416602000   fishing days: 6   country:  Taiwan
mmsi: 416001769   fishing days: 5   country:  Taiwan
mmsi: 416003724   fishing days: 5   country:  Taiwan
mmsi: 168007   fishing days: 5   country:  invalid mmsi
mmsi: 416808000   fishing days: 5   country:  Taiwan
mmsi: 416003021   fishing days: 5   country:  Taiwan
mmsi: 416771000   fishing days: 4   country:  Taiwan
mmsi: 525010274   fishing days: 4   country:  Indonesia
mmsi: 416002656   fishing days: 4   country:  Taiwan
mmsi: 416000012   fishing days: 4   country:  Taiwan
mmsi: 416000633   fishing days: 4   country:  Taiwan
mmsi: 440099000   fishing days: 4   country:  Korea
mmsi: 416087700   fishing days: 4   country:  Taiwan
mmsi: 416219500   fishing days: 4   country:  Taiwan
mmsi: 416000833   fishing days: 4   country:  Taiwan
mmsi: 200006671   fishing days: 3   country:  invalid mmsi
mmsi: 416088900   fishing days: 3   country:  Taiwan
mmsi: 412989000   fishing days: 3   country:  China
mmsi: 416220900   fishing days: 3   country:  Taiwan
mmsi: 228231700   fishing days: 3   country:  France
mmsi: 525010273   fishing days: 3   country:  Indonesia
mmsi: 416004418   fishing days: 3   country:  Taiwan
mmsi: 416003496   fishing days: 3   country:  Taiwan
mmsi: 416089800   fishing days: 3   country:  Taiwan
mmsi: 416004687   fishing days: 3   country:  Taiwan
mmsi: 412329677   fishing days: 3   country:  China
mmsi: 416002998   fishing days: 3   country:  Taiwan
mmsi: 440329000   fishing days: 2   country:  Korea
mmsi: 416889000   fishing days: 2   country:  Taiwan
mmsi: 416173500   fishing days: 2   country:  Taiwan
mmsi: 416003315   fishing days: 2   country:  Taiwan
mmsi: 412695660   fishing days: 2   country:  China
mmsi: 412695680   fishing days: 2   country:  China
mmsi: 416705000   fishing days: 2   country:  Taiwan
mmsi: 416189600   fishing days: 2   country:  Taiwan
mmsi: 416103900   fishing days: 2   country:  Taiwan
mmsi: 416015700   fishing days: 2   country:  Taiwan
mmsi: 664268000   fishing days: 2   country:  Seychelles
mmsi: 412328742   fishing days: 2   country:  China
mmsi: 416136900   fishing days: 2   country:  Taiwan
mmsi: 225461000   fishing days: 2   country:  Spain
mmsi: 416348000   fishing days: 2   country:  Taiwan
mmsi: 416869000   fishing days: 2   country:  Taiwan
mmsi: 431704490   fishing days: 2   country:  Japan
mmsi: 416002178   fishing days: 2   country:  Taiwan
mmsi: 664567000   fishing days: 2   country:  Seychelles
mmsi: 416002944   fishing days: 1   country:  Taiwan
mmsi: 416704000   fishing days: 1   country:  Taiwan
mmsi: 215482000   fishing days: 1   country:  Malta
mmsi: 888802000   fishing days: 1   country:  invalid mmsi
mmsi: 200006637   fishing days: 1   country:  invalid mmsi
mmsi: 123456789   fishing days: 1   country:  invalid mmsi
mmsi: 416002865   fishing days: 1   country:  Taiwan
mmsi: 416002934   fishing days: 1   country:  Taiwan
mmsi: 664000022   fishing days: 1   country:  Seychelles
mmsi: 440328000   fishing days: 1   country:  Korea
mmsi: 416003015   fishing days: 1   country:  Taiwan
mmsi: 664545000   fishing days: 1   country:  Seychelles
mmsi: 416121600   fishing days: 1   country:  Taiwan
mmsi: 312000125   fishing days: 1   country:  Belize
mmsi: 224922000   fishing days: 1   country:  Spain
mmsi: 412420909   fishing days: 1   country:  China
mmsi: 416238900   fishing days: 1   country:  Taiwan
mmsi: 416002667   fishing days: 1   country:  Taiwan
mmsi: 416002286   fishing days: 1   country:  Taiwan

In [12]:
# Now Group by Country
q = '''
SELECT 
country,
sum(number) number
from
(select a.mmsi mmsi,
a.number number,
if(b.country is null, "invalid mmsi",b.country) country,
b.continent continent
from
(select mmsi, 
integer(if(length(string(mmsi))= 9,LEFT(STRING(mmsi),3), '0')) code,
count(*) number from
(SELECT
  mmsi,
  date(timestamp) date,
  first(latitude) lat,
  first(longitude) lon
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > '''+str(min_lat)+'''
  AND latitude <'''+str(max_lat)+'''
  AND longitude > '''+str(min_lon)+'''
  AND longitude < '''+str(max_lon)+'''
  AND weight >=.5
group by mmsi, date) 
group by mmsi, code 
) 
a
left join [scratch_roan.country_code] b
on a.code = b.code)
group by country
order by number desc'''

country_groups = Query(q)


Waiting on bqjob_r311918235e44e178_000001533d30fb16_4 ... (3s) Current status: DONE   
Query time: 6.25192213058 seconds.

In [13]:
print "days\tcountry"
for c in country_groups:
    print c[1],'\t', c[0].split(" (")[0]


days	country
3305 	Taiwan
160 	Malaysia
108 	Seychelles
89 	Reunion
86 	Indonesia
65 	Korea
52 	Belize
40 	China
38 	Mauritius
27 	Panama
27 	Japan
26 	Spain
21 	Oman
17 	Italy
11 	invalid mmsi
11 	Philippines
10 	France
10 	Vanuatu
1 	Malta

In [16]:
objects = [c[0].split(" (")[0] for c in country_groups]
y_pos = np.arange(len(objects))
performance = [c[1] for c in country_groups]
 
plt.figure(figsize=(12,4))
plt.bar(y_pos-4, performance, align='center', alpha=0.5,)
plt.xticks(y_pos-4+.2, objects, fontsize=12) # rotation=45
locs, labels = plt.xticks()
plt.setp(labels, rotation=45,ha='right')
plt.ylabel('Usage',fontsize=12)
plt.title('Fishing Days by Country in Mascarene Plateau, Jan 2014 to July 2015',fontsize=15)

ax = plt.axes()
ax.xaxis.set_ticks_position('none') 
plt.savefig("fishing_effort_mascarene_by_Country.png",bbox_inches='tight',dpi=150,transparent=True,pad_inches=.1)
plt.show()


Map the Fishing Effort in This Region


In [87]:
# Now Group by Country
q = '''
SELECT
lat,
lon,
count(*) fishing_days
from
(SELECT
  mmsi,
  date(timestamp) date,
  integer(first(latitude)*4) lat,
  integer(first(longitude)*4) lon
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > '''+str(min_lat)+'''
  AND latitude <'''+str(max_lat)+'''
  AND longitude > '''+str(min_lon)+'''
  AND longitude < '''+str(max_lon)+'''
  AND weight >=.5
group by mmsi, date) 
group by lat, lon
'''

fishing_grid = Query(q)


Waiting on bqjob_r5ccc58aa5d57fb82_0000015339bcd28d_10 ... (0s) Current status: DONE   
Query time: 1.82012200356 seconds.

In [90]:
cellsize = .25
one_over_cellsize = 4

num_lats = (max_lat-min_lat)*one_over_cellsize+1
num_lons = (max_lon-min_lon)*one_over_cellsize+1


grid = np.zeros(shape=(num_lats,num_lons))

for row in fishing_grid:
    lat = int(row[0])
    lon = int(row[1])
    lat_index = lat-min_lat*one_over_cellsize
    lon_index = lon-min_lon*one_over_cellsize
    grid[lat_index][lon_index] = int(row[2])

In [110]:
plt.rcParams["figure.figsize"] = [8,9]

firstlat = max_lat
lastlat =  min_lat
firstlon = min_lon
lastlon = max_lon
scale = cellsize

numlats = int((firstlat-lastlat)/scale+.5)
numlons = int((lastlon-firstlon)/scale+.5)
    
lat_boxes = np.linspace(lastlat,firstlat,num=numlats,endpoint=False)
lon_boxes = np.linspace(firstlon,lastlon,num=numlons,endpoint=False)

fig = plt.figure()
extra = 10
m = Basemap(llcrnrlat=lastlat-extra, urcrnrlat=firstlat+extra,
          llcrnrlon=firstlon-extra, urcrnrlon=lastlon+extra, lat_ts=0, projection='mill',resolution="h")

m.drawmapboundary()#fill_color='#111111')
m.drawcoastlines(linewidth=.2)
m.fillcontinents('#cccccc',lake_color='#cccccc')#, lake_color, ax, zorder, alpha)

x = np.linspace(firstlon, lastlon, -(firstlon-lastlon)*one_over_cellsize+1)
y = np.linspace(lastlat, firstlat, (firstlat-lastlat)*one_over_cellsize+1)
x, y = np.meshgrid(x, y)
converted_x, converted_y = m(x, y)
from matplotlib import colors,colorbar

maximum = grid.max()
minimum = 1

norm = colors.LogNorm(vmin=minimum, vmax=maximum)
# norm = colors.Normalize(vmin=0, vmax=1000)

m.pcolormesh(converted_x, converted_y, grid, norm=norm, vmin=minimum, vmax=maximum, cmap = plt.get_cmap('viridis'))

t = "Fishing Days Jan 2014 to July 2015"
plt.title(t, color = "#000000", fontsize=18)

ax = fig.add_axes([0.2, 0.1, 0.65, 0.02]) #x coordinate , 
norm = colors.LogNorm(vmin=minimum, vmax=maximum)
# norm = colors.Normalize(vmin=0, vmax=1000)
lvls = np.logspace(np.log10(minimum),np.log10(maximum),num=8)
cb = colorbar.ColorbarBase(ax,norm = norm, orientation='horizontal', ticks=lvls, cmap = plt.get_cmap('viridis'))

#cb.ax.set_xticklabels(["0" ,round(m3**.5,1), m3, round(m3**1.5,1), m3*m3,round(m3**2.5,1), str(round(m3**3,1))+"+"], fontsize=10)
cb.ax.set_xticklabels([int(i) for i in lvls], fontsize=10, color = "#000000")
cb.set_label('Fishing Days',labelpad=-40, y=0.45, color = "#000000")
plt.savefig("fishing_effort_mascarene.png",bbox_inches='tight',dpi=300,transparent=True,pad_inches=.1)
plt.show()



In [ ]: