<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/everybody-talks-about-the-weather/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ropensci.org/blog/blog/2017/04/04/gsodr"> rOpenSci Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<blockquote>
<p>Everybody talks about the weather, but nobody does anything about it. – Charles Dudley Warner</p>
</blockquote>
<p>As a scientist who models <a target="_blank" rel="nofollow" href="https://github.com/adamhsparks/STRASA-Biotic-Stress-Maps">plant diseases</a>, I use a lot of weather data. Often this data is not available for areas of interest. Previously, I worked with the <a target="_blank" rel="nofollow" href="http://www.irri.org/">International Rice Research Institute (IRRI)</a> and often the countries I was working with did not have weather data available or I was working on a large area covering several countries and needed a single source of data to work from. Other scientists who work with crop biophysical models to model crop yields also have similar weather data needs and may experience similar issues with data availability.</p>
<p>The United States National Oceanic and Atmospheric Administration's (NOAA) National Centers for Environmental Information (NCEI <sup><a target="_blank" rel="nofollow" href="http://ropensci.org/blog/blog/2017/04/04/gsodr#fn1">1</a></sup>) provides several sources of data, one of which is the <a target="_blank" rel="nofollow" href="https://data.noaa.gov/dataset/global-surface-summary-of-the-day-gsod">Global Surface Summary of the Day (GSOD)</a> data. The data are attractive because they are daily time-step and ground or buoy station based, freely available, and the data span several years, 1929 to current, with data from 1973 to current being the most complete. For more information on the GSOD data, please see the description of the data provided by NCEI, <a target="_blank" rel="nofollow" href="http://www7.ncdc.noaa.gov/CDO/GSOD_DESC.txt">http://www7.ncdc.noaa.gov/CDO/GSOD_DESC.txt</a>.</p>
<p>While the GSOD data are a valuable source of weather data with global coverage. The data files can be cumbersome and difficult to work with for research purposes. So instead of just talking about it, I did something about it. The <em>GSODR</em> package aims to make it easy to find, transfer and format the data you need for use in analysis. The package provides four main functions for facilitating this:</p>
<ul>
<li> <code>get_GSOD()</code> – the main function that will query and transfer files from the FTP server, reformat them and return a data.frame in R or save a file to disk</li>
<li> <code>reformat_GSOD()</code> – the workhorse, this function takes individual station files on the local disk and reformats them returning a data.frame in R</li>
<li> <code>nearest_stations()</code> – this function returns a data frame containing a list of stations and their metadata that fall within the given radius of a point specified by the user</li>
<li> <code>get_station_list()</code> – this function retrieves the most up-to-date list of stations and corresponding metadata</li>
</ul>
<p>When reformatting data either with <code>get_GSOD()</code> or <code>reformat_GSOD()</code>, all units are converted to International System of Units (SI), <em>e.g.</em>, inches to millimetres and Fahrenheit to Celsius. File output can be saved as a Comma Separated Value (CSV) file or in a spatial <a target="_blank" rel="nofollow" href="http://www.geopackage.org/">GeoPackage (GPKG)</a> file, implemented by most major GIS software products, summarising each year by station, which also includes vapour pressure (ea and es) and relative humidity variables calculated from existing data in GSOD by the package.</p>
<p>Documentation is provided for <em>GSODR</em> using <a target="_blank" rel="nofollow" href="http://hadley.github.io/pkgdown/"><em>pkgdown</em></a>, which you can find on the <em>GSODR</em> website, <a target="_blank" rel="nofollow" href="http://ropensci.github.io/GSODR/">http://ropensci.github.io/GSODR/</a> or of course in the usual R vignettes and help files.</p>
<h2>How Can You Use <em>GSODR</em>?</h2>
<p>Recently a colleague contacted me asking if I knew of or had weather data for a time period covering 1960 to 2015 for selected provinces in the Philippines where the <a target="_blank" rel="nofollow" href="http://www.irri.org/">International Rice Research Institute</a> (IRRI) has conducted surveys. The IRRI survey loop in Central Luzon is a study that aims to monitor the changes in rice farming in the major rice producing area of the Philippines, the Central Luzon region, which is called the &quot;rice bowl of the Philippines&quot;. In this survey data have been collected several times since the 1960s, see the <a target="_blank" rel="nofollow" href="http://ricestat.irri.org/fhsd/php/panel.php?page=1&sortBy=title&sortOrder=ascending#">Farm Household Survey Database webpage</a> for the data collected data. Using the <em>GSODR</em> package I was able to retrieve weather data from stations within a 100km radius of the centre of the provinces included in the survey and provide my colleague with a CSV file of weather data from ground-based weather stations.</p>
<p>As an example of how we can use <em>GSODR</em>, I will demonstrate the following:</p>
<ul>
<li>
<p>retrieving a spatial object of provincial level data;</p>
</li>
<li>
<p>sub-setting this data for the provinces of interest;</p>
</li>
<li>
<p>merging the polygons into one object;</p>
</li>
<li>
<p>finding the centroid of this resulting polygon;</p>
</li>
<li>
<p>using the centroid of the polygon to find stations within 100km of this point;</p>
</li>
<li>
<p>determining which stations provide data for the specified time-period, 1960-2015; and</p>
</li>
<li>
<p>downloading the station files and creating a single CSV file of the data for analysis.</p>
</li>
</ul>
<h3>Retrieve PHL Provincial Data and Select Loop Provinces</h3>
<p>As a first step, we'll use the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=raster"><em>raster</em></a> package to retrieve data from <a target="_blank" rel="nofollow" href="http://www.gadm.org/">GADM</a> that will provide the provincial spatial data for the survey area. We will then use this to find the centroid of the area of interest, which will be used to find the nearest stations. Using <code>raster::getData()</code> fetch level 0 (national) and 1 (provincial) data for the Philippines.</p>
<div>
<pre><code data-lang="r"><span></span>install.packages<span>(</span><span>&quot;raster&quot;</span><span>)</span>
<span>library</span><span>(</span>raster<span>)</span>
RP0 <span>&lt;-</span> raster<span>::</span>getData<span>(</span>country <span>=</span> <span>&quot;Philippines&quot;</span><span>,</span> level <span>=</span> <span>0</span><span>)</span>
RP1 <span>&lt;-</span> raster<span>::</span>getData<span>(</span>country <span>=</span> <span>&quot;Philippines&quot;</span><span>,</span> level <span>=</span> <span>1</span><span>)</span>
</code></pre>
</div>
<p>Now we will select the provinces involved in the survey and make a new object called <code>Central_Luzon</code> from the provincial level data, <code>RP1</code>.</p>
<div>
<pre><code data-lang="r"><span></span>Central_Luzon <span>&lt;-</span> RP1<span>[</span>RP1<span>@</span>data<span>$</span>NAME_1 <span>==</span> <span>&quot;Pampanga&quot;</span> <span>|</span>
                     RP1<span>@</span>data<span>$</span>NAME_1 <span>==</span> <span>&quot;Tarlac&quot;</span> <span>|</span>
                     RP1<span>@</span>data<span>$</span>NAME_1 <span>==</span> <span>&quot;Pangasinan&quot;</span> <span>|</span>
                     RP1<span>@</span>data<span>$</span>NAME_1 <span>==</span> <span>&quot;La Union&quot;</span> <span>|</span>
                     RP1<span>@</span>data<span>$</span>NAME_1 <span>==</span> <span>&quot;Nueva Ecija&quot;</span> <span>|</span>
                     RP1<span>@</span>data<span>$</span>NAME_1 <span>==</span> <span>&quot;Bulacan&quot;</span><span>,</span> <span>]</span>
