def test_unmix_intervals_tz_fail(hr, freq): periods = 6 if freq == '1h' else 2 # should not work. 13-0700 is 20Z index = pd.date_range( start=f'20190101 {hr:02}-0700', freq=freq, periods=periods) with pytest.raises(ValueError): mixed_s = pd.Series([0] * len(index), index=index) forecast.unmix_intervals(mixed_s)
def test_unmix_intervals_tz(hr, freq): periods = 6 if freq == '1h' else 2 # should work. index = pd.date_range( start=f'20190101 {hr:02}-0700', freq=freq, periods=periods) # we only care to test tz functionality, not values mixed_s = pd.Series([0] * len(index), index=index) out = forecast.unmix_intervals(mixed_s) expected_s = pd.Series([0] * len(index), index=index) assert_series_equal(out, expected_s)
def test_unmix_intervals(mixed, expected): npts = len(mixed) if npts in [2, 4]: index = pd.date_range(start='20190101 03Z', freq='3h', periods=npts) else: index = pd.date_range(start='20190101 01Z', freq='1h', periods=npts) mixed_s = pd.Series(mixed, index=index) out = forecast.unmix_intervals(mixed_s) expected_s = pd.Series(expected, index=index) assert_series_equal(out, expected_s)
def _unmix_various_gfs_intervals(init_time, start_floored, end_ceil, cloud_cover_mixed): """unmix intervals for each kind of interval length in GFS forecast""" end_1h = init_time + pd.Timedelta('120hr') end_3h = init_time + pd.Timedelta('240hr') cloud_covers = [] if start_floored < end_1h: cloud_cover_1h_mixed = cloud_cover_mixed.loc[start_floored:end_1h] cloud_covers.append(forecast.unmix_intervals(cloud_cover_1h_mixed)) if end_ceil > end_1h and start_floored < end_3h: cloud_cover_3h_mixed = cloud_cover_mixed.loc[ end_1h+pd.Timedelta('3hr'):end_3h] cloud_covers.append(forecast.unmix_intervals(cloud_cover_3h_mixed)) if end_ceil > end_3h: cloud_cover_12h = cloud_cover_mixed.loc[ end_3h+pd.Timedelta('12hr'):end_ceil] cloud_covers.append(cloud_cover_12h) cloud_cover = pd.concat(cloud_covers) return cloud_cover
def gfs_quarter_deg_hourly_to_hourly_mean(latitude, longitude, elevation, init_time, start, end, interval_label, load_forecast=load_forecast, *, __model='gfs_0p25'): """ Take 1 hr GFS and convert it to hourly average data. GHI from NWP model cloud cover. DNI, DHI computed. Max forecast horizon 120 hours. Parameters ---------- latitude : float longitude : float elevation : float init_time : pd.Timestamp Full datetime of a model initialization start : pd.Timestamp Forecast start. Forecast is inclusive of this instant if interval_label is *beginning* and exclusive of this instant if interval_label is *ending*. end : pd.Timestamp Forecast end. Forecast is exclusive of this instant if interval_label is *beginning* and inclusive of this instant if interval_label is *ending*. interval_label : str Must be *beginning* or *ending* """ start_floored, end_ceil = _adjust_gfs_start_end(start, end) cloud_cover_mixed, air_temperature, wind_speed = load_forecast( latitude, longitude, init_time, start_floored, end_ceil, __model, variables=('cloud_cover', 'air_temperature', 'wind_speed')) cloud_cover = forecast.unmix_intervals(cloud_cover_mixed) return _resample_using_cloud_cover(latitude, longitude, elevation, cloud_cover, air_temperature, wind_speed, start, end, interval_label, 'bfill')
def _unmix_various_gefs_intervals(init_time, start_floored, end_ceil, cloud_cover_mixed): """unmix intervals for each kind of interval length in GEFS forecast""" # 195hr is only in index for post Sept 2020 GEFS if init_time + pd.Timedelta('195hr') in cloud_cover_mixed.index: end_3h = init_time + pd.Timedelta('240hr') else: end_3h = init_time + pd.Timedelta('192hr') cloud_covers = [] if start_floored < end_3h: cloud_cover_3h_mixed = cloud_cover_mixed.loc[start_floored:end_3h] cloud_covers.append(forecast.unmix_intervals(cloud_cover_3h_mixed)) if end_ceil > end_3h: cloud_cover_6_or_12h = cloud_cover_mixed.loc[ end_3h+pd.Timedelta('6hr'):end_ceil] cloud_covers.append(cloud_cover_6_or_12h) cloud_cover = pd.concat(cloud_covers) return cloud_cover
def test_unmix_intervals_two_freq(): index = pd.DatetimeIndex(['20190101 01', '20190101 02', '20190101 04']) mixed_s = pd.Series([1, 1/3, 1/6], index=index) with pytest.raises(ValueError): forecast.unmix_intervals(mixed_s)
def test_unmix_intervals_invalidfreq(): index = pd.date_range(start='20190101 01Z', freq='2h', periods=3) mixed_s = pd.Series([1, 1/3, 1/6], index=index) with pytest.raises(ValueError): forecast.unmix_intervals(mixed_s)