Skip to content

Commit

Permalink
handle nans for temporal means cdfs
Browse files Browse the repository at this point in the history
  • Loading branch information
grantbuster committed Jan 5, 2024
1 parent 3eadb3b commit 3962d9d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions rex/bias_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ def qdm_irrad(ghi, dni, dhi,
# This will prevent inverse CDF functions from returning zero resulting in
# a divide by zero error in the calculation of the QDM delta. These zeros
# get fixed later in _irrad_post_proc
ghi[ghi_zeros] = 1500
dni[dni_zeros] = 1500
ghi[ghi_zeros] = 1
dni[dni_zeros] = 1

ghi = ghi_qdm(ghi)
dni = dni_qdm(dni)
Expand Down
16 changes: 12 additions & 4 deletions rex/temporal_stats/temporal_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,19 @@ def cdf(data, n=50, decimals=None):
evenly spaced in quantile space on the y-axis of the CDF.
"""

p = np.linspace(0, 1, n)
x = np.interp(p, np.linspace(0, 1, len(data)), sorted(data))
nan_mask = np.isnan(data)
if nan_mask.all():
return np.zeros(n)

assert x[0] == data.min()
assert x[-1] == data.max()
p = np.linspace(0, 1, n)
x = np.interp(p, np.linspace(0, 1, len(data[~nan_mask])),
sorted(data[~nan_mask]))

msg = (f'First and last points defining the CDF ({x[0]}, {x[-1]}) '
f'were not the min and max data values '
f'({np.nanmin(data)}, {np.nanmin(data)}).')
assert x[0] == np.nanmin(data), msg
assert x[-1] == np.nanmax(data), msg

if decimals is not None:
x = np.round(x, decimals=decimals)
Expand Down

0 comments on commit 3962d9d

Please sign in to comment.