{"id":1893,"date":"2014-11-25T18:25:08","date_gmt":"2014-11-26T02:25:08","guid":{"rendered":"http:\/\/lukemiller.org\/?p=1893"},"modified":"2018-03-12T09:57:18","modified_gmt":"2018-03-12T16:57:18","slug":"extracting-noaa-sea-surface-temperatures-with-ncdf4","status":"publish","type":"post","link":"https:\/\/lukemiller.org\/index.php\/2014\/11\/extracting-noaa-sea-surface-temperatures-with-ncdf4\/","title":{"rendered":"Extracting NOAA sea surface temperatures with ncdf4"},"content":{"rendered":"<p>I&#8217;ve written previously about some <a href=\"https:\/\/lukemiller.org\/index.php\/2014\/01\/noaa-oisst-v2-high-resolution-daily-sea-surface-temperatures-with-r\/\">example R scripts<\/a> I created to extract sea surface temperature data from NOAA&#8217;s Optimum Interpolated Sea Surface Temperature products. If you want daily global sea surface temperatures on a 0.25&#215;0.25\u00b0 grid, they gather those into 1-year files available at <a href=\"http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.highres.html\" target=\"_blank\" rel=\"noopener\">http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.highres.html.<\/a> If you want weekly average SST values on a 1&#215;1\u00b0 grid, you can get those back to 1981 at <a href=\"http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.html\" target=\"_blank\" rel=\"noopener\">http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.html<\/a>. If you just want a smaller file for a single day on the 0.25&#215;0.25\u00b0 grid, you can find those at <a href=\"http:\/\/www.ncdc.noaa.gov\/sst\/griddata.php\" target=\"_blank\" rel=\"noopener\">http:\/\/www.ncdc.noaa.gov\/sst\/griddata.php<\/a>.<\/p>\n<figure id=\"attachment_1894\" aria-describedby=\"caption-attachment-1894\" style=\"width: 499px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/11\/africa_sst.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1894\" src=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/11\/africa_sst.png\" alt=\"Ocean temperatures around Africa for 2014-11-24.\" width=\"499\" height=\"448\" srcset=\"https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/11\/africa_sst.png 499w, https:\/\/lukemiller.org\/wp-content\/uploads\/2014\/11\/africa_sst-300x269.png 300w\" sizes=\"auto, (max-width: 499px) 100vw, 499px\" \/><\/a><figcaption id=\"caption-attachment-1894\" class=\"wp-caption-text\">Ocean temperatures around Africa for 2014-11-24.<\/figcaption><\/figure>\n<p>The high-resolution daily data currently appear to use a mixture of NetCDF3 formats for 2013 and earlier, while the 2014 data are NetCDF4. The older files can be opened by David Pierce&#8217;s <a href=\"http:\/\/cran.r-project.org\/web\/packages\/ncdf\/index.html\" target=\"_blank\" rel=\"noopener\">ncdf\u00a0 <\/a>package, which is easily installed from CRAN on all platforms. The newer NetCDF4 files cannot be opened by the ncdf package, but David Pierce&#8217;s newer <a href=\"http:\/\/cran.r-project.org\/web\/packages\/ncdf4\/index.html\" target=\"_blank\" rel=\"noopener\">ncdf4 package<\/a> will handle both older and newer file formats. That package should be available for OSX and Linux users directly from CRAN, but it is not available for Windows users directly. Instead, you can currently visit <a href=\"http:\/\/cirrus.ucsd.edu\/~pierce\/ncdf\/\" target=\"_blank\" rel=\"noopener\">David Pierce&#8217;s ncdf\/ncdf4 site<\/a> where there are Windows-compatible versions of the package available. To install that .zip file, simply save it to your computer. Open an instance of Rgui.exe. Go to the menu item &#8220;Packages&gt;Install package(s) from local zip file&#8221; and point R to your copy of the zip file. If that works, you should be able to call <code>library(ncdf4)<\/code> at the R command line and get access to all of the package&#8217;s functions. Fun! (<strong>Edit 2018: <\/strong><code><a href=\"https:\/\/cran.r-project.org\/web\/packages\/ncdf4\/index.html\">ncdf4<\/a><\/code> has been available on CRAN for some time now, so you should be able to just run <code>install.packages('ncdf4')<\/code> from the R command line now instead of downloading and installing the zip files)<\/p>\n<p>With that caveat out of the way, I&#8217;ve created a <a href=\"https:\/\/github.com\/millerlp\/Misc_R_scripts\/blob\/master\/NOAA_OISST_ncdf4.R\" target=\"_blank\" rel=\"noopener\">new version of my R script<\/a> called <code>NOAA_OISST_ncdf4.R<\/code> that contains three functions for extracting sets of SST data from the three types of OISST files (weekly, daily, single-day files), and a simple plotting function. The script is found at <a href=\"https:\/\/github.com\/millerlp\/Misc_R_scripts\/blob\/master\/NOAA_OISST_ncdf4.R\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/millerlp\/Misc_R_scripts\/blob\/master\/NOAA_OISST_ncdf4.R<\/a>. Save that file into your R working directory as NOAA_OISST_ncdf4.R, then call <code>source(\"NOAA_OISST_ncdf4.R\")<\/code> at the R command line to load up the four functions. The functions are<\/p>\n<ul>\n<li><code>extractOISSTdaily()<\/code> &#8211; extract gridded data from the high resolution files that contain daily SST values for a whole year that can be found at <a href=\"http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.highres.html\" target=\"_blank\" rel=\"noopener\">http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.highres.html<\/a>.<\/li>\n<li><code>extractOISST1day()<\/code> &#8211; extract gridded data from a single-day high resolution file that can be found at <a href=\"http:\/\/www.ncdc.noaa.gov\/sst\/griddata.php\" target=\"_blank\" rel=\"noopener\">http:\/\/www.ncdc.noaa.gov\/sst\/griddata.php<\/a>.<\/li>\n<li><code>extractOISSTweekly()<\/code> &#8211; extract 1&#215;1\u00b0 gridded weekly mean SST values from one of the two big compilation files found at <a href=\"http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.html\" target=\"_blank\" rel=\"noopener\">http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.html<\/a>.<\/li>\n<li><code>plotOISST()<\/code> &#8211; a basic plot of the output matrix from the other three functions.<\/li>\n<\/ul>\n<p>In each of the extract functions, you supply a number of arguments:<\/p>\n<ul>\n<li><code>fname<\/code> &#8211; the path to the OISST NetCDF file (a .nc file).<\/li>\n<li><code>lsmask<\/code> &#8211; the path to the land-sea mask file (a .nc file).<\/li>\n<li><code>lonW<\/code> &#8211; the westernmost longitude of the area you want to extract (0 to 359).<\/li>\n<li><code>lonE<\/code> &#8211; the easternmost longitude of the area you want to extract (0 to 359). This argument can be left out if you only want data from the grid longitude closest to <code>lonW<\/code>.<\/li>\n<li><code>latS<\/code> &#8211; the southernmost latitude of the area you want to extract (-89.5 to 89.5)<\/li>\n<li><code>latN<\/code> &#8211; the northernmost latitude of the area you want to extract (-89.5 to 89.5). This argument can be left out if you only want data from the grid latitude closest to <code>latS<\/code>.<\/li>\n<li><code>date1<\/code> &#8211; the starting date of the time period you want to extract. This should be a R date object or a string specified as &#8220;2014-11-25&#8221;. This argument isn&#8217;t used in the single-day <code>extractOISST1day<\/code> function.<\/li>\n<li><code>date2<\/code> &#8211; the last date of the time period you want to extract. This should be a R date object or a string specified as &#8220;2014-11-25&#8221;. This argument is optional in the first two functions if you only want one day&#8217;s data. This argument isn&#8217;t used in the single-day <code>extractOISST1day<\/code> function.<\/li>\n<\/ul>\n<p>To extract the data used in the figure above, I first downloaded the 2014 high resolution daily file linked from <a href=\"http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.highres.html\" target=\"_blank\" rel=\"noopener\">http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.highres.html<\/a>, along with the land-sea mask file lsmask.nc available directly on that page. The process in R was:<\/p>\n<p>[code lang=&#8221;r&#8221;]<br \/>\nsource(&quot;NOAA_OISST_ncdf4.R&quot;)<br \/>\nssts = extractOISSTdaily(&quot;W:\/NOAA_OISST\/daily\/sst.day.mean.2014.v2.nc&quot;,&quot;W:\/NOAA_OISST\/daily\/lsmask.oisst.v2.nc&quot;,lonW=0,lonE=70,latS=-50,latN=20,date1=&#8217;2014-11-23&#8242;,date2=&#8217;2014-11-24&#8242;)<br \/>\n[\/code]<\/p>\n<p>That extracted 2 days of data. To plot the 2nd day&#8217;s data, I did the following:<\/p>\n<p>[code lang=&#8221;r&#8221;]<br \/>\nplotOISST(ssts, 2)<br \/>\n[\/code]<\/p>\n<p>And that&#8217;s it.<\/p>\n<p>As always, if you used the OISST data, it&#8217;s important to cite the group that produces it and makes it available. As they state on the site:<br \/>\n&#8220;This may be done by including text such as NOAA High Resolution SST data provided by the NOAA\/OAR\/ESRL PSD, Boulder, Colorado, USA, from their Web site at http:\/\/www.esrl.noaa.gov\/psd\/ in any documents or publications using these data.&#8221;<\/p>\n<p>Or you can use the full-blown citations:<\/p>\n<ul>\n<li>Reynolds, Richard W., Thomas M. Smith, Chunying Liu, Dudley B. Chelton, Kenneth S. Casey, Michael G. Schlax, 2007: Daily High-Resolution-Blended Analyses for Sea Surface Temperature. J. Climate, 20, 5473-5496.<\/li>\n<li>Reynolds, R.W., N.A. Rayner, T.M. Smith, D.C. Stokes, and W. Wang, 2002: An improved in situ and satellite SST analysis for climate. J. Climate, 15, 1609-1625.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve written previously about some example R scripts I created to extract sea surface temperature data from NOAA&#8217;s Optimum Interpolated Sea Surface Temperature products. If you want daily global sea surface temperatures on a 0.25&#215;0.25\u00b0 grid, they gather those into 1-year files available at http:\/\/www.esrl.noaa.gov\/psd\/data\/gridded\/data.noaa.oisst.v2.highres.html. If you want weekly average SST values on a 1&#215;1\u00b0 [&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],"tags":[74,117,64,77,58,73,177,203],"class_list":["post-1893","post","type-post","status-publish","format-standard","hentry","category-r-project","tag-ncdf","tag-ncdf4","tag-noaa","tag-oisst","tag-r-project","tag-sea-surface-temperature","tag-sst","tag-windows"],"_links":{"self":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1893","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=1893"}],"version-history":[{"count":14,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1893\/revisions"}],"predecessor-version":[{"id":2419,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/posts\/1893\/revisions\/2419"}],"wp:attachment":[{"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/media?parent=1893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/categories?post=1893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lukemiller.org\/index.php\/wp-json\/wp\/v2\/tags?post=1893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}