Skip to content

Commit 00b1137

Browse files
committedOct 25, 2024·
Added a check for the download time period for GFS (Issue #140). The available dates are read from aws listing at runtime.
1 parent 8592725 commit 00b1137

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
 

‎pyschism/forcing/nws/nws2/gfs2.py

+25
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ def __init__(
3535
self.pscr = pscr
3636
self.product = product
3737

38+
# check start_date
39+
min_date = None
40+
response = self.s3.list_objects_v2(Bucket=self.bucket, Prefix="gfs.", Delimiter='/')
41+
if 'CommonPrefixes' in response:
42+
# Extract folder names, parse dates, and find the earliest date
43+
dates = []
44+
for obj in response['CommonPrefixes']:
45+
folder_name = obj['Prefix'].split('.')[1].rstrip('/') # Extract the date part
46+
try:
47+
date_obj = datetime.strptime(folder_name, '%Y%m%d')
48+
dates.append(date_obj)
49+
except ValueError:
50+
continue
51+
if dates:
52+
logger.info(f"Available GFS data dates, from {min(dates)} to {max(dates)}")
53+
min_date = min(dates)
54+
else:
55+
raise Exception("Unable to fetch GFS data dates from S3 bucket.")
56+
57+
if min_date is not None and self.start_date < min_date:
58+
raise ValueError(
59+
f"Start date {self.start_date} is earlier than the earliest available date {min_date}")
60+
3861
self.forecast_cycle = self.start_date
3962

4063
timevector = np.arange(
@@ -67,6 +90,7 @@ def __init__(
6790
else:
6891
logger.info(f'file {key} is not available, try next file')
6992
continue
93+
7094

7195
def get_file_namelist(self, requested_dates):
7296

@@ -149,6 +173,7 @@ def write(self, start_date, rnday, air: bool=True, prc: bool=True, rad: bool=Tru
149173

150174
start_date = nearest_cycle() if start_date is None else start_date
151175

176+
152177
if (start_date + timedelta(days=rnday)) > datetime.utcnow():
153178
logger.info(f'End date is beyond the current time, set rnday to 1 day and record to 5 days')
154179
rnday = 1

0 commit comments

Comments
 (0)