Remy M
Remy M

Reputation: 619

Obtaining rnoaa precipitation data in parallel with foreach

I am trying to obtain monthly maximum precipitation data using the rnoaa package. The data can only be obtained one station at a time, so I'd like to parallelize this step. I tried the following foreach procedure (with just 10 stations) but the resulting data frame has 0 rows and columns. What can I change to obtain the data? Thanks in advance!

Note: An API key can be obtained here: https://www.ncdc.noaa.gov//cdo-web/token

library(rnoaa)
library(dplyr)
library(doParallel)
library(foreach)

options(noaakey= "PUT KEY HERE")

stations <- ghcnd_stations()
states <- c("DE","SC","NC","VA","MD","PA")

stations <- stations %>% filter(state %in% states)
unique_stations <- unique(stations$id)

cl <- makeCluster(12, setup_timeout = 0.5)
registerDoParallel(cl)

resultdf <- foreach(i = 1:10,.combine=rbind) %dopar%{
  out <- data.frame()
  for(j in 1950:2023){
    tryCatch(
      expr = {
        pw_prcp_dat <- 
          meteo_pull_monitors(
            monitors = unique_stations[i],
            date_min=paste0(j,"-01-01"),
            date_max = paste0(j,"-12-31"),
            var = "PRCP"
          )
        
        df <- pw_prcp_dat
        df$month <- as.data.frame(do.call(rbind, strsplit(as.character(df$date), "-")))[,2]
        
        aggregate <- df %>%
          group_by(month) %>%
          summarise_at(vars(prcp), list(name = max), na.rm=T)
        
        aggregate$year <- j
        aggregate$id <- unique_stations[i]
        aggregate$latitude <- stations$latitude[stations$id==unique_stations[i]][1]
        aggregate$longitude <- stations$longitude[stations$id==unique_stations[i]][1]
        out <- rbind(out,aggregate)
      },
      error = function(e){
        message('Caught an error!')
        print(e)
      })
  }
  out
}

Upvotes: 0

Views: 60

Answers (0)

Related Questions