# Why is AstronomicalData so slow?

I'm having trouble getting some pretty simple functions using AstronomicalData to perform at anywhere near the speed I need them to.

For example, I have many places where I need to compute something like

ParametricPlot[
{AstronomicalData[
"Mercury", {"RightAscension",
DatePlus[Date[], d], {$GeoLocation[[1]] ,$GeoLocation[[2]]}}], d},
{d, 0, 365}, AspectRatio -> 1/1.6]


but find that it takes far to long for even this simple fragment to run. I want to put several such fragments (e.g. one for each of several astronomical objects) into a single figure, and even to dynamically change the parameters for the figure (such as location and date) inside a Manipulate, which would be prohibitively slow given the times I'm seeing with even the simple fragment above.

Why is AstronomicalData so slow? Is there something I can do to speed it up so I can use it as I hope to?

-
Looks like you're downloading data from the internet for every point... perhaps preloading the data to your disk might help. I don't have Mathematica to test it right now –  rm -rf Oct 8 '12 at 3:47
Just to make sure: have you done what @rm -rf has suggested (i.e., executing AstronomicalData[All, "Preload"] before doing anything else)? –  Ｊ. Ｍ. Oct 8 '12 at 5:07
@J.M.: Yes, but the plot still takes the same time. Perhaps that only loads data for the current day? –  raxacoricofallapatorius Oct 8 '12 at 20:27

Once can see by using Trace that an apparently simple call is far more complex than you might imagine. Why it is designed this way, and if it needs to be, is an entirely different matter.

Be prepared to forcefully terminate Mathematica (or at least Quit[] the kernel) and then run:

AstronomicalData["Mercury",
{"RightAscension", {2012, 10, 11, 12, 13, 14}, $GeoLocation} ] // TracePrint  A lot more going on behind the scenes than a simple database look-up, eh? - Yes, that "entirely different matter" keeps coming up with Mathematica. One wonders whether there is any thought given to use cases or architecture at all. – raxacoricofallapatorius Oct 11 '12 at 3:17 @rax Oh, I'm sure thought has been given. I wish that they'd be more transparent about the limitations of the design, trade-offs made, and places they simple haven't optimized well (Calendar package, PlotLegends, etc.). I didn't even start trying to look through the massive Trace output above, but I'd guess from the length of it that Mathematica is calculating the information requested (from more fundamental data) rather than simple doing a database access. That may be a reasonable trade-off for certain applications, yet cause trouble for yours. – Mr.Wizard Oct 11 '12 at 3:22 (Sigh.) Well, in any case, caching what I need is a serviceable, if inelegant, solution. – raxacoricofallapatorius Oct 11 '12 at 3:26 One way to overcome this issue is simply to define functions to store the data, so that it AstronomicalData is only needed once for each value, using something like astroData[datum_, object_, dayoffset_, location_] := (astroData[datum, object, dayoffset, location] = AstronomicalData[object, {datum, DatePlus[Date[], dayoffset], location}])  Then the data can either be preloaded, with something like astroData["RightAscension", "Mercury", #,$GeoLocation] & /@ Range[0, 365];


and will in any case accumulate through use so that execution eventually becomes quite speedy.

The example above becomes

ListPlot[{astroData["RightAscension", "Mercury", #, \$GeoLocation], #} & /@ Range[0, 365],
AspectRatio -> 1/1.6, Joined -> True]

-
+1 for sharing your solution. –  Mr.Wizard Oct 11 '12 at 3:27

rm -rf is right:

AstronomicalData["Mercury"]


then execute your command. It takes about 25 seconds on my computer.

-
Sorry, no, see above: this needs to run inside a Manipulate, so 25 seconds is way too slow. And in any case, running AstronomicalData beforehand makes no difference in performance (at least not for me). –  raxacoricofallapatorius Oct 8 '12 at 4:11
@rax: Worse comes to worst, you might have to roll your very own ephemeris... :| –  Ｊ. Ｍ. Oct 8 '12 at 5:08
Have you actually tried this? This doesn't do anything at all, it just returns an empty list { }. But it does it fast! :-) –  stevenvh Oct 8 '12 at 7:05
The first time I run this - at the start of every Mathematica session - I get the "Initializing indices..." messages for about 10 seconds. Then the expression returns the string "Mercury". Subsequent calls for the expression in that session are immediate. But the parametric plot still takes 15 seconds... –  cormullion Oct 8 '12 at 12:25