Post Reply 
(34S) - Sunrise/Sunset
01-28-2015, 08:08 PM (This post was last modified: 06-15-2017 01:29 PM by Gene.)
Post: #1
(34S) - Sunrise/Sunset
**Update No. 2**
Yet another version, this time with local registers and DST correction built in. (Thanks and credit to Dieter for the DST code.) DST settings are for the US, DST beginning on the second Sunday in March and ending on the first Sunday in November.

If you prefer manual DST, remove steps 4 through 22 and subroutine 01 which begins at step 224. You may then manually set flag A for DST. It will be cleared after each use - if you prefer to stay in DST mode for a series of calculations, change the "FS?C A" in step 34 to simply "FS? A."

If you don't want to use local registers, just remove the LocR16 command and the "." in front of each register number

Operation is essentially as described for the first versions. Here is a dat file for the emulator.
.zip  wp34s.zip (Size: 674 bytes / Downloads: 13)
(I removed the dat file below for the original version.)

Sunrise/Sunset Program:
Code:

Step    Instruction    Comments
001    LBL  'RS'      
002    LocR 016       allocate 16 local registers
003    DEG            Set degrees mode (used throughout)
004    STO .00        store input date
005    YEAR           get year of input date
006    FILL           fill stack
007    #011           enter 11, for November
008    XEQ 01         calculate j-day for last Sunday in October
009    #007           enter 7
010    +              add 7 for US DST end 1st Sunday in November
011    x<>Y           get year of input date back
012    #003           enter 3, for March
013    XEQ 01         calculate j-day for last Sunday in February
014    #014           enter 14
015    +              add 14 for US DST start 2nd Sunday in March
016    RCL .00        recall input date
017    D→J            get j-day of input date
018    SF A           set DST flag
019    x≥? Y          IF date < first DST day 
020    x≥? Z          OR date ≥ first standard time day
021    CF A           THEN clear DST flag
022    RCL .00        Recall input date
023    ENTER          Copy input date into Y, could use RCL X, etc.
024    YEAR           year of input date
025    DEC X          previous year
026    #12            enter 12
027    #31            enter 31
028    →DATE          create last day of previous year in current date mode.
029    X<>Y           
030    ΔDAYS          days from beginning of year
031    STO .04        
032    5              Offset from UTC (-5 represents EST)
033    +/-            Offset from UTC (-5 represents EST)
034    FS?C A         If flag A set, DST in effect for input date
035    INC X          If flag A set, increment offset from UTC
036    STO .03        Local time offset from UTC
037    9              
038    0              
039    .              
040    5              
041    ->HR           90.83333333333333 entered for degrees from zenith at
                      Sunrise/sunset (fewer steps than direct entry)
042    STO .05        degrees from zenith for Sunrise.  May be changed if
                      twilight times or other solar elevation times are desired (e.g.
                      enter 50 to find times when sun is at 40 degrees altitude.)
043    3              Latitude
044    5              Latitude
045    .              Latitude
046    1              Latitude
047    2              Latitude
048    3              Latitude
049    4              Latitude
050    5              Latitude
051    STO .01        35.12345 for example (decimal)
052    9              Longitude
053    0              Longitude
054    .              Longitude
055    5              Longitude
056    4              Longitude
057    3              Longitude
058    2              Longitude
059    1              Longitude
060    +/-            Longitude
061    STO .02        -90.54321 for example (decimal)
062    #015           15 Degrees per hour
063    STO .09        Store for later use
064    /              
065    STO .06        Longitude hour value (lngHour)
066    #018           
067    XEQ 05         Calculate approximate time for sunset
068    XEQ 06         
069    STO .13        Done calculating sunset.  Store the result
070    6              Calculate sunrise
071    XEQ 05         
072    RCL .14        Recall 360
073    RCL- Y         
074    XEQ 06         
075    RCL .13        Recall sunset.  Push sunrise to Y.
076    CL alpha       clear alpha register for new text message
077    'Ris'          
078    'e-S'          
079    'et'           
080    VIEWa          Display text
081    PSE 25         
082    CL alpha       clear alpha register for new text message
083    alphaRC# Y     copy Y register value to alpha register
084    VWalpha+ X     view alpha (sunrise) in dot-matrix, sunset time in X register.
085    STOP           
086    CPX STO Z      store sunrise and sunset times in Z and T
087    x<>Y           
088    H.MS-          subtract sunrise from sunset for day length
089    CL alpha       
090    'Day'          
091    ' Le'          
092    'ngt'          
093    'h'            
094    VWa+ X         Display text and day length
095    RTN            done
096    LBL 05         Subroutine for large common section
097    RCL- .06       
098    #024           enter 24
099    /              
100    RCL+ .04       
101    STO .07        Approximate sunset time
102    .              
103    9              
104    8              
105    5              
106    6              enter 0.9856
107    x              
108    3              
109    .              
110    2              
111    8              
112    9              enter 3.289
113    -              
114    STO .08        Sun's mean anomaly
115    SIN            Calculate sun's true longitude
116    1              
117    .              
118    9              
119    1              
120    6              enter 1.916
121    x              
122    RCL+ .08       
123    2              
124    RCLx .08       
125    SIN            
126    #002           
127    SDR 002        enter 0.02
128    x              
129    +              
130    3              
131    RCLx .08       
132    SIN            
133    3              
134    SDR 004        enter 0.0003
135    x              
136    +              
137    2              
138    8              
139    2              
140    .              
141    6              
142    3              
143    4              enter 282.634
144    +              
145    #36            
146    SDL 001        enter 360
147    STO .14        
148    MOD            
149    STO .08        Sun's true longitude
150    TAN            Calculate sun's Right Ascension
151    .              
152    9              
153    1              
154    7              
155    6              
156    4              enter 0.91764
157    x              
158    ATAN           
159    RCL .14        enter 360
160    MOD            
161    STO .10        Sun's Right Ascension
162    RCL .08        
163    #090           enter 90
164    STO .15        Store for later use
165    /              
166    FLOOR          
167    RCLx .15       Recall 90 and multiply
168    +              
169    RCL .10        
170    RCL/ .15       Recall 90 and divide
171    FLOOR          
172    RCLx .15       Recall 90 and multiply
173    -              
174    RCL/ .09       Recall 15 and divide
175    STO .10        Sun's Right Ascension converted into hours
176    RCL .08        
177    SIN            
178    .              
179    3              
180    9              
181    7              
182    8              
183    2              enter 0.39782
184    x              
185    STO .11        sinDec for sunset
186    ASIN           
187    COS            
188    STO .12        cosDec for sunset
189    RCL .05        
190    COS            
191    RCL .01        
192    SIN            
193    RCLx .11       
194    -              
195    RCL  .01       
196    COS            
197    RCLx .12       
198    /              Sun's local Hour Angle (cosH)
199    ACOS           
200    RTN            
201    LBL 06         Subroutine for another common section
202    RCL/ .09       Recall 15 and divide
203    RCL+ .10       
204    .              
205    0              
206    6              
207    5              
208    7              
209    1              enter 0.06571
210    RCLx .07       
211    -              
212    6              
213    .              
214    6              
215    2              
216    2              enter 6.622
217    -              
218    #024           enter 24
219    MOD            
220    RCL- .06       
221    RCL+ .03       
222    ->H.MS         
223    RTN            
224    LBL 01         Subroutine to calculate last Sunday of prior month.
225    #001           
226    →DATE           = 1 March resp. 1 November
227    D→J            
228    #007           
229    IDIV           
230    RCLx L          Monday on or before 1 March resp. 1 November
231    DEC X           last Sunday of February resp. October
232    END

