Fishing Effort in the Seychelles EEZ


In [4]:
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 [2]:
# create a bounding box:
max_lat = 3
min_lat = -17
max_lon = 61.5
min_lon = 40

In [3]:
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 [9]:
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
  AND JSON_EXTRACT(extra,"$.eez") = '"EEZ:Seychelles"'
  '''
number_of_mmsi = Query(q)


Waiting on bqjob_r6d13fb4acee1672b_00000153e6e1bd7b_3 ... (0s) Current status: DONE   
Query time: 1.04782605171 seconds.

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


Number of unique MMSI: 101

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


In [11]:
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
  AND JSON_EXTRACT(extra,"$.eez") = '"EEZ:Seychelles"'

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_r4425054b493e87_00000153e6e1d27d_4 ... (48s) Current status: DONE   
Query time: 51.1671459675 seconds.

The List of MMSI that were fishing are below


In [12]:
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: 664544000   fishing days: 19   country:  Seychelles
mmsi: 416768000   fishing days: 18   country:  Taiwan
mmsi: 660004300   fishing days: 16   country:  Reunion
mmsi: 247354400   fishing days: 15   country:  Italy
mmsi: 416004773   fishing days: 11   country:  Taiwan
mmsi: 660005100   fishing days: 11   country:  Reunion
mmsi: 416556000   fishing days: 10   country:  Taiwan
mmsi: 412695690   fishing days: 10   country:  China
mmsi: 416803000   fishing days: 9   country:  Taiwan
mmsi: 224069690   fishing days: 9   country:  Spain
mmsi: 416004790   fishing days: 9   country:  Taiwan
mmsi: 416241800   fishing days: 9   country:  Taiwan
mmsi: 416333000   fishing days: 8   country:  Taiwan
mmsi: 660004900   fishing days: 8   country:  Reunion
mmsi: 440822000   fishing days: 7   country:  Korea
mmsi: 441734000   fishing days: 6   country:  Korea
mmsi: 412695710   fishing days: 6   country:  China
mmsi: 416055600   fishing days: 6   country:  Taiwan
mmsi: 664582000   fishing days: 6   country:  Seychelles
mmsi: 660001900   fishing days: 6   country:  Reunion
mmsi: 548055100   fishing days: 6   country:  Philippines
mmsi: 224464000   fishing days: 5   country:  Spain
mmsi: 416244700   fishing days: 4   country:  Taiwan
mmsi: 226312000   fishing days: 4   country:  France
mmsi: 664137000   fishing days: 4   country:  Seychelles
mmsi: 441865000   fishing days: 4   country:  Korea
mmsi: 660003800   fishing days: 4   country:  Reunion
mmsi: 525010274   fishing days: 4   country:  Indonesia
mmsi: 645378000   fishing days: 3   country:  Mauritius
mmsi: 416004236   fishing days: 3   country:  Taiwan
mmsi: 228231700   fishing days: 3   country:  France
mmsi: 525010273   fishing days: 3   country:  Indonesia
mmsi: 416103900   fishing days: 2   country:  Taiwan
mmsi: 431704490   fishing days: 2   country:  Japan
mmsi: 412695680   fishing days: 2   country:  China
mmsi: 416087700   fishing days: 2   country:  Taiwan
mmsi: 440099000   fishing days: 2   country:  Korea
mmsi: 225461000   fishing days: 2   country:  Spain
mmsi: 416808000   fishing days: 2   country:  Taiwan
mmsi: 664583000   fishing days: 2   country:  Seychelles
mmsi: 664268000   fishing days: 2   country:  Seychelles
mmsi: 412328742   fishing days: 1   country:  China
mmsi: 664545000   fishing days: 1   country:  Seychelles
mmsi: 416223700   fishing days: 1   country:  Taiwan
mmsi: 412989000   fishing days: 1   country:  China
mmsi: 888802000   fishing days: 1   country:  invalid mmsi
mmsi: 224922000   fishing days: 1   country:  Spain
mmsi: 664000022   fishing days: 1   country:  Seychelles
mmsi: 525000000   fishing days: 1   country:  Indonesia

In [23]:
# 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
  AND JSON_EXTRACT(extra,"$.eez") = '"EEZ:Seychelles"'

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_r151fd67d16850a9_00000153e6e5d96a_8 ... (0s) Current status: DONE   
Query time: 1.75776100159 seconds.

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


days	country
556 	Taiwan
482 	Korea
444 	Seychelles
336 	Reunion
233 	Spain
173 	China
112 	Mauritius
77 	France
73 	invalid mmsi
61 	Italy
43 	Thailand
32 	Iran
9 	Indonesia
6 	Philippines
4 	Japan
2 	Panama

In [30]:
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('Fishing Days',fontsize=12)
plt.title('Fishing Days by Country in Seychelles, Jan 2014 to July 2015',fontsize=15)

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


Map the Fishing Effort in This Region


In [27]:
# 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
  AND JSON_EXTRACT(extra,"$.eez") = '"EEZ:Seychelles"'

group by mmsi, date) 
group by lat, lon
'''

fishing_grid = Query(q)


Waiting on bqjob_r789c33958f9d4cb4_00000153e6e65b72_9 ... (0s) Current status: DONE   
Query time: 1.82609796524 seconds.

In [28]:
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])


/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ipykernel/__main__.py:8: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

In [29]:
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 \n in Seychelles EEZ"
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_seychelles.png",bbox_inches='tight',dpi=300,transparent=True,pad_inches=.1)
plt.show()



In [ ]: