Skip to content

Commit 0e01099

Browse files
fix interp for more robust
1 parent eaab037 commit 0e01099

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

bdschism/bdschism/port_boundary.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ def read_csv(file, var, name, dt, p=2.0, interp=True, freq="M"):
3737
Outputs an interpolated DataFrame of that variable
3838
"""
3939
forecast_df = pd.read_csv(file, index_col=0, header=0, parse_dates=True)
40+
forecast_df.columns = forecast_df.columns.astype(str).str.strip()
4041
forecast_df.index = forecast_df.index.to_period(freq)
42+
if forecast_df.index.has_duplicates:
43+
forecast_df = forecast_df.groupby(forecast_df.index).mean()
4144

4245
# Check for % sign and convert to fraction
4346
if forecast_df[var].dtype == "object": # Ensure it's a string column
@@ -48,16 +51,13 @@ def read_csv(file, var, name, dt, p=2.0, interp=True, freq="M"):
4851
else:
4952
forecast_df[var] = forecast_df[var].astype("float")
5053

54+
forecast_df[var] = forecast_df[var].astype("float")
5155
if interp:
52-
interp_series = rhistinterp(forecast_df[var].astype("float"), dt, p=p)
56+
interp_df = rhistinterp(forecast_df[[var]], dt, p=float(p))
5357
else:
5458
forecast_df.index = forecast_df.index.to_timestamp()
55-
interp_series = forecast_df[var].resample(dt).ffill()
56-
interp_df = pd.DataFrame()
57-
if name is not None:
58-
interp_df[[name]] = pd.DataFrame({var: interp_series})
59-
else:
60-
interp_df[[var]] = pd.DataFrame({var: interp_series})
59+
interp_df = forecast_df[[var]].resample(dt).ffill()
60+
6161
return interp_df
6262

6363

@@ -223,6 +223,11 @@ def create_schism_bc(config_yaml, plot=False, kwargs=None):
223223
schism_temp_file = config["file"]["schism_temp_file"]
224224
out_file_suffix = config["file"]["out_file_suffix"]
225225
boundary_kinds = config["param"]["boundary_kinds"]
226+
if isinstance(boundary_kinds, str):
227+
text = boundary_kinds.strip()
228+
if text.startswith("[") and text.endswith("]"):
229+
text = text[1:-1]
230+
boundary_kinds = [item.strip() for item in text.split(",") if item.strip()]
226231
sd = config["param"]["start_date"]
227232
ed = config["param"]["end_date"]
228233

@@ -352,6 +357,9 @@ def create_schism_bc(config_yaml, plot=False, kwargs=None):
352357
if boundary_kind in gate_names:
353358
if source_kind == "CSV":
354359
var_df = read_csv(source_file, var, None, dt, p=p, interp=interp)
360+
elif source_kind == "TH":
361+
var_df = read_th(source_file)
362+
var_df = var_df[[var]]
355363
elif source_kind == "DSS":
356364
var_df = pd.DataFrame()
357365
b = var.split("/")[2]
@@ -364,10 +372,10 @@ def create_schism_bc(config_yaml, plot=False, kwargs=None):
364372
var_df = pd.DataFrame({name: [float(var)] * len(df_rng)})
365373
var_df.index = df_rng
366374
else:
367-
raise ValueError(f"source_kind={source_kind} is not yet supported")
375+
raise ValueError(f"source_kind={source_kind} is not yet supported for gates")
368376

369377
# use formula to set gate fraction (month_fraction, month_days)
370-
if source_kind == "CONSTANT":
378+
if source_kind in ["CSV", "TH", "CONSTANT"]:
371379
dfi = var_df
372380
else:
373381
dfi = set_gate_ops(boundary_kind, var_df.ffill(), name, formula)
@@ -497,16 +505,15 @@ def create_schism_bc(config_yaml, plot=False, kwargs=None):
497505
print(
498506
"Unit conversion unecessary for consumptive use (handled in yaml)"
499507
)
508+
elif source_kind in ["SCHISM", "TH"]:
509+
# Simplest case: use existing reference SCHISM data
510+
print("Use existing SCHISM input, no unit conversion necessary")
500511
else:
501-
if source_kind == "SCHISM":
502-
# Simplest case: use existing reference SCHISM data
503-
print("Use existing SCHISM input, no unit conversion necessary")
504-
else:
505-
# Do conversions.
506-
if convert == "CFS_CMS":
507-
dfi = dfi * CFS2CMS * sign
508-
elif convert == "EC_PSU":
509-
dfi = ec_psu_25c(dfi) * sign
512+
# Do conversions.
513+
if convert == "CFS_CMS":
514+
dfi = dfi * CFS2CMS * int(sign)
515+
elif convert == "EC_PSU":
516+
dfi = ec_psu_25c(dfi) * int(sign)
510517

511518
# Trim dfi so that it starts where schism input file ends, so that dfi doesn't
512519
# overwrite any historical data

0 commit comments

Comments
 (0)