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.
Files changed (71) hide show
  1. rsplot/atomic.py +36 -0
  2. rsplot/cli.py +282 -469
  3. rsplot/commands/common.py +113 -1
  4. rsplot/commands/fnr.py +389 -104
  5. rsplot/commands/forecast.py +24 -7
  6. rsplot/commands/meteo.py +499 -186
  7. rsplot/commands/overlay.py +292 -205
  8. rsplot/commands/radius.py +526 -0
  9. rsplot/commands/raster.py +479 -230
  10. rsplot/commands/reconstruct.py +27 -6
  11. rsplot/commands/sidecar.py +106 -0
  12. rsplot/commands/station.py +154 -42
  13. rsplot/commands/station_trend.py +259 -174
  14. rsplot/config.py +46 -16
  15. rsplot/data_files.py +253 -0
  16. rsplot/errors.py +43 -0
  17. rsplot/fnr.py +38 -46
  18. rsplot/geo/boundaries.py +317 -11
  19. rsplot/geo/footprints.py +205 -0
  20. rsplot/geo/processing.py +84 -5
  21. rsplot/geo/radius.py +205 -0
  22. rsplot/plotting/colormap_22.txt +256 -0
  23. rsplot/plotting/colormaps.py +24 -29
  24. rsplot/plotting/map_frame.py +203 -9
  25. rsplot/plotting/meteo.py +10 -3
  26. rsplot/plotting/overlay.py +36 -40
  27. rsplot/plotting/raster.py +15 -3
  28. rsplot/plotting/station.py +86 -33
  29. rsplot/plotting/station_focus.py +84 -0
  30. rsplot/plotting/station_trend.py +60 -47
  31. rsplot/plotting/styles.py +3 -5
  32. rsplot/readers/__init__.py +89 -7
  33. rsplot/readers/_tropomi.py +121 -3
  34. rsplot/readers/base.py +16 -1
  35. rsplot/readers/era5.py +110 -10
  36. rsplot/readers/era5_wind.py +3 -2
  37. rsplot/readers/gems.py +429 -0
  38. rsplot/readers/guokong.py +125 -39
  39. rsplot/readers/netcdf.py +32 -0
  40. rsplot/readers/tropomi_hcho.py +1 -1
  41. rsplot/readers/tropomi_no2.py +1 -1
  42. rsplot/readers/tropomi_nrti.py +116 -0
  43. rsplot/readers/tropomi_o3.py +1 -1
  44. rsplot/readers/tropomi_o3pr.py +1 -1
  45. rsplot/result_builders/__init__.py +0 -14
  46. rsplot/result_builders/common.py +84 -0
  47. rsplot/result_builders/envelope.py +236 -0
  48. rsplot/result_builders/fnr.py +47 -131
  49. rsplot/result_builders/forecast.py +7 -0
  50. rsplot/result_builders/provenance.py +59 -0
  51. rsplot/result_builders/reconstruct.py +7 -0
  52. rsplot/result_builders/satellite.py +240 -0
  53. rsplot/result_builders/series_layer.py +289 -0
  54. rsplot/result_builders/station.py +23 -0
  55. rsplot/result_builders/station_layer.py +326 -0
  56. rsplot/results.py +0 -12
  57. rsplot/schemas/result.schema.json +566 -0
  58. rsplot/temporal.py +146 -16
  59. rsplot-0.3.2.dev37.dist-info/METADATA +996 -0
  60. rsplot-0.3.2.dev37.dist-info/RECORD +76 -0
  61. {rsplot-0.3.1.dev3.dist-info → rsplot-0.3.2.dev37.dist-info}/WHEEL +1 -1
  62. rsplot/commands/recent.py +0 -164
  63. rsplot/recent.py +0 -404
  64. rsplot/result_builders/meteo.py +0 -171
  65. rsplot/result_builders/overlay.py +0 -75
  66. rsplot/result_builders/raster.py +0 -72
  67. rsplot/result_builders/station_trend.py +0 -229
  68. rsplot-0.3.1.dev3.dist-info/METADATA +0 -477
  69. rsplot-0.3.1.dev3.dist-info/RECORD +0 -64
  70. {rsplot-0.3.1.dev3.dist-info → rsplot-0.3.2.dev37.dist-info}/entry_points.txt +0 -0
  71. {rsplot-0.3.1.dev3.dist-info → rsplot-0.3.2.dev37.dist-info}/licenses/LICENSE +0 -0
