Quest for the Perfect Moon Widget

You may have noticed that as of this moment there are three different moon phase widgets over on the sidebar. None of them are perfect, alas (although the Japanese one is perfectly inscrutable). I looked around at other WordPress widgets and did not find one that gave out all the information I was interested in (especially for the eclipse) and was aesthetically pleasing. I thought I might spend a few hours and make my own.

The design was very simple. I would write a little Flash thingie that read XML data from a server and draw the moon with great precision and also look nice doing it. In addition I could put numerical readouts for more interesting (to me) numbers. Piece of cake.

I started my quest looking for a server with current moon info. The US Naval Observatory has all sorts of lunar data available, presumably calculated with far greater precision that I will ever need. The only problem is, they didn’t have data for right now. They had almanac generators and whatnot, but nothing that I could ping and get back a message that said, “at this moment, the moon is…” I couldn’t find anything at NASA, either. I broadened my search and found that nobody seems to be providing this service. “fine, then,” I thought. “I’ll make my own moon server. I’m sure there are plenty of places I can find algorithms for calculating this stuff.”

Only, that didn’t turn out to be so simple, either. The motion of the moon is incredibly complex. There exists a thing called ELP 2000-85 which is the latest attempt to make the math match what the moon actually does. What the thing does is loop through a set of calculations a bazillion times, each time with tweaked coefficients that make smaller and smaller corrections to the calculation. Compiling the tables of coefficients must have been a real pain in the butt. Refining the tables is still ongoing. The accuracy of your calculation comes down to how many times you loop through the coefficients before you decide that the computer power is better used for something else.

Nobody in their right mind would actually use all the tweaks in the ELP 2000 for anything as simple as a moon phase widget, or, for that matter, a moon landing. Along came a guy named Jean Meeus, who published a book full of handy formulas for calculating where things are going to be. He includes simplifications of the ELP 2000 (only looping through 64 iterations), and while they’re not as precise, they’re pretty damn good. I don’t have that book, either.

Time wasted so far: 3 hours. Completion of widget: 0%

But now my search began to bear fruit. I didn’t have Meeus’ formulas, but other people did, and had written software. I found some open-source code that implemented some of his stuff. Yay! I implemented the code, moving it from c to PHP so I could run it on my server. After a few routine hitches the code was up and running and telling me just where the moon was, relative to the Earth, accurate to a couple of arcseconds.

Time wasted so far: 6 hours. Completion of widget: 5%

Unfortunately, it didn’t tell me anything else. This particular code did not provide any information that required data about the sun — like, say, the phase of the moon. Harrumph. Back to the Internet I went. Fairly quickly I found some different code, this time in JavaScript, that also cited Meeus. It was much, much, simpler, ignoring many of the more difficult-to-calculate corrections, but I figured that the first code sample had already done most of that. It was simply a matter of adding the new code to what I already had. Naturally, despite having the same source reference, all the variable names were completely different.

After a great deal of forensics (that’s a big word for ‘wasted time’) I established which quantities I had accurate versions of and which I still needed to calculate. I got everything set up and ran some tests. The results were not good.

Time wasted so far: 12 hours. Completion of widget: 3%

I had expected some problems like this – perhaps in one body of code an angle was expressed in degrees and the other expected radians. Things like that. I started working through things. Only after another day of head-scratching did I test the code I’d based the second half of my project on. It was wrong. So there I was with Frankenstein’s monster of code sewn together from different sources, and one of the sources was broken before I even started. Sigh. Back to the drawing board.

Time wasted so far: 20 hours. Completion of widget: 2%

I should mention along in here somewhere that there are people who sell moon software for quite a bit of money. My little server could potentially put a dent in their sales by bringing accurate calculations to anyone who asks, but its not really the calculations they are selling, but the application around it. I’m not too worried for them.

Back to the Web and by now I was getting better searches because I knew the key terms to look for. I found two more code examples, both of which take precision to the most extreme available. One is a complete implementation of the ELP 2000-82b. This honey consists of 36 files with tables with hundreds of rows of numbers, and a sample program in Fortran that shows how to use them. For ridiculously accurate calculations, I couldn’t do much better. But… It only calculates the position of the moon, just like the first code I implemented. I’d still need to work out the phases and whatnot.

