Commit 278eaffaa56d4d0319db59526d3d3c689f79270d

Authored by Paul Nicot
1 parent 184f68e6
Exists in master and in 1 other branch simon

ajout de commentaire et nettoyage de code

Showing 1 changed file with 120 additions and 83 deletions   Show diff stats
tr_snake.py
1 1 #!/usr/bin/env python
2 2 # -*- coding: utf-8 -*-
3 3  
  4 +"""
  5 + Plot maps of the spots tracks, over different parts of the Gulf of St Lawrence.
  6 + Spot positions extract by the script tr_position.py.
  7 + -» map of the entire Gulf of St Lawrence
  8 + -» map of the St Lawrence Estuary
  9 + -» map of the Baie des Chaleurs
  10 +
  11 + the track are compiled from the selected date to the present, the last 48h are
  12 + ploted with a colorscale to better visualise the spot motion.
  13 +
  14 + Autor: Paul Nicot - UQAR-ISMER
  15 + Last updade: Dec 2015
  16 +
  17 +"""
  18 +
  19 +import matplotlib
  20 +matplotlib.use('Agg')
  21 +# for running without X server
4 22 from mpl_toolkits.basemap import Basemap
5 23 from time import gmtime, strftime
6 24 import matplotlib.pyplot as plt
... ... @@ -10,12 +28,18 @@ import numpy as np
10 28 import pandas as pd
11 29 from mpl_toolkits.axes_grid1 import make_axes_locatable
12 30  
13   -# parametres modifiables --------------------------
  31 +# Modifiable parameters
  32 +# ================================================
14 33 snake = 48 # nombre d'heure de suivi du serpent
  34 +start = 20150609 # date de debut de suivi de la saison
15 35  
16   -start = 20150611 # date de debut de suivi de la saison
  36 +file_pos = '/share/archives/partage_lasso/spot/spots_pos_flag_clean.csv'
  37 +dir_carte = '/share/archives/partage_lasso/spot/carte'
17 38 # ================================================
18 39  
  40 +
  41 +# fixed parameters
  42 +#-------------------------------------------------
19 43 sday = 84600
20 44 shour = 3600
21 45  
... ... @@ -27,10 +51,10 @@ lab3 = int(snake*0.5)
27 51 lab4 = int(snake*0.25)
28 52 lab5 = 0
29 53  
30   -# Base de donnees ----------------------------
31   -df = pd.read_csv('/home/nicopa01/data/spot/spots_pos_flag_clean.csv',delimiter=';')
32   -df = df.drop_duplicates(take_last=True)
  54 +# database
  55 +df = pd.read_csv(file_pos,delimiter=';')
33 56  
  57 +# time paramters
34 58 timestamp = int(time.time())
35 59 dt = timestamp - shour * snake
36 60 diff = timestamp - dt
... ... @@ -38,26 +62,30 @@ diff = timestamp - dt
38 62 strtime = datetime.datetime.fromtimestamp(int(timestamp)).strftime('%Y-%m-%d %H:%M')
39 63 nametime = datetime.datetime.fromtimestamp(int(timestamp)).strftime('%Y%m%d%H%M')
40 64  
41   -# date de début de suivi
  65 +# date of tracks's beginning
42 66 sT = str(start)
43 67 starT = int(time.mktime(datetime.datetime.strptime(sT, "%Y%m%d").timetuple()))
44 68  
45   -# Golfe du Saint-Laurent ---------------------------
46   -# traces des x derniers jours dans l'estuaire
  69 +
  70 +#===================================================
  71 +# Gulf of St Lawrence tracks map
  72 +#===================================================
  73 +
  74 +# selection of the reliable positions
47 75 GSL = df[(df.FLAG == "OCN") & (df.TIME <= timestamp)
48 76 & (df.TIME > timestamp - shour*snake)]
49   -
50 77 GSL_start = df.loc[(df.FLAG == "OCN") & (df.TIME <= timestamp)
51 78 & (df.TIME > starT)]
52 79  
53 80 fig, ax = plt.subplots()
54 81  
55   -# caracteristiques de la carte
  82 +# map setting
