Skip to content

Commit

Permalink
Merge pull request #6 from jagoosw/1.1.0
Browse files Browse the repository at this point in the history
1.1.0
  • Loading branch information
jagoosw authored Jul 4, 2022
2 parents 1d598ff + a81f313 commit 242e3bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
31 changes: 29 additions & 2 deletions getgfs/getgfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ def get(self, variables, date_time, lat, lon):
else:
return File(r.text)

def check_avail(self, forecast_date, forecast_time):
r = requests.get(
url.format(
res=self.resolution,
step=self.timestep,
date=forecast_date,
hour=int(forecast_time),
info="ascii?gustsfc[0][540][1260]",
)
)
if r.text[:6] == "<html>":
return False
else:
return True

def datetime_to_forecast(self, date_time):
"""Works out which forecast date/run/time is required for the latest values for a chosen time
Expand Down Expand Up @@ -223,6 +238,11 @@ def datetime_to_forecast(self, date_time):
forecast_date = query_forecast.strftime("%Y%m%d")
forecast_time = query_forecast.strftime("%H")

while not self.check_avail(forecast_date, forecast_time):
query_forecast -= timedelta(hours=6)
forecast_date = query_forecast.strftime("%Y%m%d")
forecast_time = query_forecast.strftime("%H")

query_time = "[{t_ind}]".format(
t_ind=round(
(desired_date - query_forecast).seconds
Expand Down Expand Up @@ -256,8 +276,11 @@ def value_input_to_index(self, coord, inpt):
"""
if isinstance(inpt, str):
if inpt[0] == "[" and inpt[-1] == "]" and ":" in inpt:
val_1 = float(re.findall(r"\[(.*?):", inpt))
val_2 = float(re.findall(r"\:(.*?)]", inpt))
val_1 = float(re.findall(r"\[(.*?):", inpt)[0])
val_2 = float(re.findall(r"\:(.*?)]", inpt)[0])
if coord == "lon":
val_1 = val_1 % 360
val_2 = val_2 % 360
val_min = self.value_to_index(coord, min(val_1, val_2))
val_max = self.value_to_index(coord, max(val_1, val_2))
ind = "[%s:%s]" % (val_min, val_max)
Expand All @@ -269,8 +292,12 @@ def value_input_to_index(self, coord, inpt):
"The format of the %s variable was incorrect, it must either be a single number or a range in the format [min:max]. You entered '%s'"
% (coord, inpt)
)
if coord == "lon":
inpt = inpt % 360
ind = "[%s]" % self.value_to_index(coord, inpt)
else:
if coord == "lon":
inpt = inpt % 360
ind = "[%s]" % self.value_to_index(coord, inpt)

return ind
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
url="https://getgfs.readthedocs.io/",
#
license="LICENSE.txt",
description="getgfs extracts weather forecast variables from the NOAA GFS forecast with no obscure depdndencies (looking at you ecCodes)",
description="getgfs extracts weather forecast variables from the NOAA GFS forecast",
# Dependent packages (distributions)
install_requires=[
"scipy",
Expand Down

0 comments on commit 242e3bb

Please sign in to comment.