|
| 1 | +from vtools.functions.interpolate import rhistinterp |
| 2 | +from vtools.data.vtime import days, minutes |
| 3 | +from pyhecdss import get_ts, DSSFile |
| 4 | +from pathlib import Path |
| 5 | +import pandas as pd |
| 6 | +import os |
| 7 | + |
| 8 | + |
| 9 | +dss_e2_freq = {"1HOUR": "H", "1DAY": "D", "1MON": "M"} |
| 10 | + |
| 11 | + |
| 12 | +def read_dss( |
| 13 | + filename, |
| 14 | + pathname, |
| 15 | + dt=minutes(15), |
| 16 | + p=2.0, |
| 17 | + start_date=None, |
| 18 | + end_date=None, |
| 19 | +): |
| 20 | + """ |
| 21 | + Reads in a DSM2 dss file and interpolates |
| 22 | + Outputs an interpolated DataFrame of that variable |
| 23 | +
|
| 24 | + Parameters |
| 25 | + ---------- |
| 26 | + filename: str|Path |
| 27 | + Path to the DSS file to read |
| 28 | + pathname: str |
| 29 | + Pathname within the DSS file to read. |
| 30 | + Needs to be in the format '/A_PART/B_PART/C_PART/D_PART/E_PART/F_PART/' |
| 31 | + (e.g. '//RSAN112/FLOW////') |
| 32 | + """ |
| 33 | + ts_out_list = [] |
| 34 | + col_names = [] |
| 35 | + print(f"\t{pathname}") |
| 36 | + if len(pathname.split("/")[1:-1]) != 6: |
| 37 | + raise ValueError(f"Invalid DSS pathname: {pathname}, needs 6 parts (A-F)") |
| 38 | + ts = get_ts(filename, pathname) |
| 39 | + for i, tsi in enumerate(ts): |
| 40 | + ts_path = tsi[0].columns.values[0] |
| 41 | + path_lst = (ts_path).split("/") |
| 42 | + path_e = path_lst[5] |
| 43 | + # Set default start_date and end_date to cover the full period of record if not specified |
| 44 | + tt_full = tsi[0] |
| 45 | + if start_date is None: |
| 46 | + start_date = tt_full.index[0] |
| 47 | + if end_date is None: |
| 48 | + end_date = tt_full.index[-1] |
| 49 | + tt = tt_full[start_date:end_date] |
| 50 | + pidx = pd.period_range(start_date, tt.index[-1], freq=dss_e2_freq[path_e]) |
| 51 | + ptt = pd.DataFrame(tt.values[:, 0], pidx) |
| 52 | + |
| 53 | + # Interpolate with rhistinterp |
| 54 | + if p != 0: |
| 55 | + col_data = rhistinterp(ptt, dt, p=p) |
| 56 | + elif p == 0: |
| 57 | + col_data = rhistinterp(ptt, dt) |
| 58 | + else: |
| 59 | + col_data = tsi[0] |
| 60 | + ts_out_list.append(col_data) |
| 61 | + col_names.append(ts_path) |
| 62 | + |
| 63 | + if ts_out_list: |
| 64 | + ts_out = pd.concat(ts_out_list, axis=1) |
| 65 | + ts_out.columns = col_names |
| 66 | + ts_out = ts_out.copy() # Defragment the DataFrame |
| 67 | + else: |
| 68 | + with DSSFile(filename) as dssh: |
| 69 | + dfcat = dssh.read_catalog() |
| 70 | + raise ValueError( |
| 71 | + f"Warning: DSS data not found for {pathname}. Preview of available paths in {filename} are: {dfcat}" |
| 72 | + ) |
| 73 | + |
| 74 | + return ts_out |
0 commit comments