ebas-plot 0.2.0__py3-none-any.whl → 0.2.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ebas-plot
3
- Version: 0.2.0
3
+ Version: 0.2.2
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
@@ -0,0 +1,7 @@
1
+ ebas_plot.py,sha256=eP8j1zpbKbe1Vxqb9Q47TyAl4-7YPUwB4OBe_3SE0SA,41735
2
+ ebas_plot-0.2.2.dist-info/licenses/LICENSE,sha256=7ZpO8LOz0uwP0ZXf1VdifRWnVKK6wPAICJ9ULsrndDs,840
3
+ ebas_plot-0.2.2.dist-info/METADATA,sha256=hQK50GDnBsGlYR-uzePm3X4DMx6F6PWr0FUswc7UkPY,3758
4
+ ebas_plot-0.2.2.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
5
+ ebas_plot-0.2.2.dist-info/entry_points.txt,sha256=pOXlW0kxym2YdjkQw2HxLtHgXoaLOgQBzxZ78uVovn0,45
6
+ ebas_plot-0.2.2.dist-info/top_level.txt,sha256=EcUPwQUC64wFbj_S3eIgWmItisCrkva69sjFY-5ywd4,10
7
+ ebas_plot-0.2.2.dist-info/RECORD,,
ebas_plot.py CHANGED
@@ -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.2.0"
11
+ __version__ = "0.2.2"
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])$")
@@ -211,7 +212,9 @@ def _open_dataset(url: str, token: str | None) -> xr.Dataset:
211
212
  if token:
212
213
  session = requests.Session()
213
214
  session.headers["Authorization"] = f"Bearer {token}"
214
- return xr.open_dataset(opendap_url, engine="pydap", session=session)
215
+ # dap2:// pins the protocol (silences pydap's fallback warning); pydap maps it back to https.
216
+ dap_url = opendap_url.replace("https://", "dap2://", 1)
217
+ return xr.open_dataset(dap_url, engine="pydap", session=session)
215
218
 
216
219
  try:
217
220
  return xr.open_dataset(opendap_url, engine="netcdf4")
@@ -314,7 +317,7 @@ def _default_plot_mode(ds: xr.Dataset, time_step: pd.Timedelta) -> str:
314
317
  return "line"
315
318
 
316
319
 
317
- def _scatter_hovertexts(x_time, ds: xr.Dataset) -> list[str]:
320
+ def _scatter_hovertexts(x_time, ds: xr.Dataset) -> np.ndarray:
318
321
  """Build per-point hover strings e.g. 'Aug 2006 \u2013 Oct 2006 (3mo)' or '12 \u2013 19 Nov 2012 (1w)'."""
319
322
  if "time_bnds" in ds:
320
323
  bnds = ds["time_bnds"].values
@@ -324,6 +327,20 @@ def _scatter_hovertexts(x_time, ds: xr.Dataset) -> list[str]:
324
327
  res = ds.attrs.get("ebas_resolution_code", "")
325
328
  suffix = f" ({res})" if res else ""
326
329
 
330
+ try:
331
+ start_dates = pd.DatetimeIndex(pd.to_datetime(starts))
332
+ end_dates = pd.DatetimeIndex(pd.to_datetime(ends))
333
+ return np.asarray([
334
+ f"{start:%b %Y} \u2013 {end:%b %Y}{suffix}"
335
+ if end - start >= _MONTH_ONLY_THRESHOLD else
336
+ f"{start:%-d} \u2013 {end:%-d %b %Y}{suffix}"
337
+ if start.month == end.month and start.year == end.year else
338
+ f"{start:%-d %b} \u2013 {end:%-d %b %Y}{suffix}"
339
+ for start, end in zip(start_dates, end_dates)
340
+ ], dtype=object)
341
+ except (TypeError, ValueError):
342
+ pass
343
+
327
344
  def _fmt(s, e) -> str:
328
345
  start, end = pd.Timestamp(s), pd.Timestamp(e)
