|
4 | 4 | import holoviews as hv |
5 | 5 |
|
6 | 6 | hv.extension("bokeh") |
| 7 | +from dvue.catalog import DataReferenceReader, DataReference, DataCatalog |
7 | 8 | from dvue.dataui import DataUI |
8 | 9 | from dvue.tsdataui import TimeSeriesDataUIManager |
9 | 10 | from dvue import utils |
|
15 | 16 | logger = logging.getLogger(__name__) |
16 | 17 |
|
17 | 18 |
|
| 19 | +class SchismDataReferenceReader(DataReferenceReader): |
| 20 | + """Reads a single SCHISM time series from a study output file or the |
| 21 | + observation datastore. |
| 22 | +
|
| 23 | + ``source == "datastore"`` rows are fetched via the observation datastore; |
| 24 | + all other rows are fetched from the matching :class:`SchismStudy` output |
| 25 | + files. ``convert_units`` is checked on the owning manager at every |
| 26 | + :meth:`load` call. Caching is intentionally disabled on all |
| 27 | + :class:`~dvue.catalog.DataReference` objects using this reader so that |
| 28 | + toggling ``convert_units`` is always reflected immediately. |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + study_dir_map : dict |
| 33 | + Mapping of ``str(output_dir)`` → :class:`SchismStudy`. |
| 34 | + obs_datastore : StationDatastore or None |
| 35 | + Observation datastore for ``source == "datastore"`` entries. |
| 36 | + manager : SchismOutputUIDataManager |
| 37 | + Owning manager; provides the reactive ``convert_units`` flag. |
| 38 | + """ |
| 39 | + |
| 40 | + def __init__(self, study_dir_map, obs_datastore, manager): |
| 41 | + self._study_dir_map = study_dir_map |
| 42 | + self._datastore = obs_datastore |
| 43 | + self._manager = manager |
| 44 | + |
| 45 | + def load(self, **attributes) -> pd.DataFrame: |
| 46 | + source = attributes.get("source", "") |
| 47 | + unit = attributes.get("unit", "") |
| 48 | + if source == "datastore": |
| 49 | + df = self._datastore.get_data(attributes) |
| 50 | + if self._manager.convert_units: |
| 51 | + df, unit = schismstudy.convert_to_SI(df, unit) |
| 52 | + else: |
| 53 | + base_dir = str(pathlib.Path(attributes["filename"]).parent) |
| 54 | + study = self._study_dir_map[base_dir] |
| 55 | + try: |
| 56 | + df = study.get_data(attributes) |
| 57 | + except KeyError as e: |
| 58 | + logger.warning(str(e).strip("'\"")) |
| 59 | + raise |
| 60 | + df = df[slice(df.first_valid_index(), df.last_valid_index())] |
| 61 | + df.attrs["unit"] = unit |
| 62 | + df.attrs["ptype"] = "INST-VAL" |
| 63 | + return df |
| 64 | + |
| 65 | + def __repr__(self) -> str: |
| 66 | + return f"SchismDataReferenceReader(sources={list(self._study_dir_map.keys())!r})" |
| 67 | + |
| 68 | + |
18 | 69 | class SchismOutputUIDataManager(TimeSeriesDataUIManager): |
19 | 70 |
|
20 | 71 | convert_units = param.Boolean(default=True, doc="Convert units to SI") |
@@ -43,7 +94,16 @@ def __init__(self, *studies, datastore=None, time_range=None, **kwargs): |
43 | 94 | self.color_cycle_column = "id" |
44 | 95 | self.dashed_line_cycle_column = "source" |
45 | 96 | self.marker_cycle_column = "variable" |
46 | | - |
| 97 | + # Build dvue catalog after param is fully initialized |
| 98 | + self._schism_reader = SchismDataReferenceReader( |
| 99 | + self.study_dir_map, self.datastore, self |
| 100 | + ) |
| 101 | + geo_crs = ( |
| 102 | + str(self.catalog.crs) |
| 103 | + if hasattr(self.catalog, "crs") and self.catalog.crs is not None |
| 104 | + else None |
| 105 | + ) |
| 106 | + self._dvue_catalog = self._build_dvue_catalog(geo_crs) |
47 | 107 |
|
48 | 108 | def get_widgets(self): |
49 | 109 | control_widgets = super().get_widgets() |
@@ -73,6 +133,29 @@ def _convert_to_study_format(self, df): |
73 | 133 | def get_data_catalog(self): |
74 | 134 | return self.catalog |
75 | 135 |
|
| 136 | + @property |
| 137 | + def data_catalog(self) -> DataCatalog: |
| 138 | + return self._dvue_catalog |
| 139 | + |
| 140 | + def _build_dvue_catalog(self, crs=None) -> DataCatalog: |
| 141 | + catalog = DataCatalog(crs=crs) |
| 142 | + for _, row in self.catalog.iterrows(): |
| 143 | + ref_name = f"{row['source']}::{row['id']}/{row['variable']}" |
| 144 | + attrs = {k: v for k, v in row.items() if k != "geometry"} |
| 145 | + attrs.pop("name", None) # avoid clash with the explicit name= kwarg below |
| 146 | + if "geometry" in row.index and row["geometry"] is not None: |
| 147 | + attrs["geometry"] = row["geometry"] |
| 148 | + try: |
| 149 | + catalog.add(DataReference( |
| 150 | + self._schism_reader, |
| 151 | + name=ref_name, |
| 152 | + cache=False, # convert_units is reactive; always re-run |
| 153 | + **attrs, |
| 154 | + )) |
| 155 | + except ValueError: |
| 156 | + pass # duplicate name; skip |
| 157 | + return catalog |
| 158 | + |
76 | 159 | def get_time_range(self, dfcat): |
77 | 160 | return self.time_range |
78 | 161 |
|
@@ -141,21 +224,11 @@ def is_irregular(self, r): |
141 | 224 | return False |
142 | 225 |
|
143 | 226 | def get_data_for_time_range(self, r, time_range): |
144 | | - unit = r["unit"] |
145 | | - if r["source"] == "datastore": |
146 | | - df = self.datastore.get_data(r) |
147 | | - if self.convert_units: |
148 | | - df, unit = schismstudy.convert_to_SI(df, r["unit"]) |
149 | | - else: |
150 | | - base_dir = str(pathlib.Path(r["filename"]).parent) |
151 | | - study = self.study_dir_map[base_dir] |
152 | | - try: |
153 | | - df = study.get_data(r) |
154 | | - except KeyError as e: |
155 | | - logger.warning(str(e).strip('"\'')) |
156 | | - raise |
157 | | - ptype = "INST-VAL" |
158 | | - df = df[slice(df.first_valid_index(), df.last_valid_index())] |
| 227 | + ref_name = f"{r['source']}::{r['id']}/{r['variable']}" |
| 228 | + ref = self._dvue_catalog.get(ref_name) |
| 229 | + df = ref.getData(time_range=time_range) |
| 230 | + unit = df.attrs.get("unit", r["unit"]) |
| 231 | + ptype = df.attrs.get("ptype", "INST-VAL") |
159 | 232 | return df, unit, ptype |
160 | 233 |
|
161 | 234 | # methods below if geolocation data is available |
|
0 commit comments