Registers:
Code:

Register    Use/contents
.00    Input date
.01    latitude
.02    longitude
.03    Local time offset from UTC
.04    day number of year
.05    degrees from zenith for Sunrise.
.06    Longitude hour value (lngHour)
.07    Approximate sunset time
.08    Sun's mean anomaly/Sun's true longitude
.09    15 degrees per hour
.10    Sun's Right Ascension/right ascension converted to hours
.11    sinDec for sunset
.12    cosDec for sunset
.13    sunset time
.14    360
.15    90



**See below for an update**
Below are three versions of a program for the wp34s to calculate sunrise and sunset times. The programs are based on programs that I got from Marwan Joury, versions of which he posted in this article. I give total credit to Marwan, all I did was port his work to the wp34s. Most of the comments in my program listings are from Marwan's original listings that he sent to me. (So if you have any questions regarding the meat of the program, you will have to ask Marwan...)

Program operation is as follows:

Replace steps 6 and 7 with your offset from UTC, West is negative. (You must manually account for Daylight Saving Time, or British Summer Time, or whatever your local flavor of seasonally shifting daylight hours to suit local custom is called if you have one.)

Key in your latitude in steps 15 through 22 in decimal degrees, North is positive.

Key in your longitude in steps 24 through 32 in decimal degrees, West is negative. (You may use as many digits as you want for longitude and latitude, there is no requirement for 5 digits past the decimal point.)

Go to run mode, key in the date in which you are interested in yyyy.mmdd format for version 1, dd.mmyyyy format for version 2 or mm.ddyyyy format for version 3. Press XEQ 'RS'. The program will run, briefly display "Rise-Set", then stop with the sunrise time displayed in the dot-matrix display area, and the sunset time displayed in the numerical display, in 24 hour clock format. Press R/S if you want see the day length. The sunrise and sunset times will still be on the stack in Z and Y, respectively, if you want to look at them again. All returned values are in hh.mmss format.

Since your location information is hard-coded into the program, after entering the data as a part of the program, you can just run the program for new dates until and unless you want to change the location.

You may also replace the value stored in register 6 (steps 8 through 12, which is the angle from zenith being calculated) with other values, for example 96, 102 or 108 degrees to calculate civil, nautical and astronomical twilight times, respectively. If you enter a value less than 90 degrees, you may calculate the times at which the sun rises above and then later sets below 90 minus that value. (E.g., if you enter 50 degrees, the program will calculate the times at which the sun rises to and sets below 40 degrees elevation. This may be useful if you desire to limit your exposure to UV, for example.)

One of the subroutines may be used as a stand-alone program to calculate the number of days between two dates. Key in a date, press enter, key in other date, then press XEQ 02. (Key in the later date first if you want a positive result.)

Attached below is a wp34s emulator dat file that has the y.md version. The differences between the programs are fairly trivial, so you can easily edit to create the d.my or m.dy versions. (Changes are in step 2, and between steps 78 to 86.) If you have a crystal installed in your real wp34s (to facilitate reliable communication), and a communication cable, you can place the dat file in the appropriate folder, start the emulator, and upload to your real wp34s to save a lot of manual keying.

Version 1 - Input date in yyyy.mmdd format
Code:

Step   Instruction    Comments
001    LBL  'RS'      
002    y.md           set date mode
003    DEG            Set degrees mode (used throughout)
004    XEQ  01        Get days since first of year
005    STO 05         
006    5              Offest from UTC (-5 represents EST)
007    +/-            Offest from UTC (-5 represents EST)
008    STO 03         Local time offset from UTC
009    9              
010    0              
011    .              
012    5              
013    ->HR           90.83333333333333 entered for degrees from zenith
                      at Sunrise/sunset (fewer steps than direct entry)
014    STO 06         degrees from zenith for Sunrise.  May be changed if
                      twilight times or other solar elevation times are desired (e.g.
                      enter 50 to find times when sun is at 40 degrees altitude.)
015    3              Latitude
016    5              Latitude
017    .              Latitude
018    1              Latitude
019    2              Latitude
020    3              Latitude
021    4              Latitude
022    5              Latitude
023    STO 01         35.12345 for example (decimal)
024    9              Longitude
025    5              Longitude
026    .              Longitude
027    5              Longitude
028    4              Longitude
029    3              Longitude
030    2              Longitude
031    1              Longitude
032    +/-            Longitude
033    STO 02         -95.54321 for example (decimal)
034    1              15 Degrees per hour
035    5              15 Degrees per hour
036    STO 19         Store for later use
037    /              
038    STO 07         Longitude hour value (lngHour)
039    1              
040    8              Calculate approximate time for sunset
041    XEQ 05         
042    XEQ 06         
043    STO 15         Done calculating sunset.  Store the result
044    6              Calculate sunrise
045    XEQ 05         
046    RCL 17         Recall 360
047    X<>Y           
048    -              
049    XEQ 06         
050    RCL 15         Recall sunset.  Push sunrise to Y.
051    CL alpha       clear alpha register for new text message
052    'Ris'          
053    'e-S'          
054    'et'           
055    VIEWa          Display text
056    PSE 25         
057    CL alpha       clear alpha register for new text message
058    alphaRC# Y     copy Y register value to alpha register
059    VWalpha+ X     view alpha (sunrise) in dot-matrix, sunset time in X register.
060    STOP           
061    CPX STO Z      store sunrise and sunset times in Z and T
062    ->HR           convert sunrise time to decimal hours
063    x<>y           swap x and y
064    ->HR           convert sunset time to decimal hours
065    -              subtract for decimal hour day length
066    ->H.MS         convert day length to HMS
067    CL alpha       
068    'Day'          
069    ' Le'          
070    'ngt'          
071    'h'            
072    VWa+ X         Display text and day length
073    RTN            done
074    LBL 01         Calculate day of year given date
075    ENTER          
076    YEAR           
077    DEC X          
078    .              
079    1              
080    2              
081    3              
082    1              
083    +              
084    LBL  02        Given two dates (on stack) get days between
085    y.md           set date mode for delta-day entry point
086    D->J           julian day no. of input date
087    X<>Y           
088    D->J           julian day no. of last day of previous year
089    RCL- Y         days since first of year (or between dates)
090    RTN            
091    LBL 05         Subroutine for large common section
092    RCL- 07        
093    2              
094    4              enter 24
095    STO 20         Store for later use
096    /              
097    RCL+ 05        
098    STO 08         Approximate sunset time
099    .              
100    9              
101    8              
102    5              
103    6              enter 0.9856
104    x              
105    3              
106    .              
107    2              
108    8              
109    9              enter 3.289
110    -              
111    STO 09         Sun's mean anomaly
112    SIN            Calculate sun's true longitude
113    1              
114    .              
115    9              
116    1              
117    6              enter 1.916
118    x              
119    RCL+ 09        
120    2              
121    RCLx 09        
122    SIN            
123    .              
124    0              
125    2              enter 0.02
126    x              
127    +              
128    3              
129    RCLx 09        
130    SIN            
131    .              
132    0              
133    0              
134    0              
135    3              enter 0.0003
136    x              
137    +              
138    2              
139    8              
140    2              
141    .              
142    6              
143    3              
144    4              enter 282.634
145    +              
146    3              
147    6              
148    0              enter 360
149    STO 17         
150    MOD            
151    STO 10         Sun's true longitude
152    TAN            Calculate sun's Right Ascension
153    .              
154    9              
155    1              
156    7              
157    6              
158    4              enter 0.91764
159    x              
160    ATAN           
161    RCL 17         enter 360
162    MOD            
163    STO 11         Sun's Right Ascension
164    RCL 10         
165    9              
166    0              enter 90
167    STO 18         Store for later use
168    /              
169    FLOOR          
170    RCLx 18        Recall 90 and multiply
171    +              
172    RCL 11         
173    RCL/ 18        Recall 90 and divide
174    FLOOR          
175    RCLx 18        Recall 90 and multiply
176    -              
177    RCL/ 19        Recall 15 and divide
178    STO 11         Sun's Right Ascension converted into hours
179    RCL 10         
180    SIN            
181    .              
182    3              
183    9              
184    7              
185    8              
186    2              enter 0.39782
187    x              
188    STO 12         sinDec for sunset
189    ASIN           
190    COS            
191    STO 13         cosDec for sunset
192    RCL 06         
193    COS            
194    RCL 01         
195    SIN            
196    RCLx 12        
197    -              
198    RCL  01        
199    COS            
200    RCLx 13        
201    /              Sun's local Hour Angle (cosH)
202    ACOS           
203    RTN            
204    LBL 06         
205    RCL/ 19        Recall 19 and divide
206    RCL+ 11        
207    .              
208    0              
209    6              
210    5              
211    7              
212    1              enter 0.06571
213    RCLx 08        
214    -              
215    6              
216    .              
217    6              
218    2              
219    2              enter 6.622
220    -              
221    RCL 20         Recall 24
222    MOD            
223    RCL- 07        
224    RCL+ 03        
225    ->H.MS         
226    END

