Astronomy pages using pyephem

737 views
Skip to first unread message

stumpey

unread,
Dec 22, 2011, 3:06:06 AM12/22/11
to Weewx Weather Station Discussion
Has anybody written any pages using pyephem for use as an astronomy
page on a website.?
If you have would you be prepared to share your coding?.

Some of my regular site visitors are looking for information on
planets in the night sky and their positions, I am sure they can find
this info elsewhere but I always like to drive traffic towards my site
if I can ( no financial gain just the pleasure of knowing somebody
else looks at my pages other then me ^_^ ).

If not then I will sit down and bash out some code in the near future
when the better half is out working and my girls are out with their
boyfriends and not nagging me for money or a free taxi..!

Regards
Steve

www.sunburyweather.co.uk

Thomas Keffer

unread,
Dec 22, 2011, 9:34:06 AM12/22/11
to weewx...@googlegroups.com
weewx has built in information for the Sun, Moon, Mars, Venus and Jupiter. The templates take advantage of only the first two, but you can add almanac information for the last 3 by following the pattern. No coding necessary.

But, if it's code you want, take a look at the file bin/weewx/almanac.py. It's where pyephem is used.

Cheers,

-tk


--
You received this message because you are subscribed to the Google Groups "Weewx Weather Station Discussion" group.
To post to this group, send email to weewx...@googlegroups.com.
To unsubscribe from this group, send email to weewx-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/weewx-user?hl=en.




--
Thomas Keffer

Email: kef...@threefools.org
Skype: tkeffer
+1 541-386-8891 (h)
+1 541-490-9507 (c)

stumpey

unread,
Dec 23, 2011, 3:10:30 AM12/23/11
to Weewx Weather Station Discussion
Thanks for the info will take a look over the Xmas period

Steve

René Schulze

unread,
Nov 9, 2015, 6:45:43 PM11/9/15
to weewx-user
Sorry for pushing up this old thread.

I have some questions about the almanac.py. Where do you define whitch bodies pyephem has to calculate. You wrote there are informations for Sun, Moon, Mars, Venus and Jupiter but i did'nt find all of these in the almanac.py. Is there an other file where the celestial bodies are defined or can i simply use in the template file for example $almanac.mercury.rise to define it? Sorry, i never used python before maybe i did'nt understand the code.

Greetings from Germany René

gjr80

unread,
Nov 9, 2015, 10:50:14 PM11/9/15
to weewx-user
Hi,

$almanac.mercury.rise will work fine, there is no need to define anything else, pyephem has everything it needs to calculate the position and rise/set times for Mercury (and any other celestial body pyephem is aware of). I have a page for rise/set times and alt/az for each planet and there was no need to define or setup anything, just inclusion of numerous $almanac.xxxx.zzzz tags in a weewx report template (xxxx=planet name, zzzz=rise, set, next_rising, next_setting, alt, az etc).

Gary

René Schulze

unread,
Nov 14, 2015, 6:41:13 PM11/14/15
to weewx-user
Hi,

thanks a lot. It works fine for all planets. Now i have also an complete almanac page for all planets. Then i testet another thing but it doesnt work or maybe my syntax is incorrect. I tested $almanac.star('Rigel').rise. That doesnt work, on the page is the var given as a text. I think it is not supported by the alamac.py because the is a formatter who capitalizes the planet name. In pyephem the syntax for mars and for stars is the following:

{
m = ephem.Mars()

n = ephem.star('Riegel')
}

The function seems to be case sensitive and there ist the problem. The almanac.py convertes  the $almanac.star('Rigel') to ephem.Star('Rigel')().


I would place the request of a function for "calculating other things than given bodies like planets or moons" into the wishlist for future development.

Greetings from Germany René

Thomas Keffer

unread,
Nov 14, 2015, 7:54:34 PM11/14/15
to weewx-user
Hi, Rene