The other code I found is based on earlier math, but really concentrates on what an observer would see from a given point on the Earth. It includes corrections for the optical effects of the atmosphere and for the friggin’ speed of light. It’s got a lot of stuff I don’t need (other planets, for instance), but it has everything I’d be looking for. The thing is, the code is horrible. It’s in c, and the writer apparently never heard of parameters or returning values. Or structs, or anything else that might help organize the information. It is impossible to read a function and know what it does or where all the numbers it uses come from. It would be a big task to translate the pieces I need, mainly because it’s very difficult to tell which pieces I need. Still, it’s an option.

Time wasted so far: 24 hours. Completion of widget: 3%

And that’s where I stand. You know, maybe I’ll wait until I’m on a boat full of moon geeks. I bet one of them even knows a Web site that gives current moon data.

1

24 thoughts on “Quest for the Perfect Moon Widget

  1. despite nothing to show so far, color me impressed.
    Too bad about the one in C. I hate poorly commented and organized programs.
    Be fun to look at what the Apollo program did with vacuum tubes n’ sich. I guess all they had to do was get close, and let the astronauts steer the rest of the way.
    I’ve heard of, but never seen, a bumper sticker that says, “Real men program in Fortran.”

    • I’ve spent some time tackling the C code, converting it to PHP. I’m close to having it working, but if the answers don’t come out right I’m going to have trouble. Right now I’m getting my laptop set up so I can monkey with it on the boat.

  2. I didn’t mention the very small amount of time I spent looking for a 3d moon globe that I could tilt correctly and turn by about 5 degrees.

  3. Yeah, I have no idea how it works. The fortune may be based on the day of the week (when did the 7-day week reach Japan?) or for all I know it’s just random with a very small pool.

    I think my ultimate moon widget must also have a fortune option. Writing a large collection of oddly compelling and wacky fortunes would be fun. Something like:

    “The moon is watching you, like a big eye hanging there in the sky, recording your every move. Better put off doing that thing for a while.”

      • I’m getting threading, and gravatars, too, in the popup comment window. I first noticed the gravatars a couple of days ago, and I assumed you’d twiddled with the code.

          • That’s the default place for leaving comments on WordPress; the popup window is an old-school feature that is not really supported anymore (although they don’t actually come out and tell you that). New features like threaded comments assume that you are on the single-item page rather than in the popup. I’m not sure yet whether I’ll be able to fix it.

  4. A quick look at the Japanese widget’s code and a brief Google brings us to the six-day Buddhist week. There are six cryptic fortunes in all, so the staying power of the widget is limited.

  5. Only six. But they’re baffling enough that they might not generate very good recall. I could list them here, but that would spoil it for the viewing audience. It seems it would have been a pretty small job to get someone to translate them better, so I can only assume the author liked the charm of the incomprehensibility.

  6. I’m wondering whether the translation is done by computer. I’ve seen similar crypticness (cripticality?) when immigrant students of mine write a paper in their own language and then run it through one of the translation widgets available online, then turn it in without proofreading.

  7. I’m sure is was, but with only six fortunes, you’d think the person could have found a human translator pretty easily. The comments in the code demonstrate enough proficiency to make me believe the author knows that the translations are wacky.

  8. in my studies about remote viewing I came across this site for local sidereal time calculations. There’s all sorts of other astronomical info there in addition to guitar tabs and chords. Maybe this site will help you with the moon widget? http://astroguitar.cjb.net/

    Curious as to Muddled Ramblings visitors’ favorite planet. My personal vote is ‘the Sun!’, in honor of Harry Caray.

  9. Did you look and see if there’s a google widget? I’m slowly plugging away at figuring out how to make a similar iPhone App. Going from Actionscript to object C hurts.

    • I’ve been working on a PHP backend to do the heavy calculating, but I haven’t done much with the Flash client yet. The code I have calculates the moon stuff to ridiculous accuracy. I wouldn’t have too much trouble moving the thing to Objective-C now, and it would out-moon-widget any moon widget ever widgeted, I’m confident of that.

      • The funny thing is, I’ve been working on a site for someone that does astrology and she wants moon widgets, or any astrology related widgets. So widget Jerry, widget!

        • Getting closer. MoonServer is almost ready, and the location-dependent stuff (which will be in the widget client) is mostly worked out. Location is important; as we all know atmospheric refraction is greater at low altitudes, and even the portion of the moon you can see depends on which side of the Earth you’re on. I was very happy to discover today that Google Earth provides geodetic latitude and longitude based on WGS-84.

          Yep. That was exciting.

          I just have to get libration and axial tilt figured out. I think there are pretty simple formulas for them, given all the other numbers I already have.

Leave a Reply to Jesse Cancel reply

Your email address will not be published. Required fields are marked *