Skip to content

Commit

Permalink
Merge pull request #33 from viogp/master
Browse files Browse the repository at this point in the history
Addition of forecast.future(in_days=#,in_hours=#, in_minutes=#, in _seconds=#)
  • Loading branch information
jacobtomlinson committed Aug 31, 2017
2 parents f309838 + f062c8a commit 1ac4b74
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
54 changes: 49 additions & 5 deletions datapoint/Forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,61 @@ def now(self):
"""
now = None
d = datetime.datetime.utcnow()
for_total_seconds = d - d.replace(hour=0, minute=0, second=0, microsecond=0)
for_total_seconds = d - \
d.replace(hour=0, minute=0, second=0, microsecond=0)
# python 2.6 does not have timedelta.total_seconds()
if sys.version_info < (2,7):
msm = self.timedelta_total_seconds(for_total_seconds) / 60
else:
msm = for_total_seconds.total_seconds() / 60
if self.days[0].date.strftime("%Y-%m-%dZ") == d.strftime("%Y-%m-%dZ"):
for timestep in self.days[0].timesteps:
for timestep in self.days[0].timesteps:
if timestep.name > msm:
break
now = timestep
return now
#print timestep.date,timestep.name,msm
now = timestep
return now
else:
return False

def future(self,in_days=None,in_hours=None,in_minutes=None,in_seconds=None):
"""
Function to return a future timestp
"""
future = None

# Initialize variables to 0
dd, hh, mm, ss = [0 for i in range(4)]
if (in_days != None):
dd = dd + in_days
if (in_hours != None):
hh = hh + in_hours
if (in_minutes != None):
mm = mm + in_minutes
if (in_seconds != None):
ss = ss + in_seconds
#print dd,hh,mm,ss

# Set the hours, minutes and seconds from now (minus the days)
dnow = datetime.datetime.utcnow() # Now
d = dnow + \
datetime.timedelta(hours=hh, minutes=mm, seconds = ss)
# Time from midnight
for_total_seconds = d - \
d.replace(hour=0, minute=0, second=0, microsecond=0)

# Convert into minutes since midnight
try:
msm = for_total_seconds.total_seconds()/60.
except:
# For versions before 2.7
msm = self.timedelta_total_seconds(for_total_seconds)/60.

if (dd<len(self.days)):
for timestep in self.days[dd].timesteps:
if timestep.name >= msm:
#print timestep.date,timestep.name,msm
future = timestep
return future
else:
print 'ERROR: requested date is outside the forcast range selected,', len(self.days)
return False
6 changes: 3 additions & 3 deletions datapoint/Manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ def get_nearest_site(self, longitude=False, latitude=False):
coordinates.
"""
if not longitude or not latitude:
print 'ERROR: No longitude and latitude given.'
return False

nearest = False
distance = False
distance = None
sites = self.get_all_sites()
for site in sites:
new_distance = \
Expand All @@ -193,7 +194,7 @@ def get_nearest_site(self, longitude=False, latitude=False):
float(longitude),
float(latitude))

if new_distance < distance or not distance:
if ((distance == None) or (new_distance < distance)):
distance = new_distance
nearest = site
return nearest
Expand All @@ -210,7 +211,6 @@ def get_forecast_for_site(self, site_id, frequency="daily"):
"""
data = self.__call_api(site_id, {"res":frequency})
params = data['SiteRep']['Wx']['Param']

forecast = Forecast()
forecast.data_date = data['SiteRep']['DV']['dataDate']
forecast.data_date = datetime.strptime(data['SiteRep']['DV']['dataDate'], DATA_DATE_FORMAT).replace(tzinfo=pytz.UTC)
Expand Down

0 comments on commit 1ac4b74

Please sign in to comment.