-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdaily.sh
More file actions
executable file
·147 lines (147 loc) · 5.01 KB
/
daily.sh
File metadata and controls
executable file
·147 lines (147 loc) · 5.01 KB
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
#!/bin/bash
SECONDS=0
# ---------------------------------------------------
YEAR=`date '+%Y'`
MONTH=`date '+%m'`
DAY=`date '+%d'`
HOUR=`date '+%H'`
# ---------------------------------------------------
# source the configuration file
# it must be edited and copied as ".BirdNET-BarChart" to you home directory
CONFIG_FILE=${HOME}/.BirdNET-BarChart
if [ -f "${CONFIG_FILE}" ]; then
source ${CONFIG_FILE}
else
echo " "
echo "${CONFIG_FILE} does not exist."
echo "Run the config.sh script to create."
echo " "
exit 1
fi
# ---------------------------------------------------
function calculateMoonPhase {
# Input: year, month, day; trim leading zeros
yy=$YEAR
mm=${MONTH#0}
dd=${DAY#0}
# Adjust month and year
if [ "$mm" -lt 3 ]; then
yy=$((yy - 1))
mm=$((mm + 12))
fi
mm=$((mm + 1))
# Calculate components using bc
c=$(echo "scale=5; 365.25 * $yy" | bc)
e=$(echo "scale=5; 30.6 * $mm" | bc)
jd=$(echo "scale=5; $c + $e + $dd - 694039.09" | bc)
jd=$(echo "scale=5; $jd / 29.5305882" | bc)
# Integer part (b), and fractional part
b=$(echo "$jd" | cut -d'.' -f1)
frac=$(echo "scale=5; $jd - $b" | bc)
# Multiply fractional part by 8 and round to nearest integer
b=$(echo "$frac * 8 + 0.5" | bc | awk '{printf "%f", $0}' | cut -d'.' -f1)
# Normalize if b == 8
if [ "$b" -ge 8 ]; then
b=0
fi
# Output results
case $b in
0)
echo "New Moon"
;;
1)
echo "Waxing Crescent"
;;
2)
echo "First Quarter"
;;
3)
echo "Waxing Gibbous"
;;
4)
echo "Full Moon"
;;
5)
echo "Waning Gibbous"
;;
6)
echo "Last Quarter"
;;
7)
echo "Waning Crescent"
;;
esac
}
# ---------------------------------------------------
{
# ===================================================
# record celestial data
MAXTRYS=5
for i in $(seq 1 $MAXTRYS)
do
curl --max-time 30 -H "Cache-Control: no-cache, no-store" "https://aa.usno.navy.mil/api/rstt/oneday?date=${YEAR}-${MONTH}-${DAY}&coords=${LAT},${LON}&tz=-8&dst=true" > ${BARCHART_HOME}/sky/day.js
EXITCODE=$?
if [[ $EXITCODE -eq 0 ]]
then
echo "<day>" > ${BARCHART_HOME}/sky/day.xml
jq -rf ${BARCHART_HOME}/util/json2xml.jq ${BARCHART_HOME}/sky/day.js >> ${BARCHART_HOME}/sky/day.xml
# insert calculated moon phase
PHASE=$(calculateMoonPhase)
echo "<phase>${PHASE}</phase>" >> ${BARCHART_HOME}/sky/day.xml
echo "</day>" >> ${BARCHART_HOME}/sky/day.xml
java -classpath ${XSLT_JAR} net.sf.saxon.Transform -s:${BARCHART_HOME}/sky/day.xml -xsl:${BARCHART_HOME}/sky/day.xsl > ${BARCHART_HOME}/sky/day.sql
sqlite3 ${BARCHART_HOME}/birds.db < ${BARCHART_HOME}/sky/day.sql
echo "celestial success on attempt ${i}"
break
else
echo "curl: ${EXITCODE}"
echo "celestial fail on attempt ${i}"
cat ${BARCHART_HOME}/sky/day.js
echo "===== celestial FAILURE ====="
# --------------------------
# complete failure is ignored
# subsequent query gets last success
# --------------------------
sleep 5
fi
done
# ===================================================
# initialize the dials if not already there
if [ ! -f ${BARCHART_HOME}/web/grfx/lunar/dial.gif ]; then
cp -v ${BARCHART_HOME}/web/grfx/lunar/init.gif ${BARCHART_HOME}/web/grfx/lunar/dial.gif
fi
if [ ! -f ${BARCHART_HOME}/web/grfx/seasonal/dial.gif ]; then
cp -v ${BARCHART_HOME}/web/grfx/seasonal/init.gif ${BARCHART_HOME}/web/grfx/seasonal/dial.gif
fi
# save the midnight/noon snapshot to seasonal
find ${BARCHART_HOME}/web/grfx/lunar/ -name "snapshot-*00.gif" -type f -exec cp -v {} ${BARCHART_HOME}/web/grfx/seasonal \;
find ${BARCHART_HOME}/web/grfx/lunar/ -name "snapshot-*12.gif" -type f -exec cp -v {} ${BARCHART_HOME}/web/grfx/seasonal \;
# seasonal
# count frames in the animations
FRAMECOUNT=$(identify ${BARCHART_HOME}/web/grfx/seasonal/dial.gif | wc -l)
# trim if longer than (2*360=720) twice per day for 360 days
if (( $FRAMECOUNT > 720 )); then
gifsicle --batch ${BARCHART_HOME}/web/grfx/seasonal/dial.gif --delete '#0-1'
fi
gifsicle --batch ${BARCHART_HOME}/web/grfx/seasonal/dial.gif --append ${BARCHART_HOME}/web/grfx/seasonal/snapshot-*.gif
# remove appended frames
rm -v ${BARCHART_HOME}/web/grfx/seasonal/snapshot-*.gif
# lunar
# count frames in the animations
FRAMECOUNT=$(identify ${BARCHART_HOME}/web/grfx/lunar/dial.gif | wc -l)
# trim if longer than (24*30=720) 24 hours per day for 30 days
if (( $FRAMECOUNT > 720 )); then
gifsicle --batch ${BARCHART_HOME}/web/grfx/lunar/dial.gif --delete '#0-23'
fi
# append new frames
gifsicle --batch ${BARCHART_HOME}/web/grfx/lunar/dial.gif --append ${BARCHART_HOME}/web/grfx/lunar/snapshot-*.gif
# report on the result
gifsicle --info ${BARCHART_HOME}/web/grfx/lunar/dial.gif
# remove appended frames
rm -v ${BARCHART_HOME}/web/grfx/lunar/snapshot-*.gif
# ===================================================
# how long did it take
DURATION=$SECONDS
echo "$(($DURATION / 60)) minutes and $(($DURATION % 60)) seconds elapsed."
echo "---------------------------------------------------------------------------------"
} >> ${BARCHART_HOME}/logs/${YEAR}-${MONTH}-${DAY}-${HOUR}-daily.out 2>> ${BARCHART_HOME}/logs/${YEAR}-${MONTH}-${DAY}-${HOUR}-daily.err