ebas-plot 0.1.6__tar.gz → 0.2.1__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ebas-plot
3
- Version: 0.1.6
3
+ Version: 0.2.1
4
4
  Summary: Plotting utilities for EBAS netCDF datasets.
5
5
  Author-email: Lise Eder Murberg <lemu@nilu.no>, Marthe Brevig <mbre@nilu.no>, Jonathan Elias Holme <jhol@nilu.no>
6
6
  License-Expression: AGPL-3.0-or-later
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ebas-plot
3
- Version: 0.1.6
3
+ Version: 0.2.1
4
4
  Summary: Plotting utilities for EBAS netCDF datasets.
5
5
  Author-email: Lise Eder Murberg <lemu@nilu.no>, Marthe Brevig <mbre@nilu.no>, Jonathan Elias Holme <jhol@nilu.no>
6
6
  License-Expression: AGPL-3.0-or-later
@@ -1,3 +1,4 @@
1
+ import itertools
1
2
  import re
2
3
 
3
4
  import xarray as xr
@@ -7,7 +8,7 @@ import requests
7
8
  from netCDF4 import date2num, num2date
8
9
  import pandas as pd
9
10
 
10
- __version__ = "0.1.6"
11
+ __version__ = "0.2.1"
11
12
 
12
13
 
13
14
  class DatasetAccessError(Exception):
@@ -66,7 +67,7 @@ _STATISTICS = {
66
67
  "_precision", "_max", "_min", "_median",
67
68
  } # ancillary statistics; excluded from plots (not primary mean values)
68
69
  # Only these d_* dimensions are allowed in timeseries plots, d_D is in spesific PNSD plot.
69
- _ALLOWED_D_DIMS = {"d_Wavelength", "d_Tower_inlet_height"}
70
+ _ALLOWED_D_DIMS = {"d_Wavelength", "d_Tower_inlet_height", "d_RH"}
70
71
 
71
72
  _RESOLUTION_UNIT_DAYS = {"mn": 1 / 1440, "h": 1 / 24, "d": 1, "w": 7, "mo": 30, "y": 365} # used for converting resolution codes to days
72
73
  _RESOLUTION_CODE_RE = re.compile(r"^(\d+)(mn|mo|[hdwy])$")
@@ -257,9 +258,19 @@ def _dataset_title(ds: xr.Dataset) -> str:
257
258
  return station or "Unknown Site"
258
259
 
259
260
 
260
- def _var_mean(ds: xr.Dataset, name: str) -> float:
261
+ def _var_mean(
262
+ ds: xr.Dataset,
263
+ name: str,
264
+ data_cache: dict[str, np.ndarray] | None = None,
265
+ ) -> float:
261
266
  """Mean of finite values, used as a sort key."""
262
- vals = ds[name].values.ravel()
267
+ if data_cache is not None and name in data_cache:
268
+ values = data_cache[name]
269
+ else:
270
+ values = ds[name].values
271
+ if data_cache is not None:
272
+ data_cache[name] = values
273
+ vals = values.ravel()
263
274
  finite = vals[np.isfinite(vals)]
264
275
  return float(finite.mean()) if len(finite) else 0.0
265
276
 
@@ -304,7 +315,7 @@ def _default_plot_mode(ds: xr.Dataset, time_step: pd.Timedelta) -> str:
304
315
  return "line"
305
316
 
306
317
 
307
- def _scatter_hovertexts(x_time, ds: xr.Dataset) -> list[str]:
318
+ def _scatter_hovertexts(x_time, ds: xr.Dataset) -> np.ndarray:
308
319
  """Build per-point hover strings e.g. 'Aug 2006 \u2013 Oct 2006 (3mo)' or '12 \u2013 19 Nov 2012 (1w)'."""
309
320
  if "time_bnds" in ds:
310
321
  bnds = ds["time_bnds"].values
@@ -314,6 +325,20 @@ def _scatter_hovertexts(x_time, ds: xr.Dataset) -> list[str]:
314
325
  res = ds.attrs.get("ebas_resolution_code", "")
315
326
  suffix = f" ({res})" if res else ""
316
327
 
