Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sun_calc.py « sun_position - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 39c8444ed5d01c9118bb47ccdad7ad7db345761e (plain)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# SPDX-License-Identifier: GPL-2.0-or-later

import bpy
from bpy.app.handlers import persistent
from mathutils import Euler
import math
from math import degrees, radians, pi
import datetime
from .geo import parse_position


############################################################################
#
# SunClass is used for storing intermediate sun calculations.
#
############################################################################

class SunClass:

    class TazEl:
        time = 0.0
        azimuth = 0.0
        elevation = 0.0

    class CLAMP:
        elevation = 0.0
        azimuth = 0.0
        az_start_sun = 0.0
        az_start_env = 0.0

    sunrise = TazEl()
    sunset = TazEl()
    solar_noon = TazEl()
    rise_set_ok = False

    bind = CLAMP()
    bind_to_sun = False

    latitude = 0.0
    longitude = 0.0
    elevation = 0.0
    azimuth = 0.0

    month = 0
    day = 0
    year = 0
    day_of_year = 0
    time = 0.0

    UTC_zone = 0
    sun_distance = 0.0
    use_daylight_savings = False


sun = SunClass()


def sun_update(self, context):
    update_time(context)
    move_sun(context)

def parse_coordinates(self, context):
    error_message = "ERROR: Could not parse coordinates"
    sun_props = context.scene.sun_pos_properties

    if sun_props.co_parser:
        parsed_co = parse_position(sun_props.co_parser)

        if parsed_co is not None and len(parsed_co) == 2:
            sun_props.latitude, sun_props.longitude = parsed_co
        elif sun_props.co_parser != error_message:
            sun_props.co_parser = error_message

        # Clear prop
    if sun_props.co_parser not in {'', error_message}:
        sun_props.co_parser = ''

@persistent
def sun_handler(scene):
    update_time(bpy.context)
    move_sun(bpy.context)


############################################################################
#
# move_sun() will cycle through all the selected objects
# and call set_sun_position and set_sun_rotations
# to place them in the sky.
#
############################################################################


