ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Get the Sunrise / Sunset Times

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get the Sunrise / Sunset Times

    This demo program will calculate the sunrise, sunset, and solar noon times for any any location on Earth. (Latitudes greater than +- 70 degrees will not be accurate.)

    Sorry, but I didn't use any CL whatsoever.

    I've put everything into a single demo program. On my website, I will post the code for the service programs so that all your applcaition has to do is to bind to a directory where the service programs are located, and execute a single statement: ie

    Code:
             SunRiseTime = Solar_GetSunriseTime(  SunDate
                                                : Latitude
                                                : Longitude
                                                : HourOffset
                                                : DayLightSavings );
    I'll have more functions provided in the Service Program, such as the length of the day. But for now, here is a demo program that has all the code self-contained.

    All you have to do is to copy the DDS for SolarTimeD display file and compile it, and copy the program code for SolarTime rpg program and compile it.


    EDIT New program and display file source in the post below.
    Last edited by MichaelCatalani; March 12, 2009, 05:15 PM.
    Michael Catalani
    IS Director, eCommerce & Web Development
    Acceptance Insurance Corporation
    www.AcceptanceInsurance.com
    www.ProvatoSys.com

  • #2
    Re: Get the Sunrise / Sunset Times

    Seeing this done in RPGLE makes me want to cry. Don't worry, they are tears of joy.

    What a cool program. I love to see things like... Math_DegreesToRadians... simply beautiful. Very cool. Thanks for posting
    Your future President
    Bryce

    ---------------------------------------------
    http://www.bravobryce.com

    Comment


    • #3
      Re: Get the Sunrise / Sunset Times

      very nice . . . I get shivers when I read this:
      Calculate the Solar Declination value for a given Gamma value
      WHAT???!


      change your signature to include your website address....
      this will make google go NUTS!

      your website will go up in status...

      jamie
      All my answers were extracted from the "Big Dummy's Guide to the As400"
      and I take no responsibility for any of them.

      www.code400.com

      Comment


      • #4
        Re: Get the Sunrise / Sunset Times

        Whats kind of amazing is that the RPG calculations appear to be more accurate than some of the website calculations available. ( It appears some of the sunrise / sunset websites are rounding some numbers in intermediate calculation steps. )

        I can also return the Solar Declination angle and the Equation of Time if anyone has a burning desire for those values. The service program will also return the length of the day in seconds. I'll get the service program posted to my site in the next few days.

        And yes, this is the first time I have had to use Sine, CoSine, and ArcTangent on the same line of code; in any language.


        And thanks for pointing out my signature Jamie. It has been fixed.
        Last edited by MichaelCatalani; March 10, 2009, 01:54 PM.
        Michael Catalani
        IS Director, eCommerce & Web Development
        Acceptance Insurance Corporation
        www.AcceptanceInsurance.com
        www.ProvatoSys.com

        Comment


        • #5
          Re: Get the Sunrise / Sunset Times

          mike wrap that url in this code (remove the space after the first "U" )

          [U RL="http://www.code400.com"]http://www.code400.com[/URL]
          All my answers were extracted from the "Big Dummy's Guide to the As400"
          and I take no responsibility for any of them.

          www.code400.com

          Comment


          • #6
            Re: Get the Sunrise / Sunset Times

            Originally posted by jamief View Post
            mike wrap that url in this code (remove the space after the first "U" )
            I tried that but it didn't work. Is there maybe an option somewhere I need to activate to place the html tags in the signature?
            Michael Catalani
            IS Director, eCommerce & Web Development
            Acceptance Insurance Corporation
            www.AcceptanceInsurance.com
            www.ProvatoSys.com

            Comment


            • #7
              Re: Get the Sunrise / Sunset Times

              I made a few changes to the program. The biggest one is that I have transposed the longitude for the western hemisphere to negative numbers. This is the way they are normally read, and the way they are displayed on Google Earth. NOAA reverses these numbers, which is why I had them reversed in the first place. So if you pull a longitude for a location such as Memphis, TN , which is 90 degrees WEST, this should be entered as 90- for the longitude. The longitude for Beijing China is 116E, so this would be a positive 116.

              I also added the length of day as a function.

              To get your location's exact latitude and longitude in decimal formats, you can use Google Earth. To set Google Earths program to display the latitude and longitude in decimal format, use the "tools" and "options" tabs, and change the show lat/long section to Decimal Degrees.

              I have the Service Program broken out, and a demo program which uses the service program, at http://www.provatosys.com/SolarTime.html .



              SolarTimeD Display File

              Code:
                   A*%%TS  SD  20090312  161627  QPGMR       REL-V6R1M0  5761-WDS
                   A*%%EC
                   A                                      DSPSIZ(24 80 *DS3)
                   A                                      CF03(03)
                   A          R SCREEN01
                   A*%%TS  SD  20090312  161627  QPGMR       REL-V6R1M0  5761-WDS
                   A                                  1  2DATE
                   A                                      EDTCDE(Y)
                   A                                      COLOR(PNK)
                   A                                  2  2TIME
                   A                                      COLOR(PNK)
                   A                                  1 18'Calculate Sunrise / Sunset / Solar-
                   A                                       Noon'
                   A                                      COLOR(BLU)
                   A                                  1 70USER
                   A                                      COLOR(PNK)
                   A                                  7  3'Longitude.......:'
                   A                                      COLOR(BLU)
                   A                                  6  3'Latitude........:'
                   A                                      COLOR(BLU)
                   A                                  8  3'TimeOffset......:'
                   A                                      COLOR(BLU)
                   A                                 11  3'Sunrise.........:'
                   A                                      COLOR(BLU)
                   A                                 12  3'Solar Noon......:'
                   A                                      COLOR(BLU)
                   A                                 13  3'Sunset..........:'
                   A                                      COLOR(BLU)
                   A                                  9  3'Daylight Savings:'
                   A                                      COLOR(BLU)
                   A            DSPDST         1A  B  9 22COLOR(WHT)
                   A                                  5  3'Date............:'
                   A                                      COLOR(BLU)
                   A            DSPDATE         L  B  5 22COLOR(WHT)
                   A            RISETIME      11A  O 11 22COLOR(PNK)
                   A            NOONTIME      11A  O 12 22COLOR(PNK)
                   A            SETTIME       11A  O 13 22COLOR(PNK)
                   A            DSPLAT         9Y 3B  6 22COLOR(WHT)
                   A                                      EDTCDE(M)
                   A            DSPLON         9Y 3B  7 22COLOR(WHT)
                   A                                      EDTCDE(M)
                   A            DSPOFFSET      3Y 1B  8 22COLOR(WHT)
                   A                                      EDTCDE(M)
                   A                                 16  2' Note:   If the value of your lati-
                   A                                      tude and longitude is in degrees / -
                   A                                      minutes /           seconds, then t-
                   A                                      he value will be the degrees, point-
                   A                                      , and the minutes             divid-
                   A                                      ed by 60.  For example, Honolulus  -
                   A                                      latitude is 21 degrees, 18         -
                   A                                           minutes.  The decimal form of -
                   A                                      the latitude will be 21.3.         -
                   A                                                     The Time Offset is t-
                   A                                      he number of hours that the local t-
                   A                                      ime is                   offset fro-
                   A                                      m the GMT. IE: Eastern time zone is-
                   A                                       +5, central time is +6,           -
                   A                                      mountain time is +7, Pacific is +8 -
                   A                                       London is 0. Berlin is -1.'
                   A                                  4 36'Austin, TX'
                   A                                      DSPATR(UL)
                   A                                  6 38'30.27'
                   A                                  7 38'97.72-'
                   A                                  8 39'6'
                   A                                  4 48'Sydney, Australia'
                   A                                      DSPATR(UL)
                   A                                  6 53'33.87-'
                   A                                  7 52'151.22'
                   A                                  8 53'10-'
                   A                                  4 67'New Delhi'
                   A                                      DSPATR(UL)
                   A                                  6 69'28.6'
                   A                                  7 69'77.2'
                   A                                  8 70'5.5-'
                   A                                 14  3'Length Of Day...:'
                   A                                      COLOR(BLU)
                   A                                 14 31'Seconds'
                   A                                      COLOR(PNK)
                   A            DAYLENGTH      7Y 0O 14 21COLOR(PNK)
                   A                                      EDTCDE(1)
                   A                                 23  3'F3-Exit'
                   A                                      COLOR(WHT)

              SolarTime RPG Program

              Code:
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  S o l a r T i m e   D e m o   P r o g r a m
                    *
                    *          @copyrite 1997, 2009 Michael Catalani
                    *           ProvatoSys  www.ProvatoSys.com
                    *           mcatalani@aol.com
                    *           901.581.8791
                    *
                    *  Get the Sunrise, Sunset, and Solar Noon times for a given day /
                    *  latitude / longitude
                    *
                    *
                    *  Please note: this demo program has procedures, functions, and prototype
                    *               copied directly into this program.  You should use the
                    *               copy members and service programs that is detailed at
                    *               http://www.ProvatoSys.com/SolarTime.html in a production
                    *               program.
                    *
                    *               The sunrise / sunset / solar noon calculations are
                    *               highly accurate for latitudes between +-70. The polar
                    *               regions above +-70 degrees can have days and nights that
                    *               exceed 1 day, so certain days may in these areas may
                    *               not have a sunrise or a sunset
                    *
                    *
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
              
                   h bnddir('QC2LE') dftactgrp(*no)
                   fSolarTimeDcf   e             workstn
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  S o l a r T i m e   P r o t o T y p e s
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
              
                   d pi              c                   3.14159265358979323846
              
              
                   d Math_ArcCosine  pr             8f   Extproc( 'acos' )
                   d  VariableX                     8f   value
              
                   d Math_ArcSine    pr             8f   Extproc( 'asin' )
                   d  VariableX                     8f   value
              
                   d Math_ArcTangent...
                   d                 pr             8f   Extproc( 'atan' )
                   d  VariableX                     8f   value
              
                   d Math_Cosine     pr             8f   Extproc( 'cos' )
                   d  VariableX                     8f   value
              
                   d Math_Sine       pr             8f   Extproc( 'sin' )
                   d  VariableX                     8f   value
              
                   d Math_Tangent    pr             8f   Extproc( 'tan' )
                   d  VariableX                     8f   value
              
                   d Math_Gamma      pr             8f   Extproc( 'gamma' )
                   d  VariableX                     8f   value
              
                   d Math_RadiansToDegrees...
                   d                 pr             8f
                   d   AngleRadians                 8f   const
              
                   d Math_DegreesToRadians...
                   d                 pr             8f
                   d   AngleDegrees                 8f   const
              
                   d Date_DayOfTheYear...
                   d                 pr             3s 0
                   d  PassedDate                     d   const
              
                   d Date_YearBegin  pr              d
                   d  PassedDate                     d   const
              
                   d Time_TimeToString...
                   d                 pr            11a
                   d  PassedTime                     t   const
              
                   d CalculateGammaGMT...
                   d                 pr             8f
                   d   GammaDate                    3s 0 const
              
                   d CalculateGammaLocal...
                   d                 pr             8f
                   d   GammaDate                    3s 0 const
                   d   HourOffset                   3s 1 const
              
                   d CalculateEquationOfTime...
                   d                 pr             8f
                   d   Gamma                        8f   const
              
                   D CalculateSolarDeclination...
                   d                 pr             8f
                   d   Gamma                        8f   const
              
                   d CalculateSolarAngle...
                   d                 pr             8f
                   d   Latitude                     8f   const
                   d   SolarDeclination...
                   d                                8f   const
                   d   PositiveTimeOffset...
                   d                                 n   const
              
                   d GetPositionTime...
                   d                 pr              t
                   d   SunDate                       d   const
                   d   Latitude                     8f   const
                   d   Longitude                    8f   const
                   d   RiseSetFlag                   n   const
                   d   HourOffset                   3s 1 const
                   d   DayLightSavings...
                   d                                 n   const
              
                   d Solar_GetSunRiseTime...
                   d                 pr              t
                   d   SunRiseDate                   d   const
                   d   Latitude                     8f   const
                   d   Longitude                    8f   const
                   d   HourOffset                   3s 1 const
                   d   DayLightSavings...
                   d                                 n   const
              
                   d Solar_GetSunSetTime...
                   d                 pr              t
                   d   SunSetDate                    d   const
                   d   Latitude                     8f   const
                   d   Longitude                    8f   const
                   d   HourOffset                   3s 1 const
                   d   DayLightSavings...
                   d                                 n   const
              
                   d Solar_GetSolarNoonTime...
                   d                 pr              t
                   d   SunDate                       d   const
                   d   Longitude                    8f   const
                   d   HourOffset                   3s 1 const
                   d   DayLightSavings...
                   d                                 n   const
              
                   d Solar_GetLengthOfDay...
                   d                 pr             8f
                   d   SunRiseDate                   d   const
                   d   Latitude                     8f   const
                   d   Longitude                    8f   const
                   d   HourOffset                   3s 1 const
                   d   DayLightSavings...
                   d                                 n   const
              
              
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  S o l a r T i m e   Variables and Program Code
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
              
              
                   d  Latitude       s              8f
                   d  Longitude      s              8f
                   d  SunDate        s               d
                   d  HourOffset     s              3s 1
                   d  GammaSolarNoon...
                   d                 s              8f
                   d  DayLightSavings...
                   d                 s               n
                   d  SunRiseTime    s               t
                   d  SunSetTime     s               t
                   d  SolarNoonTime  s               t
                   d  LengthOfDay    s              8f
              
                    /free
              
                     // Fill the Display File fields with default data
                     dspDate = %date;
                     dspLat = 30;
                     dspLon = 90;
                     dspOffset= 6;
                     dspDST = 'Y';
              
                     dow NOT *in03;
                       exfmt screen01;
                       if *in03 = *on;
                         leave;
                       endif;
              
                       HourOffset = dspOffset;
                       Latitude = dspLat;
                       Longitude  = dspLon;
                       SunDate =  dspDate;
              
                       If dspDST = 'Y';
                         DayLightSavings = *on;
                       else;
                         DayLightSavings = *off;
                       endif;
              
                       SunRiseTime = Solar_GetSunriseTime(  SunDate
                                                          : Latitude
                                                          : Longitude
                                                          : HourOffset
                                                          : DayLightSavings );
              
                       SolarNoonTime = Solar_GetSolarNoonTime(  SunDate
                                                              : Longitude
                                                              : HourOffset
                                                              : DayLightSavings );
              
                       SunSetTime = Solar_GetSunSetTime(  SunDate
                                                        : Latitude
                                                        : Longitude
                                                        : HourOffset
                                                        : DayLightSavings );
              
                       LengthOfDay = Solar_GetLengthOfDay(  SunDate
                                                          : Latitude
                                                          : Longitude
                                                          : HourOffset
                                                          : DayLightSavings );
              
                       RiseTime = Time_TimeToString( SunRiseTime );
                       SetTime = Time_TimeToString( SunSetTime );
                       NoonTime = Time_TimeToString( SolarNoonTime );
                       DayLength = LengthOfDay;
              
              
                      enddo;
              
                     *inlr = *on;
                     return;
              
                    /end-free
              
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  M a t h _ R a d i a n s T o D e g r e e s
                    *
                    *          Convert Radians to Degrees
                    *
                    *          Input Field - Radians ( floating point )
                    *          Returns     - Degrees ( floating point )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p Math_RadiansToDegrees...
                   p                 b                   export
                   d Math_RadiansToDegrees...
                   d                 pi             8f
                   d   AngleRadians                 8f   const
              
                   d Degrees         s              8f
              
                    /free
              
                      Degrees  = 180 * AngleRadians / pi;
                      Return Degrees;
              
                    /end-free
                   p                 e
              
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  M a t h _ D e g r e e s T o R a d i a n s
                    *
                    *          Convert Degrees to Radians
                    *
                    *          Input Field - Degrees ( floating point )
                    *          Returns     - Radians ( floating point )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p Math_DegreesToRadians...
                   p                 b                   export
                   d Math_DegreesToRadians...
                   d                 pi             8f
                   d   AngleDegrees                 8f   const
                   d  Radians        s              8f
              
                    /free
              
                      Radians  = pi * AngleDegrees / 180;
                      Return Radians;
              
                    /end-free
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  D a t e _ Y e a r B e g i n
                    *
                    *          Returns the January 1 date for a given date
                    *            ie  PassedDate = 2008-06-02
                    *                Returns      2008-01-01
                    *
                    *
                    *          Input Field - Input Date ( Date Field )
                    *          Returns     - January 1st Date For The year of the passed Date
                    *                        ( Date Field )
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p Date_YearBegin  b                   export
                   d Date_YearBegin  pi              d
                   d  PassedDate                     d   const
              
                   d  YearBeginDate  s               d
              
                    /free
              
              
                      YearBeginDate = PassedDate -
                                      %months( %subdt( PassedDate : *months ) - 1 ) -
                                      %days(   %subdt( PassedDate : *days )   - 1 );
              
                      return ( YearBeginDate );
              
                    /end-free
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  D a t e _ D a y O f T h e Y e a r
                    *
                    *          Get The Day # of the Year
                    *
                    *          Input Field - Input Date ( Date Field )
                    *          Returns     - Day # Of The Year ( 3,0 )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p Date_DayOfTheYear...
                   p                 b                   export
                   d Date_DayOfTheYear...
                   d                 pi             3s 0
                   d  PassedDate                     d   const
                   d DayNumber       s              3s 0
                   d Midnight        s               t   inz
                   d FromDate        s               z
                   d ToDate          s               z
                    /free
              
              
                     FromDate = Date_YearBegin( PassedDate ) +  Midnight;
                     ToDate   = PassedDate + Midnight;
              
                     daynumber = %diff( ToDate : FromDate : *days) + 1 ;
              
                     return DayNumber;
              
                    /end-free
                   p                 e
              
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  T i m e _ T i m e T o S t r i n g
                    *
                    *          Returns a formatted time string
                    *
                    *          Input Field - Input Time ( Time Field )
                    *          Returns     - XX:XX:XX am/pm Time  ( Character 11 )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p Time_TimeToString...
                   p                 b                   export
                   d Time_TimeToString...
                   d                 pi            11a
                   d  PassedTime                     t   const
                   d  TimeString     s             11a   varying
                   d  Hours          s              2p 0
                   d  Minutes        s              2p 0
                   d  Seconds        s              2p 0
                   d  AmPm           s              3a
                    /free
              
                         Hours   = %subdt( PassedTime : *hours   );
                         Minutes = %subdt( PassedTime : *minutes );
                         Seconds = %subdt( PassedTime : *seconds );
              
                         if %subdt( PassedTime : *hours) >= 12;
                           AmPm = ' pm';
                           if %subdt( PassedTime : *hours) > 12;
                             Hours -= 12;
                           endif;
                         else;
                           AmPm = ' am';
                           if %subdt( PassedTime : *hours) = 0;
                             Hours = 12;
                           endif;
                         endif;
              
                         TimeString = %editc( Hours   : 'X' ) + ':' +
                                      %editc( Minutes : 'X' ) + ':' +
                                      %editc( Seconds : 'X' ) +
                                      AmPm;
              
                         return ( TimeString );
              
                     /end-free
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  C a l c u l a t e G a m m a G M T
                    *
                    *          Calculate The Solar Gamma For A Certain Day Of The Year
                    *          for the Greenwich Mean Time location
                    *
                    *          Input Field - Day Of The Year ( 3,0 )
                    *          Returns     - Gamma           ( floating point )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p CalculateGammaGMT...
                   p                 b
                   d CalculateGammaGMT...
                   d                 pi             8f
                   d   JulianDate                   3s 0 const
              
                   d  Radians        s              8f
                   d  Gamma          s              8f
              
                    /free
              
                      Gamma = ( 2 * pi / 365 ) * ( JulianDate - 1 );
                      Return Gamma;
              
                    /end-free
              
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  C a l c u l a t e G a m m a L o c a l
                    *
                    *          Calculate The Solar Gamma For A Certain Day Of The Year
                    *          for the time zone offset location
                    *
                    *          Input Field - Day Of The Year ( 3,0 )
                    *          Returns     - Gamma           ( floating point )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p CalculateGammaLocal...
                   p                 b
                   d CalculateGammaLocal...
                   d                 pi             8f
                   d   JulianDate                   3s 0 const
                   d   HourOffset                   3s 1 const
              
                   d  Radians        s              8f
                   d  Gamma          s              8f
              
                    /free
              
              
                      Gamma = ( 2 * pi / 365) *
                              ( JulianDate - 1 + ( HourOffset / 24 ));
              
                      Return Gamma;
              
                    /end-free
              
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  C a l c u l a t e E q u a t i o n O f T i m e
                    *
                    *          Calculate the Equatation of Time for a given Gamma value
                    *
                    *          Input Field - Gamma ( floating point )
                    *          Returns     - Equatorial Time ( floating point )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p CalculateEquationOfTime...
                   p                 b
                   d CalculateEquationOfTime...
                   d                 pi             8f
                   d   Gamma                        8f   const
                   d  EquationOfTime...
                   d                 s              8f
              
                    /free
              
                      EquationOfTime= ( 229.18 * ( 0.000075 + 0.001568 *
                                        Math_Cosine( Gamma ) -  0.032077 *
                                        Math_Sine(   Gamma ) - 0.014615 *
                                        Math_Cosine( 2 * Gamma ) - 0.040849 *
                                        Math_Sine(   2 * Gamma )));
              
                      Return EquationOfTime;
              
                    /end-free
              
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  C a l c u l a t e S o l a r D e c l i n a t i o n
                    *
                    *          Calculate the Solar Declination value for a given Gamma value
                    *
                    *          Input Field - Gamma ( floating point )
                    *          Returns     - Solar Declination ( floating point )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p CalculateSolarDeclination...
                   p                 b
                   d CalculateSolarDeclination...
                   d                 pi             8f
                   d   Gamma                        8f   const
                   d  SolarDeclination...
                   d                 s              8f
              
                    /free
              
                     SolarDeclination = ( 0.006918 - 0.399912 *
                                          Math_Cosine(     Gamma ) + 0.070257 *
                                          Math_Sine(       Gamma ) - 0.006758 *
                                          Math_Cosine( 2 * Gamma ) + 0.000907 *
                                          Math_Sine(   2 * Gamma ));
              
                      Return SolarDeclination;
              
                    /end-free
              
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  C a l c u l a t e S o l a r A n g l e
                    *
                    *          Calculate the Solar Angle for a given latitude and
                    *          and Solar Declination.
                    *
                    *          If the value of positive time offset is *on, then the routine
                    *          calculates the solar angle at sunrise.  If it is off, then
                    *          it is calculating the solar angle at sunset.
                    *
                    *          Input Field - Latitude ( floating point )
                    *                      - Solar Declination ( Floating Point )
                    *                      - Positive Time Offset ( boolean on / off )
                    *
                    *          Returns     - Solar Angle ( floating point )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p CalculateSolarAngle...
                   p                 b
                   d CalculateSolarAngle...
                   d                 pi             8f
                   d   Latitude                     8f   const
                   d   SolarDeclination...
                   d                                8f   const
                   d   PositiveTimeOffset...
                   d                                 n   const
                   d  SolarAngle     s              8f
                   d  LatitudeRadians...
                   d                 s              8f
              
                    /free
              
                      LatitudeRadians = Math_DegreesToRadians( Latitude );
              
                      SolarAngle = ( Math_ArcCosine(Math_Cosine(
                                    Math_DegreesToRadians( 90.833 )) /
                                  ( Math_Cosine( LatitudeRadians ) *
                                    Math_Cosine(SolarDeclination)) -
                                    Math_Tangent( LatitudeRadians ) *
                                    Math_Tangent( SolarDeclination )));
              
                      if NOT PositiveTimeOffset;
                        SolarAngle = SolarAngle * -1;
                      endif;
              
                      Return SolarAngle;
              
                    /end-free
              
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  G e t P o s i t i o n T i m e
                    *
                    *          Retrieve the position time for sunrise / sunset for a
                    *          given date, latitude, and longitude
                    *
                    *          Input Field - Sunrise Date ( date field )
                    *                      - Latitude ( floating point )
                    *                      - Longitude ( floating point )
                    *                      - Day Light Savings Time ( boolean on / off )
                    *          Returns     - Sunrise Time ( Time Field )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p GetPositionTime...
                   p                 b
                   d GetPositionTime...
                   d                 pi              t
                   d   SunDate                       d   const
                   d   Latitude                     8f   const
                   d   Longitude                    8f   const
                   d   RiseSetFlag                   n   const
                   d   HourOffset                   3s 1 const
                   d   DayLightSavings...
                   d                                 n   const
                   d  Gamma          s              8f
                   d  EquationOfTime...
                   d                 s              8f
                   d  SolarDeclination...
                   d                 s              8f
                   d  Delta          s              8f
                   d  TimeDifference...
                   d                 s              8f
                   d  TimeGMT        s              8f
                   d  GammaSunTime   s              8f
                   d  SolarAngle     s              8f
                   d  SunTime        s               t   inz
                   d  TimeLst        s              8f
              
                    /free
              
                      if RiseSetFlag;
                        Gamma = CalculateGammaGMT( Date_DayOfTheYear( SunDate ));
                      else;
                        Gamma = CalculateGammaGMT( Date_DayOfTheYear( SunDate + %days( 1 )) );
                      endif;
              
                      EquationOfTime = CalculateEquationOfTime( Gamma );
                      SolarDeclination = CalculateSolarDeclination( Gamma );
              
                      SolarAngle = CalculateSolarAngle( Latitude
                                                      : SolarDeclination
                                                      : RiseSetFlag );
              
                      Delta = (Longitude * -1) - Math_RadiansToDegrees( SolarAngle );
                      TimeDifference = 4 * Delta;
                      TimeGMT = 720 + TimeDifference - EquationOfTime;
                      GammaSunTime = CalculateGammaLocal(  Date_DayOfTheYear( SunDate )
                                                         : TimeGMT / 60);
              
                      EquationOfTime = CalculateEquationOfTime( GammaSunTime );
              
                      SolarDeclination = CalculateSolarDeclination( GammaSunTime );
              
                      SolarAngle = CalculateSolarAngle(  Latitude
                                                       : SolarDeclination
                                                       : RiseSetFlag );
              
                      Delta = ( Longitude * -1) - Math_RadiansToDegrees( SolarAngle );
                      TimeDifference = 4 * Delta;
                      TimeGMT = 720 + TimeDifference - EquationOfTime;
                      TimeLST = TimeGMT - (60 * HourOffset );
                      reset SunTime;
                      SunTime += %seconds( TimeLST * 60 );
              
                      if DayLightSavings;
                        SunTime += %hours( 1 );
                      endif;
              
                      return SunTime;
              
                    /end-free
              
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  S o l a r _ G e t S u n R i s e T i m e
                    *
                    *          Retrieve the sunrise time for a given date, latitude,
                    *          and longitude
                    *
                    *          Input Field - Sunrise Date ( date field )
                    *                      - Latitude ( floating point )
                    *                      - Longitude ( floating point )
                    *                      - HourOffset between localtime and GMT
                    *                      - Day Light Savings Time ( boolean on / off )
                    *          Returns     - Sunrise Time ( Time Field )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p Solar_GetSunriseTime...
                   p                 b                   export
                   d Solar_GetSunriseTime...
                   d                 pi              t
                   d   SunRiseDate                   d   const
                   d   Latitude                     8f   const
                   d   Longitude                    8f   const
                   d   HourOffset                   3s 1 const
                   d   DayLightSavings...
                   d                                 n   const
              
                   d  SunRise        s               n   inz( *on )
              
                    /free
              
              
                      return GetPositionTime(  SunRiseDate
                                             : Latitude
                                             : Longitude
                                             : SunRise
                                             : HourOffset
                                             : DayLightSavings );
              
                    /end-free
              
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  S o l a r _ G e t S u n S e t T i m e
                    *
                    *          Retrieve the sunset time for a given date, latitude,
                    *          and longitude
                    *
                    *          Input Field - Sunrise Date ( date field )
                    *                      - Latitude ( floating point )
                    *                      - Longitude ( floating point )
                    *                      - HourOffset between localtime and GMT
                    *                      - Day Light Savings Time ( boolean on / off )
                    *          Returns     - Sunrise Time ( Time Field )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p Solar_GetSunSetTime...
                   p                 b                   export
                   d Solar_GetSunSetTime...
                   d                 pi              t
                   d   SunSetDate                    d   const
                   d   Latitude                     8f   const
                   d   Longitude                    8f   const
                   d   HourOffset                   3s 1 const
                   d   DayLightSavings...
                   d                                 n   const
              
                   d  SunSet         s               n   inz( *off )
              
                    /free
              
                      return GetPositionTime(  SunSetDate
                                             : Latitude
                                             : Longitude
                                             : SunSet
                                             : HourOffset
                                             : DayLightSavings );
              
                    /end-free
              
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  S o l a r _ G e t S o l a r N o o n T i m e
                    *
                    *          Retrieve the time at which the sun is at the highest point
                    *          in the sky for a given date and longitude
                    *
                    *          Input Field - Sunrise Date ( date field )
                    *                      - Longitude ( floating point )
                    *                      - HourOffset between localtime and GMT
                    *                      - Day Light Savings Time ( boolean on / off )
                    *          Returns     - Solar Noon Time ( Time Field )
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p Solar_GetSolarNoonTime...
                   p                 b                   export
                   d Solar_GetSolarNoonTime...
                   d                 pi              t
                   d   SunDate                       d   const
                   d   Longitude                    8f   const
                   d   HourOffset                   3s 1 const
                   d   DayLightSavings...
                   d                                 n   const
                   d  Gamma          s              8f
                   d  EquationOfTime...
                   d                 s              8f
                   d  SolarDeclination...
                   d                 s              8f
                   d  TimeGMT        s              8f
                   d  SunTime        s               t   inz
                   d  TimeLst        s              8f
              
              
                    /free
              
                      Gamma = CalculateGammaLocal(  Date_DayOfTheYear( SunDate )
                                                  : 12 + ( ( Longitude * -1 ) / 15 ));
              
                      EquationOfTime = CalculateEquationOfTime( Gamma );
                      SolarDeclination = CalculateSolarDeclination( Gamma );
                      TimeGMT = 720 + ( Longitude * -1 * 4 ) - EquationOfTime;
                      TimeLST = TimeGMT - ( 60 * HourOffset );
              
                      reset SunTime;
                      SunTime += %seconds( TimeLST * 60 );
              
                      if DayLightSavings;
                        SunTime += %hours( 1 );
                      endif;
              
                      return SunTime;
              
                    /end-free
              
                   p                 e
              
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                    *
                    *  S o l a r _ G e t L e n g t h O f D a y
                    *
                    *          Retrieve the length of the day in seconds
                    *
                    *          Input Field - Date ( date field )
                    *                      - Latitude ( floating point )
                    *                      - Longitude ( floating point )
                    *          Returns     - Length Of Day in Seconds (8F)
                    *
                    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                   p Solar_GetLengthOfDay...
                   p                 b                   export
                   d Solar_GetLengthOfDay...
                   d                 pi             8f
                   d   SunRiseDate                   d   const
                   d   Latitude                     8f   const
                   d   Longitude                    8f   const
                   d   HourOffset                   3s 1 const
                   d   DayLightSavings...
                   d                                 n   const
                   d  RiseTime       s               t   inz
                   d  SetTime        s               t   inz
                   d  LengthOfDay    s              8f   inz
                    /free
              
                      RiseTime = Solar_GetSunRiseTime(  SunRiseDate
                                                       : Latitude
                                                       : Longitude
                                                       : HourOffset
                                                       : DayLightSavings );
              
                      SetTime = Solar_GetSunSetTime(  SunRiseDate
                                                    : Latitude
                                                    : Longitude
                                                    : HourOffset
                                                    : DayLightSavings );
              
                      LengthOfDay = %abs( %diff( SetTime : RiseTime : *seconds ));
                      return LengthOfDay;
              
                    /end-free
              
                   p                 e
              Michael Catalani
              IS Director, eCommerce & Web Development
              Acceptance Insurance Corporation
              www.AcceptanceInsurance.com
              www.ProvatoSys.com

              Comment


              • #8
                Re: Get the Sunrise / Sunset Times

                Originally posted by jamief View Post
                mike wrap that url in this code (remove the space after the first "U" )
                FWIW you can use the noparse tags [noparse][/noparse] to do this [url="http://www.code400.com"]http://www.code400.com[/url] then no need to add spaces,etc to fool the PHP parser
                I'm not anti-social, I just don't like people -Tommy Holden

                Comment


                • #9
                  Re: Get the Sunrise / Sunset Times

                  Originally posted by MichaelCatalani View Post
                  This demo program will calculate the sunrise, sunset, and solar noon times for any any location on Earth. (Latitudes greater than +- 70 degrees will not be accurate.)
                  Gotta ask - cos I don't actually know, do the times vary based on altitude ?
                  Greg Craill: "Life's hard - Get a helmet !!"

                  Comment


                  • #10
                    Re: Get the Sunrise / Sunset Times

                    No, they don't.
                    Your future President
                    Bryce

                    ---------------------------------------------
                    http://www.bravobryce.com

                    Comment


                    • #11
                      Re: Get the Sunrise / Sunset Times

                      Originally posted by gcraill View Post
                      Gotta ask - cos I don't actually know, do the times vary based on altitude ?

                      If you mean the altitude of a city, then no. This is because the horizon should be at the approximate alititude of the location itself. A higher altitude wont affect the sunrise time, because it also doesnt affect the horizon angle.
                      Last edited by MichaelCatalani; March 17, 2009, 09:38 AM.
                      Michael Catalani
                      IS Director, eCommerce & Web Development
                      Acceptance Insurance Corporation
                      www.AcceptanceInsurance.com
                      www.ProvatoSys.com

                      Comment

                      Working...
                      X