328
+ try:
329
+ start_dates = pd.DatetimeIndex(pd.to_datetime(starts))
330
+ end_dates = pd.DatetimeIndex(pd.to_datetime(ends))
331
+ return np.asarray([
332
+ f"{start:%b %Y} \u2013 {end:%b %Y}{suffix}"
333
+ if end - start >= _MONTH_ONLY_THRESHOLD else
334
+ f"{start:%-d} \u2013 {end:%-d %b %Y}{suffix}"
335
+ if start.month == end.month and start.year == end.year else
336
+ f"{start:%-d %b} \u2013 {end:%-d %b %Y}{suffix}"
337
+ for start, end in zip(start_dates, end_dates)
338
+ ], dtype=object)
339
+ except (TypeError, ValueError):
340
+ pass
341
+
317
342
  def _fmt(s, e) -> str:
318
343
  start, end = pd.Timestamp(s), pd.Timestamp(e)
319
344
  if end - start >= _MONTH_ONLY_THRESHOLD: # coarse/campaign samples
@@ -323,7 +348,7 @@ def _scatter_hovertexts(x_time, ds: xr.Dataset) -> list[str]:
323
348
  start_fmt = f"{start:%-d}" if same_month else f"{start:%-d %b}"
324
349
  return f"{start_fmt} \u2013 {end:%-d %b %Y}{suffix}"
325
350
 
326
- return [_fmt(s, e) for s, e in zip(starts, ends)]
351
+ return np.asarray([_fmt(s, e) for s, e in zip(starts, ends)], dtype=object)
327
352
 
328
353
 
329
354
  def _pnsd_var(ds: xr.Dataset) -> str | None:
@@ -336,7 +361,10 @@ def _dimension_with_prefix(da: xr.DataArray, prefix: str) -> str | None:
336
361
  return next((dim for dim in da.dims if dim == prefix or dim.startswith(f"{prefix}_")), None)
337
362
 
338
363
 
339
- def _plot_vars(ds: xr.Dataset) -> list[str]:
364
+ def _plot_vars(
365
+ ds: xr.Dataset,
366
+ data_cache: dict[str, np.ndarray] | None = None,
367
+ ) -> list[str]:
340
368
  """Return measurement variable names to plot for a timeseries dataset.
341
369
 
342
370
  Excludes auxiliary variables, percentile statistics, all-missing variables,
@@ -366,7 +394,13 @@ def _plot_vars(ds: xr.Dataset) -> list[str]:
366
394
  for d in ds[name].dims
367
395
  ):
368
396
  continue
369
- if not np.isfinite(ds[name].values).any():
397
+ if data_cache is not None and name in data_cache:
398
+ values = data_cache[name]
399
+ else:
400
+ values = ds[name].values
401
+ if data_cache is not None:
402
+ data_cache[name] = values
403
+ if not np.isfinite(values).any():
370
404
  continue
371
405
  result.append(name)
372
406
 
@@ -559,7 +593,7 @@ def _break_temporal_gaps(
559
593
 
560
594
 
561
595
  def _add_timeseries_trace(
562
- fig: go.Figure,
596
+ trace_specs: list[dict],
563
597
  *,
564
598
  x_time,
565
599
  y: np.ndarray,
@@ -603,7 +637,8 @@ def _add_timeseries_trace(
603
637
  if legendgroup is not None:
604
638
  kw["legendgroup"] = legendgroup
605
639
  kw["legendgrouptitle_text"] = legendgroup
606
- fig.add_trace(go.Scatter(**kw))
640
+ kw["type"] = "scatter"
641
+ trace_specs.append(kw)
607
642
 
608
643
 
609
644
  def _has_line_segments(
@@ -629,6 +664,9 @@ def plot_timeseries(
629
664
  var_color_offsets: dict[str, int] | None = None,
630
665
  wl_color_offsets: dict[str, int] | None = None,
631
666
  mode: str | None = None,
667
+ _data_cache: dict[str, np.ndarray] | None = None,
668
+ _hovertexts: np.ndarray | None = None,
669
+ _x_time: np.ndarray | None = None,
632
670
  ) -> go.Figure:
633
671
  """Plot timeseries variables in a single plot, grouped by variable in the legend.
634
672
 
