{"id":754,"date":"2011-02-18T19:42:58","date_gmt":"2011-02-19T00:42:58","guid":{"rendered":"http:\/\/lukemiller.org\/?p=754"},"modified":"2013-05-19T15:34:10","modified_gmt":"2013-05-19T19:34:10","slug":"accessing-noaa-tide-data-with-r","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2011\/02\/accessing-noaa-tide-data-with-r\/","title":{"rendered":"Accessing NOAA tide data with R"},"content":{"rendered":"<figure id=\"attachment_758\" aria-describedby=\"caption-attachment-758\" style=\"width: 562px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/02\/boston_tides.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-758\" title=\"boston_tides\" alt=\"\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/02\/boston_tides.png\" width=\"562\" height=\"289\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/02\/boston_tides.png 562w, https:\/\/lukemiller.org\/wp-content\/uploads\/2011\/02\/boston_tides-300x154.png 300w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><\/a><figcaption id=\"caption-attachment-758\" class=\"wp-caption-text\">Boston Harbor tides for December 2010, retrieved from NOAA CO-OPS server.<\/figcaption><\/figure>\n<p>The National Oceanic and Atmospheric Administration makes the data from their many tide monitoring stations around the continent available for download. One way to access these data is through NOAA&#8217;s Center for Operational Oceanographic Products and Services <a href=\"http:\/\/opendap.co-ops.nos.noaa.gov\/\" target=\"_blank\">(CO-OPS)<\/a> website, which provides several access methods including an OPeNDAP server. The OPeNDAP server allows you to construct a fairly simple URL query to submit in a web browser, and it will return the requested data as ascii text. The basics of making an OPeNDAP query are explained <a href=\"http:\/\/docs.opendap.org\/index.php\/UserGuideOPeNDAPMessages\" target=\"_blank\">here<\/a>, but you can probably just figure out how it works from the code below and by visiting the CO-OPS OPeNDAP server linked above. Some NOAA tide stations also record water temperature, air temperature, wind speed, wind direction, and conductivity, all of which could be accessed through the OPeNDAP server.<\/p>\n<p>I wrote the following R script to build up an OPeNDAP query, submit it to the CO-OPS server using the <a href=\"http:\/\/cran.r-project.org\/web\/packages\/RCurl\/index.html\" target=\"_blank\">RCurl <\/a>package, and then parse the resulting text into a data frame that can be saved to disk. The R script is available in my <a href=\"http:\/\/github.com\/millerlp\/Misc_R_scripts\/blob\/master\/NOAA_tide_ht_retrieval.R\" target=\"_blank\">GitHub repository<\/a>. Either copy that script to your clipboard (there&#8217;s a button to do so) and save it on your computer, or navigate to the main repository page (<a href=\"http:\/\/github.com\/millerlp\/Misc_R_scripts\" target=\"_blank\">Misc_R_scripts<\/a>) and download everything as a zip file. When you&#8217;re downloading 6-minute verified tide height data, the server will only let you retrieve 1 month&#8217;s worth of data at a time. You can only access 6-minute verified data that is older than 2 months.<\/p>\n<p>To find your local tide station, try the <a href=\"http:\/\/opendap.co-ops.nos.noaa.gov\/stations\/index.jsp\" target=\"_blank\">http:\/\/opendap.co-ops.nos.noaa.gov\/stations\/index.jsp<\/a> site or the main NOAA <a href=\"http:\/\/tidesandcurrents.noaa.gov\/\" target=\"_blank\">Tides and Currents webpage<\/a>.<\/p>\n<p>Edit July 2011: I have also put together a stripped-down version of the above script that will automatically step through a set of specified years and download the tide data for each month into separate files. That script is available <a href=\"https:\/\/github.com\/millerlp\/Misc_R_scripts\/blob\/master\/coops_tide_ht_retrieval_yearly.R\" target=\"_blank\">on GitHub as well<\/a>. If you want to get several years&#8217; worth of data all at once for a single site, use that script.<\/p>\n<p>The script:<\/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;\"># coops_tide_ht_retrieval.R<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Author: Luke Miller Feb 2011<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">################################################################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Run this script as-is. It will prompt you to enter the appropriate<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># station and date values when needed. You can only request less than 1 month of <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># data at a time, so if you get an error returned, try asking for slightly less<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># data.<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># This script will download a set of verified tide height data from a NOAA <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># CO-OPS DODS\/OPeNDAP server and parse it into a data frame to be saved to disk <\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Station list: http:\/\/opendap.co-ops.nos.noaa.gov\/stations\/index.jsp<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Scroll through that list to find your station ID. <\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\"># OPeNDAP server gateway: http:\/\/opendap.co-ops.nos.noaa.gov\/dods\/<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># The gateway has links to several other data types available including <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># water temperature, air temperature, wind etc. <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># See http:\/\/docs.opendap.org\/index.php\/UserGuideOPeNDAPMessages for info on<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># structuring OPeNDAP queries.<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Six-minute water level data can only be retrieved 1 month at a time. Other<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># fields such as water temperature could return up to 1 year at a time. This <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># script only deals with 6-minute tide height data, but could serve as a guide <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># for accessing other data types from NOAA CO-OPS. <\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># The tide height is reported in meters and time in UTC (Greenwich).<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">###############################################################################<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/require\"><span style=\"color: #003399; font-weight: bold;\">require<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/packages\/cran\/RCurl\"><span>RCurl<\/span><\/a><span style=\"color: #009900;\">)<\/span>\r\n\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/noquote\"><span style=\"color: #003399; font-weight: bold;\">noquote<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/print\"><span style=\"color: #003399; font-weight: bold;\">print<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"Enter NOAA tide station number (i.e. Monterey = 9413450): \"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\nstation = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/scan\"><span style=\"color: #003399; font-weight: bold;\">scan<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #339933;\">,<\/span>what = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/character\"><span style=\"color: #003399; font-weight: bold;\">character<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span>nlines = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">)<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/noquote\"><span style=\"color: #003399; font-weight: bold;\">noquote<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/print\"><span style=\"color: #003399; font-weight: bold;\">print<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"Enter start date (format: 20080123 = Jan 23, 2008): \"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\nstartdate = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/scan\"><span style=\"color: #003399; font-weight: bold;\">scan<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #339933;\">,<\/span>what = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/character\"><span style=\"color: #003399; font-weight: bold;\">character<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span>nlines = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#get one line of values<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/noquote\"><span style=\"color: #003399; font-weight: bold;\">noquote<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/print\"><span style=\"color: #003399; font-weight: bold;\">print<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"Enter end date (format: 20081231 = Dec 31, 2008): \"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\nenddate = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/scan\"><span style=\"color: #003399; font-weight: bold;\">scan<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #339933;\">,<\/span>what = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/character\"><span style=\"color: #003399; font-weight: bold;\">character<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span>nlines = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">#OPeNDAP query for 6-minute verified water level looks like this (on 1 line):<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#http:\/\/opendap.co-ops.nos.noaa.gov\/dods\/IOOS\/<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#SixMin_Verified_Water_Level.ascii?<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#WATERLEVEL_6MIN_VFD_PX._STATION_ID,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#WATERLEVEL_6MIN_VFD_PX._DATUM,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#WATERLEVEL_6MIN_VFD_PX.DATE_TIME,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#WATERLEVEL_6MIN_VFD_PX.WL_VALUE,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#WATERLEVEL_6MIN_VFD_PX.I,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#WATERLEVEL_6MIN_VFD_PX.F,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#WATERLEVEL_6MIN_VFD_PX.R,<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#WATERLEVEL_6MIN_VFD_PX.T<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#&amp;WATERLEVEL_6MIN_VFD_PX._STATION_ID=%229449880%22<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#&amp;WATERLEVEL_6MIN_VFD_PX._DATUM=%22MLLW%22<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#&amp;WATERLEVEL_6MIN_VFD_PX._BEGIN_DATE=%2220080801%22<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#&amp;WATERLEVEL_6MIN_VFD_PX._END_DATE=%2220080808%22<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">#####################################################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">######## DON'T CHANGE ANYTHING BELOW THIS LINE ####################<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#The parts of the url to be assembled:<\/span>\r\nurl1 = <span style=\"color: #0000ff;\">\"http:\/\/opendap.co-ops.nos.noaa.gov\/dods\/IOOS\/\"<\/span>\r\nurl2 = <span style=\"color: #0000ff;\">\"SixMin_Verified_Water_Level.ascii?\"<\/span>\r\nurl3 = <span style=\"color: #0000ff;\">\"WATERLEVEL_6MIN_VFD_PX._STATION_ID,\"<\/span> <span style=\"color: #666666; font-style: italic;\">#return stationId<\/span>\r\nurl4 = <span style=\"color: #0000ff;\">\"WATERLEVEL_6MIN_VFD_PX._DATUM,\"<\/span> <span style=\"color: #666666; font-style: italic;\">#return datum<\/span>\r\nurl5 = <span style=\"color: #0000ff;\">\"WATERLEVEL_6MIN_VFD_PX.DATE_TIME,\"<\/span> <span style=\"color: #666666; font-style: italic;\">#return record date-time<\/span>\r\nurl6 = <span style=\"color: #0000ff;\">\"WATERLEVEL_6MIN_VFD_PX.WL_VALUE,\"<\/span> <span style=\"color: #666666; font-style: italic;\">#return water level value<\/span>\r\nurl7 = <span style=\"color: #0000ff;\">\"WATERLEVEL_6MIN_VFD_PX.I,\"<\/span> <span style=\"color: #666666; font-style: italic;\">#return quality flag<\/span>\r\nurl8 = <span style=\"color: #0000ff;\">\"WATERLEVEL_6MIN_VFD_PX.F,\"<\/span> <span style=\"color: #666666; font-style: italic;\">#return quality flag<\/span>\r\nurl9 = <span style=\"color: #0000ff;\">\"WATERLEVEL_6MIN_VFD_PX.R,\"<\/span> <span style=\"color: #666666; font-style: italic;\">#return quality flag<\/span>\r\nurl10 = <span style=\"color: #0000ff;\">\"WATERLEVEL_6MIN_VFD_PX.T\"<\/span> <span style=\"color: #666666; font-style: italic;\">#return quality flag<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#The remaining parts of the url specify how to filter the data on the server <\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#to only retrieve the desired station and date range. Values must be enclosed<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#in ascii double-quotes, which are represented by the code %22<\/span>\r\n<span style=\"color: #666666; font-style: italic;\"># Do not change any values here, do not insert your own values here.<\/span>\r\nurl11 = <span style=\"color: #0000ff;\">\"&amp;WATERLEVEL_6MIN_VFD_PX._STATION_ID=%22\"<\/span> <span style=\"color: #666666; font-style: italic;\"># station ID goes here<\/span>\r\nurl12 = <span style=\"color: #0000ff;\">\"%22\"<\/span>\r\nurl13 = <span style=\"color: #0000ff;\">\"&amp;WATERLEVEL_6MIN_VFD_PX._DATUM=%22MLLW%22\"<\/span><span style=\"color: #666666; font-style: italic;\">#we want MLLW as the datum<\/span>\r\nurl14 = <span style=\"color: #0000ff;\">\"&amp;WATERLEVEL_6MIN_VFD_PX._BEGIN_DATE=%22\"<\/span> <span style=\"color: #666666; font-style: italic;\"># start date gets put in here<\/span>\r\nurl15 = <span style=\"color: #0000ff;\">\"%22\"<\/span>\r\nurl16 = <span style=\"color: #0000ff;\">\"&amp;WATERLEVEL_6MIN_VFD_PX._END_DATE=%22\"<\/span> <span style=\"color: #666666; font-style: italic;\"># end date gets put in here<\/span>\r\nurl17 = <span style=\"color: #0000ff;\">\"%22\"<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">####### DON'T CHANGE ANYTHING ABOVE THIS LINE ############<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">##############################################<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">#Assemble the URL<\/span>\r\nurltotal = <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>url1<span style=\"color: #339933;\">,<\/span>url2<span style=\"color: #339933;\">,<\/span>url3<span style=\"color: #339933;\">,<\/span>url4<span style=\"color: #339933;\">,<\/span>url5<span style=\"color: #339933;\">,<\/span>url6<span style=\"color: #339933;\">,<\/span>url7<span style=\"color: #339933;\">,<\/span>url8<span style=\"color: #339933;\">,<\/span>url9<span style=\"color: #339933;\">,<\/span>url10<span style=\"color: #339933;\">,<\/span>url11<span style=\"color: #339933;\">,<\/span>\r\n\t\tstation<span style=\"color: #339933;\">,<\/span>url12<span style=\"color: #339933;\">,<\/span>url13<span style=\"color: #339933;\">,<\/span>url14<span style=\"color: #339933;\">,<\/span>startdate<span style=\"color: #339933;\">,<\/span>url15<span style=\"color: #339933;\">,<\/span>url16<span style=\"color: #339933;\">,<\/span>enddate<span style=\"color: #339933;\">,<\/span>url17<span style=\"color: #339933;\">,<\/span>sep =<span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n<span style=\"color: #666666; font-style: italic;\">#Download the data<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"Contacting server...<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>\"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/utils\/flush.console\"><span style=\"color: #003399; font-weight: bold;\">flush.console<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span>\r\ndat = getURL<span style=\"color: #009900;\">(<\/span>urltotal<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#use RCurl to retrieve text into a vector 'dat'<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"Data returned...<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>\"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/utils\/flush.console\"><span style=\"color: #003399; font-weight: bold;\">flush.console<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/Sys.sleep\"><span style=\"color: #003399; font-weight: bold;\">Sys.sleep<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#If you access data in a loop, be courteous and give the server<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#a short break between requests<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#cleanup<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/rm\"><span style=\"color: #003399; font-weight: bold;\">rm<\/span><\/a><span style=\"color: #009900;\">(<\/span>url1<span style=\"color: #339933;\">,<\/span>url2<span style=\"color: #339933;\">,<\/span>url3<span style=\"color: #339933;\">,<\/span>url4<span style=\"color: #339933;\">,<\/span>url5<span style=\"color: #339933;\">,<\/span>url6<span style=\"color: #339933;\">,<\/span>url7<span style=\"color: #339933;\">,<\/span>url8<span style=\"color: #339933;\">,<\/span>url9<span style=\"color: #339933;\">,<\/span>url10<span style=\"color: #339933;\">,<\/span>url11<span style=\"color: #339933;\">,<\/span>url12<span style=\"color: #339933;\">,<\/span>url13<span style=\"color: #339933;\">,<\/span>url14<span style=\"color: #009900;\">)<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/rm\"><span style=\"color: #003399; font-weight: bold;\">rm<\/span><\/a><span style=\"color: #009900;\">(<\/span>url15<span style=\"color: #339933;\">,<\/span>url16<span style=\"color: #339933;\">,<\/span>url17<span style=\"color: #009900;\">)<\/span>\r\n\r\ncon = <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>dat<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#create text Connection to dat vector<\/span>\r\nall.lines = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/readLines\"><span style=\"color: #003399; font-weight: bold;\">readLines<\/span><\/a><span style=\"color: #009900;\">(<\/span>con<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#read lines of text into separate slots in a vector<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/close\"><span style=\"color: #003399; font-weight: bold;\">close<\/span><\/a><span style=\"color: #009900;\">(<\/span>con<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#close connection to dat vector<\/span>\r\n\r\n<span style=\"color: #000000; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/length\"><span style=\"color: #003399; font-weight: bold;\">length<\/span><\/a><span style=\"color: #009900;\">(<\/span><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;\">'^Error'<\/span><span style=\"color: #339933;\">,<\/span>all.lines<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span><span>&gt;<\/span><span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\">#check for error in retrieval<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"There was an error...<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span>dat<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>\"<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#print contents of dat to show error<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/flush.console\"><span style=\"color: #003399; font-weight: bold;\">flush.console<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span>\r\n<span style=\"color: #009900;\">}<\/span> <span style=\"color: #000000; font-weight: bold;\">else<\/span> <span style=\"color: #009900;\">{<\/span> <span style=\"color: #666666; font-style: italic;\">#retrieval was successful, parse the text<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">#The column headers are typically preceded by a line of dashes<\/span>\r\n\theaderlines = <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;\">\"^--------\"<\/span><span style=\"color: #339933;\">,<\/span>all.lines<span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#find index of headers (-1)<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">#read column header names into a vector<\/span>\r\n\tcon = <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>dat<span style=\"color: #009900;\">)<\/span>\r\n\theaders = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/scan\"><span style=\"color: #003399; font-weight: bold;\">scan<\/span><\/a><span style=\"color: #009900;\">(<\/span>con<span style=\"color: #339933;\">,<\/span> skip = headerlines<span style=\"color: #339933;\">,<\/span> nlines = <span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">\",\"<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\t\twhat = <span style=\"color: #0000ff;\">\"character\"<\/span><span style=\"color: #339933;\">,<\/span> strip.white = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/close\"><span style=\"color: #003399; font-weight: bold;\">close<\/span><\/a><span style=\"color: #009900;\">(<\/span>con<span style=\"color: #009900;\">)<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">#read rest of the data into a data frame 'df'<\/span>\r\n\tcon = <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>dat<span style=\"color: #009900;\">)<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/stats\/df\"><span style=\"color: #003399; font-weight: bold;\">df<\/span><\/a> = <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>con<span style=\"color: #339933;\">,<\/span> skip = headerlines<span>+<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span> sep = <span style=\"color: #0000ff;\">\",\"<\/span><span style=\"color: #339933;\">,<\/span> header = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\t\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/quote\"><span style=\"color: #003399; font-weight: bold;\">quote<\/span><\/a> = <span style=\"color: #0000ff;\">\"<span style=\"color: #000099; font-weight: bold;\">\\\"<\/span>\"<\/span><span style=\"color: #339933;\">,<\/span> col.names = headers<span style=\"color: #339933;\">,<\/span> strip.white = <span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\t\tstringsAsFactors = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/close\"><span style=\"color: #003399; font-weight: bold;\">close<\/span><\/a><span style=\"color: #009900;\">(<\/span>con<span style=\"color: #009900;\">)<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">###########################################################################<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\">#The following operations will need to be altered if you change the <\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\">#fields or data type being returned by the OPeNDAP server<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">#Convert the time column to POSIX time (seconds since 1970-01-01 00:00:00)<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/stats\/df\"><span style=\"color: #003399; font-weight: bold;\">df<\/span><\/a><span style=\"color: #009900;\">[<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #009900;\">]<\/span> = <a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.POSIXct\"><span style=\"color: #003399; font-weight: bold;\">as.POSIXct<\/span><\/a><span style=\"color: #009900;\">(<\/span>strptime<span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/stats\/df\"><span style=\"color: #003399; font-weight: bold;\">df<\/span><\/a><span style=\"color: #009900;\">[<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">3<\/span><span style=\"color: #009900;\">]<\/span><span style=\"color: #339933;\">,<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/format\"><span style=\"color: #003399; font-weight: bold;\">format<\/span><\/a> = <span style=\"color: #0000ff;\">\"%b %d %Y %I:%M%p\"<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\t\t\t\ttz = <span style=\"color: #0000ff;\">\"GMT\"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">#Give the columns shorter names<\/span>\r\n\t<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><a href=\"http:\/\/inside-r.org\/r-doc\/stats\/df\"><span style=\"color: #003399; font-weight: bold;\">df<\/span><\/a><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;\">\"stationId\"<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"datum\"<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"TimeUTC\"<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"TideHT\"<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"Flag.Inferred\"<\/span><span style=\"color: #339933;\">,<\/span>\r\n\t\t\t<span style=\"color: #0000ff;\">\"Flag.Flat.Tol\"<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"Flag.Rate.Tol\"<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"Flag.Temp.Tol\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">#Uncomment this if you want to plot the data<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#\tplot(df$TimeUTC, df$TideHT, type = \"l\",<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#\t\t\txlab = \"Date\",ylab = \"Tide Height, meters\")<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">#Save data automatically to a .csv file. <\/span>\r\n\tfilename = <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;\">\"Station_\"<\/span><span style=\"color: #339933;\">,<\/span>station<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"_\"<\/span><span style=\"color: #339933;\">,<\/span>startdate<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"-\"<\/span><span style=\"color: #339933;\">,<\/span>enddate<span style=\"color: #339933;\">,<\/span>\r\n\t\t\t<span style=\"color: #0000ff;\">\".csv\"<\/span><span style=\"color: #339933;\">,<\/span>sep = <span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#make file name<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/write.csv\"><span style=\"color: #003399; font-weight: bold;\">write.csv<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/stats\/df\"><span style=\"color: #003399; font-weight: bold;\">df<\/span><\/a><span style=\"color: #339933;\">,<\/span>filename<span style=\"color: #339933;\">,<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/row.names\"><span style=\"color: #003399; font-weight: bold;\">row.names<\/span><\/a> = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span><span style=\"color: #339933;\">,<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/quote\"><span style=\"color: #003399; font-weight: bold;\">quote<\/span><\/a> = <span style=\"color: #000000; font-weight: bold;\">FALSE<\/span><span style=\"color: #009900;\">)<\/span> <span style=\"color: #666666; font-style: italic;\">#write file to disk<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/cat\"><span style=\"color: #003399; font-weight: bold;\">cat<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"Saved to \"<\/span><span style=\"color: #339933;\">,<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/getwd\"><span style=\"color: #003399; font-weight: bold;\">getwd<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"\/\"<\/span><span style=\"color: #339933;\">,<\/span>filename<span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"<span style=\"color: #000099; font-weight: bold;\">\\n<\/span>\"<\/span><span style=\"color: #339933;\">,<\/span>sep = <span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/utils\/flush.console\"><span style=\"color: #003399; font-weight: bold;\">flush.console<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">#Alternate file save method lets user specify file name at run time<\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\">#Uncomment this if you wish to use it instead of the automated file <\/span>\r\n\t<span style=\"color: #666666; font-style: italic;\">#output above<\/span>\r\n<span style=\"color: #666666; font-style: italic;\">#\twrite.csv(df,file.choose(),row.names = FALSE, quote = FALSE)<\/span>\r\n\r\n\t<span style=\"color: #666666; font-style: italic;\">#cleanup<\/span>\r\n\t<a href=\"http:\/\/inside-r.org\/r-doc\/base\/rm\"><span style=\"color: #003399; font-weight: bold;\">rm<\/span><\/a><span style=\"color: #009900;\">(<\/span>dat<span style=\"color: #339933;\">,<\/span>con<span style=\"color: #339933;\">,<\/span>all.lines<span style=\"color: #339933;\">,<\/span>startdate<span style=\"color: #339933;\">,<\/span>enddate<span style=\"color: #339933;\">,<\/span>filename<span style=\"color: #339933;\">,<\/span>headerlines<span style=\"color: #339933;\">,<\/span> headers<span style=\"color: #009900;\">)<\/span>\r\n\r\n<span style=\"color: #009900;\">}<\/span> <span style=\"color: #666666; font-style: italic;\">#end of if-else statement<\/span><\/pre>\n<\/div>\n<\/div>\n<p><a title=\"Created by Pretty R at inside-R.org\" href=\"http:\/\/www.inside-r.org\/pretty-r\">Created by Pretty R at inside-R.org<\/a><\/p>\n<p>I stumbled across the Pretty R site at some point on <a href=\"http:\/\/www.r-bloggers.com\" target=\"_blank\">http:\/\/www.r-bloggers.com<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s some R code that can be used to download archived tide height data from NOAA&#8217;s CO-OPS OPeNDAP server. The code makes use of RCurl to send a URL query to the server, and then splits apart the resulting data into a data frame. <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[218],"tags":[65,64,63,7,58,62,34],"class_list":["post-754","post","type-post","status-publish","format-standard","hentry","category-r-project","tag-historical-data","tag-noaa","tag-opendap","tag-r","tag-r-project","tag-tide-height","tag-water-temperature"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/754","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=754"}],"version-history":[{"count":28,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/754\/revisions"}],"predecessor-version":[{"id":2606,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/754\/revisions\/2606"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}