ebas-plot 0.2.2__tar.gz → 0.2.4__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.2.2
3
+ Version: 0.2.4
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.2.2
3
+ Version: 0.2.4
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
@@ -8,7 +8,7 @@ import requests
8
8
  from netCDF4 import date2num, num2date
9
9
  import pandas as pd
10
10
 
11
- __version__ = "0.2.2"
11
+ __version__ = "0.2.4"
12
12
 
13
13
 
14
14
  class DatasetAccessError(Exception):
@@ -182,6 +182,10 @@ def open_dataset(
182
182
  return _slice_time(ds, *bounds) if any(bounds) else ds
183
183
 
184
184
 
185
+ # Known hosts only: the user's Bearer token is forwarded to the dataset host.
186
+ _THREDDS_HOSTS = ("https://thredds.nilu.no", "https://dev-thredds.nilu.no")
187
+
188
+
185
189
  def _open_dataset(url: str, token: str | None) -> xr.Dataset:
186
190
  if not url.startswith("http"): # local file
187
191
  ds = validate_dataset(url)
@@ -190,9 +194,9 @@ def _open_dataset(url: str, token: str | None) -> xr.Dataset:
190
194
  return ds
191
195
  elif url.startswith("https://doi.nilu.no/doi/") or url.startswith("https://doi.org/"): # DOI link
192
196
  opendap_url = _doi_to_opendap_url(url.split("/")[-1])
193
- elif url.startswith("https://thredds.nilu.no/thredds/fileServer/ebas_doi/"): # Download link, convert to OPeNDAP
197
+ elif any(url.startswith(f"{h}/thredds/fileServer/ebas_doi/") for h in _THREDDS_HOSTS): # Download link, convert to OPeNDAP
194
198
  opendap_url = url.replace("fileServer", "dodsC")
195
- elif url.startswith("https://thredds.nilu.no/thredds/dodsC/ebas_doi/"): # direct OPeNDAP link
199
+ elif any(url.startswith(f"{h}/thredds/dodsC/ebas_doi/") for h in _THREDDS_HOSTS): # direct OPeNDAP link
196
200
  opendap_url = url
197
201
  else:
198
202
  raise ValueError(f"Unsupported dataset URL: {url}")
@@ -447,6 +451,38 @@ def _group_vars_by_physical_quantity(ds: xr.Dataset, plot_vars: list[str]) -> li
447
451
  return list(groups.items())
448
452
 
449
453
 
454
+ def _fill_pnsd_time_gaps(
455
+ x_time: np.ndarray, z: np.ndarray, time_step: pd.Timedelta,
456
+ ) -> tuple[np.ndarray, np.ndarray]:
457
+ """Insert all-NaN columns for absent time steps without moving observations."""
458
+ if len(x_time) < 2 or pd.isna(time_step) or time_step <= pd.Timedelta(0):
459
+ return x_time, z
460
+ timestamps = pd.DatetimeIndex(x_time).as_unit("ns")
461
+ intervals = np.diff(timestamps.asi8)
462
+ gap_indices = np.flatnonzero(intervals > 1.5 * time_step.value) + 1
463
+ if not len(gap_indices):
464
+ return x_time, z
465
+ missing_counts = (intervals[gap_indices - 1] - (time_step / 2).value) // time_step.value
466
+ cumulative_missing = np.cumsum(missing_counts)
467
+ total_missing = int(cumulative_missing[-1])
468
+ offsets = np.arange(total_missing) - np.repeat(
469
+ cumulative_missing - missing_counts, missing_counts
470
+ )
471
+ shifts = np.zeros(len(x_time), dtype=np.int64)
472
+ shifts[gap_indices] = missing_counts
473
+ observed_positions = np.arange(len(x_time)) + np.cumsum(shifts)
474
+ missing_positions = np.repeat(observed_positions[gap_indices - 1] + 1, missing_counts) + offsets
475
+ filled_times = np.empty(len(x_time) + total_missing, dtype="datetime64[ns]")
476
+ filled_times[observed_positions] = timestamps.to_numpy()
477
+ filled_times[missing_positions] = (
478
+ np.repeat(timestamps.asi8[gap_indices - 1], missing_counts)
479
+ + (offsets + 1) * time_step.value
480
+ ).astype("datetime64[ns]")
481
+ filled_values = np.full((z.shape[0], len(filled_times)), np.nan, dtype=z.dtype)
482
+ filled_values[:, observed_positions] = z
483
+ return filled_times, filled_values
484
+
485
+
450
486
  def plot_pnsd(ds: xr.Dataset) -> go.Figure:
451
487
  """Plot particle number size distribution (PNSD) as a heatmap."""
452
488
  var_name = _pnsd_var(ds)
@@ -467,6 +503,9 @@ def plot_pnsd(ds: xr.Dataset) -> go.Figure:
467
503
  z = np.log10(z_raw)
468
504
  x_time = get_plot_time(ds)
469
505
  y_d = ds["d_D"].values
506
+ if not np.issubdtype(np.asarray(x_time).dtype, np.number):
507
+ time_step = pd.Series(pd.to_datetime(x_time)).diff().median()
508
+ x_time, z = _fill_pnsd_time_gaps(x_time, z, _sampling_resolution(ds, time_step))
470
509
 
471
510
  # Fixed logarithmic scale from -1.0 to 5.0 with 0.2 interval; APS instruments cap lower (max 3.0).
472
511
  log_min = -1.0
@@ -489,6 +528,7 @@ def plot_pnsd(ds: xr.Dataset) -> go.Figure:
489
528
  zmin=log_min,
490
529
  zmax=log_max,
491
530
  zsmooth='best',
531
+ connectgaps=False,
492
532
  colorscale=plotly_discrete_colorscale,
493
533
  showscale=True,
494
534
  colorbar=dict(
File without changes
File without changes
File without changes
File without changes