-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlot_umi.py
226 lines (194 loc) · 6.36 KB
/
Plot_umi.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
# -*- coding: utf-8 -*-
"""
Umiujaq plotting script.
Based on the output of the Profile extraction code, plot the transects.
"""
import sys
from argparse import ArgumentParser
from pathlib import Path
import numpy as np
import pandas as pd
import pickle
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from matplotlib.colorbar import ColorbarBase
from matplotlib.colors import Normalize
from matplotlib.ticker import MultipleLocator
import cartopy.crs as ccrs
from geo_functs import open_large_raster
def main(infold, mns_path, epsg_code, outfile, titre):
"""Main plotting function."""
files = {}
for file in infold.iterdir():
files.update({file.name: file})
# Extract the info
nb = list(files.keys())[0].split("_")[-1].split(".")[0]
width = list(files.keys())[0].split("_")[-3]
azi = list(files.keys())[0].split("_")[-5]
# Open gdal raster
MNS_data, MNS_gt, MNS_ds = open_large_raster(str(mns_path))
# Open transect
data_tr = {}
for fname, pth in files.items():
if "transect" in fname:
with open(pth, "rb") as f:
transects = pickle.load(f)
else:
data_tr.update({"_".join(fname.split("_")[0:2]): pd.read_hdf(pth)})
# Get very approximate center of transects
midishline = transects[int(len(transects) / 2)]
mid_point = midishline.interpolate(0.5, normalized=True)
midpoint_buffer = mid_point.buffer(midishline.length / 2)
envelope = midpoint_buffer.envelope
# Turn interactive plotting off
plt.ioff()
# Create figure
fig = plt.figure(figsize=(15.4, 6.6))
fig.suptitle(titre)
# Epsg
proj_code = ccrs.epsg(epsg_code)
# 2 by 2 grid
gs = GridSpec(ncols=3, nrows=2, figure=fig, width_ratios=[0.1, 1.5, 4])
ax = plt.subplot(gs[0, 1], projection=proj_code)
ax1 = plt.subplot(gs[:, 0])
ax2 = plt.subplot(gs[-1, 1], projection=proj_code)
ax3 = plt.subplot(gs[:, -1])
# AX
mns_masked = np.ma.masked_where(MNS_data < 0, MNS_data)
extent = (
MNS_gt[0],
MNS_gt[0] + MNS_ds.RasterXSize * MNS_gt[1],
MNS_gt[3] + MNS_ds.RasterYSize * MNS_gt[5],
MNS_gt[3],
)
ax.imshow(
mns_masked, extent=extent, origin="upper", cmap="gist_earth"
)
ax.plot(
[midishline.coords[0][0], midishline.coords[-1][0]],
[midishline.coords[0][1], midishline.coords[-1][1]],
linestyle="-",
color="red",
linewidth=1,
)
norm = Normalize(vmin=np.min(mns_masked), vmax=np.max(mns_masked))
cbar = ColorbarBase(
ax1, cmap=plt.get_cmap("gist_earth"), norm=norm, orientation="vertical"
)
cbar.ax.yaxis.set_label_position("left")
cbar.ax.set_ylabel("Altitude / m")
# AX2
ax2.imshow(
mns_masked, extent=extent, origin="upper", cmap="gist_earth"
)
for line in transects:
ax2.plot(
[line.coords[0][0], line.coords[-1][0]],
[line.coords[0][1], line.coords[-1][1]],
linestyle="-",
color="black",
alpha=0.6,
linewidth=0.5,
)
ax2.set_extent(
[
envelope.bounds[0],
envelope.bounds[2],
envelope.bounds[1],
envelope.bounds[-1],
],
crs=ccrs.epsg(epsg_code),
)
ax2.set_title("Zoom on transects", y=-0.2)
# AX3
# Plot MNT/ MNS ground
data_tr["MNT_solnu"].T.plot(
ax=ax3, color="sienna", alpha=0.1, legend=False
)
data_tr["MNT_solnu"].T.mean(axis=1).plot(
ax=ax3, color="sienna", legend=True, label="Mean summer DTM"
)
data_tr["MNS_solnu"].T.plot(
ax=ax3, color="lightgreen", alpha=0.1, legend=False
)
data_tr["MNS_solnu"].T.mean(axis=1).plot(
ax=ax3, color="lightgreen", legend=True, label="Mean summer DSM"
)
# Plot MNS neige
data_tr["MNS_neige"].T.plot(
ax=ax3, color="midnightblue", alpha=0.2, legend=False
)
data_tr["MNS_neige"].T.mean(axis=1).plot(
ax=ax3, color="midnightblue", legend=True, label="Mean winter DSM"
)
ax3.set_title(
"Azimuth: %s°, Width: %sm, # of transects: %s" % (azi, width, nb)
)
ax3.set_xlabel("Distance along transect / m")
ax3.set_ylabel("Altitude / m")
ax3.set_xlim(0, midishline.length)
ax3.set_ylim(
np.nanmin(data_tr["MNT_solnu"].T.mean(axis=1)) - 5,
np.nanmax(data_tr["MNS_neige"].T.mean(axis=1)) + 5,
)
ax3.xaxis.set_major_locator(MultipleLocator(10))
ax3.xaxis.set_minor_locator(MultipleLocator(5))
ax3.yaxis.set_major_locator(MultipleLocator(1))
ax3.yaxis.set_minor_locator(MultipleLocator(0.5))
ax3.xaxis.set_ticks_position("both")
ax3.yaxis.set_ticks_position("both")
ax3.tick_params(direction="inout", which="both")
fig.savefig(infold.joinpath(outfile), bbox_inches="tight", dpi=300)
if __name__ == "__main__":
# If no arguments, return a help message
if len(sys.argv) == 1:
print(
'No arguments provided. Please run the command: "python %s -h"'
"for help." % sys.argv[0]
)
sys.exit(2)
else:
# Parse Arguments from command line
parser = ArgumentParser(
description="Import parameters for the Umiujaq plotting"
)
parser.add_argument(
"--infold",
metavar="Input path",
required=True,
help="Path to the folder containg the MNS and MNT"
" neige and solnu extracted data",
)
parser.add_argument(
"--mns", metavar="MNS", required=True, help="Path to MNS solnu"
)
parser.add_argument(
"--epsg",
metavar="Epsg code of the data",
required=True,
type=int,
help="EPSG code",
)
parser.add_argument(
"--outfile",
metavar="Outfolder",
required=True,
help="Path to the saved pdf",
)
parser.add_argument(
"--titre",
metavar="Figure title",
required=False,
default="Transect",
type=str,
help="Title for the figure",
)
input_args = parser.parse_args()
# Run main
main(
Path(input_args.infold),
input_args.mns,
input_args.epsg,
input_args.outfile,
input_args.titre,
)