def move_sun(context):
    addon_prefs = context.preferences.addons[__package__].preferences
    sun_props = context.scene.sun_pos_properties

    if sun_props.usage_mode == "HDR":
        nt = context.scene.world.node_tree.nodes
        env_tex = nt.get(sun_props.hdr_texture)

        if sun.bind_to_sun != sun_props.bind_to_sun:
            # bind_to_sun was just toggled
            sun.bind_to_sun = sun_props.bind_to_sun
            sun.bind.az_start_sun = sun_props.hdr_azimuth
            if env_tex:
                sun.bind.az_start_env = env_tex.texture_mapping.rotation.z

        if env_tex and sun_props.bind_to_sun:
            az = sun_props.hdr_azimuth - sun.bind.az_start_sun + sun.bind.az_start_env
            env_tex.texture_mapping.rotation.z = az

        if sun_props.sun_object:
            sun.theta = math.pi / 2 - sun_props.hdr_elevation
            sun.phi = -sun_props.hdr_azimuth

            obj = sun_props.sun_object
            set_sun_position(obj, sun_props.sun_distance)
            rotation_euler = Euler((sun_props.hdr_elevation - pi/2,
                                    0, -sun_props.hdr_azimuth))

            set_sun_rotations(obj, rotation_euler)
        return

    local_time = sun_props.time
    zone = -sun_props.UTC_zone
    sun.use_daylight_savings = sun_props.use_daylight_savings
    if sun.use_daylight_savings:
        zone -= 1

    north_offset = degrees(sun_props.north_offset)

    if addon_prefs.show_rise_set:
        calc_sunrise_sunset(rise=True)
        calc_sunrise_sunset(rise=False)

    get_sun_position(local_time, sun_props.latitude, sun_props.longitude,
            north_offset, zone, sun_props.month, sun_props.day, sun_props.year,
            sun_props.sun_distance)

    if sun_props.sky_texture:
        sky_node = bpy.context.scene.world.node_tree.nodes.get(sun_props.sky_texture)
        if sky_node is not None and sky_node.type == "TEX_SKY":
            locX = math.sin(sun.phi) * math.sin(-sun.theta)
            locY = math.sin(sun.theta) * math.cos(sun.phi)
            locZ = math.cos(sun.theta)
            sky_node.texture_mapping.rotation.z = 0.0
            sky_node.sun_direction = locX, locY, locZ
            sky_node.sun_elevation = math.radians(sun.elevation)
            sky_node.sun_rotation = math.radians(sun.az_north)

    # Sun object
    if (sun_props.sun_object is not None
            and sun_props.sun_object.name in context.view_layer.objects):
        obj = sun_props.sun_object
        set_sun_position(obj, sun_props.sun_distance)
        rotation_euler = Euler((math.radians(sun.elevation - 90), 0,
                                math.radians(-sun.az_north)))
        set_sun_rotations(obj, rotation_euler)

    # Sun collection
    if sun_props.object_collection is not None:
        sun_objects = sun_props.object_collection.objects
        object_count = len(sun_objects)
        if sun_props.object_collection_type == 'DIURNAL':
            # Diurnal motion
            if object_count > 1:
                time_increment = sun_props.time_spread / (object_count - 1)
                local_time = local_time + time_increment * (object_count - 1)
            else:
                time_increment = sun_props.time_spread

            for obj in sun_objects:
                get_sun_position(local_time, sun_props.latitude,
                                 sun_props.longitude, north_offset, zone,
                                 sun_props.month, sun_props.day,
                                 sun_props.year, sun_props.sun_distance)
                set_sun_position(obj, sun_props.sun_distance)
                local_time -= time_increment
                obj.rotation_euler = (
                    (math.radians(sun.elevation - 90), 0,
                     math.radians(-sun.az_north)))
        else:
            # Analemma
            day_increment = 365 / object_count
            day = sun_props.day_of_year + day_increment * (object_count - 1)
            for obj in sun_objects:
                dt = (datetime.date(sun_props.year, 1, 1) +
                      datetime.timedelta(day - 1))
                get_sun_position(local_time, sun_props.latitude,
                                 sun_props.longitude, north_offset, zone,
                                 dt.month, dt.day, sun_props.year,
                                 sun_props.sun_distance)
                set_sun_position(obj, sun_props.sun_distance)
                day -= day_increment
                obj.rotation_euler = (
                    (math.radians(sun.elevation - 90), 0,
                     math.radians(-sun.az_north)))

def update_time(context):
    sun_props = context.scene.sun_pos_properties

    if sun_props.use_day_of_year:
        dt = (datetime.date(sun_props.year, 1, 1) +
              datetime.timedelta(sun_props.day_of_year - 1))
        sun.day = dt.day
        sun.month = dt.month
        sun.day_of_year = sun_props.day_of_year
        if sun_props.day != dt.day:
            sun_props.day = dt.day
        if sun_props.month != dt.month:
            sun_props.month = dt.month
    else:
        dt = datetime.date(sun_props.year, sun_props.month, sun_props.day)
        day_of_year = dt.timetuple().tm_yday
        if sun_props.day_of_year != day_of_year:
            sun_props.day_of_year = day_of_year
        sun.day = sun_props.day
        sun.month = sun_props.month
        sun.day_of_year = day_of_year
    sun.year = sun_props.year
    sun.longitude = sun_props.longitude
    sun.latitude = sun_props.latitude
    sun.UTC_zone = sun_props.UTC_zone


def format_time(the_time, daylight_savings, longitude, UTC_zone=None):
    if UTC_zone is not None:
        if daylight_savings:
            UTC_zone += 1
        the_time -= UTC_zone

    the_time %= 24

    hh = int(the_time)
    mm = (the_time - int(the_time)) * 60
    ss = int((mm - int(mm)) * 60)

    return ("%02i:%02i:%02i" % (hh, mm, ss))


def format_hms(the_time):
    hh = str(int(the_time))
    min = (the_time - int(the_time)) * 60
    sec = int((min - int(min)) * 60)
    mm = "0" + str(int(min)) if min < 10 else str(int(min))
    ss = "0" + str(sec) if sec < 10 else str(sec)

    return (hh + ":" + mm + ":" + ss)


