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
rsplot/temporal.py CHANGED
@@ -7,9 +7,9 @@ remain available for compatibility.
7
7
 
8
8
  from __future__ import annotations
9
9
 
10
- from dataclasses import dataclass
10
+ from dataclasses import dataclass, field
11
11
  from datetime import datetime, timedelta
12
- from typing import TYPE_CHECKING
12
+ from typing import TYPE_CHECKING, Any
13
13
  from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
14
14
 
15
15
  import numpy as np
@@ -135,6 +135,7 @@ class WindowResult:
135
135
  n_pixels_total: int
136
136
  n_files_total: int
137
137
  n_broken_total: int
138
+ daily_metadata: dict[str, Any] = field(default_factory=dict)
138
139
 
139
140
 
140
141
  def compute_window_mean(
@@ -156,6 +157,7 @@ def compute_window_mean(
156
157
  valid_counts: np.ndarray | None = None
157
158
  dates_found: list[str] = []
158
159
  dates_failed: dict[str, str] = {}
160
+ daily_metadata: dict[str, Any] = {}
159
161
  n_pix_total = 0
160
162
  n_files_total = 0
161
163
  n_broken_total = 0
@@ -164,19 +166,22 @@ def compute_window_mean(
164
166
 
165
167
  for date in track(dates, description=f"[cyan]读取 {label} 窗口..."):
166
168
  try:
167
- swath = reader.read(
168
- data_dir,
169
- date,
170
- extent,
169
+ batch = read_scan_mean(
170
+ reader=reader,
171
+ data_dir=data_dir,
172
+ request=date,
173
+ extent=extent,
174
+ res=res,
171
175
  qa_threshold=qa_threshold,
172
176
  )
173
- day_lon, day_lat, grid = grid_data(
174
- swath.lon,
175
- swath.lat,
176
- swath.values,
177
- extent,
178
- res,
179
- )
177
+ if batch.metadata:
178
+ daily_metadata[date] = batch.metadata
179
+ n_pix_total += batch.n_pixels
180
+ n_files_total += batch.n_files
181
+ n_broken_total += batch.n_broken
182
+ day_lon, day_lat, grid = batch.LON, batch.LAT, batch.grid
183
+ if not np.isfinite(grid).any():
184
+ raise ValueError(batch.failure_message())
180
185
  LON, LAT = day_lon, day_lat
181
186
  if sum_grid is None:
182
187
  sum_grid = np.zeros(grid.shape, dtype=float)
@@ -185,9 +190,6 @@ def compute_window_mean(
185
190
  np.add(sum_grid, grid, out=sum_grid, where=finite)
186
191
  valid_counts += finite
187
192
  dates_found.append(date)
188
- n_pix_total += int(swath.n_pixels)
189
- n_files_total += int(swath.n_files)
190
- n_broken_total += int(swath.n_broken)
191
193
  except (FileNotFoundError, ValueError) as e:
192
194
  dates_failed[date] = str(e).splitlines()[0]
193
195
 
@@ -221,4 +223,132 @@ def compute_window_mean(
221
223
  n_pixels_total=n_pix_total,
222
224
  n_files_total=n_files_total,
223
225
  n_broken_total=n_broken_total,
226
+ daily_metadata=daily_metadata,
227
+ )
228
+
229
+
230
+ @dataclass
231
+ class ScanMean:
232
+ """An unfilled grid mean with per-cell temporal sample counts."""
233
+
234
+ LON: np.ndarray
235
+ LAT: np.ndarray
236
+ grid: np.ndarray
237
+ n_valid: np.ndarray
238
+ n_pixels: int
239
+ n_files: int
240
+ n_broken: int
241
+ metadata: dict[str, Any]
242
+
243
+ def failure_message(self) -> str:
244
+ """Keep CLI errors compact; full per-file diagnostics stay in JSON."""
245
+ message = (
246
+ f"{self.metadata.get('request', '')} 区域内无有效扫描 "
247
+ f"({self.n_files} 文件, {self.n_broken} 损坏)。"
248
+ )
249
+ examples = list(self.metadata.get("failed_files", {}).items())[:3]
250
+ if examples:
251
+ from pathlib import Path
252
+
253
+ message += " " + "; ".join(
254
+ f"{Path(path).name}: {error}" for path, error in examples
255
+ )
256
+ return message
257
+
258
+
259
+ def read_scan_mean(
260
+ *,
261
+ reader: BaseReader,
262
+ data_dir: str,
263
+ request: str,
264
+ extent: tuple[float, float, float, float],
265
+ res: float,
266
+ qa_threshold: float = 0.5,
267
+ ) -> ScanMean:
268
+ """Grid each scan independently, then average valid scans equally."""
269
+ # A NaN sentinel establishes the grid even if every scan is invalid.
270
+ lon, lat, template = grid_data(
271
+ np.array([extent[0]]),
272
+ np.array([extent[2]]),
273
+ np.array([np.nan]),
274
+ extent,
275
+ res,
276
+ )
277
+ footprints = getattr(reader, "uses_footprints", False)
278
+ if footprints:
279
+ from rsplot.geo.footprints import overlap_mean, regular_grid
280
+
281
+ lon, lat = regular_grid(extent, res)
282
+ template = np.full(lon.shape, np.nan)
283
+ total = np.zeros(template.shape, dtype=float)
284
+ counts = np.zeros(template.shape, dtype=np.int32)
285
+ n_pixels = n_files = n_broken = 0
286
+ scans = []
287
+ iterator = getattr(reader, "iter_scans", None)
288
+ scans_iter = (
289
+ iterator(data_dir, request, extent, qa_threshold)
290
+ if iterator is not None
291
+ else (reader.read(data_dir, request, extent, qa_threshold),)
292
+ )
293
+ for scan in scans_iter:
294
+ n_pixels += scan.n_pixels
295
+ n_files += scan.n_files
296
+ n_broken += scan.n_broken
297
+ if scan.metadata:
298
+ scans.append(scan.metadata)
299
+ if not scan.n_pixels:
300
+ continue
301
+ if footprints:
302
+ if scan.corner_lon is None or scan.corner_lat is None:
303
+ raise ValueError("GEMS 扫描缺少像元边界,不能退回中心点分箱。")
304
+ grid = overlap_mean(
305
+ scan.corner_lon, scan.corner_lat, scan.values, lon, lat, res
306
+ )
307
+ else:
308
+ _, _, grid = grid_data(
309
+ scan.lon, scan.lat, scan.values, extent, res
310
+ )
311
+ valid = np.isfinite(grid)
312
+ np.add(total, grid, out=total, where=valid)
313
+ counts += valid
314
+ mean = np.full(template.shape, np.nan)
315
+ np.divide(total, counts, out=mean, where=counts > 0)
316
+ covered = counts[counts > 0]
317
+ metadata = {
318
+ "request": request,
319
+ "input_timezone": "Asia/Shanghai",
320
+ "aggregation": "scan" if len(request) == 12 else "equal_scan_mean",
321
+ "scans": scans,
322
+ "n_scans_found": len(scans),
323
+ "n_scans_valid": sum(s.get("n_pixels", 0) > 0 for s in scans),
324
+ "failed_files": {s["file"]: s["error"] for s in scans if "error" in s},
325
+ "coverage_scope": "read_extent",
326
+ "read_extent": list(extent),
327
+ "coverage_scans": {
328
+ "min": int(covered.min()) if covered.size else 0,
329
+ "max": int(covered.max()) if covered.size else 0,
330
+ "mean": float(covered.mean()) if covered.size else 0.0,
331
+ },
332
+ }
333
+ if getattr(reader, "processing", None) == "nrti" and scans:
334
+ metadata = {
335
+ **scans[0],
336
+ "request": request,
337
+ "coverage_scope": "read_extent",
338
+ "read_extent": list(extent),
339
+ }
340
+ if footprints:
341
+ metadata["spatial_aggregation"] = "footprint_overlap_area_mean"
342
+ metadata["area_crs"] = "EPSG:6933"
343
+ metadata["analysis_resolution_deg"] = res
344
+ metadata["grid_alignment"] = "global_multiples_of_resolution"
345
+ return ScanMean(
346
+ lon,
347
+ lat,
348
+ mean,
349
+ counts,
350
+ n_pixels,
351
+ n_files,
352
+ n_broken,
353
+ metadata if scans else {},
224
354
  )