tr_snake.py 7.85 KB
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
	Plot maps of the spots tracks, over different parts of the Gulf of St Lawrence.
	Spot positions extract by the script tr_position.py.
		-» map of the entire Gulf of St Lawrence
		-» map of the St Lawrence Estuary
		-» map of the Baie des Chaleurs

	the track are compiled from the selected date to the present, the last 48h are
	ploted with a colorscale to better visualise the spot motion.
		
	Autor: 		Paul Nicot - UQAR-ISMER
	Last updade:	Dec 2015

"""

import matplotlib
matplotlib.use('Agg')
# for running without X server
from mpl_toolkits.basemap import Basemap
from time import gmtime, strftime
import matplotlib.pyplot as plt
import time
import datetime
import numpy as np
import pandas as pd
from mpl_toolkits.axes_grid1 import make_axes_locatable

# Modifiable parameters
# ================================================
snake = 48				# nombre d'heure de suivi du serpent
start = 20150609				# date de debut de suivi de la saison

file_pos = '/share/archives/partage_lasso/spot/spots_pos_flag_clean.csv'
dir_carte = '/share/archives/partage_lasso/spot/carte'
# ================================================


# fixed parameters
#-------------------------------------------------
sday  = 84600
shour = 3600

# colormap
cmap=plt.get_cmap('OrRd')
lab1 = int(snake)
lab2 = int(snake*0.75)
lab3 = int(snake*0.5)
lab4 = int(snake*0.25)
lab5 = 0

# database 
df = pd.read_csv(file_pos,delimiter=';')

# time paramters
timestamp = int(time.time())
dt = timestamp - shour * snake
diff = timestamp - dt

strtime = datetime.datetime.fromtimestamp(int(timestamp)).strftime('%Y-%m-%d %H:%M')
nametime  = datetime.datetime.fromtimestamp(int(timestamp)).strftime('%Y%m%d%H%M')

# date of tracks's beginning
sT = str(start)
starT = int(time.mktime(datetime.datetime.strptime(sT, "%Y%m%d").timetuple()))


#===================================================
# Gulf of St Lawrence tracks map
#===================================================

# selection of the reliable positions
GSL = df[(df.FLAG == "OCN") & (df.TIME <= timestamp) 
		& (df.TIME > timestamp - shour*snake)]
GSL_start = df.loc[(df.FLAG == "OCN") & (df.TIME <= timestamp) 
		& (df.TIME > starT)]

fig, ax = plt.subplots()

# map setting
m = Basemap(projection='stere',lon_0=-64.5,lat_0=48,\
			llcrnrlat=45.2,urcrnrlat=51.6,\
			llcrnrlon=-70,urcrnrlon=-56.5,\
			rsphere=6371.2,resolution='h')

# map background
m.drawmapboundary(fill_color='lightblue') # fill to edge
m.drawcountries()
m.fillcontinents(color='grey',lake_color='lightblue',zorder=0)
m.drawrivers()
m.drawcoastlines(linewidth=0.7)
m.drawparallels(np.arange(40,60,2),labels=[1,0,0,0],linewidth=0.0)
m.drawmeridians(np.arange(-80,-40,2),labels=[0,0,0,1],linewidth=0.0)

# plot track from the start date
for spot, group in GSL_start.groupby(['SPOT']):
	lat = group.LAT.values
	lon = group.LON.values
	x,y = m(lon, lat)
	plt.plot(x,y,color='0.35',linewidth=0.7)

# plot colored track of the last positions (snake)
if not GSL.empty:
	for spot, group in GSL.groupby(['SPOT']):
		latitude = group.LAT.values
		longitude = group.LON.values
		t = (group.TIME.values -float(dt)) / diff
		x,y = m(longitude, latitude)
		cax = plt.scatter(x,y,c=t, s=35, cmap=cmap,linewidth=0,vmin=0, vmax=1)

if GSL.empty:
	cax = plt.scatter(-1,-1,c=1, s=35, cmap=cmap,linewidth=0,vmin=0, vmax=1)

# colorbar
cbar = fig.colorbar(cax,fraction=0.03, pad=0.03)
cbar.set_ticks([0,0.25,0.5,0.75,1])
cbar.update_ticks()
cbar.ax.set_yticklabels([lab1,lab2,lab3,lab4,lab5])
cbar.set_label('heures', rotation=270,labelpad=15)

# legende
plt.annotate(strtime + ' HAE', xy=(1, 1),  xycoords='data', size=20,
			xytext=(0.7, 0.9), textcoords='axes fraction',
			horizontalalignment='right', verticalalignment='top')
plt.title(u"Dérive de surface (" + str(snake) + u" dernières heures)")
# recording
plt.savefig(dir_carte + '/GSL/GSL_' + nametime + '.png',dpi=300)
# only png can be generate
plt.close()

#===================================================
# St Lawrence Estuary
#===================================================
# selection of the reliable positions
SLE = df[(df.FLAG == "OCN")
	   & (df.TIME <= timestamp) & (df.TIME > timestamp - shour*snake)]
SLE_start = df.loc[(df.FLAG == "OCN") & (df.TIME <= timestamp)
			& (df.TIME > starT)]

fig, ax = plt.subplots()

# map setting 
m = Basemap(projection='stere',lon_0=-68.5,lat_0=48,\
			llcrnrlat=47.8,urcrnrlat=49.5,\
			llcrnrlon=-70,urcrnrlon=-67,\
			rsphere=6371.2,resolution='h')

m.drawmapboundary(fill_color='lightblue') # fill to edge
m.drawcountries()
m.fillcontinents(color='grey',lake_color='lightblue',zorder=0)
m.drawrivers()
m.drawcoastlines(linewidth=0.7)
m.drawparallels(np.arange(40,60,0.5),labels=[1,0,0,0],linewidth=0.0)
m.drawmeridians(np.arange(-80,-40,1),labels=[0,0,0,1],linewidth=0.0)

# plot track from the start date 
for spot, group in SLE_start.groupby(['SPOT']):
	lat = group.LAT.values
	lon = group.LON.values
	x,y = m(lon, lat)
	plt.plot(x,y,color='0.35',linewidth=0.7)

# plot track of the last positions (snake)
if not SLE.empty:
	for spot, group in SLE.groupby(['SPOT']):
		latitude = group.LAT.values
		longitude = group.LON.values
		t = (group.TIME.values - float(dt)) / diff
		x,y = m(longitude, latitude)
		cax = plt.scatter(x,y,c=t, s=45, cmap=cmap,linewidth=0,vmin=0, vmax=1)

if SLE.empty:
	cax = plt.scatter(-1,-1,c=1, s=35, cmap=cmap,linewidth=0,vmin=0, vmax=1)	

# colorbar
cbar = fig.colorbar(cax,fraction=0.03, pad=0.03)
cbar.set_ticks([0,0.25,0.5,0.75,1])
cbar.update_ticks()
cbar.ax.set_yticklabels([lab1,lab2,lab3,lab4,lab5])
cbar.set_label('heures', rotation=270,labelpad=15)

# legende
plt.annotate(strtime + ' HAE', xy=(1, 1),  xycoords='data', size=20,
			xytext=(0.9, 0.1), textcoords='axes fraction',
			horizontalalignment='right', verticalalignment='top')
plt.title(u"Dérive de surface (" + str(snake) + u" dernières heures)")
# recording
plt.savefig(dir_carte + '/SLE/SLE_' + nametime + '.png',dpi=300)

plt.close()

#===================================================	
# Baie des chaleurs
#===================================================
# selection of the reliable positions
BDC = df[(df.FLAG == "OCN") 
	   & (df.TIME <= timestamp) & (df.TIME > timestamp - shour*snake)]
BDC_start = df.loc[(df.FLAG == "OCN") & (df.TIME <= timestamp)
					& (df.TIME > starT)]
fig, ax = plt.subplots()

# map setting
m = Basemap(projection='stere',lon_0=-68.5,lat_0=48,\
			llcrnrlat=47.5,urcrnrlat=48.9,\
			llcrnrlon=-66.9,urcrnrlon=-63.8,\
			rsphere=6371.2,resolution='h')

m.drawmapboundary(fill_color='lightblue') # fill to edge
m.drawcountries()
m.fillcontinents(color='grey',lake_color='lightblue',zorder=0)
m.drawrivers()
m.drawcoastlines(linewidth=0.7)
m.drawparallels(np.arange(40,60,0.5),labels=[1,0,0,0],linewidth=0.0)
m.drawmeridians(np.arange(-80,-40,1),labels=[0,0,0,1],linewidth=0.0)

# plot track from the start date
for spot, group in BDC_start.groupby(['SPOT']):
	lat = group.LAT.values
	lon = group.LON.values
	x,y = m(lon, lat)
	plt.plot(x,y,color='0.35',linewidth=0.7)	

# plot track of the last positions (snake)
if not BDC.empty:
	for spot, group in BDC.groupby(['SPOT']):
		latitude = group.LAT.values
		longitude = group.LON.values
		t = (group.TIME.values - float(dt)) / diff
		x,y = m(longitude, latitude)
		cax = plt.scatter(x,y,c=t, s=45, cmap=cmap,linewidth=0,vmin=0, vmax=1)

if BDC.empty:
	cax = plt.scatter(-1,-1,c=1, s=35, cmap=cmap,linewidth=0,vmin=0, vmax=1)	

# colorbar
cbar = fig.colorbar(cax,fraction=0.03, pad=0.03)
cbar.set_ticks([0,0.25,0.5,0.75,1])
cbar.update_ticks()
cbar.ax.set_yticklabels([lab1,lab2,lab3,lab4,lab5])
cbar.set_label('heures', rotation=270,labelpad=15)

# legende
plt.annotate(strtime + ' HAE', xy=(1, 1),  xycoords='data', size=20,
			xytext=(0.6, 0.1), textcoords='axes fraction',
			horizontalalignment='right', verticalalignment='top') 
			
plt.title(u"Dérive de surface (" + str(snake) + u" dernières heures)")
# recording
plt.savefig(dir_carte + '/BDC/BDC_' + nametime + '.png',dpi=300)
plt.close()