-
Notifications
You must be signed in to change notification settings - Fork 0
/
astro.ex
1046 lines (771 loc) · 28.8 KB
/
astro.ex
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule Astro do
@moduledoc """
Functions for basic astronomical observations such
as sunrise, sunset, solstice, equinox, moonrise,
moonset and moon phase.
"""
alias Astro.{Solar, Lunar, Location, Time, Math, Guards}
import Astro.Math, only: [
sin: 1,
cos: 1,
atan_r: 2,
tan: 1,
mod: 2,
to_degrees: 1
]
import Astro.Solar, only: [
obliquity_correction: 1
]
@type longitude :: float()
@type latitude :: float()
@type altitude :: float()
@type degrees :: float()
@type angle() :: number()
@type meters() :: number()
@type phase() :: angle()
@type location :: {longitude, latitude} | Geo.Point.t() | Geo.PointZ.t()
@type date :: Calendar.date() | Calendar.datetime()
@type options :: keyword()
defguard is_lunar_phase(phase) when phase >= 0.0 and phase <= 360.0
@doc """
Returns a tuple `{azimuth, altitude}` for a given
date time and location.
## Arguments
* `location` is the latitude, longitude and
optionally elevation for the desired sunrise
azimuth and altitude. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `Geo.Point.t` struct to represent a location without elevation
* a `Geo.PointZ.t` struct to represent a location and elevation
* `date_time` is a `DateTime` any struct that meets the
requirements of `t:Calendar.datetime`.
## Returns
* a tuple of the format `{azimith, altitude}` which are
expressed in float degrees.
## Example
iex> {:ok, date_time} = DateTime.new(~D[2023-05-17], ~T[12:47:00], "Australia/Sydney")
iex> location = {151.1637781, -33.5145852}
iex> {_azimuth, _altitude} = Astro.sun_azimuth_elevation(location, date_time)
"""
# Use https://midcdmz.nrel.gov/solpos/spa.html for validation
# current implementation is approx 1 degree at variance with
# that calculator.
@doc since: "0.11.0"
@spec sun_azimuth_elevation(location(), Calendar.datetime()) :: {azimuth :: float, altitude :: float}
def sun_azimuth_elevation(location, unquote(Guards.datetime()) = date_time) do
_ = calendar
%Geo.PointZ{coordinates: {right_ascension, declination, _distance}} =
sun_position_at(date_time)
%Geo.PointZ{coordinates: {_longitude, latitude, _altitude}} =
Location.normalize_location(location)
local_sidereal_time =
Time.local_sidereal_time(location, date_time)
hour_angle =
mod(local_sidereal_time - right_ascension, 360.0)
altitude =
:math.asin(sin(declination) * sin(latitude) + cos(declination) * cos(latitude) * cos(hour_angle))
|> to_degrees
a =
:math.acos((sin(declination) - sin(altitude) * sin(latitude)) / (cos(altitude) * cos(latitude)))
|> to_degrees()
azimuth =
if sin(hour_angle) < 0.0, do: a, else: 360.0 - a
{azimuth, altitude}
end
@doc """
Returns a `t:Geo.PointZ` containing
the right ascension and declination of
the sun at a given date or date time.
## Arguments
* `date_time` is a `DateTime` or a `Date` or
any struct that meets the requirements of
`t:Calendar.date` or `t:Calendar.datetime`
## Returns
* a `t:Geo.PointZ` struct with coordinates
`{right_ascension, declination, distance}` with properties
`%{reference: :celestial, object: :sun}`.
`distance` is in meters.
## Example
iex> Astro.sun_position_at(~D[1992-10-13])
%Geo.PointZ{
coordinates: {-161.6185428539835, -7.785325031528879, 149169604711.3518},
properties: %{object: :sun, reference: :celestial},
srid: nil
}
"""
@doc since: "0.6.0"
@spec sun_position_at(date()) :: Geo.PointZ.t()
def sun_position_at(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Solar.solar_position()
|> convert_distance_to_m()
|> Location.normalize_location()
|> Map.put(:properties, %{reference: :celestial, object: :sun})
end
def sun_position_at(unquote(Guards.date()) = date) do
_ = calendar
date
|> Date.to_gregorian_days()
|> Solar.solar_position()
|> convert_distance_to_m()
|> Location.normalize_location()
|> Map.put(:properties, %{reference: :celestial, object: :sun})
end
defp convert_distance_to_m({lng, lat, alt}) do
{lng, lat, Math.au_to_m(alt)}
end
@doc """
Returns a `t:Geo.PointZ` containing
the right ascension and declination of
the moon at a given date or date time.
## Arguments
* `date_time` is a `DateTime` or a `Date` or
any struct that meets the requirements of
`t:Calendar.date` or `t:Calendar.datetime`
## Returns
* a `t:Geo.PointZ` struct with coordinates
`{right_ascension, declination, distance}` with properties
`%{reference: :celestial, object: :moon}`
`distance` is in meters.
## Example
iex> Astro.moon_position_at(~D[1992-04-12]) |> Astro.Location.round(6)
%Geo.PointZ{
coordinates: {134.697888, 13.765243, 5.511320224169038e19},
properties: %{object: :moon, reference: :celestial},
srid: nil
}
"""
@doc since: "0.6.0"
@spec moon_position_at(date()) :: Geo.PointZ.t()
def moon_position_at(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.lunar_position()
|> convert_distance_to_m()
|> Location.normalize_location()
|> Map.put(:properties, %{reference: :celestial, object: :moon})
end
def moon_position_at(unquote(Guards.date()) = date) do
_ = calendar
date
|> Date.to_gregorian_days()
|> Lunar.lunar_position()
|> convert_distance_to_m()
|> Location.normalize_location()
|> Map.put(:properties, %{reference: :celestial, object: :moon})
end
@doc """
Returns the illumination of the moon
as a fraction for a given date or date time.
## Arguments
* `date_time` is a `DateTime` or a `Date` or
any struct that meets the requirements of
`t:Calendar.date` or `t:Calendar.datetime`
## Returns
* a `float` value between `0.0` and `1.0`
representing the fractional illumination of
the moon.
## Example
iex> Astro.illuminated_fraction_of_moon_at(~D[2017-03-16])
0.8884442367681415
iex> Astro.illuminated_fraction_of_moon_at(~D[1992-04-12])
0.6786428237168787
"""
@doc since: "0.6.0"
@spec illuminated_fraction_of_moon_at(date()) :: number()
def illuminated_fraction_of_moon_at(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.illuminated_fraction_of_moon()
end
def illuminated_fraction_of_moon_at(unquote(Guards.date()) = date) do
_ = calendar
date
|> Date.to_gregorian_days()
|> Lunar.illuminated_fraction_of_moon()
end
@doc """
Returns the date time of the new
moon before a given date or date time.
## Arguments
* `date_time` is a `DateTime` or a `Date` or
any struct that meets the requirements of
`t:Calendar.date` or `t:Calendar.datetime`
## Returns
* `{:ok, date_time}` at which the new moon occurs or
* `{:error, {module, reason}}`
## Example
iex> Astro.date_time_new_moon_before ~D[2021-08-23]
{:ok, ~U[2021-08-08 13:49:07.000000Z]}
"""
@doc since: "0.5.0"
@spec date_time_new_moon_before(date()) ::
{:ok, Calendar.datetime()}, {:error, {module(), String.t}}
def date_time_new_moon_before(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.date_time_new_moon_before()
|> Time.date_time_from_moment()
end
def date_time_new_moon_before(unquote(Guards.date()) = date) do
_ = calendar
date
|> Date.to_gregorian_days()
|> Lunar.date_time_new_moon_before()
|> Time.date_time_from_moment()
end
@doc """
Returns the date time of the new
moon at or after a given date or
date time.
## Arguments
* `date_time` is a `DateTime` or a `Date` or
any struct that meets the requirements of
`t:Calendar.date` or `t:Calendar.datetime`
## Returns
* `{:ok, date_time}` at which the new moon occurs or
* `{:error, {module, reason}}`
## Example
iex> Astro.date_time_new_moon_at_or_after ~D[2021-08-23]
{:ok, ~U[2021-09-07 00:50:43.000000Z]}
"""
@doc since: "0.5.0"
@spec date_time_new_moon_at_or_after(date) ::
{:ok, Calendar.datetime()}, {:error, {module(), String.t}}
def date_time_new_moon_at_or_after(unquote(Guards.datetime()) = datetime) do
_ = calendar
datetime
|> Time.date_time_to_moment()
|> Lunar.date_time_new_moon_at_or_after()
|> Time.date_time_from_moment()
end
def date_time_new_moon_at_or_after(unquote(Guards.date()) = date) do
_ = calendar
date
|> Date.to_gregorian_days()
|> Lunar.date_time_new_moon_at_or_after()
|> Time.date_time_from_moment()
end
@doc """
Returns the lunar phase as a
float number of degrees at a given
date or date time.
## Arguments
* `date_time` is a `DateTime`, `Date` or
a `moment` which is a float number of days
since `0000-01-01`
## Returns
* the lunar phase as a float number of
degrees.
## Example
iex> Astro.lunar_phase_at ~U[2021-08-22 12:01:02.170362Z]
180.00001498208536
iex> Astro.lunar_phase_at(~U[2021-07-10 01:18:25.422335Z])
0.021567106773019873
"""
@doc since: "0.5.0"
@spec lunar_phase_at(date()) :: phase()
def lunar_phase_at(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.lunar_phase_at()
end
def lunar_phase_at(unquote(Guards.date()) = date) do
_ = calendar
date
|> Date.to_gregorian_days()
|> Lunar.lunar_phase_at()
end
@doc """
Returns the moon phase as a UTF8 binary
representing an emoji of the moon phase.
## Arguments
* `phase` is a moon phase between `0.0` and `360.0`
## Returns
* A single grapheme string representing the [Unicode
moon phase emoji](https://unicode-table.com/en/sets/moon/)
## Examples
iex> Astro.lunar_phase_emoji 0
"🌑"
iex> Astro.lunar_phase_emoji 45
"🌒"
iex> Astro.lunar_phase_emoji 90
"🌓"
iex> Astro.lunar_phase_emoji 135
"🌔"
iex> Astro.lunar_phase_emoji 180
"🌕"
iex> Astro.lunar_phase_emoji 245
"🌖"
iex> Astro.lunar_phase_emoji 270
"🌗"
iex> Astro.lunar_phase_emoji 320
"🌘"
iex> Astro.lunar_phase_emoji 360
"🌑"
iex> ~U[2021-08-22 12:01:02.170362Z]
...> |> Astro.lunar_phase_at()
...> |> Astro.lunar_phase_emoji()
"🌕"
"""
@emoji_base 0x1f310
@emoji_phase_count 8
@emoji_phase (360.0 / @emoji_phase_count)
@spec lunar_phase_emoji(phase()) :: String.t()
def lunar_phase_emoji(360) do
lunar_phase_emoji(0)
end
def lunar_phase_emoji(phase) when is_lunar_phase(phase) do
offset = ceil(phase / @emoji_phase + 0.5)
:unicode.characters_to_binary([offset + @emoji_base])
end
@doc """
Returns the date time of a given
lunar phase at or before a given
date time or date.
## Arguments
* `date_time` is a `DateTime` or a `Date` or
any struct that meets the requirements of
`t:Calendar.date` or `t:Calendar.datetime`
* `phase` is the required lunar phase expressed
as a float number of degrees between `0` and
`3660`
## Returns
* `{:ok, date_time}` at which the phase occurs or
* `{:error, {module, reason}}`
## Example
iex> Astro.date_time_lunar_phase_at_or_before(~D[2021-08-01], Astro.Lunar.new_moon())
{:ok, ~U[2021-07-10 01:15:33.000000Z]}
"""
@doc since: "0.5.0"
@spec date_time_lunar_phase_at_or_before(date(), Astro.phase()) ::
{:ok, Calendar.datetime()}, {:error, {module(), String.t}}
def date_time_lunar_phase_at_or_before(unquote(Guards.datetime()) = date_time, phase) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.date_time_lunar_phase_at_or_before(phase)
|> Time.date_time_from_moment()
end
def date_time_lunar_phase_at_or_before(unquote(Guards.date()) = date, phase) do
_ = calendar
date
|> Date.to_gregorian_days()
|> Lunar.date_time_lunar_phase_at_or_before(phase)
|> Time.date_time_from_moment()
end
@doc """
Returns the date time of a given
lunar phase at or after a given
date time or date.
## Arguments
* `date_time` is a `DateTime` or a `Date` or
any struct that meets the requirements of
`t:Calendar.date` or `t:Calendar.datetime`
* `phase` is the required lunar phase expressed
as a float number of degrees between `0.0` and
`360.0`
## Returns
* `{:ok, date_time}` at which the phase occurs or
* `{:error, {module, reason}}`
## Example
iex> Astro.date_time_lunar_phase_at_or_after(~D[2021-08-01], Astro.Lunar.full_moon())
{:ok, ~U[2021-08-22 12:01:02.000000Z]}
"""
@doc since: "0.5.0"
@spec date_time_lunar_phase_at_or_after(date(), Astro.phase()) ::
{:ok, Calendar.datetime()}, {:error, {module(), String.t}}
def date_time_lunar_phase_at_or_after(unquote(Guards.datetime()) = date_time, phase) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.date_time_lunar_phase_at_or_after(phase)
|> Time.date_time_from_moment()
end
def date_time_lunar_phase_at_or_after(unquote(Guards.date()) = date, phase) do
_ = calendar
date
|> Date.to_gregorian_days()
|> Lunar.date_time_lunar_phase_at_or_after(phase)
|> Time.date_time_from_moment()
end
@doc """
Calculates the sunrise for a given location and date.
Sunrise is the moment when the upper limb of
the sun appears on the horizon in the morning.
## Arguments
* `location` is the latitude, longitude and
optionally elevation for the desired sunrise
time. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `Geo.Point.t` struct to represent a location without elevation
* a `Geo.PointZ.t` struct to represent a location and elevation
* `date` is a `t:Date`, `t:NaiveDateTime` or `t:DateTime`
to indicate the date of the year in which
the sunrise time is required.
* `options` is a keyword list of options.
## Options
* `solar_elevation` represents the type of sunrise
required. The default is `:geometric` which equates to
a solar elevation of 90°. In this case the calulation
also accounts for refraction and elevation to return a
result which accords with the eyes perception. Other
solar elevations are:
* `:civil` representing a solar elevation of 96.0°. At this
point the sun is just below the horizon so there is
generally enough natural light to carry out most
outdoor activities.
* `:nautical` representing a solar elevation of 102.0°
This is the point at which the horizon is just barely visible
and the moon and stars can still be used for navigation.
* `:astronomical`representing a solar elevation of 108.0°.
This is the point beyond which astronomical observation
becomes impractical.
* Any floating point number representing the desired
solar elevation.
* `:time_zone` is the time zone to in which the sunrise
is requested. The default is `:default` in which
the sunrise time is reported in the time zone of
the requested location. Any other time zone name
supported by the option `:time_zone_database` is
acceptabe.
* `:time_zone_database` represents the module that
implements the `Calendar.TimeZoneDatabase` behaviour.
The default is `Tzdata.TimeZoneDatabase`.
## Returns
* a `DateTime.t` representing the time of sunrise in the
requested timzone at the requested location or
* `{:error, :time_zone_not_found}` if the requested
time zone is unknown
* `{:error, :no_time}` if for the requested date
and location there is no sunrise. This can occur at
very high latitudes during summer and winter.
## Examples
# Sunrise in Sydney, Australia
Astro.sunrise({151.20666584, -33.8559799094}, ~D[2019-12-04])
{:ok, #DateTime<2019-12-04 05:37:00.000000+11:00 AEDT Australia/Sydney>}
# Sunrise in Alert, Nanavut, Canada
Astro.sunrise({-62.3481, 82.5018}, ~D[2019-12-04])
{:error, :no_time}
"""
@spec sunrise(location, date, options) ::
{:ok, DateTime.t()} | {:error, :time_zone_not_found | :no_time}
def sunrise(location, date, options \\ default_options()) when is_list(options) do
options = Keyword.put(options, :rise_or_set, :rise)
Solar.sun_rise_or_set(location, date, options)
end
@doc """
Calculates the sunset for a given location and date.
Sunset is the moment when the upper limb of
the sun disappears below the horizon in the evening.
## Arguments
* `location` is the latitude, longitude and
optionally elevation for the desired sunrise
time. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `Geo.Point.t` struct to represent a location without elevation
* a `Geo.PointZ.t` struct to represent a location and elevation
* `date` is a `t:Date`, `t:NaiveDateTime` or `t:DateTime`
to indicate the date of the year in which
the sunset time is required.
* `options` is a keyword list of options.
## Options
* `solar_elevation` represents the type of sunset
required. The default is `:geometric` which equates to
a solar elevation of 90°. In this case the calulation
also accounts for refraction and elevation to return a
result which accords with the eyes perception. Other
solar elevations are:
* `:civil` representing a solar elevation of 96.0°. At this
point the sun is just below the horizon so there is
generally enough natural light to carry out most
outdoor activities.
* `:nautical` representing a solar elevation of 102.0°
This is the point at which the horizon is just barely visible
and the moon and stars can still be used for navigation.
* `:astronomical`representing a solar elevation of 108.0°.
This is the point beyond which astronomical observation
becomes impractical.
* Any floating point number representing the desired
solar elevation.
* `:time_zone` is the time zone to in which the sunset
is requested. The default is `:default` in which
the sunset time is reported in the time zone of
the requested location. Any other time zone name
supported by the option `:time_zone_database` is
acceptabe.
* `:time_zone_database` represents the module that
implements the `Calendar.TimeZoneDatabase` behaviour.
The default is `Tzdata.TimeZoneDatabase`.
## Returns
* a `t:DateTime` representing the time of sunset in the
requested time zone at the requested location or
* `{:error, :time_zone_not_found}` if the requested
time zone is unknown
* `{:error, :no_time}` if for the requested date
and location there is no sunset. This can occur at
very high latitudes during summer and winter.
## Examples
# Sunset in Sydney, Australia
Astro.sunset({151.20666584, -33.8559799094}, ~D[2019-12-04])
{:ok, #DateTime<2019-12-04 19:53:00.000000+11:00 AEDT Australia/Sydney>}
# Sunset in Alert, Nanavut, Canada
Astro.sunset({-62.3481, 82.5018}, ~D[2019-12-04])
{:error, :no_time}
"""
@spec sunset(location, date, options) ::
{:ok, DateTime.t()} | {:error, :time_zone_not_found | :no_time}
def sunset(location, date, options \\ default_options()) when is_list(options) do
options = Keyword.put(options, :rise_or_set, :set)
Solar.sun_rise_or_set(location, date, options)
end
@doc """
Returns the datetime in UTC for either the
March or September equinox.
## Arguments
* `year` is the gregorian year for which the equinox is
to be calculated
* `event` is either `:march` or `:september` indicating
which of the two annual equinox datetimes is required
## Returns
* `{:ok, datetime}` representing the UTC datetime of
the equinox
## Examples
iex> Astro.equinox 2019, :march
{:ok, ~U[2019-03-20 21:58:06Z]}
iex> Astro.equinox 2019, :september
{:ok, ~U[2019-09-23 07:49:30Z]}
## Notes
This equinox calculation is expected to be accurate
to within 2 minutes for the years 1000 CE to 3000 CE.
An equinox is commonly regarded as the instant of
time when the plane of Earth's equator passes through
the center of the Sun. This occurs twice each year:
around 20 March and 23 September.
In other words, it is the moment at which the
center of the visible Sun is directly above the equator.
"""
@spec equinox(Calendar.year(), :march | :september) :: {:ok, DateTime.t()}
def equinox(year, event) when event in [:march, :september] and year in 1000..3000 do
Solar.equinox_and_solstice(year, event)
end
@doc """
Returns the datetime in UTC for either the
June or December solstice.
## Arguments
* `year` is the gregorian year for which the solstice is
to be calculated
* `event` is either `:june` or `:december` indicating
which of the two annual solstice datetimes is required
## Returns
* `{:ok, datetime}` representing the UTC datetime of
the solstice
## Examples
iex> Astro.solstice 2019, :december
{:ok, ~U[2019-12-22 04:18:57Z]}
iex> Astro.solstice 2019, :june
{:ok, ~U[2019-06-21 15:53:45Z]}
## Notes
This solstice calculation is expected to be accurate
to within 2 minutes for the years 1000 CE to 3000 CE.
A solstice is an event occurring when the Sun appears
to reach its most northerly or southerly excursion
relative to the celestial equator on the celestial
sphere. Two solstices occur annually, around June 21
and December 21.
The seasons of the year are determined by
reference to both the solstices and the equinoxes.
The term solstice can also be used in a broader
sense, as the day when this occurs. The day of a
solstice in either hemisphere has either the most
sunlight of the year (summer solstice) or the least
sunlight of the year (winter solstice) for any place
other than the Equator.
Alternative terms, with no ambiguity as to which
hemisphere is the context, are "June solstice" and
"December solstice", referring to the months in
which they take place every year.
"""
@spec solstice(Calendar.year(), :june | :december) :: {:ok, DateTime.t()}
def solstice(year, event) when event in [:june, :december] and year in 1000..3000 do
Solar.equinox_and_solstice(year, event)
end
@doc """
Returns solar noon for a
given date and location as
a UTC datetime
## Arguments
* `location` is the latitude, longitude and
optionally elevation for the desired solar noon
time. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `Geo.Point.t` struct to represent a location without elevation
* a `Geo.PointZ.t` struct to represent a location and elevation
* `date` is any date in the Gregorian
calendar (for example, `Calendar.ISO`)
## Returns
* a UTC datetime representing solar noon
at the given location for the given date
## Example
iex> Astro.solar_noon {151.20666584, -33.8559799094}, ~D[2019-12-06]
{:ok, ~U[2019-12-06 01:45:42Z]}
## Notes
Solar noon is the moment when the Sun passes a
location's meridian and reaches its highest position
in the sky. In most cases, it doesn't happen at 12 o'clock.
At solar noon, the Sun reaches its
highest position in the sky as it passes the
local meridian.
"""
@spec solar_noon(Astro.location(), Calendar.date()) :: {:ok, DateTime.t()}
def solar_noon(location, date) do
%Geo.PointZ{coordinates: {longitude, _, _}} = Location.normalize_location(location)
julian_day = Time.julian_day_from_date(date)
julian_centuries = Time.julian_centuries_from_julian_day(julian_day)
julian_centuries
|> Solar.solar_noon_utc(-longitude)
|> Time.datetime_from_date_and_minutes(date)
end
@doc """
Returns solar longitude for a
given date. Solar longitude is used
to identify the seasons.
## Arguments
* `date` is any date in the Gregorian
calendar (for example, `Calendar.ISO`)
## Returns
* a `float` number of degrees between 0 and
360 representing the solar longitude
on `date`
## Examples
iex> Astro.sun_apparent_longitude ~D[2019-03-21]
0.08035853207991295
iex> Astro.sun_apparent_longitude ~D[2019-06-22]
90.32130455695378
iex> Astro.sun_apparent_longitude ~D[2019-09-23]
179.68691978440197
iex> Astro.sun_apparent_longitude ~D[2019-12-23]
270.83941087483504
## Notes
Solar longitude (the ecliptic longitude of the sun)
in effect describes the position of the earth in its
orbit, being zero at the moment of the vernal
equinox.
Since it is based on how far the earth has moved
in its orbit since the equinox, it is a measure of
what time of the tropical year (the year of seasons)
we are in, but without the inaccuracies of a calendar
date, which is perturbed by leap years and calendar
imperfections.
"""
@spec sun_apparent_longitude(Calendar.date()) :: degrees()
def sun_apparent_longitude(date) do
date
|> Time.julian_day_from_date()
|> Time.julian_centuries_from_julian_day()
|> Solar.sun_apparent_longitude()
end
@doc """
Returns the number of hours of daylight for a given
location on a given date.
## Arguments
* `location` is the latitude, longitude and
optionally elevation for the desired hours of
daylight. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `Geo.Point.t` struct to represent a location without elevation
* a `Geo.PointZ.t` struct to represent a location and elevation
* `date` is any date in the Gregorian
calendar (for example, `Calendar.ISO`)
## Returns
* `{:ok, time}` where `time` is a `Time.t()`
## Examples
iex> Astro.hours_of_daylight {151.20666584, -33.8559799094}, ~D[2019-12-07]
{:ok, ~T[14:18:45]}
# No sunset in summer
iex> Astro.hours_of_daylight {-62.3481, 82.5018}, ~D[2019-06-07]
{:ok, ~T[23:59:59]}
# No sunrise in winter
iex> Astro.hours_of_daylight {-62.3481, 82.5018}, ~D[2019-12-07]
{:ok, ~T[00:00:00]}
## Notes
In latitudes above the polar circles (approximately
+/- 66.5631 degrees) there will be no hours of daylight
in winter and 24 hours of daylight in summer.
"""
@spec hours_of_daylight(Astro.location(), Calendar.date()) :: {:ok, Elixir.Time.t()}
def hours_of_daylight(location, date) do
with {:ok, sunrise} <- sunrise(location, date),
{:ok, sunset} <- sunset(location, date) do
seconds_of_sunlight = DateTime.diff(sunset, sunrise)
{hours, minutes, seconds} = Time.seconds_to_hms(seconds_of_sunlight)
Elixir.Time.new(hours, minutes, seconds)
else
{:error, :no_time} ->
if no_daylight_hours?(location, date) do
Elixir.Time.new(0, 0, 0)
else
Elixir.Time.new(23, 59, 59)
end
end
end
@polar_circle_latitude 66.5631
defp no_daylight_hours?(location, date) do
%Geo.PointZ{coordinates: {_longitude, latitude, _elevation}} =
Location.normalize_location(location)
cond do
(latitude >= @polar_circle_latitude and date.month in 10..12) or date.month in 1..3 -> true
latitude <= -@polar_circle_latitude and date.month in 4..9 -> true