## Process precip data for Nuuk ## Packages library("tibble") library("readr") library("tidyr") library("curl") ## Data - data are at ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/v2/v2.prcp.Z ## file is compressed url <- "ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/v2/v2.prcp.Z" tmp <- "./v2.prcp.Z" curl_download(url, tmp, quiet = FALSE) ## At this point you'll need to uncompress the x-compress file you just downloaded, ## and extract the `v2.prcp` file to the working (or other) directory. ## There is an R package `uncompress` that could do this for you, but it was removed ## from CRAN at some point; the last version archived there is from 2012. path.2.v2.prcp <- "./v2.prcp" ## data have 16 chars, then twelve groups of 5 chars ## the first 16 chars contain relevant info and need extracting into separate variables on reading precip <- read_fwf(path.2.v2.prcp, fwf_widths(c(3, 5, 3, 1, 4, rep(5, 12)), col_names = c("Country","WMOID","Modifier","Duplicate","Year", month.abb)), na = c("-9999","-8888")) # -8888 means a trace, which I ignore (naughty!) # -9999 is straight up missing ## subset only the bits we want precip <- precip[precip$WMOID == 4250L, ] # 4250 is the WMO station ID for Nuuk (Godthaab) ## gather the month cols into a single key value pair precip <- gather(precip, Month, Precip, -Country, -WMOID, -Modifier, -Duplicate, -Year) ## add a numeric column for month months <- 1:12 names(months) <- month.abb precip <- add_column(precip, nMonth = unname(months[precip$Month]), .after = "Month") ## Drop some other stuff precip <- precip[, c("WMOID", "Year", "Month", "nMonth", "Precip")] ## Save this to RDS write_rds(precip, "./nuuk-precip-data.rds")