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: 632
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
This is no "original" library.
Are you referring to: AstroLib
If so, this is copyrighted and may therefore not be wrapped for B4A.
 

Penko

Active Member
Licensed User
Longtime User
Hey, I am a little bit confused.

I want to get the sunrise and sunshine values. I understood I have to specify the date and the gps coordinates. However, what's the deltahours argument?

I thought it should be the timezone I am in and that's why I just passed DateTime.TimeZoneOffset to this.

Is that correct? I am asking because there is no explanation related to this argument, I didn't find it anywhere in the docs.
 

alfcen

Well-Known Member
Licensed User
Longtime User
Your assumption is correct. Pass your TZ plus DST if observed - both in units of hours.
 

Penko

Active Member
Licensed User
Longtime User
Hello Robert,

thanks for your reply.

My country is normally GMT+2 but it observes the daylight saving, so it is currently GMT+3.

DateTime.TimeZoneOffset does indeed return "3" at the moment. The question is if this is reliable enough. In a couple of months, the clock will be one hour back and I wonder if Datetime.TimeZoneOffset would return "2" at that moment.

The problem is that I want to auto-detect the current timezone reliably and pass the argument to the Astro library. The only method I found is exactly Datetime.TimeZoneOffset but probably there is another approach.

I am using the GPS so it is important to auto-detect the timezone rather than making the user select it.

So, what do you think?
 

alfcen

Well-Known Member
Licensed User
Longtime User
Hi Penko,

When it comes to DST, I can't join the discussion because DST is not observed in Japan, for which reason I always provide manual DST setting that will be saved. This is no trouble at all even for spoiled users.

Looks like your cell provider/phone switches DST automatically, which may not be the case for all but I really can't tell.

In B4A, GPS time is always returned in my local time, but I can't check this against DST.

Conventionally, I am employing:
zt = DateTime.GetTimeZoneOffsetAt(DateTime.Now) to determine the time zone, but I cannot ascertain whether this would take DST into account.
 

Irvin Banta

New Member
Licensed User
Longtime User
thanks for the library!...error though

I'm a nuub here...

B4X:
Dim sunrise As Astro
Log(sunrise.Sun(20, 4, 2013, lblStoredLat.Text, lblStoredLon.Text, -06))

this is what i get in the Log:
[Ljava.lang.String;@424b9b88

I've tried a bunch of different ways, even using the GPS coordinates directly...but still to no dice.

does this library still work?

thanks!
irvinbuffalo:sign0104:
 

alfcen

Well-Known Member
Licensed User
Longtime User
Astro

Irvin,

You cannot pass string values, but doubles only.
Besides, this function returns a string array.

Example:
Dim sunrise As Astro
Dim riseset() as String
Dim lng, lat as Double
lng = -122.123: lat = 35.543 'or use GPS variables

and then,

riseset = sunrise.Sun(20, 4, 2013, lat, lng, -06)
Log("Sunrise: " & riseset(0))
Log("Sunset: " & riseset(1))
 

Irvin Banta

New Member
Licensed User
Longtime User
Perfecto!

Thank you for the reply!

Perfecto! Boom!

knew all I had to do is ask, thanks so much!

:sign0098:
 

wm.chatman

Well-Known Member
Licensed User
Longtime User
I have tryd to get the hours, but I am to dumb :)
Could someone maybe help so I am able to calculate the Sun hours/Moon hours and show the output with label1 and label2.

At my Location the sunrise/set is: 08:12/16:36 = 8.24 h sun
At my Location the moonrise/set is: 12:39/03:53 = 15.14 h moon

searching brought no results on forum...
 

derez

Expert
Licensed User
Longtime User
What alfcen wrote above is exactly what you need:
Example:
Dim sunrise As Astro
Dim riseset() as String
Dim lng, lat as Double
lng = -122.123: lat = 35.543 'or use GPS variables

and then,

riseset = sunrise.Sun(20, 4, 2013, lat, lng, -06)
Log("Sunrise: " & riseset(0))
Log("Sunset: " & riseset(1))
and for the moon do the same:
riseset = sunrise.Moon(20, 4, 2013, lat, lng, -06)
Log("Moonrise: " & riseset(0))
Log("Moonset: " & riseset(1))
 

wm.chatman

Well-Known Member
Licensed User
Longtime User
sun / moonrise works, everything is giving the correct data outout. Only with the moonset there are one slight problem.
Moonset is 04:52 and is at my location at 05:43 !
Why the 1+ hour difference for the moonset ?

All other data output is correct.
 
Last edited:

derez

Expert
Licensed User
Longtime User
Have you put the correct time zone difference ? what is your time zone ?
Please tell me the parameters you put into the moon method.
 
