-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.sh
More file actions
663 lines (557 loc) · 22.8 KB
/
weather.sh
File metadata and controls
663 lines (557 loc) · 22.8 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
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
#!/bin/bash
bond_db_file="${HOME}/.bond/db.json"
current_weather_json="${HOME}/.bond/weather.json"
current_weather_txt="${HOME}/.bond/weather.txt"
current_solar_json="${HOME}/.bond/solar.json"
updateRan=0
bond_token=''
ip_address=''
forcerefresh=0
current_hour=$( date +'%Y-%m-%dT%H:' )
# Extract the path using dirname
path=$(dirname "$0")
while getopts "F" opt; do
case $opt in
F)
forcerefresh=1
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
esac
done
if [[ -s "${current_weather_txt}" && "${forcerefresh}" -eq 0 ]]
then
if [[ $( find "${current_weather_txt}" -mmin -20 | wc -l ) -eq 1 ]]
then
cat "${current_weather_txt}"
exit
fi
fi
# Make sure we have jq installed.
if [[ -z "$( command -v jq )" ]]
then
if [[ "${updateRan}" -eq 0 ]]
then
sudo apt update
updateRan=1
fi
echo "Installing jq"
sudo apt install jq -y
echo
fi
# Make sure we have bc installed.
if [[ -z "$( command -v bc )" ]]
then
if [[ "${updateRan}" -eq 0 ]]
then
sudo apt update
updateRan=1
fi
echo "Installing bc"
sudo apt install bc -y
echo
fi
# Make sure we have hdate installed.
if [[ -z "$( command -v hdate )" ]]
then
if [[ "${updateRan}" -eq 0 ]]
then
sudo apt update
updateRan=1
fi
echo "Installing hdate"
sudo apt install hdate -y
echo
fi
BondSelect () {
if [[ -r "${bond_db_file}" ]]
then
echo "Reading ${bond_db_file} file"
selected_bondid=$( jq -r '.selected_bondid // empty' < "$bond_db_file" )
fi
}
BondGetIPFromFile () {
if [[ -z "${ip_address}" ]]
then
if [[ -r "${bond_db_file}" ]]
then
ip_address=$( jq -r ".bonds.${selected_bondid}.ip // empty" < "$bond_db_file" )
fi
fi
}
BondTokenGet() {
# JSON
if [[ -z "${bond_token}" ]]
then
if [[ -r "$bond_db_file" ]]
then
bond_token=$( jq -r ".bonds.${selected_bondid}.token // empty" < "$bond_db_file" )
fi
fi
}
BondGetTime() {
BondSelect
BondGetIPFromFile
BondTokenGet
local bondTime
bondTime=$( curl -s --max-time 5 -H "BOND-Token: ${bond_token}" "http://${ip_address}/v2/sys/time" )
maidenhead=$( echo "${bondTime}" | jq -r '.grid //empty' )
timezone=$( echo "${bondTime}" | jq -r '.tz //empty' )
tzOffest=$( TZ=":${timezone}" date +%:z )
# Replace "+0" with "+"
formatted_offset=$(echo "$tzOffest" | sed 's/+0/+/g' | sed 's/-0/-/g')
}
maidenhead_to_lat_long() {
local grid="$1"
# Initialize variables
local latmult=10
local lonmult=20
local lat=-90
local lon=-180
# Initialize initial_String with L and 5
local initial_String="LL55LL55LL"
# Replace the first characters of initial_String with the grid value
local initial_String="${grid}${initial_String:${#grid}}"
for ((i = 0; i < ${#initial_String}; i+=2)); do
if (( (i+1) % 4 == 1 )); then
if (( i > 0 )); then
lonmult=$(bc <<< "scale=8; $lonmult / 24")
latmult=$(bc <<< "scale=8; $latmult / 24")
fi
char1="${initial_String:i:1}"
char2="${initial_String:i+1:1}"
char1_rank=$(printf "%d" "'${char1^^}'")
char2_rank=$(printf "%d" "'${char2^^}'")
lon=$(bc <<< "scale=8; $lon + $lonmult * ($char1_rank - 65)")
lat=$(bc <<< "scale=8; $lat + $latmult * ($char2_rank - 65)")
else
latmult=$(bc <<< "scale=8; $latmult / 10")
lonmult=$(bc <<< "scale=8; $lonmult / 10")
char1="${initial_String:i:1}"
char2="${initial_String:i+1:1}"
lon=$(bc <<< "scale=8; $lon + $lonmult * $char1")
lat=$(bc <<< "scale=8; $lat + $latmult * $char2")
fi
done
printf "%.4f,%.4f" "${lat}" "${lon}"
}
getSunriseSunset() {
coordinates=$1
formatted_offset=$2
lat=$(echo "${coordinates}" | cut -d',' -f1)
long=$(echo "${coordinates}" | cut -d',' -f2)
hdateOutput=$( hdate -z "${formatted_offset}" -l "${lat}" -L "${long}" -t)
sunrise=$( echo "${hdateOutput}" | grep 'sunrise' )
sunset=$( echo "${hdateOutput}" | grep 'sunset' )
midday=$( echo "${hdateOutput}" | grep 'midday' )
}
cToF() {
if [[ -z "${1}" ]]
then
return
fi
printf "%.0f" "$( bc <<< "scale=2; 9/5 * $1 + 32" )"
}
fToC() {
if [[ -z "${1}" ]]
then
return
fi
printf "%.0f" "$( bc <<< "scale=2; ($1 - 32) * 5 / 9" )"
}
mmToInch() {
unit=${1}
if [[ -z "${unit}" ]]
then
echo "0.00"
return
fi
printf "%.2f" "$( bc <<< "scale=4; $1 / 25.4")"
}
kphToMph() {
# Conversion factor: 1 km/h = 0.621371 mph
local conversion_factor=0.621371
# Perform the conversion using bc
printf "%.1f" "$(echo "scale=2; $1 * $conversion_factor" | bc)"
}
calculate_heat_index_f() {
local temperature=$1
local humidity=$2
local hi1
local hi2
local c1
local adjustment
hi1=$( echo "scale=8; 0.5 * (${temperature} + 61.0 + ((${temperature}-68.0) * 1.2) + ($humidity * 0.094))" | bc -l )
if [[ $(echo "${hi1} < 80" | bc -l) ]]
then
printf "%.0f" "${hi1}"
return
fi
hi2=$( echo "scale=8; -42.379 + (2.04901523 * $temperature) + (10.14333127 * $humidity) - (0.22475541 * $temperature * $humidity) - (0.00683783 * $temperature * $temperature) - (0.05481717 * $humidity * $humidity) + (0.00122874 * $temperature * $temperature * $humidity) + (0.00085282 * $temperature * $humidity * $humidity) - (0.00000199 * $temperature * $temperature * $humidity * $humidity )" | bc -l )
if [[ $( echo "${humidity} < 13" | bc -l ) && $( echo "${temperature} > 80" | bc -l ) && $( echo "${temperature} < 112" | bc -l ) ]]
then
c1=$( echo "($temperature - 95)" | bc )
adjustment=$(echo "scale=8; ((13 - $humidity) / 4) * sqrt((17 - ${c1#-}) / 17)" | bc -l)
hi2=$( echo "${hi2} - ${adjustment}" | bc -l )
printf "%.0f" "${hi2}"
return
fi
if [[ $( echo "${humidity} > 85" | bc -l ) && $( echo "${temperature} > 80" | bc -l ) && $( echo "${temperature} < 87" | bc -l ) ]]
then
adjustment=$(echo "scale=8; (($humidity - 85) / 10) * ((87 - $temperature) / 5)" | bc -l)
hi2=$( echo "${hi2} + ${adjustment}" | bc -l )
printf "%.0f" "${hi2}"
return
fi
printf "%.0f" "${hi2}"
}
GetClosestTime() {
timestamps=${1}
target_timestamp=$(date +%s)
closest_timestamp=''
closest_diff=${target_timestamp}
for timestamp in $timestamps
do
diff=$((timestamp - target_timestamp))
diff=$( echo "if ($diff < 0) -($diff) else $diff" | bc)
if [[ -z $closest_timestamp || $diff -lt $closest_diff ]]
then
closest_timestamp=$timestamp
closest_diff=$diff
fi
done
echo "${closest_timestamp}"
}
GetValueOfClosetTime() {
input=${1}
# Convert dates to unix time
inputValues=$( echo "${input}" | cut -d " " -f 1 )
inputTimes=$( echo "${input}" | cut -d " " -f 2 | awk -F"[-T:]" '{print mktime(sprintf("%04d %02d %02d %02d %02d %02d", $1, $2, $3, $4, $5, $6))}' )
inputTimestamp=$( GetClosestTime "${inputTimes}" )
inputCombinedData=$( paste -d ' ' <(echo "${inputValues}") <(echo "${inputTimes}") )
input=$( echo "${inputCombinedData}" | grep "${inputTimestamp}" | cut -d " " -f 1 )
echo "${input}"
}
GetAverageBeforeAndAfterNoon() {
data=${1}
ValueBeforeNoon=0
ValueBeforeNoonCounter=0
ValueAfterNoon=0
ValueAfterNoonCounter=0
while read -r line
do
time=$(echo "$line" | awk '{print $2}')
value=$(echo "$line" | awk '{print $1}')
unix_time=$(date -d "$time" +%s)
if [[ $unix_time -lt $unix_midday ]]
then
#>&2 echo "Morning: ${ValueBeforeNoon} + ${value}"
ValueBeforeNoon=$( echo "${ValueBeforeNoon} + ${value}" | bc )
((ValueBeforeNoonCounter++))
else
#>&2 echo "Afternoon: ${ValueAfterNoon} + ${value}"
ValueAfterNoon=$( echo "${ValueAfterNoon} + ${value}" | bc )
((ValueAfterNoonCounter++))
fi
done <<< "${data}"
AverageBeforeNoon="N/A"
if [[ "${ValueBeforeNoonCounter}" -gt 0 ]]
then
AverageBeforeNoon=$(echo "${ValueBeforeNoon} / ${ValueBeforeNoonCounter}" | bc)
fi
AverageAfterNoon="N/A"
if [[ "${ValueAfterNoonCounter}" -gt 0 ]]
then
AverageAfterNoon=$(echo "${ValueAfterNoon} / ${ValueAfterNoonCounter}" | bc)
fi
echo "Morning: ${AverageBeforeNoon}"
echo "Afternoon: ${AverageAfterNoon}"
}
GetMaxBeforeAndAfterNoon() {
data=${1}
MaxValueBeforeNoon=-320
MaxValueAfterNoon=-320
while read -r line
do
time=$(echo "$line" | awk '{print $2}')
value=$(echo "$line" | awk '{print $1}')
unix_time=$(date -d "$time" +%s)
if [[ $unix_time -lt $unix_midday ]]
then
if [[ $(echo "${value} > ${MaxValueBeforeNoon}" | bc -l) ]]
then
MaxValueBeforeNoon=${value}
fi
else
if [[ $(echo "${value} > ${MaxValueAfterNoon}" | bc -l) ]]
then
MaxValueAfterNoon=${value}
fi
fi
done <<< "${data}"
if [[ "${MaxValueBeforeNoon}" == "-320" ]]
then
MaxValueBeforeNoon="N/A"
fi
if [[ "${MaxValueAfterNoon}" == "-320" ]]
then
MaxValueAfterNoon="N/A"
fi
echo "Morning: ${MaxValueBeforeNoon}"
echo "Afternoon: ${MaxValueAfterNoon}"
}
BondGetTime
coordinates=$(maidenhead_to_lat_long "$maidenhead")
getSunriseSunset "${coordinates}" "${formatted_offset}"
# Get weather data.
if [[ -s "${current_weather_json}" || "${forcerefresh}" -eq 1 ]]
then
if [[ $( find "${current_weather_json}" -mmin -15 | wc -l ) -eq 0 || "${forcerefresh}" -eq 1 ]]
then
echo "Updating weather info as it is older than 15 minutes."
url=$( curl -s "https://api.weather.gov/points/${coordinates}" | jq -r '.properties.forecastGridData')
echo "Weather URL: ${url}"
if [[ -n "${url}" ]]
then
weather=$( curl -s "${url}" | jq '.properties' )
if [[ -n "${weather}" ]]
then
echo "${weather}" > "${current_weather_json}"
fi
fi
fi
fi
if [[ -z "${weather}" ]]
then
weather=$( cat "$current_weather_json" )
fi
# Get sun data once every 8 hours.
current_hour=$(date +%H)
current_minute=$(date +%M)
if (( current_hour % 8 == 0 )) && (( current_minute < 10 ))
then
lat=$( echo "$coordinates" | cut -d ',' -f 1 )
long=$( echo "$coordinates" | cut -d ',' -f 2 )
echo "https://api.forecast.solar/estimate/${lat}/${long}/0/0/1"
solar=$( curl -s "https://api.forecast.solar/estimate/${lat}/${long}/0/0/1" )
result=$(echo "${solar}" | jq -r '.result')
if [[ "${result}" != "null" ]]
then
echo "$solar" > "${current_solar_json}"
fi
fi
if [[ -z "${solar}" ]]
then
solar=$( cat "${current_solar_json}" )
fi
# Get total watt hours in the day
searchDate=$( date +'%Y-%m-%d' )
wattHoursDay=$( echo "${solar}" | jq -r --arg searchDate "${searchDate}" '.result.watt_hours_day[$searchDate]' )
# get Morning and afternoon watt hours
unix_midday=$( echo "${midday} ${tzOffest}" | awk '{print $2 $3}' )
unix_midday=$( date -d "${unix_midday}" +%s )
watt_data=$( echo "${solar}" | jq -r '.result.watts | to_entries[] | "\(.key) \(.value)"' | grep "${searchDate}" | grep -v ' 0$' )
#echo "${watt_data}"
wattHoursBeforeNoon=0
wattHoursAfterNoon=0
target_timestamp=$(date +%s)
closest_timestamp=''
closest_timestamp_search=''
closest_diff=${target_timestamp}
while read -r line
do
time=$(echo "$line" | awk '{print $1, $2}')
value=$(echo "$line" | awk '{print $3}')
unix_time=$(date -d "$time" +%s)
if [[ $unix_time -lt $unix_midday ]]
then
wattHoursBeforeNoon=$((wattHoursBeforeNoon + value))
else
wattHoursAfterNoon=$((wattHoursAfterNoon + value))
fi
diff=$((unix_time - target_timestamp))
diff=$( echo "if ($diff < 0) -($diff) else $diff" | bc)
if [[ -z $closest_timestamp || $diff -lt $closest_diff ]]
then
closest_timestamp=$unix_time
closest_timestamp_search=$time
closest_diff=$diff
fi
done <<< "${watt_data}"
wattHoursNow=$( echo "${watt_data}" | grep "${closest_timestamp_search}" | awk '{print $3}')
updateTime=$( echo "${weather}" | jq -r '.updateTime' | cut -d '+' -f 1 )
validTime=$( echo "${weather}" | jq -r '.validTimes' | cut -d '+' -f 1 )
searchTime=$( date +'%Y-%m-%dT%H:' )
searchDate=$( date +'%Y-%m-%dT' )
temperatureNow=$( echo "${weather}" | jq --arg searchDate "${searchDate}" -r '.temperature.values[] | select(.validTime | startswith($searchDate)) | "\(.value) \(.validTime)"' | cut -d "+" -f 1 )
#temperatureMorningAfternoon=$( GetAverageBeforeAndAfterNoon "${temperatureNow}" )
#temperatureMorning=$( echo "${temperatureMorningAfternoon}" | grep -oP 'Morning: \K\d+' )
#temperatureAfternoon=$( echo "${temperatureMorningAfternoon}" | grep -oP 'Afternoon: \K\d+' )
temperatureMaxMorningAfternoon=$( GetMaxBeforeAndAfterNoon "${temperatureNow}" )
temperatureMaxMorning=$( echo "${temperatureMaxMorningAfternoon}" | grep -oP 'Morning: \K\d+' )
temperatureMaxAfternoon=$( echo "${temperatureMaxMorningAfternoon}" | grep -oP 'Afternoon: \K\d+' )
temperatureNow=$( GetValueOfClosetTime "${temperatureNow}" )
temperatureNowUnit=$( echo "${weather}" | jq -r '.temperature.uom' | tail -c 2 | tr -d '[:space:]' )
#echo "temperatureNow ${temperatureNow} ${temperatureNowUnit}"
temperatureMax=$( echo "${weather}" | jq --arg searchDate "${searchDate}" -r '.maxTemperature.values[] | select(.validTime | startswith($searchDate)) | .value' )
temperatureMaxUnit=$( echo "${weather}" | jq -r '.maxTemperature.uom' | tail -c 2 | tr -d '[:space:]' )
#echo "temperatureMax ${temperatureMax} ${temperatureMaxUnit}"
temperatureMin=$( echo "${weather}" | jq --arg searchDate "${searchDate}" -r '.minTemperature.values[] | select(.validTime | startswith($searchDate)) | .value' )
temperatureMinUnit=$( echo "${weather}" | jq -r '.minTemperature.uom' | tail -c 2 | tr -d '[:space:]' )
#echo "temperatureMin ${temperatureMin} ${temperatureMinUnit}"
heatIndexNow=$( echo "${weather}" | jq --arg searchTime "${searchTime}" -r '.heatIndex.values[] | select(.validTime | startswith($searchTime)) | .value // empty' | tr -d '[:space:]' )
heatIndexNowUnit=$( echo "${weather}" | jq -r '.heatIndex.uom' | tail -c 2 | tr -d '[:space:]' )
#echo "heatIndexNow ${heatIndexNow} ${heatIndexNowUnit}"
apparentTemperatureNow=$( echo "${weather}" | jq --arg searchDate "${searchDate}" -r '.apparentTemperature.values[] | select(.validTime | startswith($searchDate)) | "\(.value) \(.validTime)"' | cut -d "+" -f 1 )
apparentTemperatureNow=$( GetValueOfClosetTime "${apparentTemperatureNow}" )
apparentTemperatureNowUnit=$( echo "${weather}" | jq -r '.apparentTemperature.uom' | tail -c 2 | tr -d '[:space:]' )
#echo "apparentTemperatureNow ${apparentTemperatureNow} ${apparentTemperatureNowUnit}"
relativeHumidity=$( echo "${weather}" | jq --arg searchDate "${searchDate}" -r '.relativeHumidity.values[] | select(.validTime | startswith($searchDate)) | "\(.value) \(.validTime)"' | cut -d "+" -f 1 )
relativeHumidity=$( GetValueOfClosetTime "${relativeHumidity}" )
#echo "relativeHumidity ${relativeHumidity}"
skyCoverNow=$( echo "${weather}" | jq --arg searchDate "${searchDate}" -r '.skyCover.values[] | select(.validTime | startswith($searchDate)) | "\(.value) \(.validTime)"' | cut -d "+" -f 1 )
skyCoverMorningAfternoon=$( GetAverageBeforeAndAfterNoon "${skyCoverNow}" )
skyCoverMorning=$( echo "${skyCoverMorningAfternoon}" | grep -oP 'Morning: \K\d+' )
skyCoverAfternoon=$( echo "${skyCoverMorningAfternoon}" | grep -oP 'Afternoon: \K\d+' )
skyCoverNow=$( GetValueOfClosetTime "${skyCoverNow}" )
probabilityOfPrecipitationNow=$( echo "${weather}" | jq --arg searchTime "${searchTime}" -r '.probabilityOfPrecipitation.values[] | select(.validTime | startswith($searchTime)) | .value // empty' | tr -d '[:space:]' )
if [[ -z "${probabilityOfPrecipitationNow}" ]]
then
probabilityOfPrecipitationNow=0
fi
#echo "probabilityOfPrecipitationNow ${probabilityOfPrecipitationNow}"
quantitativePrecipitationNow=$( echo "${weather}" | jq --arg searchTime "${searchTime}" -r '.quantitativePrecipitation.values[] | select(.validTime | startswith($searchTime)) | .value // empty' | tr -d '[:space:]' )
quantitativePrecipitationNowUnit=$( echo "${weather}" | jq -r '.quantitativePrecipitation.uom // empty' | tr -d '[:space:]' | tail -c 2 )
#echo "quantitativePrecipitationNow ${quantitativePrecipitationNow} ${quantitativePrecipitationNowUnit}"
windSpeedNow=$( echo "${weather}" | jq --arg searchDate "${searchDate}" -r '.windSpeed.values[] | select(.validTime | startswith($searchDate)) | "\(.value) \(.validTime)"' | cut -d "+" -f 1 )
#windSpeedMorningAfternoon=$( GetAverageBeforeAndAfterNoon "${windSpeedNow}" )
windSpeedNow=$( GetValueOfClosetTime "${windSpeedNow}" )
windSpeedNowUnit=$( echo "${weather}" | jq -r '.windSpeed.uom' | cut -d ":" -f2 | sed 's/_h-1/\/h/g' )
#echo "windSpeedNow ${windSpeedNow} ${windSpeedNowUnit}"
windGustNow=$( echo "${weather}" | jq --arg searchDate "${searchDate}" -r '.windGust.values[] | select(.validTime | startswith($searchDate)) | "\(.value) \(.validTime)"' | cut -d "+" -f 1 )
#windGustMorningAfternoon=$( GetAverageBeforeAndAfterNoon "${windGustNow}" )
windGustNow=$( GetValueOfClosetTime "${windGustNow}" )
windGustNowUnit=$( echo "${weather}" | jq -r '.windGust.uom' | cut -d ":" -f2 | sed 's/_h-1/\/h/g' )
#echo "windGustNow ${windGustNow} ${windGustNowUnit}"
transportWindSpeedNow=$( echo "${weather}" | jq --arg searchDate "${searchDate}" -r '.transportWindSpeed.values[] | select(.validTime | startswith($searchDate)) | "\(.value) \(.validTime)"' | cut -d "+" -f 1 )
#transportWindSpeedMorningAfternoon=$( GetAverageBeforeAndAfterNoon "${transportWindSpeedNow}" )
transportWindSpeedNow=$( GetValueOfClosetTime "${transportWindSpeedNow}" )
transportWindSpeedNowUnit=$( echo "${weather}" | jq -r '.transportWindSpeed.uom' | cut -d ":" -f2 | sed 's/_h-1/\/h/g' )
#echo "transportWindSpeedNow ${transportWindSpeedNow} ${transportWindSpeedNowUnit}"
# Convert to F from C if needed.
temperatureMaxMorningUnit=${temperatureNowUnit}
temperatureMaxAfternoonUnit=${temperatureNowUnit}
if [[ "${temperatureNowUnit}" == "C" || "${temperatureNowUnit}" == "c" ]]
then
temperatureNow=$( cToF "${temperatureNow}" )
temperatureNowUnit="F"
fi
if [[ "${temperatureMaxMorningUnit}" == "C" || "${temperatureMaxMorningUnit}" == "c" ]]
then
temperatureMaxMorning=$( cToF "${temperatureMaxMorning}" )
temperatureMaxMorningUnit="F"
fi
if [[ "${temperatureMaxAfternoonUnit}" == "C" || "${temperatureMaxAfternoonUnit}" == "c" ]]
then
temperatureMaxAfternoon=$( cToF "${temperatureMaxAfternoon}" )
temperatureMaxAfternoonUnit="F"
fi
if [[ "${temperatureMaxUnit}" == "C" || "${temperatureMaxUnit}" == "c" ]]
then
temperatureMax=$( cToF "${temperatureMax}" )
temperatureMaxUnit="F"
fi
if [[ "${temperatureMinUnit}" == "C" || "${temperatureMinUnit}" == "c" ]]
then
temperatureMin=$( cToF "${temperatureMin}" )
temperatureMinUnit="F"
fi
if [[ "${heatIndexNowUnit}" == "C" || "${heatIndexNowUnit}" == "c" ]]
then
if [[ -n "${heatIndexNow}" ]]
then
heatIndexNow=$( cToF "${heatIndexNow}" )
heatIndexNowUnit="F"
fi
fi
if [[ "${apparentTemperatureNowUnit}" == "C" || "${apparentTemperatureNowUnit}" == "c" ]]
then
apparentTemperatureNow=$( cToF "${apparentTemperatureNow}" )
apparentTemperatureNowUnit="F"
fi
# Convert to mph from km/h.
if [[ "${windSpeedNowUnit}" == "km/h" || "${windSpeedNowUnit}" == "km/h" ]]
then
windSpeedNow=$( kphToMph "${windSpeedNow}" )
windSpeedNowUnit="mph"
fi
if [[ "${windGustNowUnit}" == "km/h" || "${windGustNowUnit}" == "km/h" ]]
then
windGustNow=$( kphToMph "${windGustNow}" )
windGustNowUnit="mph"
fi
if [[ "${transportWindSpeedNowUnit}" == "km/h" || "${transportWindSpeedNowUnit}" == "km/h" ]]
then
transportWindSpeedNow=$( kphToMph "${transportWindSpeedNow}" )
transportWindSpeedNowUnit="mph"
fi
# Convert to in from mm
if [[ "${quantitativePrecipitationNowUnit}" == "mm" ]]
then
quantitativePrecipitationNow=$( mmToInch "${quantitativePrecipitationNow}" )
quantitativePrecipitationNowUnit="in"
fi
# Calculate heat index if missing.
if [[ -z "${heatIndexNow}" ]]
then
heatIndexNow=$( calculate_heat_index_f "${temperatureNow}" "${relativeHumidity}" )
heatIndexNowUnit="F"
fi
max_wind_speed=$(echo -e "${windSpeedNow}\n${windGustNow}\n${transportWindSpeedNow}" | grep -oE '[0-9.]+' | awk '{print $1}' | sort -nr | head -1)
current_time=$( date +"%H:%M" )
unix_time_now=$( date +%s )
unix_validTime=$( echo "${validTime}" | awk -F"[-T:]" '{print mktime(sprintf("%04d %02d %02d %02d %02d %02d", $1, $2, $3, $4, $5, $6))}' )
unix_updateTime=$( echo "${updateTime}" | awk -F"[-T:]" '{print mktime(sprintf("%04d %02d %02d %02d %02d %02d", $1, $2, $3, $4, $5, $6))}' )
unix_sunrise=$( echo "${sunrise} ${tzOffest}" | awk '{print $2 $3}' )
unix_sunrise=$( date -d "${unix_sunrise}" +%s )
unix_sunset=$( echo "${sunset} ${tzOffest}" | awk '{print $2 $3}' )
unix_sunset=$( date -d "${unix_sunset}" +%s )
unix_midday=$( echo "${midday} ${tzOffest}" | awk '{print $2 $3}' )
unix_midday=$( date -d "${unix_midday}" +%s )
air_quality=$( bash "${path}/airquality.sh" )
output=$(cat <<EOF
Coordinates: ${coordinates}
Valid Weather Time: ${validTime} ${unix_validTime}
Weather Data Good Until: ${updateTime} ${unix_updateTime}
Generated time: ${current_time} ${tzOffest} ${unix_time_now}
${sunrise} ${tzOffest} ${unix_sunrise}
${sunset} ${tzOffest} ${unix_sunset}
${midday} ${tzOffest} ${unix_midday}
Heat Index: ${heatIndexNow} F
Apparent Temperature: ${apparentTemperatureNow} ${apparentTemperatureNowUnit}
Temperature: ${temperatureNow} ${temperatureNowUnit}
Max Temperature: ${temperatureMax} ${temperatureMaxUnit}
Max Morning Temperature: ${temperatureMaxMorning} ${temperatureMaxMorningUnit}
Max Afternoon Temperature: ${temperatureMaxAfternoon} ${temperatureNowUnit}
Min Temperature: ${temperatureMin} ${temperatureMinUnit}
Relative Humidity: ${relativeHumidity} %
Sky Cover Now: ${skyCoverNow} %
Average Sky Cover Morning: ${skyCoverMorning} %
Average Sky Cover Afternoon: ${skyCoverAfternoon} %
Solar Watt Now: ${wattHoursNow} (1kw system)
Solar Watt Hours Today: ${wattHoursDay} (1kw system)
Solar Watt Hours Morning: ${wattHoursBeforeNoon} (1kw system)
Solar Watt Hours Afternoon: ${wattHoursAfterNoon} (1kw system)
Probability of Precipitation: ${probabilityOfPrecipitationNow} %
Quantitative Precipitation: ${quantitativePrecipitationNow} ${quantitativePrecipitationNowUnit}
Wind: ${windSpeedNow} ${windSpeedNowUnit}
Wind Gust: ${windGustNow} ${windGustNowUnit}
Transport Wind: ${transportWindSpeedNow} ${transportWindSpeedNowUnit}
Max Wind: ${max_wind_speed} ${windSpeedNowUnit}
${air_quality}
EOF
)
echo "$output" > "${current_weather_txt}"
echo "$output"