{"id":1536,"date":"2013-05-17T23:02:18","date_gmt":"2013-05-18T03:02:18","guid":{"rendered":"http:\/\/lukemiller.org\/?p=1536"},"modified":"2016-09-06T17:32:12","modified_gmt":"2016-09-07T00:32:12","slug":"interfacing-xtide-and-r","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2013\/05\/interfacing-xtide-and-r\/","title":{"rendered":"Interfacing XTide and R"},"content":{"rendered":"<p><strong>Edit: <\/strong>There is now a full-fledged R package, <a href=\"https:\/\/CRAN.R-project.org\/package=rtide\" target=\"_blank\">rtide<\/a>, to accomplish the same basic task of generating time series of tide predictions that is outlined here. See this <a href=\"https:\/\/lukemiller.org\/?p=2300\">more recent post<\/a> for information.<\/p>\n<p><a href=\"http:\/\/www.flaterco.com\/xtide\/xtide.html\" target=\"_blank\">XTide<\/a> is an open-source program that predicts tide heights and current speeds for hundreds of tide and current stations around the United States. It can be used to produce tide predictions in the past and future for a site at your chosen interval (down to the minute), as well as producing sunrise and sunset times, moon phases, and times when the tide level passes a chosen level mark. It has a user-friendly GUI version (xtide.exe), but also comes in a command line version (tide.exe) that can be interfaced with other programs like R.<\/p>\n<p>Getting R and XTide to talk to each other is a matter of producing a command line query to pass from R to XTide, and then using R to parse the results into a handy format for further manipulation. I have put together an example script that walks through the process, and shows how to parse the results into a data frame with time stamps. The script, <a href=\"https:\/\/github.com\/millerlp\/Misc_R_scripts\/blob\/master\/xtide_interface.R\">xtide_interface.R<\/a>, is available in this <a href=\"https:\/\/github.com\/millerlp\/Misc_R_scripts\">GitHub repository<\/a>.<\/p>\n<div style=\"overflow: auto;\">\n<div class=\"geshifilter\">\n<pre class=\"r geshifilter-R\" style=\"font-family: monospace;\"><span style=\"color: #666666; font-style: italic;\"># xtide_interface.R<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># An example R script to show how to build XTide queries, submit them to the <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># system command line, and then parse the results into a data frame. There are<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># several different examples outlined below. <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Author: Luke Miller May 17, 2013<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">###############################################################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Site names: http:\/\/www.flaterco.com\/xtide\/locations.html <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># List of command line flags: http:\/\/www.flaterco.com\/xtide\/settings.html<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># Obviously XTide, or a port like tide.exe (for Windows) must be downloaded from <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># the XTide site and installed. It is a standalone program, separate from R.<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># In Windows, the XTide directory that contains the tide.exe program needs to be<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># added to the system search PATH. It's easiest if you also put the <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># harmonics.tcd file in the same directory as tide.exe.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">###############################################################################<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># Sys.setenv(TZ=\"UTC\") \t# Useful if you're getting data for sites in multiple <\/span>\r\n\t\t\t\t\t\t<span style=\"color: #666666; font-style: italic;\"># time zones and need to standardize on one time zone.<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># Start and end times, supplied as character strings<\/span>\r\nstartchar = <span style=\"color: #0000ff;\">'2013-05-16 16:00'<\/span>\r\nendchar = <span style=\"color: #0000ff;\">'2013-05-20 16:00'<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Site name, taken from http:\/\/www.flaterco.com\/xtide\/locations.html<\/span>\r\nsitename = <span style=\"color: #0000ff;\">'Bodega Harbor entrance, California'<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># Example switches for Xtide command line program<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># -l\tGive the site name after this switch<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># -b\tBeginning time, in the format YYYY-MM-DD HH:MM<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># -e \tEnding time, in the format YYYY-MM-DD HH:MM<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># -f \toutput format, c = csv<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># -u \tunits, i.e. m or ft<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># -em\tevent mask, suppress output of sunrise, sunset, moon phase etc<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># -z \tUTC (zulu) time zone<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># -ml\tmark level, only returns info in plain mode when tide crosses this level<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># -m\toutput information: r = raw, m = medium rare, p = plain<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#\t\traw mode returns the site, unix time code, and height<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#\t\tmedium rare mode returns site, date, time, and height<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#\t\tplain mode returns site, date, time, height, and event<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># \t\t\ti.e. High Tide, Low Tide, mark level crossing, sunrise, sunset etc<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">################################################################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Example 1<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Create query to return high and low tide times only, with units in feet and<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># using the current system time zone. <\/span>\r\ntidecommand = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'tide -l \"'<\/span><span style=\"color: #339933;\">,<\/span>sitename<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'\" -b \"'<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\tstartchar<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'\" -e \"'<\/span><span style=\"color: #339933;\">,<\/span> endchar<span style=\"color: #339933;\">,<\/span>\r\n\t\t<span style=\"color: #0000ff;\">'\" -f c -em pSsMm -m p -u ft '<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nss = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/system\"><span style=\"color: #003399; font-weight: bold;\">system<\/span><\/a><span style=\"color: #009900;\">(<\/span>tidecommand<span style=\"color: #339933;\">,<\/span> intern = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#invoke tide.exe and return results<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># Convert the character strings in 'ss' into a data frame<\/span>\r\nhilow = <a href=\"http:\/\/inside-r.org\/r-doc\/utils\/read.table\"><span style=\"color: #003399; font-weight: bold;\">read.table<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/textConnection\"><span style=\"color: #003399; font-weight: bold;\">textConnection<\/span><\/a><span style=\"color: #009900;\">(<\/span>ss<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">','<\/span><span style=\"color: #339933;\">,<\/span> colClasses = <span style=\"color: #0000ff;\">'character'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Add column names to the data frame<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/names\"><span style=\"color: #003399; font-weight: bold;\">names<\/span><\/a><span style=\"color: #009900;\">(<\/span>hilow<span style=\"color: #009900;\">)<\/span> = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Site'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Date'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Hour'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'TideHt'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Event'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Combine the Date &amp; Hour columns into a POSIX time stamp<\/span>\r\nhilow$Time = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXlt\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXlt<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span>hilow$Date<span style=\"color: #339933;\">,<\/span>hilow$Hour<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> \r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">\"%Y-%m-%d %I:%M %p\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Strip off the height units and convert tide height to numeric values<\/span>\r\nhilow$TideHt = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.numeric\"><span style=\"color: #003399; font-weight: bold;\">as.numeric<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/gsub\"><span style=\"color: #003399; font-weight: bold;\">gsub<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'[ [:alpha:]]'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">' '<\/span><span style=\"color: #339933;\">,<\/span>hilow$TideHt<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/head\"><span style=\"color: #003399; font-weight: bold;\">head<\/span><\/a><span style=\"color: #009900;\">(<\/span>hilow<span style=\"color: #009900;\">)<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">################################################################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Example 2<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Create query to return high and low tide times, along with any time when <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># the tide crosses the +2.0ft level. (High\/low tides are always included).<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Time values will be in the system time zone. Be careful of daylight savings<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># transitions.<\/span>\r\ntidecommand = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'tide -l \"'<\/span><span style=\"color: #339933;\">,<\/span>sitename<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'\" -b \"'<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\tstartchar<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'\" -e \"'<\/span><span style=\"color: #339933;\">,<\/span> endchar<span style=\"color: #339933;\">,<\/span>\r\n\t\t<span style=\"color: #0000ff;\">'\" -f c -em pSsMm -m p -ml 2.0ft -u ft '<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nss = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/system\"><span style=\"color: #003399; font-weight: bold;\">system<\/span><\/a><span style=\"color: #009900;\">(<\/span>tidecommand<span style=\"color: #339933;\">,<\/span> intern = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#invoke tide.exe and return results<\/span>\r\nss2 = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/grep\"><span style=\"color: #003399; font-weight: bold;\">grep<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Mark Falling'<\/span><span style=\"color: #339933;\">,<\/span> ss<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#find lines with 'Mark Falling' <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Convert the character strings in 'ss' into a data frame<\/span>\r\nevents = <a href=\"http:\/\/inside-r.org\/r-doc\/utils\/read.table\"><span style=\"color: #003399; font-weight: bold;\">read.table<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/textConnection\"><span style=\"color: #003399; font-weight: bold;\">textConnection<\/span><\/a><span style=\"color: #009900;\">(<\/span>ss<span style=\"color: #009900;\">[<\/span>ss2<span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">','<\/span><span style=\"color: #339933;\">,<\/span> \r\n\t\tcolClasses = <span style=\"color: #0000ff;\">'character'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Add column names to the data frame<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/names\"><span style=\"color: #003399; font-weight: bold;\">names<\/span><\/a><span style=\"color: #009900;\">(<\/span>events<span style=\"color: #009900;\">)<\/span> = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Site'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Date'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Hour'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'TideHt'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Event'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Combine the Date &amp; Hour columns into a POSIX time stamp<\/span>\r\nevents$Time = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXlt\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXlt<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span>events$Date<span style=\"color: #339933;\">,<\/span>events$Hour<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> \r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">\"%Y-%m-%d %I:%M %p\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Strip off the height units and convert tide height to numeric values<\/span>\r\nevents$TideHt = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.numeric\"><span style=\"color: #003399; font-weight: bold;\">as.numeric<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/gsub\"><span style=\"color: #003399; font-weight: bold;\">gsub<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'[ [:alpha:]]'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">' '<\/span><span style=\"color: #339933;\">,<\/span>events$TideHt<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/head\"><span style=\"color: #003399; font-weight: bold;\">head<\/span><\/a><span style=\"color: #009900;\">(<\/span>events<span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Calculate time between each event.<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/diff\"><span style=\"color: #003399; font-weight: bold;\">diff<\/span><\/a><span style=\"color: #009900;\">(<\/span>events$Time<span style=\"color: #009900;\">)<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">################################################################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Example 3<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Create query for returning times of high, low tide, plus Sunrise and Sunset,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># all in UTC timezone, for your site. Height units are meters.<\/span>\r\ntidecommand = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'tide -l \"'<\/span><span style=\"color: #339933;\">,<\/span>sitename<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'\" -b \"'<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\tstartchar<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'\" -e \"'<\/span><span style=\"color: #339933;\">,<\/span> endchar<span style=\"color: #339933;\">,<\/span> \r\n\t\t<span style=\"color: #0000ff;\">'\" -f c -em pMm -m p -u m -z'<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nss = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/system\"><span style=\"color: #003399; font-weight: bold;\">system<\/span><\/a><span style=\"color: #009900;\">(<\/span>tidecommand<span style=\"color: #339933;\">,<\/span> intern = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#invoke Xtide and return results<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># Convert the character strings in 'ss' into a data frame<\/span>\r\nhilowSun = <a href=\"http:\/\/inside-r.org\/r-doc\/utils\/read.table\"><span style=\"color: #003399; font-weight: bold;\">read.table<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/textConnection\"><span style=\"color: #003399; font-weight: bold;\">textConnection<\/span><\/a><span style=\"color: #009900;\">(<\/span>ss<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">','<\/span><span style=\"color: #339933;\">,<\/span> colClasses = <span style=\"color: #0000ff;\">'character'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Add column names to the data frame<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/names\"><span style=\"color: #003399; font-weight: bold;\">names<\/span><\/a><span style=\"color: #009900;\">(<\/span>hilowSun<span style=\"color: #009900;\">)<\/span> = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Site'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Date'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Hour'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'TideHt'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Event'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Combine the Date &amp; Hour columns into a POSIX time stamp<\/span>\r\nhilowSun$Time = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXlt\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXlt<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span>hilowSun$Date<span style=\"color: #339933;\">,<\/span>hilowSun$Hour<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> \r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">\"%Y-%m-%d %I:%M %p\"<\/span><span style=\"color: #339933;\">,<\/span> tz = <span style=\"color: #0000ff;\">\"UTC\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Strip off the height units and convert tide height to numeric values<\/span>\r\nhilowSun$TideHt = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.numeric\"><span style=\"color: #003399; font-weight: bold;\">as.numeric<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/gsub\"><span style=\"color: #003399; font-weight: bold;\">gsub<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'[ [:alpha:]]'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">' '<\/span><span style=\"color: #339933;\">,<\/span>hilowSun$TideHt<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># Convert Time values to current R session time zone, overwriting old timestamps<\/span>\r\nhilowSun$LocalTime = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span>hilowSun$Time<span style=\"color: #009900;\">)<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/head\"><span style=\"color: #003399; font-weight: bold;\">head<\/span><\/a><span style=\"color: #009900;\">(<\/span>hilowSun<span style=\"color: #009900;\">)<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">################################################################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Example 4<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Create query to return tide height at fixed time intervals (10 minutes here),<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># in UTC time zone, units of meters.<\/span>\r\ntidecommand = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'tide -l \"'<\/span><span style=\"color: #339933;\">,<\/span>sitename<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'\" -b \"'<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\tstartchar<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'\" -e \"'<\/span><span style=\"color: #339933;\">,<\/span> endchar<span style=\"color: #339933;\">,<\/span>\r\n\t\t<span style=\"color: #0000ff;\">'\" -f c -m m -s 00:10 -u m -z'<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nss = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/system\"><span style=\"color: #003399; font-weight: bold;\">system<\/span><\/a><span style=\"color: #009900;\">(<\/span>tidecommand<span style=\"color: #339933;\">,<\/span> intern = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#invoke tide.exe and return results<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Convert the character strings in 'ss' into a data frame<\/span>\r\ntides = <a href=\"http:\/\/inside-r.org\/r-doc\/utils\/read.table\"><span style=\"color: #003399; font-weight: bold;\">read.table<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/textConnection\"><span style=\"color: #003399; font-weight: bold;\">textConnection<\/span><\/a><span style=\"color: #009900;\">(<\/span>ss<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">','<\/span><span style=\"color: #339933;\">,<\/span> colClasses = <span style=\"color: #0000ff;\">'character'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Add column names to the data frame<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/names\"><span style=\"color: #003399; font-weight: bold;\">names<\/span><\/a><span style=\"color: #009900;\">(<\/span>tides<span style=\"color: #009900;\">)<\/span> = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Site'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Date'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Hour'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'TideHt'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Combine the Date &amp; Hour columns into a POSIX time stamp<\/span>\r\ntides$Time = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXlt\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXlt<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span>tides$Date<span style=\"color: #339933;\">,<\/span>tides$Hour<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> \r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">\"%Y-%m-%d %I:%M %p\"<\/span><span style=\"color: #339933;\">,<\/span> tz = <span style=\"color: #0000ff;\">\"UTC\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Strip off the height units and convert tide height to numeric values<\/span>\r\ntides$TideHt = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.numeric\"><span style=\"color: #003399; font-weight: bold;\">as.numeric<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/gsub\"><span style=\"color: #003399; font-weight: bold;\">gsub<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'[ [:alpha:]]'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">' '<\/span><span style=\"color: #339933;\">,<\/span>tides$TideHt<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Create a column of time stamps in the current R session time zone<\/span>\r\ntides$LocalTime = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span>tides$Time<span style=\"color: #009900;\">)<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/head\"><span style=\"color: #003399; font-weight: bold;\">head<\/span><\/a><span style=\"color: #009900;\">(<\/span>tides<span style=\"color: #009900;\">)<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">################################################################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Example 5<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Create query to return times of high\/low tide, sunrise and sunset, in the <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># local time zone. Extract the time for the Sunsets and place in a data frame<\/span>\r\ntidecommand = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'tide -l \"'<\/span><span style=\"color: #339933;\">,<\/span>sitename<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'\" -b \"'<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\tstartchar<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'\" -e \"'<\/span><span style=\"color: #339933;\">,<\/span> endchar<span style=\"color: #339933;\">,<\/span>\r\n\t\t<span style=\"color: #0000ff;\">'\" -f c -em pMm -m p -u ft '<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nss = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/system\"><span style=\"color: #003399; font-weight: bold;\">system<\/span><\/a><span style=\"color: #009900;\">(<\/span>tidecommand<span style=\"color: #339933;\">,<\/span> intern = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#invoke tide.exe and return results<\/span>\r\n\r\nss2 = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/grep\"><span style=\"color: #003399; font-weight: bold;\">grep<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Sunset'<\/span><span style=\"color: #339933;\">,<\/span>ss<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#find lines with Sunset<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Convert the character strings in 'ss' into a data frame<\/span>\r\nsunsets = <a href=\"http:\/\/inside-r.org\/r-doc\/utils\/read.table\"><span style=\"color: #003399; font-weight: bold;\">read.table<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/textConnection\"><span style=\"color: #003399; font-weight: bold;\">textConnection<\/span><\/a><span style=\"color: #009900;\">(<\/span>ss<span style=\"color: #009900;\">[<\/span>ss2<span style=\"color: #009900;\">]<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">','<\/span><span style=\"color: #339933;\">,<\/span> \r\n\t\tcolClasses = <span style=\"color: #0000ff;\">'character'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Add column names to the data frame<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/names\"><span style=\"color: #003399; font-weight: bold;\">names<\/span><\/a><span style=\"color: #009900;\">(<\/span>sunsets<span style=\"color: #009900;\">)<\/span> = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">'Site'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Date'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Hour'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'TideHt'<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">'Event'<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Combine the Date &amp; Hour columns into a POSIX time stamp<\/span>\r\nsunsets$Time = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXlt\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXlt<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span>sunsets$Date<span style=\"color: #339933;\">,<\/span>sunsets$Hour<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span> \r\n\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">\"%Y-%m-%d %I:%M %p\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/head\"><span style=\"color: #003399; font-weight: bold;\">head<\/span><\/a><span style=\"color: #009900;\">(<\/span>sunsets<span style=\"color: #009900;\">)<\/span><\/pre>\n<\/div>\n<\/div>\n<p>The output from the first example would look something like this (I chopped off the last column so it would fit here):<\/p>\n<pre>                                Site       Date         Hour TideHt     Event\r\n1 Bodega Harbor entrance| California 2013-05-16  5:30 PM PDT   4.23 High Tide\r\n2 Bodega Harbor entrance| California 2013-05-16 11:16 PM PDT   2.82  Low Tide\r\n3 Bodega Harbor entrance| California 2013-05-17  4:05 AM PDT   4.05 High Tide\r\n4 Bodega Harbor entrance| California 2013-05-17 11:06 AM PDT   0.56  Low Tide\r\n5 Bodega Harbor entrance| California 2013-05-17  6:14 PM PDT   4.41 High Tide\r\n6 Bodega Harbor entrance| California 2013-05-18 12:23 AM PDT   2.47  Low Tide<\/pre>\n<p>The output from Example 4 above resembles the following (I&#8217;ve cut out some of the columns again to save space):<\/p>\n<pre>        Date        Hour   TideHt           LocalTime\r\n1 2013-05-16 4:00 PM UTC 0.180493 2013-05-16 09:00:00\r\n2 2013-05-16 4:10 PM UTC 0.158821 2013-05-16 09:10:00\r\n3 2013-05-16 4:20 PM UTC 0.140198 2013-05-16 09:20:00\r\n4 2013-05-16 4:30 PM UTC 0.124632 2013-05-16 09:30:00\r\n5 2013-05-16 4:40 PM UTC 0.112148 2013-05-16 09:40:00\r\n6 2013-05-16 4:50 PM UTC 0.102741 2013-05-16 09:50:00<\/pre>\n<p>&nbsp;<\/p>\n<p>The interface between R and XTide is the easy part. The hard part is getting XTide properly installed on your computer.<\/p>\n<p>Oddly enough, this is one of those cases where the <a title=\"Windows version (download link)\" href=\"ftp:\/\/ftp.flaterco.com\/xtide\/xtide-2.12.1-win32.zip\">Windows version (download link) \u00a0<\/a>might actually be easier than the Mac install, since the Mac version only seems to exist as source code that <a href=\"http:\/\/www.flaterco.com\/xtide\/installation.html\" target=\"_blank\">must be built via the command line <\/a>(found on <a href=\"http:\/\/www.flaterco.com\/xtide\/files.html#contrib\" target=\"_blank\">this page<\/a>, someone point me to a pre-rolled Mac binary please). To build from source you need both the xtide source and libtcd source. In either case, on Windows, Mac, or Linux, you also need to download a <a href=\"ftp:\/\/ftp.flaterco.com\/xtide\/harmonics-dwf-20121224-free.tar.bz2\">harmonics file (download link) .<\/a><\/p>\n<p>On Windows,\u00a0 after installing the binary XTide file, it&#8217;s probably easiest to just put the unzipped harmonics .tcd file in the same directory where you installed XTide (where xtide.exe and tide.exe are found). You also need to edit the Windows environmental variable PATH to include the path to the folder where xtide.exe and tide.exe are installed (<a href=\"http:\/\/www.mathworks.com\/support\/solutions\/en\/data\/1-15ZLK\/\" target=\"_blank\">see here<\/a> for an example dealing with Matlab).<\/p>\n<p>On Mac\/Linux, it&#8217;s easiest to create a xtide.conf file in the \/etc directory found inside the system root directory. The screenshot below shows the process of getting to the \/etc directory and starting the nano editor to create the xtide.conf file.<\/p>\n<figure id=\"attachment_1537\" aria-describedby=\"caption-attachment-1537\" style=\"width: 451px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-17-at-7.20.06-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1537 \" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-17-at-7.20.06-PM.png\" alt=\"Screen Shot 2013-05-17 at 7.20.06 PM\" width=\"451\" height=\"262\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-17-at-7.20.06-PM.png 451w, https:\/\/lukemiller.org\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-17-at-7.20.06-PM-300x174.png 300w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/a><figcaption id=\"caption-attachment-1537\" class=\"wp-caption-text\">Navigating to the \/etc folder on the Mac.<\/figcaption><\/figure>\n<p>In nano, on the very first line, enter the path to your harmonics .tcd file. Then hit Control+O, Control+X to save and exit nano.<\/p>\n<figure id=\"attachment_1540\" aria-describedby=\"caption-attachment-1540\" style=\"width: 639px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-17-at-7.27.37-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1540\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-17-at-7.27.37-PM.png\" alt=\"Screen Shot 2013-05-17 at 7.27.37 PM\" width=\"639\" height=\"410\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-17-at-7.27.37-PM.png 639w, https:\/\/lukemiller.org\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-17-at-7.27.37-PM-300x192.png 300w\" sizes=\"auto, (max-width: 639px) 100vw, 639px\" \/><\/a><figcaption id=\"caption-attachment-1540\" class=\"wp-caption-text\">An example of the path to the harmonics .tcd file that goes on the first line of the xtide.conf file. Change your path to suit wherever your harmonics file ended up.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>If you got Xtide properly built from source, the xtide.conf file should get it working by helping XTide find the harmonics file. The installation pages for XTide reference the export HFILE_PAGE=&#8230;. prior to building the source files, but I didn&#8217;t have any success with that method. The xtide.conf method finally got things working for me on the Mac.<\/p>\n<p>When XTide is installed and running, you should be able to open up a system terminal and do something simple like<\/p>\n<pre>tide -l \"Bodega Harbor entrance, California\"<\/pre>\n<p>and get a string of tide predictions out. If that works in your system terminal, the R script above should work as well, since R is simply calling the tide program via the system shell.<\/p>\n<p>Finally, if you&#8217;re not interested in predictions for Bodega Harbor and instead want data for your local site, you need to find the XTide name for your site here: <a href=\"http:\/\/www.flaterco.com\/xtide\/locations.html\" target=\"_blank\">http:\/\/www.flaterco.com\/xtide\/locations.html. <\/a>There are tons of sites there, so use Ctrl+F or Command+F to use your browser&#8217;s find function to search for the name of your local tide station. If your station has some crazy long name on that page, you&#8217;ll need to use that same crazy long name (For example, &#8220;Dumbarton Highway Bridge, 0.28 nmi. SE of, San Francisco Bay, California Current (25d)&#8221;) for the <code>sitename<\/code> variable.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Edit: There is now a full-fledged R package, rtide, to accomplish the same basic task of generating time series of tide predictions that is outlined here. See this more recent post for information. XTide is an open-source program that predicts tide heights and current speeds for hundreds of tide and current stations around the United [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[218,240],"tags":[154,58,155,156,62,153],"class_list":["post-1536","post","type-post","status-publish","format-standard","hentry","category-r-project","category-tide-prediction","tag-current-speed","tag-r-project","tag-sunrise","tag-sunset","tag-tide-height","tag-xtide"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/comments?post=1536"}],"version-history":[{"count":14,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1536\/revisions"}],"predecessor-version":[{"id":2318,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1536\/revisions\/2318"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=1536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=1536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=1536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}