@@ -1,171 +0,0 @@
1
- """Meteorological map JSON-sidecar construction."""
2
-
3
- from __future__ import annotations
4
-
5
- from typing import TYPE_CHECKING, Any
6
-
7
- import numpy as np
8
-
9
- from rsplot.result_builders.common import serialize_region
10
- from rsplot.result_builders.station import build_station_result
11
- from rsplot.stats import basic_stats, round_finite
12
-
13
- if TYPE_CHECKING:
14
- import geopandas as gpd
15
-
16
- from rsplot.geo.boundaries import RegionInfo
17
- from rsplot.readers.era5 import Era5FieldData
18
- from rsplot.readers.era5_wind import WindFieldData
19
- from rsplot.readers.guokong import StationData
20
-
21
-
22
- def build_wind_block(wind: WindFieldData, stride: int) -> dict[str, Any]:
23
- """Build machine-readable metadata for a plotted ERA5 wind layer."""
24
- step = max(1, stride)
25
- sample = np.s_[::step, ::step]
26
- sampled_valid = np.isfinite(wind.speed[sample])
27
- finite = np.isfinite(wind.speed)
28
- total = wind.n_cells
29
-
30
- mean_u = float(np.nanmean(wind.u))
31
- mean_v = float(np.nanmean(wind.v))
32
- direction_to = (np.degrees(np.arctan2(mean_u, mean_v)) + 360.0) % 360.0
33
- direction_from = (direction_to + 180.0) % 360.0
34
-
35
- max_flat_index = int(np.nanargmax(wind.speed))
36
- max_index = np.unravel_index(max_flat_index, wind.speed.shape)
37
- return {
38
- "source": "ERA5",
39
- "level": wind.level,
40
- "station_datetime": wind.station_datetime,
41
- "datetime_utc": wind.datetime_utc,
42
- "unit": wind.unit,
43
- "source_files": list(wind.source_files),
44
- "grid_shape": list(wind.speed.shape),
45
- "coverage_pct": (
46
- round(100 * int(finite.sum()) / total, 1) if total else 0.0
47
- ),
48
- "n_region_cells": int(total),
49
- "stride": step,
50
- "n_vectors_valid": int(finite.sum()),
51
- "n_arrows_plotted": int(sampled_valid.sum()),
52
- "display": {"color": "black", "colorbar": False},
53
- "u_stats": basic_stats(wind.u),
54
- "v_stats": basic_stats(wind.v),
55
- "speed_stats": basic_stats(wind.speed),
56
- "resultant": {
57
- "mean_u": round_finite(mean_u),
58
- "mean_v": round_finite(mean_v),
59
- "direction_to_deg": round_finite(direction_to, 1),
60
- "wind_from_deg": round_finite(direction_from, 1),
61
- },
62
- "max_speed_location": {
63
- "lon": round(float(wind.lon[max_index]), 3),
64
- "lat": round(float(wind.lat[max_index]), 3),
65
- "speed": round_finite(wind.speed[max_index]),
66
- },
67
- }
68
-
69
-
70
- def build_meteo_result(
71
- *,
72
- region: RegionInfo,
73
- variable: str,
74
- input_datetime: str,
75
- input_timezone: str,
76
- datetime_utc: str,
77
- output: str,
78
- requested_datetime: str | None = None,
79
- field: Era5FieldData | None = None,
80
- field_cmap: str | None = None,
81
- field_vmin: float | None = None,
82
- field_vmax: float | None = None,
83
- wind: WindFieldData | None = None,
84
- wind_stride: int | None = None,
85
- station: StationData | None = None,
86
- station_datetime: str | None = None,
87
- station_threshold: float | None = None,
88
- station_unit: str = "",
89
- cities_gdf: gpd.GeoDataFrame | None = None,
90
- ) -> dict[str, Any]:
91
- """Build the stable machine-readable result for ``meteo``."""
92
- mode = _meteo_mode(field=field, wind=wind, station=station)
93
- primary_level = field.level if field is not None else wind.level
94
- primary_unit = field.unit if field is not None else wind.unit
95
- result: dict[str, Any] = {
96
- "command": "meteo",
97
- "mode": mode,
98
- "image": output,
99
- "region": serialize_region(region),
100
- "datetime": input_datetime,
101
- "requested_datetime": requested_datetime or input_datetime,
102
- "timezone": input_timezone,
103
- "datetime_utc": datetime_utc,
104
- "variable": variable,
105
- "level": primary_level,
106
- "unit": primary_unit,
107
- }
108
-
109
- if field is not None:
110
- total = field.n_cells
111
- result["field"] = {
112
- "variable": field.variable,
113
- "level": field.level,
114
- "unit": field.unit,
115
- "source_file": field.source_file,
116
- "grid_shape": list(field.values.shape),
117
- "coverage_pct": (
118
- round(100 * field.n_valid / total, 1) if total else 0.0
119
- ),
120
- "n_region_cells": total,
121
- "n_valid": field.n_valid,
122
- "stats": basic_stats(field.values),
123
- "display": {
124
- "cmap": field_cmap,
125
- "vmin": (
126
- round_finite(field_vmin)
127
- if field_vmin is not None
128
- else None
129
- ),
130
- "vmax": (
131
- round_finite(field_vmax)
132
- if field_vmax is not None
133
- else None
134
- ),
135
- "range_method": "manual_or_region_p02_p98",
136
- },
137
- }
138
-
139
- if wind is not None:
140
- result["wind"] = build_wind_block(wind, wind_stride or 1)
141
-
142
- if station is not None:
143
- station_block = build_station_result(
144
- region=region,
145
- data=station,
146
- datetime_str=station_datetime or input_datetime,
147
- threshold=station_threshold,
148
- unit=station_unit,
149
- cities_gdf=cities_gdf,
150
- output=output,
151
- )
152
- for key in ("command", "image", "region"):
153
- station_block.pop(key, None)
154
- result["station"] = station_block
155
-
156
- return result
157
-
158
-
159
- def _meteo_mode(
160
- *,
161
- field: Era5FieldData | None,
162
- wind: WindFieldData | None,
163
- station: StationData | None,
164
- ) -> str:
165
- if station is not None and field is not None:
166
- return "temperature_station_wind"
167
- if station is not None:
168
- return "station_wind"
169
- if field is not None and wind is not None:
170
- return "temperature_wind"
171
- return "temperature" if field is not None else "wind"
@@ -1,75 +0,0 @@
1
- """Combined raster/station JSON-sidecar construction."""
2
-
3
- from __future__ import annotations
4
-
5
- from typing import TYPE_CHECKING, Any
6
-
7
- import numpy as np
8
-
9
- from rsplot.result_builders.common import serialize_region
10
- from rsplot.result_builders.raster import build_raster_result
11
- from rsplot.result_builders.station import build_station_result
12
-
13
- if TYPE_CHECKING:
14
- import geopandas as gpd
15
-
16
- from rsplot.geo.boundaries import RegionInfo
17
- from rsplot.readers import ProductInfo
18
- from rsplot.readers.guokong import StationData
19
-
20
-
21
- def build_overlay_result(
22
- *,
23
- region: RegionInfo,
24
- product: str,
25
- date: str,
26
- prod_info: ProductInfo,
27
- LON: np.ndarray,
28
- LAT: np.ndarray,
29
- grid: np.ndarray,
30
- swath_meta: dict[str, Any],
31
- raster_vmin: float,
32
- raster_vmax: float,
33
- data: StationData,
34
- datetime_str: str,
35
- threshold: float | None,
36
- unit: str,
37
- cities_gdf: gpd.GeoDataFrame | None,
38
- output: str,
39
- ) -> dict[str, Any]:
40
- """Combine raster and station result blocks without shared duplication."""
41
- raster_block = build_raster_result(
42
- region=region,
43
- product=product,
44
- date=date,
45
- prod_info=prod_info,
46
- LON=LON,
47
- LAT=LAT,
48
- grid=grid,
49
- swath_meta=swath_meta,
50
- vmin=raster_vmin,
51
- vmax=raster_vmax,
52
- output=output,
53
- )
54
- station_block = build_station_result(
55
- region=region,
56
- data=data,
57
- datetime_str=datetime_str,
58
- threshold=threshold,
59
- unit=unit,
60
- cities_gdf=cities_gdf,
61
- output=output,
62
- )
63
- for key in ("command", "image", "region"):
64
- raster_block.pop(key, None)
65
- station_block.pop(key, None)
66
-
67
- return {
68
- "command": "overlay",
69
- "image": output,
70
- "region": serialize_region(region),
71
- "datetime": datetime_str,
72
- "date": date,
73
- "raster": raster_block,
74
- "station": station_block,
75
- }
@@ -1,72 +0,0 @@
1
- """Raster JSON-sidecar construction."""
2
-
3
- from __future__ import annotations
4
-
5
- from typing import TYPE_CHECKING, Any
6
-
7
- import numpy as np
8
-
9
- from rsplot.result_builders.common import (
10
- grid_per_admin_stats,
11
- grid_stats,
12
- serialize_region,
13
- top_hotspots,
14
- )
15
- from rsplot.stats import round_finite
16
-
17
- if TYPE_CHECKING:
18
- from rsplot.geo.boundaries import RegionInfo
19
- from rsplot.readers import ProductInfo
20
-
21
-
22
- def build_raster_result(
23
- *,
24
- region: RegionInfo,
25
- product: str,
26
- date: str,
27
- prod_info: ProductInfo,
28
- LON: np.ndarray,
29
- LAT: np.ndarray,
30
- grid: np.ndarray,
31
- swath_meta: dict[str, Any],
32
- vmin: float,
33
- vmax: float,
34
- output: str,
35
- window: dict[str, Any] | None = None,
36
- ) -> dict[str, Any]:
37
- """Build the stable JSON sidecar payload for a raster run."""
38
- result: dict[str, Any] = {
39
- "command": "raster",
40
- "image": output,
41
- "region": serialize_region(region),
42
- "product": product,
43
- "date": date,
44
- "unit": prod_info.colorbar_label,
45
- "vrange": {
46
- "vmin": round_finite(vmin),
47
- "vmax": round_finite(vmax),
48
- },
49
- "swath": swath_meta,
50
- "stats": grid_stats(
51
- LON,
52
- LAT,
53
- grid,
54
- region,
55
- valid_key="valid_region_pixels",
56
- total_key="total_region_pixels",
57
- ),
58
- }
59
- if window is not None:
60
- result["window"] = window
61
-
62
- sub_gdf = region.sub_boundary_gdf
63
- if sub_gdf is not None and len(sub_gdf) > 0:
64
- result["spatial_summary"] = grid_per_admin_stats(
65
- LON,
66
- LAT,
67
- grid,
68
- sub_gdf,
69
- )
70
- result["hotspots"] = top_hotspots(LON, LAT, grid, sub_gdf, n=5)
71
-
72
- return result
@@ -1,229 +0,0 @@
1
- """JSON-sidecar construction for station and regional hourly trends."""
2
-
3
- from __future__ import annotations
4
-
5
- from typing import TYPE_CHECKING, Any
6
-
7
- import numpy as np
8
-
9
- from rsplot.readers.guokong import RegionStationSeries, StationSeries
10
- from rsplot.result_builders.common import serialize_region
11
- from rsplot.stats import basic_stats, round_finite
12
-
13
- if TYPE_CHECKING:
14
- from rsplot.geo.boundaries import RegionInfo
15
-
16
-
17
- def build_station_trend_result(
18
- *,
19
- series: StationSeries,
20
- dates: list[str],
21
- threshold: float | None,
22
- unit: str,
23
- output: str,
24
- ) -> dict[str, Any]:
25
- """Build the machine-readable payload for a single-station trend."""
26
- valid = np.isfinite(series.values)
27
- n_valid = int(valid.sum())
28
- result: dict[str, Any] = {
29
- "command": "trend",
30
- "image": output,
31
- "station": {
32
- "id": series.station_id,
33
- "lon": round(series.lon, 3),
34
- "lat": round(series.lat, 3),
35
- },
36
- "variable": series.var_name,
37
- "unit": unit or None,
38
- "threshold": threshold,
39
- "period": {
40
- "start": series.timestamps[0],
41
- "end": series.timestamps[-1],
42
- "days_requested": len(dates),
43
- "hours_requested": series.n_hours,
44
- },
45
- "availability": {
46
- "files_found": len(series.source_files),
47
- "files_missing": len(series.missing_files),
48
- "files_failed": len(series.failed_files),
49
- "hours_station_missing": len(series.station_missing_hours),
50
- "hours_valid": n_valid,
51
- "missing_file_hours": list(series.missing_files),
52
- "failed_files": dict(series.failed_files),
53
- "station_missing_hours": list(series.station_missing_hours),
54
- },
55
- "stats": basic_stats(series.values),
56
- "series": [
57
- {
58
- "datetime": timestamp,
59
- "value": round_finite(value),
60
- }
61
- for timestamp, value in zip(
62
- series.timestamps,
63
- series.values,
64
- strict=True,
65
- )
66
- ],
67
- }
68
-
69
- if threshold is not None and n_valid:
70
- exceed = int((series.values[valid] > threshold).sum())
71
- result["exceedance"] = {
72
- "count": exceed,
73
- "rate_pct": round(100 * exceed / n_valid, 1),
74
- }
75
- return result
76
-
77
-
78
- def build_region_trend_result(
79
- *,
80
- series: RegionStationSeries,
81
- region: RegionInfo,
82
- dates: list[str],
83
- threshold: float | None,
84
- unit: str,
85
- view: str,
86
- focus_station_ids: list[str],
87
- output: str,
88
- ) -> dict[str, Any]:
89
- """Build the compact machine-readable payload for a regional trend."""
90
- mean_values = series.hourly_mean
91
- hourly_counts = series.hourly_valid_counts
92
- station_summaries: list[dict[str, Any]] = []
93
- exceeding_stations: list[dict[str, Any]] = []
94
-
95
- for index, station_id in enumerate(series.station_ids.astype(str)):
96
- values = series.values[index]
97
- summary: dict[str, Any] = {
98
- "id": station_id,
99
- "lon": round(float(series.lon[index]), 3),
100
- "lat": round(float(series.lat[index]), 3),
101
- "hours_valid": int(np.isfinite(values).sum()),
102
- "stats": basic_stats(values),
103
- }
104
- if threshold is not None and summary["hours_valid"]:
105
- exceedance = _exceedance_stats(values, threshold)
106
- summary["exceedance"] = exceedance
107
- if exceedance["count"]:
108
- exceeding_stations.append(
109
- {
110
- "id": station_id,
111
- "lon": summary["lon"],
112
- "lat": summary["lat"],
113
- **exceedance,
114
- }
115
- )
116
- station_summaries.append(summary)
117
-
118
- exceeding_stations.sort(
119
- key=lambda row: (
120
- -(row["max_excess"] or 0),
121
- -row["count"],
122
- row["id"],
123
- )
124
- )
125
- focus_stations = [
126
- _focus_station_result(series, station_id, threshold)
127
- for station_id in focus_station_ids
128
- ]
129
-
130
- return {
131
- "command": "trend",
132
- "mode": "region",
133
- "image": output,
134
- "region": serialize_region(region),
135
- "variable": series.var_name,
136
- "unit": unit or None,
137
- "threshold": threshold,
138
- "params": {
139
- "view": view,
140
- "focus_station_ids": focus_station_ids,
141
- },
142
- "period": {
143
- "start": series.timestamps[0],
144
- "end": series.timestamps[-1],
145
- "days_requested": len(dates),
146
- "hours_requested": series.n_hours,
147
- },
148
- "availability": {
149
- "files_found": len(series.source_files),
150
- "files_missing": len(series.missing_files),
151
- "files_failed": len(series.failed_files),
152
- "stations_total": series.n_stations,
153
- "stations_with_data": int(
154
- np.isfinite(series.values).any(axis=1).sum()
155
- ),
156
- "hours_with_data": int((hourly_counts > 0).sum()),
157
- "observation_values": series.n_valid,
158
- "missing_file_hours": list(series.missing_files),
159
- "failed_files": dict(series.failed_files),
160
- },
161
- "regional_mean": {
162
- "stats": basic_stats(mean_values),
163
- "series": [
164
- {
165
- "datetime": timestamp,
166
- "value": round_finite(value),
167
- "n_stations": int(count),
168
- }
169
- for timestamp, value, count in zip(
170
- series.timestamps,
171
- mean_values,
172
- hourly_counts,
173
- strict=True,
174
- )
175
- ],
176
- },
177
- "station_summaries": station_summaries,
178
- "exceeding_stations": exceeding_stations,
179
- "focus_stations": focus_stations,
180
- }
181
-
182
-
183
- def _focus_station_result(
184
- series: RegionStationSeries,
185
- station_id: str,
186
- threshold: float | None,
187
- ) -> dict[str, Any]:
188
- index = series.station_index(station_id)
189
- if index is None:
190
- raise ValueError(f"区域时序中未找到重点站 '{station_id}'")
191
- values = series.values[index]
192
- result: dict[str, Any] = {
193
- "id": str(series.station_ids[index]),
194
- "lon": round(float(series.lon[index]), 3),
195
- "lat": round(float(series.lat[index]), 3),
196
- "stats": basic_stats(values),
197
- "series": [
198
- {
199
- "datetime": timestamp,
200
- "value": round_finite(value),
201
- }
202
- for timestamp, value in zip(
203
- series.timestamps,
204
- values,
205
- strict=True,
206
- )
207
- ],
208
- }
209
- if threshold is not None and np.isfinite(values).any():
210
- result["exceedance"] = _exceedance_stats(values, threshold)
211
- return result
212
-
213
-
214
- def _exceedance_stats(
215
- values: np.ndarray,
216
- threshold: float,
217
- ) -> dict[str, Any]:
218
- valid = np.isfinite(values)
219
- n_valid = int(valid.sum())
220
- exceed = valid & (values > threshold)
221
- count = int(exceed.sum())
222
- max_excess = (
223
- round_finite(np.max(values[exceed] - threshold)) if count else None
224
- )
225
- return {
226
- "count": count,
227
- "rate_pct": round(100 * count / n_valid, 1) if n_valid else 0.0,
228
- "max_excess": max_excess,
229
- }