Saturday, June 1, 2013

Mathematica's WeatherData

This doesn't have much to do with physics, but I recently got the chance to play with Mathematica's WeatherData, and wanted to share some of the cool things that are possible with it. It's an awesome resource, and if you have the chance, it's fun to play with the incredible amount of data it contains. 

As an example, it's fairly easy to find how monthly temperatures in the last year compare to averages over the last several decades:

monthData = WeatherData["Chicago", "MeanTemperature", {{1900, 1, 1}, {2013, 5, 31}, "Month"}];
getInfo[month_] := Module[{mData, mAvgs, av, curr},
   mData = Select[monthData, #[[1]][[2]] == month &];
   mAvgs = Table[mData[[i]][[2]], {i, 1, Length[mData] - 1}];
   av = Mean[mAvgs];
   curr = mData[[Length[mData]]][[2]];
   {month, av, curr, 
    ToString[Abs[av - curr]] <> " degrees " <> 
     If[av < curr, "warmer", "colder"]}];
Table[getInfo[i], {i, 1, 12}] // MatrixForm

It's really cool to have such easy access to data about the weather. 

As an aside, the data appears to begin around 1950, and the last year containing complete data (at least according to one of the demonstrations online) is 2008. That said, I had no difficulty finding data for the last few years, so that information may have just been because the demonstration was published in 2009. 

With the data, it is also fairly straightforward to find average yearly temperatures over time:

yearData = WeatherData["Chicago", "MeanTemperature", {{1952, 1, 1}, {2012, 12, 31}, "Year"}];
DateListPlot[yearData, Joined -> True]
image
You can sort of see here how the temperature generally increases over the last several decades, but the fluctuations kind of drown it out. Instead, let's look at the average temperature each day of the year for two different decades: the 1960s and the 2000s. The data here is averaged over 10-year samples, with a smoothing length of 10 days in either direction to eliminate the essentially random day-to-day jitter.
getDayData[month_, day_, data_] := Module[{dData, dayAvgs},
  dData = Select[data, And[#[[1]][[2]] == month, #[[1]][[3]] == day] &];
  dayAvgs = Table[dData[[i]][[2]], {i, 1, Length[dData]}];
  {{0, month, day}, Mean[dayAvgs]}]; 

getAllDayDataRange[yearStart_, yearEnd_] := Module[{dayData},
  dayData = 
   WeatherData["Chicago", 
    "MeanTemperature", {{yearStart, 1, 1}, {yearEnd, 12, 31}, "Day"}];  Flatten[
   Table[getDayData[m, d, dayData], {m, 1, 12}, {d, 1, DaysInMonth[[m]]}], 1]
  ];

DaysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

sixtiesData = getAllDayDataRange[1960, 1969];
smoothedSixties = sixtiesData[[11 ;; Length[sixtiesData] - 10]];
smoothedSixties[[All, 2]] = MovingAverage[sixtiesData[[All, 2]], 21];

thousandsData = getAllDayDataRange[2000, 2009];
smoothedThousands = thousandsData[[11 ;; Length[thousandsData] - 10]];
smoothedThousands[[All, 2]] = MovingAverage[thousandsData[[All, 2]], 21];
image
Now that is a striking change! 

I just wanted to share a neat resource for climate data to play with. Here I've barely scratched the surface of the information it contains; there's also information on pressure, humidity, wind, cloud conditions...it's a source of tons of data.

No comments:

Post a Comment