def format_lat_long(lat_long, is_latitude):
    hh = str(abs(int(lat_long)))
    min = abs((lat_long - int(lat_long)) * 60)
    sec = abs(int((min - int(min)) * 60))
    mm = "0" + str(int(min)) if min < 10 else str(int(min))
    ss = "0" + str(sec) if sec < 10 else str(sec)
    if lat_long == 0:
        coord_tag = " "
    else:
        if is_latitude:
            coord_tag = " N" if lat_long > 0 else " S"
        else:
            coord_tag = " E" if lat_long > 0 else " W"

    return hh + "° " + mm + "' " + ss + '"' + coord_tag




############################################################################
#
# Calculate the actual position of the sun based on input parameters.
#
# The sun positioning algorithms below are based on the National Oceanic
# and Atmospheric Administration's (NOAA) Solar Position Calculator
# which rely on calculations of Jean Meeus' book "Astronomical Algorithms."
# Use of NOAA data and products are in the public domain and may be used
# freely by the public as outlined in their policies at
#               www.nws.noaa.gov/disclaimer.php
#
# The calculations of this script can be verified with those of NOAA's
# using the Azimuth and Solar Elevation displayed in the SunPos_Panel.
# NOAA's web site is:
#               http://www.esrl.noaa.gov/gmd/grad/solcalc
############################################################################


def get_sun_position(local_time, latitude, longitude, north_offset,
                   utc_zone, month, day, year, distance):

    addon_prefs = bpy.context.preferences.addons[__package__].preferences
    sun_props = bpy.context.scene.sun_pos_properties

    longitude *= -1                 # for internal calculations
    utc_time = local_time + utc_zone   # Set Greenwich Meridian Time

    if latitude > 89.93:            # Latitude 90 and -90 gives
        latitude = radians(89.93)  # erroneous results so nudge it
    elif latitude < -89.93:
        latitude = radians(-89.93)
    else:
        latitude = radians(latitude)

    t = julian_time_from_y2k(utc_time, year, month, day)

    e = radians(obliquity_correction(t))
    L = apparent_longitude_of_sun(t)
    solar_dec = sun_declination(e, L)
    eqtime = calc_equation_of_time(t)

    time_correction = (eqtime - 4 * longitude) + 60 * utc_zone
    true_solar_time = ((utc_time - utc_zone) * 60.0 + time_correction) % 1440

    hour_angle = true_solar_time / 4.0 - 180.0
    if hour_angle < -180.0:
        hour_angle += 360.0

    csz = (math.sin(latitude) * math.sin(solar_dec) +
           math.cos(latitude) * math.cos(solar_dec) *
           math.cos(radians(hour_angle)))
    if csz > 1.0:
        csz = 1.0
    elif csz < -1.0:
        csz = -1.0

    zenith = math.acos(csz)

    az_denom = math.cos(latitude) * math.sin(zenith)

    if abs(az_denom) > 0.001:
        az_rad = ((math.sin(latitude) *
                  math.cos(zenith)) - math.sin(solar_dec)) / az_denom
        if abs(az_rad) > 1.0:
            az_rad = -1.0 if (az_rad < 0.0) else 1.0
        azimuth = 180.0 - degrees(math.acos(az_rad))
        if hour_angle > 0.0:
            azimuth = -azimuth
    else:
        azimuth = 180.0 if (latitude > 0.0) else 0.0

    if azimuth < 0.0:
        azimuth = azimuth + 360.0

    exoatm_elevation = 90.0 - degrees(zenith)

    if sun_props.use_refraction:
        if exoatm_elevation > 85.0:
            refraction_correction = 0.0
        else:
            te = math.tan(radians(exoatm_elevation))
            if exoatm_elevation > 5.0:
                refraction_correction = (
                    58.1 / te - 0.07 / (te ** 3) + 0.000086 / (te ** 5))
            elif (exoatm_elevation > -0.575):
                s1 = (-12.79 + exoatm_elevation * 0.711)
                s2 = (103.4 + exoatm_elevation * (s1))
                s3 = (-518.2 + exoatm_elevation * (s2))
                refraction_correction = 1735.0 + exoatm_elevation * (s3)
            else:
                refraction_correction = -20.774 / te

        refraction_correction = refraction_correction / 3600
        solar_elevation = 90.0 - (degrees(zenith) - refraction_correction)

    else:
        solar_elevation = 90.0 - degrees(zenith)

    solar_azimuth = azimuth
    solar_azimuth += north_offset

    sun.az_north = solar_azimuth

    sun.theta = math.pi / 2 - radians(solar_elevation)
    sun.phi = radians(solar_azimuth) * -1
    sun.azimuth = azimuth
    sun.elevation = solar_elevation