Version 2 - Input date in dd.mmyyyy format
Code:

Step   Instruction    Comments
001    LBL  'RS'      
002    d.my           set date mode
003    DEG            Set degrees mode (used throughout)
004    XEQ  01        Get days since first of year
005    STO 05         
006    5              Offest from UTC (-5 represents EST)
007    +/-            Offest from UTC (-5 represents EST)
008    STO 03         Local time offset from UTC
009    9              
010    0              
011    .              
012    5              
013    ->HR           90.83333333333333 entered for degrees from zenith
                      at Sunrise/sunset (fewer steps than direct entry)
014    STO 06         degrees from zenith for Sunrise.  May be changed if
                      twilight times or other solar elevation times are desired (e.g. enter
                      50 to find times when sun is at 40 degrees altitude.)
015    3              Latitude
016    5              Latitude
017    .              Latitude
018    1              Latitude
019    2              Latitude
020    3              Latitude
021    4              Latitude
022    5              Latitude
023    STO 01         35.12345 for example (decimal)
024    9              Longitude
025    5              Longitude
026    .              Longitude
027    5              Longitude
028    4              Longitude
029    3              Longitude
030    2              Longitude
031    1              Longitude
032    +/-            Longitude
033    STO 02         -95.54321 for example (decimal)
034    1              15 Degrees per hour
035    5              15 Degrees per hour
036    STO 19         Store for later use
037    /              
038    STO 07         Longitude hour value (lngHour)
039    1              
040    8              Calculate approximate time for sunset
041    XEQ 05         
042    XEQ 06         
043    STO 15         Done calculating sunset.  Store the result
044    6              Calculate sunrise
045    XEQ 05         
046    RCL 17         Recall 360
047    X<>Y           
048    -              
049    XEQ 06         
050    RCL 15         Recall sunset.  Push sunrise to Y.
051    CL alpha       clear alpha register for new text message
052    'Ris'          
053    'e-S'          
054    'et'           
055    VIEWa          Display text
056    PSE 25         
057    CL alpha       clear alpha register for new text message
058    alphaRC# Y     copy Y register value to alpha register
059    VWalpha+ X     view alpha (sunrise) in dot-matrix, sunset time in X register.
060    STOP           
061    CPX STO Z      store sunrise and sunset times in Z and T
062    ->HR           convert sunrise time to decimal hours
063    x<>y           swap x and y
064    ->HR           convert sunset time to decimal hours
065    -              subtract for decimal hour day length
066    ->H.MS         convert day length to HMS
067    CL alpha       
068    'Day'          
069    ' Le'          
070    'ngt'          
071    'h'            
072    VWa+ X         Display text and day length
073    RTN            done
074    LBL 01         Calculate day of year given date
075    ENTER          
076    YEAR           
077    DEC X          
078    SDR 6          
079    3              
080    1              
081    .              
082    1              
083    2              
084    +              
085    LBL  02        Given two dates (on stack) get days between
086    d.my           set date mode for delta-day entry point
087    D->J           julian day no. of input date
088    X<>Y           
089    D->J           julian day no. of last day of previous year
090    RCL- Y         days since first of year (or between dates)
091    RTN            
092    LBL 05         Subroutine for large common section
093    RCL- 07        
094    2              
095    4              enter 24
096    STO 20         Store for later use
097    /              
098    RCL+ 05        
099    STO 08         Approximate sunset time
100    .              
101    9              
102    8              
103    5              
104    6              enter 0.9856
105    x              
106    3              
107    .              
108    2              
109    8              
110    9              enter 3.289
111    -              
112    STO 09         Sun's mean anomaly
113    SIN            Calculate sun's true longitude
114    1              
115    .              
116    9              
117    1              
118    6              enter 1.916
119    x              
120    RCL+ 09        
121    2              
122    RCLx 09        
123    SIN            
124    .              
125    0              
126    2              enter 0.02
127    x              
128    +              
129    3              
130    RCLx 09        
131    SIN            
132    .              
133    0              
134    0              
135    0              
136    3              enter 0.0003
137    x              
138    +              
139    2              
140    8              
141    2              
142    .              
143    6              
144    3              
145    4              enter 282.634
146    +              
147    3              
148    6              
149    0              enter 360
150    STO 17         
151    MOD            
152    STO 10         Sun's true longitude
153    TAN            Calculate sun's Right Ascension
154    .              
155    9              
156    1              
157    7              
158    6              
159    4              enter 0.91764
160    x              
161    ATAN           
162    RCL 17         enter 360
163    MOD            
164    STO 11         Sun's Right Ascension
165    RCL 10         
166    9              
167    0              enter 90
168    STO 18         Store for later use
169    /              
170    FLOOR          
171    RCLx 18        Recall 90 and multiply
172    +              
173    RCL 11         
174    RCL/ 18        Recall 90 and divide
175    FLOOR          
176    RCLx 18        Recall 90 and multiply
177    -              
178    RCL/ 19        Recall 15 and divide
179    STO 11         Sun's Right Ascension converted into hours
180    RCL 10         
181    SIN            
182    .              
183    3              
184    9              
185    7              
186    8              
187    2              enter 0.39782
188    x              
189    STO 12         sinDec for sunset
190    ASIN           
191    COS            
192    STO 13         cosDec for sunset
193    RCL 06         
194    COS            
195    RCL 01         
196    SIN            
197    RCLx 12        
198    -              
199    RCL  01        
200    COS            
201    RCLx 13        
202    /              Sun's local Hour Angle (cosH)
203    ACOS           
204    RTN            
205    LBL 06         
206    RCL/ 19        Recall 19 and divide
207    RCL+ 11        
208    .              
209    0              
210    6              
211    5              
212    7              
213    1              enter 0.06571
214    RCLx 08        
215    -              
216    6              
217    .              
218    6              
219    2              
220    2              enter 6.622
221    -              
222    RCL 20         Recall 24
223    MOD            
224    RCL- 07        
225    RCL+ 03        
226    ->H.MS         
227    END

