# How to choose variables from a list for a function and then use the solution in a subsequent function?

I am trying to calculate heating degree days and cooling degree days and output that information to a table. I am using mathematica's curated data to do this. In text this is what I would like to do. Use a city name to collect weather data (the mean temp for every day of a certain year), then calculate the HDD and CDD and to create a cumulative value of hdd and cdd for the entire year. I want to use the city name to collect a value for country name, country gdp, and city population. I have used a demonstration project to try and figure this out but I am stuck at this point. I have the following already worked out.

Module[
{dateRange, mean, cdd, hdd, station, country, location, population,
GDPPerCapita,
reference = (65 - 32)/1.8, cumList},
station = "Chicago";
country = CityData[station, "Country"];
population = CityData[station, "Population"];
location = CityData[station, "Coordinates"];
GDPPerCapita = CountryData[country, "GDPPerCapita"];
dateRange = {{2011, 1, 1}, {2011, 12, 31}, "Day"};
mean = WeatherData[station, "MeanTemperature", dateRange];
cdd = Join[Transpose[{mean[[All, 1]]}],
Transpose[{Max[# - reference, 0] & /@ mean[[All, 2]]}], 2];
hdd = Join[Transpose[{mean[[All, 1]]}],
Transpose[{Max[reference - #, 0] & /@ mean[[All, 2]]}], 2];
cumList = Transpose[{Join[
Transpose[{cdd[[All, 1]]}],
Transpose[{Drop[FoldList[Plus, 0, cdd[[All, 2]] + hdd[[All, 2]]],
1]}],
2]}];
Grid[station, country, location, population, GDPPerCapita,
Last[cumList]]]


In this particular example I used a set city name ie station = "Chicago". When I do this it returns the correct result for that specific city, in a grid in this case..

Grid["Chicago", "UnitedStates", {41.8376, -87.6818}, 2695598,
45230.2, {{{2011, 12, 31}, 3935.4}}]


I want the variables to be chosen from a list of cities....

cityLIST = CityData[#, "Name"] & /@ CityData[];


So basically I want to run this or something like it for every string in a list, and output the results to a table. I am new to mathematica, this is the first thing I have ever tried. If anyone has a minute to help me out with this I would really appreciate it.

Thanks

• Welcome to Mathematica.Stackexchange. It's great to see you jump right in the middle and try all those things. However, I'd prefer you try reading some basic documentation first before asking questions. I'd start with tutorial/GettingStartedOverview and tutorial/CoreLanguageOverview in the documentation system. – Sjoerd C. de Vries Jun 5 '12 at 21:04

A slight modification of your code makes this a real function:

myWeatherData[station_] :=
Module[{dateRange, mean, cdd, hdd, country, location, population,
GDPPerCapita, reference = (65 - 32)/1.8, cumList},
country = CityData[station, "Country"];
population = CityData[station, "Population"];
location = CityData[station, "Coordinates"];
GDPPerCapita = CountryData[country, "GDPPerCapita"];
dateRange = {{2011, 1, 1}, {2011, 12, 31}, "Day"};
mean = WeatherData[station, "MeanTemperature", dateRange];
cdd = Join[Transpose[{mean[[All, 1]]}],
Transpose[{Max[# - reference, 0] & /@ mean[[All, 2]]}], 2];
hdd = Join[Transpose[{mean[[All, 1]]}],
Transpose[{Max[reference - #, 0] & /@ mean[[All, 2]]}], 2];
cumList =  Transpose[{Join[Transpose[{cdd[[All, 1]]}],
Transpose[{Drop FoldList[Plus, 0, cdd[[All, 2]] +
hdd[[All, 2]]], 1]}], 2]}];
{station, country, location, population, GDPPerCapita, Last[cumList]}
]


The city list

cityLIST = CityData[#, "Name"] & /@ CityData[];


It is pretty long:

 cityLIST // Length

(* ==>  164186  *)


Therefore, we'll try it on the first ten cities in the list:

Grid[myWeatherData /@ cityLIST[[1 ;; 10]], Frame -> All] You need to define a function like this

(*
We use StringQ in the variable pattern so that it takes only strings of city names.
Without the StringQ it will also work!
*)
MyDataCollecter[station_?StringQ] :=
Module[{dateRange, mean, cdd, hdd, country, location, population,
GDPPerCapita, reference = (65 - 32)/1.8, cumList},
country = CityData[station, "Country"];
population = CityData[station, "Population"];
location = CityData[station, "Coordinates"];
GDPPerCapita = CountryData[country, "GDPPerCapita"];
dateRange = {{2011, 1, 1}, {2011, 12, 31}, "Day"};
mean = WeatherData[station, "MeanTemperature", dateRange];
cdd = Join[Transpose[{mean[[All, 1]]}],
Transpose[{Max[# - reference, 0] & /@ mean[[All, 2]]}], 2];
hdd = Join[Transpose[{mean[[All, 1]]}],
Transpose[{Max[reference - #, 0] & /@ mean[[All, 2]]}], 2];
cumList =
Transpose[{Join[Transpose[{cdd[[All, 1]]}],
Transpose[{Drop[
FoldList[Plus, 0, cdd[[All, 2]] + hdd[[All, 2]]], 1]}], 2]}];
(* For the look *)
Grid[{ToString[#] & /@ {Station, Country, Coordinate, Population,
GDP, CumList}, {station, country, location, population,
GDPPerCapita, Grid[Last[cumList], Frame -> All]}}, Frame -> All,
ItemStyle -> "Subsection", Background -> {None, {Pink, Cyan}}]];
(* To make the function accept a list of city names *)
SetAttributes[MyDataCollecter, Listable];


Now we apply this function on a randomly chosen list of of ten cities from your list of cities cityLIST.

MyDataCollecter@RandomChoice[cityLIST, 10] // TableForm But I hope you will notice that for many cities there is no data in the Wolfram Database and that why MMA can report an error called WeatherData::notent:. Here is an example.

WeatherData::notent: "\!$$\"\\\"Kumbi\\\"\"$$ is not a known entity,
class, or tag for  WeatherData. Use WeatherData[] for a list of entities."


I suggest that you look at the answers of this post on how to adapt your code when such error occurs.

BR