-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path4-normal.go
100 lines (86 loc) · 2.48 KB
/
4-normal.go
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
package prayer
import (
"math"
"time"
"github.com/hablullah/go-sampa"
)
func calcNormal(cfg Config, year int) ([]Schedule, int) {
// Prepare location
location := sampa.Location{
Latitude: cfg.Latitude,
Longitude: cfg.Longitude,
Elevation: cfg.Elevation,
}
// Prepare custom Sun events
customEvents := []sampa.CustomSunEvent{{
Name: "dawn",
BeforeTransit: true,
Elevation: func(sampa.SunPosition) float64 { return -18 },
}, {
Name: "dusk",
BeforeTransit: false,
Elevation: func(sampa.SunPosition) float64 { return -18 },
}, {
Name: "fajr",
BeforeTransit: true,
Elevation: func(sampa.SunPosition) float64 { return -cfg.TwilightConvention.FajrAngle },
}, {
Name: "isha",
BeforeTransit: false,
Elevation: func(sampa.SunPosition) float64 { return -cfg.TwilightConvention.IshaAngle },
}, {
Name: "asr",
BeforeTransit: false,
Elevation: func(todayData sampa.SunPosition) float64 {
a := getAsrCoefficient(cfg.AsrConvention)
b := math.Abs(todayData.TopocentricDeclination - cfg.Latitude)
elevation := acot(a + math.Tan(degToRad(b)))
return radToDeg(elevation)
},
}}
// Calculate schedules for each day in a year.
start := time.Date(year, 1, 1, 0, 0, 0, 0, cfg.Timezone)
limit := start.AddDate(1, 0, 0)
nDays := int(limit.Sub(start).Hours() / 24)
// Create slice to contain result
schedules := make([]Schedule, nDays)
// Calculate each day
var idx int
var nAbnormal int
for dt := start; dt.Before(limit); dt = dt.AddDate(0, 0, 1) {
// Calculate the events
e, _ := sampa.GetSunEvents(dt, location, nil, customEvents...)
// Create the prayer schedule
s := Schedule{
Date: dt.Format("2006-01-02"),
Fajr: e.Others["fajr"].DateTime,
Sunrise: e.Sunrise.DateTime,
Zuhr: e.Transit.DateTime,
Asr: e.Others["asr"].DateTime,
Maghrib: e.Sunset.DateTime,
Isha: e.Others["isha"].DateTime,
}
// Check if schedule is normal
dawn := e.Others["dawn"].DateTime
dusk := e.Others["dusk"].DateTime
hasNight := !e.Sunrise.IsZero() && !e.Sunset.IsZero()
hasTwilight := !dawn.IsZero() && !dusk.IsZero()
s.IsNormal = hasNight && hasTwilight
// Save the schedule
schedules[idx] = s
if !s.IsNormal {
nAbnormal++
}
idx++
}
return schedules, nAbnormal
}
func radToDeg(rad float64) float64 {
return rad * 180 / math.Pi
}
func degToRad(deg float64) float64 {
return deg * math.Pi / 180
}
func acot(cotValue float64) float64 {
return math.Atan(1 / cotValue)
}