rrnoaa

How to download precipitation data using rnoaa


I am new to the 'rnoaa' R package. I am wondering how do I find stationsid names to identify stations. I am interested in downloading hourly or daily precipitation data from 2011 to 2020 from the Prince William Sound Alaska area. I looked here: https://www.ncdc.noaa.gov/cdo-web/search but it seems to have data only up to 2014. Could someone give me a hint on what rnoaa function to use to download the desired rainfall data? I read that the following rnoaa function:

cpc_prcp(date = "1998-04-23", drop_undefined = TRUE) 

However, I don't know what to include inside the function to get the data that I am looking for and also the range of dates (2011 to 2020)


Solution

  • You could try this workflow:

    An internet search gives the latitude and longitude for Prince William Sound Alaska area.

    library(rnoaa)
    
    # create a data frame for Prince William latitude and longitude
    lat_lon_df <- data.frame(id = "pw",
                             lat = 60.690545,
                             lon = -147.097055)
    
    # find 10 closest monitors to  Prince William
    mon_near_pw <- 
      meteo_nearby_stations(
      lat_lon_df = lat_lon_df,
      lat_colname = "lat",
      lon_colname = "lon",
      var = "PRCP",
      year_min = 2011,
      year_max = 2020,
      limit = 10,
    )
    
    
    mon_near_pw
    #> $pw
    #> # A tibble: 10 x 5
    #>    id          name                      latitude longitude distance
    #>    <chr>       <chr>                        <dbl>     <dbl>    <dbl>
    #>  1 USC00501240 CANNERY CREEK                 61.0     -148.     42.9
    #>  2 USC00509747 WALLY NOERENBERG HATCHERY     60.8     -148.     55.1
    #>  3 USS0048L06S Esther Island                 60.8     -148.     55.3
    #>  4 USC00505604 MAIN BAY                      60.5     -148.     57.6
    #>  5 USS0046M04S Sugarloaf Mtn                 61.1     -146.     61.1
    #>  6 USC00509687 VALDEZ                        61.1     -146.     62.4
    #>  7 USW00026442 VALDEZ WSO                    61.1     -146.     63.4
    #>  8 US1AKVC0005 VALDEZ 3.6 ENE                61.1     -146.     66.3
    #>  9 USC00509685 VALDEZ AIRPORT                61.1     -146.     67.3
    #> 10 USC00502179 CORDOVA WWTP                  60.5     -146.     74.0
    
    # extract precipitation data for the first location
    pw_prcp_dat <- 
      meteo_pull_monitors(
      monitors = mon_near_pw$pw$id[1],
      date_min = "2011-01-01",
      date_max = "2020-12-31",
      var = "PRCP"
    )
    
    head(pw_prcp_dat)
    #> # A tibble: 6 x 3
    #>   id          date        prcp
    #>   <chr>       <date>     <dbl>
    #> 1 USC00501240 2011-01-01   704
    #> 2 USC00501240 2011-01-02   742
    #> 3 USC00501240 2011-01-03   211
    #> 4 USC00501240 2011-01-04   307
    #> 5 USC00501240 2011-01-05   104
    #> 6 USC00501240 2011-01-06     0
    
    # out of curiosity have plotted monthly summary of precipitation.
    # For metadata see: https://www1.ncdc.noaa.gov/pub/data/ghcn/daily/readme.txt
    # PRCP = Precipitation (tenths of mm)
    
    library(dplyr)
    library(lubridate)
    library(ggplot2)
    
    pw_prcp_dat %>% 
      mutate(year = year(date),
             month = month(date)) %>%
      group_by(year, month) %>% 
      summarise(prcp = sum(prcp, na.rm = TRUE) / 10) %>% 
      ggplot(aes(factor(month), prcp))+
      geom_col()+
      facet_wrap(~year)+
      labs(y = "Precipitation [mm]",
           x = "Month")+
      theme_bw()
    

    Created on 2021-08-22 by the reprex package (v2.0.0)