Friday, May 23, 2008

Add temperature to gas usage (part 1)

Friday, May 23, 2008 Posted by Unknown , , , ,
Wouldn't it be nice if we can extend the previous 'datawarehouse' with the temperatures? so we can compare the gas (heating) or electricity (airco) usage with the current or average day temperature.

Let's create a .net application which gets the current temperature from a webservice every hour and adds them to a new fact table. We gonna use Windows Communication Foundation and Linq in this example.

In this part we gonna create a webservice client to the weatherservice. In the next part we gonna add the linq part to load the temperature in the oracle database.

I found a pretty nice webservice that is free and also has the european weather:

First thing is to create a stub for the webservice:


This creates a GlobalWeather.cs stub and an output.config file which contains the bindings.

Ignore the errors, this is because there are other than soap ports in the wsdl.

rename the output.config to degrees.exe.config

create the client degrees.cs:

[sourcecode language="c#"]

using System;
using System.Web.Services.Protocols;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;
public class SetCoverImage
public static void Main (string [] args)
GlobalWeatherSoapClient w = new GlobalWeatherSoapClient();

// The service returns a plain string

// We load the string in an xml reader to parse it and find the temperature
XmlReader reader = XmlReader.Create(new System.IO.StringReader(w.GetWeather("Groningen","Netherlands")));
// Parse the file and find the Temperature element its value
while (reader.Read())
if (reader.NodeType==XmlNodeType.Element && reader.Name=="Temperature")
// Do some regex to find the temperature in celcius
reader.Read(); Console.WriteLine(Regex.Replace(Regex.Replace(reader.Value,"^.*\(","")," C\).*$",""));
if (reader.NodeType==XmlNodeType.Element && reader.Name=="Status")
reader.Read(); Console.WriteLine(reader.Value.Trim());


csc degrees.cs GlobalWeather.cs

<?xml version="1.0" encoding="utf-16"?>
<Location>Groningen Airport Eelde, Netherlands (EHGG) 53-08N 006-35E 4M</Location>
<Time>May 23, 2008 - 07:25 AM EDT / 2008.05.23 1125 UTC</Time>
<Wind> from the NE (050 degrees) at 8 MPH (7 KT) (direction variable):0</Wind>
<Temperature> 69 F (21 C)</Temperature>
<DewPoint> 39 F (4 C)</DewPoint>
<RelativeHumidity> 32%</RelativeHumidity>
<Pressure> 30.00 in. Hg (1016 hPa)</Pressure>

The service returns the temperature in my region.

Next step is to load it into the database. (see part 2)