2
$\begingroup$

I want to store hourly temperature readings retrieved from Wolfram|Alpha in an array in my notebook. If this is not possible, then tell me how to preserve the retrieved data in my notebook, so when it is reopened, the previously retrieved data need not be reentered. Additional data entry should be limited to just the data required from the user.

Thanks in advance.

$\endgroup$
2
  • $\begingroup$ Have you looked at WeatherData which can retrieve lists of weather readings over various periods of time. Save can be used to store data in external files between sessions. $\endgroup$ Commented Nov 14, 2012 at 17:39
  • $\begingroup$ If you use WeatherData you'll need to make the stepsize All and parse out the hourly information (if it's there) since you can't ask for an interval less than a day $\endgroup$
    – ssch
    Commented Nov 14, 2012 at 18:21

3 Answers 3

4
$\begingroup$

WeatherData[] can give you what you want:

startdate = DateList[];
city = "Amsterdam"
startpoint = 5 (*hours ago: for amsterdam, the most recent data was not available...*)
range = 48 (*in hours*)
data = WeatherData[city,"Temperature", #] & /@ 
(DatePlus[DateList[], {-#, "Hour"}][[1 ;; 4]] & /@ Range[startpoint, range]);

For some reason WeatherData returns the temperature for every half hour, although I asked for hours, but you can delete the odd elements if you want to.

And finally: getting rid of the weird layout with timestamps embedded:

Select[Flatten[data, 2], NumberQ]

Result:

{12., 11., 13., 13., 11.9, 12., 12., 12., 12., 10., 11., 9.1, 9.,10., 9., 
9., 9., 9., 8.8, 9., 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.,9., 9., 9.4, 
9., 9., 9., 9., 10., 10., 9.6, 10., 10., 10., 10., 10.,10., 9.8, 10., 
10., 10., 10., 10., 10., 10.6, 11., 10., 11., 11.,11., 11., 10.1, 10., 
11., 10., 10., 9., 10., 8.7, 9., 9., 9., 9., 8.,8., 7.9, 8., 8., 8., 8., 
8., 8., 7.6, 8., 8., 8., 8., 8., 8., 7.5,7.,7., 7.6, 8, 8., 7.5, 7, 6.8, 7}

Explanation:

Range[startpoint, range]

This makes a list of all integers from startpoint to range.

(DatePlus[startdate, {-#, "Hour"}][[1 ;; 4]] &/@ Range[startpoint, range])

Then i Map[] (&/@ or look in the help files) over DatePlus[] to make a list of every single moment you want to get temperature data from in the form mma displays dates and times.

WeatherData[city,"Temperature", #]

Finally i Map[] the list over the WeatherData[] function.

Just change the values you want to enter and you should be ok without understanding the exact code ("Amsterdam" to "New York","Temperature" to "Humidity", etcetera)

Edit: added explanation.

$\endgroup$
9
  • $\begingroup$ WeatherData[city,"Temperature", #] & /@ (DatePlus[DateList[], {-#, "Hour"}][[1 ;; 4]] & /@ Range[startpoint, range]); can you explain this line as i m new to mathematica and have to accomplishe a task in short time. $\endgroup$
    – Soum
    Commented Nov 14, 2012 at 19:19
  • $\begingroup$ First: (DatePlus[startdate, {-#, "Hour"}][[1 ;; 4]] &/@ Range[startpoint, range]) This makes a list of every single moment you want to get temperature data from. $\endgroup$ Commented Nov 14, 2012 at 19:38
  • 1
    $\begingroup$ @Soum How about: "Create a pure function that finds the temperature in the named city and apply this function to the list produced by applying another pure function to build a list of date/time values starting 'startpoint' hours ago and going back through time for 'range' hours, using only the year/month/day/hour values." Something like that, anyway. If you're new to Mathematica, unravelling code like this isn't going to be easy - your best bet is looking at each function in turn, then building it up slowly. $\endgroup$
    – cormullion
    Commented Nov 14, 2012 at 19:40
  • $\begingroup$ Range[startpoint, range] This makes a list of all integers from startpoint to range. (DatePlus[startdate, {-#, "Hour"}][[1 ;; 4]] &/@ Range[startpoint, range]) Then i Map[] (&/@ or look in the help files) over DatePlus[] to make a list of every single moment you want to get temperature data from in the form mma displays dates and times. WeatherData[city,"Temperature", #] Finally i Map[] the list over the WeatherData[] function. Just change the values you want to enter and you should be ok without understanding the exact code("Amsterdam" to "New York","Temperature" to "Humidity", etcetera). $\endgroup$ Commented Nov 14, 2012 at 19:45
  • 1
    $\begingroup$ @cormullion: good one, added it to my answer. $\endgroup$ Commented Nov 14, 2012 at 20:05
1
$\begingroup$

The following command returns W|A's most recently recorded temperature at your location.

WolframAlpha["temperature", {{"Result", 1}, "ComputableData"}]

(* Out: {48, "(53 minutes ago)"} *)

To evaluate this periodically and store it in a a function value, use the *ScheduledTask commands. For example, the following creates a task which executes the above command every 30 seconds, starting at time 0, and stores the result in temperatureRecord[cnt++]. Sinct cnt is incremented each time through, the result is an easily accessible list of the temperatures

cnt = 1;
Clear[temperatureRecord];
task = CreateScheduledTask[
   temperatureRecord[cnt++] = WolframAlpha["temperature",
     {{"Result", 1}, "ComputableData"}], 30, 0];
StartScheduledTask[task];

After some time has expired, we can access the information like so:

temperatureRecord /@ Range[cnt]

(* Out: {{48, "(53 minutes ago)"}, {48, "(54 minutes ago)"}, 
 Missing["NotAvailable"], {48, "(55 minutes ago)"}, 
 {48, "(55 minutes ago)"}, {48, "(56 minutes ago)"}, 
 {48, "(56 minutes ago)"}, {48, "(57 minutes ago)"}, 
 {48, "(58 minutes ago)"}, {48, "(58 minutes ago)"}, 
 {48, "(58 minutes ago)"}, temperatureRecord[12]} *)

Note that the temperature might appear as Missing sometimes. If cnt has been incremented but the WolframAlpha command has yet to return, we'll get an undefined result, like temperatureRecord[12].

When done, we remove the task.

RemoveScheduledTask[task];

To get the "persistence" that you describe, you could Export the data you've created so far to a package file:

Export["temperatureRecord.m", temperatureRecord /@ Range[cnt]];

You could then place an Import statement at the top of the notebook and set the state based on that before continuing:

tempSoFar = Import["temperatureRecord.m"];
Do[temperatureRecord[k] = tempSoFar[[k]],
  {k, 1, Length[tempSoFar]}];
cnt = Length[tempSoFar];
$\endgroup$
1
0
$\begingroup$

I'm a bit unclear with what you want, but this command for example will give you the TimeSeriesData for Toronto over the last four days, in an hourly format. You could then save it in a variable and preserve it for future users?

torontoweather = {WolframAlpha["Weather in Toronto, last four days", {{"WeatherCharts:WeatherData", 1}, "TimeSeriesData"}]}

This will give you the list that you could subsequently just save in your notebook?

$\endgroup$
1
  • $\begingroup$ You can click that gray plus in the WolframAlpha output and select computable data to get that expression $\endgroup$
    – ssch
    Commented Nov 14, 2012 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.