pyephem is a terrible example of Python programming. Inconsistent use of names and mixing of abstractions is among its many sins. While your analysis is spot on, it's turning out to be a bit more complicated.

While pyephem accepts the shorthand syntax

ephem.Mars(observer)

when computing ephemeris for planets, it does not with stars. Instead, one must do

rigel = ephem.star('Rigel')
rigel.compute(observer)

Similar, but not the same. 

The right thing to do is probably switch to the latter syntax for all heavenly bodies, but that's going to require some head scratching...

If you're a Python whiz, feel free to give it a try.

-tk



--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

René Schulze

unread,
Nov 14, 2015, 8:26:37 PM11/14/15
to weewx-user
Hi Tom,

Sorry, i never coded something in Python. In the past i used only c, php, java(script) and html. And now, after many years of work, i go to an evening school for graduation as a state-certified technican. But what do we learn there... html, php, java... c. Python syntax  seems to be simple but it's differently to the c syntax languages. Maybe it's possible to use pyephem directly from php with FastCGI but first i should find out how can i do this. In this time i'm so busy with school and work. Thats why i was searching for an easy way like using weewx almanac

René

Thomas Keffer

unread,
Nov 19, 2015, 9:12:32 PM11/19/15
to weewx-user
René,

Well, I thought it was going to be a quick project, but it quickly led down a rabbit hole.

A few hours of work, and almanac.py is now much simpler and more capable. You can now ask for ephemeris of stars, as well as the Sun, Moon, and planets. For example,

$almanac.rigel.rise
or 
$almanac.rigel.transit

Also much easier to understand. No more fancy Python embedded classes, etc.

This will appear in V3.3, or, if you want it now, you can download almanac.py from GitHub.

-tk

René Schulze

unread,
Nov 20, 2015, 10:41:29 AM11/20/15
to weewx-user
Hi Tom,

thanks a lot. I will try to program an astronomical subsite on my weather homepage in next time, so if i have some.

Greetings René

René Schulze

unread,
Nov 20, 2015, 1:00:02 PM11/20/15
to weewx-user
Hi Tom,

I did a short test of the new almanac.py. First i got an error in the message log:

Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****  Traceback (most recent call last):
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****    File "/usr/share/weewx/weewx/cheetahgenerator.py", line 294, in generate
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****      print >> _file, text
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****    File "/usr/lib/python2.7/dist-packages/Cheetah/Template.py", line 1005, in __str__
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****      rc = getattr(self, mainMethName)()
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****    File "_etc_weewx_skins_Standard_almanac_html_tmpl.py", line 992, in respond
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****    File "_etc_weewx_skins_Standard_almanac_html_tmpl.py", line 476, in __errorCatcher59
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****    File "<string>", line 1, in <module>
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****    File "/usr/share/weewx/weewx/almanac.py", line 340, in __getattr__
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****      ephem_body = _get_ephem_body(self.heavenly_body)
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****    File "/usr/share/weewx/weewx/almanac.py", line 413, in _get_ephem_body
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****      ephem_body = ephem.star(cap_name)
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****    File "/usr/local/lib/python2.7/dist-packages/ephem/stars.py", line 124, in star
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****      star = stars[name].copy()
Nov 20 17:53:35 unimatrix-rasp weewx[31850]: ****  KeyError: 'Star'

The reason was that i had only the planets in my almanac.html.tmpl. The section with star rigel was marked as a comment. Then i set the rigel section active and the error goes away. The rigel rise, set and transit is now calculated.

I'm now confused. I read the sourcecode of almanac.py and if i understand this correctly the code do the following:

First you ask for heavenly_body = ephem.Heavenly_body() and if there is an exception you ask for heavenly_body = ephem.star('Heavenly_body') but ist seem the script always ask for a Star. If no Star given in the template raises an exception an the html file is not generated. There's is another wiggy thing. The index.html contains also a section for almanac, the sun and the moon, but not a star. This file is correctly generated. If you would test it look at the attachment. If i mark lines 299 - 313 as comment in the almanac.html.tmpl i get the errors an the almanac.html was not generated.


