rsplot 0.3.1.dev3__py3-none-any.whl → 0.3.2.dev37__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rsplot/atomic.py +36 -0
- rsplot/cli.py +282 -469
- rsplot/commands/common.py +113 -1
- rsplot/commands/fnr.py +389 -104
- rsplot/commands/forecast.py +24 -7
- rsplot/commands/meteo.py +499 -186
- rsplot/commands/overlay.py +292 -205
- rsplot/commands/radius.py +526 -0
- rsplot/commands/raster.py +479 -230
- rsplot/commands/reconstruct.py +27 -6
- rsplot/commands/sidecar.py +106 -0
- rsplot/commands/station.py +154 -42
- rsplot/commands/station_trend.py +259 -174
- rsplot/config.py +46 -16
- rsplot/data_files.py +253 -0
- rsplot/errors.py +43 -0
- rsplot/fnr.py +38 -46
- rsplot/geo/boundaries.py +317 -11
- rsplot/geo/footprints.py +205 -0
- rsplot/geo/processing.py +84 -5
- rsplot/geo/radius.py +205 -0
- rsplot/plotting/colormap_22.txt +256 -0
- rsplot/plotting/colormaps.py +24 -29
- rsplot/plotting/map_frame.py +203 -9
- rsplot/plotting/meteo.py +10 -3
- rsplot/plotting/overlay.py +36 -40
- rsplot/plotting/raster.py +15 -3
- rsplot/plotting/station.py +86 -33
- rsplot/plotting/station_focus.py +84 -0
- rsplot/plotting/station_trend.py +60 -47
- rsplot/plotting/styles.py +3 -5
- rsplot/readers/__init__.py +89 -7
- rsplot/readers/_tropomi.py +121 -3
- rsplot/readers/base.py +16 -1
- rsplot/readers/era5.py +110 -10
- rsplot/readers/era5_wind.py +3 -2
- rsplot/readers/gems.py +429 -0
- rsplot/readers/guokong.py +125 -39
- rsplot/readers/netcdf.py +32 -0
- rsplot/readers/tropomi_hcho.py +1 -1
- rsplot/readers/tropomi_no2.py +1 -1
- rsplot/readers/tropomi_nrti.py +116 -0
- rsplot/readers/tropomi_o3.py +1 -1
- rsplot/readers/tropomi_o3pr.py +1 -1
- rsplot/result_builders/__init__.py +0 -14
- rsplot/result_builders/common.py +84 -0
- rsplot/result_builders/envelope.py +236 -0
- rsplot/result_builders/fnr.py +47 -131
- rsplot/result_builders/forecast.py +7 -0
- rsplot/result_builders/provenance.py +59 -0
- rsplot/result_builders/reconstruct.py +7 -0
- rsplot/result_builders/satellite.py +240 -0
- rsplot/result_builders/series_layer.py +289 -0
- rsplot/result_builders/station.py +23 -0
- rsplot/result_builders/station_layer.py +326 -0
- rsplot/results.py +0 -12
- rsplot/schemas/result.schema.json +566 -0
- rsplot/temporal.py +146 -16
- rsplot-0.3.2.dev37.dist-info/METADATA +996 -0
- rsplot-0.3.2.dev37.dist-info/RECORD +76 -0
- {rsplot-0.3.1.dev3.dist-info → rsplot-0.3.2.dev37.dist-info}/WHEEL +1 -1
- rsplot/commands/recent.py +0 -164
- rsplot/recent.py +0 -404
- rsplot/result_builders/meteo.py +0 -171
- rsplot/result_builders/overlay.py +0 -75
- rsplot/result_builders/raster.py +0 -72
- rsplot/result_builders/station_trend.py +0 -229
- rsplot-0.3.1.dev3.dist-info/METADATA +0 -477
- rsplot-0.3.1.dev3.dist-info/RECORD +0 -64
- {rsplot-0.3.1.dev3.dist-info → rsplot-0.3.2.dev37.dist-info}/entry_points.txt +0 -0
- {rsplot-0.3.1.dev3.dist-info → rsplot-0.3.2.dev37.dist-info}/licenses/LICENSE +0 -0
rsplot/plotting/map_frame.py
CHANGED
|
@@ -9,7 +9,6 @@ draw order stays explicit.
|
|
|
9
9
|
from __future__ import annotations
|
|
10
10
|
|
|
11
11
|
from dataclasses import dataclass
|
|
12
|
-
from pathlib import Path
|
|
13
12
|
from typing import TYPE_CHECKING
|
|
14
13
|
|
|
15
14
|
import cartopy.crs as ccrs
|
|
@@ -24,7 +23,9 @@ from rsplot.tiles.tianditu import TianDiTuTiles
|
|
|
24
23
|
|
|
25
24
|
if TYPE_CHECKING:
|
|
26
25
|
from cartopy.mpl.geoaxes import GeoAxes
|
|
26
|
+
from matplotlib.artist import Artist
|
|
27
27
|
from matplotlib.figure import Figure
|
|
28
|
+
from shapely.geometry.base import BaseGeometry
|
|
28
29
|
|
|
29
30
|
from rsplot.geo.boundaries import RegionInfo
|
|
30
31
|
|
|
@@ -67,7 +68,11 @@ def create_map_frame(
|
|
|
67
68
|
projection = tiles.crs
|
|
68
69
|
else:
|
|
69
70
|
tiles = None
|
|
70
|
-
projection =
|
|
71
|
+
projection = (
|
|
72
|
+
ccrs.AzimuthalEquidistant(*region.projection_center)
|
|
73
|
+
if region.projection_center is not None
|
|
74
|
+
else ccrs.PlateCarree()
|
|
75
|
+
)
|
|
71
76
|
|
|
72
77
|
fig, ax = plt.subplots(
|
|
73
78
|
figsize=figure_size_for_level(region.level),
|
|
@@ -90,13 +95,27 @@ def add_basemap_tiles(frame: MapFrame, region: RegionInfo) -> None:
|
|
|
90
95
|
|
|
91
96
|
def add_region_boundaries(ax: GeoAxes, region: RegionInfo) -> None:
|
|
92
97
|
"""Draw detail, subdivision, and main boundaries in established order."""
|
|
98
|
+
selected = {
|
|
99
|
+
member.geometry.normalize().wkb
|
|
100
|
+
for member in getattr(region, "members", ())
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
def visible_geometries(frame):
|
|
104
|
+
if not selected:
|
|
105
|
+
return frame.geometry
|
|
106
|
+
return [
|
|
107
|
+
geometry
|
|
108
|
+
for geometry in frame.geometry
|
|
109
|
+
if geometry.normalize().wkb not in selected
|
|
110
|
+
]
|
|
111
|
+
|
|
93
112
|
if (
|
|
94
113
|
region.detail_boundary_gdf is not None
|
|
95
114
|
and len(region.detail_boundary_gdf) > 0
|
|
96
115
|
):
|
|
97
116
|
ax.add_feature(
|
|
98
117
|
ShapelyFeature(
|
|
99
|
-
region.detail_boundary_gdf
|
|
118
|
+
visible_geometries(region.detail_boundary_gdf),
|
|
100
119
|
ccrs.PlateCarree(),
|
|
101
120
|
edgecolor="#aaa",
|
|
102
121
|
facecolor="none",
|
|
@@ -112,7 +131,7 @@ def add_region_boundaries(ax: GeoAxes, region: RegionInfo) -> None:
|
|
|
112
131
|
is_county_sub = region.level == "city"
|
|
113
132
|
ax.add_feature(
|
|
114
133
|
ShapelyFeature(
|
|
115
|
-
region.sub_boundary_gdf
|
|
134
|
+
visible_geometries(region.sub_boundary_gdf),
|
|
116
135
|
ccrs.PlateCarree(),
|
|
117
136
|
edgecolor="#888" if not is_county_sub else "#666",
|
|
118
137
|
facecolor="none",
|
|
@@ -133,6 +152,21 @@ def add_region_boundaries(ax: GeoAxes, region: RegionInfo) -> None:
|
|
|
133
152
|
)
|
|
134
153
|
)
|
|
135
154
|
|
|
155
|
+
if region.geometry_kind == "station_radius":
|
|
156
|
+
from dataclasses import replace
|
|
157
|
+
|
|
158
|
+
add_subdivision_labels(
|
|
159
|
+
ax,
|
|
160
|
+
replace(
|
|
161
|
+
region,
|
|
162
|
+
sub_boundary_gdf=region.main_boundary_gdf,
|
|
163
|
+
subdivision_level="city",
|
|
164
|
+
),
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
if getattr(region, "members", ()):
|
|
168
|
+
add_member_labels(ax, region)
|
|
169
|
+
|
|
136
170
|
|
|
137
171
|
def add_subdivision_labels(ax: GeoAxes, region: RegionInfo) -> None:
|
|
138
172
|
"""Add name labels for a region's subdivision features."""
|
|
@@ -143,8 +177,38 @@ def add_subdivision_labels(ax: GeoAxes, region: RegionInfo) -> None:
|
|
|
143
177
|
name_field = region.sub_name_field
|
|
144
178
|
fontsize = region.params.label_fontsize
|
|
145
179
|
|
|
180
|
+
selected_geometry = {
|
|
181
|
+
m.geometry.normalize().wkb for m in getattr(region, "members", ())
|
|
182
|
+
}
|
|
183
|
+
seen_labels = set()
|
|
146
184
|
for _, row in gdf.iterrows():
|
|
147
|
-
|
|
185
|
+
key = row.geometry.normalize().wkb
|
|
186
|
+
if key in selected_geometry or (row[name_field], key) in seen_labels:
|
|
187
|
+
continue
|
|
188
|
+
seen_labels.add((row[name_field], key))
|
|
189
|
+
geometry = row.geometry
|
|
190
|
+
if region.geometry_kind == "station_radius":
|
|
191
|
+
from shapely.geometry import box
|
|
192
|
+
|
|
193
|
+
west, east, south, north = region.extent
|
|
194
|
+
pad = region.params.pad
|
|
195
|
+
geometry = geometry.intersection(
|
|
196
|
+
box(west - pad, south - pad, east + pad, north + pad)
|
|
197
|
+
)
|
|
198
|
+
if geometry.is_empty:
|
|
199
|
+
continue
|
|
200
|
+
if region.subdivision_level == "city":
|
|
201
|
+
upper = geometry.intersection(
|
|
202
|
+
box(
|
|
203
|
+
west - pad,
|
|
204
|
+
south + (north - south) * 0.85,
|
|
205
|
+
east + pad,
|
|
206
|
+
north + pad,
|
|
207
|
+
)
|
|
208
|
+
)
|
|
209
|
+
if not upper.is_empty:
|
|
210
|
+
geometry = upper
|
|
211
|
+
point = geometry.representative_point()
|
|
148
212
|
if region.level in (
|
|
149
213
|
"key_region",
|
|
150
214
|
"province",
|
|
@@ -168,6 +232,31 @@ def add_subdivision_labels(ax: GeoAxes, region: RegionInfo) -> None:
|
|
|
168
232
|
)
|
|
169
233
|
|
|
170
234
|
|
|
235
|
+
def add_member_labels(ax: GeoAxes, region: RegionInfo) -> None:
|
|
236
|
+
"""Label selections separately from subdivisions; qualify duplicate names."""
|
|
237
|
+
from collections import Counter
|
|
238
|
+
|
|
239
|
+
names = Counter(member.name for member in region.members)
|
|
240
|
+
for member in region.members:
|
|
241
|
+
point = member.geometry.representative_point()
|
|
242
|
+
label = (
|
|
243
|
+
(member.qualified_name or member.name)
|
|
244
|
+
if names[member.name] > 1
|
|
245
|
+
else member.name
|
|
246
|
+
)
|
|
247
|
+
ax.text(
|
|
248
|
+
point.x,
|
|
249
|
+
point.y,
|
|
250
|
+
label,
|
|
251
|
+
transform=ccrs.PlateCarree(),
|
|
252
|
+
fontsize=region.params.label_fontsize,
|
|
253
|
+
ha="center",
|
|
254
|
+
va="center",
|
|
255
|
+
color="#222",
|
|
256
|
+
path_effects=[pe.withStroke(linewidth=2, foreground="white")],
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
|
|
171
260
|
def configure_map_axes(
|
|
172
261
|
ax: GeoAxes,
|
|
173
262
|
region: RegionInfo,
|
|
@@ -186,6 +275,14 @@ def configure_map_axes(
|
|
|
186
275
|
crs=ccrs.PlateCarree(),
|
|
187
276
|
)
|
|
188
277
|
|
|
278
|
+
if region.projection_center is not None and not use_basemap:
|
|
279
|
+
projected = ax.projection.project_geometry(
|
|
280
|
+
region.geometry, ccrs.PlateCarree()
|
|
281
|
+
)
|
|
282
|
+
west, south, east, north = projected.bounds
|
|
283
|
+
ax.set_xlim(west, east)
|
|
284
|
+
ax.set_ylim(south, north)
|
|
285
|
+
|
|
189
286
|
gridlines = ax.gridlines(
|
|
190
287
|
draw_labels=True,
|
|
191
288
|
linewidth=0.2,
|
|
@@ -195,8 +292,43 @@ def configure_map_axes(
|
|
|
195
292
|
gridlines.top_labels = gridlines.right_labels = False
|
|
196
293
|
gridlines.xformatter = LONGITUDE_FORMATTER
|
|
197
294
|
gridlines.yformatter = LATITUDE_FORMATTER
|
|
198
|
-
|
|
199
|
-
|
|
295
|
+
if region.projection_center is not None:
|
|
296
|
+
gridlines.rotate_labels = False
|
|
297
|
+
if region.members or region.geometry_kind == "station_radius":
|
|
298
|
+
gridlines.xlocator = mticker.MaxNLocator(nbins=5)
|
|
299
|
+
gridlines.ylocator = mticker.MaxNLocator(nbins=5)
|
|
300
|
+
else:
|
|
301
|
+
gridlines.xlocator = mticker.MultipleLocator(region.params.grid_step)
|
|
302
|
+
gridlines.ylocator = mticker.MultipleLocator(region.params.grid_step)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
TITLE_FONTSIZE = 14
|
|
306
|
+
SUBTITLE_FONTSIZE = 10
|
|
307
|
+
SUBTITLE_COLOR = "#555555"
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def set_map_title(
|
|
311
|
+
ax: GeoAxes, title: str, subtitle: str | None = None
|
|
312
|
+
) -> None:
|
|
313
|
+
"""Draw the title, with an optional grey subtitle just above the map."""
|
|
314
|
+
if not subtitle:
|
|
315
|
+
ax.set_title(title, fontsize=TITLE_FONTSIZE)
|
|
316
|
+
return
|
|
317
|
+
gap = 6 # points between the axes, the subtitle and the title
|
|
318
|
+
ax.annotate(
|
|
319
|
+
subtitle,
|
|
320
|
+
xy=(0.5, 1.0),
|
|
321
|
+
xycoords="axes fraction",
|
|
322
|
+
xytext=(0, gap),
|
|
323
|
+
textcoords="offset points",
|
|
324
|
+
ha="center",
|
|
325
|
+
va="bottom",
|
|
326
|
+
fontsize=SUBTITLE_FONTSIZE,
|
|
327
|
+
color=SUBTITLE_COLOR,
|
|
328
|
+
)
|
|
329
|
+
ax.set_title(
|
|
330
|
+
title, fontsize=TITLE_FONTSIZE, pad=2 * gap + 1.3 * SUBTITLE_FONTSIZE
|
|
331
|
+
)
|
|
200
332
|
|
|
201
333
|
|
|
202
334
|
def finalize_map_figure(
|
|
@@ -213,8 +345,70 @@ def finalize_map_figure(
|
|
|
213
345
|
fig.tight_layout(rect=layout_rect)
|
|
214
346
|
|
|
215
347
|
if output:
|
|
216
|
-
|
|
217
|
-
|
|
348
|
+
from rsplot.atomic import atomic_write
|
|
349
|
+
|
|
350
|
+
with atomic_write(output) as temp:
|
|
351
|
+
fig.savefig(temp, dpi=dpi, bbox_inches="tight")
|
|
218
352
|
plt.close(fig)
|
|
219
353
|
else:
|
|
220
354
|
plt.show()
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def clip_raster_to_region(
|
|
358
|
+
artist: Artist,
|
|
359
|
+
ax: GeoAxes,
|
|
360
|
+
region: RegionInfo,
|
|
361
|
+
*,
|
|
362
|
+
coverage_geometry: BaseGeometry | None = None,
|
|
363
|
+
) -> None:
|
|
364
|
+
"""Clip only the data layer, preserving holes and disconnected islands."""
|
|
365
|
+
import numpy as np
|
|
366
|
+
from matplotlib.path import Path as MplPath
|
|
367
|
+
from shapely.geometry import GeometryCollection, MultiPolygon
|
|
368
|
+
from shapely.geometry.polygon import orient
|
|
369
|
+
|
|
370
|
+
def orient_geometry(geometry: BaseGeometry) -> BaseGeometry:
|
|
371
|
+
if geometry.geom_type == "Polygon":
|
|
372
|
+
return orient(geometry, sign=1.0)
|
|
373
|
+
if geometry.geom_type == "MultiPolygon":
|
|
374
|
+
return MultiPolygon(
|
|
375
|
+
[orient(part, sign=1.0) for part in geometry.geoms]
|
|
376
|
+
)
|
|
377
|
+
if geometry.geom_type == "GeometryCollection":
|
|
378
|
+
return GeometryCollection(
|
|
379
|
+
[orient_geometry(part) for part in geometry.geoms]
|
|
380
|
+
)
|
|
381
|
+
return geometry
|
|
382
|
+
|
|
383
|
+
# Project the geometry into the axes coordinates before building a clip
|
|
384
|
+
# path, so both PlateCarree and satellite-basemap Mercator axes work.
|
|
385
|
+
clip_geometry = region.geometry
|
|
386
|
+
if coverage_geometry is not None:
|
|
387
|
+
clip_geometry = clip_geometry.intersection(coverage_geometry)
|
|
388
|
+
geometry = ax.projection.project_geometry(
|
|
389
|
+
orient_geometry(clip_geometry), ccrs.PlateCarree()
|
|
390
|
+
)
|
|
391
|
+
paths = []
|
|
392
|
+
|
|
393
|
+
def append_polygons(geometry: BaseGeometry) -> None:
|
|
394
|
+
if geometry.is_empty:
|
|
395
|
+
return
|
|
396
|
+
if geometry.geom_type == "Polygon":
|
|
397
|
+
polygon = orient(geometry, sign=1.0)
|
|
398
|
+
for ring in (polygon.exterior, *polygon.interiors):
|
|
399
|
+
vertices = np.asarray(ring.coords)[:, :2]
|
|
400
|
+
codes = np.full(len(vertices), MplPath.LINETO, dtype=np.uint8)
|
|
401
|
+
codes[0], codes[-1] = MplPath.MOVETO, MplPath.CLOSEPOLY
|
|
402
|
+
paths.append(MplPath(vertices, codes))
|
|
403
|
+
elif hasattr(geometry, "geoms"):
|
|
404
|
+
for part in geometry.geoms:
|
|
405
|
+
append_polygons(part)
|
|
406
|
+
|
|
407
|
+
append_polygons(geometry)
|
|
408
|
+
if not paths:
|
|
409
|
+
raise ValueError("行政区边界没有可用于裁剪的多边形。")
|
|
410
|
+
artist.set_clip_path(MplPath.make_compound_path(*paths), ax.transData)
|
|
411
|
+
# Cartopy can create an auxiliary collection for wrapped mesh cells.
|
|
412
|
+
wrapped = getattr(artist, "_wrapped_collection_fix", None)
|
|
413
|
+
if wrapped is not None:
|
|
414
|
+
wrapped.set_clip_path(MplPath.make_compound_path(*paths), ax.transData)
|
rsplot/plotting/meteo.py
CHANGED
|
@@ -15,7 +15,10 @@ from rsplot.plotting.map_frame import (
|
|
|
15
15
|
create_map_frame,
|
|
16
16
|
finalize_map_figure,
|
|
17
17
|
)
|
|
18
|
-
from rsplot.plotting.station import
|
|
18
|
+
from rsplot.plotting.station import (
|
|
19
|
+
add_station_colorbar,
|
|
20
|
+
draw_station_layer,
|
|
21
|
+
)
|
|
19
22
|
from rsplot.plotting.wind import draw_wind_vectors
|
|
20
23
|
|
|
21
24
|
if TYPE_CHECKING:
|
|
@@ -120,7 +123,9 @@ def plot_meteo(
|
|
|
120
123
|
field_colorbar.ax.tick_params(labelsize=8)
|
|
121
124
|
|
|
122
125
|
station_axis = ax.inset_axes([1.03, 0.04, 0.025, 0.42])
|
|
123
|
-
station_colorbar =
|
|
126
|
+
station_colorbar = add_station_colorbar(
|
|
127
|
+
fig, station_scatter, station.var_name, cax=station_axis
|
|
128
|
+
)
|
|
124
129
|
if station_colorbar_label:
|
|
125
130
|
station_colorbar.set_label(station_colorbar_label, fontsize=9)
|
|
126
131
|
station_colorbar.ax.tick_params(labelsize=8)
|
|
@@ -133,8 +138,10 @@ def plot_meteo(
|
|
|
133
138
|
)
|
|
134
139
|
field_colorbar.set_label(f"Temperature ({field.unit})")
|
|
135
140
|
elif station_scatter is not None:
|
|
136
|
-
station_colorbar =
|
|
141
|
+
station_colorbar = add_station_colorbar(
|
|
142
|
+
fig,
|
|
137
143
|
station_scatter,
|
|
144
|
+
station.var_name,
|
|
138
145
|
ax=ax,
|
|
139
146
|
shrink=0.7,
|
|
140
147
|
pad=0.05,
|
rsplot/plotting/overlay.py
CHANGED
|
@@ -12,24 +12,27 @@ from typing import TYPE_CHECKING
|
|
|
12
12
|
import cartopy.crs as ccrs
|
|
13
13
|
import numpy as np
|
|
14
14
|
|
|
15
|
-
from rsplot.plotting.colormaps import
|
|
16
|
-
STATION_CONCENTRATION_CMAP,
|
|
17
|
-
register_rsplot_colormaps,
|
|
18
|
-
)
|
|
15
|
+
from rsplot.plotting.colormaps import register_rsplot_colormaps
|
|
19
16
|
from rsplot.plotting.map_frame import (
|
|
20
17
|
add_basemap_tiles,
|
|
21
18
|
add_region_boundaries,
|
|
19
|
+
clip_raster_to_region,
|
|
22
20
|
configure_map_axes,
|
|
23
21
|
create_map_frame,
|
|
24
22
|
finalize_map_figure,
|
|
23
|
+
set_map_title,
|
|
25
24
|
)
|
|
26
25
|
from rsplot.plotting.station import (
|
|
27
|
-
|
|
26
|
+
add_station_colorbar,
|
|
27
|
+
build_station_colormap,
|
|
28
28
|
station_marker_size,
|
|
29
29
|
)
|
|
30
30
|
|
|
31
31
|
if TYPE_CHECKING:
|
|
32
|
+
from shapely.geometry.base import BaseGeometry
|
|
33
|
+
|
|
32
34
|
from rsplot.geo.boundaries import RegionInfo
|
|
35
|
+
from rsplot.plotting.station_focus import StationFocus
|
|
33
36
|
from rsplot.readers.guokong import StationData
|
|
34
37
|
|
|
35
38
|
|
|
@@ -52,9 +55,11 @@ def plot_overlay(
|
|
|
52
55
|
dpi: int = 300,
|
|
53
56
|
basemap: bool | None = None,
|
|
54
57
|
tianditu_key: str | None = None,
|
|
55
|
-
hcho_cmap_file: str | None = None,
|
|
56
58
|
title: str | None = None,
|
|
59
|
+
subtitle: str | None = None,
|
|
57
60
|
output: str | None = None,
|
|
61
|
+
coverage_geometry: BaseGeometry | None = None,
|
|
62
|
+
station_focus: StationFocus | None = None,
|
|
58
63
|
) -> None:
|
|
59
64
|
"""Draw satellite raster + station scatter on the same axes.
|
|
60
65
|
|
|
@@ -63,7 +68,7 @@ def plot_overlay(
|
|
|
63
68
|
colorbars share the right side — raster on top, stations below —
|
|
64
69
|
so each quantity keeps its own scale.
|
|
65
70
|
"""
|
|
66
|
-
register_rsplot_colormaps(
|
|
71
|
+
register_rsplot_colormaps()
|
|
67
72
|
|
|
68
73
|
# --- Figure setup ---
|
|
69
74
|
frame = create_map_frame(
|
|
@@ -89,6 +94,7 @@ def plot_overlay(
|
|
|
89
94
|
alpha=raster_alpha,
|
|
90
95
|
zorder=1,
|
|
91
96
|
)
|
|
97
|
+
clip_raster_to_region(im, ax, region, coverage_geometry=coverage_geometry)
|
|
92
98
|
|
|
93
99
|
# --- Basemap tiles ---
|
|
94
100
|
add_basemap_tiles(frame, region)
|
|
@@ -104,37 +110,21 @@ def plot_overlay(
|
|
|
104
110
|
if len(vals) > 0:
|
|
105
111
|
marker_size = station_marker_size(region.level)
|
|
106
112
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
else:
|
|
123
|
-
p5 = float(np.nanpercentile(vals, 5))
|
|
124
|
-
p95 = float(np.nanpercentile(vals, 95))
|
|
125
|
-
sc = ax.scatter(
|
|
126
|
-
lon,
|
|
127
|
-
lat,
|
|
128
|
-
c=vals,
|
|
129
|
-
cmap=STATION_CONCENTRATION_CMAP,
|
|
130
|
-
vmin=p5,
|
|
131
|
-
vmax=p95,
|
|
132
|
-
s=marker_size,
|
|
133
|
-
edgecolors="#111",
|
|
134
|
-
linewidths=0.4,
|
|
135
|
-
transform=ccrs.PlateCarree(),
|
|
136
|
-
zorder=5,
|
|
137
|
-
)
|
|
113
|
+
cmap, norm = build_station_colormap(
|
|
114
|
+
data, expand_constant=station_focus is not None
|
|
115
|
+
)
|
|
116
|
+
sc = ax.scatter(
|
|
117
|
+
lon,
|
|
118
|
+
lat,
|
|
119
|
+
c=vals,
|
|
120
|
+
cmap=cmap,
|
|
121
|
+
norm=norm,
|
|
122
|
+
s=marker_size,
|
|
123
|
+
edgecolors="#111",
|
|
124
|
+
linewidths=0.4,
|
|
125
|
+
transform=ccrs.PlateCarree(),
|
|
126
|
+
zorder=5,
|
|
127
|
+
)
|
|
138
128
|
|
|
139
129
|
# Red circle for exceedance
|
|
140
130
|
if station_threshold is not None:
|
|
@@ -151,6 +141,10 @@ def plot_overlay(
|
|
|
151
141
|
zorder=6,
|
|
152
142
|
)
|
|
153
143
|
|
|
144
|
+
if station_focus is not None:
|
|
145
|
+
from rsplot.plotting.station_focus import add_station_focus
|
|
146
|
+
|
|
147
|
+
add_station_focus(ax, region, station_focus)
|
|
154
148
|
configure_map_axes(ax, region, use_basemap=frame.use_basemap)
|
|
155
149
|
|
|
156
150
|
# --- Twin colorbars, hugging the right edge of the map ---
|
|
@@ -166,7 +160,9 @@ def plot_overlay(
|
|
|
166
160
|
|
|
167
161
|
if sc is not None:
|
|
168
162
|
cax_station = ax.inset_axes([1.03, 0.04, 0.025, 0.42])
|
|
169
|
-
cb_station =
|
|
163
|
+
cb_station = add_station_colorbar(
|
|
164
|
+
fig, sc, data.var_name, cax=cax_station
|
|
165
|
+
)
|
|
170
166
|
if station_label:
|
|
171
167
|
cb_station.set_label(station_label, fontsize=9)
|
|
172
168
|
cb_station.ax.tick_params(labelsize=8)
|
|
@@ -174,7 +170,7 @@ def plot_overlay(
|
|
|
174
170
|
# Title
|
|
175
171
|
if title is None:
|
|
176
172
|
title = f"{region.name} · raster + stations"
|
|
177
|
-
ax
|
|
173
|
+
set_map_title(ax, title, subtitle)
|
|
178
174
|
|
|
179
175
|
# Reserve room on the right for the two inset colorbars.
|
|
180
176
|
finalize_map_figure(
|
rsplot/plotting/raster.py
CHANGED
|
@@ -17,14 +17,19 @@ from rsplot.plotting.map_frame import (
|
|
|
17
17
|
add_basemap_tiles,
|
|
18
18
|
add_region_boundaries,
|
|
19
19
|
add_subdivision_labels,
|
|
20
|
+
clip_raster_to_region,
|
|
20
21
|
configure_map_axes,
|
|
21
22
|
create_map_frame,
|
|
22
23
|
figure_size_for_level,
|
|
23
24
|
finalize_map_figure,
|
|
25
|
+
set_map_title,
|
|
24
26
|
)
|
|
25
27
|
|
|
26
28
|
if TYPE_CHECKING:
|
|
29
|
+
from shapely.geometry.base import BaseGeometry
|
|
30
|
+
|
|
27
31
|
from rsplot.geo.boundaries import RegionInfo
|
|
32
|
+
from rsplot.plotting.station_focus import StationFocus
|
|
28
33
|
|
|
29
34
|
|
|
30
35
|
def plot_raster(
|
|
@@ -40,9 +45,11 @@ def plot_raster(
|
|
|
40
45
|
vmax: float = 20.0,
|
|
41
46
|
basemap: bool | None = None,
|
|
42
47
|
tianditu_key: str | None = None,
|
|
43
|
-
hcho_cmap_file: str | None = None,
|
|
44
48
|
title: str | None = None,
|
|
49
|
+
subtitle: str | None = None,
|
|
45
50
|
output: str | None = None,
|
|
51
|
+
coverage_geometry: BaseGeometry | None = None,
|
|
52
|
+
station_focus: StationFocus | None = None,
|
|
46
53
|
colorbar_label: str = "NO2 VCD (x10^15 molec/cm2)",
|
|
47
54
|
) -> None:
|
|
48
55
|
"""Draw a raster map for the given region.
|
|
@@ -54,7 +61,7 @@ def plot_raster(
|
|
|
54
61
|
basemap : override region.params.basemap if not None.
|
|
55
62
|
output : save to file path. If None, plt.show().
|
|
56
63
|
"""
|
|
57
|
-
register_rsplot_colormaps(
|
|
64
|
+
register_rsplot_colormaps()
|
|
58
65
|
|
|
59
66
|
# --- Figure setup ---
|
|
60
67
|
frame = create_map_frame(
|
|
@@ -79,10 +86,15 @@ def plot_raster(
|
|
|
79
86
|
shading="auto",
|
|
80
87
|
alpha=alpha,
|
|
81
88
|
)
|
|
89
|
+
clip_raster_to_region(im, ax, region, coverage_geometry=coverage_geometry)
|
|
82
90
|
|
|
83
91
|
# --- Basemap tiles (beneath boundaries, above raster) ---
|
|
84
92
|
add_basemap_tiles(frame, region)
|
|
85
93
|
add_region_boundaries(ax, region)
|
|
94
|
+
if station_focus is not None:
|
|
95
|
+
from rsplot.plotting.station_focus import add_station_focus
|
|
96
|
+
|
|
97
|
+
add_station_focus(ax, region, station_focus)
|
|
86
98
|
configure_map_axes(ax, region, use_basemap=frame.use_basemap)
|
|
87
99
|
|
|
88
100
|
# Colorbar
|
|
@@ -92,7 +104,7 @@ def plot_raster(
|
|
|
92
104
|
# Title
|
|
93
105
|
if title is None:
|
|
94
106
|
title = f"TROPOMI - {region.name}"
|
|
95
|
-
ax
|
|
107
|
+
set_map_title(ax, title, subtitle)
|
|
96
108
|
|
|
97
109
|
finalize_map_figure(fig, dpi=dpi, output=output)
|
|
98
110
|
|
rsplot/plotting/station.py
CHANGED
|
@@ -6,13 +6,13 @@ with red circles for stations exceeding air quality limits.
|
|
|
6
6
|
|
|
7
7
|
from __future__ import annotations
|
|
8
8
|
|
|
9
|
-
from typing import TYPE_CHECKING
|
|
9
|
+
from typing import TYPE_CHECKING, Any
|
|
10
10
|
|
|
11
11
|
import cartopy.crs as ccrs
|
|
12
12
|
import matplotlib.colors as mcolors
|
|
13
13
|
import numpy as np
|
|
14
14
|
|
|
15
|
-
from rsplot.config import AQI_COLORS
|
|
15
|
+
from rsplot.config import AQI_COLORS, get_station_color_scale
|
|
16
16
|
from rsplot.plotting.colormaps import STATION_CONCENTRATION_CMAP
|
|
17
17
|
from rsplot.plotting.map_frame import (
|
|
18
18
|
add_basemap_tiles,
|
|
@@ -24,7 +24,10 @@ from rsplot.plotting.map_frame import (
|
|
|
24
24
|
|
|
25
25
|
if TYPE_CHECKING:
|
|
26
26
|
from cartopy.mpl.geoaxes import GeoAxes
|
|
27
|
+
from matplotlib.cm import ScalarMappable
|
|
27
28
|
from matplotlib.collections import PathCollection
|
|
29
|
+
from matplotlib.colorbar import Colorbar
|
|
30
|
+
from matplotlib.figure import Figure
|
|
28
31
|
|
|
29
32
|
from rsplot.geo.boundaries import RegionInfo
|
|
30
33
|
from rsplot.readers.guokong import StationData
|
|
@@ -41,6 +44,71 @@ def build_aqi_colormap() -> tuple[
|
|
|
41
44
|
return cmap, norm
|
|
42
45
|
|
|
43
46
|
|
|
47
|
+
def build_station_colormap(
|
|
48
|
+
data: StationData,
|
|
49
|
+
*,
|
|
50
|
+
expand_constant: bool = False,
|
|
51
|
+
) -> tuple[mcolors.Colormap | str, mcolors.Normalize]:
|
|
52
|
+
"""Share AQI bands, fixed O3/NO2 scales and legacy percentile scales."""
|
|
53
|
+
if data.var_name == "AQI":
|
|
54
|
+
return build_aqi_colormap()
|
|
55
|
+
|
|
56
|
+
scale = get_station_color_scale(data.var_name)
|
|
57
|
+
if scale is not None:
|
|
58
|
+
ramp = mcolors.LinearSegmentedColormap.from_list(
|
|
59
|
+
scale.cmap,
|
|
60
|
+
["#1a9850", "#a6d96a", "#fee08b", "#fdae61"],
|
|
61
|
+
)
|
|
62
|
+
colors = ramp(np.linspace(0, 1, 255))
|
|
63
|
+
colors = np.vstack([colors, mcolors.to_rgba(scale.exceedance_color)])
|
|
64
|
+
cmap = mcolors.ListedColormap(colors, name=scale.cmap)
|
|
65
|
+
cmap.set_over(scale.exceedance_color)
|
|
66
|
+
# BoundaryNorm includes the lower edge. Shift the red boundary by
|
|
67
|
+
# one float so equality stays orange and only value > limit is red.
|
|
68
|
+
bounds = np.concatenate(
|
|
69
|
+
[
|
|
70
|
+
np.linspace(scale.vmin, scale.threshold, 256)[:-1],
|
|
71
|
+
[np.nextafter(scale.threshold, np.inf), scale.vmax],
|
|
72
|
+
]
|
|
73
|
+
)
|
|
74
|
+
return cmap, mcolors.BoundaryNorm(bounds, cmap.N)
|
|
75
|
+
|
|
76
|
+
values = data.values[np.isfinite(data.values)]
|
|
77
|
+
if not values.size:
|
|
78
|
+
return STATION_CONCENTRATION_CMAP, mcolors.Normalize()
|
|
79
|
+
vmin, vmax = np.percentile(values, [5, 95]).astype(float)
|
|
80
|
+
if expand_constant and vmax <= vmin:
|
|
81
|
+
margin = max(abs(vmin) * 0.05, 0.1)
|
|
82
|
+
vmin, vmax = vmin - margin, vmax + margin
|
|
83
|
+
return STATION_CONCENTRATION_CMAP, mcolors.Normalize(vmin, vmax)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def station_colorbar_options(var_name: str) -> dict[str, Any]:
|
|
87
|
+
"""Keep fixed concentration bars proportional, with a red upper tip."""
|
|
88
|
+
scale = get_station_color_scale(var_name)
|
|
89
|
+
if scale is None:
|
|
90
|
+
return {}
|
|
91
|
+
return {
|
|
92
|
+
"spacing": "proportional",
|
|
93
|
+
"ticks": [*np.linspace(scale.vmin, scale.threshold, 5), scale.vmax],
|
|
94
|
+
"extend": "max",
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def add_station_colorbar(
|
|
99
|
+
fig: Figure,
|
|
100
|
+
mappable: ScalarMappable,
|
|
101
|
+
var_name: str,
|
|
102
|
+
**kwargs: Any,
|
|
103
|
+
) -> Colorbar:
|
|
104
|
+
"""Apply shared station colorbar settings without dense gradient ticks."""
|
|
105
|
+
options = station_colorbar_options(var_name)
|
|
106
|
+
colorbar = fig.colorbar(mappable, **kwargs, **options)
|
|
107
|
+
if options:
|
|
108
|
+
colorbar.minorticks_off()
|
|
109
|
+
return colorbar
|
|
110
|
+
|
|
111
|
+
|
|
44
112
|
def station_marker_size(level: str) -> int:
|
|
45
113
|
"""Marker area by region level, in points squared."""
|
|
46
114
|
return {
|
|
@@ -82,36 +150,19 @@ def draw_station_layer(
|
|
|
82
150
|
return None
|
|
83
151
|
|
|
84
152
|
marker_size = station_marker_size(region.level)
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
)
|
|
99
|
-
else:
|
|
100
|
-
vmin = float(np.nanpercentile(values, 5))
|
|
101
|
-
vmax = float(np.nanpercentile(values, 95))
|
|
102
|
-
scatter = ax.scatter(
|
|
103
|
-
lon,
|
|
104
|
-
lat,
|
|
105
|
-
c=values,
|
|
106
|
-
cmap=STATION_CONCENTRATION_CMAP,
|
|
107
|
-
vmin=vmin,
|
|
108
|
-
vmax=vmax,
|
|
109
|
-
s=marker_size,
|
|
110
|
-
edgecolors="#333",
|
|
111
|
-
linewidths=0.3,
|
|
112
|
-
transform=ccrs.PlateCarree(),
|
|
113
|
-
zorder=5,
|
|
114
|
-
)
|
|
153
|
+
cmap, norm = build_station_colormap(data)
|
|
154
|
+
scatter = ax.scatter(
|
|
155
|
+
lon,
|
|
156
|
+
lat,
|
|
157
|
+
c=values,
|
|
158
|
+
cmap=cmap,
|
|
159
|
+
norm=norm,
|
|
160
|
+
s=marker_size,
|
|
161
|
+
edgecolors="#333",
|
|
162
|
+
linewidths=0.3,
|
|
163
|
+
transform=ccrs.PlateCarree(),
|
|
164
|
+
zorder=5,
|
|
165
|
+
)
|
|
115
166
|
|
|
116
167
|
if threshold is not None:
|
|
117
168
|
exceed_mask = values > threshold
|
|
@@ -161,7 +212,9 @@ def plot_station(
|
|
|
161
212
|
if region.level in ("country", "key_region", "province")
|
|
162
213
|
else 0.7
|
|
163
214
|
)
|
|
164
|
-
colorbar =
|
|
215
|
+
colorbar = add_station_colorbar(
|
|
216
|
+
fig, scatter, data.var_name, ax=ax, shrink=shrink, pad=0.05
|
|
217
|
+
)
|
|
165
218
|
if colorbar_label:
|
|
166
219
|
colorbar.set_label(colorbar_label)
|
|
167
220
|
|