Skip to content

Commit

Permalink
Merge github.com:timcera/mettoolbox
Browse files Browse the repository at this point in the history
  • Loading branch information
timcera committed Aug 27, 2022
2 parents 1cbe18e + 747b2fa commit 6dadf2f
Show file tree
Hide file tree
Showing 4 changed files with 2,854 additions and 25 deletions.
104 changes: 89 additions & 15 deletions src/mettoolbox/disaggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,12 @@ def humidity(
hum_mean_col: Optional[Union[tsutils.IntGreaterEqualToOne, str, pd.Series]] = None,
temp_min_col: Optional[Union[tsutils.IntGreaterEqualToOne, str, pd.Series]] = None,
temp_max_col: Optional[Union[tsutils.IntGreaterEqualToOne, str, pd.Series]] = None,
precip_col: Optional[Union[tsutils.IntGreaterEqualToOne, str, pd.Series]] = None,
a0=None,
a1=None,
kr=None,
hourly_temp=None,
hourly_precip_hum=None,
preserve_daily_mean=None,
):
"""Disaggregate daily humidity to hourly humidity data."""
Expand All @@ -294,6 +296,15 @@ def humidity(
)
)

if method == "month_hour_precip_mean" and precip_col is None:
raise ValueError(
tsutils.error_wrapper(
"""
If `method` is "month_hour_precip_mean" then the daily precip is a required column
identified with the keyword `precip_col`"""
)
)

