Monday, October 22, 2007

Correlating Data

After analyzing any measurement values I always ask myself the same question, is this data wrong or not?. To check if my temperature measurements were good enough I gathered in all the thermometers in my home and placed next to the Arduino+LM35. Then I confirmed that the LM35 was doing a good job, but that wasn´t enough, I needed to know if the measurement of the LM35 could be at least a naive model of the whole room temperature. To do so I started working with PyEphem and probed that the temperature data was correlated with the sun altitude.
The Theory
In this season in Spain, I'm currently no using the heating system and I suppose my neighbors neither, so the only heat source is the sun. As the room where the sensor is placed has a big window, if my measurements are correct there should be a direct correlation between the sun altitude and the temperature of the room.

pyEphem First Approach

How to get sun data from pyEphem

import ephem

#For those unfamiliar with azimuth and altitude:
#Azimuth: describe position in the sky by measuring angle around the horizon,
#Altitude: angle above the horizon.
#To locate the Sun with az:122 and alt:70
#then turn right 122, bringing you almost around to the southeast (which lies 125 around the sky f
#and by looking 70 above that point on the horizon . fairly high, given that 90 is directly overhe

#This library always give time in UTC
#For Spain bear in mind this
#Area: Spain (excepto islas canarias)
#Horario normal: UTC+1
#Horario verano: UTC+2
#Duracion Horario de verano:
#Desde el ultimo domingo de marzo a las 02:00 hasta el ultimo domingo de Octubre

def plotSunEphem():
  room = ephem.Observer()
  room.long, = '-3.957739' , '40.490239' = '2007/10/7 20:35:00'
  sun = ephem.Sun()
  print "alt:%s azi:%s" % (sun.alt,
  print "alt:%f azi:%f" % (sun.alt,
  print sun.rise_time, sun.set_time

def main():

if __name__ == "__main__":

Visual Correlation

Using pyEphem and plotting the sun altitude in the same plot of the temperature I could graphicaly see the correlation between both data without doing any statistical computation.

MatPlotLib issues and Python Image Library to the rescue

Something cool from MatPlotLib is that it supports native date plots, but it has its drawbacks. The date_plot doesn't suport multiple data in one plot. As I didn't want to reimplement the method I had to compute both plots and the use the PIL to blend them. The PIL is a great tool.

import Image
import ImageChops

def MultiplyImages(imageFilename01, imageFilename02, resultFilename):
  image01 =
  image02 =

  image03 = ImageChops.multiply(image01, image02)

Final Rework

I've been coding very quickly and with not too much time, so at this stage of the project I decided to devote sometime to recode a bit to make the source code nicer. An here I leave the link to the temperature plot script responsible for making the plot of the today, yesterday and beforeyesterday temperatures and the sun altitude and showing it in a webpage.

The result
Except cloudy days and days when we decided to air the room, there is a big correlation between temperature and sun altitude as it can be seen in the main image of this post.

No comments:

Post a Comment