Version 3 - Input date in mm.ddyyyy format
Code:

Step   Instruction    Comments
001    LBL  'RS'      
002    m.dy           set date mode
003    DEG            Set degrees mode (used throughout)
004    XEQ  01        Get days since first of year
005    STO 05         
006    5              Offest from UTC (-5 represents EST)
007    +/-            Offest from UTC (-5 represents EST)
008    STO 03         Local time offset from UTC
009    9              
010    0              
011    .              
012    5              
013    ->HR           90.83333333333333 entered for degrees from zenith
                     at Sunrise/sunset (fewer steps than direct entry)
014    STO 06         degrees from zenith for Sunrise.  May be changed if
                     twilight times or other solar elevation times are desired (e.g. enter
                     50 to find times when sun is at 40 degrees altitude.)
015    3              Latitude
016    5              Latitude
017    .              Latitude
018    1              Latitude
019    2              Latitude
020    3              Latitude
021    4              Latitude
022    5              Latitude
023    STO 01         35.12345 for example (decimal)
024    9              Longitude
025    5              Longitude
026    .              Longitude
027    5              Longitude
028    4              Longitude
029    3              Longitude
030    2              Longitude
031    1              Longitude
032    +/-            Longitude
033    STO 02         -95.54321 for example (decimal)
034    1              15 Degrees per hour
035    5              15 Degrees per hour
036    STO 19         Store for later use
037    /              
038    STO 07         Longitude hour value (lngHour)
039    1              
040    8              Calculate approximate time for sunset
041    XEQ 05         
042    XEQ 06         
043    STO 15         Done calculating sunset.  Store the result
044    6              Calculate sunrise
045    XEQ 05         
046    RCL 17         Recall 360
047    X<>Y           
048    -              
049    XEQ 06         
050    RCL 15         Recall sunset.  Push sunrise to Y.
051    CL alpha       clear alpha register for new text message
052    'Ris'          
053    'e-S'          
054    'et'           
055    VIEWa          Display text
056    PSE 25         
057    CL alpha       clear alpha register for new text message
058    alphaRC# Y     copy Y register value to alpha register
059    VWalpha+ X     view alpha (sunrise) in dot-matrix, sunset time in X register.
060    STOP           
061    CPX STO Z      store sunrise and sunset times in Z and T
062    ->HR           convert sunrise time to decimal hours
063    x<>y           swap x and y
064    ->HR           convert sunset time to decimal hours
065    -              subtract for decimal hour day length
066    ->H.MS         convert day length to HMS
067    CL alpha       
068    'Day'          
069    ' Le'          
070    'ngt'          
071    'h'            
072    VWa+ X         Display text and day length
073    RTN            done
074    LBL 01         Calculate day of year given date
075    ENTER          
076    YEAR           
077    DEC X          
078    SDR 6          
079    1              
080    2              
081    .              
082    3              
083    1              
084    +              
085    LBL  02        Given two dates (on stack) get days between
086    m.dy           set date mode for delta-day entry point
087    D->J           julian day no. of input date
088    X<>Y           
089    D->J           julian day no. of last day of previous year
090    RCL- Y         days since first of year (or between dates)
091    RTN            
092    LBL 05         Subroutine for large common section
093    RCL- 07        
094    2              
095    4              enter 24
096    STO 20         Store for later use
097    /              
098    RCL+ 05        
099    STO 08         Approximate sunset time
100    .              
101    9              
102    8              
103    5              
104    6              enter 0.9856
105    x              
106    3              
107    .              
108    2              
109    8              
110    9              enter 3.289
111    -              
112    STO 09         Sun's mean anomaly
113    SIN            Calculate sun's true longitude
114    1              
115    .              
116    9              
117    1              
118    6              enter 1.916
119    x              
120    RCL+ 09        
121    2              
122    RCLx 09        
123    SIN            
124    .              
125    0              
126    2              enter 0.02
127    x              
128    +              
129    3              
130    RCLx 09        
131    SIN            
132    .              
133    0              
134    0              
135    0              
136    3              enter 0.0003
137    x              
138    +              
139    2              
140    8              
141    2              
142    .              
143    6              
144    3              
145    4              enter 282.634
146    +              
147    3              
148    6              
149    0              enter 360
150    STO 17         
151    MOD            
152    STO 10         Sun's true longitude
153    TAN            Calculate sun's Right Ascension
154    .              
155    9              
156    1              
157    7              
158    6              
159    4              enter 0.91764
160    x              
161    ATAN           
162    RCL 17         enter 360
163    MOD            
164    STO 11         Sun's Right Ascension
165    RCL 10         
166    9              
167    0              enter 90
168    STO 18         Store for later use
169    /              
170    FLOOR          
171    RCLx 18        Recall 90 and multiply
172    +              
173    RCL 11         
174    RCL/ 18        Recall 90 and divide
175    FLOOR          
176    RCLx 18        Recall 90 and multiply
177    -              
178    RCL/ 19        Recall 15 and divide
179    STO 11         Sun's Right Ascension converted into hours
180    RCL 10         
181    SIN            
182    .              
183    3              
184    9              
185    7              
186    8              
187    2              enter 0.39782
188    x              
189    STO 12         sinDec for sunset
190    ASIN           
191    COS            
192    STO 13         cosDec for sunset
193    RCL 06         
194    COS            
195    RCL 01         
196    SIN            
197    RCLx 12        
198    -              
199    RCL  01        
200    COS            
201    RCLx 13        
202    /              Sun's local Hour Angle (cosH)
203    ACOS           
204    RTN            
205    LBL 06         
206    RCL/ 19        Recall 19 and divide
207    RCL+ 11        
208    .              
209    0              
210    6              
211    5              
212    7              
213    1              enter 0.06571
214    RCLx 08        
215    -              
216    6              
217    .              
218    6              
219    2              
220    2              enter 6.622
221    -              
222    RCL 20         Recall 24
223    MOD            
224    RCL- 07        
225    RCL+ 03        
226    ->H.MS         
227    END

Register Usage:
Code:

01    Latitude
02    Longitude
03    Offset from UTC (local time)
04    Date 
05    Days from the beginning of the year.
06    Zenith
07    Longitude -> Hour Angle (lngHour)
08    Approximate time (t)
09    Sun's mean anomaly (M)
10    Sun's true longitude (L)
11    Right Ascension (RA)
12    Sun's declination (sinDec)
13    Sun's declination (cosDec)
14    Sun's local hour angle (H)
15    Sunset time
16    Year value
17    360
18    90
19    15
20    24
21    0.06571
22    6.622

**Update No. 1**
Based on Paul's and Dieter's ideas below, here is a single new version that will work in any date mode. (You must of course know what date mode the calculator is set to and enter the date accordingly.) I eliminated the accessible subroutine to calculate days between dates, since the built in wp34s ΔDAYS function can be used for that. The single program is now only 206 steps. The steps where time zone, longitude and latitude are enter have changed from above, but those are easy to figure. Otherwise, data entry, operation, and register usage remain the same, except 24 is no longer stored in register 20. I will change the wp34s.dat file to this version when I get the chance.
Code:

001    LBL  'RS'      
002    DEG            Set degrees mode (used throughout)
003    ENTER          Copy input date into Y, could use RCL X
004    YEAR           year of input date
005    DEC X          previous year
006    #12            enter 12
007    #31            enter 31
008    →DATE          create last day of previous year in current date mode.
009    X<>Y           
010    ΔDAYS          days from 1st of year
011    STO 05         
012    5              Offest from UTC (-5 represents EST)
013    +/-            Offest from UTC (-5 represents EST)
014    STO 03         Local time offset from UTC
015    9              
016    0              
017    .              
018    5              
019    ->HR           90.83333333333333 entered for degrees from zenith
                      at Sunrise/sunset (fewer steps than direct entry)
020    STO 06         degrees from zenith for Sunrise.  May be changed if
                      twilight times or other solar elevation times are desired (e.g.
                      enter 50 to find times when sun is at 40 degrees altitude.)
021    3              Latitude
022    5              Latitude
023    .              Latitude
024    1              Latitude
025    2              Latitude
026    3              Latitude
027    4              Latitude
028    5              Latitude
029    STO 01         35.12345 for example (decimal)
030    9              Longitude
031    0              Longitude
032    .              Longitude
033    5              Longitude
034    4              Longitude
035    3              Longitude
036    2              Longitude
037    1              Longitude
038    +/-            Longitude
039    STO 02         -90.54321 for example (decimal)
040    #015           15 Degrees per hour
041    STO 19         Store for later use
042    /              
043    STO 07         Longitude hour value (lngHour)
044    #018           
045    XEQ 05         Calculate approximate time for sunset
046    XEQ 06         
047    STO 15         Done calculating sunset.  Store the result
048    6              Calculate sunrise
049    XEQ 05         
050    RCL 17         Recall 360
051    RCL- Y         
052    XEQ 06         
053    RCL 15         Recall sunset.  Push sunrise to Y.
054    CL alpha       clear alpha register for new text message
055    'Ris'          
056    'e-S'          
057    'et'           
058    VIEWa          Display text
059    PSE 25         
060    CL alpha       clear alpha register for new text message
061    alphaRC# Y     copy Y register value to alpha register
062    VWalpha+ X     view alpha (sunrise) in dot-matrix, sunset time in X register.
063    STOP           
064    CPX STO Z      store sunrise and sunset times in Z and T
065    ->HR           convert sunrise time to decimal hours
066    x<>y           swap x and y
067    ->HR           convert sunset time to decimal hours
068    -              subtract for decimal hour day length
069    ->H.MS         convert day length to HMS
070    CL alpha       
071    'Day'          
072    ' Le'          
073    'ngt'          
074    'h'            
075    VWa+ X         Display text and day length
076    RTN            done
077    LBL 05         Subroutine for large common section
078    RCL- 07        
079    #024           enter 24
080    /              
081    RCL+ 05        
082    STO 08         Approximate sunset time
083    .              
084    9              
085    8              
086    5              
087    6              enter 0.9856
088    x              
089    3              
090    .              
091    2              
092    8              
093    9              enter 3.289
094    -              
095    STO 09         Sun's mean anomaly
096    SIN            Calculate sun's true longitude
097    1              
098    .              
099    9              
100    1              
101    6              enter 1.916
102    x              
103    RCL+ 09        
104    2              
105    RCLx 09        
106    SIN            
107    .              
108    0              
109    2              enter 0.02
110    x              
111    +              
112    3              
113    RCLx 09        
114    SIN            
115    3              
116    SDR 004        enter 0.0003
117    x              
118    +              
119    2              
120    8              
121    2              
122    .              
123    6              
124    3              
125    4              enter 282.634
126    +              
127    3              
128    6              
129    0              enter 360
130    STO 17         
131    MOD            
132    STO 10         Sun's true longitude
133    TAN            Calculate sun's Right Ascension
134    .              
135    9              
136    1              
137    7              
138    6              
139    4              enter 0.91764
140    x              
141    ATAN           
142    RCL 17         enter 360
143    MOD            
144    STO 11         Sun's Right Ascension
145    RCL 10         
146    #090           enter 90
147    STO 18         Store for later use
148    /              
149    FLOOR          
150    RCLx 18        Recall 90 and multiply
151    +              
152    RCL 11         
153    RCL/ 18        Recall 90 and divide
154    FLOOR          
155    RCLx 18        Recall 90 and multiply
156    -              
157    RCL/ 19        Recall 15 and divide
158    STO 11         Sun's Right Ascension converted into hours
159    RCL 10         
160    SIN            
161    .              
162    3              
163    9              
164    7              
165    8              
166    2              enter 0.39782
167    x              
168    STO 12         sinDec for sunset
169    ASIN           
170    COS            
171    STO 13         cosDec for sunset
172    RCL 06         
173    COS            
174    RCL 01         
175    SIN            
176    RCLx 12        
177    -              
178    RCL  01        
179    COS            
180    RCLx 13        
181    /              Sun's local Hour Angle (cosH)
182    ACOS           
183    RTN            
184    LBL 06         Subroutine for another common section
185    RCL/ 19        Recall 19 and divide
186    RCL+ 11        
187    .              
188    0              
189    6              
190    5              
191    7              
192    1              enter 0.06571
193    RCLx 08        
194    -              
195    6              
196    .              
197    6              
198    2              
199    2              enter 6.622
200    -              
201    #024           enter 24
202    MOD            
203    RCL- 07        
204    RCL+ 03        
205    ->H.MS         
206    END

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
02-09-2015, 10:06 AM (This post was last modified: 02-09-2015 10:16 AM by Kiwi Geoff.)
Post: #2
RE: WP 34S - Sunrise/Sunset
Hello Jeff O,
Thank you for making the WP34s "RS" code available.
I was interested to find out how accurate a few lines of trig on a calculator could be - and was *astonished* how well your program predicts the Sunrise / Sunset.
Here is a graph that shows how well your program works compared to a JPL ephemeris (DE200). Using the default location and UTC offset in your code.
[Image: 2015_SR_diff.png]
The averaged "sunrise error" is just 1 second over 2015.
This is more than enough accuracy for the purpose - because "optical effects" on the horizon can have a much greater effect on when we 'see' the sunrise.
Here is a sample of the DE200 derived "RS" for April 2015 for those that would like to run your program Jeff, and have an accurate reference to show just how well the WP34s program performs. Using the default location and UTC offset in the code.
Code:

2015-04-01 r  07:09:15.878  Az =  83.874
2015-04-01 t  13:26:02.520  El =  59.503
2015-04-01 s  19:43:23.599  Az = 276.373
2015-04-02 r  07:07:52.315  Az =  83.403
2015-04-02 t  13:25:44.701  El =  59.888
2015-04-02 s  19:44:11.555  Az = 276.845
2015-04-03 r  07:06:29.010  Az =  82.932
2015-04-03 t  13:25:27.009  El =  60.271
2015-04-03 s  19:44:59.498  Az = 277.315
2015-04-04 r  07:05:05.996  Az =  82.464
2015-04-04 t  13:25:09.463  El =  60.653
2015-04-04 s  19:45:47.441  Az = 277.783
2015-04-05 r  07:03:43.302  Az =  81.997
2015-04-05 t  13:24:52.088  El =  61.033
2015-04-05 s  19:46:35.396  Az = 278.249
2015-04-06 r  07:02:20.962  Az =  81.532
2015-04-06 t  13:24:34.904  El =  61.411
2015-04-06 s  19:47:23.375  Az = 278.714
2015-04-07 r  07:00:59.005  Az =  81.070
2015-04-07 t  13:24:17.933  El =  61.787
2015-04-07 s  19:48:11.389  Az = 279.176
2015-04-08 r  06:59:37.464  Az =  80.609
2015-04-08 t  13:24:01.195  El =  62.162
2015-04-08 s  19:48:59.450  Az = 279.636
2015-04-09 r  06:58:16.369  Az =  80.151
2015-04-09 t  13:23:44.712  El =  62.534
2015-04-09 s  19:49:47.565  Az = 280.094
2015-04-10 r  06:56:55.752  Az =  79.695
2015-04-10 t  13:23:28.503  El =  62.904
2015-04-10 s  19:50:35.745  Az = 280.549
2015-04-11 r  06:55:35.644  Az =  79.241
2015-04-11 t  13:23:12.587  El =  63.272
2015-04-11 s  19:51:23.995  Az = 281.002
2015-04-12 r  06:54:16.074  Az =  78.790
2015-04-12 t  13:22:56.983  El =  63.638
2015-04-12 s  19:52:12.321  Az = 281.452
2015-04-13 r  06:52:57.073  Az =  78.342
2015-04-13 t  13:22:41.708  El =  64.001
2015-04-13 s  19:53:00.727  Az = 281.900
2015-04-14 r  06:51:38.669  Az =  77.897
2015-04-14 t  13:22:26.775  El =  64.362
2015-04-14 s  19:53:49.215  Az = 282.344
2015-04-15 r  06:50:20.891  Az =  77.454
2015-04-15 t  13:22:12.201  El =  64.720
2015-04-15 s  19:54:37.783  Az = 282.786
2015-04-16 r  06:49:03.766  Az =  77.014
2015-04-16 t  13:21:57.997  El =  65.076
2015-04-16 s  19:55:26.431  Az = 283.225
2015-04-17 r  06:47:47.318  Az =  76.578
2015-04-17 t  13:21:44.174  El =  65.428
2015-04-17 s  19:56:15.154  Az = 283.660
2015-04-18 r  06:46:31.572  Az =  76.145
2015-04-18 t  13:21:30.742  El =  65.778
2015-04-18 s  19:57:03.946  Az = 284.092
2015-04-19 r  06:45:16.552  Az =  75.715
2015-04-19 t  13:21:17.709  El =  66.125
2015-04-19 s  19:57:52.801  Az = 284.520
2015-04-20 r  06:44:02.282  Az =  75.289
2015-04-20 t  13:21:05.084  El =  66.469
2015-04-20 s  19:58:41.712  Az = 284.945
2015-04-21 r  06:42:48.788  Az =  74.866
2015-04-21 t  13:20:52.876  El =  66.809
2015-04-21 s  19:59:30.672  Az = 285.367
2015-04-22 r  06:41:36.093  Az =  74.447
2015-04-22 t  13:20:41.094  El =  67.146
2015-04-22 s  20:00:19.671  Az = 285.784
2015-04-23 r  06:40:24.225  Az =  74.031
2015-04-23 t  13:20:29.747  El =  67.480
2015-04-23 s  20:01:08.704  Az = 286.198
2015-04-24 r  06:39:13.212  Az =  73.620
2015-04-24 t  13:20:18.847  El =  67.811
2015-04-24 s  20:01:57.764  Az = 286.608
2015-04-25 r  06:38:03.079  Az =  73.213
2015-04-25 t  13:20:08.402  El =  68.138
2015-04-25 s  20:02:46.843  Az = 287.013
2015-04-26 r  06:36:53.856  Az =  72.810
2015-04-26 t  13:19:58.425  El =  68.461
2015-04-26 s  20:03:35.934  Az = 287.414
2015-04-27 r  06:35:45.572  Az =  72.411
2015-04-27 t  13:19:48.925  El =  68.780
2015-04-27 s  20:04:25.031  Az = 287.811
2015-04-28 r  06:34:38.253  Az =  72.017
2015-04-28 t  13:19:39.913  El =  69.096
2015-04-28 s  20:05:14.126  Az = 288.203
2015-04-29 r  06:33:31.931  Az =  71.627
2015-04-29 t  13:19:31.401  El =  69.407
2015-04-29 s  20:06:03.211  Az = 288.591
2015-04-30 r  06:32:26.633  Az =  71.242
2015-04-30 t  13:19:23.398  El =  69.715
2015-04-30 s  20:06:52.277  Az = 288.974
Many thanks for the program Jeff.

Regards, Kiwi Geoff (Christchurch, New Zealand).
Find all posts by this user
Quote this message in a reply
02-09-2015, 09:13 PM
Post: #3
RE: WP 34S - Sunrise/Sunset
No thought of incorporating the WP 34S date functions to avoid having three programs?

- Pauli
Find all posts by this user
Quote this message in a reply
02-10-2015, 10:31 PM
Post: #4
RE: WP 34S - Sunrise/Sunset
(02-09-2015 09:13 PM)Paul Dale Wrote:  No thought of incorporating the WP 34S date functions to avoid having three programs?

Yes, this would make the three different versions obsolete and the program would become more compact. For instance →DATE can be used to generate 31 December according to the current date mode setting, and the subroutine at LBL 02 can be replaced by the ΔDAYS command:

Code:
...
ENTER
YEAR
DEC X
#012
#031
→DATE
ΔDAYS
+/-
...

I just added such a version of the program to my 34s and stored it in the library. I think it's really nice and useful. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
02-11-2015, 03:22 AM
Post: #5
RE: WP 34S - Sunrise/Sunset
(02-09-2015 10:06 AM)Kiwi Geoff Wrote:  Many thanks for the program Jeff.

Regards, Kiwi Geoff (Christchurch, New Zealand).

You are welcome, credit for the program goes to Marwan, I just ported to the wp34s. Thanks for checking the accuracy, I am pleased with the results.

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
02-11-2015, 07:48 AM (This post was last modified: 02-11-2015 07:49 AM by Thomas_Sch.)
Post: #6
RE: WP 34S - Sunrise/Sunset
(02-10-2015 10:31 PM)Dieter Wrote:  
Code:
...
ENTER
YEAR
DEC X
#012
#031
→DATE
ΔDAYS
+/-
...

I just added such a version of the program to my 34s and stored it in the library. I think it's really nice and useful. ;-)

Dieter
Hello Dieter,

could you please post your modified version of the program?

Many thanks!
Thomas
Find all posts by this user
Quote this message in a reply
02-11-2015, 03:06 PM (This post was last modified: 02-11-2015 03:09 PM by Dieter.)
Post: #7
RE: WP 34S - Sunrise/Sunset
(02-11-2015 07:48 AM)Thomas_Sch Wrote:  could you please post your modified version of the program?

Im am still working on it. My curent version also takes care of daylight savings time. Since 1996 the European Union switches to DST on last Sunday in March and back to standard time on last Sunday in October (both at 1:00 UTC). The following subroutine sets flag A (so that the "=" announciator comes on) if a date falls in the DST period.

Code:
LBL"DST"
STO 00    // or any other unused register
YEAR
FILL
#011
XEQ 01
x<>Y
#004
XEQ 01
RCL 00
D→J
SF A      // set DST flag
x≥? Y     // IF date < first DST day 
x≥? Z     // OR date ≥ first standard time day
CF A      // THEN clear DST flag
RTN

LBL 01
#001
→DATE     // = 1 April resp. November
D→J
#007
IDIV
RCLx L    // Monday on or before 1 April resp. November
DEC X     // last Sunday of March resp. October
RTN

input  X: date (format according to date mode setting)

       Z: first autumn day with standard time (JDN)
output Y: first spring day with DST (JDN)
       X: date (JDN)

       Flag A set: DST
       Flag A clr: standard time

I may post my version of Jeff's program, but not without his permission. So Jeff, is this OK for you?

Dieter
Find all posts by this user
Quote this message in a reply
02-11-2015, 05:49 PM
Post: #8
RE: WP 34S - Sunrise/Sunset
(02-11-2015 03:06 PM)Dieter Wrote:  I may post my version of Jeff's program, but not without his permission. So Jeff, is this OK for you?

Dieter

Certainly. I suggest posting in a separate new thread in the software library so it is not buried in this one below mine. Maybe post a message in this thread with a link to your new version.

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
02-11-2015, 06:09 PM
Post: #9
RE: WP 34S - Sunrise/Sunset
Thanks to you all, supporting me learning how to write programs for the WP 34S !
Find all posts by this user
Quote this message in a reply
02-11-2015, 08:03 PM
Post: #10
RE: WP 34S - Sunrise/Sunset
(02-09-2015 09:13 PM)Paul Dale Wrote:  No thought of incorporating the WP 34S date functions to avoid having three programs?

- Pauli

Not until you prompted me, see above for that plus other slight code improvements.

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
02-11-2015, 09:06 PM
Post: #11
RE: WP 34S - Sunrise/Sunset
(02-11-2015 08:03 PM)Jeff O. Wrote:  Not until you prompted me, see above for that plus other slight code improvements.

Nice, thanks. Can we include this in the WP 34S library examples?


- Pauli

PS: you didn't use LocR to allocate local registers and avoid polluting the global register pool.
Find all posts by this user
Quote this message in a reply
02-11-2015, 09:46 PM
Post: #12
RE: WP 34S - Sunrise/Sunset
(02-11-2015 05:49 PM)Jeff O. Wrote:  Certainly. I suggest posting in a separate new thread in the software library so it is not buried in this one below mine. Maybe post a message in this thread with a link to your new version.

Fine. I just posted my version in this thread. The output is different from your version as all three results are returned simultaneously. They appear in the two display lines as well as on the stack (X=day length, Y=sunset, Z=sunrise).

As usual, comments and corrections are welcome.

Dieter
Find all posts by this user
Quote this message in a reply
02-13-2015, 12:19 AM
Post: #13
RE: WP 34S - Sunrise/Sunset
(02-11-2015 09:06 PM)Paul Dale Wrote:  Nice, thanks. Can we include this in the WP 34S library examples?


- Pauli

PS: you didn't use LocR to allocate local registers and avoid polluting the global register pool.

Yes, of course you may include. But I am working on a new improved version that implements Dieter's DST code, plus uses local registers, so wait for that one. Should be tonight or tomorrow.

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
02-13-2015, 06:52 AM
Post: #14
RE: WP 34S - Sunrise/Sunset
(02-13-2015 12:19 AM)Jeff O. Wrote:  Yes, of course you may include. But I am working on a new improved version that implements Dieter's DST code, plus uses local registers, so wait for that one. Should be tonight or tomorrow.

May I suggest a few more improvements?

The program in its current version uses lots of registers. Some for constants that are better entered directly (e.g. 90, 15, 18, 360), cf. my version. Some more registers are used for intermediate results that are not recalled later. Without these, the register count could drop to ~10 (instead of >20 now).

Regarding the DST code: my suggestion implements the rules within the EU since 1996 (in earlier years different rules applied, e.g. DST ending in September). This is fine for EU citizens, but users in different places of the world should implement their own DST routine. For "worldwide use" I'd suggest removing the DST part and simply have the user set Flag A if he wants to see DST results (+1 hour). Alternatively the program could always return UTC results. Which would please our UK members and annoy those in Australia/NZ. #-)

Finally: like many other HPs, the 34s features direct h.ms arithmetics, so when computing the day length there is no need to convert to decimal hours, subtract and convert back to h.ms again: simply use H.MS– instead. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
02-13-2015, 02:19 PM
Post: #15
RE: WP 34S - Sunrise/Sunset
(02-13-2015 06:52 AM)Dieter Wrote:  May I suggest a few more improvements?

Of course, I have already made a lot of improvements based on your methods.

(02-13-2015 06:52 AM)Dieter Wrote:  The program in its current version uses lots of registers. Some for constants that are better entered directly (e.g. 90, 15, 18, 360), cf. my version. Some more registers are used for intermediate results that are not recalled later. Without these, the register count could drop to ~10 (instead of >20 now).

I went through and eliminated several stored constants by using inline integers. I now use 16 registers. 360 is outside the range of inline integers, but its entry could still be shortened by one step via #036 SDL 001. I still store it the first time it is entered because it is used two more times, so storage and later recall saves a step. 90 and 15 can of course be entered via inline integer, but I store it because when it is used later, RCL arithmetic is used to shave some steps. (In my usage, it saves one step in each case.) But it may be possible to eliminate others by re-use or using the stack, I will look into that.

(02-13-2015 06:52 AM)Dieter Wrote:  Regarding the DST code: my suggestion implements the rules within the EU since 1996 (in earlier years different rules applied, e.g. DST ending in September). This is fine for EU citizens, but users in different places of the world should implement their own DST routine. For "worldwide use" I'd suggest removing the DST part and simply have the user set Flag A if he wants to see DST results (+1 hour). Alternatively the program could always return UTC results. Which would please our UK members and annoy those in Australia/NZ. #-)

Your DST routine was easy to modify (assuming I did it correctly) to implement the DST rules followed in the US (and Canada, I believe, that being that DST starts 2nd Sunday in March, ends 1st Sunday in November). I like having it automatically handled in the program, but a manual mode may be more appropriate for some users, in which case your suggestion to remove the DST code and simply set or not set Flag A is excellent. Flag A lights the big “=” annunciator in the display, so that provides a nice visual indication.

(02-13-2015 06:52 AM)Dieter Wrote:  Finally: like many other HPs, the 34s features direct h.ms arithmetics, so when computing the day length there is no need to convert to decimal hours, subtract and convert back to h.ms again: simply use H.MS– instead. ;-)

Yet another example of my failing to utilize the full capabilities of the wp34s as I attempted to port over versions written for less capable calculators.

Thanks for your review and comments.

Jeff

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
02-13-2015, 08:01 PM
Post: #16
RE: WP 34S - Sunrise/Sunset
(02-13-2015 02:19 PM)Jeff O. Wrote:  I went through and eliminated several stored constants by using inline integers. I now use 16 registers.

There are some intermediate values that are stored, but I cannot see that they are used later.

(02-13-2015 02:19 PM)Jeff O. Wrote:  360 is outside the range of inline integers, but its entry could still be shortened by one step via #036 SDL 001. I still store it the first time it is entered because it is used two more times, so storage and later recall saves a step. 90 and 15 can of course be entered via inline integer, but I store it because when it is used later, RCL arithmetic is used to shave some steps. (In my usage, it saves one step in each case.)