def set_sun_position(obj, distance):
    locX = math.sin(sun.phi) * math.sin(-sun.theta) * distance
    locY = math.sin(sun.theta) * math.cos(sun.phi) * distance
    locZ = math.cos(sun.theta) * distance

    #----------------------------------------------
    # Update selected object in viewport
    #----------------------------------------------
    obj.location = locX, locY, locZ


def set_sun_rotations(obj, rotation_euler):
    rotation_quaternion = rotation_euler.to_quaternion()
    obj.rotation_quaternion = rotation_quaternion

    if obj.rotation_mode in {'XZY', 'YXZ', 'YZX', 'ZXY','ZYX'}:
        obj.rotation_euler = rotation_quaternion.to_euler(obj.rotation_mode)
    else:
        obj.rotation_euler = rotation_euler

    rotation_axis_angle = obj.rotation_quaternion.to_axis_angle()
    obj.rotation_axis_angle = (rotation_axis_angle[1],
                               *rotation_axis_angle[0])


def calc_sunrise_set_UTC(rise, jd, latitude, longitude):
    t = calc_time_julian_cent(jd)
    eq_time = calc_equation_of_time(t)
    solar_dec = calc_sun_declination(t)
    hour_angle = calc_hour_angle_sunrise(latitude, solar_dec)
    if not rise:
        hour_angle = -hour_angle
    delta = longitude + degrees(hour_angle)
    time_UTC = 720 - (4.0 * delta) - eq_time
    return time_UTC


def calc_sun_declination(t):
    e = radians(obliquity_correction(t))
    L = apparent_longitude_of_sun(t)
    solar_dec = sun_declination(e, L)
    return solar_dec


def calc_hour_angle_sunrise(lat, solar_dec):
    lat_rad = radians(lat)
    HAarg = (math.cos(radians(90.833)) /
            (math.cos(lat_rad) * math.cos(solar_dec))
            - math.tan(lat_rad) * math.tan(solar_dec))
    if HAarg < -1.0:
        HAarg = -1.0
    elif HAarg > 1.0:
        HAarg = 1.0
    HA = math.acos(HAarg)
    return HA


def calc_solar_noon(jd, longitude, timezone, dst):
    t = calc_time_julian_cent(jd - longitude / 360.0)
    eq_time = calc_equation_of_time(t)
    noon_offset = 720.0 - (longitude * 4.0) - eq_time
    newt = calc_time_julian_cent(jd + noon_offset / 1440.0)
    eq_time = calc_equation_of_time(newt)

    nv = 780.0 if dst else 720.0
    noon_local = (nv- (longitude * 4.0) - eq_time + (timezone * 60.0)) % 1440
    sun.solar_noon.time = noon_local / 60.0


def calc_sunrise_sunset(rise):
    zone = -sun.UTC_zone

    jd = get_julian_day(sun.year, sun.month, sun.day)
    time_UTC = calc_sunrise_set_UTC(rise, jd, sun.latitude, sun.longitude)
    new_time_UTC = calc_sunrise_set_UTC(rise, jd + time_UTC / 1440.0,
                     sun.latitude, sun.longitude)
    time_local = new_time_UTC + (-zone * 60.0)
    tl = time_local / 60.0
    get_sun_position(tl, sun.latitude, sun.longitude, 0.0,
            zone, sun.month, sun.day, sun.year,
            sun.sun_distance)
    if sun.use_daylight_savings:
        time_local += 60.0
        tl = time_local / 60.0
    tl %= 24.0
    if rise:
        sun.sunrise.time = tl
        sun.sunrise.azimuth = sun.azimuth
        sun.sunrise.elevation = sun.elevation
        calc_solar_noon(jd, sun.longitude, -zone, sun.use_daylight_savings)
        get_sun_position(sun.solar_noon.time, sun.latitude, sun.longitude,
            0.0, zone, sun.month, sun.day, sun.year,
            sun.sun_distance)
        sun.solar_noon.elevation = sun.elevation
    else:
        sun.sunset.time = tl
        sun.sunset.azimuth = sun.azimuth
        sun.sunset.elevation = sun.elevation

