B4A Library Astro library

The library provides the times of Sunrise, Sunset, Moon rise and Moon set, according to place and date.
It also provides the phase of the moon (equivalent to the day of the moon month).
The Sunrise and Sunset times vary according to the value of Zenith angle.
The following conventions are in use:
Official – 90.8333 (90° 50’)
Civil - 96°
Nautical - 102°
Astronomical - 108°
The values can be used by their names.
The library use the Official value as default, and it can be changed using the method Zenith.

No demo program as it is very simple to use.
Please report problems.

Edit: Version 1.1 corrected the following as per alfcen comments:
- no_rise or no_set time are returned as "--:--"
- sun rise and set times are within the 24 hours.

Edit: the files are updated to ver. 1.2 with additional methods which provide data about the sun and the moon, all by our astronomy expert alfcen:sign0188:
Edit: Ver 1.21 correction of rounding problem.
Edit: Ver 1.22 bug correction in sun + format change.
Edit: ver 1.23 bug correction in moon phase.
Edit: ver 1.4 added a method to calculate sun's position (azimuth and elevation).
 

Attachments

  • Astro 1.4.zip
    17.5 KB · Views: 624
Last edited:

corwin42

Expert
Licensed User
Longtime User
Thank you very much.

This reminds me that some time ago I wanted to write a service that will adjust the display brightness depending on twilight times so that if it gets dark outside the display slowly gets darker, too. Perhaps now I will do that. :sign0098:
 

alfcen

Well-Known Member
Licensed User
Longtime User
Astro Lib

Hello Derez,

Nice idea that simplifies a lot! Thanks very much!

Here are my findings:

1. Today's sunrise occurs here at 30:36 in that 24 hours need to be deducted to obtain the correct time (06:36).

2. For near-polar latitudes, times are output as sunrise: 0:0 and sunset: X,
and vice-versa, depending on hemisphere and summer/winter. This may require an explanation.

3. "No Moonrise/set this day" perhaps better provided as "--:--"

4. Moon phase, better labelled as "Moon Age", is 1.5 days off (shows 22 for now, but should be 23.5 days). Probably insufficient accuracy of sun and moon ecliptic longitudes in the algorithm. Since the jar file is rather small, I assume that perturbations are not accounted for.

5. Moon phase should be provided with at least one digit, such as 23.5, by adding hour and minute to the parameters.

6. Except for the 24 hour offset in sunrise, all rise and set times are quite accurate.

Cheers
Robert
 

derez

Expert
Licensed User
Longtime User
W

Robert I'll check and correct some of your comments. The moon phase algo is one for Hebrew calendar and results integers.
 
Last edited:

derez

Expert
Licensed User
Longtime User
Version 1.1 replaced the files in the first post.

To clarify my previous answer to Robert :
The algorithm I used is the algorithm for the Hebrew calander which works by the moon month. The algorithm to calculate it is quite old - more than a thousand years, and it gives the correct results with the definition of phase = 1 is that some part of the moon is visible after a complete darkness (which is phase 29 or 30).
The date is true for the whole day, so there are no fractions.
This may explain the difference between alfcen results and mine.

Robert - if you can provide me with the algorithm, I'll be glad to add it to the library.
 
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Sorry Derez, I did not realise that you were referring to the Hebrew calendar.
I sent you a PM regarding the algorithm.
 

alfcen

Well-Known Member
Licensed User
Longtime User
Hello Derez,

Just an idea, would it be possible to incorporate Zenith into the Sunrise/set function, such as

ast.Sun(18,3,2012,36.198,135.86,9,108)
(Day,Month,Year,Lat,Lon,zone,zenith)

In astronomy it is also often useful to privode Julian Date, such as

ast.Sun(2456005.0487,36.198,135.86,9,108)
(JD,Lat,Lon,zone,zenith)

or for B4A:
ast.Sun(DateTime.Now,36.198,135.86,9,108)
(Ticks,Lat,Lon,zone,zenith)

It is of course up to you and your time. Your rise/set
functions are already superb and accurate!

In case you are planning to further develop the Astro library then we could join
forces and possibly embrace further functions, such as

Maximum elevation of Sun/Moon
Current Zodiac position
Azimuth/altitude
Hour angle
Light distance (for the curious)
Occurance of next full and new moons (for fishers)
Golden hours (for photographers)
etc.

I do all these computations in Basic code, but with the aid of
a library, source code can be much simplified and probably run a bit faster.

Cheers
Robert
 

derez

Expert
Licensed User
Longtime User
Zenith and more

Robert
The Zenith is available as a separate method and you can set it either by using preset values (OFFICIAL, NAUTICAL etc) or by the angle directly:
ast.zenith = 103 ... or ast.zenith = ast.OFFICIAL ...
Is it important to have it directly in the sun method ?
for Julian date I have this : http://www.ben-daglish.net/moon.shtml , is it ok to use it ?

As for more functions - Of course. It is much more fun when there is a need for what you do and you have feedback. Just send me the code in basic or C . Thank you.
 
Last edited:

derez

Expert
Licensed User
Longtime User
version update

The files in the first post are updated to ver. 1.2 with additional methods which provide data about the sun and the moon, all by our astronomy expert alfcen:sign0188:
 

alfcen

Well-Known Member
Licensed User
Longtime User
AstroLib Demo

Here is a demo for those new to astronomy.
Please find explanations in the source code.
 

Attachments

  • astrolib-demo.zip
    352.6 KB · Views: 712
  • demo.jpg
    demo.jpg
    75.1 KB · Views: 799
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Very nice, derez, the time strings are looking good.
Thanks a lot!
 

boten

Active Member
Licensed User
Longtime User
edit: MY BAD!! Got my east & west confused.

Erez, I tried the astro lib. Used Jerusalem coords

B4X:
lng=-35.24
lat=31.77
zt = DateTime.GetTimeZoneOffsetAt(DateTime.Now)
r=ast.Sun(23,3,2012,lat,lng,zt)

and got 10:20 & 22:35 for sunrise/sunset.

What am I doing wrong?
 
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Hi boten

Eastern longitudes are positive, western are negative.
lng=35.24 is correct.
Make sure to compensate for DST if needed.
 

alfcen

Well-Known Member
Licensed User
Longtime User
Geocentric ecliptic longitude/latitude for planets are not currently included.
 
Top