Sun amd Moon data for GPS Programs

Discussion in 'Share Your Creations' started by derez, Apr 1, 2009.

  1. derez

    derez Expert Licensed User

    Sun and Moon data for GPS Programs

    The attached is a module to be attached to GPS programs.
    Given the coordinates and date from the GPS it calculates the Sunrise and Sunset times, the moonrise and Moonset times and the moon phase for that date with a photo of the moon.

    The algorithms are translations of what I found in the net, except for the algo of the moon phase for which I used part of the algorithm of date translation from Gregorian Calendar to Hebrew calendar, and this is the most accurate algorithm.

    (If you are interested in the Hebrew calendar - ask me for the full algo).

    The main module in the attachment is just a tool to run the module, instructions for the integration appear in the header of the module.
    There are provisions for use with either GPS.dll or GPSDriver.dll

    The photos should be put with their "moons" directory next to the application.

    Happy navigation ! :)

    Updated to ver 2, to make the module independent of the type of GPS library.

    After Alfcen remark, I updated it to ver 3 to be able to work independently of any other gps related code, with gpsdriver.

    Edit: see in #16 updated and corrected astro.bas module, after bugs found by Agraham and Jony. Thanks.

    Attached Files:

    Last edited: Oct 4, 2009
  2. Standa

    Standa Member Licensed User

    Thank you very much. Algorithm in your example is first after long time, what is really working :). I write program using your example. I hope in two days I put it here :).
  3. alfcen

    alfcen Well-Known Member Licensed User

    Hi David,
    I've been absent from the forum for a couple of months and was excited at
    finding your Sun and Moon application (which I haven't checked yet as I
    can't get a GPS fix right now - not a problem of your program).

    At the time when the GPSDriver.dll became available I have written a similar application purely with astronomy and Google Earth in mind, which you can see and download here:
    alfcen - gps astro

    As soon as I get my GPS running I will get back to you. Perhaps we can exchange a few thoughts without going too deep into the algorithm.

  4. derez

    derez Expert Licensed User

    Welcome back Robert, I wondered what has happened to you, not responding to my cry for help

    but as you see I found the solution.

    I am waiting your (and anyone elses) comments and ideas for improvements.

    Checked your program vs mine:
    In yours sunrise is 06:07 while at mine is 06:12
    sunset is 19:14 vs 19:08
    It might be due to different definitions of rise and set, as angles above horizon.
    Last edited: Apr 19, 2009
  5. wm.chatman

    wm.chatman Well-Known Member Licensed User

    :)Hi Mr. Derez

    First I would like to thank you again for the felp on the SR/SS matter.
    After a few changes and trys and reworking the code with the help from Klaus, it is working at 99.9 %. Plus minus 1 min.
    As you know i am new with the GPS Algos., and new user Basic4ppc. But, not new with using a Compass out in the Field. I think you are correct when you think that this has something to do with different definitions of rise and set, as angles above horizon. I do not think there is a 100% Algo for SR/SS and MR/MS. Had a chance to few different source code on this.

    take care and best Regards,
  6. alfcen

    alfcen Well-Known Member Licensed User

    Hello David

    Couldn't test yours. After selecting GPSDriver, all I get to is a [Astro] button.
    No effect on click. GPS up and running. Sure I missed something:)

    My algorithm takes into account atmospheric refraction and altitude
    as well as the sun's apparent diameter at its current distance.
    Sunrise and set is when the upper limb of the sun's disk is coincident
    with an ideal horizon.

    I checked it with a local tide calendar. Rise is +/- 2 min, set is pinpoint.
    The same algorithm is pinpoint also for rise in a VB6 app.
    Need to look further into this.

  7. derez

    derez Expert Licensed User

    It didn't work for you because you have not completed all gps code which is required. My comment " assuming..." meant that you have to complete it.
    anyway - I have updated it to version 3 (in the first post) and now it should work with gpsdriver.

    I don't know what the algorithm is based on, I think I reduced it to the minimum so yours is probably better. I know it does not take altitude into the calculation.
  8. jony

    jony Member

    Hi. Can you make a diferent version which will work without gps? To insert date ,Latitude , Longitude and have the result?
    Also, what is the Astro.bas file ? I am newbe and dont understand that much.. :)). It looks as basic4ppc , but not completed and i cant run it.. I tried to make it in a basic4ppc form, but i have errors , (Mimg is used before it is assigned any value, the same for Sunandmoon, etc.. )
    I need a program that i give date ,Latitude , Longitude from a form, and have Sunrise and Sunset times, the moonrise and Moonset times and the moon phase. If anyone can help.. Thanks ,
    Last edited: Oct 3, 2009
  9. derez

    derez Expert Licensed User

    The astro.bas is a module, attached to my GPS navigation program.
    The great idea of modules is that you can take it and attach it to other programs, using a minimal interface.
    This means that it will be very easy for me or for anyone else to do what you ask for.

    Can you wait few days ?
  10. derez

    derez Expert Licensed User

    No GPS Astro

    I attach the modified Astro3 program, to work without GPS.

    The date format is 3.10.2009
    Lat and Long are decimal (e.g 34.82)
    This can be changed in the main program.

    Attached Files:

  11. agraham

    agraham Expert Licensed User

    Hi David. Unless I am being stupid there seems to be something wrong.

    Where I am at Lat: 53.27, Lon: -2.7, Tz 0
    On 24th June 2009, about the longest day, sunrise is shown 8:42, sunset is 15:43 - daylength about 7 hours
    On 24th December 2009, about the shortest day, sunrise is shown as 3:40, sunset is 20:39 - daylength about 17 hours
  12. jony

    jony Member

    Many , many thanks derez! :) But as agraham says , there is something wrong.
    I have try also this one which also has wrong results.. :(
    I have build a Greek "name day" program . (It shows every day which celebration is) , which i give for free to use all the Greeks. I would like to add sun rise and sun set time , and perhaps build a titanium panel which will show the picture of moon. But i really have a hard time find a sun rise / sun set time code for basic4ppc which will work ok . I am not a programmer , i do it just for hobby .
    An example perhaps might help

    ioannina Greece
    Latitude: 39.6964
    Longitude: 20.8225

    D=22 M=12 Y=2009 TZ=3
    The program gives sun rise 09:05 sun set 18:04 which is close to the real
    .................................... 08:55 ......... 18:16 Sun or Moon Rise/Set Table for One Year (Latitude: 39° 41' N , Longitude: 20° 49' E , 3h East of Greenwich )

    Only a day after
    D=23 M=12 Y=2009 TZ=3
    The program gives sun rise 06:05 sun set 21:04
    (Also for this date the Moon set is empty).

    If you find out what is wrong it would be excellent .

    Anyway thank you very very much for your help and your time. (And sorry for my english..)
    Last edited: Oct 4, 2009
  13. derez

    derez Expert Licensed User

    You are right. There is a jump between 21 and 22 to June.
    Also the day to day change is not coherent.

    I admit that I never checked it this way, I compared to internet data of cities and it looked OK.

    The problem is that I have no idea how the sun algo works, I just translated something that I found in the net (unlike the moon phase, which I did the same but I understand...).

    I shall check if it is not something that I added wrongly.:(


    Jony - the moon set shows empty because there is not moonset at that date, it is on the next day (after 00:00), that is OK.
    The sun algo is wrong.

    Edit: It is caused by ATAN singulrity and abrupt change from minus to plus, and I cannot trace the source. I'll have to find the original algorithm and compare. Please be patient...
    Last edited: Oct 5, 2009
  14. jony

    jony Member

    Ok , so we need someone to understand sun algo.. :)
    Any mathematician here ? :) I have studied statistic in university but dont really understand the sun algo logic.. If i had a book for this..
  15. derez

    derez Expert Licensed User

    Good news !
    I found this Sunrise/Sunset Algorithm
    It looks simple to implement :)

    Give me a day or two and I'll update the code :)
  16. derez

    derez Expert Licensed User

    It was quicker then I estimated.

    I compared the data to this link: Sun or Moon Rise/Set Table for One Year

    It matches to the level of one minute.

    The zenith parameter has a very large effect on the results.

    Thank you for the testing, I hope you'll check this version as well (only the astro module is changed).

    Attached Files:

  17. jony

    jony Member

    Absolutely amazing work here man! :) I tested it until... 2025! , and every time it gives excellent result . Like you said +- 1 min . I have found commercial programs (which you buy) which's result are +- 15 min.. And you did all this extremely fast. I was thinking ,because i couldn't find a good code for this, to take the data from the above site e.g for 10 years , make an array , and have the data "ready" to use . Now everything is much better . And the most amazing thing is that you build this application when i was searching for this.. Thanks again for your work. :)
    Last edited: Oct 4, 2009
  18. derez

    derez Expert Licensed User

    As I said - I'm using it for my own GPS application, so it was important to put things in order.

    Maybe if you change the Zenith value to 90+5/6 it will be even more accurate.
  19. mjcoon

    mjcoon Well-Known Member Licensed User

    Do any of you chaps know of a "Galilean Moons of Jupiter" algorithm or program?

    I've been using a graphic program for my old Psion PDA but would like one for PPC now...

    Cheers, Mike.
  20. jony

    jony Member

    Last edited: Oct 5, 2009
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice