Sky Conditions

In [1]:
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'
In [2]:
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']
In [3]:
response = iota.describe_dataset(datasetName = dataset)
sql = response["dataset"]["actions"][0]["queryAction"]["sqlQuery"]
display(HTML('<h2 style="font-size:24px">'+sql+"</h2>"))

select * from telescope_data where __dt >= current_date - interval '1' day

In [4]:
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'
In [5]:
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')
Out[5]:
s3.Object(bucket_name='rtjm-dashboard-bucket', key='sky-conditions.png')

See if the conditions have been stable for the last time period analyzed

In [6]:
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'

Determine if it is day or night

In [7]:
mean_light = statistics.mean(df_light)
if (mean_light > 75) :
    period = 'Day'
else :
    period = 'Night'

Read previous state and see if the state has changed. If so, send a message to SNS

In [8]:
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)

Store the current conditions as the state to be used on the next run

In [9]:
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')
Out[9]:
s3.Object(bucket_name='rtjm-dashboard-bucket', key='sky-state.json')