56 83 m = Basemap(projection='stere',lon_0=-64.5,lat_0=48,\
57   - llcrnrlat=45.2,urcrnrlat=51.6,\
58   - llcrnrlon=-70,urcrnrlon=-56.5,\
59   - rsphere=6371.2,resolution='h')
  84 + llcrnrlat=45.2,urcrnrlat=51.6,\
  85 + llcrnrlon=-70,urcrnrlon=-56.5,\
  86 + rsphere=6371.2,resolution='h')
60 87  
  88 +# map background
61 89 m.drawmapboundary(fill_color='lightblue') # fill to edge
62 90 m.drawcountries()
63 91 m.fillcontinents(color='grey',lake_color='lightblue',zorder=0)
... ... @@ -66,21 +94,25 @@ m.drawcoastlines(linewidth=0.7)
66 94 m.drawparallels(np.arange(40,60,2),labels=[1,0,0,0],linewidth=0.0)
67 95 m.drawmeridians(np.arange(-80,-40,2),labels=[0,0,0,1],linewidth=0.0)
68 96  
69   -# trace depuis le début de l'ete
  97 +# plot track from the start date
70 98 for spot, group in GSL_start.groupby(['SPOT']):
71 99 lat = group.LAT.values
72 100 lon = group.LON.values
73 101 x,y = m(lon, lat)
74   - plt.plot(x,y,color='0.35',linewidth=0.7)
75   -
76   -# trace les chemins de bouees
77   -for spot, group in GSL.groupby(['SPOT']):
78   - latitude = group.LAT.values
79   - longitude = group.LON.values
80   - t = (group.TIME.values - float(dt)) / diff
81   - x,y = m(longitude, latitude)
82   - cax = plt.scatter(x,y,c=t, s=35, cmap=cmap,linewidth=0,vmin=0, vmax=1)
83   -
  102 + plt.plot(x,y,color='0.35',linewidth=0.7)
  103 +
  104 +# plot colored track of the last positions (snake)
  105 +if not GSL.empty:
  106 + for spot, group in GSL.groupby(['SPOT']):
  107 + latitude = group.LAT.values
  108 + longitude = group.LON.values
  109 + t = (group.TIME.values -float(dt)) / diff
  110 + x,y = m(longitude, latitude)
  111 + cax = plt.scatter(x,y,c=t, s=35, cmap=cmap,linewidth=0,vmin=0, vmax=1)
  112 +
  113 +if GSL.empty:
  114 + cax = plt.scatter(-1,-1,c=1, s=35, cmap=cmap,linewidth=0,vmin=0, vmax=1)
  115 +
84 116 # colorbar
85 117 cbar = fig.colorbar(cax,fraction=0.03, pad=0.03)
86 118 cbar.set_ticks([0,0.25,0.5,0.75,1])
... ... @@ -90,29 +122,30 @@ cbar.set_label(&#39;heures&#39;, rotation=270,labelpad=15)
90 122  
91 123 # legende
92 124 plt.annotate(strtime + ' HAE', xy=(1, 1), xycoords='data', size=20,
93   - xytext=(0.6, 0.87), textcoords='axes fraction',
94   - horizontalalignment='right', verticalalignment='top')
  125 + xytext=(0.7, 0.9), textcoords='axes fraction',
  126 + horizontalalignment='right', verticalalignment='top')
95 127 plt.title(u"Dérive de surface (" + str(snake) + u" dernières heures)")
96   -#enregistrement
97   -plt.savefig('/home/nicopa01/data/spot/suivi/GSL/GSL_' + nametime + '.jpg',dpi=300)
  128 +# recording
  129 +plt.savefig(dir_carte + '/GSL/GSL_' + nametime + '.png',dpi=300)
  130 +# only png can be generate
98 131 plt.close()
99   -
100 132  
101   -# Estuaire du Saint-Laurent ---------------------------
102   -# traces des x derniers jours dans l'estuaire
  133 +#===================================================
  134 +# St Lawrence Estuary
  135 +#===================================================
  136 +# selection of the reliable positions
103 137 SLE = df[(df.FLAG == "OCN")
104   - & (df.TIME <= timestamp) & (df.TIME > timestamp - shour*snake)]
105   -
  138 + & (df.TIME <= timestamp) & (df.TIME > timestamp - shour*snake)]
106 139 SLE_start = df.loc[(df.FLAG == "OCN") & (df.TIME <= timestamp)
107   - & (df.TIME > starT)]
  140 + & (df.TIME > starT)]
