6.3 (Aside): Download daily PRISM data files and save them by month in parallel

The following code downloads daily PRISM data files (covers the U.S.) and save them by month from 1990 to 2018 in parallel, which is used for section 6.2. I am by no means suggesting this is they way you should store daily PRISM data. You might want to save daily PRISM data by year as a GeoTiff file. That’s what you decide. Extracting values from a raster layer containing a month-worth of weather data for U.S. counties took 26.491 seconds as we saw earlier in section 6.2.2. I tried extracting from a RasterStack of daily PRISM data for one year (365 layers) using exact_extract(). The progress bar did not move at all for more than 10 minutes, so I stopped the process. For my computer, PRISM data stored by month is the right size.

#--- set your own path ---#
options(prism.path = "./Data/PRISM/")

month_year_data <- expand.grid(month  = 1:12, year = 1990:2018) %>% 
  data.table()

get_prism <- function(i, var_type) {

  print(paste0("working on ", i))

  temp_month <- month_year_data[i, month]
  temp_year <- month_year_data[i, year]

  monthly_tif_file <- paste0("./Data/PRISM/PRISM_", var_type, "_y", temp_year, "_m", temp_month, ".tif")

  if(file.exists(monthly_tif_file)){
    # do nothing
  } else { # download the data

    start_date <- as.Date(paste0("1/", temp_month, "/", temp_year), "%d/%m/%Y")

    if (temp_month < 12){

      end_date <- as.Date(paste0("1/", temp_month + 1, "/", temp_year), "%d/%m/%Y") - 1

    } else {
      end_date <- as.Date(paste0("1/", 1, "/", temp_year + 1), "%d/%m/%Y") - 1
    }
    
    get_prism_dailys(
      type = var_type,
      minDate = as.character(start_date),
      maxDate = as.character(end_date),
      keepZip = FALSE
    ) 

    dates_ls <- seq(start_date, end_date, "days") 

    dates_prism_txt <- str_remove_all(dates_ls, "-")

    folder_name <- paste0("PRISM_", var_type, "_stable_4kmD2_", dates_prism_txt, "_bil") 
    file_name <- paste0("PRISM_", var_type, "_stable_4kmD2_", dates_prism_txt, "_bil.bil") 
    file_path <- paste0("./Data/PRISM/", folder_name, "/", file_name)

    #--- save as a multi-band GeoTiff file ---#
    temp_ml <- rast(stack(file_path))

    writeRaster(temp_ml, paste0("./Data/PRISM/PRISM_", var_type, "_y", temp_year, "_m", temp_month, ".tif"), overwrite = T)

    #--- delete all the downloaded files ---#
    unlink(paste0("./Data/PRISM/", folder_name), recursive = TRUE)
  }
}

mclapply(
  1:nrow(month_year_data), 
  function (x) get_prism(x, "ppt"),
  mc.cores = num_cores
)