Script to output tide information (tidal height for date and time)

There's a good riding spot near where I store my boat that's blocked by a bridge under which I can pass only when the tide is not too high. Unfortunately, most tide prediction services (including the couple of phone apps I tried) only report high/low tide points for the day or have a web interface that's a bit cumbersome to operate on a phone. So as any giant dork would do, I wrote a script to do this for me.

If the computer it runs on has a local harmonic tide clock and tide predictor program (David Flater's XTide, which on Debian and friends you can get with a simple "sudo apt-get install xtide"), it will get the tidal data from that, else it will leach off a remote Tide and Current Predictor service (that's also based off the XTide program). The script will output daily files with time stamp and tide in feet for hours between 6 am and 9 pm, at 5 min increments, and mark any that I have to watch out for (2.6+ ft for my boat's tower) with angled brackets.


Example run:

$ tides.sh
INFO: generating tide data locally
/path/to/dir/2012-11-05.Mon exists, skipping
/path/to/dir/2012-11-06.Tue exists, skipping
/path/to/dir/2012-11-07.Wed exists, skipping
/path/to/dir/2012-11-08.Thu exists, skipping
/path/to/dir/2012-11-09.Fri exists, skipping
/path/to/dir/2012-11-10.Sat exists, skipping
/path/to/dir/2012-11-11.Sun exists, skipping
/path/to/dir/2012-11-12.Mon exists, skipping
/path/to/dir/2012-11-13.Tue exists, skipping
/path/to/dir/2012-11-14.Wed exists, skipping
/path/to/dir/2012-11-15.Thu exists, skipping
/path/to/dir/2012-11-16.Fri exists, skipping
/path/to/dir/2012-11-17.Sat missing, creating
purging old version from cache..
populating cache with new page content..
200 http://example.com/path/to/page/

Resulting output:

$ cat 2012-11-17.Sat
2012-11-17 06:00 (PST) 0.985791
2012-11-17 06:05 (PST) 1.060347
2012-11-17 06:10 (PST) 1.134894
2012-11-17 06:15 (PST) 1.209046
2012-11-17 06:20 (PST) 1.283170
2012-11-17 06:25 (PST) 1.356894
2012-11-17 06:30 (PST) 1.430102
2012-11-17 06:35 (PST) 1.502444
2012-11-17 06:40 (PST) 1.574299
2012-11-17 06:45 (PST) 1.645329
2012-11-17 06:50 (PST) 1.715444
2012-11-17 06:55 (PST) 1.784559
2012-11-17 07:00 (PST) 1.852372
2012-11-17 07:05 (PST) 1.919261
2012-11-17 07:10 (PST) 1.984929
2012-11-17 07:15 (PST) 2.049311
2012-11-17 07:20 (PST) 2.112139
2012-11-17 07:25 (PST) 2.173771
2012-11-17 07:30 (PST) 2.233940
2012-11-17 07:35 (PST) 2.292591
2012-11-17 07:40 (PST) 2.349668
2012-11-17 07:45 (PST) 2.404938
2012-11-17 07:50 (PST) 2.458716
2012-11-17 07:55 (PST) 2.510760
2012-11-17 08:00 (PST) 2.561016
2012-11-17 08:05 (PST) 2.609272 <
2012-11-17 08:10 (PST) 2.655792 <
2012-11-17 08:15 (PST) 2.700355 <
2012-11-17 08:20 (PST) 2.742902 <
2012-11-17 08:25 (PST) 2.783373 <
2012-11-17 08:30 (PST) 2.821584 <
2012-11-17 08:35 (PST) 2.857728 <
2012-11-17 08:40 (PST) 2.891612 <
2012-11-17 08:45 (PST) 2.923175 <
2012-11-17 08:50 (PST) 2.952262 <
2012-11-17 08:55 (PST) 2.979009 <
2012-11-17 09:00 (PST) 3.003256 <
2012-11-17 09:05 (PST) 3.024947 <
2012-11-17 09:10 (PST) 3.044031 <
2012-11-17 09:15 (PST) 3.060409 <
2012-11-17 09:20 (PST) 3.074146 <
2012-11-17 09:25 (PST) 3.085147 <
2012-11-17 09:30 (PST) 3.093378 <
2012-11-17 09:35 (PST) 3.098801 <
2012-11-17 09:40 (PST) 3.101436 <
2012-11-17 09:45 (PST) 3.101216 <
2012-11-17 09:50 (PST) 3.097991 <
2012-11-17 09:55 (PST) 3.091750 <
2012-11-17 10:00 (PST) 3.082514 <
2012-11-17 10:05 (PST) 3.070311 <
2012-11-17 10:10 (PST) 3.055183 <
2012-11-17 10:15 (PST) 3.037180 <
2012-11-17 10:20 (PST) 3.016365 <
2012-11-17 10:25 (PST) 2.992810 <
2012-11-17 10:30 (PST) 2.966599 <
2012-11-17 10:35 (PST) 2.937826 <
2012-11-17 10:40 (PST) 2.906593 <
2012-11-17 10:45 (PST) 2.873014 <
2012-11-17 10:50 (PST) 2.837210 <
2012-11-17 10:55 (PST) 2.799311 <
2012-11-17 11:00 (PST) 2.759452 <
2012-11-17 11:05 (PST) 2.717778 <
2012-11-17 11:10 (PST) 2.674435 <
2012-11-17 11:15 (PST) 2.629577 <
2012-11-17 11:20 (PST) 2.583360
2012-11-17 11:25 (PST) 2.535943
2012-11-17 11:30 (PST) 2.487322
2012-11-17 11:35 (PST) 2.437982
2012-11-17 11:40 (PST) 2.387924
2012-11-17 11:45 (PST) 2.337306
2012-11-17 11:50 (PST) 2.286284
2012-11-17 11:55 (PST) 2.235012
2012-11-17 12:00 (PST) 2.183638
2012-11-17 12:05 (PST) 2.132307
2012-11-17 12:10 (PST) 2.081160
2012-11-17 12:15 (PST) 2.030328
2012-11-17 12:20 (PST) 1.979941
2012-11-17 12:25 (PST) 1.930118
2012-11-17 12:30 (PST) 1.880975
2012-11-17 12:35 (PST) 1.832620
2012-11-17 12:40 (PST) 1.785152
2012-11-17 12:45 (PST) 1.738668
2012-11-17 12:50 (PST) 1.693255
2012-11-17 12:55 (PST) 1.648995
2012-11-17 13:00 (PST) 1.605965
2012-11-17 13:05 (PST) 1.564235
2012-11-17 13:10 (PST) 1.523872
2012-11-17 13:15 (PST) 1.484939
2012-11-17 13:20 (PST) 1.447493
2012-11-17 13:25 (PST) 1.411591
2012-11-17 13:30 (PST) 1.377285
2012-11-17 13:35 (PST) 1.344627
2012-11-17 13:40 (PST) 1.313668
2012-11-17 13:45 (PST) 1.284457
2012-11-17 13:50 (PST) 1.257044
2012-11-17 13:55 (PST) 1.231480
2012-11-17 14:00 (PST) 1.207816
2012-11-17 14:05 (PST) 1.186103
2012-11-17 14:10 (PST) 1.166397
2012-11-17 14:15 (PST) 1.148753
2012-11-17 14:20 (PST) 1.133228
2012-11-17 14:25 (PST) 1.119881
2012-11-17 14:30 (PST) 1.108772
2012-11-17 14:35 (PST) 1.099939
2012-11-17 14:40 (PST) 1.093504
2012-11-17 14:45 (PST) 1.089496
2012-11-17 14:50 (PST) 1.087978
2012-11-17 14:55 (PST) 1.089003
2012-11-17 15:00 (PST) 1.092628
2012-11-17 15:05 (PST) 1.098914
2012-11-17 15:10 (PST) 1.107917
2012-11-17 15:15 (PST) 1.119690
2012-11-17 15:20 (PST) 1.134280
2012-11-17 15:25 (PST) 1.151731
2012-11-17 15:30 (PST) 1.172076
2012-11-17 15:35 (PST) 1.195345
2012-11-17 15:40 (PST) 1.221555
2012-11-17 15:45 (PST) 1.250717
2012-11-17 15:50 (PST) 1.282718
2012-11-17 15:55 (PST) 1.317759
2012-11-17 16:00 (PST) 1.355716
2012-11-17 16:05 (PST) 1.396551
2012-11-17 16:10 (PST) 1.440216
2012-11-17 16:15 (PST) 1.486649
2012-11-17 16:20 (PST) 1.535773
2012-11-17 16:25 (PST) 1.587499
2012-11-17 16:30 (PST) 1.641725
2012-11-17 16:35 (PST) 1.698332
2012-11-17 16:40 (PST) 1.757193
2012-11-17 16:45 (PST) 1.818163
2012-11-17 16:50 (PST) 1.881090
2012-11-17 16:55 (PST) 1.945588
2012-11-17 17:00 (PST) 2.011911
2012-11-17 17:05 (PST) 2.079662
2012-11-17 17:10 (PST) 2.148645
2012-11-17 17:15 (PST) 2.218658
2012-11-17 17:20 (PST) 2.289492
2012-11-17 17:25 (PST) 2.360934
2012-11-17 17:30 (PST) 2.432766
2012-11-17 17:35 (PST) 2.504770
2012-11-17 17:40 (PST) 2.576724
2012-11-17 17:45 (PST) 2.648410 <
2012-11-17 17:50 (PST) 2.719609 <
2012-11-17 17:55 (PST) 2.790107 <
2012-11-17 18:00 (PST) 2.859465 <
2012-11-17 18:05 (PST) 2.927943 <
2012-11-17 18:10 (PST) 2.995111 <
2012-11-17 18:15 (PST) 3.060780 <
2012-11-17 18:20 (PST) 3.124770 <
2012-11-17 18:25 (PST) 3.186913 <
2012-11-17 18:30 (PST) 3.247050 <
2012-11-17 18:35 (PST) 3.305033 <
2012-11-17 18:40 (PST) 3.360726 <
2012-11-17 18:45 (PST) 3.414008 <
2012-11-17 18:50 (PST) 3.464767 <
2012-11-17 18:55 (PST) 3.512905 <
2012-11-17 19:00 (PST) 3.558337 <
2012-11-17 19:05 (PST) 3.600851 <
2012-11-17 19:10 (PST) 3.640671 <
2012-11-17 19:15 (PST) 3.677600 <
2012-11-17 19:20 (PST) 3.711600 <
2012-11-17 19:25 (PST) 3.742643 <
2012-11-17 19:30 (PST) 3.770710 <
2012-11-17 19:35 (PST) 3.795793 <
2012-11-17 19:40 (PST) 3.817890 <
2012-11-17 19:45 (PST) 3.837008 <
2012-11-17 19:50 (PST) 3.853159 <
2012-11-17 19:55 (PST) 3.866363 <
2012-11-17 20:00 (PST) 3.876643 <
2012-11-17 20:05 (PST) 3.884025 <
2012-11-17 20:10 (PST) 3.888531 <
2012-11-17 20:15 (PST) 3.890223 <
2012-11-17 20:20 (PST) 3.889186 <
2012-11-17 20:25 (PST) 3.885574 <
2012-11-17 20:30 (PST) 3.879398 <
2012-11-17 20:35 (PST) 3.870731 <
2012-11-17 20:40 (PST) 3.859587 <
2012-11-17 20:45 (PST) 3.845949 <
2012-11-17 20:50 (PST) 3.829938 <
2012-11-17 20:55 (PST) 3.811475 <
2012-11-17 21:00 (PST) 3.790711 <

Here's the script:

#!/bin/bash
 
# Get weekly tide predictions
 
dir=/path/to/dir
 
if [ ! -d $dir ]; then
  mkdir $dir
fi
 
GetTideLocal()
{
  $(type -P tide) \
  -b "$date 06:00" \
  -e "$date 21:05" \
  -l "Orwood, Old River, San Joaquin River, California" \
  -m m \
  -s "00:05" \
  -u ft \
  -tf "%H:%M (%Z)" \
  2>/dev/null
}
 
GetTideRemote()
{
  curl -s "http://tbone.biol.sc.edu/tide/tideshow.cgi?glen=1;type=mrare;interval=00%3A05;units=ft;year=$Y;month=$M;day=$D;hour=06;min=00;d_hour=21;d_min=00;tzone=local;site=Orwood%2C%20Old%20River%2C%20San%20Joaquin%20River%2C%20California" |
  grep "^$Y-" |
  sed -r '/(0[0-5]:)/d;/(2[1-3]:)/d;'
}
 
AddHighTideMarkers()
{
 awk '$NF > 2.6 {gsub("$"," <")} {print};'
}
 
if [ -x "$(type -P tide)" ]; then
  echo "INFO: generating tide data locally"
  for n in {0..12}; do
   date=$(date -d "+$n day" "+%Y-%m-%d")
   day=$(date -d "$date" +"%a")
   if [ -f $dir/$date.$day ]; then
     echo "$dir/$date.$day exists, skipping"
   else
     echo "$dir/$date.$day missing, creating"
     GetTideLocal | AddHighTideMarkers > $dir/$date.$day
   fi
  done
else
  echo "INFO: getting tide data from a remote web service"
  for n in {0..12}; do
   date=$(date -d "+$n day" "+%Y-%m-%d")
   Y=$(date -d "+$n day" "+%Y")
   M=$(date -d "+$n day" "+%m")
   D=$(date -d "+$n day" "+%d")
   day=$(date -d "$date" +"%a")
   if [ -f $dir/$date.$day ]; then
     echo "$dir/$date.$day exists, skipping"
   else
     echo "$dir/$date.$day missing, creating"
     GetTideRemote | AddHighTideMarkers > $dir/$date.$day
   fi
  done
fi
 
# delete old files
find $dir -type f -mtime +0 -regex '.*/[0-9][0-9][0-9][0-9].*' | xargs -r rm -rf
 
chmod 664 $dir/*
 
# purge index page from varnish cache
v-purge /path/to/page/

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).