-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunctions.py
378 lines (334 loc) · 15.9 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import re
import json
from qgis.core import QgsPointXY, QgsGeometry, QgsExpression, QgsProject
from qgis.PyQt.QtCore import QDateTime
from qgis.utils import qgsfunction
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo, available_timezones
from skyfield.api import load, load_file, wgs84
from skyfield import almanac
from .wintz import win_tz_map
from .utils import settings
# import traceback
group_name = 'Earth Sun Moon'
def InitFunctions():
QgsExpression.registerFunction(esm_moon_phase)
QgsExpression.registerFunction(esm_moon_zenith)
QgsExpression.registerFunction(esm_sun_zenith)
QgsExpression.registerFunction(esm_sun_moon_info)
QgsExpression.registerFunction(esm_local_datetime)
QgsExpression.registerFunction(esm_local_qdatetime)
def UnloadFunctions():
QgsExpression.unregisterFunction('esm_moon_phase')
QgsExpression.unregisterFunction('esm_moon_zenith')
QgsExpression.unregisterFunction('esm_sun_zenith')
QgsExpression.unregisterFunction('esm_sun_moon_info')
QgsExpression.unregisterFunction('esm_local_datetime')
QgsExpression.unregisterFunction('esm_local_qdatetime')
def get_datetime(dt, tz_name):
if not tz_name:
if isinstance(dt, QDateTime): # QDateTime format - terrible timezone support
tz_name = dt.timeZoneAbbreviation()
elif isinstance(dt, (int,float)): # Assume it is a Epoch timestamp
tz_name = 'UTC'
elif isinstance(dt, datetime): # Python datetime format
try:
tz_name = dt.tzinfo.zone
except Exception:
tz_name = dt.tzname()
if tz_name in win_tz_map:
tz_name = win_tz_map[tz_name]
if tz_name not in available_timezones():
if isinstance(dt, datetime):
if dt.tzinfo:
offset = int(dt.tzinfo.utcoffset(dt).total_seconds()/3600.0)
tz_name = 'Etc/GMT{:+d}'.format(-offset)
else:
tz_name = 'UTC'
else:
tz_name = 'UTC'
tz = ZoneInfo(tz_name)
if isinstance(dt, QDateTime):
dt_aware = dt.toPyDateTime()
dt_aware = dt_aware.replace(tzinfo=tz)
elif isinstance(dt, (int,float)):
dt_aware = datetime.fromtimestamp(dt/1000.0, tz) # Assune input in ms and need to covert to s
else:
dt_aware = dt.replace(tzinfo=tz)
return(dt_aware)
def utc_datetime(dt):
return(dt.astimezone(ZoneInfo('UTC')))
@qgsfunction(-1, group=group_name)
def esm_moon_phase(values, feature, parent):
"""
Given a date and time, return the moon's phase in degrees where 0° is the New Noon, 90° is First Quarter, 180° is Full Moon, and 270° is Last Quarter.
<h4>Syntax</h4>
<p><b>esm_moon_phase</b>( <i>datetime</i>[, tz_name] )</p>
<h4>Arguments</h4>
<p><i>datetime</i> → this can be a native QGIS QDateTime, a python datetime, or a timestamp in ms.</p>
<p><i>tz_name</i> → optional timezone IANA formatted string such as 'UTC' or 'America/New_York'. If not specified and the input is a timestamp, then it is assumed to be UTC. If it is a python datetime format or QDateTime format, then the datetime is checked to see if there is an associated timezone to use. If not, UTC is used.</p>
<h4>Example usage</h4>
<ul>
<li><b>esm_moon_phase</b>(1483225200000) → returns moon phase 30.601519722460964 for the timestamp</li>
<li><b>esm_moon_phase</b>(<b>make_datetime</b>(2023,5,4,17,45,30), 'America/New_York') → Moon phase on 2023-05-04T17:45:30-0400 is 169.79073790632003.</li>
</ul>
"""
if len(values) < 1 or len(values) > 2:
parent.setEvalErrorString("Error: invalid number of arguments")
return
dt = values[0]
if len(values) == 2:
tz_name = values[1]
else:
tz_name = None
try:
dt = get_datetime(dt, tz_name)
utc = dt.astimezone(ZoneInfo('UTC'))
ts = settings.timescale()
t = ts.from_datetime(utc)
eph = settings.ephem()
phase = almanac.moon_phase(eph, t)
return(float(phase.degrees))
except Exception:
# traceback.print_exc()
parent.setEvalErrorString("Error: datetime, timezone error in calculation moon phase")
return
@qgsfunction(-1, group=group_name)
def esm_sun_zenith(values, feature, parent):
"""
Given a date and time, return the EPSG:4326 coordinate point where the sun is directly overhead.
<h4>Syntax</h4>
<p><b>esm_sun_zenith</b>( <i>datetime</i>[, tz_name] )</p>
<h4>Arguments</h4>
<p><i>datetime</i> → this can be a native QGIS QDateTime, a python datetime, or a timestamp in ms.</p>
<p><i>tz_name</i> → optional timezone IANA formatted string such as 'UTC' or 'America/New_York'. If not specified and the input is a timestamp, then it is assumed to be UTC. If it is a python datetime format or QDateTime format, then the datetime is checked to see if there is an associated timezone to use. If not, UTC is used.</p>
<h4>Example usage</h4>
<ul>
<li><b>geom_to_wkt( esm_sun_zenith( make_datetime</b>(2023,5,4,17,45,30), 'UTC')) → 'Point (-68.14671867 -0.15771011)'.</li>
</ul>
"""
if len(values) < 1 or len(values) > 2:
parent.setEvalErrorString("Error: invalid number of arguments")
return
dt = values[0]
if len(values) == 2:
tz_name = values[1]
else:
tz_name = None
try:
dt = get_datetime(dt, tz_name)
utc = dt.astimezone(ZoneInfo('UTC'))
eph = settings.ephem()
earth = eph['earth'] # vector from solar system barycenter to geocenter
sun = eph['sun'] # vector from solar system barycenter to sun
geocentric_sun = sun - earth # vector from geocenter to sun
ts = settings.timescale()
t = ts.utc(utc.year, utc.month, utc.day, utc.hour, utc.minute, utc.second)
try:
sun_position = wgs84.geographic_position_of(geocentric_sun.at(t)) # geographic_position_of method requires a geocentric position
except Exception:
parent.setEvalErrorString("The ephemeris file does not cover the selected date range. Go to Settings and download and select an ephemeris file that contains your date range.")
return
pt = QgsPointXY(sun_position.longitude.degrees, sun_position.latitude.degrees)
return(QgsGeometry.fromPointXY(pt))
except Exception:
parent.setEvalErrorString("Error: datetime, timezone error in calculation moon phase")
return
@qgsfunction(-1, group=group_name)
def esm_moon_zenith(values, feature, parent):
"""
Given a date and time, return the EPSG:4326 coordinate point where the moon is directly overhead.
<h4>Syntax</h4>
<p><b>esm_moon_zenith</b>( <i>datetime</i>[, tz_name] )</p>
<h4>Arguments</h4>
<p><i>datetime</i> → this can be a native QGIS QDateTime, a python datetime, or a timestamp in ms.</p>
<p><i>tz_name</i> → optional timezone IANA formatted string such as 'UTC' or 'America/New_York'. If not specified and the input is a timestamp, then it is assumed to be UTC. If it is a python datetime format or QDateTime format, then the datetime is checked to see if there is an associated timezone to use. If not, UTC is used.</p>
<h4>Example usage</h4>
<ul>
<li><b>geom_to_wkt( esm_moon_zenith( make_datetime</b>(2023,5,4,17,45,30), 'UTC')) → 'Point (80.94657837 -11.8883969)'.</li>
</ul>
"""
if len(values) < 1 or len(values) > 2:
parent.setEvalErrorString("Error: invalid number of arguments")
return
dt = values[0]
if len(values) == 2:
tz_name = values[1]
else:
tz_name = None
try:
dt = get_datetime(dt, tz_name)
utc = dt.astimezone(ZoneInfo('UTC'))
eph = settings.ephem()
earth = eph['earth'] # vector from solar system barycenter to geocenter
moon = eph['moon'] # vector from solar system barycenter to moon
geocentric_moon = moon - earth # vector from geocenter to moon
ts = settings.timescale()
t = ts.utc(utc.year, utc.month, utc.day, utc.hour, utc.minute, utc.second)
try:
moon_position = wgs84.geographic_position_of(geocentric_moon.at(t)) # geographic_position_of method requires a geocentric position
except Exception:
parent.setEvalErrorString("The ephemeris file does not cover the selected date range. Go to Settings and download and select an ephemeris file that contains your date range.")
return
pt = QgsPointXY(moon_position.longitude.degrees, moon_position.latitude.degrees)
return(QgsGeometry.fromPointXY(pt))
except Exception:
parent.setEvalErrorString("Error: datetime, timezone error in calculation moon phase")
return
@qgsfunction(-1, group=group_name)
def esm_sun_moon_info(values, feature, parent):
"""
Given a date and time, latitude and longitude in EPSG:4326, output format type, and optional timezone of the date and time object, it returns a python dictionary or JSON string of solar and lunar information. If you want to avoing all confusion, use UTC as the datetime timezone.
<h4>Syntax</h4>
<p><b>esm_sun_moon_info</b>( <i>datetime, latitude, longitude[, output_type, tz_name]</i> )</p>
<h4>Arguments</h4>
<p><i>datetime</i> → this can be a native QGIS QDateTime, a python datetime, or a timestamp in ms.</p>
<p><i>latitude</i> → latitude of reference point in EPSG:4326.</p>
<p><i>longitude</i> → longitude of reference point in EPSG:4326.</p>
<p><i>output_type</i> → output type with 'dict' returning a python dictionary and 'json' returning a json formatted string. The default is 'dict'.</p>
<p><i>tz_name</i> → optional timezone IANA formatted string such as 'UTC' or 'America/New_York'. If not specified and the input is a timestamp, then it is assumed to be UTC. If it is a python datetime format or QDateTime format, then the datetime is checked to see if there is an associated timezone to use. If not, UTC is used.</p>
<h4>Example usage</h4>
<ul>
<li><b>esm_sun_moon_info( make_datetime</b>(2023,5,4,17,45,00), 38.66113944, -90.06202624, 'dict', 'America/Chicago') → { 'astronomical_twilight': '2023-05-04T01:58:08Z',...,'sunset': '2023-05-04T00:54:08Z' }.</li>
</ul>
"""
if len(values) < 3 or len(values) > 5:
parent.setEvalErrorString("Error: invalid number of arguments")
return
try:
dt = values[0]
lat = float(values[1])
lon = float(values[2])
if len(values) >= 4:
output_type = values[3]
else:
output_type = 'dict'
if len(values) == 5:
tz_name = values[4]
else:
tz_name = None
dt = get_datetime(dt, tz_name)
# Convert the date, time and timezone to UTC
utc = dt.astimezone(ZoneInfo('UTC'))
ts = settings.timescale()
# Return all dates and times in terms of UTC
cur_time = ts.from_datetime(utc)
loc = wgs84.latlon(lat, lon)
info = {}
# Load ephemeris
eph = settings.ephem()
earth = eph['earth']
sun = eph['sun']
moon = eph['moon']
# Get sun azimuth and altitude
observer = earth + loc
astrometric = observer.at(cur_time).observe(sun)
alt, az, d = astrometric.apparent().altaz()
info['sun_azimuth'] = float(az.degrees)
info['sun_elevation'] = float(alt.degrees)
# Get moon azimuth and altitude
astrometric = observer.at(cur_time).observe(moon)
alt, az, d = astrometric.apparent().altaz()
info['moon_azimuth'] = float(az.degrees)
info['moon_elevation'] = float(alt.degrees)
# Get solar noon
midnight = utc.replace(hour=0, minute=0, second=0, microsecond=0)
next_midnight = midnight + timedelta(days=1)
t0 = ts.from_datetime(midnight) # Starting time to search for events
t1 = ts.from_datetime(next_midnight) # Ending time to search for events
f = almanac.meridian_transits(eph, sun, loc)
times, events = almanac.find_discrete(t0, t1, f)
if times:
# Select transits instead of antitransits.
times = times[events == 1]
t = times[0]
info['solar_noon'] = t.utc_iso()
# Find the twlight hours
f = almanac.dark_twilight_day(eph, loc)
times, events = almanac.find_discrete(t0, t1, f)
previous_e = f(t0)
has_start = False
has_end = False
for t, e in zip(times, events):
if previous_e < e:
if e == 4: # Day starts
day_start = t
has_start = True
info['sunrise'] = t.utc_iso()
elif e == 3: # Dawn
info['dawn'] = t.utc_iso()
else:
if e == 3: # Civil twilight starts
day_end = t
has_end = True
info['sunset'] = t.utc_iso()
info['civil_twilight'] = t.utc_iso()
elif e == 2: # Nautical twilight starts
info['nautical_twilight'] = t.utc_iso()
elif e == 1: # Astronomical twilight starts
info['astronomical_twilight'] = t.utc_iso()
elif e == 0: # Night starts
info['night'] = t.utc_iso()
previous_e = e
# Calculate the phase of the moon
t = ts.from_datetime(utc)
phase = almanac.moon_phase(eph, t)
info['moon_phase'] = float(phase.degrees)
if output_type == 'dict':
return(info)
else:
return(json.dumps(info, indent = 1))
except Exception:
parent.setEvalErrorString("Error: datetime, timezone error in calculation moon phase")
return
@qgsfunction(args='auto', group=group_name)
def esm_local_datetime(feature, parent):
"""
Returns the current date and time as a python datetime object with the local computer's timezone settings.
<h4>Syntax</h4>
<p><b>esm_local_datetime</b>( )</p>
<h4>Arguments</h4>
<p>None</p>
<h4>Example usage</h4>
<ul>
<li><b>esm_local_datetime</b>() → returns a local python datetime with timezone set</li>
</ul>
"""
try:
dt = datetime.now(timezone.utc)
dt = dt.replace(tzinfo=ZoneInfo('UTC'))
tz_name = dt.astimezone().tzname()
dt = get_datetime(dt, tz_name) # This will try to standardize the timezone
return(dt)
except Exception:
parent.setEvalErrorString("Error: Was not able to get the local time")
return
@qgsfunction(args='auto', group=group_name)
def esm_local_qdatetime(feature, parent):
"""
Returns the current date and time as a standard QGIS QDateTime object with the local computer's timezone settings.
<h4>Syntax</h4>
<p><b>esm_local_qdatetime</b>( )</p>
<h4>Arguments</h4>
<p>None</p>
<h4>Example usage</h4>
<ul>
<li><b>esm_local_qdatetime</b>() → returns local QGIS supported QDateTime object.</li>
</ul>
"""
try:
dt = QDateTime.currentDateTime()
return(dt)
except Exception:
parent.setEvalErrorString("Error: Was not able to get the local time")
return