</code></pre>
</div>
<p>With a little help from an old acquaintance, <a target="_blank" rel="nofollow" href="http://r-nold.blogspot.com.au/2014/06/creating-inset-map-with-ggplot2.html">Arnold Salvacion</a> and <a target="_blank" rel="nofollow" href="https://github.com/sckott/sckott.github.com/blob/master/_posts/2012-08-22-ggplot-inset-map.md">Scott Chamberlain</a> we can create a map inset showing where the Central Luzon Loop survey takes place.</p>
<p>First we'll use <code>gSimplify()</code> from <em>rgeos</em> to simplify the map of the Philippines to make the map generation in the next few steps quicker.</p>
<div>
<pre><code data-lang="r"><span></span>RP0 <span>&lt;-</span> rgeos<span>::</span>gSimplify<span>(</span>RP0<span>,</span> tol <span>=</span> <span>0.05</span><span>)</span>
</code></pre>
</div>
<div>
<pre><code data-lang="r"><span></span><span>library</span><span>(</span>ggplot2<span>)</span>
<span>library</span><span>(</span>grid<span>)</span>
<span>library</span><span>(</span>gridExtra<span>)</span>

CL_names <span>&lt;-</span> <span>data.frame</span><span>(</span>coordinates<span>(</span>Central_Luzon<span>))</span> <span># get center coordinates of provinces in Central Luzon</span>
CL_names<span>$</span>label <span>&lt;-</span> Central_Luzon<span>@</span>data<span>$</span>NAME_1

