tr_snake_old.py 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# produit des cartes des trajectoires des spots dans des zones choisies 
# entre 2 dates a intervale de temps reglables.

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

# parametres modifiables --------------------------
region  = 'BDC'				# region (GSL / OHA / BDC / SLE)

day0  = 20150929		# jour de la premiere carte (fmt = YYYYMMDD)
h     = 2				# intervalle de temps entre les cartes (h)
day1  = 20151001		# jour de la derniere carte (fmt = YYYYMMDD)

start = 20150611		# date de debut de suivi de la saison

snake = 36				# nombre d'heure de suivi du serpent
# ================================================

ts = str(day0)
t0 = int(time.mktime(datetime.datetime.strptime(ts, "%Y%m%d").timetuple()))

tt = str(day1)
t1 = int(time.mktime(datetime.datetime.strptime(tt, "%Y%m%d").timetuple()))

tst = str(start)
tS = int(time.mktime(datetime.datetime.strptime(tst, "%Y%m%d").timetuple()))

sday  = 84600
shour = 3600
tint  = shour * h


# 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

# Base de donnees ----------------------------
df = pd.read_csv('/home/nicopa01/data/spot/spots_pos_flag_clean.csv',delimiter=';')
df = df.drop_duplicates(take_last=True)


for x in xrange(t0, t1, tint):
	
	timestamp = x							# temps unix
	dt = timestamp - shour * snake			# heure de debut de suivi
	diff = timestamp - dt					# intervale de suivi
	
	strtime = datetime.datetime.fromtimestamp(int(x)).strftime('%Y-%m-%d %H:%M')
	
	startt = datetime.datetime.fromtimestamp(int(tS)).strftime('%Y-%m-%d %H:%M')
	
	nametime  = datetime.datetime.fromtimestamp(int(x)).strftime('%Y%m%d%H%M')

	if region == 'SLE':
			
		# Estuaire du Saint-Laurent ---------------------------
		# traces des x derniers jours dans l'estuaire
		SLE = df.loc[(df.FLAG == "OCN")
			   & (df.TIME <= x) & (df.TIME > x - shour*snake)]
		
		# trace depuis le debut de l'ete
		SLE_start = df.loc[(df.FLAG == "OCN") & (df.TIME <= x) 
						& (df.TIME > tS)]
		
		fig, ax = plt.subplots()
		
		# caracteristiques de la carte
		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.8)
		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)
		
		
		for spot, group in SLE_start.groupby(['SPOT']):
			lat = group.LAT.values
			lon = group.LON.values
			x,y = m(lon, lat)
			cax = plt.plot(x,y,color='0.35',linewidth=0.7)
		
		# trace les chemins de bouees
		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)
			
		# 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)")
		#enregistrement
		#plt.savefig('/home/nicopa01/data/spot/suivi/SLE/SLE5_' + nametime + '.png',dpi=300)
		plt.savefig('/home/nicopa01/data/spot/suivi/SLE/SLE_' + nametime + '.jpg',dpi=300)
		plt.close()
		
	if region == 'BDC':
		
		bdc = df.loc[(df.SPOT == 24) | (df.SPOT == 23) | (df.SPOT == 52) |
				 (df.SPOT == 53) | (df.SPOT >= 700) &  (df.SPOT < 899)]
		
		# Baie des chaleurs ---------------------------
		# traces des x derniers jours dans la baie
		BDC = bdc.loc[(df.FLAG == "OCN") & (df.TIME <= x) & (df.TIME > x - shour*snake)]
		
		# selectionne les positions depuis le debut de l'ete
		BDC_start = bdc.loc[(df.FLAG == "OCN") & (df.TIME <= x) & (df.TIME > tS)]
  
		n = bdc.loc[(df.FLAG == "OCN") & (df.TIME <= x) & (df.TIME > tS)]
		number = str(n.TIME.count())
		
		fig, ax = plt.subplots()
		
		# caracteristiques de la carte
		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.8)
		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)

		# trace depuis le début de l'ete
		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)		

		# trace les chemins de bouees
		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)

		# 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.annotate(number + ' positions depuis le ' + startt, xy=(1, 1),  
				    xycoords='data', size=13,
		            xytext=(0.68, 0.9), textcoords='axes fraction',
		            horizontalalignment='right', verticalalignment='top')    
		            
		plt.title(u"Dérive de surface (" + str(snake) + u" dernières heures)")
		#enregistrement
		#plt.savefig('/home/nicopa01/data/spot/suivi/BDC/BDC_' + nametime + '.png',dpi=300)
		plt.savefig('/home/nicopa01/data/spot/suivi/BDC/BDC_' + nametime + '.jpg',dpi=300)
		plt.close()
		
	if region == 'GSL':
			
		# Estuaire du Saint-Laurent ---------------------------
		# traces des x derniers jours dans l'estuaire
		GSL = df[(df.FLAG == "OCN") & (df.TIME <= x) & (df.TIME > dt)]
		
		GSL_start = df.loc[(df.FLAG == "OCN") & (df.TIME <= x) & (df.TIME > tS)]
		
		n = df.loc[(df.FLAG == "OCN") & (df.TIME <= x) & (df.TIME > tS)]
		number = str(n.TIME.count())

		fig, ax = plt.subplots()
		
		# caracteristiques de la carte
		
		
		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')
            
		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.8)
		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)
		
		# trace depuis le début de l'ete
		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)		
		
		# trace les chemins de bouees
		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=45, cmap=cmap,linewidth=0,vmin=0, vmax=1)
		    plt.plot(x,y,'k-',linewidth=0.5)
			
		# 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)")
		#enregistrement
		#plt.savefig('/home/nicopa01/data/spot/suivi/GSL/GSL_' + nametime + '.png',dpi=300)
		plt.savefig('/home/nicopa01/data/spot/suivi/GSL/GSL_' + nametime + '.jpg',dpi=300)
		plt.close()
		
	if region == 'OHA':
			
		# Estuaire du Saint-Laurent ---------------------------
		# traces des x derniers jours dans l'estuaire
		OHA = df[(df.FLAG == "OCN")
				& (df.TIME <= x) & (df.TIME > x - shour*snake)]
		
		OHA_start = df.loc[(df.FLAG == "OCN")
				& (df.TIME <= x) & (df.TIME > tS)]

		fig, ax = plt.subplots()
		
		# caracteristiques de la carte
		m = Basemap(projection='stere',lon_0=-60,lat_0=49,\
		            llcrnrlat=47,urcrnrlat=51.5,\
		            llcrnrlon=-63.8,urcrnrlon=-56.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.8)
		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)
		
		# trace depuis le début de l'ete
		for spot, group in OHA_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)		
			
		# trace les chemins de bouees
		for spot, group in OHA.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)
			
		# 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=16,
		            xytext=(0.55, 0.9), textcoords='axes fraction',
		            horizontalalignment='right', verticalalignment='top')
		plt.title(u"Dérive de surface (" + str(snake) + u" dernières heures)")
		#enregistrement
		#plt.savefig('/home/nicopa01/data/spot/suivi/OHA/OHA_' + nametime + '.png',dpi=300)
		plt.savefig('/home/nicopa01/data/spot/suivi/OHA/OHA_' + nametime + '.jpg',dpi=300)
		plt.close()