108 141  
109 142 fig, ax = plt.subplots()
110 143  
111   -# caracteristiques de la carte
  144 +# map setting
112 145 m = Basemap(projection='stere',lon_0=-68.5,lat_0=48,\
113   - llcrnrlat=47.8,urcrnrlat=49.5,\
114   - llcrnrlon=-70,urcrnrlon=-67,\
115   - rsphere=6371.2,resolution='h')
  146 + llcrnrlat=47.8,urcrnrlat=49.5,\
  147 + llcrnrlon=-70,urcrnrlon=-67,\
  148 + rsphere=6371.2,resolution='h')
116 149  
117 150 m.drawmapboundary(fill_color='lightblue') # fill to edge
118 151 m.drawcountries()
... ... @@ -122,21 +155,25 @@ m.drawcoastlines(linewidth=0.7)
122 155 m.drawparallels(np.arange(40,60,0.5),labels=[1,0,0,0],linewidth=0.0)
123 156 m.drawmeridians(np.arange(-80,-40,1),labels=[0,0,0,1],linewidth=0.0)
124 157  
125   -# trace depuis le début de l'ete
  158 +# plot track from the start date
126 159 for spot, group in SLE_start.groupby(['SPOT']):
127 160 lat = group.LAT.values
128 161 lon = group.LON.values
129 162 x,y = m(lon, lat)
130 163 plt.plot(x,y,color='0.35',linewidth=0.7)
131 164  
132   -# trace les chemins de bouees
133   -for spot, group in SLE.groupby(['SPOT']):
134   - latitude = group.LAT.values
135   - longitude = group.LON.values
136   - t = (group.TIME.values - float(dt)) / diff
137   - x,y = m(longitude, latitude)
138   - cax = plt.scatter(x,y,c=t, s=45, cmap=cmap,linewidth=0,vmin=0, vmax=1)
139   -
  165 +# plot track of the last positions (snake)
  166 +if not SLE.empty:
  167 + for spot, group in SLE.groupby(['SPOT']):
  168 + latitude = group.LAT.values
  169 + longitude = group.LON.values
  170 + t = (group.TIME.values - float(dt)) / diff
  171 + x,y = m(longitude, latitude)
  172 + cax = plt.scatter(x,y,c=t, s=45, cmap=cmap,linewidth=0,vmin=0, vmax=1)
  173 +
  174 +if SLE.empty:
  175 + cax = plt.scatter(-1,-1,c=1, s=35, cmap=cmap,linewidth=0,vmin=0, vmax=1)
  176 +
140 177 # colorbar
141 178 cbar = fig.colorbar(cax,fraction=0.03, pad=0.03)
142 179 cbar.set_ticks([0,0.25,0.5,0.75,1])
... ... @@ -146,34 +183,29 @@ cbar.set_label(&#39;heures&#39;, rotation=270,labelpad=15)
146 183  
147 184 # legende
148 185 plt.annotate(strtime + ' HAE', xy=(1, 1), xycoords='data', size=20,
149   - xytext=(0.9, 0.1), textcoords='axes fraction',
150   - horizontalalignment='right', verticalalignment='top')
  186 + xytext=(0.9, 0.1), textcoords='axes fraction',
  187 + horizontalalignment='right', verticalalignment='top')
151 188 plt.title(u"Dérive de surface (" + str(snake) + u" dernières heures)")
152   -#enregistrement
153   -plt.savefig('/home/nicopa01/data/spot/suivi/SLE/SLE_' + nametime + '.jpg',dpi=300)
154   -plt.close()
155   -
156   -
157   -# Baie des chaleurs ---------------------------
158   -# traces des x derniers jours dans la baie
  189 +# recording
  190 +plt.savefig(dir_carte + '/SLE/SLE_' + nametime + '.png',dpi=300)
159 191  
160   -bdc = df.loc[(df.SPOT == 24) | (df.SPOT == 23) | (df.SPOT == 52) |
161   - (df.SPOT == 53) | (df.SPOT >= 700) & (df.SPOT < 899)]
162   -
163   -# Baie des chaleurs ---------------------------
164   -# traces des x derniers jours dans la baie
165   -BDC = bdc.loc[(df.FLAG == "OCN") & (df.TIME <= timestamp)
166   - & (df.TIME > timestamp - shour*snake)]
167   -
168   -BDC_start = bdc.loc[(df.FLAG == "OCN") & (df.TIME <= timestamp) & (df.TIME > starT)]
  192 +plt.close()