Greetings René



Am Freitag, 20. November 2015 03:12:32 UTC+1 schrieb Tom Keffer:
template.zip

Thomas Keffer

unread,
Nov 20, 2015, 9:19:28 PM11/20/15
to weewx-user
The template almanac.html.tmpl generated without problems for me (attached).

Let's take a look at the function _get_ephem_body()

def _get_ephem_body(heavenly_body):
    cap_name = heavenly_body.capitalize() 
    try:
        ephem_body = getattr(ephem, cap_name)()
    except AttributeError:
        ephem_body = ephem.star(cap_name)

    return ephem_body


Assume that heavenly_body is Mars.

Then

getattr(ephem, cap_name)

evaluates to

ephem.Mars

which works fine, so an exception is not raised. So, the block 

ephem_body = ephem.star(cap_name)

is not executed. Instead, flow goes to the return statement.

Conversely, assume heavenly_body is Rigel. Then

getattr(ephem, cap_name)

evaluates to

ephem.Rigel

which raises an AttributeError exception. So, now flow goes to the statement

ephem_body = ephem.star(cap_name)

which evaluates as

ephem_body = ephem.star('Rigel')

Hope that makes it clearer.

-tk

almanac.html

René Schulze

unread,
Nov 21, 2015, 6:53:28 AM11/21/15
to weewx-user
Hi Tom.

thanks a lot again. After a few hours of thinking i got it. Now i understand how it works step by step. It was my problem. Look:
Yesterday i did the test with the new almanac.py but the the template was the old. I didn't realize that comment markers will be ignored by cheetah. Here an excerpt of my old template

<--
                <tr>
                  <td class="celestial_label">Aufgang:</td>
                  <td class="celestial_data">$almanac.Star('Rigel')l.rise</td>
                </tr>
-->

This code raises an exception because the generator ignores the comment markers. The code between the markers will be always processed. Then i set the right syntax in the template and it works fine. I didn't test the comment markers again so i don't realize the the old wrong code in the template was the problem. Sorry i should more thinking than writing.

Greetings René 

vince

unread,
Nov 21, 2015, 11:19:56 AM11/21/15
to weewx-user


On Saturday, November 21, 2015 at 3:53:28 AM UTC-8, René Schulze wrote:
Yesterday i did the test with the new almanac.py but the the template was the old. I didn't realize that comment markers will be ignored by cheetah. Here an excerpt of my old template

<--
                <tr>
                  <td class="celestial_label">Aufgang:</td>
                  <td class="celestial_data">$almanac.Star('Rigel')l.rise</td>
                </tr>
-->


Those aren't valid comment markers for html - the top one is missing the exclamation point.
 
Should be:

<!--
    things to skip
-->


This code raises an exception because the generator ignores the comment markers. The code between the markers will be always processed. Then i set the right syntax in the template and it works fine. I didn't test the comment markers again so i don't realize the the old wrong code in the template was the problem. Sorry i should more thinking than writing.



I think the code was raising an exception because you had invalid html in the template.
 

Thomas Keffer

unread,
Nov 21, 2015, 11:21:05 AM11/21/15
to weewx-user
Cheetah uses double hash marks ('##') to delineate comments.

HTML uses <!--    -->. Cheetah knows nothing about HTML.

-tk

René Schulze

unread,
Nov 21, 2015, 11:56:40 AM11/21/15
to weewx-user
Hi Vince

Yes of course <!-- and --> is the right syntax of html comment. It was a write error in my post.

@Tom Yes this i found out last night. I added some other stars to the template and for one of them i used the german name. The same error raises again and in the line of KeyError i found the wrong star name. That opened my eyes and it was clear why the error raises in my first test.

Greetings René
Reply all
Reply to author
Forward
0 new messages