@@ -653,7 +691,8 @@ def plot_timeseries(
653
691
  f"Dataset coordinates: {list(ds.coords)}"
654
692
  )
655
693
 
656
- plot_vars = subset_vars if subset_vars is not None else _plot_vars(ds)
694
+ data_cache = _data_cache if _data_cache is not None else {}
695
+ plot_vars = subset_vars if subset_vars is not None else _plot_vars(ds, data_cache)
657
696
  if not plot_vars:
658
697
  raise ValueError(
659
698
  "No plottable variables found (expected 'v_*' mean variables). "
@@ -661,7 +700,11 @@ def plot_timeseries(
661
700
  )
662
701
 
663
702
  # Pre-load all variable data once to avoid redundant OPeNDAP reads during sort, gap computation, and trace emission.
664
- _var_data: dict[str, np.ndarray] = {n: ds[n].values for n in plot_vars}
703
+ _var_data: dict[str, np.ndarray] = {}
704
+ for name in plot_vars:
705
+ if name not in data_cache:
706
+ data_cache[name] = ds[name].values
707
+ _var_data[name] = data_cache[name]
665
708
 
666
709
  # Detect whether all variables share the same unit; if so use flat legend style
667
710
  all_units = [_var_unit(ds, n) for n in plot_vars]
@@ -695,8 +738,8 @@ def plot_timeseries(
695
738
  for n in sorted(names, key=lambda n: 0 if n in _preferred_var else 1)
696
739
  ]
697
740
 
698
- x_time = get_plot_time(ds)
699
- fig = go.Figure()
741
+ x_time = _x_time if _x_time is not None else get_plot_time(ds)
742
+ trace_specs: list[dict] = []
700
743
 
701
744
  _iso_marker_size = 4
702
745
 
@@ -710,7 +753,9 @@ def plot_timeseries(
710
753
  "Line mode was requested/selected, but the data is too sparse or irregular "
711
754
  "to draw any connecting line -- use mode='scatter' instead."
712
755
  )
713
- hovertexts = _scatter_hovertexts(x_time, ds)
756
+ hovertexts = (
757
+ _hovertexts if _hovertexts is not None else _scatter_hovertexts(x_time, ds)
758
+ ) if use_scatter else None
714
759
 
715
760
  # Determine plot mode
716
761
  has_wavelength = any(_dimension_with_prefix(ds[n], "d_Wavelength") is not None for n in plot_vars)
@@ -742,23 +787,36 @@ def plot_timeseries(
742
787
  wavelength_dim = _dimension_with_prefix(da, "d_Wavelength")
743
788
  if wavelength_dim:
744
789
  wl_values = da.coords[wavelength_dim].values
745
- for color, wl in zip(_wl_colors(name, len(wl_values)), wl_values):
746
- y = da.sel({wavelength_dim: wl}).values.copy()
747
- _add_timeseries_trace(fig, x_time=x_time, y=y, name=f"{float(wl):.1f} nm",
748
- color=color, legendgroup=group_title,
749
- use_scatter=use_scatter, hovertexts=hovertexts,
750
- max_gap_steps=_max_gap_steps, time_step=_time_step,
751
- iso_marker_size=_iso_marker_size)
790
+ wavelength_axis = da.get_axis_num(wavelength_dim)
791
+ parameter_dims = [dim for dim in da.dims if dim not in (wavelength_dim, "time")]
792
+ parameter_indices = list(itertools.product(*(range(da.sizes[dim]) for dim in parameter_dims)))
793
+ for wavelength_index, (color, wl) in enumerate(zip(_wl_colors(name, len(wl_values)), wl_values)):
794
+ for indices in parameter_indices:
795
+ selection = [slice(None)] * da.ndim
796
+ selection[wavelength_axis] = wavelength_index
797
+ labels = [f"{float(wl):.1f} nm"]
798
+ for dim, index in zip(parameter_dims, indices):
799
+ selection[da.get_axis_num(dim)] = index
800
+ value = da.coords[dim].values[index]
801
+ label = dim.removeprefix("d_").replace("_", " ")
802
+ labels.append(f"{label}={value:g}")
803
+ y = _var_data[name][tuple(selection)].copy()
804
+ _add_timeseries_trace(trace_specs, x_time=x_time, y=y, name=", ".join(labels),
805
+ color=color, legendgroup=group_title,
806
+ use_scatter=use_scatter, hovertexts=hovertexts,
807
+ max_gap_steps=_max_gap_steps, time_step=_time_step,
808
+ iso_marker_size=_iso_marker_size)
752
809
 
753
810
  elif "d_Tower_inlet_height" in da.dims:
754
811
  base_color = _var_color(name, _CUSTOM_COLORS_VARIABLES)
755
812
  height_values = da.coords["d_Tower_inlet_height"].values
813
+ height_axis = da.get_axis_num("d_Tower_inlet_height")
756
814
  n_heights = len(height_values)
757
815
  opacities = [1.0 - (i / max(n_heights - 1, 1)) * 0.65 for i in range(n_heights)]
758
816
  for h_idx, height in enumerate(height_values):
759
817
  color = _hex_to_rgba(base_color, opacities[h_idx])
760
- y = da.sel(d_Tower_inlet_height=height).values.copy()
761
- _add_timeseries_trace(fig, x_time=x_time, y=y, name=f"h\u1d35\u2099\u1d35={height:.0f}m",
818
+ y = np.take(_var_data[name], h_idx, axis=height_axis).copy()
819
+ _add_timeseries_trace(trace_specs, x_time=x_time, y=y, name=f"h\u1d35\u2099\u1d35={height:.0f}m",
762
820
  color=color, legendgroup=group_title,
763
821
  use_scatter=use_scatter, hovertexts=hovertexts,
764
822
  max_gap_steps=_max_gap_steps, time_step=_time_step,
@@ -784,57 +842,66 @@ def plot_timeseries(
784
842
  visible = True if name in _preferred_var else "legendonly"
785
843
 
786
844
  y = _var_data[name].copy()
787
- _add_timeseries_trace(fig, x_time=x_time, y=y, name=trace_name,
845
+ _add_timeseries_trace(trace_specs, x_time=x_time, y=y, name=trace_name,
788
846
  color=color, legendgroup=lg, visible=visible,
789
847
  use_scatter=use_scatter, hovertexts=hovertexts,
790
848
  max_gap_steps=_max_gap_steps, time_step=_time_step,
791
849
  iso_marker_size=_iso_marker_size)
792
850
 
793
- n_traces = len(fig.data)
851
+ n_traces = len(trace_specs)
794
852
  legend_rows = max(1, -(-n_traces // 3)) # ceil(n_traces / 3)
795
853
  bottom_margin = max(80, 30 + legend_rows * 28)
796
854
 
797
855
  # Approximate 7px per character at font size 11; min 120px.
798
- all_labels = [t.name or "" for t in fig.data] + [
799
- t.legendgrouptitle.text for t in fig.data
800
- if getattr(t, "legendgrouptitle", None) and t.legendgrouptitle.text
856
+ all_labels = [trace.get("name", "") for trace in trace_specs] + [
857
+ trace["legendgrouptitle_text"] for trace in trace_specs
858
+ if trace.get("legendgrouptitle_text")
801
859
  ]
802
860
  entry_width = max(80, max((len(l) for l in all_labels), default=10) * 7)
803
861
 
804
- fig.update_layout(
805
- title=title if title is not None else _dataset_title(ds),
806
- xaxis_title="Time",
807
- template="plotly_white",
808
- hovermode="x unified",
809
- showlegend=True,
810
- autosize=True,
811
- height=400 + bottom_margin,
812
- legend=dict(
813
- groupclick="toggleitem",
814
- itemclick="toggle",
815
- itemdoubleclick="toggleothers",
816
- orientation="h",
817
- yanchor="top",
818
- y=-0.2,
819
- xanchor="left",
820
- x=0,
821
- font=dict(size=11),
822
- entrywidth=entry_width,
823
- entrywidthmode="pixels",
862
+ fig = go.Figure(
863
+ data=trace_specs,
864
+ layout=dict(
865
+ title=title if title is not None else _dataset_title(ds),
866
+ xaxis_title="Time",
867
+ template="plotly_white",
868
+ hovermode="x unified",
869
+ showlegend=True,
870
+ autosize=True,
871
+ height=400 + bottom_margin,
872
+ legend=dict(
873
+ groupclick="toggleitem",
874
+ itemclick="toggle",
875
+ itemdoubleclick="toggleothers",
876
+ orientation="h",
877
+ yanchor="top",
878
+ y=-0.2,
879
+ xanchor="left",
880
+ x=0,
881
+ font=dict(size=11),
882
+ entrywidth=entry_width,
883
+ entrywidthmode="pixels",
884
+ ),
885
+ margin=dict(l=60, r=20, t=50, b=bottom_margin),
886
+ modebar=dict(remove=["lasso2d", "select2d"]),
824
887
  ),
825
- margin=dict(l=60, r=20, t=50, b=bottom_margin),
826
- modebar=dict(remove=["lasso2d", "select2d"]),
827
888
  )
828
889
  return fig
829
890
 
830
891
 
831
- def plot_timeseries_per_var(ds: xr.Dataset, *, mode: str | None = None) -> list[go.Figure]:
892
+ def plot_timeseries_per_var(
893
+ ds: xr.Dataset,
894
+ *,
895
+ mode: str | None = None,
896
+ _data_cache: dict[str, np.ndarray] | None = None,
897
+ ) -> list[go.Figure]:
832
898
  """Return one figure per physical quantity, preserving combined-plot colors.
833
899
 
834
900
  Variables with the same base name but different units are grouped together.
835
901
  Falls back to a single combined figure when there is only one group.
836
902
  """
837
- plot_vars = _plot_vars(ds)
903
+ data_cache = _data_cache if _data_cache is not None else {}
904
+ plot_vars = _plot_vars(ds, data_cache)
838
905
  if not plot_vars:
839
906
  raise ValueError(
840
907
  "No plottable variables found (expected 'v_*' mean variables). "
@@ -842,7 +909,12 @@ def plot_timeseries_per_var(ds: xr.Dataset, *, mode: str | None = None) -> list[
842
909
  )
843
910
  groups = _group_vars_by_physical_quantity(ds, plot_vars)
844
911
  if len(groups) <= 1:
845
- return [plot_timeseries(ds, mode=mode)]
912
+ return [plot_timeseries(ds, mode=mode, _data_cache=data_cache)]
913
+
914
+ x_time = get_plot_time(ds)
915
+ time_step = pd.Series(pd.to_datetime(x_time)).diff().median()
916
+ use_scatter = (mode or _default_plot_mode(ds, time_step)) == "scatter"
917
+ hovertexts = _scatter_hovertexts(x_time, ds) if use_scatter else None
846
918
 
847
919
  # Walk the same sorted order as the combined plot to build per-variable color offsets.
848
920
  all_units = {_var_unit(ds, n) for n in plot_vars}
@@ -856,8 +928,8 @@ def plot_timeseries_per_var(ds: xr.Dataset, *, mode: str | None = None) -> list[
856
928
  wc_map: dict[str, int] = {}
857
929
  def _group_sort_key(group: tuple[str, list[str]]) -> tuple[int, float]:
858
930
  _, names = group
859
- best = max(names, key=lambda n: _var_mean(ds, n))
860
- return (0 if best in preferred else 1, -_var_mean(ds, best))
931
+ best = max(names, key=lambda n: _var_mean(ds, n, data_cache))
932
+ return (0 if best in preferred else 1, -_var_mean(ds, best, data_cache))
861
933
 
862
934
  ordered_vars = [
863
935
  n for _, names in sorted(groups, key=_group_sort_key)
@@ -879,7 +951,10 @@ def plot_timeseries_per_var(ds: xr.Dataset, *, mode: str | None = None) -> list[
879
951
  title=f"{lbl} at {station}" if station else lbl,
880
952
  var_color_offsets={n: vc_map[n] for n in vars_ if n in vc_map},
881
953
  wl_color_offsets={n: wc_map[n] for n in vars_ if n in wc_map},
882
- mode=mode)
954
+ mode=mode,
955
+ _data_cache=data_cache,
956
+ _hovertexts=hovertexts,
957
+ _x_time=x_time)
883
958
  for lbl, vars_ in groups
884
959
  ]
885
960
 
File without changes
File without changes
File without changes
File without changes