-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_stations.py
188 lines (155 loc) · 6.88 KB
/
plot_stations.py
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
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
import mpld3
from mpld3 import plugins
import matplotlib.gridspec as gridspec # GRIDSPEC !
import xarray as xr
from matplotlib.colorbar import Colorbar
def createMapProjections(lat0, lon0, region='Whole'):
#m = Basemap(projection='hammer',lon_0=270)
# plot just upper right quadrant (corners determined from global map).
# keywords llcrnrx,llcrnry,urcrnrx,urcrnry used to define the lower
# left and upper right corners in map projection coordinates.
# llcrnrlat,llcrnrlon,urcrnrlon,urcrnrlat could be used to define
# lat/lon values of corners - but this won't work in cases such as this
# where one of the corners does not lie on the earth.
#m1 = Basemap(projection='ortho',lon_0=-9,lat_0=60,resolution=None)
#m = Basemap(projection='cyl', llcrnrlon=-120, llcrnrlat=-80, urcrnrlon=55,
# urcrnrlat=-30, lat_0 = -74, lon_0 =-43);
m1 = Basemap(projection='ortho', lat_0 =lat0, lon_0 =lon0, resolution='l');
width = m1.urcrnrx - m1.llcrnrx
height = m1.urcrnry - m1.llcrnry
dist_x = 8199.491128244028
w_factor = 0.3
h_factor = 0.3
if(region =='Whole'):
m = Basemap(projection='ortho', lat_0=lat0, lon_0=lon0, resolution='l', llcrnrx=-width*w_factor, llcrnry=-height*h_factor, urcrnrx=w_factor*width, urcrnry=h_factor*height)
elif(region == 'Weddell'):
m = Basemap(projection='ortho', lat_0=lat0, lon_0=lon0, resolution='l', llcrnrx=-width*0.225, llcrnry=-0.0*0.1*height, urcrnrx=0.*width, urcrnry=0.20*height)
else:
raise Exception('region should be either \'Whole\' or \'Weddell\' ')
m.drawmapboundary();
m.readshapefile("/media/data/Datasets/Shapefiles/AntarcticGroundingLine/GSHHS_f_L6", "GSHHS_f_L6", color='0.75', linewidth=0.1)
#m.fillcontinents(color='#ddaa66');
m.drawcoastlines(linewidth=0.2)
parallels = np.arange(-80, -50+1, 5.)
m.drawparallels(parallels,labels=[1,1,1,0], linewidth=0.2)
meridians = np.arange(-180, 180, 20.)
m.drawmeridians(meridians,labels=[1,1,0,1], linewidth=0.2)
return m
def plot_station_locations_with_mouseover(positions, title=' ', save=False, savename="untitled.png", wd=12, ht=12, region='Whole', markers=None):
css = """
table
{
border-collapse: collapse;
}
th
{
color: #ffffff;
background-color: #000000;
}
td
{
background-color: #cccccc;
}
table, th, td
{
font-family:Arial, Helvetica, sans-serif;
border: 1px solid black;
text-align: right;
}
"""
x = positions[:,1]
y = positions[:,0]
fig = plt.figure(figsize=(wd,ht));
ax = plt.axes()
lat0 = -90
lon0 = 0
m = createMapProjections(lat0, lon0, region=region)
xgrid,ygrid = m(x,y)
m.drawmapboundary();
m.drawcoastlines()
#m.fillcontinents(color='#cc9966');
m.readshapefile("/media/data/Datasets/Shapefiles/AntarcticGroundingLine/GSHHS_f_L6", "GSHHS_f_L6", color='m')
## if(markers != None):
## for i in range(len(markers)):
## plt.text(xgrid[i],ygrid[i], str(markers[i]), color='b');
plt.title(title, y=1.07)
parallels = np.arange(-80, -30+1, 5.)
labels = [1,1,1,0]
m.drawparallels(parallels,labels=labels)
meridians = np.arange(-180, 180, 20.)
labels = [1,1,0,1]
m.drawmeridians(meridians, labels=labels)
points_with_annotation = []
for i in range(len(markers)):
scat, = m.plot(xgrid[i],ygrid[i],'o',color='b');
annotation = ax.annotate("%s" % markers[i],
xy=(xgrid[i], ygrid[i]), xycoords='data',
xytext=(xgrid[i], ygrid[i]), textcoords='data',
horizontalalignment="left",
arrowprops=dict(arrowstyle="simple",
connectionstyle="arc3,rad=-0.2"),
bbox=dict(boxstyle="round", facecolor="w",
edgecolor="0.5", alpha=0.9)
)
# by default, disable the annotation visibility
annotation.set_visible(False)
points_with_annotation.append([scat, annotation])
def on_move(event):
visibility_changed = False
for scat, annotation in points_with_annotation:
should_be_visible = (scat.contains(event)[0] == True)
if should_be_visible != annotation.get_visible():
visibility_changed = True
annotation.set_visible(should_be_visible)
if visibility_changed:
plt.draw()
on_move_id = fig.canvas.mpl_connect('motion_notify_event', on_move)
if(save== True):
plt.savefig(savename)
plt.show();
def plot_station_locations(positions, title=' ', save=False, savename="untitled.png", wd=12, ht=12, region='Whole', plotBathy=True):
x = positions[:,1]
y = positions[:,0]
lat0 = -90
lon0 = 0
plt.figure(1, figsize=(wd,ht));
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 0.05])
mapax = plt.subplot(gs[0, 0])
m = createMapProjections(lat0, lon0, region=region)
m.drawmapboundary();
m.drawcoastlines()
#m.fillcontinents(color='#cc9966');
m.readshapefile("/media/data/Datasets/Shapefiles/AntarcticGroundingLine/GSHHS_f_L6", "GSHHS_f_L6", color='m')
## if(markers != None):
## for i in range(len(markers)):
## plt.text(xgrid[i],ygrid[i], str(markers[i]), color='b');
parallels = np.arange(-80, -30+1, 5.)
labels = [1,1,1,0]
m.drawparallels(parallels,labels=labels)
meridians = np.arange(-180, 180, 20.)
labels = [1,1,0,1]
m.drawmeridians(meridians, labels=labels)
m.scatter(x, y, latlon=True, color='r', s=0.25, marker='.')
if(plotBathy == True):
bathy = xr.open_dataset('/media/data/Datasets/Bathymetry/GEBCO_2014_2D.nc')
lonlen = len(bathy.lon)
lonindices = np.arange(0, lonlen+1, 30)
lonindices[-1] = lonindices[-1] - 1
bathyS = bathy.isel(lon=lonindices, lat=np.arange(0, 3600, 5))
clevs = np.array([-100, -500, -1000, -1500, -2000, -3000,-6000])[::-1]
longrid, latgrid = np.meshgrid(bathyS.lon.values, bathyS.lat.values)
cs = m.contour(longrid, latgrid, bathyS.elevation.where(bathyS.elevation <= 0).values, latlon=True, levels=clevs, linewidths=0.2, extend='min', ax=mapax) #, , cmap='rainbow' , levels=clevs,
## plt.figure(2)
## cf = plt.contourf(longrid, latgrid,bathyS.elevation.where(bathyS.elevation <= 0).values, levels=clevs, extend='min') #, , cmap='rainbow' , levels=clevs,
## plt.figure(1)
bathycolorbar = plt.subplot(gs[1, 0])
cbar1 = Colorbar(ax = bathycolorbar, mappable = cs, orientation = 'horizontal')
cbar1.ax.get_children()[0].set_linewidths(5)
cbar1.set_label('Depth (m)')
if(save == True):
plt.savefig(savename, dpi=900)
plt.show()