Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 0d327bc

Browse files
committed
add code to keep dusk and dawn, add extra test
1 parent 620f383 commit 0d327bc

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

nowcasting_dataset/time.py

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import random
44
import warnings
5+
from datetime import timedelta
56
from typing import Dict, Iterable, List, Optional, Tuple
67

78
import numpy as np
@@ -40,6 +41,31 @@ def select_daylight_datetimes(
4041
across all locations.
4142
4243
"""
44+
45+
if keep_dawn_dusk_hours > 0:
46+
# get dawn datetimes by
47+
# - adding X hours the original datetimes,
48+
# - running through select_daylight_datetimes
49+
# - minusing X hours from the results
50+
# similar for dusk
51+
temp_keep_dawn_dusk_hours = 0
52+
dawn = select_daylight_datetimes(
53+
datetimes=datetimes + timedelta(hours=keep_dawn_dusk_hours),
54+
locations=locations,
55+
ghi_threshold=ghi_threshold,
56+
keep_dawn_dusk_hours=temp_keep_dawn_dusk_hours,
57+
) - timedelta(hours=keep_dawn_dusk_hours)
58+
59+
dusk = select_daylight_datetimes(
60+
datetimes=datetimes - timedelta(hours=keep_dawn_dusk_hours),
61+
locations=locations,
62+
ghi_threshold=ghi_threshold,
63+
keep_dawn_dusk_hours=temp_keep_dawn_dusk_hours,
64+
) + timedelta(hours=keep_dawn_dusk_hours)
65+
66+
# take union of dusk and dawn
67+
return dusk.union(dawn)
68+
4369
ghi_for_all_locations = []
4470
for x, y in locations:
4571
lat, lon = geospatial.osgb_to_lat_lon(x, y)

tests/test_time.py

+25
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@ def test_select_daylight_datetimes_dusk():
2727
np.testing.assert_array_equal(daylight_datetimes, correct_daylight_datetimes)
2828

2929

30+
def test_select_daylight_datetimes_dusk_non_gridded_date():
31+
"""Test day light filter works with keeping dusk and dawn extra hours
32+
33+
This is for non-gridded date.
34+
This is to check that no extra datetimes are being returned
35+
"""
36+
datetimes = pd.DatetimeIndex(
37+
[
38+
"2020-01-01 07:00",
39+
"2020-01-01 16:00",
40+
"2020-01-01 17:00",
41+
"2020-01-01 18:00",
42+
"2020-01-01 19:00",
43+
]
44+
)
45+
correct_daylight_datetimes = pd.DatetimeIndex(
46+
["2020-01-01 07:00", "2020-01-01 16:00", "2020-01-01 17:00", "2020-01-01 18:00"]
47+
)
48+
locations = [(0, 0), (20_000, 20_000)]
49+
daylight_datetimes = nd_time.select_daylight_datetimes(
50+
datetimes=datetimes, locations=locations, keep_dawn_dusk_hours=2
51+
)
52+
np.testing.assert_array_equal(daylight_datetimes, correct_daylight_datetimes)
53+
54+
3055
@pytest.mark.parametrize("min_seq_length", [2, 3, 12])
3156
def test_get_contiguous_time_periods_1_with_1_chunk(min_seq_length):
3257
"""Test getting continuous chunks of data with 1 chunk of data"""

0 commit comments

Comments
 (0)