169 193  
  194 +#===================================================
  195 +# Baie des chaleurs
  196 +#===================================================
  197 +# selection of the reliable positions
  198 +BDC = df[(df.FLAG == "OCN")
  199 + & (df.TIME <= timestamp) & (df.TIME > timestamp - shour*snake)]
  200 +BDC_start = df.loc[(df.FLAG == "OCN") & (df.TIME <= timestamp)
  201 + & (df.TIME > starT)]
170 202 fig, ax = plt.subplots()
171 203  
172   -# caracteristiques de la carte
  204 +# map setting
173 205 m = Basemap(projection='stere',lon_0=-68.5,lat_0=48,\
174   - llcrnrlat=47.5,urcrnrlat=48.7,\
175   - llcrnrlon=-66.9,urcrnrlon=-64.2,\
176   - rsphere=6371.2,resolution='h')
  206 + llcrnrlat=47.5,urcrnrlat=48.9,\
  207 + llcrnrlon=-66.9,urcrnrlon=-63.8,\
  208 + rsphere=6371.2,resolution='h')
177 209  
178 210 m.drawmapboundary(fill_color='lightblue') # fill to edge
179 211 m.drawcountries()
... ... @@ -183,21 +215,25 @@ m.drawcoastlines(linewidth=0.7)
183 215 m.drawparallels(np.arange(40,60,0.5),labels=[1,0,0,0],linewidth=0.0)
184 216 m.drawmeridians(np.arange(-80,-40,1),labels=[0,0,0,1],linewidth=0.0)
185 217  
186   -# trace depuis le début de l'ete
  218 +# plot track from the start date
187 219 for spot, group in BDC_start.groupby(['SPOT']):
188 220 lat = group.LAT.values
189 221 lon = group.LON.values
190 222 x,y = m(lon, lat)
191 223 plt.plot(x,y,color='0.35',linewidth=0.7)
192 224  
193   -# trace les chemins de bouees
194   -for spot, group in BDC.groupby(['SPOT']):
195   - latitude = group.LAT.values
196   - longitude = group.LON.values
197   - t = (group.TIME.values - float(dt)) / diff
198   - x,y = m(longitude, latitude)
199   - cax = plt.scatter(x,y,c=t, s=45, cmap=cmap,linewidth=0,vmin=0, vmax=1)
200   -
  225 +# plot track of the last positions (snake)
  226 +if not BDC.empty:
  227 + for spot, group in BDC.groupby(['SPOT']):
  228 + latitude = group.LAT.values
  229 + longitude = group.LON.values
  230 + t = (group.TIME.values - float(dt)) / diff
  231 + x,y = m(longitude, latitude)
  232 + cax = plt.scatter(x,y,c=t, s=45, cmap=cmap,linewidth=0,vmin=0, vmax=1)
  233 +
  234 +if BDC.empty:
  235 + cax = plt.scatter(-1,-1,c=1, s=35, cmap=cmap,linewidth=0,vmin=0, vmax=1)
  236 +
201 237 # colorbar
202 238 cbar = fig.colorbar(cax,fraction=0.03, pad=0.03)
203 239 cbar.set_ticks([0,0.25,0.5,0.75,1])
... ... @@ -207,10 +243,11 @@ cbar.set_label(&#39;heures&#39;, rotation=270,labelpad=15)
207 243  
208 244 # legende
209 245 plt.annotate(strtime + ' HAE', xy=(1, 1), xycoords='data', size=20,
210   - xytext=(0.6, 0.1), textcoords='axes fraction',
211   - horizontalalignment='right', verticalalignment='top')
212   -
  246 + xytext=(0.6, 0.1), textcoords='axes fraction',
  247 + horizontalalignment='right', verticalalignment='top')
  248 +
213 249 plt.title(u"Dérive de surface (" + str(snake) + u" dernières heures)")
214   -#enregistrement
215   -plt.savefig('/home/nicopa01/data/spot/suivi/BDC/BDC_' + nametime + '.jpg',dpi=300)
  250 +# recording
  251 +plt.savefig(dir_carte + '/BDC/BDC_' + nametime + '.png',dpi=300)
216 252 plt.close()
  253 +
... ...