if (
method in ["minimal", "dewpoint_regression", "linear_dewpoint_variation"]
and temp_min_col is None
Expand Down Expand Up @@ -359,7 +370,6 @@ def humidity(
"dewpoint_regression",
"linear_dewpoint_variation",
"min_max",
"month_hour_precip_mean",
]
and hourly_temp is None
):
Expand All @@ -374,6 +384,64 @@ def humidity(

pd.options.display.width = 60

columns = []
if method == "equal":
try:
hum_mean_col = int(hum_mean_col)
except TypeError:
pass
columns.append(hum_mean_col)

if method == "min_max":
try:
temp_min_col = int(temp_min_col)
except TypeError:
pass
columns.append(temp_min_col)
try:
temp_max_col = int(temp_max_col)
except TypeError:
pass
columns.append(temp_max_col)
try:
hum_min_col = int(hum_min_col)
except TypeError:
pass
columns.append(hum_min_col)
try:
hum_max_col = int(hum_max_col)
except TypeError:
pass
columns.append(hum_max_col)

if method in ["minimal", "dewpoint_regression", "linear_dewpoint_variation"]:
try:
temp_min_col = int(temp_min_col)
except TypeError:
pass
columns.append(temp_min_col)

if method == "month_hour_precip_mean":
try:
precip_col = int(precip_col)
except TypeError:
pass
columns.append(precip_col)

if preserve_daily_mean is not None:
if method in [
"minimal",
"dewpoint_regression",
"linear_dewpoint_variation",
"min_max",
"month_hour_precip_mean",
]:
try:
hum_mean_col = int(preserve_daily_mean)
except TypeError:
pass
columns.append(hum_mean_col)

tsd = tsutils.common_kwds(
tsutils.read_iso_ts(
input_ts, skiprows=skiprows, names=names, index_type=index_type
Expand All @@ -389,36 +457,42 @@ def humidity(
)

if method == "equal":
tsd["hum"] = tsd["hum_mean_col"]

if method in ["minimal", "dewpoint_regression", "linear_dewpoint_variation"]:
tsd["tmin"] = tsd["temp_min_col"]

if method == "min_max":
tsd["hum_min"] = tsd["hum_min_col"]
tsd["hum_max"] = tsd["hum_max_col"]
tsd["tmin"] = tsd["temp_min_col"]
tsd["tmax"] = tsd["temp_max_col"]
tsd.columns = ["hum"]

if preserve_daily_mean is not None:
tsd["hum"] = tsd[preserve_daily_mean]
if method in ["minimal", "dewpoint_regression", "linear_dewpoint_variation"]:
tsd.columns = ["tmin", "hum"]
if method == "month_hour_precip_mean":
tsd.columns = ["precip", "hum"]
if method == "min_max":
tsd.columns = ["tmin", "tmax", "hum_min", "hum_max", "hum"]
preserve_daily_mean = True
else:
if method in ["minimal", "dewpoint_regression", "linear_dewpoint_variation"]:
tsd.columns = ["tmin"]
if method == "month_hour_precip_mean":
tsd.columns = ["precip"]
if method == "min_max":
tsd.columns = ["tmin", "tmax", "hum_min", "hum_max"]

if method in [
"minimal",
"dewpoint_regression",
"linear_dewpoint_variation",
"min_max",
"month_hour_precip_mean",
]:
hourly_temp = tstoolbox.read(hourly_temp)
hourly_temp = hourly_temp.astype(float).squeeze()

if method == "month_hour_precip_mean":
month_hour_precip_mean = calculate_month_hour_precip_mean(hourly_temp)
hourly_precip_hum = tstoolbox.read(hourly_precip_hum)
month_hour_precip_mean = calculate_month_hour_precip_mean(hourly_precip_hum)
else:
month_hour_precip_mean = "None"

ntsd = pd.DataFrame(
disaggregate_humidity(
tsd,
tsd.astype(float),
method=method,
temp=hourly_temp,
a0=a0,
Expand Down
37 changes: 37 additions & 0 deletions src/mettoolbox/mettoolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,17 @@ def humidity_cli(
target_units=None,
print_input=False,
tablefmt="csv",
precip_col=None,
temp_min_col=None,
temp_max_col=None,
hum_min_col=None,
hum_max_col=None,
hum_mean_col=None,
a0=None,
a1=None,
kr=None,
hourly_temp=None,
hourly_precip_hum=None,
preserve_daily_mean=None,
):
"""Disaggregate daily relative humidity to hourly humidity.
Expand All @@ -190,6 +194,19 @@ def humidity_cli(
| | calculated as average of `hum_tmin_col` and |
| | `hum_tmax_col`. |
+--------------+---------------------------------------------+
| temp_min_col | Required column name or number representing |
| | the minimum daily temperature for `minimal`,|
| | `dewpoint regression`, `linear dewpoint |
| | variation`, and `min_max` methods. |
+--------------+---------------------------------------------+
| temp_max_col | Required column name or number representing |
| | the maximum daily temperature for min_max |
| | method. |
+--------------+---------------------------------------------+
| precip_col | Required column name or number representing |
| | the total precipitation for |
| | month_hour_precip_mean method. |
+--------------+---------------------------------------------+
Parameters
----------
Expand Down Expand Up @@ -291,6 +308,18 @@ def humidity_cli(
${tablefmt}
precip_col:
Column index (data columns start numbering at 1) or column name
from the input data that contains the daily precipitation.
temp_min_col:
Column index (data columns start numbering at 1) or column name
from the input data that contains the daily minimum temperature.
temp_max_col:
Column index (data columns start numbering at 1) or column name
from the input data that contains the daily maximum temperature.
hum_min_col:
Column index (data columns start numbering at 1) or column name
from the input data that contains the daily minimum humidity.
Expand Down Expand Up @@ -319,6 +348,10 @@ def humidity_cli(
Filename of a CSV file that contains an hourly time series of
temperatures.
hourly_precip_hum: str
Filename of a CSV file that contains an hourly time series of
precipitation and humidity.
preserve_daily_mean: str
Column name or index (data columns start at 1) that identifies
the observed daily mean humidity. If not None will correct the
Expand All @@ -341,12 +374,16 @@ def humidity_cli(
source_units=source_units,
target_units=target_units,
print_input=print_input,
precip_col=hum_min_col,
temp_min_col=hum_min_col,
temp_max_col=hum_max_col,
hum_min_col=hum_min_col,
hum_max_col=hum_max_col,
hum_mean_col=hum_mean_col,
a0=a0,
a1=a1,
kr=kr,
hourly_precip_hum=hourly_precip_hum,
hourly_temp=hourly_temp,
preserve_daily_mean=preserve_daily_mean,
),
Expand Down
529 changes: 519 additions & 10 deletions tests/Disaggregation_test.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 6dadf2f

Please sign in to comment.