import boto3
import json
import os
import pandas as pd
import numpy as np
import time
import datetime
import statistics
import pytz
import io
from pytz import timezone
from IPython.display import display, HTML
from matplotlib import pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
iota = boto3.client('iotanalytics')
dataset = "telescope_frequent"
dataset_url = iota.get_dataset_content(datasetName = dataset,versionId = "$LATEST")['entries'][0]['dataURI']
df = pd.read_csv(dataset_url,low_memory=False)
df['datetime']=pd.DatetimeIndex(pd.to_datetime(df["received"]/1000, unit='s')) \
.tz_localize('UTC') \
.tz_convert('US/Pacific')
df.index = df['datetime']
response = iota.describe_dataset(datasetName = dataset)
sql = response["dataset"]["actions"][0]["queryAction"]["sqlQuery"]
display(HTML('<h2 style="font-size:24px">'+sql+"</h2>"))
df_tempg = df[(df['full_topic'].str.contains('infrared/temperature'))]
df_tempg = df_tempg['ambient']
df_tempg.name='ground_temperature'
df_temps = df[(df['full_topic'].str.contains('infrared/temperature'))]
df_temps = df_temps['object']
df_temps.name='sky_temperature'
df_light = df[(df['full_topic'].str.contains('/photoresistor'))]
df_light = df_light['illumination']
df_delta = df_tempg - df_temps
df_delta.name = 'delta_temperature'
df_tempg.plot(style=['-hg'],figsize=(20,8),linewidth=2,grid=True,kind='line',secondary_y=False)
df_temps.plot(style=['--c'],figsize=(20,8),linewidth=2,grid=True,kind='line',secondary_y=False)
df_light.plot(style=['-y*'],figsize=(20,8),linewidth=2,grid=True,kind='line',secondary_y=False)
df_delta.plot(style=['-*b'],figsize=(20,8),linewidth=2,grid=True,kind='line',secondary_y=False)
plt.legend()
# -- Save the resulting plot to disk and then upload to S3
image_data = io.BytesIO()
plt.savefig(image_data, format='png')
image_data.seek(0)
s3 = boto3.resource('s3')
bucket = s3.Bucket('rtjm-dashboard-bucket')
bucket.put_object(Body=image_data, ContentType='image/png', Key='sky-conditions.png')
mean = statistics.mean(df_delta)
sigma = statistics.stdev(df_delta)
sky='Unknown'
if (sigma < 0.2 and mean > 20) :
sky = 'Clear'
if (sigma < 0.2 and mean < 3) :
sky = 'Rain or Snow'
if (sigma < 0.5 and mean > 3 and mean < 12) :
sky = 'Low cloud'
if (sigma < 0.5 and mean >10 and mean < 18) :
sky = 'High cloud'
mean_light = statistics.mean(df_light)
if (mean_light > 75) :
period = 'Day'
else :
period = 'Night'
content_object = s3.Object('rtjm-dashboard-bucket', 'sky-state.json')
file_content = content_object.get()['Body'].read().decode('utf-8')
json_content = json.loads(file_content)
prior_period = json_content["period"]
prior_sky = json_content["sky"]
if (prior_period != period or prior_sky != sky ) :
message = period+' conditions are '+sky
sns = boto3.client('sns')
response = sns.publish(TopicArn='arn:aws:sns:us-west-2:ACCOUNT_ID:information-service',Message=message)
output = {"generated":datetime.datetime.now(pytz.utc).strftime("%Y-%m-%d %H:%M:%S"),\
"period":period,\
"sky":sky,\
"mean_light":round(mean_light,2),\
"mean_delta":round(mean,2),\
"delta_stdev":round(sigma,3) \
}
state = json.dumps(output)
bucket.put_object(Body=state, ContentType='application/json', Key='sky-state.json')