Last edited:

wm.chatman

Well-Known Member
Licensed User
Longtime User
this is all I have.

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim gb, gl, zt As Float
Dim rad As Double: rad = 180 / cPI
Dim ast As Astro
Dim sunrs() As String
Dim monrs() As String
Dim panRS As Panel
Dim imvPhase As ImageView
Dim labMoonRS As Label
Dim labSunRS As Label
Dim imgSun As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("layout_riseset")
gb = 50.06 'Your true coordinates here
gl = 10.9
zt = DateTime.GetTimeZoneOffsetAt(DateTime.Now)
RiseSet
'50.083 (Breitengrad) 50.6 my Location
'10.933 (Längengrad) my Location
'Last gb = 50.6
'last gl = gl = 10.95

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub RiseSet
Dim m As String
Dim mdata(5) As Double
Dim dd, mm, yy As Int
dd = DateTime.GetDayOfMonth(DateTime.Now)
mm = DateTime.GetMonth(DateTime.Now)
yy = DateTime.GetYear(DateTime.Now)
m = "Sun" & CRLF
sunrs = ast.Sun(dd,mm,yy,gb,gl,zt)
m = m & "Rise: " & sunrs(0) & CRLF
m = m & "Set: " & sunrs(1) & CRLF & CRLF & CRLF & CRLF & CRLF
labSunRS.Text = m
mdata = ast.GetMoonData(dd,mm,yy,gb,gl,zt)
imvPhase.SetBackgroundImage(LoadBitmap(File.DirAssets,"phases/" & Round(mdata(1)) & ".png"))
monrs = ast.Moon(dd,mm,yy,gb,gl,zt)
m = "Moon" & CRLF
m = m & "Rise: " & monrs(0) & CRLF
m = m & "Set: " & monrs(1) & CRLF
m = m & "Age: " & Round2(mdata(1),2) & CRLF
m = m & "Phase: " & Round(mdata(0) * rad) & Chr(176) & CRLF
labMoonRS.Text = m
' panRS.BringToFront
' panRS.Visible = True
End Sub

Sub panRS_Click
panRS.Visible = False
imvPhase.Visible = False
imgSun.Visible = False
Activity.Finish
End Sub
 

derez

Expert
Licensed User
Longtime User
I took your coordinates
gb = 50.06 'Your true coordinates here
gl = 10.9
and ran the program from 10/1 to 14/1 with zt = 1, in comparison to the results from this http://aa.usno.navy.mil/data/docs/RS_OneYear.php
(attached here) and they are the same except for 1 minute in one case.
You have to compare your results carefully as the difference from one day to its following day is about 40 minute.
App log:
date = 10/1/2014
Sun
Rise: 08:13
Set: 16:35
Moon
Rise: 12:38
Set: 02:52
date = 11/1/2014
Sun
Rise: 08:12
Set: 16:37
Moon
Rise: 13:15
Set: 03:55
date = 12/1/2014
Sun
Rise: 08:12
Set: 16:38
Moon
Rise: 13:56
Set: 04:52
date = 13/1/2014
Sun
Rise: 08:11
Set: 16:40
Moon
Rise: 14:43
Set: 05:44
date = 14/1/2014
Sun
Rise: 08:10
Set: 16:41
Moon
Rise: 15:35
Set: 06:30
 

Attachments

  • times.png
    times.png
    11.8 KB · Views: 220
Last edited:

wm.chatman

Well-Known Member
Licensed User
Longtime User
so you are saying this is correct, I just dont get it David. Everywhere else I look I get moonset at 06:29 for todays date. But on Samsung S2 I get the -40 minutes :(
 
Last edited:

derez

Expert
Licensed User
Longtime User
The Samsung gives the correct time for today 13/1 . Check the date in "Everywhere else".
Also it depends on how you refer to rise and set - the library and the site data give the times for rise and set on that day. You might think that after the moon rised in the evening, what is its set time which is the next morning...
 
Last edited:

wm.chatman

Well-Known Member
Licensed User
Longtime User
by "Everywhere else" I meant the Internet, sorry forgot to write that down.
and I only use this on samsung, nowhere else.
You stated in post 36 > ran the program from 10/1 to 14/1 with zt = 1 <
do I change the zt = DateTime.GetTimeZoneOffsetAt(DateTime.Now) to zt = 1 ?
I an having a slight problem understanding this bit. Was going to set link to source app, since to large for uploading it. :(
 
Last edited:

derez

Expert
Licensed User
Longtime User
I put 1 since I live in zone 2 so the above statement would result 2 instead of 1, which is what you should get. you don't have to change.
 
Top