I don't know the answer and don't care to try, but if I did, I would use Frink[1] for this task. Seems the perfect tool for these kinds of problems. Hell, I bet if you posted on the mailing list, Alan Eliasen would happily solve this problem for you - seems like the kind of problem he likes to solve on the mailing list.
[1] http://futureboy.us/frinkdocs/