329
346
  if end - start >= _MONTH_ONLY_THRESHOLD: # coarse/campaign samples
@@ -333,7 +350,7 @@ def _scatter_hovertexts(x_time, ds: xr.Dataset) -> list[str]:
333
350
  start_fmt = f"{start:%-d}" if same_month else f"{start:%-d %b}"
334
351
  return f"{start_fmt} \u2013 {end:%-d %b %Y}{suffix}"
335
352
 
336
- return [_fmt(s, e) for s, e in zip(starts, ends)]
353
+ return np.asarray([_fmt(s, e) for s, e in zip(starts, ends)], dtype=object)
337
354
 
338
355
 
339
356
  def _pnsd_var(ds: xr.Dataset) -> str | None:
@@ -578,7 +595,7 @@ def _break_temporal_gaps(
578
595
 
579
596
 
580
597
  def _add_timeseries_trace(
581
- fig: go.Figure,
598
+ trace_specs: list[dict],
582
599
  *,
583
600
  x_time,
584
601
  y: np.ndarray,
@@ -622,7 +639,8 @@ def _add_timeseries_trace(
622
639
  if legendgroup is not None:
623
640
  kw["legendgroup"] = legendgroup
624
641
  kw["legendgrouptitle_text"] = legendgroup
625
- fig.add_trace(go.Scatter(**kw))
642
+ kw["type"] = "scatter"
643
+ trace_specs.append(kw)
626
644
 
627
645
 
628
646
  def _has_line_segments(
@@ -649,6 +667,8 @@ def plot_timeseries(
649
667
  wl_color_offsets: dict[str, int] | None = None,
650
668
  mode: str | None = None,
651
669
  _data_cache: dict[str, np.ndarray] | None = None,
670
+ _hovertexts: np.ndarray | None = None,
671
+ _x_time: np.ndarray | None = None,
652
672
  ) -> go.Figure:
653
673
  """Plot timeseries variables in a single plot, grouped by variable in the legend.
654
674
 
@@ -720,8 +740,8 @@ def plot_timeseries(
720
740
  for n in sorted(names, key=lambda n: 0 if n in _preferred_var else 1)
721
741
  ]
722
742
 
723
- x_time = get_plot_time(ds)
724
- fig = go.Figure()
743
+ x_time = _x_time if _x_time is not None else get_plot_time(ds)
744
+ trace_specs: list[dict] = []
725
745
 
726
746
  _iso_marker_size = 4
727
747
 
@@ -735,7 +755,9 @@ def plot_timeseries(
735
755
  "Line mode was requested/selected, but the data is too sparse or irregular "
736
756
  "to draw any connecting line -- use mode='scatter' instead."
737
757
  )
738
- hovertexts = _scatter_hovertexts(x_time, ds)
758
+ hovertexts = (
759
+ _hovertexts if _hovertexts is not None else _scatter_hovertexts(x_time, ds)
760
+ ) if use_scatter else None
739
761
 
740
762
  # Determine plot mode
741
763
  has_wavelength = any(_dimension_with_prefix(ds[n], "d_Wavelength") is not None for n in plot_vars)
@@ -768,13 +790,24 @@ def plot_timeseries(
768
790
  if wavelength_dim:
769
791
  wl_values = da.coords[wavelength_dim].values
770
792
  wavelength_axis = da.get_axis_num(wavelength_dim)
793
+ parameter_dims = [dim for dim in da.dims if dim not in (wavelength_dim, "time")]
794
+ parameter_indices = list(itertools.product(*(range(da.sizes[dim]) for dim in parameter_dims)))
771
795
  for wavelength_index, (color, wl) in enumerate(zip(_wl_colors(name, len(wl_values)), wl_values)):
772
- y = np.take(_var_data[name], wavelength_index, axis=wavelength_axis).copy()
773
- _add_timeseries_trace(fig, x_time=x_time, y=y, name=f"{float(wl):.1f} nm",
774
- color=color, legendgroup=group_title,
775
- use_scatter=use_scatter, hovertexts=hovertexts,
776
- max_gap_steps=_max_gap_steps, time_step=_time_step,
777
- iso_marker_size=_iso_marker_size)
796
+ for indices in parameter_indices:
797
+ selection = [slice(None)] * da.ndim
798
+ selection[wavelength_axis] = wavelength_index
799
+ labels = [f"{float(wl):.1f} nm"]
800
+ for dim, index in zip(parameter_dims, indices):
801
+ selection[da.get_axis_num(dim)] = index
802
+ value = da.coords[dim].values[index]
803
+ label = dim.removeprefix("d_").replace("_", " ")
804
+ labels.append(f"{label}={value:g}")
805
+ y = _var_data[name][tuple(selection)].copy()
806
+ _add_timeseries_trace(trace_specs, x_time=x_time, y=y, name=", ".join(labels),
807
+ color=color, legendgroup=group_title,
808
+ use_scatter=use_scatter, hovertexts=hovertexts,
809
+ max_gap_steps=_max_gap_steps, time_step=_time_step,
810
+ iso_marker_size=_iso_marker_size)
778
811
 
779
812
  elif "d_Tower_inlet_height" in da.dims:
780
813
  base_color = _var_color(name, _CUSTOM_COLORS_VARIABLES)
@@ -785,7 +818,7 @@ def plot_timeseries(
785
818
  for h_idx, height in enumerate(height_values):
786
819
  color = _hex_to_rgba(base_color, opacities[h_idx])
787
820
  y = np.take(_var_data[name], h_idx, axis=height_axis).copy()
788
- _add_timeseries_trace(fig, x_time=x_time, y=y, name=f"h\u1d35\u2099\u1d35={height:.0f}m",
821
+ _add_timeseries_trace(trace_specs, x_time=x_time, y=y, name=f"h\u1d35\u2099\u1d35={height:.0f}m",
789
822
  color=color, legendgroup=group_title,
790
823
  use_scatter=use_scatter, hovertexts=hovertexts,
791
824
  max_gap_steps=_max_gap_steps, time_step=_time_step,
@@ -811,46 +844,49 @@ def plot_timeseries(
811
844
  visible = True if name in _preferred_var else "legendonly"
812
845
 
813
846
  y = _var_data[name].copy()
814
- _add_timeseries_trace(fig, x_time=x_time, y=y, name=trace_name,
847
+ _add_timeseries_trace(trace_specs, x_time=x_time, y=y, name=trace_name,
815
848
  color=color, legendgroup=lg, visible=visible,
816
849
  use_scatter=use_scatter, hovertexts=hovertexts,
817
850
  max_gap_steps=_max_gap_steps, time_step=_time_step,
818
851
  iso_marker_size=_iso_marker_size)
819
852
 
820
- n_traces = len(fig.data)
853
+ n_traces = len(trace_specs)
821
854
  legend_rows = max(1, -(-n_traces // 3)) # ceil(n_traces / 3)
822
855
  bottom_margin = max(80, 30 + legend_rows * 28)
823
856
 
824
857
  # Approximate 7px per character at font size 11; min 120px.
825
- all_labels = [t.name or "" for t in fig.data] + [
826
- t.legendgrouptitle.text for t in fig.data
827
- if getattr(t, "legendgrouptitle", None) and t.legendgrouptitle.text
858
+ all_labels = [trace.get("name", "") for trace in trace_specs] + [
859
+ trace["legendgrouptitle_text"] for trace in trace_specs
860
+ if trace.get("legendgrouptitle_text")
828
861
  ]
829
862
  entry_width = max(80, max((len(l) for l in all_labels), default=10) * 7)
830
863
 
831
- fig.update_layout(
832
- title=title if title is not None else _dataset_title(ds),
833
- xaxis_title="Time",
834
- template="plotly_white",
835
- hovermode="x unified",
836
- showlegend=True,
837
- autosize=True,
838
- height=400 + bottom_margin,
839
- legend=dict(
840
- groupclick="toggleitem",
841
- itemclick="toggle",
842
- itemdoubleclick="toggleothers",
843
- orientation="h",
844
- yanchor="top",
845
- y=-0.2,
846
- xanchor="left",
847
- x=0,
848
- font=dict(size=11),
849
- entrywidth=entry_width,
850
- entrywidthmode="pixels",
864
+ fig = go.Figure(
865
+ data=trace_specs,
866
+ layout=dict(
867
+ title=title if title is not None else _dataset_title(ds),
868
+ xaxis_title="Time",
869
+ template="plotly_white",
870
+ hovermode="x unified",
871
+ showlegend=True,
872
+ autosize=True,
873
+ height=400 + bottom_margin,
874
+ legend=dict(
875
+ groupclick="toggleitem",
876
+ itemclick="toggle",
877
+ itemdoubleclick="toggleothers",
878
+ orientation="h",
879
+ yanchor="top",
880
+ y=-0.2,
881
+ xanchor="left",
882
+ x=0,
883
+ font=dict(size=11),
884
+ entrywidth=entry_width,
885
+ entrywidthmode="pixels",
886
+ ),
887
+ margin=dict(l=60, r=20, t=50, b=bottom_margin),
888
+ modebar=dict(remove=["lasso2d", "select2d"]),
851
889
  ),
852
- margin=dict(l=60, r=20, t=50, b=bottom_margin),
853
- modebar=dict(remove=["lasso2d", "select2d"]),
854
890
  )
855
891
  return fig
856
892
 
@@ -877,6 +913,11 @@ def plot_timeseries_per_var(
877
913
  if len(groups) <= 1:
878
914
  return [plot_timeseries(ds, mode=mode, _data_cache=data_cache)]
879
915
 
916
+ x_time = get_plot_time(ds)
917
+ time_step = pd.Series(pd.to_datetime(x_time)).diff().median()
918
+ use_scatter = (mode or _default_plot_mode(ds, time_step)) == "scatter"
919
+ hovertexts = _scatter_hovertexts(x_time, ds) if use_scatter else None
920
+
880
921
  # Walk the same sorted order as the combined plot to build per-variable color offsets.
881
922
  all_units = {_var_unit(ds, n) for n in plot_vars}
882
923
  preferred = (
@@ -913,7 +954,9 @@ def plot_timeseries_per_var(
913
954
  var_color_offsets={n: vc_map[n] for n in vars_ if n in vc_map},
914
955
  wl_color_offsets={n: wc_map[n] for n in vars_ if n in wc_map},
915
956
  mode=mode,
916
- _data_cache=data_cache)
957
+ _data_cache=data_cache,
958
+ _hovertexts=hovertexts,
959
+ _x_time=x_time)
917
960
  for lbl, vars_ in groups
918
961
  ]
919
962
 
@@ -1,7 +0,0 @@
1
- ebas_plot.py,sha256=qnOqFyAIQRNkqkegt8-ho5eOSq9irT4cCUnm4FWrTCU,39438
2
- ebas_plot-0.2.0.dist-info/licenses/LICENSE,sha256=7ZpO8LOz0uwP0ZXf1VdifRWnVKK6wPAICJ9ULsrndDs,840
3
- ebas_plot-0.2.0.dist-info/METADATA,sha256=hrp3ldBZv1gabawrjqPFHBp8z74L3WUN37qtToR3wYk,3758
4
- ebas_plot-0.2.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
5
- ebas_plot-0.2.0.dist-info/entry_points.txt,sha256=pOXlW0kxym2YdjkQw2HxLtHgXoaLOgQBzxZ78uVovn0,45
6
- ebas_plot-0.2.0.dist-info/top_level.txt,sha256=EcUPwQUC64wFbj_S3eIgWmItisCrkva69sjFY-5ywd4,10
7
- ebas_plot-0.2.0.dist-info/RECORD,,