-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_ppi_blockage_map.py
232 lines (196 loc) · 6.33 KB
/
plot_ppi_blockage_map.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
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
"""Plot a 2-panel figure of radar and lidar availability PPIs.
Looks for files in directory `inpath` called
- xband_obs_pct_{startdate}_{enddate}_pct.txt
- xband_obs_pct_{startdate}_{enddate}_range.txt
- xband_obs_pct_{startdate}_{enddate}_azimuth.txt
- lidar_obs_pct_{startdate}_{enddate}_pct.txt
- lidar_obs_pct_{startdate}_{enddate}_range.txt
- lidar_obs_pct_{startdate}_{enddate}_azimuth.txt
Author: Jenna Ritvanen <[email protected]>
"""
import os
import sys
import argparse
from datetime import datetime
import numpy as np
import pandas as pd
import matplotlib as mlt
mlt.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from mpl_toolkits.axes_grid1 import make_axes_locatable
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import cartopy.crs as ccrs
plt.style.use("./presentation.mplstyle")
from radar_plotting import plotting
import contextily as ctx
from pathlib import Path
centerpoint = (24.87608, 60.28233)
airport_aws = (24.95675, 60.32670)
COPYRIGHT_TEXT = "Map tiles by Stamen Design, under CC BY 3.0. \nMap data by OpenStreetMap, under ODbL."
@mlt.ticker.FuncFormatter
def m2km_formatter(x, pos):
return f"{x / 1000:.0f}"
if __name__ == "__main__":
argparser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
argparser.add_argument("startdate", type=str, help="the startdate (YYYYmm)")
argparser.add_argument("enddate", type=str, help="the enddate (YYYYmm)")
argparser.add_argument(
"inpath", type=str, help="Path where the input files are located"
)
argparser.add_argument(
"--ext",
type=str,
default="png",
choices=["pdf", "png"],
help="Output plot file format.",
)
argparser.add_argument("--outpath", type=str, default=".", help="Output path")
argparser.add_argument(
"--maxdist",
type=float,
default=15,
help="Maximum distance plotted in figures in km",
)
argparser.add_argument(
"--dpi", type=int, default=300, help="Dots per inch in figure"
)
args = argparser.parse_args()
outpath = Path(args.outpath)
outpath.mkdir(parents=True, exist_ok=True)
inpath = Path(args.inpath).resolve()
startdate = datetime.strptime(args.startdate, "%Y%m")
enddate = (
datetime.strptime(args.enddate, "%Y%m") + pd.offsets.MonthEnd(0)
).to_pydatetime()
pct_xband = np.loadtxt(
inpath / f"xband_obs_pct_{startdate:%Y%m%d}_{enddate:%Y%m%d}_pct.txt"
)
xband_rr = np.loadtxt(
inpath / f"xband_obs_pct_{startdate:%Y%m%d}_{enddate:%Y%m%d}_range.txt"
)
xband_az = np.loadtxt(
inpath / f"xband_obs_pct_{startdate:%Y%m%d}_{enddate:%Y%m%d}_azimuth.txt"
)
pct_lidar = np.loadtxt(
inpath / f"lidar_obs_pct_{startdate:%Y%m%d}_{enddate:%Y%m%d}_pct.txt"
)
lidar_rr = np.loadtxt(
inpath / f"lidar_obs_pct_{startdate:%Y%m%d}_{enddate:%Y%m%d}_range.txt"
)
lidar_az = np.loadtxt(
inpath / f"lidar_obs_pct_{startdate:%Y%m%d}_{enddate:%Y%m%d}_azimuth.txt"
)
outfn = os.path.join(outpath, f"meas_pct_map.{args.ext}")
cbar_ax_kws = {
"width": "5%", # width = 5% of parent_bbox width
"height": "100%",
"loc": "lower left",
"bbox_to_anchor": (1.01, 0.0, 1, 1),
"borderpad": 0,
}
fig = plt.figure(figsize=(12, 10))
ax_lidar, fig, aeqd, ext = plotting.axes_with_background_map(
centerpoint, 15, 10, fig=fig, no_map=True, map="toner-line", ncols=2, index=1
)
ctx.add_basemap(
ax_lidar, crs=aeqd, zorder=9, zoom=11, source=ctx.providers.Stamen.TonerLite
)
p = plotting.plot_ppi(
ax_lidar,
pct_lidar,
lidar_az,
lidar_rr,
rasterized=True,
vmin=0,
vmax=1,
cmap="viridis",
zorder=100,
alpha=0.7,
linewidth=0,
antialiased=True,
edgecolor="none",
)
ax_lidar.scatter(
*airport_aws,
s=75,
transform=ccrs.PlateCarree(),
zorder=110,
label="Helsinki Airport",
marker="X",
color="k",
)
ax_radar, fig, aeqd, ext = plotting.axes_with_background_map(
centerpoint,
15,
10,
fig=fig,
no_map=True,
map="toner-line",
sharey=None,
ncols=2,
index=2,
)
ctx.add_basemap(
ax_radar, crs=aeqd, zorder=9, zoom=11, source=ctx.providers.Stamen.TonerLite
)
p = plotting.plot_ppi(
ax_radar,
pct_xband,
xband_az,
xband_rr,
rasterized=True,
vmin=0,
vmax=1,
cmap="viridis",
zorder=100,
alpha=0.7,
linewidth=0,
antialiased=True,
edgecolor="none",
)
ax_radar.scatter(
*airport_aws,
s=75,
transform=ccrs.PlateCarree(),
zorder=110,
label="Helsinki Airport",
marker="X",
color="k",
)
cax = inset_axes(ax_radar, bbox_transform=ax_radar.transAxes, **cbar_ax_kws)
cbar = plt.colorbar(p, orientation="vertical", cax=cax, ax=None)
cbar.set_label("Fraction", weight="bold")
cbar.ax.tick_params(labelsize=12)
for ax, title in zip([ax_lidar, ax_radar], ["(a) Lidar", "(b) X-band radar"]):
plotting.set_ticks_km(
ax,
[
-args.maxdist * 1e3,
args.maxdist * 1e3,
-args.maxdist * 1e3,
args.maxdist * 1e3,
],
16,
16,
)
# x-axis
ax.set_xlabel("Distance from site [km]", weight="bold", size="medium")
ax.set_title(title, y=-0.15, size="large")
ax.xaxis.set_major_formatter(m2km_formatter)
# y-axis
ax.set_ylabel("Distance from site [km]", weight="bold", size="medium")
ax.yaxis.set_major_formatter(m2km_formatter)
ax.set_xlim([-args.maxdist * 1e3, args.maxdist * 1e3])
ax.set_ylim([-args.maxdist * 1e3, args.maxdist * 1e3])
ax.tick_params(axis="both", which="major", labelsize="small")
ax.set_aspect(1)
ax.text(
0.75, 0.01, COPYRIGHT_TEXT, fontsize=4, zorder=100, transform=ax.transAxes
)
ax_radar.set_yticks([])
ax_radar.set_yticklabels([])
ax_radar.set_ylabel("")
fig.savefig(outfn, dpi=args.dpi, bbox_inches="tight")