Merge branch 'blender-v2.92-release'
[blender-addons.git] / sun_position / sun_calc.py
blob26161888bc8c619247c3b4ca752c4b3e36981fec
1 ### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 import bpy
20 from bpy.app.handlers import persistent
21 from mathutils import Euler
22 import math
23 from math import degrees, radians, pi
24 import datetime
25 from .geo import parse_position
28 ############################################################################
30 # SunClass is used for storing intermediate sun calculations.
32 ############################################################################
34 class SunClass:
36 class TazEl:
37 time = 0.0
38 azimuth = 0.0
39 elevation = 0.0
41 class CLAMP:
42 elevation = 0.0
43 azimuth = 0.0
44 az_start_sun = 0.0
45 az_start_env = 0.0
47 sunrise = TazEl()
48 sunset = TazEl()
49 solar_noon = TazEl()
50 rise_set_ok = False
52 bind = CLAMP()
53 bind_to_sun = False
55 latitude = 0.0
56 longitude = 0.0
57 elevation = 0.0
58 azimuth = 0.0
60 month = 0
61 day = 0
62 year = 0
63 day_of_year = 0
64 time = 0.0
66 UTC_zone = 0
67 sun_distance = 0.0
68 use_daylight_savings = False
71 sun = SunClass()
74 def sun_update(self, context):
75 update_time(context)
76 move_sun(context)
78 def parse_coordinates(self, context):
79 error_message = "ERROR: Could not parse coordinates"
80 sun_props = context.scene.sun_pos_properties
82 if sun_props.co_parser:
83 parsed_co = parse_position(sun_props.co_parser)
85 if parsed_co is not None and len(parsed_co) == 2:
86 sun_props.latitude, sun_props.longitude = parsed_co
87 elif sun_props.co_parser != error_message:
88 sun_props.co_parser = error_message
90 # Clear prop
91 if sun_props.co_parser not in {'', error_message}:
92 sun_props.co_parser = ''
94 @persistent
95 def sun_handler(scene):
96 update_time(bpy.context)
97 move_sun(bpy.context)
100 ############################################################################
102 # move_sun() will cycle through all the selected objects
103 # and call set_sun_position and set_sun_rotations
104 # to place them in the sky.
106 ############################################################################
109 def move_sun(context):
110 addon_prefs = context.preferences.addons[__package__].preferences
111 sun_props = context.scene.sun_pos_properties
113 if sun_props.usage_mode == "HDR":
114 nt = context.scene.world.node_tree.nodes
115 env_tex = nt.get(sun_props.hdr_texture)
117 if sun.bind_to_sun != sun_props.bind_to_sun:
118 # bind_to_sun was just toggled
119 sun.bind_to_sun = sun_props.bind_to_sun
120 sun.bind.az_start_sun = sun_props.hdr_azimuth
121 if env_tex:
122 sun.bind.az_start_env = env_tex.texture_mapping.rotation.z
124 if env_tex and sun_props.bind_to_sun:
125 az = sun_props.hdr_azimuth - sun.bind.az_start_sun + sun.bind.az_start_env
126 env_tex.texture_mapping.rotation.z = az
128 if sun_props.sun_object:
129 sun.theta = math.pi / 2 - sun_props.hdr_elevation
130 sun.phi = -sun_props.hdr_azimuth
132 obj = sun_props.sun_object
133 set_sun_position(obj, sun_props.sun_distance)
134 rotation_euler = Euler((sun_props.hdr_elevation - pi/2,
135 0, -sun_props.hdr_azimuth))
137 set_sun_rotations(obj, rotation_euler)
138 return
140 local_time = sun_props.time
141 zone = -sun_props.UTC_zone
142 sun.use_daylight_savings = sun_props.use_daylight_savings
143 if sun.use_daylight_savings:
144 zone -= 1
146 north_offset = degrees(sun_props.north_offset)
148 if addon_prefs.show_rise_set:
149 calc_sunrise_sunset(rise=True)
150 calc_sunrise_sunset(rise=False)
152 get_sun_position(local_time, sun_props.latitude, sun_props.longitude,
153 north_offset, zone, sun_props.month, sun_props.day, sun_props.year,
154 sun_props.sun_distance)
156 if sun_props.sky_texture:
157 sky_node = bpy.context.scene.world.node_tree.nodes.get(sun_props.sky_texture)
158 if sky_node is not None and sky_node.type == "TEX_SKY":
159 locX = math.sin(sun.phi) * math.sin(-sun.theta)
160 locY = math.sin(sun.theta) * math.cos(sun.phi)
161 locZ = math.cos(sun.theta)
162 sky_node.texture_mapping.rotation.z = 0.0
163 sky_node.sun_direction = locX, locY, locZ
164 sky_node.sun_elevation = math.radians(sun.elevation)
165 sky_node.sun_rotation = math.radians(sun.az_north)
167 # Sun object
168 if (sun_props.sun_object is not None
169 and sun_props.sun_object.name in context.view_layer.objects):
170 obj = sun_props.sun_object
171 set_sun_position(obj, sun_props.sun_distance)
172 rotation_euler = Euler((math.radians(sun.elevation - 90), 0,
173 math.radians(-sun.az_north)))
174 set_sun_rotations(obj, rotation_euler)
176 # Sun collection
177 if sun_props.object_collection is not None:
178 sun_objects = sun_props.object_collection.objects
179 object_count = len(sun_objects)
180 if sun_props.object_collection_type == 'DIURNAL':
181 # Diurnal motion
182 if object_count > 1:
183 time_increment = sun_props.time_spread / (object_count - 1)
184 local_time = local_time + time_increment * (object_count - 1)
185 else:
186 time_increment = sun_props.time_spread
188 for obj in sun_objects:
189 get_sun_position(local_time, sun_props.latitude,
190 sun_props.longitude, north_offset, zone,
191 sun_props.month, sun_props.day,
192 sun_props.year, sun_props.sun_distance)
193 set_sun_position(obj, sun_props.sun_distance)
194 local_time -= time_increment
195 obj.rotation_euler = (
196 (math.radians(sun.elevation - 90), 0,
197 math.radians(-sun.az_north)))
198 else:
199 # Analemma
200 day_increment = 365 / object_count
201 day = sun_props.day_of_year + day_increment * (object_count - 1)
202 for obj in sun_objects:
203 dt = (datetime.date(sun_props.year, 1, 1) +
204 datetime.timedelta(day - 1))
205 get_sun_position(local_time, sun_props.latitude,
206 sun_props.longitude, north_offset, zone,
207 dt.month, dt.day, sun_props.year,
208 sun_props.sun_distance)
209 set_sun_position(obj, sun_props.sun_distance)
210 day -= day_increment
211 obj.rotation_euler = (
212 (math.radians(sun.elevation - 90), 0,
213 math.radians(-sun.az_north)))
215 def update_time(context):
216 sun_props = context.scene.sun_pos_properties
218 if sun_props.use_day_of_year:
219 dt = (datetime.date(sun_props.year, 1, 1) +
220 datetime.timedelta(sun_props.day_of_year - 1))
221 sun.day = dt.day
222 sun.month = dt.month
223 sun.day_of_year = sun_props.day_of_year
224 if sun_props.day != dt.day:
225 sun_props.day = dt.day
226 if sun_props.month != dt.month:
227 sun_props.month = dt.month
228 else:
229 dt = datetime.date(sun_props.year, sun_props.month, sun_props.day)
230 day_of_year = dt.timetuple().tm_yday
231 if sun_props.day_of_year != day_of_year:
232 sun_props.day_of_year = day_of_year
233 sun.day = sun_props.day
234 sun.month = sun_props.month
235 sun.day_of_year = day_of_year
236 sun.year = sun_props.year
237 sun.longitude = sun_props.longitude
238 sun.latitude = sun_props.latitude
239 sun.UTC_zone = sun_props.UTC_zone
242 def format_time(the_time, daylight_savings, longitude, UTC_zone=None):
243 if UTC_zone is not None:
244 if daylight_savings:
245 UTC_zone += 1
246 the_time -= UTC_zone
248 the_time %= 24
250 hh = int(the_time)
251 mm = (the_time - int(the_time)) * 60
252 ss = int((mm - int(mm)) * 60)
254 return ("%02i:%02i:%02i" % (hh, mm, ss))
257 def format_hms(the_time):
258 hh = str(int(the_time))
259 min = (the_time - int(the_time)) * 60
260 sec = int((min - int(min)) * 60)
261 mm = "0" + str(int(min)) if min < 10 else str(int(min))
262 ss = "0" + str(sec) if sec < 10 else str(sec)
264 return (hh + ":" + mm + ":" + ss)
267 def format_lat_long(lat_long, is_latitude):
268 hh = str(abs(int(lat_long)))
269 min = abs((lat_long - int(lat_long)) * 60)
270 sec = abs(int((min - int(min)) * 60))
271 mm = "0" + str(int(min)) if min < 10 else str(int(min))
272 ss = "0" + str(sec) if sec < 10 else str(sec)
273 if lat_long == 0:
274 coord_tag = " "
275 else:
276 if is_latitude:
277 coord_tag = " N" if lat_long > 0 else " S"
278 else:
279 coord_tag = " E" if lat_long > 0 else " W"
281 return hh + "° " + mm + "' " + ss + '"' + coord_tag
286 ############################################################################
288 # Calculate the actual position of the sun based on input parameters.
290 # The sun positioning algorithms below are based on the National Oceanic
291 # and Atmospheric Administration's (NOAA) Solar Position Calculator
292 # which rely on calculations of Jean Meeus' book "Astronomical Algorithms."
293 # Use of NOAA data and products are in the public domain and may be used
294 # freely by the public as outlined in their policies at
295 # www.nws.noaa.gov/disclaimer.php
297 # The calculations of this script can be verified with those of NOAA's
298 # using the Azimuth and Solar Elevation displayed in the SunPos_Panel.
299 # NOAA's web site is:
300 # http://www.esrl.noaa.gov/gmd/grad/solcalc
301 ############################################################################
304 def get_sun_position(local_time, latitude, longitude, north_offset,
305 utc_zone, month, day, year, distance):
307 addon_prefs = bpy.context.preferences.addons[__package__].preferences
308 sun_props = bpy.context.scene.sun_pos_properties
310 longitude *= -1 # for internal calculations
311 utc_time = local_time + utc_zone # Set Greenwich Meridian Time
313 if latitude > 89.93: # Latitude 90 and -90 gives
314 latitude = radians(89.93) # erroneous results so nudge it
315 elif latitude < -89.93:
316 latitude = radians(-89.93)
317 else:
318 latitude = radians(latitude)
320 t = julian_time_from_y2k(utc_time, year, month, day)
322 e = radians(obliquity_correction(t))
323 L = apparent_longitude_of_sun(t)
324 solar_dec = sun_declination(e, L)
325 eqtime = calc_equation_of_time(t)
327 time_correction = (eqtime - 4 * longitude) + 60 * utc_zone
328 true_solar_time = ((utc_time - utc_zone) * 60.0 + time_correction) % 1440
330 hour_angle = true_solar_time / 4.0 - 180.0
331 if hour_angle < -180.0:
332 hour_angle += 360.0
334 csz = (math.sin(latitude) * math.sin(solar_dec) +
335 math.cos(latitude) * math.cos(solar_dec) *
336 math.cos(radians(hour_angle)))
337 if csz > 1.0:
338 csz = 1.0
339 elif csz < -1.0:
340 csz = -1.0
342 zenith = math.acos(csz)
344 az_denom = math.cos(latitude) * math.sin(zenith)
346 if abs(az_denom) > 0.001:
347 az_rad = ((math.sin(latitude) *
348 math.cos(zenith)) - math.sin(solar_dec)) / az_denom
349 if abs(az_rad) > 1.0:
350 az_rad = -1.0 if (az_rad < 0.0) else 1.0
351 azimuth = 180.0 - degrees(math.acos(az_rad))
352 if hour_angle > 0.0:
353 azimuth = -azimuth
354 else:
355 azimuth = 180.0 if (latitude > 0.0) else 0.0
357 if azimuth < 0.0:
358 azimuth = azimuth + 360.0
360 exoatm_elevation = 90.0 - degrees(zenith)
362 if sun_props.use_refraction:
363 if exoatm_elevation > 85.0:
364 refraction_correction = 0.0
365 else:
366 te = math.tan(radians(exoatm_elevation))
367 if exoatm_elevation > 5.0:
368 refraction_correction = (
369 58.1 / te - 0.07 / (te ** 3) + 0.000086 / (te ** 5))
370 elif (exoatm_elevation > -0.575):
371 s1 = (-12.79 + exoatm_elevation * 0.711)
372 s2 = (103.4 + exoatm_elevation * (s1))
373 s3 = (-518.2 + exoatm_elevation * (s2))
374 refraction_correction = 1735.0 + exoatm_elevation * (s3)
375 else:
376 refraction_correction = -20.774 / te
378 refraction_correction = refraction_correction / 3600
379 solar_elevation = 90.0 - (degrees(zenith) - refraction_correction)
381 else:
382 solar_elevation = 90.0 - degrees(zenith)
384 solar_azimuth = azimuth
385 solar_azimuth += north_offset
387 sun.az_north = solar_azimuth
389 sun.theta = math.pi / 2 - radians(solar_elevation)
390 sun.phi = radians(solar_azimuth) * -1
391 sun.azimuth = azimuth
392 sun.elevation = solar_elevation
395 def set_sun_position(obj, distance):
396 locX = math.sin(sun.phi) * math.sin(-sun.theta) * distance
397 locY = math.sin(sun.theta) * math.cos(sun.phi) * distance
398 locZ = math.cos(sun.theta) * distance
400 #----------------------------------------------
401 # Update selected object in viewport
402 #----------------------------------------------
403 obj.location = locX, locY, locZ
406 def set_sun_rotations(obj, rotation_euler):
407 rotation_quaternion = rotation_euler.to_quaternion()
408 obj.rotation_quaternion = rotation_quaternion
410 if obj.rotation_mode in {'XZY', 'YXZ', 'YZX', 'ZXY','ZYX'}:
411 obj.rotation_euler = rotation_quaternion.to_euler(obj.rotation_mode)
412 else:
413 obj.rotation_euler = rotation_euler
415 rotation_axis_angle = obj.rotation_quaternion.to_axis_angle()
416 obj.rotation_axis_angle = (rotation_axis_angle[1],
417 *rotation_axis_angle[0])
420 def calc_sunrise_set_UTC(rise, jd, latitude, longitude):
421 t = calc_time_julian_cent(jd)
422 eq_time = calc_equation_of_time(t)
423 solar_dec = calc_sun_declination(t)
424 hour_angle = calc_hour_angle_sunrise(latitude, solar_dec)
425 if not rise:
426 hour_angle = -hour_angle
427 delta = longitude + degrees(hour_angle)
428 time_UTC = 720 - (4.0 * delta) - eq_time
429 return time_UTC
432 def calc_sun_declination(t):
433 e = radians(obliquity_correction(t))
434 L = apparent_longitude_of_sun(t)
435 solar_dec = sun_declination(e, L)
436 return solar_dec
439 def calc_hour_angle_sunrise(lat, solar_dec):
440 lat_rad = radians(lat)
441 HAarg = (math.cos(radians(90.833)) /
442 (math.cos(lat_rad) * math.cos(solar_dec))
443 - math.tan(lat_rad) * math.tan(solar_dec))
444 if HAarg < -1.0:
445 HAarg = -1.0
446 elif HAarg > 1.0:
447 HAarg = 1.0
448 HA = math.acos(HAarg)
449 return HA
452 def calc_solar_noon(jd, longitude, timezone, dst):
453 t = calc_time_julian_cent(jd - longitude / 360.0)
454 eq_time = calc_equation_of_time(t)
455 noon_offset = 720.0 - (longitude * 4.0) - eq_time
456 newt = calc_time_julian_cent(jd + noon_offset / 1440.0)
457 eq_time = calc_equation_of_time(newt)
459 nv = 780.0 if dst else 720.0
460 noon_local = (nv- (longitude * 4.0) - eq_time + (timezone * 60.0)) % 1440
461 sun.solar_noon.time = noon_local / 60.0
464 def calc_sunrise_sunset(rise):
465 zone = -sun.UTC_zone
467 jd = get_julian_day(sun.year, sun.month, sun.day)
468 time_UTC = calc_sunrise_set_UTC(rise, jd, sun.latitude, sun.longitude)
469 new_time_UTC = calc_sunrise_set_UTC(rise, jd + time_UTC / 1440.0,
470 sun.latitude, sun.longitude)
471 time_local = new_time_UTC + (-zone * 60.0)
472 tl = time_local / 60.0
473 get_sun_position(tl, sun.latitude, sun.longitude, 0.0,
474 zone, sun.month, sun.day, sun.year,
475 sun.sun_distance)
476 if sun.use_daylight_savings:
477 time_local += 60.0
478 tl = time_local / 60.0
479 tl %= 24.0
480 if rise:
481 sun.sunrise.time = tl
482 sun.sunrise.azimuth = sun.azimuth
483 sun.sunrise.elevation = sun.elevation
484 calc_solar_noon(jd, sun.longitude, -zone, sun.use_daylight_savings)
485 get_sun_position(sun.solar_noon.time, sun.latitude, sun.longitude,
486 0.0, zone, sun.month, sun.day, sun.year,
487 sun.sun_distance)
488 sun.solar_noon.elevation = sun.elevation
489 else:
490 sun.sunset.time = tl
491 sun.sunset.azimuth = sun.azimuth
492 sun.sunset.elevation = sun.elevation
494 ##########################################################################
495 ## Get the elapsed julian time since 1/1/2000 12:00 gmt
496 ## Y2k epoch (1/1/2000 12:00 gmt) is Julian day 2451545.0
497 ##########################################################################
500 def julian_time_from_y2k(utc_time, year, month, day):
501 century = 36525.0 # Days in Julian Century
502 epoch = 2451545.0 # Julian Day for 1/1/2000 12:00 gmt
503 jd = get_julian_day(year, month, day)
504 return ((jd + (utc_time / 24)) - epoch) / century
507 def get_julian_day(year, month, day):
508 if month <= 2:
509 year -= 1
510 month += 12
511 A = math.floor(year / 100)
512 B = 2 - A + math.floor(A / 4.0)
513 jd = (math.floor((365.25 * (year + 4716.0))) +
514 math.floor(30.6001 * (month + 1)) + day + B - 1524.5)
515 return jd
518 def calc_time_julian_cent(jd):
519 t = (jd - 2451545.0) / 36525.0
520 return t
523 def sun_declination(e, L):
524 return (math.asin(math.sin(e) * math.sin(L)))
527 def calc_equation_of_time(t):
528 epsilon = obliquity_correction(t)
529 ml = radians(mean_longitude_sun(t))
530 e = eccentricity_earth_orbit(t)
531 m = radians(mean_anomaly_sun(t))
532 y = math.tan(radians(epsilon) / 2.0)
533 y = y * y
534 sin2ml = math.sin(2.0 * ml)
535 cos2ml = math.cos(2.0 * ml)
536 sin4ml = math.sin(4.0 * ml)
537 sinm = math.sin(m)
538 sin2m = math.sin(2.0 * m)
539 etime = (y * sin2ml - 2.0 * e * sinm + 4.0 * e * y *
540 sinm * cos2ml - 0.5 * y ** 2 * sin4ml - 1.25 * e ** 2 * sin2m)
541 return (degrees(etime) * 4)
544 def obliquity_correction(t):
545 ec = obliquity_of_ecliptic(t)
546 omega = 125.04 - 1934.136 * t
547 return (ec + 0.00256 * math.cos(radians(omega)))
550 def obliquity_of_ecliptic(t):
551 return ((23.0 + 26.0 / 60 + (21.4480 - 46.8150) / 3600 * t -
552 (0.00059 / 3600) * t ** 2 + (0.001813 / 3600) * t ** 3))
555 def true_longitude_of_sun(t):
556 return (mean_longitude_sun(t) + equation_of_sun_center(t))
559 def calc_sun_apparent_long(t):
560 o = true_longitude_of_sun(t)
561 omega = 125.04 - 1934.136 * t
562 lamb = o - 0.00569 - 0.00478 * math.sin(radians(omega))
563 return lamb
566 def apparent_longitude_of_sun(t):
567 return (radians(true_longitude_of_sun(t) - 0.00569 - 0.00478 *
568 math.sin(radians(125.04 - 1934.136 * t))))
571 def mean_longitude_sun(t):
572 return (280.46646 + 36000.76983 * t + 0.0003032 * t ** 2) % 360
575 def equation_of_sun_center(t):
576 m = radians(mean_anomaly_sun(t))
577 c = ((1.914602 - 0.004817 * t - 0.000014 * t ** 2) * math.sin(m) +
578 (0.019993 - 0.000101 * t) * math.sin(m * 2) +
579 0.000289 * math.sin(m * 3))
580 return c
583 def mean_anomaly_sun(t):
584 return (357.52911 + t * (35999.05029 - 0.0001537 * t))
587 def eccentricity_earth_orbit(t):
588 return (0.016708634 - 0.000042037 * t - 0.0000001267 * t ** 2)