That's saving steps by using more registers. ;-) If I get it right, in standard precision every register accounts for four (!) program steps. This means that the use of a separate register should save at least five steps to have a positive effect on memory usage. Otherwise using the same constant several times is the better solution.

(02-13-2015 02:19 PM)Jeff O. Wrote:  Your DST routine was easy to modify (assuming I did it correctly) to implement the DST rules followed in the US (and Canada, I believe, that being that DST starts 2nd Sunday in March, ends 1st Sunday in November).

Is this really observed by all states? Generally, implementing a different DST routine would be fine for the US and Canada, but for a international use it's not better than the EU version I posted – or any other for any region of the world.

(02-13-2015 02:19 PM)Jeff O. Wrote:  I like having it automatically handled in the program, but a manual mode may be more appropriate for some users

Maybe the best solution is a manual mode along with some examples for writing your own localized DST routine. Here your US/Canada version as well as mine for the EU could be included. The user has to adjust the code anyway with his local timezone and location, so the idea of an individual DST routine is not too far off. All it has to do is set flag A during the DST period.

Dieter
Find all posts by this user
Quote this message in a reply
02-15-2015, 07:57 PM (This post was last modified: 02-15-2015 08:01 PM by Dieter.)
Post: #17
RE: WP 34S - Sunrise/Sunset
(02-13-2015 02:19 PM)Jeff O. Wrote:  Your DST routine was easy to modify (assuming I did it correctly) to implement the DST rules followed in the US (and Canada

The code looks OK. The two XEQ 01 calls return the last Sunday in October resp. in February, adding 7 resp. 14 days gives the first (second) Sunday in November (March). Only the comment in the code needs an update:

Code:
228    #007           
229    IDIV           
230    RCLx L          Monday on or before 1 March resp. 1 November
231    DEC X           last Sunday of February resp. October
232    END

I would like to ask a final question: the accuracy for 2015 is astonishing. But what about other years? I assume the program uses constants that are optimized for a certain period. How well does the program perform for e.g. 2000, 2050 or 2100? What is the range of years the used constants were optimized for?

Dieter
Find all posts by this user
Quote this message in a reply
02-15-2015, 08:31 PM
Post: #18
RE: WP 34S - Sunrise/Sunset
(02-15-2015 07:57 PM)Dieter Wrote:  Only the comment in the code needs an update:
Thanks, I missed that. Fixed in my listing.

(02-15-2015 07:57 PM)Dieter Wrote:  I would like to ask a final question: the accuracy for 2015 is astonishing. But what about other years? I assume the program uses constants that are optimized for a certain period. How well does the program perform for e.g. 2000, 2050 or 2100? What is the range of years the used constants were optimized for?

Dieter
That's a good question. I simply copied Marwan's work, so he or someone else with knowledge of such calculations would have to answer.

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
02-21-2015, 02:17 AM
Post: #19
RE: WP 34S - Sunrise/Sunset
(01-28-2015 08:08 PM)Jeff O. Wrote:  **Update No. 2**
Yet another version, this time with local registers and DST correction built in. (Thanks and credit to Dieter for the DST code.) DST settings are for the US, DST beginning on the second Sunday in March and ending on the first Sunday in November.
Hi Jeff,

Those of us living below the equator are coming up to our Autumn (or Spring in Northern Hemisphere) - our seasons are the opposite. To make Dieter's DST code work correctly for both North & South Hemispheres, I ADDED 3 lines of code ( marked with **NEW** ) as per the following - to make the code work properly for us "Down Under".
Code:

...
SF A           //  Set Flag A (assume DST)
x[>=]? Y       //  IF date < 1st DST day
x[>=]? Z       //  OR date = 1st day with standard time
CF A           //  THEN clear flag A
RCL .01        //  **NEW**  Recall Latitude
x[<=]0?        //  **NEW**  Southern Hemisphere ?
FF A           //  **NEW**  "Flip" DST Flag for Southern Hemisphere 
RTN
...
N.B. This does not include the actual DST begin and end dates - which are different for each country etc.

Regards, Kiwi Geoff.
Find all posts by this user
Quote this message in a reply
02-21-2015, 07:05 AM
Post: #20
RE: WP 34S - Sunrise/Sunset
(02-21-2015 02:17 AM)Kiwi Geoff Wrote:  Those of us living below the equator are coming up to our Autumn (or Spring in Northern Hemisphere) - our seasons are the opposite. To make Dieter's DST code work correctly for both North & South Hemispheres, I ADDED 3 lines of code ( marked with **NEW** ) as per the following - to make the code work properly for us "Down Under".

This requires R.01 to hold the latitude – but this happens not until further down the code. So another modification is required: move the latitude entry (lines 043...051) upwards, for instance like this:

Code:
Step   Instruction    Comments
001    LBL  'RS'      
002    LocR 016       allocate 16 local registers
003    DEG            Set degrees mode (used throughout)
004    STO .00        store input date
005    YEAR           get year of input date
006    FILL           fill stack
007    #011           enter 11, for November
008    XEQ 01         calculate j-day for last Sunday in October
009    #007           enter 7
010    +              add 7 for US DST end 1st Sunday in November
011    x<>Y           get year of input date back
012    #003           enter 3, for March
013    XEQ 01         calculate j-day for last Sunday in February
014    #014           enter 14
015    +              add 14 for US DST start 2nd Sunday in March
016    RCL .00        recall input date
017    D→J            get j-day of input date
018    SF A           set DST flag
019    x≥? Y          IF date < first DST day 
020    x≥? Z          OR date ≥ first standard time day
021    CF A           THEN clear DST flag
022    RCL .00        Recall input date
023    ENTER          Copy input date into Y, could use RCL X, etc.
024    YEAR           year of input date
025    DEC X          previous year
026    #12            enter 12
027    #31            enter 31
028    →DATE          create last day of previous year in current date mode.
029    X<>Y           
030    ΔDAYS          days from beginning of year
031    STO .04        
032    3              Latitude
033    5              Latitude
034    .              Latitude
035    1              Latitude
036    2              Latitude
037    3              Latitude
038    4              Latitude
039    5              Latitude
040    STO .01        35.12345 for example (decimal)
041    x≤0?           Southern hemisphere?
042    FF A           then reverse DST setting
043    9              Longitude
044    0              Longitude
045    .              Longitude
046    5              Longitude
047    4              Longitude
048    3              Longitude
049    2              Longitude
050    1              Longitude
051    +/-            Longitude
052    STO .02        -90.54321 for example (decimal)
053    #015           15 Degrees per hour
054    STO .09        Store for later use
055    /              
056    STO .06        Longitude hour value (lngHour)
057    5              Offset from UTC (-5 represents EST)
058    +/-            Offset from UTC (-5 represents EST)
059    FS?  A         If flag A set, DST in effect for input date
060    INC X          If flag A set, increment offset from UTC
061    STO .03        Local time offset from UTC
062    9              
063    0              
064    .              
065    5              
066    ->HR           90.83333333333333 entered for degrees from zenith at
                      Sunrise/sunset (fewer steps than direct entry)
067    STO .05        degrees from zenith for Sunrise.  May be changed if
                      twilight times or other solar elevation times are desired (e.g.
                      enter 50 to find times when sun is at 40 degrees altitude.)

Continue with line 066 of the original code (#018 XEQ 05 ...)

I also changed FS?C A (line 59) to FS? A because I think having the "=" annunciator signal a DST result is a useful feature.

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)