Results 1 to 5 of 5

Thread: [VB/VBA] LunarPhase - Phase of the Moon

  1. #1
    PowerPoster
    Join Date
    Feb 06
    Posts
    8,577

    [VB/VBA] LunarPhase - Phase of the Moon

    When you don't need it, you don't need it. But when you do it can take a little thinking through.

    The basic concept is fairly easy if you simply need rough, non-astronomical results. Take your target date/time, find out how far it is from a base date/time with a known phase (say New Moon), take the Lunar phase cycle period, and then the result is easy:

    Phase = Delta Mod Period

    Usually you'll want the value in more granular form. Maybe quarters is good enough, etc. so just divide the "Phase" value by the fraction of "Period" you want for granularity.

    Another refinement is to make sure your base and target times are for the same time zone so you can avoid being off by 0 to 24 hours.


    Here I used a UTC "base" New Moon date & time so I correct local time to UTC before calculating (this function is in the attached demo project). I found 24 images for the entire cycle so I am returning a phase value that is 1/24th of a Lunar period. I was careful to reduce the use of non-integer arithmetic and avoid rounding.

    So hopefully this works out reasonably accurately for simple "display the phase of the Moon" purposes:
    Code:
    Public Const PhaseBase As Date = #1/17/1980 9:18:00 PM# 'GMT/UTC.
    Public Const LunarSynod As Long = 42524    'Minutes.
    
    Public Function LunarPhase(ByVal TargetDate As Date) As Integer
        'Returns lunar phase for TargetDate starting from PhaseBase
        'timestamp forward, at 1/24 cycle intervals, i.e. values
        'from 0 to 23:
        '
        '    0 = New Moon
        '    6 = First Quarter
        '   12 = Full Moon
        '   18 = Last Quarter
        
        LunarPhase = _
            (Fix(DateDiff("n", PhaseBase, ToUTC(TargetDate))) Mod LunarSynod) _
          \ (LunarSynod \ 24)
    End Function
    I've wrapped it in a small demo application for testing.
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by dilettante; Aug 29th, 2012 at 09:48 AM. Reason: reposted corrected demo attachment

  2. #2
    PowerPoster
    Join Date
    Feb 06
    Posts
    8,577

    Re: [VB/VBA] LunarPhase - Phase of the Moon

    One thing weird about the demo. Perhaps I just forgot this about MonthView controls or perhaps I've done something strange.

    Once you click in the MonthView it seems "reluctant" to release focus.

    E.g. say you click on a date in the currently shown month, then click the "Run" button. Clicking on the butten once gets it focus but doesn't fire a button click. You have to click it a second time.


    Ahh, the penny finally dropped. This nagged at me but finally I remember now:

    BUG: MonthView Control Prevents CommandButton Click Event

  3. #3
    PowerPoster
    Join Date
    Feb 06
    Posts
    8,577

    Re: [VB/VBA] LunarPhase - Phase of the Moon

    I posted another updated attachment above. I had a rather embarassing error in the function used to convert from local time to UTC.

  4. #4
    PowerPoster
    Join Date
    Feb 06
    Posts
    8,577

    Re: [VB/VBA] LunarPhase - Phase of the Moon

    Quote Originally Posted by From a PM
    small bugs in the demo:

    Clicking Run button, demo skipped some date.
    Yes, it will indeed jump over some dates, but that isn't a bug.

    As it toddles along in its "run" mode the demo advances time by 1/24th of a lunar synod which is about 42524 minutes (or about 29.5306 days). That means that each of its "ticks" is a bit longer than one day, so every so often the fractions add up making it jump two days.

    You could change this, you simply need to pick a higher frequency (for example 36 instead of 24) as long as you draw yourself 36 images to illustrate the entire cycle from New Moon to Full Moon.

    But the calculation itself doesn't have a bug, the demo just jumps.

    Normally you might use something like this to display the phase of the moon "right now" for whatever purpose. The 1/24th granularity and 24 images work fine for that. You only see jumps if you try something silly, like my animation demo.

  5. #5
    PowerPoster
    Join Date
    Feb 06
    Posts
    8,577

    [VB/VBA] SunRS - Sun Rise/Set/Day Length

    I thought this might be a logical place to piggyback a related but much more involved approximate astronomical calculation. I was pretty disappointed in the implementtion examples that I found for this too, and went digging once again. You may find that this rendition works for you either as is or with some tweaking.


    SunRS

    SunRS.cls is a VB6 class that accepts latitude and longitude as input properties, and then a method call supplying a date. The call will calculate sunrise, sunset, solar noon, and the day length which can be retrieved as output properties.


    Background

    There doesn't seem to be a concise description anywhere of the calculations required to accomplish this. The few descriptions I've seen express the process in terms of several calculations of intermediate values to be used, along with some description of those. Perhaps this is to help justify the "correctness" or perhaps to help educate people instead of providing a blind copy/paste "answer."

    I chose to base my implementation on the description found in Sunrise equation. The code as written tries to stay very close to the description there calculation by calculation.

    I even used the same corrections for the oddities involved:
    • "Dip."
    • Atmospheric refraction.
    • Observer elevation.
    • Width of the apparent solar disk (disc if you like).


    Deviation

    Even so there seems little agreement on the "fudge factors" to be used. There are even multiple definitions such as "legal sunrise" or "nautical sunrise" and so on.

    If you need to perform those compensations or get more elaborate about the general compensation factors I suggest you do more research. As it is the values I'm getting appear adequate for my own purposes.

    Hopefully others can get some use out of this.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •