| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# This script read in a Callisto schedule file from STDIN, and filters |
| 4 |
# it so that recording is on only between sunrise and sunset. The |
| 5 |
# result is output to STDOUT. The coordinates are read from the |
| 6 |
# Callisto configuration file given as the first command line |
| 7 |
# argument. |
| 8 |
# |
| 9 |
# Emacs calendar is used to calculate the sunrise and sunset |
| 10 |
# times. This may seem a bit strange, but it is really hard to find a |
| 11 |
# "standard" (here defined as available in standard Debian |
| 12 |
# repositories) program that does it and does it right. Emacs was the |
| 13 |
# best option of the three I managed to find, which are the following |
| 14 |
# (with their pros and cons): |
| 15 |
# |
| 16 |
# Emacs calendar |
| 17 |
# + Emacs is a very standard package |
| 18 |
# + Handles polar night and day |
| 19 |
# - A bit heavy for just this purpose |
| 20 |
# - Needs timezone compensation to get both rise and set times in |
| 21 |
# some cases (see below) |
| 22 |
# |
| 23 |
# DateTime::Event::Sunrise |
| 24 |
# + A Perl module, would be the natural option for a Perl script |
| 25 |
# - Does not handle polar night or day, only carps about sun not |
| 26 |
# setting/rising and returns bogus times (though identical for |
| 27 |
# rise and set) |
| 28 |
# - Sometimes may get stuck in an infinite loop |
| 29 |
# |
| 30 |
# gcal |
| 31 |
# + *Seems* to handle polar night and day, at least if correct input |
| 32 |
# is given |
| 33 |
# - impossible-to-learn syntax |
| 34 |
# - Bug: If height above sea level is given in the coordinates, it |
| 35 |
# just shifts the timezoine. Who knows what other bugs there may |
| 36 |
# be. |
| 37 |
# - Not as standard as Emacs |
| 38 |
|
| 39 |
|
| 40 |
$DEBUG = 0; |
| 41 |
$MARGIN = 0; |
| 42 |
|
| 43 |
# this is used to terminate a line in the output schedule: |
| 44 |
$EOL = "\r\n"; |
| 45 |
|
| 46 |
if ($ARGV[0] eq "-d") { |
| 47 |
shift(@ARGV); |
| 48 |
$DEBUG = 1; |
| 49 |
} |
| 50 |
|
| 51 |
if (scalar(@ARGV) != 1 && scalar(@ARGV) != 2) { |
| 52 |
print(STDERR "ERROR: Usage: $0 <callisto.cfg> [<margin>]\n"); |
| 53 |
exit 1; |
| 54 |
} |
| 55 |
|
| 56 |
if (scalar(@ARGV) == 2) { |
| 57 |
$MARGIN = int($ARGV[1]); |
| 58 |
if ($MARGIN < 0) { |
| 59 |
print(STDERR "ERROR: invalid margin\n"); |
| 60 |
exit 1; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
# Read callisto configuration file |
| 65 |
if (!open(F, "<$ARGV[0]")) { |
| 66 |
print(STDERR "ERROR: Cannot open configuration file $ARGV[0]\n"); |
| 67 |
exit 1; |
| 68 |
} |
| 69 |
while ($l = <F>) { |
| 70 |
if ($l =~ /^\s*\[latitude\]=([NnSs]),([\d\.]+)(\s.*)$/i) { |
| 71 |
$LATITUDE = 1.0*$2; |
| 72 |
$LATITUDE = -$LATITUDE if ($1 =~ /[Ss]/); |
| 73 |
} |
| 74 |
if ($l =~ /^\s*\[longitude\]=([WwEe]),([\d\.]+)(\s.*)$/i) { |
| 75 |
$LONGITUDE = 1.0*$2; |
| 76 |
$LONGITUDE = -$LONGITUDE if ($1 =~ /[Ww]/); |
| 77 |
} |
| 78 |
if ($l =~ /^\s*\[focuscode\]=(\d\d)(\s.*)$/i) { |
| 79 |
$FOCUSCODE = $1; |
| 80 |
} |
| 81 |
} |
| 82 |
close(F); |
| 83 |
|
| 84 |
if (!defined($LATITUDE) || !defined($LONGITUDE) || !defined($FOCUSCODE)) { |
| 85 |
print(STDERR "ERROR: Bad configuration file: $ARGV[0]\n"); |
| 86 |
exit 1; |
| 87 |
} |
| 88 |
|
| 89 |
print(STDERR "long=$LONGITUDE, lat=$LATITUDE, fc=$FOCUSCODE\n") if ($DEBUG); |
| 90 |
|
| 91 |
|
| 92 |
# Read base schedule from stdin |
| 93 |
$total = 0; |
| 94 |
@schedule = (); |
| 95 |
%schedule_time = (); |
| 96 |
%schedule_action = (); |
| 97 |
while ($l = <STDIN>) { |
| 98 |
$l =~ s/^\s*//; |
| 99 |
next if ($l =~ /^(\/.*)?$/); |
| 100 |
|
| 101 |
$total++; |
| 102 |
if ($l =~ /^(\d\d:\d\d:\d\d,\d\d,\d)/) { |
| 103 |
$l = $1; |
| 104 |
@ll = split(/,/, $l); |
| 105 |
if ($ll[1] == $FOCUSCODE) { |
| 106 |
push(@schedule, $l); |
| 107 |
$schedule_action{$l} = $ll[2]; |
| 108 |
@ll = split(/:/, $ll[0]); |
| 109 |
$schedule_time{$l} = 3600*$ll[0] + 60*$ll[1] + $ll[2]; |
| 110 |
} |
| 111 |
} else { |
| 112 |
print(STDERR "WARNING: Ignoring malformed schedule entry: $l"); |
| 113 |
} |
| 114 |
} |
| 115 |
@schedule = sort(@schedule); |
| 116 |
print(STDERR "Base schedule:\n ", join("\n ", @schedule), "\n") if ($DEBUG); |
| 117 |
|
| 118 |
if (!scalar(@schedule) && $total) { |
| 119 |
print(STDERR "WARNING: Empty schedule for FC $FOCUSCODE\n"); |
| 120 |
} |
| 121 |
|
| 122 |
# find latest start/stop action in the schedule: |
| 123 |
$action = 3; # default to start (i.e. always on) |
| 124 |
for $s (@schedule) { |
| 125 |
if ($schedule_action{$s} =~ /^[023]$/) { |
| 126 |
$action = $schedule_action{$s}; |
| 127 |
} |
| 128 |
} |
| 129 |
print(STDERR "Last action: $action\n") if ($DEBUG); |
| 130 |
|
| 131 |
|
| 132 |
# find sunrise and sunset times for this UTC date: |
| 133 |
($Year, $Month, $Day) = utc_day(time()); |
| 134 |
($up, $dn) = emacs_sunrise($LATITUDE, $LONGITUDE, $Year, $Month, $Day); |
| 135 |
exit 1 if ($up < 0 || $dn < 0); |
| 136 |
|
| 137 |
if ($up <= $dn) { |
| 138 |
$nightlen = 86400 - ($dn - $up); |
| 139 |
} else { |
| 140 |
$nightlen = $up - $dn; |
| 141 |
} |
| 142 |
|
| 143 |
if ($nightlen < 2*$MARGIN) { |
| 144 |
# night is too short to apply margin, turn schedule into polar day: |
| 145 |
$up = 0; |
| 146 |
$dn = 86400; |
| 147 |
} elsif ($nightlen < 86400) { |
| 148 |
# if there is a day, lengthen it from both ends by margin seconds: |
| 149 |
$up = (86400 + $up - $MARGIN) % 86400; |
| 150 |
$dn = ($dn + $MARGIN) % 86400; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
# print the filtered schedule |
| 155 |
print("// Schedule file generated by callisto-sunschedule.$EOL"); |
| 156 |
$s = 0; |
| 157 |
$n = scalar(@schedule); |
| 158 |
if ($up < $dn) { # night-day-night |
| 159 |
while ($s < $n && $schedule_time{$schedule[$s]} <= $up) { |
| 160 |
if ($schedule_action{$schedule[$s]} =~ /^[023]$/) { |
| 161 |
$action = $schedule_action{$schedule[$s]}; |
| 162 |
} elsif ($schedule_action{$schedule[$s]} eq "8") { |
| 163 |
print($schedule[$s], $EOL); |
| 164 |
} |
| 165 |
$s++; |
| 166 |
} |
| 167 |
|
| 168 |
printf("%02u:%02u:%02u,%02u,%u$EOL", |
| 169 |
int($up / 3600), int($up / 60) % 60, $up % 60, |
| 170 |
$FOCUSCODE, |
| 171 |
$action); |
| 172 |
|
| 173 |
while ($s < $n && $schedule_time{$schedule[$s]} < $dn) { |
| 174 |
print($schedule[$s], $EOL); |
| 175 |
$s++; |
| 176 |
} |
| 177 |
|
| 178 |
if ($dn < 86400) { |
| 179 |
printf("%02u:%02u:%02u,%02u,0$EOL", |
| 180 |
int($dn / 3600), int($dn / 60) % 60, $dn % 60, |
| 181 |
$FOCUSCODE); |
| 182 |
} |
| 183 |
|
| 184 |
while ($s < $n) { |
| 185 |
if ($schedule_action{$schedule[$s]} eq "8") { |
| 186 |
print($schedule[$s], $EOL); |
| 187 |
} |
| 188 |
$s++; |
| 189 |
} |
| 190 |
|
| 191 |
} else { # day-night-day |
| 192 |
while ($s < $n && $schedule_time{$schedule[$s]} < $dn) { |
| 193 |
if ($schedule_action{$schedule[$s]} =~ /^[023]$/) { |
| 194 |
$action = $schedule_action{$schedule[$s]}; |
| 195 |
} |
| 196 |
print($schedule[$s], $EOL); |
| 197 |
$s++; |
| 198 |
} |
| 199 |
|
| 200 |
printf("%02u:%02u:%02u,%02u,0$EOL", |
| 201 |
int($dn / 3600), int($dn / 60) % 60, $dn % 60, |
| 202 |
$FOCUSCODE); |
| 203 |
|
| 204 |
while ($s < $n && $schedule_time{$schedule[$s]} <= $up) { |
| 205 |
if ($schedule_action{$schedule[$s]} =~ /^[023]$/) { |
| 206 |
$action = $schedule_action{$schedule[$s]}; |
| 207 |
} elsif ($schedule_action{$schedule[$s]} eq "8") { |
| 208 |
print($schedule[$s], $EOL); |
| 209 |
} |
| 210 |
$s++; |
| 211 |
} |
| 212 |
|
| 213 |
if ($up < 86400) { |
| 214 |
printf("%02u:%02u:%02u,%02u,%u$EOL", |
| 215 |
int($up / 3600), int($up / 60) % 60, $up % 60, |
| 216 |
$FOCUSCODE, |
| 217 |
$action); |
| 218 |
} |
| 219 |
|
| 220 |
while ($s < $n) { |
| 221 |
print($schedule[$s], $EOL); |
| 222 |
$s++; |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
sub utc_day { |
| 236 |
my @t = gmtime($_[0]); |
| 237 |
return ($t[5]+1900, $t[4]+1, $t[3]); |
| 238 |
} |
| 239 |
|
| 240 |
sub emacs_sunrise_query { |
| 241 |
my ($LAT, $LON, $Y, $M, $D, $TZ) = @_; |
| 242 |
my $sr = |
| 243 |
"(let* (". |
| 244 |
" (srdate '($M $D $Y))". |
| 245 |
" (calendar-latitude $LAT)". |
| 246 |
" (calendar-longitude $LON)". |
| 247 |
" (calendar-daylight-savings-starts nil)". |
| 248 |
" (calendar-daylight-savings-starts nil)". |
| 249 |
" (calendar-time-zone $TZ)". |
| 250 |
" (sr (solar-sunrise-sunset srdate))". |
| 251 |
" )". |
| 252 |
" (print (car (car sr)))". |
| 253 |
" (print (car (car (cdr sr))))". |
| 254 |
" (print (car (cdr (cdr sr))))". |
| 255 |
" (print (solar-sunrise-sunset-string srdate))". |
| 256 |
")"; |
| 257 |
|
| 258 |
@answer = `emacs -batch --eval "(require 'solar)" --eval "$sr" 2>/dev/null | grep -v ^\$`; |
| 259 |
|
| 260 |
for my $a (@answer) { |
| 261 |
chomp($a); |
| 262 |
$a =~ s/\"//g; |
| 263 |
} |
| 264 |
return @answer; |
| 265 |
} |
| 266 |
|
| 267 |
sub emacs_sunrise { |
| 268 |
my ($LAT, $LON, $Y, $M, $D) = @_; |
| 269 |
my $TZ = 0; |
| 270 |
|
| 271 |
my ($up, $dn, $len, $str) = |
| 272 |
emacs_sunrise_query($LAT, $LON, $Y, $M, $D, $TZ); |
| 273 |
print(STDERR "emacs_sunrise: $up -> $dn ($len) \@0\n") if ($DEBUG); |
| 274 |
|
| 275 |
if ($len eq "0:00") { |
| 276 |
print(STDERR "Polar night\n") if ($DEBUG); |
| 277 |
return (86400, 0); |
| 278 |
} elsif ($len eq "24:00") { |
| 279 |
print(STDERR "Polar day\n") if ($DEBUG); |
| 280 |
return (0, 86400); |
| 281 |
} elsif ($up eq "nil" && $dn eq "nil") { |
| 282 |
print(STDERR "ERROR: no sunrise or sunset but day length is $len\n"); |
| 283 |
return (-1 -1); |
| 284 |
} |
| 285 |
|
| 286 |
if ($up eq "nil") { |
| 287 |
$TZ = int(60*(24.0 - $dn)) - 1; |
| 288 |
($up, $dn, $len, $str) = |
| 289 |
emacs_sunrise_query($LAT, $LON, $Y, $M, $D, $TZ); |
| 290 |
} elsif ($dn eq "nil") { |
| 291 |
$TZ = int(-60*$up) + 1; |
| 292 |
($up, $dn, $len, $str) = |
| 293 |
emacs_sunrise_query($LAT, $LON, $Y, $M, $D, $TZ); |
| 294 |
} |
| 295 |
|
| 296 |
if ($up eq "nil" || $dn eq "nil") { |
| 297 |
# The compensation is +-minute, so *very* short night/day will |
| 298 |
# cause this even with compensation |
| 299 |
if ($len =~ /^0:0[123]$/) { |
| 300 |
print(STDERR "Almost polar night\n") if ($DEBUG); |
| 301 |
return (86400, 0); |
| 302 |
} elsif ($len =~ /^23:5[789]$/) { |
| 303 |
print(STDERR "Almost polar day\n") if ($DEBUG); |
| 304 |
return (0, 86400); |
| 305 |
} else { |
| 306 |
print(STDERR "ERROR: no sunrise/sunset but day length is ". |
| 307 |
"significant ($len)\n"); |
| 308 |
return (-2, -2); |
| 309 |
} |
| 310 |
} |
| 311 |
print(STDERR "emacs_sunrise(compensated): $up -> $dn ($len) \@$TZ\n") if ($DEBUG); |
| 312 |
|
| 313 |
$up -= $TZ/60; |
| 314 |
$dn -= $TZ/60; |
| 315 |
$up += 24 if ($up < 0); |
| 316 |
$up -= 24 if ($up >= 24); |
| 317 |
$dn += 24 if ($dn < 0); |
| 318 |
$dn -= 24 if ($dn >= 24); |
| 319 |
|
| 320 |
if ($up < $dn) { |
| 321 |
print(STDERR "Final(NDN): $up -> $dn ($len) \@0\n") if ($DEBUG); |
| 322 |
} else { |
| 323 |
print(STDERR "Final(DND): $up -> $dn ($len) \@0\n") if ($DEBUG); |
| 324 |
} |
| 325 |
|
| 326 |
return (int(3600*$up), int(3600*$dn)); |
| 327 |
} |