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/commands/raster.py
CHANGED
|
@@ -2,14 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from pathlib import Path
|
|
6
7
|
from typing import TYPE_CHECKING, Any
|
|
7
8
|
|
|
8
9
|
import numpy as np
|
|
9
|
-
import typer
|
|
10
10
|
|
|
11
11
|
from rsplot.commands.common import (
|
|
12
12
|
parse_basemap,
|
|
13
|
+
resolve_satellite,
|
|
14
|
+
satellite_filter_label,
|
|
13
15
|
validate_color_range,
|
|
14
16
|
validate_non_negative,
|
|
15
17
|
validate_positive,
|
|
@@ -17,20 +19,105 @@ from rsplot.commands.common import (
|
|
|
17
19
|
warn_missing_tianditu_key,
|
|
18
20
|
)
|
|
19
21
|
from rsplot.config import AppConfig
|
|
22
|
+
from rsplot.errors import RsplotError
|
|
20
23
|
|
|
21
24
|
if TYPE_CHECKING:
|
|
22
25
|
from rich.console import Console
|
|
23
26
|
|
|
27
|
+
from rsplot.geo.boundaries import RegionInfo
|
|
28
|
+
from rsplot.result_builders.satellite import SatelliteInputs
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
# How the colour range was chosen, recorded with the display settings.
|
|
31
|
+
RANGE_METHOD = {
|
|
32
|
+
"cli": "manual",
|
|
33
|
+
"product_default": "product_default",
|
|
34
|
+
"built_in": "region_p02_p98",
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass
|
|
39
|
+
class RasterRun:
|
|
40
|
+
"""Prepared satellite layer and sidecar, independent of rendering."""
|
|
41
|
+
|
|
42
|
+
lon: np.ndarray
|
|
43
|
+
lat: np.ndarray
|
|
44
|
+
grid: np.ndarray
|
|
45
|
+
region: RegionInfo
|
|
46
|
+
plot_options: dict[str, Any]
|
|
47
|
+
# Inputs for the sidecar satellite layer.
|
|
48
|
+
satellite: SatelliteInputs | None = None
|
|
49
|
+
render: dict[str, Any] = field(default_factory=dict)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def run_raster(**kwargs: Any) -> None:
|
|
53
|
+
"""Prepare, render, and emit the administrative raster command."""
|
|
54
|
+
from rsplot.commands.sidecar import run_with_sidecar
|
|
55
|
+
|
|
56
|
+
run_with_sidecar(
|
|
57
|
+
kwargs=kwargs,
|
|
58
|
+
emit=lambda created: _emit_raster(kwargs, created),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _emit_raster(kwargs: dict[str, Any], created: list[Path]) -> None:
|
|
63
|
+
from rsplot.data_files import (
|
|
64
|
+
data_path,
|
|
65
|
+
write_grid_netcdf,
|
|
66
|
+
)
|
|
67
|
+
from rsplot.plotting.raster import plot_raster
|
|
68
|
+
from rsplot.result_builders.envelope import (
|
|
69
|
+
command_line,
|
|
70
|
+
sidecar_path,
|
|
71
|
+
success_envelope,
|
|
72
|
+
write_sidecar,
|
|
73
|
+
)
|
|
74
|
+
from rsplot.result_builders.satellite import build_satellite_layer
|
|
75
|
+
|
|
76
|
+
run = prepare_raster(**kwargs)
|
|
77
|
+
inputs = run.satellite
|
|
78
|
+
assert inputs is not None
|
|
79
|
+
image = str(Path(run.plot_options["output"]).resolve())
|
|
80
|
+
sidecar = str(Path(sidecar_path(image)))
|
|
81
|
+
|
|
82
|
+
grid_path = data_path(image, "satellite", ".nc")
|
|
83
|
+
created.append(grid_path)
|
|
84
|
+
grid_entry = write_grid_netcdf(
|
|
85
|
+
grid_path,
|
|
86
|
+
inputs.lon,
|
|
87
|
+
inputs.lat,
|
|
88
|
+
inputs.grid,
|
|
89
|
+
inputs.counted,
|
|
90
|
+
extent=run.region.extent,
|
|
91
|
+
layer="satellite",
|
|
92
|
+
attrs={
|
|
93
|
+
"quantity": inputs.prod_info.quantity,
|
|
94
|
+
"unit": inputs.prod_info.unit,
|
|
95
|
+
"scale": inputs.prod_info.unit_scale,
|
|
96
|
+
"time_aggregation": inputs.time["aggregation"],
|
|
97
|
+
},
|
|
98
|
+
valid_days=inputs.valid_days,
|
|
99
|
+
)
|
|
100
|
+
data_entries = [grid_entry]
|
|
101
|
+
|
|
102
|
+
created.append(Path(image))
|
|
103
|
+
plot_raster(run.lon, run.lat, run.grid, run.region, **run.plot_options)
|
|
104
|
+
layer, flags = build_satellite_layer(
|
|
105
|
+
inputs,
|
|
106
|
+
run.region,
|
|
107
|
+
data=grid_entry["path"],
|
|
108
|
+
)
|
|
109
|
+
payload = success_envelope(
|
|
110
|
+
command="raster",
|
|
111
|
+
request=command_line(),
|
|
112
|
+
region=run.region,
|
|
113
|
+
layers=[layer],
|
|
114
|
+
flags=flags,
|
|
115
|
+
render={"image": image, **run.render},
|
|
116
|
+
artifacts={"image": image, "sidecar": sidecar, "data": data_entries},
|
|
117
|
+
)
|
|
118
|
+
created.append(Path(sidecar))
|
|
119
|
+
write_sidecar(payload, sidecar)
|
|
120
|
+
kwargs["console"].print(f"[bold green]✓ 已保存:[/bold green] {image}")
|
|
34
121
|
|
|
35
122
|
|
|
36
123
|
def parse_raster_dates(
|
|
@@ -41,35 +128,48 @@ def parse_raster_dates(
|
|
|
41
128
|
from rsplot.temporal import parse_date_window, parse_yyyymmdd
|
|
42
129
|
|
|
43
130
|
if days is not None and days < 1:
|
|
44
|
-
raise
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
131
|
+
raise RsplotError(
|
|
132
|
+
"invalid_argument", "--days 必须 >= 1。", option="--days"
|
|
133
|
+
)
|
|
134
|
+
if days is not None and "-" in date_arg:
|
|
135
|
+
raise RsplotError(
|
|
136
|
+
"invalid_argument",
|
|
137
|
+
"日期区间不能与 --days 同时使用;"
|
|
138
|
+
"请选择 YYYYMMDD-YYYYMMDD 或结束日期 YYYYMMDD 配合 --days N。",
|
|
139
|
+
option="--days",
|
|
140
|
+
)
|
|
141
|
+
try:
|
|
142
|
+
if "-" in date_arg:
|
|
143
|
+
dates = parse_date_window(date_arg, default_days=0)
|
|
144
|
+
window_mode = True
|
|
145
|
+
elif days is not None:
|
|
146
|
+
dates = parse_date_window(date_arg, default_days=days)
|
|
147
|
+
window_mode = True
|
|
148
|
+
else:
|
|
149
|
+
if len(date_arg) != 8 or not date_arg.isdigit():
|
|
150
|
+
raise ValueError(
|
|
151
|
+
f"日期格式错误: '{date_arg}',"
|
|
152
|
+
"应为 YYYYMMDD 或 YYYYMMDD-YYYYMMDD"
|
|
153
|
+
)
|
|
154
|
+
parse_yyyymmdd(date_arg)
|
|
155
|
+
dates = [date_arg]
|
|
156
|
+
window_mode = False
|
|
157
|
+
except RsplotError:
|
|
158
|
+
raise
|
|
159
|
+
except ValueError as error:
|
|
160
|
+
raise RsplotError("invalid_time", str(error)) from error
|
|
60
161
|
return dates, window_mode
|
|
61
162
|
|
|
62
163
|
|
|
63
|
-
def
|
|
164
|
+
def prepare_raster(
|
|
64
165
|
*,
|
|
65
|
-
region: str,
|
|
166
|
+
region: str | RegionInfo,
|
|
66
167
|
date_arg: str,
|
|
67
168
|
product: str,
|
|
169
|
+
sensor: str = "tropomi",
|
|
170
|
+
processing: str | None = None,
|
|
68
171
|
days: int | None,
|
|
69
|
-
|
|
70
|
-
level: str | None,
|
|
71
|
-
data_dir: str | None,
|
|
72
|
-
output: str | None,
|
|
172
|
+
output: str,
|
|
73
173
|
dpi: int | None,
|
|
74
174
|
res: float | None,
|
|
75
175
|
basemap: str | None,
|
|
@@ -81,52 +181,71 @@ def run_raster(
|
|
|
81
181
|
qa: float | None,
|
|
82
182
|
cf: float = 0.5,
|
|
83
183
|
console: Console,
|
|
84
|
-
) ->
|
|
85
|
-
"""
|
|
86
|
-
from rsplot.geo.boundaries import
|
|
184
|
+
) -> RasterRun:
|
|
185
|
+
"""Prepare a satellite layer for an administrative or supplied geometry."""
|
|
186
|
+
from rsplot.geo.boundaries import RegionInfo, resolve_regions
|
|
187
|
+
from rsplot.geo.gridding import fill_nan_gaps, region_grid_mask
|
|
87
188
|
from rsplot.geo.processing import (
|
|
88
189
|
buffered_extent,
|
|
190
|
+
prepare_display_grid,
|
|
89
191
|
prepare_existing_grid,
|
|
90
192
|
prepare_swath_grid,
|
|
91
193
|
)
|
|
92
|
-
from rsplot.plotting.raster import plot_raster
|
|
93
194
|
from rsplot.plotting.styles import setup_fonts
|
|
94
|
-
from rsplot.readers import auto_vrange
|
|
95
|
-
from rsplot.
|
|
96
|
-
from rsplot.
|
|
195
|
+
from rsplot.readers import auto_vrange
|
|
196
|
+
from rsplot.result_builders.common import grid_boundary_policy
|
|
197
|
+
from rsplot.result_builders.envelope import stats_block
|
|
198
|
+
from rsplot.result_builders.satellite import SatelliteInputs
|
|
199
|
+
from rsplot.temporal import compute_window_mean, read_scan_mean
|
|
97
200
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
201
|
+
cfg = AppConfig.load()
|
|
202
|
+
prod_info, use_qa = resolve_satellite(
|
|
203
|
+
product, sensor, qa, cfg, console, processing
|
|
204
|
+
)
|
|
205
|
+
sensor = prod_info.sensor
|
|
206
|
+
processing = prod_info.processing
|
|
207
|
+
scan_mode = sensor == "gems" and len(date_arg) == 12
|
|
208
|
+
if scan_mode:
|
|
209
|
+
from rsplot.readers.gems import parse_gems_time
|
|
103
210
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
use_n_min = (
|
|
114
|
-
n_min
|
|
115
|
-
if n_min is not None
|
|
116
|
-
else default_raster_n_min(product, window_n)
|
|
117
|
-
)
|
|
118
|
-
if use_n_min > window_n:
|
|
119
|
-
console.print(
|
|
120
|
-
f"[red]--n-min ({use_n_min}) 大于窗口天数 ({window_n})[/red]"
|
|
211
|
+
try:
|
|
212
|
+
parse_gems_time(date_arg)
|
|
213
|
+
except ValueError as error:
|
|
214
|
+
raise RsplotError("invalid_time", str(error)) from error
|
|
215
|
+
if days is not None:
|
|
216
|
+
raise RsplotError(
|
|
217
|
+
"invalid_argument",
|
|
218
|
+
"GEMS 单次扫描不能同时使用 --days。",
|
|
219
|
+
option="--days",
|
|
121
220
|
)
|
|
122
|
-
|
|
221
|
+
dates, window_mode = parse_raster_dates(
|
|
222
|
+
date_arg[:8] if scan_mode else date_arg, days
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
window_n = len(dates)
|
|
123
226
|
|
|
124
|
-
cfg = AppConfig.load()
|
|
125
227
|
setup_fonts(cfg.font_dir)
|
|
126
228
|
|
|
127
|
-
|
|
128
|
-
|
|
229
|
+
if isinstance(region, RegionInfo):
|
|
230
|
+
info = region
|
|
231
|
+
else:
|
|
232
|
+
try:
|
|
233
|
+
info = resolve_regions(region, cfg)
|
|
234
|
+
except RsplotError:
|
|
235
|
+
raise
|
|
236
|
+
except ValueError as error:
|
|
237
|
+
raise RsplotError(
|
|
238
|
+
"region_not_found", str(error), details={"region": region}
|
|
239
|
+
) from error
|
|
240
|
+
console.print(f"[bold]分析范围:[/bold] {info.name}")
|
|
129
241
|
console.print(f" → [green]{info.name}[/green] (级别: {info.level})")
|
|
242
|
+
if info.members:
|
|
243
|
+
console.print(
|
|
244
|
+
" → 成员: "
|
|
245
|
+
+ "、".join(
|
|
246
|
+
member.qualified_name or member.name for member in info.members
|
|
247
|
+
)
|
|
248
|
+
)
|
|
130
249
|
console.print(
|
|
131
250
|
f" → 范围: {info.extent[0]:.2f}°~{info.extent[1]:.2f}°E, "
|
|
132
251
|
f"{info.extent[2]:.2f}°~{info.extent[3]:.2f}°N"
|
|
@@ -134,7 +253,6 @@ def run_raster(
|
|
|
134
253
|
|
|
135
254
|
params = info.params
|
|
136
255
|
use_dpi = dpi if dpi is not None else cfg.dpi
|
|
137
|
-
prod_info = get_product_info(product)
|
|
138
256
|
product_defaults = prod_info.defaults_for_level(info.level)
|
|
139
257
|
use_res = (
|
|
140
258
|
res
|
|
@@ -144,7 +262,6 @@ def run_raster(
|
|
|
144
262
|
else params.res
|
|
145
263
|
)
|
|
146
264
|
use_cmap = cmap if cmap is not None else prod_info.cmap
|
|
147
|
-
use_qa = qa if qa is not None else cfg.qa_threshold
|
|
148
265
|
use_smooth = (
|
|
149
266
|
smooth
|
|
150
267
|
if smooth is not None
|
|
@@ -162,8 +279,7 @@ def run_raster(
|
|
|
162
279
|
if use_smooth is not None:
|
|
163
280
|
validate_non_negative(use_smooth, "--smooth")
|
|
164
281
|
except ValueError as e:
|
|
165
|
-
|
|
166
|
-
raise typer.Exit(1) from e
|
|
282
|
+
raise RsplotError("invalid_argument", str(e)) from e
|
|
167
283
|
if use_smooth == 0:
|
|
168
284
|
use_smooth = None
|
|
169
285
|
warn_missing_tianditu_key(
|
|
@@ -174,149 +290,156 @@ def run_raster(
|
|
|
174
290
|
)
|
|
175
291
|
|
|
176
292
|
read_extent = buffered_extent(info.extent, 1.0)
|
|
177
|
-
use_data_dir = (
|
|
178
|
-
|
|
179
|
-
)
|
|
180
|
-
reader = prod_info.reader_cls()
|
|
293
|
+
use_data_dir = cfg.get_data_dir(prod_info.data_key or product)
|
|
294
|
+
reader = prod_info.create_reader()
|
|
181
295
|
reader.cloud_fraction_max = cf
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
296
|
+
if sensor == "gems":
|
|
297
|
+
console.print(f" → GEMS 目标网格 {use_res}°(分析与显示)")
|
|
298
|
+
provenance: list[dict[str, Any]] = []
|
|
299
|
+
pixels_used = 0
|
|
300
|
+
valid_days: np.ndarray | None = None
|
|
185
301
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
console.print(f" 日期: {dates[0]}, QA > {use_qa}, CF <= {cf}")
|
|
191
|
-
swath = reader.read(
|
|
192
|
-
use_data_dir,
|
|
193
|
-
dates[0],
|
|
194
|
-
read_extent,
|
|
195
|
-
qa_threshold=use_qa,
|
|
196
|
-
)
|
|
197
|
-
console.print(
|
|
198
|
-
f" → [green]{swath.n_pixels}[/green] 有效像元 "
|
|
199
|
-
f"({swath.n_files} 文件, {swath.n_broken} 损坏)"
|
|
200
|
-
)
|
|
201
|
-
console.print(f"\n[bold]网格化:[/bold] {use_res}° 分辨率")
|
|
202
|
-
prepared = prepare_swath_grid(
|
|
203
|
-
swath.lon,
|
|
204
|
-
swath.lat,
|
|
205
|
-
swath.values,
|
|
206
|
-
read_extent,
|
|
207
|
-
use_res,
|
|
208
|
-
info.geometry,
|
|
209
|
-
)
|
|
210
|
-
swath_meta = {
|
|
211
|
-
"n_pixels": int(swath.n_pixels),
|
|
212
|
-
"n_files": int(swath.n_files),
|
|
213
|
-
"n_broken": int(swath.n_broken),
|
|
214
|
-
"qa_threshold": use_qa,
|
|
215
|
-
"cloud_fraction_max": cf,
|
|
216
|
-
"resolution_deg": use_res,
|
|
217
|
-
}
|
|
218
|
-
else:
|
|
219
|
-
assert use_n_min is not None
|
|
220
|
-
console.print(
|
|
221
|
-
f"\n[bold]时间窗口:[/bold] {dates[0]} ~ {dates[-1]} "
|
|
222
|
-
f"({window_n} 天,每像元至少 {use_n_min} 天有效)"
|
|
223
|
-
)
|
|
224
|
-
console.print(
|
|
225
|
-
f"\n[bold]读取数据:[/bold] {use_data_dir} (产品: {product})"
|
|
226
|
-
)
|
|
227
|
-
console.print(f" QA > {use_qa}, CF <= {cf}")
|
|
228
|
-
window = compute_window_mean(
|
|
229
|
-
reader=reader,
|
|
230
|
-
data_dir=use_data_dir,
|
|
231
|
-
dates=dates,
|
|
232
|
-
extent=read_extent,
|
|
233
|
-
res=use_res,
|
|
234
|
-
qa_threshold=use_qa,
|
|
235
|
-
label=product.upper(),
|
|
236
|
-
console=console,
|
|
237
|
-
)
|
|
238
|
-
console.print(
|
|
239
|
-
f" → 有效天数 [green]{len(window.dates_found)}[/green] / "
|
|
240
|
-
f"{window_n} (失败 {len(window.dates_failed)}),累计 "
|
|
241
|
-
f"[green]{window.n_pixels_total}[/green] 像元 "
|
|
242
|
-
f"({window.n_files_total} 文件, {window.n_broken_total} 损坏)"
|
|
243
|
-
)
|
|
244
|
-
mean_grid = window.grid.copy()
|
|
245
|
-
mean_grid[window.n_valid < use_n_min] = np.nan
|
|
246
|
-
prepared = prepare_existing_grid(
|
|
247
|
-
window.LON,
|
|
248
|
-
window.LAT,
|
|
249
|
-
mean_grid,
|
|
250
|
-
info.geometry,
|
|
251
|
-
fill_gaps=False,
|
|
252
|
-
)
|
|
253
|
-
scientific_valid_mask = np.isfinite(prepared.values)
|
|
254
|
-
|
|
255
|
-
covered = window.n_valid[window.n_valid > 0]
|
|
256
|
-
coverage_days: dict[str, Any] = {
|
|
257
|
-
"min": int(covered.min()) if len(covered) else 0,
|
|
258
|
-
"max": int(covered.max()) if len(covered) else 0,
|
|
259
|
-
"mean": round(float(covered.mean()), 1) if len(covered) else 0.0,
|
|
260
|
-
}
|
|
261
|
-
if len(covered):
|
|
262
|
-
coverage_days.update(
|
|
263
|
-
{
|
|
264
|
-
"p10": int(np.percentile(covered, 10)),
|
|
265
|
-
"p25": int(np.percentile(covered, 25)),
|
|
266
|
-
"median": int(np.percentile(covered, 50)),
|
|
267
|
-
"p75": int(np.percentile(covered, 75)),
|
|
268
|
-
"p90": int(np.percentile(covered, 90)),
|
|
269
|
-
}
|
|
302
|
+
try:
|
|
303
|
+
if not window_mode:
|
|
304
|
+
console.print(
|
|
305
|
+
f"\n[bold]读取数据:[/bold] {use_data_dir} (产品: {product})"
|
|
270
306
|
)
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
"
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
307
|
+
console.print(
|
|
308
|
+
f" 时间: {date_arg}, "
|
|
309
|
+
f"{satellite_filter_label(sensor, use_qa, cf)}"
|
|
310
|
+
)
|
|
311
|
+
if sensor == "gems":
|
|
312
|
+
batch = read_scan_mean(
|
|
313
|
+
reader=reader,
|
|
314
|
+
data_dir=use_data_dir,
|
|
315
|
+
request=date_arg,
|
|
316
|
+
extent=read_extent,
|
|
317
|
+
res=use_res,
|
|
318
|
+
)
|
|
319
|
+
prepared = prepare_existing_grid(
|
|
320
|
+
batch.LON,
|
|
321
|
+
batch.LAT,
|
|
322
|
+
batch.grid,
|
|
323
|
+
info.geometry,
|
|
324
|
+
fill_gaps=False,
|
|
325
|
+
)
|
|
326
|
+
provenance = [batch.metadata]
|
|
327
|
+
pixels_used = int(batch.n_pixels)
|
|
328
|
+
else:
|
|
329
|
+
swath = reader.read(
|
|
330
|
+
use_data_dir,
|
|
331
|
+
dates[0],
|
|
332
|
+
read_extent,
|
|
333
|
+
qa_threshold=use_qa,
|
|
334
|
+
)
|
|
335
|
+
console.print(
|
|
336
|
+
f" → [green]{swath.n_pixels}[/green] 有效像元 "
|
|
337
|
+
f"({swath.n_files} 文件, {swath.n_broken} 损坏)"
|
|
338
|
+
)
|
|
339
|
+
console.print(f"\n[bold]网格化:[/bold] {use_res}° 分辨率")
|
|
340
|
+
prepared = prepare_swath_grid(
|
|
341
|
+
swath.lon,
|
|
342
|
+
swath.lat,
|
|
343
|
+
swath.values,
|
|
344
|
+
read_extent,
|
|
345
|
+
use_res,
|
|
346
|
+
info.geometry,
|
|
347
|
+
)
|
|
348
|
+
provenance = [swath.metadata]
|
|
349
|
+
pixels_used = int(swath.n_pixels)
|
|
350
|
+
else:
|
|
351
|
+
console.print(
|
|
352
|
+
f"\n[bold]时间窗口:[/bold] {dates[0]} ~ {dates[-1]} "
|
|
353
|
+
f"({window_n} 天,每像元取有效天等权均值)"
|
|
354
|
+
)
|
|
355
|
+
console.print(
|
|
356
|
+
f"\n[bold]读取数据:[/bold] {use_data_dir} (产品: {product})"
|
|
357
|
+
)
|
|
358
|
+
console.print(f" {satellite_filter_label(sensor, use_qa, cf)}")
|
|
359
|
+
window = compute_window_mean(
|
|
360
|
+
reader=reader,
|
|
361
|
+
data_dir=use_data_dir,
|
|
362
|
+
dates=dates,
|
|
363
|
+
extent=read_extent,
|
|
364
|
+
res=use_res,
|
|
365
|
+
qa_threshold=use_qa,
|
|
366
|
+
label=product.upper(),
|
|
367
|
+
console=console,
|
|
368
|
+
)
|
|
369
|
+
console.print(
|
|
370
|
+
f" → 有效天数 [green]{len(window.dates_found)}[/green] / "
|
|
371
|
+
f"{window_n} (失败 {len(window.dates_failed)}),累计 "
|
|
372
|
+
f"[green]{window.n_pixels_total}[/green] 像元 "
|
|
373
|
+
f"({window.n_files_total} 文件, "
|
|
374
|
+
f"{window.n_broken_total} 损坏)"
|
|
375
|
+
)
|
|
376
|
+
# Each cell averages its valid days; cells with none stay NaN.
|
|
377
|
+
prepared = prepare_existing_grid(
|
|
378
|
+
window.LON,
|
|
379
|
+
window.LAT,
|
|
380
|
+
window.grid,
|
|
381
|
+
info.geometry,
|
|
382
|
+
fill_gaps=False,
|
|
383
|
+
)
|
|
384
|
+
provenance = list(window.daily_metadata.values())
|
|
385
|
+
pixels_used = int(window.n_pixels_total)
|
|
386
|
+
valid_days = window.n_valid
|
|
289
387
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
388
|
+
except (RsplotError, OSError):
|
|
389
|
+
raise
|
|
390
|
+
except ValueError as error:
|
|
391
|
+
# Readers raise ValueError when nothing valid survives filtering.
|
|
392
|
+
raise RsplotError("no_valid_data", str(error)) from error
|
|
393
|
+
|
|
394
|
+
# Statistics keep observed cells only. A TROPOMI day fills small gaps
|
|
395
|
+
# for the image alone; windows and GEMS draw observed cells.
|
|
396
|
+
display_source = prepared.unmasked_values
|
|
397
|
+
display_gap_fill = "none"
|
|
398
|
+
if sensor != "gems" and not window_mode:
|
|
399
|
+
display_source = fill_nan_gaps(
|
|
400
|
+
prepared.lon, prepared.lat, display_source
|
|
294
401
|
)
|
|
295
|
-
|
|
402
|
+
display_gap_fill = "nearest_within_3_cells"
|
|
403
|
+
display_lon, display_lat = prepared.lon, prepared.lat
|
|
404
|
+
display_support = None
|
|
405
|
+
if sensor == "gems":
|
|
406
|
+
from rsplot.geo.footprints import prepare_footprint_display
|
|
296
407
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
408
|
+
display = prepare_footprint_display(
|
|
409
|
+
prepared.lon,
|
|
410
|
+
prepared.lat,
|
|
411
|
+
display_source,
|
|
412
|
+
info.geometry,
|
|
413
|
+
analysis_res=use_res,
|
|
414
|
+
display_res=use_res,
|
|
415
|
+
smooth_sigma=use_smooth,
|
|
416
|
+
)
|
|
417
|
+
display_lon, display_lat = display.lon, display.lat
|
|
418
|
+
display_grid, display_support = display.values, display.support
|
|
419
|
+
else:
|
|
420
|
+
display_grid = prepare_display_grid(
|
|
300
421
|
prepared.lon,
|
|
301
422
|
prepared.lat,
|
|
302
|
-
|
|
423
|
+
display_source,
|
|
303
424
|
info.geometry,
|
|
304
|
-
fill_gaps=False,
|
|
305
425
|
smooth_sigma=use_smooth,
|
|
306
426
|
)
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
427
|
+
|
|
428
|
+
if not np.isfinite(display_grid).any():
|
|
429
|
+
if window_mode:
|
|
430
|
+
message = (
|
|
431
|
+
f"{dates[0]}~{dates[-1]} 窗口内目标区域 {info.name} "
|
|
432
|
+
"无任何有效观测。请扩大窗口或检查区域及质量/云量筛选条件。"
|
|
312
433
|
)
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
info.geometry,
|
|
318
|
-
fill_gaps=False,
|
|
434
|
+
else:
|
|
435
|
+
message = (
|
|
436
|
+
f"{date_arg} 在目标区域 {info.name} 内无有效像元。"
|
|
437
|
+
"请检查所选日期、区域及质量/云量筛选条件。"
|
|
319
438
|
)
|
|
439
|
+
raise RsplotError("no_valid_data", message)
|
|
440
|
+
|
|
441
|
+
if use_smooth is not None:
|
|
442
|
+
console.print(f" → 显示高斯平滑 sigma={use_smooth}")
|
|
320
443
|
console.print(
|
|
321
444
|
f" → 网格 {prepared.values.shape}, "
|
|
322
445
|
f"区域内覆盖率 {prepared.coverage_pct:.1f}% "
|
|
@@ -325,62 +448,188 @@ def run_raster(
|
|
|
325
448
|
|
|
326
449
|
if vmin is not None and vmax is not None:
|
|
327
450
|
use_vmin, use_vmax = vmin, vmax
|
|
451
|
+
vrange_source = "cli"
|
|
328
452
|
elif prod_info.vmin is not None and prod_info.vmax is not None:
|
|
329
453
|
use_vmin = vmin if vmin is not None else prod_info.vmin
|
|
330
454
|
use_vmax = vmax if vmax is not None else prod_info.vmax
|
|
455
|
+
vrange_source = "product_default"
|
|
331
456
|
else:
|
|
332
|
-
auto_min, auto_max = auto_vrange(
|
|
457
|
+
auto_min, auto_max = auto_vrange(
|
|
458
|
+
prepared.values
|
|
459
|
+
if np.isfinite(prepared.values).any()
|
|
460
|
+
else display_grid,
|
|
461
|
+
prod_info,
|
|
462
|
+
)
|
|
333
463
|
use_vmin = vmin if vmin is not None else auto_min
|
|
334
464
|
use_vmax = vmax if vmax is not None else auto_max
|
|
465
|
+
vrange_source = "built_in"
|
|
335
466
|
try:
|
|
336
467
|
validate_color_range(use_vmin, use_vmax)
|
|
337
468
|
except ValueError as e:
|
|
338
|
-
|
|
339
|
-
raise typer.Exit(1) from e
|
|
469
|
+
raise RsplotError("invalid_argument", f"色标范围无效:{e}") from e
|
|
340
470
|
console.print(f" → 色标范围: {use_vmin:.1f} ~ {use_vmax:.1f}")
|
|
341
471
|
|
|
342
|
-
if
|
|
343
|
-
if window_mode
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
472
|
+
if processing == "nrti":
|
|
473
|
+
period = f"{dates[0]}_{dates[-1]}" if window_mode else date_arg
|
|
474
|
+
if title is None:
|
|
475
|
+
label = " mean" if window_mode else ""
|
|
476
|
+
title = (
|
|
477
|
+
f"TROPOMI NRTI {product.upper()} "
|
|
478
|
+
f"{period}{label} - {info.name}"
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
if title is None and sensor == "gems":
|
|
482
|
+
period = (
|
|
483
|
+
f"{dates[0]}~{dates[-1]} mean"
|
|
484
|
+
if window_mode
|
|
485
|
+
else (
|
|
486
|
+
f"{date_arg} scan" if scan_mode else f"{date_arg} daily mean"
|
|
487
|
+
)
|
|
488
|
+
)
|
|
489
|
+
title = f"GEMS {product.upper()} {period} - {info.name}"
|
|
347
490
|
if title is None and window_mode:
|
|
348
491
|
title = (
|
|
349
|
-
f"TROPOMI {product.upper()}
|
|
350
|
-
f"
|
|
492
|
+
f"TROPOMI {product.upper()} "
|
|
493
|
+
f"{dates[0]}~{dates[-1]}, {window_n}d mean - {info.name}"
|
|
351
494
|
)
|
|
495
|
+
if title is None:
|
|
496
|
+
title = f"TROPOMI {product.upper()} {dates[0]} - {info.name}"
|
|
352
497
|
|
|
353
|
-
|
|
354
|
-
plot_raster(
|
|
355
|
-
prepared.lon,
|
|
356
|
-
prepared.lat,
|
|
357
|
-
prepared.values,
|
|
358
|
-
info,
|
|
498
|
+
plot_options = dict(
|
|
359
499
|
dpi=use_dpi,
|
|
360
500
|
cmap=use_cmap,
|
|
361
501
|
vmin=use_vmin,
|
|
362
502
|
vmax=use_vmax,
|
|
363
503
|
basemap=use_basemap,
|
|
364
504
|
tianditu_key=cfg.tianditu_key,
|
|
365
|
-
hcho_cmap_file=cfg.hcho_cmap_file,
|
|
366
505
|
title=title,
|
|
367
506
|
output=output,
|
|
507
|
+
**({"coverage_geometry": display_support} if sensor == "gems" else {}),
|
|
368
508
|
colorbar_label=prod_info.colorbar_label,
|
|
369
509
|
)
|
|
370
|
-
console.print(f"\n[bold green]✓ 已保存:[/bold green] {output}")
|
|
371
510
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
511
|
+
# --- sidecar inputs ---------------------------------------------------
|
|
512
|
+
counted = region_grid_mask(prepared.lon, prepared.lat, info.geometry)
|
|
513
|
+
china = sensor == "gems"
|
|
514
|
+
if scan_mode:
|
|
515
|
+
time = {
|
|
516
|
+
"aggregation": "scan",
|
|
517
|
+
"at": date_arg,
|
|
518
|
+
"basis": "nominal_scan_time",
|
|
519
|
+
}
|
|
520
|
+
else:
|
|
521
|
+
time = {
|
|
522
|
+
"aggregation": (
|
|
523
|
+
"window_mean"
|
|
524
|
+
if window_mode
|
|
525
|
+
else "daily_scan_mean"
|
|
526
|
+
if china
|
|
527
|
+
else "daily"
|
|
528
|
+
),
|
|
529
|
+
"start": dates[0],
|
|
530
|
+
"end": dates[-1],
|
|
531
|
+
"basis": "calendar_day",
|
|
532
|
+
}
|
|
533
|
+
days_block = None
|
|
534
|
+
if window_mode:
|
|
535
|
+
assert valid_days is not None
|
|
536
|
+
days_block = {
|
|
537
|
+
"requested": window_n,
|
|
538
|
+
"found": len(window.dates_found),
|
|
539
|
+
"failed": dict(window.dates_failed),
|
|
540
|
+
# One valid day keeps a cell; there is no stricter threshold.
|
|
541
|
+
"n_min": 1,
|
|
542
|
+
"valid_days_per_cell": stats_block(
|
|
543
|
+
valid_days[counted & (valid_days > 0)].astype(float)
|
|
544
|
+
),
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
method: dict[str, Any] = {
|
|
548
|
+
"cloud_fraction_max": cf,
|
|
549
|
+
"grid_deg": use_res,
|
|
550
|
+
"pixels_used": pixels_used,
|
|
551
|
+
"cell_selection": "center_strictly_inside",
|
|
552
|
+
"cell_weighting": "equal",
|
|
553
|
+
}
|
|
554
|
+
if china:
|
|
555
|
+
scans = [scan for meta in provenance for scan in meta.get("scans", [])]
|
|
556
|
+
method.update(
|
|
557
|
+
quality_filter="final_algorithm_flag_0_sza_vza_below_70",
|
|
558
|
+
spatial_aggregation="footprint_overlap_area_mean",
|
|
559
|
+
area_crs="EPSG:6933",
|
|
560
|
+
scan_weighting="equal_scans",
|
|
561
|
+
scans_found=len(scans),
|
|
562
|
+
scans_valid=sum(scan.get("n_pixels", 0) > 0 for scan in scans),
|
|
563
|
+
footprint_source=sorted(
|
|
564
|
+
{
|
|
565
|
+
scan["footprint_source"]
|
|
566
|
+
for scan in scans
|
|
567
|
+
if "footprint_source" in scan
|
|
568
|
+
}
|
|
569
|
+
),
|
|
570
|
+
)
|
|
571
|
+
else:
|
|
572
|
+
method.update(qa_min_exclusive=use_qa)
|
|
573
|
+
if processing == "nrti":
|
|
574
|
+
dedup = [m for m in provenance if "deduplication" in m]
|
|
575
|
+
method["deduplication"] = (
|
|
576
|
+
dedup[0]["deduplication"] if dedup else None
|
|
577
|
+
)
|
|
578
|
+
method["duplicates_removed"] = sum(
|
|
579
|
+
int(m.get("n_duplicates_removed", 0)) for m in dedup
|
|
580
|
+
)
|
|
581
|
+
if window_mode:
|
|
582
|
+
method["day_weighting"] = "equal_days"
|
|
583
|
+
|
|
584
|
+
satellite = SatelliteInputs(
|
|
376
585
|
prod_info=prod_info,
|
|
377
|
-
|
|
378
|
-
|
|
586
|
+
lon=prepared.lon,
|
|
587
|
+
lat=prepared.lat,
|
|
379
588
|
grid=prepared.values,
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
589
|
+
counted=counted,
|
|
590
|
+
time=time,
|
|
591
|
+
data_root=str(use_data_dir),
|
|
592
|
+
metadata=provenance,
|
|
593
|
+
method=method,
|
|
594
|
+
valid_days=valid_days,
|
|
595
|
+
days=days_block,
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
effective_basemap = (
|
|
599
|
+
use_basemap if use_basemap is not None else params.basemap
|
|
600
|
+
)
|
|
601
|
+
display_policy = grid_boundary_policy(
|
|
602
|
+
display_lon, display_lat, display_grid, info
|
|
603
|
+
)["display"]
|
|
604
|
+
if sensor == "gems":
|
|
605
|
+
display_policy["clip"] += "_and_analysis_valid_cells"
|
|
606
|
+
render = {
|
|
607
|
+
"dpi": int(use_dpi),
|
|
608
|
+
"basemap": "satellite" if effective_basemap else "none",
|
|
609
|
+
"title": title,
|
|
610
|
+
"layers": {
|
|
611
|
+
"satellite": {
|
|
612
|
+
"cmap": use_cmap,
|
|
613
|
+
"vmin": use_vmin,
|
|
614
|
+
"vmax": use_vmax,
|
|
615
|
+
"range_method": RANGE_METHOD[vrange_source],
|
|
616
|
+
"display_gap_fill": display_gap_fill,
|
|
617
|
+
"display_smoothing_sigma": use_smooth or 0,
|
|
618
|
+
"display_grid_shape": list(display_grid.shape),
|
|
619
|
+
"display_selection": display_policy["selection"],
|
|
620
|
+
"display_clip": display_policy["clip"],
|
|
621
|
+
"display_valid_cells": display_policy[
|
|
622
|
+
"n_valid_intersecting_cells"
|
|
623
|
+
],
|
|
624
|
+
}
|
|
625
|
+
},
|
|
626
|
+
}
|
|
627
|
+
return RasterRun(
|
|
628
|
+
display_lon,
|
|
629
|
+
display_lat,
|
|
630
|
+
display_grid,
|
|
631
|
+
info,
|
|
632
|
+
plot_options,
|
|
633
|
+
satellite=satellite,
|
|
634
|
+
render=render,
|
|
385
635
|
)
|
|
386
|
-
emit_result(result, output)
|