<span># Main Map</span>
p1 <span>&lt;-</span> ggplot<span>()</span> <span>+</span>
  geom_polygon<span>(</span>data <span>=</span> Central_Luzon<span>,</span> aes<span>(</span>x <span>=</span> long<span>,</span> y <span>=</span> lat<span>,</span> group <span>=</span> group<span>),</span>
               colour <span>=</span> <span>&quot;grey10&quot;</span><span>,</span> fill <span>=</span> <span>&quot;#fff7bc&quot;</span><span>)</span> <span>+</span>
  geom_text<span>(</span>data <span>=</span> CL_names<span>,</span> aes<span>(</span>x <span>=</span> X1<span>,</span> y <span>=</span> X2<span>,</span> label <span>=</span> label<span>),</span>
            size <span>=</span> <span>3</span><span>,</span> colour <span>=</span> <span>&quot;grey20&quot;</span><span>)</span> <span>+</span>
  theme<span>(</span>axis.text.y <span>=</span> element_text<span>(</span>angle <span>=</span> <span>90</span><span>,</span> hjust <span>=</span> <span>0.5</span><span>))</span> <span>+</span>
  ggtitle<span>(</span><span>&quot;Central Luzon Provinces Surveyed&quot;</span><span>)</span> <span>+</span>
  theme_bw<span>()</span> <span>+</span>
  xlab<span>(</span><span>&quot;Longitude&quot;</span><span>)</span> <span>+</span>
  ylab<span>(</span><span>&quot;Latitude&quot;</span><span>)</span> <span>+</span>
  coord_map<span>()</span>
</code></pre>
</div>
<div>
<pre><code data-lang="text"><span></span>## Regions defined for each Polygons
</code></pre>
</div>
<div>
<pre><code data-lang="r"><span></span><span># Inset</span>
p2 <span>&lt;-</span> ggplot<span>()</span> <span>+</span>
  geom_polygon<span>(</span>data <span>=</span> RP0<span>,</span> aes<span>(</span>long<span>,</span> lat<span>,</span> group <span>=</span> group<span>),</span>
               colour <span>=</span> <span>&quot;grey10&quot;</span><span>,</span>
               fill <span>=</span> <span>&quot;#fff7bc&quot;</span><span>)</span> <span>+</span>
  coord_equal<span>()</span> <span>+</span>
  theme_bw<span>()</span> <span>+</span>
  labs<span>(</span>x <span>=</span> <span>NULL</span><span>,</span> y <span>=</span> <span>NULL</span><span>)</span> <span>+</span>
  geom_rect<span>(</span>aes<span>(</span>xmin <span>=</span> extent<span>(</span>Central_Luzon<span>)[</span><span>1</span><span>],</span>
                xmax <span>=</span> extent<span>(</span>Central_Luzon<span>)[</span><span>2</span><span>],</span>
                ymin <span>=</span> extent<span>(</span>Central_Luzon<span>)[</span><span>3</span><span>],</span>
                ymax <span>=</span> extent<span>(</span>Central_Luzon<span>)[</span><span>4</span><span>]),</span>
            alpha <span>=</span> <span>0</span><span>,</span>
            colour <span>=</span> <span>&quot;red&quot;</span><span>,</span>
            size <span>=</span> <span>1</span><span>,</span>
            linetype <span>=</span> <span>1</span><span>)</span> <span>+</span>
  theme<span>(</span>axis.text.x <span>=</span> element_blank<span>(),</span>
        axis.text.y <span>=</span> element_blank<span>(),</span>
        axis.ticks <span>=</span> element_blank<span>(),</span>
        axis.title.x <span>=</span> element_blank<span>(),</span>
        axis.title.y <span>=</span> element_blank<span>(),</span>
        plot.margin <span>=</span> unit<span>(</span><span>c</span><span>(</span><span>0</span><span>,</span> <span>0</span><span>,</span> <span>0</span> <span>,</span><span>0</span><span>),</span> <span>&quot;mm&quot;</span><span>))</span>

grid.newpage<span>()</span>
v1 <span>&lt;-</span> viewport<span>(</span>width <span>=</span> <span>1</span><span>,</span> height <span>=</span> <span>1</span><span>,</span> x <span>=</span> <span>0.5</span><span>,</span> y <span>=</span> <span>0.5</span><span>)</span> <span># plot area for the main map</span>
v2 <span>&lt;-</span> viewport<span>(</span>width <span>=</span> <span>0.28</span><span>,</span> height <span>=</span> <span>0.28</span><span>,</span> x <span>=</span> <span>0.67</span><span>,</span> y <span>=</span> <span>0.78</span><span>)</span> <span># plot area for the inset map</span>
<span>print</span><span>(</span>p1<span>,</span> vp <span>=</span> v1<span>)</span>
<span>print</span><span>(</span>p2<span>,</span> vp <span>=</span> v2<span>)</span>
</code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/ropensci.org/assets/blog-images/2017-04-04-gsodr/plot_CL-1.png?w=456" data-recalc-dims="1"></p>
<h3>Dissolve Polygons and Find Centroid of Loop Survey Area</h3>
<p>Now that we have the provincial data that we want, we will dissolve the polygons that represent the individual provinces in Central Luzon and find the centroid of all of them, which we will use as the central point for querying stations from the GSOD data set.</p>
<div>
<pre><code data-lang="r"><span></span>Central_Luzon <span>&lt;-</span> rgeos<span>::</span>gUnaryUnion<span>(</span>Central_Luzon<span>)</span>
centroid <span>&lt;-</span> rgeos<span>::</span>gCentroid<span>(</span>Central_Luzon<span>)</span>

ggplot<span>()</span> <span>+</span>
  geom_polygon<span>(</span>data <span>=</span> Central_Luzon<span>,</span> aes<span>(</span>x <span>=</span> long<span>,</span> y <span>=</span> lat<span>,</span> group <span>=</span> group<span>),</span>
               colour <span>=</span> <span>&quot;grey10&quot;</span><span>,</span> fill <span>=</span> <span>&quot;#fff7bc&quot;</span><span>)</span> <span>+</span>
  geom_point<span>(</span>aes<span>(</span>x <span>=</span> centroid<span>@</span>coords<span>[</span><span>1</span><span>],</span> y <span>=</span> centroid<span>@</span>coords<span>[</span><span>2</span><span>]))</span> <span>+</span>
  theme<span>(</span>axis.text.y <span>=</span> element_text<span>(</span>angle <span>=</span> <span>90</span><span>,</span> hjust <span>=</span> <span>0.5</span><span>))</span> <span>+</span>
  ggtitle<span>(</span><span>&quot;Centre of Provinces Surveyed&quot;</span><span>)</span> <span>+</span>
  theme_bw<span>()</span> <span>+</span>
  xlab<span>(</span><span>&quot;Longitude&quot;</span><span>)</span> <span>+</span>
  ylab<span>(</span><span>&quot;Latitude&quot;</span><span>)</span> <span>+</span>
  coord_map<span>()</span>
</code></pre>
</div>
<p><img alt="" src="https://i2.wp.com/ropensci.org/assets/blog-images/2017-04-04-gsodr/dissolve_and_plot_CL-1.png?w=456" data-recalc-dims="1"></p>
<p>Next, make a list of stations that are within this area. First we need to fetch the station medadata, &quot;isd-history.csv&quot; from the FTP server and then check which stations fall within a 100km radius of the centre of the provinces we're interested in.</p>
<div>
<pre><code data-lang="r"><span></span><span>library</span><span>(</span>GSODR<span>)</span>
<span>library</span><span>(</span>readr<span>)</span>
<span># Fetch station list from NCEI</span>
station_meta <span>&lt;-</span> read_csv<span>(</span>
  <span>&quot;ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-history.csv&quot;</span><span>,</span>
  col_types <span>=</span> <span>&quot;ccccccddddd&quot;</span><span>,</span>
  col_names <span>=</span> <span>c</span><span>(</span><span>&quot;USAF&quot;</span><span>,</span> <span>&quot;WBAN&quot;</span><span>,</span> <span>&quot;STN_NAME&quot;</span><span>,</span> <span>&quot;CTRY&quot;</span><span>,</span> <span>&quot;STATE&quot;</span><span>,</span> <span>&quot;CALL&quot;</span><span>,</span> <span>&quot;LAT&quot;</span><span>,</span>
                <span>&quot;LON&quot;</span><span>,</span> <span>&quot;ELEV_M&quot;</span><span>,</span> <span>&quot;BEGIN&quot;</span><span>,</span> <span>&quot;END&quot;</span><span>),</span> skip <span>=</span> <span>1</span><span>)</span>
station_meta<span>$</span>STNID <span>&lt;-</span> <span>as.character</span><span>(</span><span>paste</span><span>(</span>station_meta<span>$</span>USAF<span>,</span>
                                         station_meta<span>$</span>WBAN<span>,</span>
                                         sep <span>=</span> <span>&quot;-&quot;</span><span>))</span>

loop_stations <span>&lt;-</span> nearest_stations<span>(</span>LAT <span>=</span> centroid<span>@</span>coords<span>[</span><span>2</span><span>],</span>
                                  LON <span>=</span> centroid<span>@</span>coords<span>[</span><span>1</span><span>],</span>
                                  distance <span>=</span> <span>100</span><span>)</span>

loop_stations <span>&lt;-</span> station_meta<span>[</span>station_meta<span>$</span>STNID <span>%in%</span> loop_stations<span>,</span> <span>]</span>

loop_stations <span>&lt;-</span> loop_stations<span>[</span>loop_stations<span>$</span>BEGIN <span>&lt;=</span> <span>19591231</span> <span>&amp;</span>
                               loop_stations<span>$</span>END <span>>=</span> <span>20151231</span><span>,</span> <span>]</span>

<span>print</span><span>(</span>loop_stations<span>[,</span> <span>c</span><span>(</span><span>1</span><span>:</span><span>2</span><span>,</span> <span>3</span><span>,</span> <span>7</span><span>:</span><span>12</span><span>)])</span>
</code></pre>
</div>
<div>
<pre><code data-lang="text"><span></span>## # A tibble: 9 × 9
##     USAF  WBAN          STN_NAME    LAT     LON ELEV_M    BEGIN      END
##    &lt;chr> &lt;chr>             &lt;chr>  &lt;dbl>   &lt;dbl>  &lt;dbl>    &lt;dbl>    &lt;dbl>
## 1 983240 99999  IBA/LUZON ISLAND 15.333 119.967    5.0 19490104 20170315
## 2 983250 99999           DAGUPAN 16.083 120.350    2.0 19450119 20170315
## 3 983270 99999        CLARK INTL 15.186 120.560  147.5 19450214 20170315
## 4 983280 99999            BAGUIO 16.375 120.620 1295.7 19490101 20170315
## 5 984260 41231 SUBIC BAY WEATHER 14.800 120.267   19.0 19450209 20170315
## 6 984290 99999 NINOY AQUINO INTL 14.509 121.020   22.9 19450331 20170315
## 7 984300 99999    SCIENCE GARDEN 14.650 121.050   46.0 19450228 20170315
## 8 984320 99999          AMBULONG 14.083 121.050   11.0 19490205 20170315
## 9 984330 99999             TANAY 14.583 121.367  651.0 19490101 20170315
## # ... with 1 more variables: STNID &lt;chr>
</code></pre>
</div>
<p>These are all of the stations that are availble within 100km of the centroid of this area and the years for which data are available.</p>
<h3>Using <code>get_GSOD()</code> to Fetch the Requested Station Files</h3>
<p>This example shows how you could construct a query using the <code>get_GSOD()</code> function. Be aware that it may result in incomplete data and error from the server if it stops responding. We've done our best to make <em>GSODR</em> handle these errors, but if it does this, see the following option for using the <code>reformat_GSOD()</code> function.</p>
<div>
<pre><code data-lang="r"><span></span>PHL <span>&lt;-</span> get_GSOD<span>(</span>station <span>=</span>
                  <span>eval</span><span>(</span><span>parse</span><span>(</span>text <span>=</span> loop_stations<span>[,</span> <span>12</span><span>])),</span> years <span>=</span> <span>1960</span><span>:</span><span>2015</span><span>)</span>
</code></pre>
</div>
<div>
<pre><code data-lang="text"><span></span>##
## Checking requested station file for availability on server.

##
## Downloading individual station files.

## Starting data file processing
</code></pre>
</div>
<h3>Another Option, Using <code>reformat_GSOD()</code></h3>
<p><em>GSODR</em> provides a function for dealing with local files that have been transferred from the server already as well, <code>reformat_GSOD()</code>. If the previous example with <code>get_GSOD()</code> does not work, this is a good alternative that takes a bit more intervention but gives the same results.</p>
<p>Using your FTP client (<em>e.g.</em>, FileZilla) log into the NCEI FTP server, <code>ftp.ncdc.noaa.gov</code> and navigate to /pub/data/gsod/. Manually downloading the files for each station listed above from 1960 to 2015 is possible, but tedious. An easier solution is to simply download the annual files found in each yearly directory, &quot;gsod-YYYY.tar&quot; and untar them locally and then use R to list the available files and select only the files for the stations of interest. Lastly, write the data to disk as a CSV file for saving and later use.</p>
<div>
<pre><code data-lang="r"><span></span>years <span>&lt;-</span> <span>1960</span><span>:</span><span>2015</span>

loop_stations <span>&lt;-</span> <span>eval</span><span>(</span><span>parse</span><span>(</span>text <span>=</span> loop_stations<span>[,</span> <span>12</span><span>]))</span>

<span># create file list</span>
loop_stations <span>&lt;-</span> <span>do.call</span><span>(</span>
  <span>paste0</span><span>,</span> <span>c</span><span>(</span><span>expand.grid</span><span>(</span>loop_stations<span>,</span> <span>&quot;-&quot;</span><span>,</span> years<span>,</span> <span>&quot;.op.gz&quot;</span><span>))</span>
  <span>)</span>

local_files <span>&lt;-</span> <span>list.files</span><span>(</span>path <span>=</span> <span>&quot;./GSOD&quot;</span><span>,</span> full.names <span>=</span> <span>TRUE</span><span>,</span> recursive <span>=</span> <span>TRUE</span><span>)</span>
local_files <span>&lt;-</span> local_files<span>[</span><span>basename</span><span>(</span>local_files<span>)</span> <span>%in%</span> loop_stations<span>]</span>

loop_data <span>&lt;-</span> reformat_GSOD<span>(</span>file_list <span>=</span> local_files<span>)</span>

readr<span>::</span>write_csv<span>(</span>loop_data<span>,</span> file <span>=</span> <span>&quot;Loop_Survey_Weather_1960-2015&quot;</span><span>)</span>
</code></pre>
</div>
<h3>The Data Content and Format</h3>
<p>The final data returned by either of these methods will be data that include the following elements for the years of 1960-2015</p>
<ul>
<li>
<p><strong>STNID</strong> – Station number (WMO/DATSAV3 number) for the location;</p>
</li>
<li>
<p><strong>WBAN</strong> – number where applicable–this is the historical &quot;Weather Bureau Air Force Navy&quot; number – with WBAN being the acronym;</p>
</li>
<li>
<p><strong>STN_NAME</strong> – Unique text identifier;</p>
</li>
<li>
<p><strong>CTRY</strong> – Country in which the station is located;</p>
</li>
<li>
<p><strong>LAT</strong> – Latitude. <em>Station dropped in cases where values are &lt; -90 or > 90 degrees or Lat = 0 and Lon = 0</em>;</p>
</li>
<li>
<p><strong>LON</strong> – Longitude. <em>Station dropped in cases where values are &lt; -180 or > 180 degrees or Lat = 0 and Lon = 0</em>;</p>
</li>
<li>
<p><strong>ELEV_M</strong> – Elevation in metres;</p>
</li>
<li>
<p><strong>ELEV_M_SRTM_90m</strong> – Elevation in metres corrected for possible errors, derived from the CGIAR-CSI SRTM 90m database (Jarvis et al. 2008);</p>
</li>
<li>
<p><strong>YEARMODA</strong> – Date in YYYY-mm-dd format;</p>
</li>
<li>
<p><strong>YEAR</strong> – The year (YYYY);</p>
</li>
<li>
<p><strong>MONTH</strong> – The month (mm);</p>
</li>
<li>
<p><strong>DAY</strong> – The day (dd);</p>
</li>
<li>
<p><strong>YDAY</strong> – Sequential day of year (not in original GSOD);</p>
</li>
<li>
<p><strong>TEMP</strong> – Mean daily temperature converted to degrees C to tenths. Missing = NA;</p>
</li>
<li>
<p><strong>TEMP_CNT</strong> – Number of observations used in calculating mean daily temperature;</p>
</li>
<li>
<p><strong>DEWP</strong> – Mean daily dew point converted to degrees C to tenths. Missing = NA;</p>
</li>
<li>
<p><strong>DEWP_CNT</strong> – Number of observations used in calculating mean daily dew point;</p>
</li>
<li>
<p><strong>SLP</strong> – Mean sea level pressure in millibars to tenths. Missing = NA;</p>
</li>
<li>
<p><strong>SLP_CNT</strong> – Number of observations used in calculating mean sea level pressure;</p>
</li>
<li>
<p><strong>STP</strong> – Mean station pressure for the day in millibars to tenths. Missing = NA;</p>
</li>
<li>
<p><strong>STP_CNT</strong> – Number of observations used in calculating mean station pressure;</p>
</li>
<li>
<p><strong>VISIB</strong> – Mean visibility for the day converted to kilometres to tenths Missing = NA;</p>
</li>
<li>
<p><strong>VISIB_CNT</strong> – Number of observations used in calculating mean daily visibility;</p>
</li>
<li>
<p><strong>WDSP</strong> – Mean daily wind speed value converted to metres/second to tenths Missing = NA;</p>
</li>
<li>
<p><strong>WDSP_CNT</strong> – Number of observations used in calculating mean daily wind speed;</p>
</li>
<li>
<p><strong>MXSPD</strong> – Maximum sustained wind speed reported for the day converted to metres/second to tenths. Missing = NA;</p>
</li>
<li>
<p><strong>GUST</strong> – Maximum wind gust reported for the day converted to metres/second to tenths. Missing = NA;</p>
</li>
<li>
<p><strong>MAX</strong> – Maximum temperature reported during the day converted to Celsius to tenths–time of max temp report varies by country and region, so this will sometimes not be the max for the calendar day. Missing = NA;</p>
</li>
<li>
<p><strong>MAX_FLAG</strong> – Blank indicates max temp was taken from the explicit max temp report and not from the 'hourly' data. An &quot;*&quot; indicates max temp was derived from the hourly data (i.e., highest hourly or synoptic-reported temperature);</p>
</li>
<li>
<p><strong>MIN</strong> – Minimum temperature reported during the day converted to Celsius to tenths–time of min temp report varies by country and region, so this will sometimes not be the max for the calendar day. Missing = NA;</p>
</li>
<li>
<p><strong>MIN_FLAG</strong> – Blank indicates max temp was taken from the explicit min temp report and not from the 'hourly' data. An &quot;*&quot; indicates min temp was derived from the hourly data (i.e., highest hourly or synoptic-reported temperature);</p>
</li>
<li>
<p><strong>PRCP</strong> – Total precipitation (rain and/or melted snow) reported during the day converted to millimetres to hundredths; will usually not end with the midnight observation, i.e., may include latter part of previous day. A value of &quot;.00&quot; indicates no measurable precipitation (includes a trace). Missing = NA; <em>Note: Many stations do not report '0' on days with no precipitation– therefore, 'NA' will often appear on these days. For example, a station may only report a 6-hour amount for the period during which rain fell.</em> See FLAGS_PRCP column for source of data;</p>
</li>
<li>
<p><strong>PRCP_FLAG</strong> –</p>
<ul>
<li> A = 1 report of 6-hour precipitation amount;</li>
<li> B = Summation of 2 reports of 6-hour precipitation amount;</li>
<li> C = Summation of 3 reports of 6-hour precipitation amount;</li>
<li> D = Summation of 4 reports of 6-hour precipitation amount;</li>
<li> E = 1 report of 12-hour precipitation amount;</li>
<li> F = Summation of 2 reports of 12-hour precipitation amount;</li>
<li> G = 1 report of 24-hour precipitation amount;</li>
<li> H = Station reported '0' as the amount for the day (e.g., from 6-hour reports), but also reported at least one occurrence of precipitation in hourly observations–this could indicate a rrace occurred, but should be considered as incomplete data for the day;</li>
<li> I = Station did not report any precipitation data for the day and did not report any occurrences of precipitation in its hourly observations–it's still possible that precipitation occurred but was not reported;</li>
</ul>
</li>
<li>
<p><strong>SNDP</strong> – Snow depth in millimetres to tenths. Missing = NA;</p>
</li>
<li>
<p><strong>I_FOG</strong> – Indicator for fog, (1 = yes, 0 = no/not reported) for the occurrence during the day;</p>
</li>
<li>
<p><strong>I_RAIN_DRIZZLE</strong> – Indicator for rain or drizzle, (1 = yes, 0 = no/not reported) for the occurrence during the day;</p>
</li>
<li>
<p><strong>I_SNOW_ICE</strong> – Indicator for snow or ice pellets, (1 = yes, 0 = no/not reported) for the occurrence during the day;</p>
</li>
<li>
<p><strong>I_HAIL</strong> – Indicator for hail, (1 = yes, 0 = no/not reported) for the occurrence during the day;</p>
</li>
<li>
<p><strong>I_THUNDER</strong> – Indicator for thunder, (1 = yes, 0 = no/not reported) for the occurrence during the day;</p>
</li>
<li>
<p><strong>I_TORNADO_FUNNEL</strong> – Indicator for tornado or funnel cloud, (1 = yes, 0 = no/not reported) for the occurrence during the day;</p>
</li>
<li>
<p><strong>ea</strong> – Mean daily actual vapour pressure;</p>
</li>
<li>
<p><strong>es</strong> – Mean daily saturation vapour pressure;</p>
</li>
<li>
<p><strong>RH</strong> – Mean daily relative humidity.</p>
</li>
</ul>
<p>Here's what the data look like.</p>
<div>
<pre><code data-lang="text"><span></span>##    WBAN        STNID         STN_NAME CTRY STATE CALL    LAT     LON
## 1 99999 983240-99999 IBA/LUZON ISLAND   RP  &lt;NA> RPUI 15.333 119.967
## 2 99999 983240-99999 IBA/LUZON ISLAND   RP  &lt;NA> RPUI 15.333 119.967
## 3 99999 983240-99999 IBA/LUZON ISLAND   RP  &lt;NA> RPUI 15.333 119.967
## 4 99999 983240-99999 IBA/LUZON ISLAND   RP  &lt;NA> RPUI 15.333 119.967
## 5 99999 983240-99999 IBA/LUZON ISLAND   RP  &lt;NA> RPUI 15.333 119.967
## 6 99999 983240-99999 IBA/LUZON ISLAND   RP  &lt;NA> RPUI 15.333 119.967
##   ELEV_M ELEV_M_SRTM_90m    BEGIN      END YEARMODA YEAR MONTH DAY YDAY
## 1      5               9 19490104 20170315 19730101 1973    01  01    1
## 2      5               9 19490104 20170315 19730102 1973    01  02    2
## 3      5               9 19490104 20170315 19730103 1973    01  03    3
## 4      5               9 19490104 20170315 19730104 1973    01  04    4
## 5      5               9 19490104 20170315 19730105 1973    01  05    5
## 6      5               9 19490104 20170315 19730106 1973    01  06    6
##   TEMP TEMP_CNT DEWP DEWP_CNT    SLP SLP_CNT STP STP_CNT VISIB VISIB_CNT
## 1 26.8        7 23.8        7 1014.1       7  NA       0  34.3         7
## 2 26.6        5 23.2        5 1013.8       5  NA       0  34.0         5
## 3 25.8        6 23.2        6 1013.5       6  NA       0  34.9         6
## 4 28.4        5 25.0        5 1014.5       5  NA       0  38.0         5
## 5 25.8        4 23.5        4 1014.8       4  NA       0    NA         0
## 6 25.0        8 22.9        8 1014.8       8  NA       0  35.6         8
##   WDSP WDSP_CNT MXSPD GUST MAX MAX_FLAG MIN MIN_FLAG PRCP PRCP_FLAG SNDP
## 1  0.4        7   3.0   NA  31        *  24        *    0         I   NA
## 2  0.9        5   4.1   NA  33        *  19        *    0         I   NA
## 3  0.8        6   4.1   NA  30        *  21        *    0         I   NA
## 4  1.0        5   5.1   NA  32        *  23        *    0         I   NA
## 5  0.2        4   1.5   NA  30        *  22        *    0         I   NA
## 6  0.4        8   2.5   NA  31        *  19        *    0         I   NA
##   I_FOG I_RAIN_DRIZZLE I_SNOW_ICE I_HAIL I_THUNDER I_TORNADO_FUNNEL  EA
## 1     0              0          0      0         0                0 2.9
## 2     0              0          0      0         0                0 2.8
## 3     0              0          0      0         0                0 2.8
## 4     0              0          0      0         0                0 3.2
## 5     0              0          0      0         0                0 2.9
## 6     0              0          0      0         0                0 2.8
##    ES   RH
## 1 3.5 82.9
## 2 3.5 80.0
## 3 3.3 84.8
## 4 3.9 82.1
## 5 3.3 87.9
## 6 3.2 87.5
</code></pre>
</div>
<p>Using the <em>GSODR</em> package and R I was able to easily retrieve and provide weather data for the years requested that cover the area of interest for this survey and create a CSV file of the data for use with other software for the analysis.</p>
<h2>Using GSOD Data With Climate Data From <em>GSODRdata</em></h2>
<p>If you want to use climate and ecological data with weather data, we've also provided a supplementary data package to go with <em>GSODR</em>, <a target="_blank" rel="nofollow" href="https://github.com/adamhsparks/GSODRdata"><em>GSODRdata</em></a>, which provides climate data from five sources in six data frames:</p>
<ul>
<li>
<p><strong>CHELSA</strong> – <a target="_blank" rel="nofollow" href="http://chelsa-climate.org/">Climatic surfaces at 1 km resolution</a> is based on a quasi-mechanistic statistical downscaling of the ERA interim global circulation model (Karger <em>et al.</em> 2016). ESA's CCI-LC cloud probability monthly averages are based on the MODIS snow products (MOD10A2).</p>
</li>
<li>
<p><strong>CRU_CL_2</strong> – The <a target="_blank" rel="nofollow" href="https://crudata.uea.ac.uk/~timm/grid/CRU_CL_2_0.html">CRU CL 2.0 data-set</a> (New <em>et al.</em> 2002) comprises monthly grids of observed mean climate from 1961-1990, and covering the global land surface at a 10 minute spatial resolution. There are eight climatic variables available, and also the elevations on the grid: diurnal temperature range, precipitation, mean temperature, wet-day frequency, frost-day frequency, relative humidity, sunshine, and wind-speed. In addition minimum and maximum temperature may be deduced from mean temperature and diurnal temperature range (see <a target="_blank" rel="nofollow" href="https://crudata.uea.ac.uk/~timm/grid/faq.html">FAQ</a>).</p>
</li>
<li>
<p><strong>ESACCI</strong> – ESA's CCI-LC snow cover probability <a target="_blank" rel="nofollow" href="http://maps.elie.ucl.ac.be/CCI/viewer/index.php">http://maps.elie.ucl.ac.be/CCI/viewer/index.php</a></p>
</li>
<li>
<p><strong>MODCF</strong> – <a target="_blank" rel="nofollow" href="https://github.com/adammwilson/Cloud">Remotely sensed high-resolution global cloud dynamics for predicting ecosystem and biodiversity distributions</a> (Wilson <em>et al.</em> 2016) provides new near-global, fine-grain (≈1km) monthly cloud frequencies from 15 years of twice-daily MODIS satellite images.</p>
</li>
<li>
<p><strong>WorldClim_Bio</strong> – <a target="_blank" rel="nofollow" href="http://www.worldclim.org/version1">WorldClim Global Climate Data – Free climate data for ecological modeling and GIS</a> (Hijmans <em>et al.</em> 2004) provides freely available <a target="_blank" rel="nofollow" href="http://worldclim.org/bioclim">bioclimatic variables</a>. These data are freely available for download from <a target="_blank" rel="nofollow" href="http://www.worldclim.org/version1">http://www.worldclim.org/version1</a>.</p>
</li>
<li>
<p><strong>WorldClim_Clim</strong> – <a target="_blank" rel="nofollow" href="http://www.worldclim.org/version1">WorldClim Global Climate Data – Free climate data for ecological modeling and GIS</a> (Hijmans <em>et al.</em> 2004) provides freely available, average monthly climate data. Current conditions (interpolations of observed data, representative of 1960-1990) are freely available for download from <a target="_blank" rel="nofollow" href="http://www.worldclim.org/version1">http://www.worldclim.org/version1</a>.</p>
</li>
</ul>
<p>Due to the size of the package, >9Mb, it is only available from GitHub. However, these data frames provide climate and ecological data that corresponds to the GSOD station locations, making it easy to find and work with weather and climate data at the same time. If you're interested you can find some further examples in the <a target="_blank" rel="nofollow" href="https://cran.rstudio.com/web/packages/GSODR/vignettes/Working_with_spatial_and_climate_data.html"><em>GSODR</em> vignettes</a>.</p>
<h2>Conclusions</h2>
<p>The GSOD data have a wide range of applications and <em>GSODR</em> makes this data more accessible to scientists that need a global weather data set. Using <em>GSODR</em> means that you can efficiently query for a set of years, for specific stations or areas within a given radius. The GSOD data are not perfect, there are many gaps prior to 1973, but in the more recent years the data became more reliable and more stations are being added.</p>
<div>
<pre><code data-lang="r"><span></span>ggplot<span>(</span>station_meta<span>,</span> aes<span>(</span>x <span>=</span> END<span>))</span> <span>+</span>
  geom_histogram<span>()</span> <span>+</span>
  xlab<span>(</span><span>&quot;End Date (YYYYMMDD)&quot;</span><span>)</span> <span>+</span>
  ylab<span>(</span><span>&quot;Number of Stations&quot;</span><span>)</span> <span>+</span>
  ggtitle<span>(</span><span>&quot;Count of stations' end date&quot;</span><span>)</span> <span>+</span>
  theme_bw<span>()</span>
</code></pre>
</div>
<p><img alt="" src="https://i2.wp.com/ropensci.org/assets/blog-images/2017-04-04-gsodr/station_numbers-1.png?w=456" data-recalc-dims="1"></p>
<h2>How could you use <em>GSODR</em>?</h2>
<p>Let us know how you use <em>GSODR</em> in your work. If you find an issue, please file an <a target="_blank" rel="nofollow" href="https://github.com/ropensci/GSODR/issues">issue</a> and we'll work to get it corrected as quickly as possible. Also, if you think that you have an idea that might make GSODR better <a target="_blank" rel="nofollow" href="https://github.com/ropensci/GSODR/issues">let us know</a> that too.</p>
<h2>Acknowledgments</h2>
<p>We're grateful to Jeff Hanson and Dillon Niederhut who took the time to <a target="_blank" rel="nofollow" href="https://github.com/ropensci/onboarding/issues/79">review</a> <em>GSODR</em> as a part of the rOpenSci <a target="_blank" rel="nofollow" href="https://github.com/ropensci/onboarding">onboarding process</a> and the <a target="_blank" rel="nofollow" href="https://doi.org/10.21105%2Fjoss.00177">related paper</a> published in the Journal of Open Source Software (Sparks <em>et al.</em>, 2017). Their suggestions greatly improved this package. Also, thanks to Scott Chamberlain for his editorial comments on this blog post, including spelling corrections to his name.</p>
<h1>References</h1>
<p>Adam H Sparks, Tomislav Hengl and Andrew Nelson (2017). GSODR: Global Summary Daily Weather Data in R. The Journal of Open Source Software, 2(10). DOI: 10.21105/joss.00177. URL: <a target="_blank" rel="nofollow" href="https://doi.org/10.21105/joss.00177">https://doi.org/10.21105/joss.00177</a></p>
<h1>Notes</h1>
<div>
<hr>
<ol>
<li>
<p>Formerly the National Climatic Data Center (NCDC) <a rev="footnote" target="_blank" rel="nofollow" href="http://ropensci.org/blog/blog/2017/04/04/gsodr#fnref1">↩</a></p>
</li>
</ol>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ropensci.org/blog/blog/2017/04/04/gsodr"> rOpenSci Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/19TLllpzHeg"></a></div>