# Integrating over data points from an external source (wolfram|alpha and weather)

I moved to another city and the weather sucks.

Sometimes I feel like getting sad and so I go to wolfram|alpha and check for example

${}$ http://www.wolframalpha.com/input/?i=weather+in+Rome+vs.+London

${}$

Today I feel like getting depressed.

Consequently I wonder if there is a way to import all the data into Mathematica and write a function

TemperatureComparison[{city1,city2},{date1,date2}]


which should evaluate to

$$\frac{ \int_{\text{date 1}}^{\text{date 2}} \left(T_{\text{city 2}}(t)-T_{\text{city 1}}(t)\right) \text dt } { t_{\text{date 2}}-t_{\text{date 1}} },$$

$T_{\text{city X}}(t)$ being the temperature in $\text{city X}$ at time $t$.

• You've seen WeatherData[]? Oct 12 '12 at 8:23
• You can get the data out of Wolfram|Alpha pretty easily. Take a look at this article for an example: support.wolfram.com/kb/7357 The data here is financial, but the process is the same. Oct 12 '12 at 14:41

As suggested by @J.M. you don't need WolframAlpha. One way is to define :

temperatureComparison[{city1_, city2_}, {date1_, date2_}] :=
Module[{data1, data2, int1, int2},
data1 = {AbsoluteTime[#[]], #[]} & /@  WeatherData[city1, "MeanTemperature", {date1, date2, "Day"}] ;
data2 = {AbsoluteTime[#[]], #[]} & /@ WeatherData[city2, "MeanTemperature", {date1, date2, "Day"}] ;
int1 = Interpolation[data1];
int2 = Interpolation[data2];
{#, NIntegrate[
int1[t] - int2[t], {t, AbsoluteTime[date1],
AbsoluteTime[#]}]/(AbsoluteTime[#] - AbsoluteTime[date1])} & /@
NestList[DatePlus[#, 1] &, DatePlus[date1, 1], Round@DateDifference[date1, date2] - 1]


]

which you can use as :

output = temperatureComparison[{"Rome", {"London", "GreaterLondon",
"UnitedKingdom"}}, {{2012, 1, 1}, Date[]}]

DateListPlot[output] 