@@ -37,8 +37,6 @@ the dates and times of lunar quarters to be calculated.
37
37
Caveat. I am not an astronomer. If there are errors in the fundamental
38
38
algorithms I am unlikely to be able to offer an opinion, still less a fix.
39
39
40
- The ` moonphase ` module is currently under development: API changes are possible.
41
-
42
40
Moon phase options have been removed from ` sun_moon ` because accuracy was poor.
43
41
44
42
## 1.1 Applications
@@ -73,9 +71,9 @@ licence.
73
71
## 1.3 Installation
74
72
75
73
Installation copies files from the ` astronomy ` directory to a directory
76
- ` \lib\sched ` on the target. This is for optional use with the
74
+ ` \lib\sched ` on the target. This directory eases optional use with the
77
75
[ schedule module] ( https://github.com/peterhinch/micropython-async/blob/master/v3/docs/SCHEDULE.md ) .
78
- This may be done with the official
76
+ Installation may be done with the official
79
77
[ mpremote] ( https://docs.micropython.org/en/latest/reference/mpremote.html ) :
80
78
``` bash
81
79
$ mpremote mip install " github:peterhinch/micropython-samples/astronomy"
@@ -87,7 +85,8 @@ On networked platforms it may alternatively be installed with
87
85
```
88
86
Currently these tools install to ` /lib ` on the built-in Flash memory. To install
89
87
to a Pyboard's SD card [ rshell] ( https://github.com/dhylands/rshell ) may be used.
90
- Move to ` micropython-samples ` on the PC, run ` rshell ` and issue:
88
+ Clone the repo and move to ` micropython-samples ` on the PC, run ` rshell ` and
89
+ issue:
91
90
```
92
91
> rsync astronomy /sd/sched
93
92
```
@@ -104,13 +103,14 @@ from sched.sun_moon import RiSet
104
103
Time is a slippery concept when applied to global locations. This document uses
105
104
the following conventions:
106
105
* ` UTC ` The international time standard based on the Greenwich meridian.
107
- * ` LT (Local time) ` Time as told on a clock at the device's location. May include
106
+ * ` LT (Local time) ` Time on a clock at the device's location. May include
108
107
daylight saving time (` DST ` ).
109
108
* ` MT (Machine time) ` Time defined by the platform's hardware clock.
110
- * ` LTO (Local time offset) ` A ` RiSet ` instance contains a user supplied ` LTO ` .
111
- The class computes rise and set times in UTC, using ` LTO ` to output results in
112
- ` LT ` via ` LT = UTC + LTO ` . If an application maintains ` LTO ` to match ` DST ` , the
113
- rise and set times will be in ` LT ` .
109
+ * ` LTO (Local time offset) ` A ` RiSet ` instance contains a user supplied ` LTO `
110
+ intended for timezone support. The class computes rise and set times in UTC,
111
+ using ` LTO ` to compute results using ` RESULT = UTC + LTO ` . For output in ` LT `
112
+ there are two options: periodically adjust ` LTO ` to handle DST or (better)
113
+ provide a ` dst ` function so that conversion is automatic.
114
114
115
115
# 2. The RiSet class
116
116
@@ -140,10 +140,16 @@ time (`MT`).
140
140
(6 is Civil, 12 is Nautical, 18 is Astronomical). By default twilight times are
141
141
not computed, saving some processor time. Offsets are positive numbers
142
142
representing degrees below the horizon where twilight is deemed to start and end.
143
+ * ` dst=lambda x: x ` This is an optional user defined function for Daylight
144
+ Saving Time (DST). The assumption is that machine time is not changed, typically
145
+ permanently in winter time. A ` dst ` function handles seasonal changes. The
146
+ default assumes no DST is applicable. For how to write a DST function for a
147
+ given country see [ section 6.4] ( ./README.md#64-dst ) .
143
148
144
149
By default when an application instantiates ` RiSet ` for the first time the
145
150
constructor prints the system date and time. This can be inhibited by setting
146
- the class variable ` verbose ` to ` False ` .
151
+ the class variable ` verbose ` to ` False ` . The purpose is to alert the user to a
152
+ common source of error where machine time is not set.
147
153
148
154
## 2.2 Methods
149
155
@@ -162,8 +168,9 @@ horizon.
162
168
* ` has_risen(sun: bool)->bool ` Returns ` True ` if the selected object has risen.
163
169
* ` has_set(sun: bool)->bool ` Returns ` True ` if the selected object has set.
164
170
* ` set_lto(t) ` Set local time offset ` LTO ` in hours relative to UTC. Primarily
165
- intended for system longitude. The value is checked to ensure
166
- ` -15.0 < lto < 15.0 ` . See [ section 2.3] ( ./README.md#23-effect-of-local-time ) .
171
+ intended for timezone support, but this function can be used to support DST. The
172
+ value is checked to ensure ` -15.0 < lto < 15.0 ` . See
173
+ [ section 2.3] ( ./README.md#23-effect-of-local-time ) .
167
174
168
175
The return value of the rise and set method is determined by the ` variant ` arg.
169
176
In all cases rise and set events are identified which occur in the current 24
@@ -173,7 +180,8 @@ with the moon at most locations, and with the sun in polar regions.
173
180
Variants:
174
181
* 0 Return integer seconds since midnight ` LT ` (or ` None ` if no event).
175
182
* 1 Return integer seconds since since epoch of the MicroPython platform
176
- (or ` None ` ). This is machine time (` MT ` ) as per ` time.time() ` .
183
+ (or ` None ` ). This allows comparisons with machine time (` MT ` ) as per
184
+ ` time.time() ` .
177
185
* 2 Return text of form hh:mm: ss (or --:--:--) being local time (` LT ` ).
178
186
179
187
Example constructor invocations:
@@ -184,13 +192,16 @@ r = RiSet(lat=-33.87667, long=151.21, lto=11) # Sydney 33°52′04″S 151°12
184
192
```
185
193
## 2.3 Effect of local time
186
194
187
- MicroPython has no concept of local time . The hardware platform has a clock
195
+ MicroPython has no concept of timezones . The hardware platform has a clock
188
196
which reports machine time (` MT ` ): this might be set to local winter time or
189
197
summer time. The ` RiSet ` instances' ` LTO ` should be set to represent the
190
198
difference between ` MT ` and ` UTC ` . In continuously running applications it is
191
199
best to avoid changing the hardware clock (` MT ` ) for reasons discussed below.
192
- Daylight savings time should be implemented by changing the ` RiSet ` instances'
193
- ` LTO ` .
200
+ Daylight savings time may be implemented in one of two ways:
201
+ * By changing the ` RiSet ` instances' ` LTO ` accordingly.
202
+ * Or by providing a ` dst ` function as discussed in
203
+ [ section 6.4] ( ./README.md#64-dst ) . This is the preferred solution as DST is then
204
+ handled automatically.
194
205
195
206
Rise and set times are computed relative to UTC and then adjusted using the
196
207
` RiSet ` instance's ` LTO ` before being returned (see ` .adjust() ` ). This means
@@ -199,7 +210,7 @@ is used in determining rise and set times.
199
210
200
211
The ` .has_risen() ` , ` .has_set() ` and ` .is_up() ` methods do use machine time
201
212
(` MT ` ) and rely on ` MT == UTC + LTO ` : if ` MT ` has drifted, precision will be
202
- reduced .
213
+ lost at times close to rise and set events .
203
214
204
215
The constructor and the ` set_day() ` method set the instance's date relative to
205
216
` MT ` . They use only the date component of ` MT ` , hence they may be run at any
@@ -231,16 +242,12 @@ synchronisation is required it is best done frequently to minimise the size of
231
242
jumps.
232
243
233
244
For this reason changing system time to accommodate daylight saving time is a
234
- bad idea. It is usually best to run winter time all year round. Where a DST
235
- change occurs, the ` RiSet.set_lto() ` method should be run to ensure that ` RiSet `
236
- operates in current local time.
245
+ bad idea. It is usually best to run winter time all year round and to use the
246
+ ` dst ` constructor arg to handle time changes.
237
247
238
248
# 3. Utility functions
239
249
240
250
` now_days() -> int ` Returns the current time as days since the platform epoch.
241
- ` abs_to_rel_days(days: int) -> int ` Takes a number of days since the Unix epoch
242
- (1970,1,1) and returns a number of days relative to the current date. Platform
243
- independent. This facilitates testing with pre-determined target dates.
244
251
245
252
# 4. Demo script
246
253
@@ -288,6 +295,10 @@ Maximum error 0. Expect 0 on 64-bit platform, 30s on 32-bit
288
295
```
289
296
Code comments show times retrieved from ` timeanddate.com ` .
290
297
298
+ The script includes some commented out code at the end. This tests ` is_up ` ,
299
+ ` has_risen ` and ` has_set ` over 365 days. It is commented out to reduce printed
300
+ output.
301
+
291
302
# 5. Scheduling events
292
303
293
304
A likely use case is to enable events to be timed relative to sunrise and set.
@@ -436,7 +447,8 @@ to produce a time-precise value. The five quarters are calculated for the
436
447
lunation including the midnight at the start of the specified day.
437
448
* ` set_lto(t:float) ` Redefine the local time offset, ` t ` being in hours as
438
449
per the constructor arg.
439
- * ` datum(text: bool = True) ` Returns the current datum.
450
+ * ` datum(text: bool = True) ` Returns the current datum in secs since local epoch
451
+ or in human-readable text form.
440
452
441
453
## 6.3 Usage examples
442
454
@@ -506,4 +518,6 @@ than 3s.
506
518
507
519
## 7.2 MoonPhase class
508
520
509
- TODO
521
+ This uses Python's arbitrary precision integers to overcome the limitations of
522
+ 32-bit floating point units. Results on 32 bit platforms match those on 64-bits
523
+ to within ~ 1 minute. Results match those on ` timeanddate.com ` within ~ 3 minutes.
0 commit comments