##########################################################################
## Get the elapsed julian time since 1/1/2000 12:00 gmt
## Y2k epoch (1/1/2000 12:00 gmt) is Julian day 2451545.0
##########################################################################


def julian_time_from_y2k(utc_time, year, month, day):
    century = 36525.0  # Days in Julian Century
    epoch = 2451545.0  # Julian Day for 1/1/2000 12:00 gmt
    jd = get_julian_day(year, month, day)
    return ((jd + (utc_time / 24)) - epoch) / century


def get_julian_day(year, month, day):
    if month <= 2:
        year -= 1
        month += 12
    A = math.floor(year / 100)
    B = 2 - A + math.floor(A / 4.0)
    jd = (math.floor((365.25 * (year + 4716.0))) +
         math.floor(30.6001 * (month + 1)) + day + B - 1524.5)
    return jd


def calc_time_julian_cent(jd):
    t = (jd - 2451545.0) / 36525.0
    return t


def sun_declination(e, L):
    return (math.asin(math.sin(e) * math.sin(L)))


def calc_equation_of_time(t):
    epsilon = obliquity_correction(t)
    ml = radians(mean_longitude_sun(t))
    e = eccentricity_earth_orbit(t)
    m = radians(mean_anomaly_sun(t))
    y = math.tan(radians(epsilon) / 2.0)
    y = y * y
    sin2ml = math.sin(2.0 * ml)
    cos2ml = math.cos(2.0 * ml)
    sin4ml = math.sin(4.0 * ml)
    sinm = math.sin(m)
    sin2m = math.sin(2.0 * m)
    etime = (y * sin2ml - 2.0 * e * sinm + 4.0 * e * y *
             sinm * cos2ml - 0.5 * y ** 2 * sin4ml - 1.25 * e ** 2 * sin2m)
    return (degrees(etime) * 4)


def obliquity_correction(t):
    ec = obliquity_of_ecliptic(t)
    omega = 125.04 - 1934.136 * t
    return (ec + 0.00256 * math.cos(radians(omega)))


def obliquity_of_ecliptic(t):
    return ((23.0 + 26.0 / 60 + (21.4480 - 46.8150) / 3600 * t -
            (0.00059 / 3600) * t ** 2 + (0.001813 / 3600) * t ** 3))


def true_longitude_of_sun(t):
    return (mean_longitude_sun(t) + equation_of_sun_center(t))


def calc_sun_apparent_long(t):
    o = true_longitude_of_sun(t)
    omega = 125.04 - 1934.136 * t
    lamb = o - 0.00569 - 0.00478 * math.sin(radians(omega))
    return lamb


def apparent_longitude_of_sun(t):
    return (radians(true_longitude_of_sun(t) - 0.00569 - 0.00478 *
            math.sin(radians(125.04 - 1934.136 * t))))


def mean_longitude_sun(t):
    return (280.46646 + 36000.76983 * t + 0.0003032 * t ** 2) % 360


def equation_of_sun_center(t):
    m = radians(mean_anomaly_sun(t))
    c = ((1.914602 - 0.004817 * t - 0.000014 * t ** 2) * math.sin(m) +
        (0.019993 - 0.000101 * t) * math.sin(m * 2) +
         0.000289 * math.sin(m * 3))
    return c


def mean_anomaly_sun(t):
    return (357.52911 + t * (35999.05029 - 0.0001537 * t))


def eccentricity_earth_orbit(t):
    return (0.016708634 - 0.000042037 * t - 0.0000001267 * t ** 2)