rsplot 0.3.2.dev4__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 +272 -497
- rsplot/commands/common.py +52 -3
- rsplot/commands/fnr.py +389 -104
- rsplot/commands/forecast.py +0 -2
- rsplot/commands/meteo.py +499 -186
- rsplot/commands/overlay.py +284 -326
- rsplot/commands/radius.py +526 -0
- rsplot/commands/raster.py +418 -293
- rsplot/commands/reconstruct.py +0 -1
- rsplot/commands/sidecar.py +106 -0
- rsplot/commands/station.py +154 -42
- rsplot/commands/station_trend.py +259 -174
- rsplot/config.py +43 -16
- rsplot/data_files.py +253 -0
- rsplot/errors.py +43 -0
- rsplot/fnr.py +38 -46
- rsplot/geo/boundaries.py +212 -9
- rsplot/geo/footprints.py +3 -0
- rsplot/geo/processing.py +2 -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 +84 -6
- rsplot/plotting/meteo.py +10 -3
- rsplot/plotting/overlay.py +31 -40
- rsplot/plotting/raster.py +10 -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 +52 -4
- rsplot/readers/_tropomi.py +121 -3
- rsplot/readers/era5.py +110 -10
- rsplot/readers/era5_wind.py +3 -2
- rsplot/readers/gems.py +11 -8
- 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 +4 -1
- rsplot/result_builders/envelope.py +236 -0
- rsplot/result_builders/fnr.py +47 -131
- rsplot/result_builders/provenance.py +59 -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 +7 -0
- {rsplot-0.3.2.dev4.dist-info → rsplot-0.3.2.dev37.dist-info}/METADATA +426 -121
- rsplot-0.3.2.dev37.dist-info/RECORD +76 -0
- {rsplot-0.3.2.dev4.dist-info → rsplot-0.3.2.dev37.dist-info}/WHEEL +1 -1
- rsplot/commands/recent.py +0 -186
- rsplot/recent.py +0 -447
- rsplot/result_builders/meteo.py +0 -171
- rsplot/result_builders/overlay.py +0 -105
- rsplot/result_builders/raster.py +0 -104
- rsplot/result_builders/station_trend.py +0 -229
- rsplot-0.3.2.dev4.dist-info/RECORD +0 -66
- {rsplot-0.3.2.dev4.dist-info → rsplot-0.3.2.dev37.dist-info}/entry_points.txt +0 -0
- {rsplot-0.3.2.dev4.dist-info → rsplot-0.3.2.dev37.dist-info}/licenses/LICENSE +0 -0
rsplot/plotting/raster.py
CHANGED
|
@@ -22,12 +22,14 @@ from rsplot.plotting.map_frame import (
|
|
|
22
22
|
create_map_frame,
|
|
23
23
|
figure_size_for_level,
|
|
24
24
|
finalize_map_figure,
|
|
25
|
+
set_map_title,
|
|
25
26
|
)
|
|
26
27
|
|
|
27
28
|
if TYPE_CHECKING:
|
|
28
29
|
from shapely.geometry.base import BaseGeometry
|
|
29
30
|
|
|
30
31
|
from rsplot.geo.boundaries import RegionInfo
|
|
32
|
+
from rsplot.plotting.station_focus import StationFocus
|
|
31
33
|
|
|
32
34
|
|
|
33
35
|
def plot_raster(
|
|
@@ -43,10 +45,11 @@ def plot_raster(
|
|
|
43
45
|
vmax: float = 20.0,
|
|
44
46
|
basemap: bool | None = None,
|
|
45
47
|
tianditu_key: str | None = None,
|
|
46
|
-
hcho_cmap_file: str | None = None,
|
|
47
48
|
title: str | None = None,
|
|
49
|
+
subtitle: str | None = None,
|
|
48
50
|
output: str | None = None,
|
|
49
51
|
coverage_geometry: BaseGeometry | None = None,
|
|
52
|
+
station_focus: StationFocus | None = None,
|
|
50
53
|
colorbar_label: str = "NO2 VCD (x10^15 molec/cm2)",
|
|
51
54
|
) -> None:
|
|
52
55
|
"""Draw a raster map for the given region.
|
|
@@ -58,7 +61,7 @@ def plot_raster(
|
|
|
58
61
|
basemap : override region.params.basemap if not None.
|
|
59
62
|
output : save to file path. If None, plt.show().
|
|
60
63
|
"""
|
|
61
|
-
register_rsplot_colormaps(
|
|
64
|
+
register_rsplot_colormaps()
|
|
62
65
|
|
|
63
66
|
# --- Figure setup ---
|
|
64
67
|
frame = create_map_frame(
|
|
@@ -88,6 +91,10 @@ def plot_raster(
|
|
|
88
91
|
# --- Basemap tiles (beneath boundaries, above raster) ---
|
|
89
92
|
add_basemap_tiles(frame, region)
|
|
90
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)
|
|
91
98
|
configure_map_axes(ax, region, use_basemap=frame.use_basemap)
|
|
92
99
|
|
|
93
100
|
# Colorbar
|
|
@@ -97,7 +104,7 @@ def plot_raster(
|
|
|
97
104
|
# Title
|
|
98
105
|
if title is None:
|
|
99
106
|
title = f"TROPOMI - {region.name}"
|
|
100
|
-
ax
|
|
107
|
+
set_map_title(ax, title, subtitle)
|
|
101
108
|
|
|
102
109
|
finalize_map_figure(fig, dpi=dpi, output=output)
|
|
103
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
|
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Station labels and radius decorations shared by raster and overlay maps."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
import cartopy.crs as ccrs
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
from rsplot.plotting.station import station_marker_size
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from cartopy.mpl.geoaxes import GeoAxes
|
|
15
|
+
|
|
16
|
+
from rsplot.geo.boundaries import RegionInfo
|
|
17
|
+
from rsplot.readers.guokong import StationData
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class StationFocus:
|
|
22
|
+
data: StationData
|
|
23
|
+
center_id: str
|
|
24
|
+
radius_km: float
|
|
25
|
+
locations_only: bool = False
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def add_station_focus(
|
|
29
|
+
ax: GeoAxes,
|
|
30
|
+
region: RegionInfo,
|
|
31
|
+
focus: StationFocus,
|
|
32
|
+
) -> None:
|
|
33
|
+
"""Add station IDs and missing sites inside the map frame."""
|
|
34
|
+
data = focus.data
|
|
35
|
+
marker_size = station_marker_size(region.level)
|
|
36
|
+
missing = ~np.isfinite(data.values)
|
|
37
|
+
visible = (
|
|
38
|
+
np.ones(data.n_stations, dtype=bool)
|
|
39
|
+
if focus.locations_only
|
|
40
|
+
else missing
|
|
41
|
+
)
|
|
42
|
+
if visible.any():
|
|
43
|
+
ax.scatter(
|
|
44
|
+
data.lon[visible],
|
|
45
|
+
data.lat[visible],
|
|
46
|
+
s=marker_size,
|
|
47
|
+
facecolors="#2374ab" if focus.locations_only else "none",
|
|
48
|
+
edgecolors="white" if focus.locations_only else "#666",
|
|
49
|
+
linewidths=1.2,
|
|
50
|
+
transform=ccrs.PlateCarree(),
|
|
51
|
+
zorder=7,
|
|
52
|
+
label="国控站点" if focus.locations_only else "站点缺测",
|
|
53
|
+
)
|
|
54
|
+
west, east, south, north = region.extent
|
|
55
|
+
for index, station_id in enumerate(data.id):
|
|
56
|
+
right_edge = data.lon[index] > west + (east - west) * 0.8
|
|
57
|
+
y_offset = 10 if index % 2 == 0 else -15
|
|
58
|
+
if data.lat[index] < south + (north - south) * 0.15:
|
|
59
|
+
y_offset = 10
|
|
60
|
+
elif data.lat[index] > south + (north - south) * 0.85:
|
|
61
|
+
y_offset = -15
|
|
62
|
+
ax.annotate(
|
|
63
|
+
str(station_id),
|
|
64
|
+
(data.lon[index], data.lat[index]),
|
|
65
|
+
xycoords=ccrs.PlateCarree()._as_mpl_transform(ax),
|
|
66
|
+
xytext=(-8 if right_edge else 8, y_offset),
|
|
67
|
+
ha="right" if right_edge else "left",
|
|
68
|
+
textcoords="offset points",
|
|
69
|
+
fontsize=8,
|
|
70
|
+
bbox={"facecolor": "white", "alpha": 0.8, "edgecolor": "none"},
|
|
71
|
+
arrowprops={"arrowstyle": "-", "color": "#555", "lw": 0.5},
|
|
72
|
+
zorder=9,
|
|
73
|
+
)
|
|
74
|
+
if not focus.locations_only and not np.isfinite(data.values).any():
|
|
75
|
+
ax.text(
|
|
76
|
+
0.02,
|
|
77
|
+
0.98,
|
|
78
|
+
"该小时站点观测全部缺测",
|
|
79
|
+
transform=ax.transAxes,
|
|
80
|
+
va="top",
|
|
81
|
+
fontsize=9,
|
|
82
|
+
zorder=10,
|
|
83
|
+
bbox={"facecolor": "white", "alpha": 0.8, "edgecolor": "none"},
|
|
84
|
+
)
|
rsplot/plotting/station_trend.py
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from datetime import datetime, timedelta
|
|
6
|
-
from pathlib import Path
|
|
7
6
|
from typing import TYPE_CHECKING
|
|
7
|
+
from zoneinfo import ZoneInfo
|
|
8
8
|
|
|
9
9
|
import matplotlib.dates as mdates
|
|
10
10
|
import matplotlib.pyplot as plt
|
|
@@ -12,10 +12,12 @@ import numpy as np
|
|
|
12
12
|
from matplotlib.collections import LineCollection
|
|
13
13
|
from matplotlib.ticker import FuncFormatter
|
|
14
14
|
|
|
15
|
+
from rsplot.atomic import atomic_write
|
|
15
16
|
from rsplot.readers.guokong import RegionStationSeries, StationSeries
|
|
16
17
|
|
|
17
18
|
if TYPE_CHECKING:
|
|
18
19
|
from matplotlib.axes import Axes
|
|
20
|
+
from matplotlib.figure import Figure
|
|
19
21
|
|
|
20
22
|
|
|
21
23
|
_DISPLAY_UNITS = {
|
|
@@ -23,6 +25,10 @@ _DISPLAY_UNITS = {
|
|
|
23
25
|
"mg/m3": "mg/m³",
|
|
24
26
|
}
|
|
25
27
|
|
|
28
|
+
# Station hours are China time. Every date locator and formatter gets this
|
|
29
|
+
# zone explicitly so a matplotlibrc ``timezone`` cannot shift the axis.
|
|
30
|
+
CHINA_TIME = ZoneInfo("Asia/Shanghai")
|
|
31
|
+
|
|
26
32
|
_FOCUS_COLORS = (
|
|
27
33
|
"#e6550d",
|
|
28
34
|
"#31a354",
|
|
@@ -43,12 +49,9 @@ def plot_station_trend(
|
|
|
43
49
|
title: str | None = None,
|
|
44
50
|
) -> None:
|
|
45
51
|
"""Plot an hourly station series, preserving NaN gaps."""
|
|
46
|
-
times =
|
|
47
|
-
datetime.strptime(timestamp, "%Y%m%d%H")
|
|
48
|
-
for timestamp in series.timestamps
|
|
49
|
-
]
|
|
52
|
+
times = _china_hours(series.timestamps)
|
|
50
53
|
width = min(16.0, max(10.0, 8.0 + series.n_hours / 96))
|
|
51
|
-
fig, ax = plt.subplots(figsize=(width, 5.
|
|
54
|
+
fig, ax = plt.subplots(figsize=(width, 5.8), dpi=dpi)
|
|
52
55
|
try:
|
|
53
56
|
show_markers = series.n_hours <= 72
|
|
54
57
|
ax.plot(
|
|
@@ -102,17 +105,10 @@ def plot_station_trend(
|
|
|
102
105
|
fontsize=9,
|
|
103
106
|
color="#555555",
|
|
104
107
|
)
|
|
105
|
-
|
|
106
|
-
fig.subplots_adjust(
|
|
107
|
-
left=0.09,
|
|
108
|
-
right=0.98,
|
|
109
|
-
bottom=0.17,
|
|
110
|
-
top=0.84,
|
|
111
|
-
)
|
|
108
|
+
_place_trend_legend(fig, ax)
|
|
112
109
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
fig.savefig(path, dpi=dpi, bbox_inches="tight")
|
|
110
|
+
with atomic_write(output) as temp:
|
|
111
|
+
fig.savefig(temp, dpi=dpi, bbox_inches="tight")
|
|
116
112
|
finally:
|
|
117
113
|
plt.close(fig)
|
|
118
114
|
|
|
@@ -130,10 +126,7 @@ def plot_region_station_trend(
|
|
|
130
126
|
title: str | None = None,
|
|
131
127
|
) -> None:
|
|
132
128
|
"""Plot regional station context, mean, and explicit focus stations."""
|
|
133
|
-
times =
|
|
134
|
-
datetime.strptime(timestamp, "%Y%m%d%H")
|
|
135
|
-
for timestamp in series.timestamps
|
|
136
|
-
]
|
|
129
|
+
times = _china_hours(series.timestamps)
|
|
137
130
|
time_values = mdates.date2num(times)
|
|
138
131
|
width = min(16.0, max(10.0, 8.0 + series.n_hours / 96))
|
|
139
132
|
fig, ax = plt.subplots(figsize=(width, 5.8), dpi=dpi)
|
|
@@ -243,32 +236,36 @@ def plot_region_station_trend(
|
|
|
243
236
|
fontsize=9,
|
|
244
237
|
color="#555555",
|
|
245
238
|
)
|
|
246
|
-
|
|
247
|
-
fig.legend(
|
|
248
|
-
handles,
|
|
249
|
-
labels,
|
|
250
|
-
loc="upper center",
|
|
251
|
-
bbox_to_anchor=(0.5, 0.855),
|
|
252
|
-
ncol=min(6, len(labels)),
|
|
253
|
-
frameon=False,
|
|
254
|
-
fontsize=9,
|
|
255
|
-
columnspacing=1.4,
|
|
256
|
-
handlelength=2.4,
|
|
257
|
-
)
|
|
258
|
-
fig.subplots_adjust(
|
|
259
|
-
left=0.09,
|
|
260
|
-
right=0.98,
|
|
261
|
-
bottom=0.17,
|
|
262
|
-
top=0.78,
|
|
263
|
-
)
|
|
239
|
+
_place_trend_legend(fig, ax)
|
|
264
240
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
fig.savefig(path, dpi=dpi, bbox_inches="tight")
|
|
241
|
+
with atomic_write(output) as temp:
|
|
242
|
+
fig.savefig(temp, dpi=dpi, bbox_inches="tight")
|
|
268
243
|
finally:
|
|
269
244
|
plt.close(fig)
|
|
270
245
|
|
|
271
246
|
|
|
247
|
+
def _place_trend_legend(fig: Figure, ax: Axes) -> None:
|
|
248
|
+
"""Keep station and regional legends between the subtitle and axes."""
|
|
249
|
+
handles, labels = ax.get_legend_handles_labels()
|
|
250
|
+
fig.legend(
|
|
251
|
+
handles,
|
|
252
|
+
labels,
|
|
253
|
+
loc="upper center",
|
|
254
|
+
bbox_to_anchor=(0.5, 0.855),
|
|
255
|
+
ncol=min(6, len(labels)),
|
|
256
|
+
frameon=False,
|
|
257
|
+
fontsize=9,
|
|
258
|
+
columnspacing=1.4,
|
|
259
|
+
handlelength=2.4,
|
|
260
|
+
)
|
|
261
|
+
fig.subplots_adjust(
|
|
262
|
+
left=0.09,
|
|
263
|
+
right=0.98,
|
|
264
|
+
bottom=0.17,
|
|
265
|
+
top=0.78,
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
|
|
272
269
|
def _background_segments(
|
|
273
270
|
times: np.ndarray,
|
|
274
271
|
values: np.ndarray,
|
|
@@ -293,28 +290,44 @@ def _background_segments(
|
|
|
293
290
|
return segments
|
|
294
291
|
|
|
295
292
|
|
|
293
|
+
def _china_hours(timestamps: list[str]) -> list[datetime]:
|
|
294
|
+
"""Parse ``YYYYMMDDHH`` station labels as China-time instants."""
|
|
295
|
+
return [
|
|
296
|
+
datetime.strptime(timestamp, "%Y%m%d%H").replace(tzinfo=CHINA_TIME)
|
|
297
|
+
for timestamp in timestamps
|
|
298
|
+
]
|
|
299
|
+
|
|
300
|
+
|
|
296
301
|
def _configure_time_axis(ax: Axes, times: list[datetime]) -> None:
|
|
297
302
|
"""Choose readable ticks without Matplotlib's overlapping offset label."""
|
|
298
303
|
span_hours = (times[-1] - times[0]).total_seconds() / 3600
|
|
299
304
|
if span_hours <= 72:
|
|
300
|
-
ax.xaxis.set_major_locator(
|
|
305
|
+
ax.xaxis.set_major_locator(
|
|
306
|
+
mdates.HourLocator(byhour=(0, 6, 12, 18), tz=CHINA_TIME)
|
|
307
|
+
)
|
|
301
308
|
ax.xaxis.set_major_formatter(FuncFormatter(_format_hour_tick))
|
|
302
309
|
return
|
|
303
310
|
|
|
304
311
|
if span_hours <= 24 * 14:
|
|
305
312
|
days = max(1, round(span_hours / 24))
|
|
306
313
|
interval = max(1, round(days / 7))
|
|
307
|
-
ax.xaxis.set_major_locator(
|
|
308
|
-
|
|
314
|
+
ax.xaxis.set_major_locator(
|
|
315
|
+
mdates.DayLocator(interval=interval, tz=CHINA_TIME)
|
|
316
|
+
)
|
|
317
|
+
ax.xaxis.set_major_formatter(
|
|
318
|
+
mdates.DateFormatter("%m-%d", tz=CHINA_TIME)
|
|
319
|
+
)
|
|
309
320
|
return
|
|
310
321
|
|
|
311
|
-
locator = mdates.AutoDateLocator(minticks=5, maxticks=10)
|
|
322
|
+
locator = mdates.AutoDateLocator(minticks=5, maxticks=10, tz=CHINA_TIME)
|
|
312
323
|
ax.xaxis.set_major_locator(locator)
|
|
313
|
-
ax.xaxis.set_major_formatter(
|
|
324
|
+
ax.xaxis.set_major_formatter(
|
|
325
|
+
mdates.DateFormatter("%Y-%m-%d", tz=CHINA_TIME)
|
|
326
|
+
)
|
|
314
327
|
|
|
315
328
|
|
|
316
329
|
def _format_hour_tick(value: float, _position: int | None = None) -> str:
|
|
317
|
-
timestamp = mdates.num2date(value)
|
|
330
|
+
timestamp = mdates.num2date(value, tz=CHINA_TIME)
|
|
318
331
|
if timestamp.hour == 0:
|
|
319
332
|
return timestamp.strftime("%m-%d\n%H:%M")
|
|
320
333
|
return timestamp.strftime("%H:%M")
|
rsplot/plotting/styles.py
CHANGED
|
@@ -10,9 +10,7 @@ from matplotlib import font_manager
|
|
|
10
10
|
from rsplot.plotting.colormaps import register_rsplot_colormaps
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
def setup_fonts(
|
|
14
|
-
font_dir: str | None, hcho_cmap_file: str | None = None
|
|
15
|
-
) -> None:
|
|
13
|
+
def setup_fonts(font_dir: str | None) -> None:
|
|
16
14
|
"""Register Chinese fonts and set matplotlib defaults.
|
|
17
15
|
|
|
18
16
|
Looks for Microsoft YaHei in font_dir. Falls back gracefully
|
|
@@ -20,7 +18,7 @@ def setup_fonts(
|
|
|
20
18
|
"""
|
|
21
19
|
if not font_dir:
|
|
22
20
|
mpl.rcParams["axes.unicode_minus"] = False
|
|
23
|
-
register_rsplot_colormaps(
|
|
21
|
+
register_rsplot_colormaps()
|
|
24
22
|
return
|
|
25
23
|
|
|
26
24
|
font_root = Path(font_dir)
|
|
@@ -40,4 +38,4 @@ def setup_fonts(
|
|
|
40
38
|
mpl.rcParams["font.sans-serif"] = ["Microsoft YaHei"]
|
|
41
39
|
|
|
42
40
|
mpl.rcParams["axes.unicode_minus"] = False
|
|
43
|
-
register_rsplot_colormaps(
|
|
41
|
+
register_rsplot_colormaps()
|
rsplot/readers/__init__.py
CHANGED
|
@@ -14,6 +14,8 @@ from rsplot.readers.tropomi_o3 import TropomiO3Reader
|
|
|
14
14
|
from rsplot.readers.tropomi_o3pr import TropomiO3PRReader
|
|
15
15
|
|
|
16
16
|
HCHO_CMAP = "rsplot_hcho"
|
|
17
|
+
NO2_CMAP = "rsplot_no2"
|
|
18
|
+
COLORMAP_22 = "rsplot_colormap_22"
|
|
17
19
|
|
|
18
20
|
|
|
19
21
|
@dataclass
|
|
@@ -43,6 +45,16 @@ class ProductInfo:
|
|
|
43
45
|
sensor: str = "tropomi"
|
|
44
46
|
data_key: str | None = None
|
|
45
47
|
quantity: str | None = None
|
|
48
|
+
processing: str | None = None
|
|
49
|
+
# Machine-readable unit: stored value * unit_scale = value in unit.
|
|
50
|
+
unit: str = ""
|
|
51
|
+
unit_scale: float = 1.0
|
|
52
|
+
|
|
53
|
+
def create_reader(self) -> BaseReader:
|
|
54
|
+
"""Construct the configured reader without changing legacy callers."""
|
|
55
|
+
if self.processing is not None:
|
|
56
|
+
return self.reader_cls(processing=self.processing)
|
|
57
|
+
return self.reader_cls()
|
|
46
58
|
|
|
47
59
|
def defaults_for_level(self, level: str) -> ProductPlotDefaults:
|
|
48
60
|
"""Return product-specific defaults for an administrative level."""
|
|
@@ -53,26 +65,33 @@ PRODUCT_REGISTRY: dict[str, ProductInfo] = {
|
|
|
53
65
|
"no2": ProductInfo(
|
|
54
66
|
reader_cls=TropomiNO2Reader,
|
|
55
67
|
colorbar_label="NO2 VCD (x10^15 molec/cm2)",
|
|
56
|
-
cmap=
|
|
68
|
+
cmap=NO2_CMAP,
|
|
57
69
|
vmin=0.0,
|
|
58
70
|
vmax=20.0,
|
|
59
71
|
min_range=5.0,
|
|
72
|
+
quantity="tropospheric_no2_column",
|
|
73
|
+
unit="molec/cm2",
|
|
74
|
+
unit_scale=1e15,
|
|
60
75
|
),
|
|
61
76
|
"o3": ProductInfo(
|
|
62
77
|
reader_cls=TropomiO3Reader,
|
|
63
78
|
colorbar_label="O3 Total Column (DU)",
|
|
64
|
-
cmap=
|
|
79
|
+
cmap=COLORMAP_22,
|
|
65
80
|
vmin=None,
|
|
66
81
|
vmax=None,
|
|
67
82
|
min_range=30.0,
|
|
83
|
+
quantity="total_ozone_column",
|
|
84
|
+
unit="DU",
|
|
68
85
|
),
|
|
69
86
|
"o3pr": ProductInfo(
|
|
70
87
|
reader_cls=TropomiO3PRReader,
|
|
71
88
|
colorbar_label="O3 Tropospheric Column from Profile (DU)",
|
|
72
|
-
cmap=
|
|
89
|
+
cmap=COLORMAP_22,
|
|
73
90
|
vmin=None,
|
|
74
91
|
vmax=None,
|
|
75
92
|
min_range=5.0,
|
|
93
|
+
quantity="tropospheric_ozone_column",
|
|
94
|
+
unit="DU",
|
|
76
95
|
level_defaults={
|
|
77
96
|
# O3__PR is a coarse 30x30 km profile product. These defaults
|
|
78
97
|
# keep map pixels close to the native sampling while still using
|
|
@@ -91,12 +110,26 @@ PRODUCT_REGISTRY: dict[str, ProductInfo] = {
|
|
|
91
110
|
vmin=None, # unit not yet confirmed — let auto-range pick
|
|
92
111
|
vmax=None,
|
|
93
112
|
min_range=5.0,
|
|
113
|
+
quantity="tropospheric_hcho_column",
|
|
114
|
+
unit="molec/cm2",
|
|
115
|
+
unit_scale=1e15,
|
|
94
116
|
),
|
|
95
117
|
}
|
|
96
118
|
|
|
97
119
|
|
|
98
|
-
def get_product_info(
|
|
120
|
+
def get_product_info(
|
|
121
|
+
product: str,
|
|
122
|
+
*,
|
|
123
|
+
sensor: str = "tropomi",
|
|
124
|
+
processing: str | None = None,
|
|
125
|
+
) -> ProductInfo:
|
|
99
126
|
"""Resolve a product within a sensor, preserving legacy registry keys."""
|
|
127
|
+
if processing is not None:
|
|
128
|
+
processing = processing.lower()
|
|
129
|
+
if processing != "nrti":
|
|
130
|
+
raise ValueError("--processing 仅支持 nrti;省略时保持原有行为。")
|
|
131
|
+
if sensor.lower() != "tropomi":
|
|
132
|
+
raise ValueError("--processing nrti 仅用于 --sensor tropomi。")
|
|
100
133
|
key = product.lower()
|
|
101
134
|
sensor = sensor.lower()
|
|
102
135
|
if sensor not in ("tropomi", "gems"):
|
|
@@ -107,8 +140,16 @@ def get_product_info(product: str, *, sensor: str = "tropomi") -> ProductInfo:
|
|
|
107
140
|
f"Unsupported product '{product}'. Available: {available}"
|
|
108
141
|
)
|
|
109
142
|
if sensor == "tropomi":
|
|
143
|
+
if processing == "nrti":
|
|
144
|
+
return replace(
|
|
145
|
+
PRODUCT_REGISTRY[key],
|
|
146
|
+
processing=processing,
|
|
147
|
+
data_key=f"tropomi_nrti_{key}",
|
|
148
|
+
)
|
|
110
149
|
return PRODUCT_REGISTRY[key]
|
|
150
|
+
from rsplot.config import LEVEL_DEFAULTS
|
|
111
151
|
from rsplot.readers.gems import (
|
|
152
|
+
GEMS_DEFAULT_RESOLUTION,
|
|
112
153
|
GemsHCHOReader,
|
|
113
154
|
GemsNO2Reader,
|
|
114
155
|
GemsO3PRReader,
|
|
@@ -131,6 +172,13 @@ def get_product_info(product: str, *, sensor: str = "tropomi") -> ProductInfo:
|
|
|
131
172
|
PRODUCT_REGISTRY[key],
|
|
132
173
|
reader_cls=readers[key],
|
|
133
174
|
sensor="gems",
|
|
175
|
+
level_defaults={
|
|
176
|
+
level: replace(
|
|
177
|
+
PRODUCT_REGISTRY[key].defaults_for_level(level),
|
|
178
|
+
res=GEMS_DEFAULT_RESOLUTION,
|
|
179
|
+
)
|
|
180
|
+
for level in LEVEL_DEFAULTS
|
|
181
|
+
},
|
|
134
182
|
data_key="gems_o3" if key == "o3pr" else f"gems_{key}",
|
|
135
183
|
quantity=quantities[key],
|
|
136
184
|
)
|