|
| 1 | +#!/usr/bin/env Rscript |
| 2 | + |
| 3 | +# Get hourly weather data from Caladapt's WRF climate scenarios |
| 4 | +# for all grid cells covering a set of parcels |
| 5 | +# |
| 6 | +# Caution: Expect long runtimes -- my statewide download took on the order of |
| 7 | +# 10 min per model-year = ~4 hours per model when fetching 203 grid cells |
| 8 | +# from 2024 to 2051. |
| 9 | + |
| 10 | +# TODO Consider downloading models in parallel via furrr::future_walk, |
| 11 | +# copying the approach in ERA5_met_extract.R. |
| 12 | + |
| 13 | +options <- list( |
| 14 | + optparse::make_option("--parcel_geom_file", |
| 15 | + default = "data_raw/management/crops/v4.1.2/parcels-consolidated.gpkg", |
| 16 | + help = "file containing polygons defining the area of interest" |
| 17 | + ), |
| 18 | + optparse::make_option("--output_dir", |
| 19 | + default = "data_raw/wrf_45km_nc", |
| 20 | + help = paste( |
| 21 | + "Directory to write output.", |
| 22 | + "It will contain one subdir per grid cell downloaded,", |
| 23 | + "each with one netcdf per year per model.", |
| 24 | + "Will also contain a `parcel_to_grid` CSV mapping each parcel id to", |
| 25 | + "a CalAdapt grid cell." |
| 26 | + ) |
| 27 | + ), |
| 28 | + optparse::make_option("--start_year", |
| 29 | + default = 2024, |
| 30 | + help = "First year of projections to retrieve" |
| 31 | + ), |
| 32 | + optparse::make_option("--end_year", |
| 33 | + default = 2024, |
| 34 | + help = "Last year of projections to retrieve" |
| 35 | + ), |
| 36 | + optparse::make_option("--models", |
| 37 | + default = paste0( |
| 38 | + "CESM2,CNRM-ESM2-1,EC-Earth3,EC-Earth3-Veg,", |
| 39 | + "FGOALS-g3,MIROC6,MPI-ESM1-2-HR,TaiESM1" |
| 40 | + ), |
| 41 | + help = paste( |
| 42 | + "Comma-separated list of GCMs to retrieve.", |
| 43 | + "See `caladaptaer::cae_models(\"WRF\")` for valid names." |
| 44 | + ) |
| 45 | + ), |
| 46 | + optparse::make_option("--scenario", |
| 47 | + default = "ssp370", |
| 48 | + help = paste( |
| 49 | + "Climate scenario. See `caladaptaer::cae_scenarios(\"WRF\")`", |
| 50 | + "for valid values." |
| 51 | + ) |
| 52 | + ), |
| 53 | + optparse::make_option("--resolution", |
| 54 | + default = "d01", |
| 55 | + help = "Spatial resolution: 'd01' for 45km, 'd02' for 9km, 'd03' for 3km." |
| 56 | + ) |
| 57 | +) |> |
| 58 | + # Show default values in help message |
| 59 | + purrr::modify(\(x) { |
| 60 | + x@help <- paste(x@help, "[default: %default]") |
| 61 | + x |
| 62 | + }) |
| 63 | + |
| 64 | +args <- optparse::OptionParser(option_list = options) |> |
| 65 | + optparse::parse_args() |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +# Needs caladaptaer, available via |
| 70 | +# remotes::install_github("lebauerapproach/caladaptaer") |
| 71 | +# install.packages("CFtime") |
| 72 | + |
| 73 | +# library(caladaptaer) |
| 74 | +# library(tidyverse) |
| 75 | + |
| 76 | +models <- strsplit(args$models, ",")[[1]] |> |
| 77 | + trimws() |
| 78 | + |
| 79 | +centroids <- terra::vect(args$parcel_geom_file) |> |
| 80 | + _[,"parcel_id"] |> |
| 81 | + terra::centroids() |> |
| 82 | + terra::project("epsg:4326") |> |
| 83 | + as.data.frame(geom="XY") |> |
| 84 | + dplyr::rename(lon = x, lat = y) |
| 85 | + |
| 86 | +# Easiest current way to get a reference grid: fetch one timepoint with no |
| 87 | +# location specified |
| 88 | +# (future caladaptaer releases may add a more streamlined catalog lookup) |
| 89 | +caladapt_ref <- caladaptaer::cae_fetch( |
| 90 | + variable = "t2", |
| 91 | + model = "CESM2", |
| 92 | + scenario = args$scenario, |
| 93 | + start_time = "2050-07-01T00:00:00", |
| 94 | + end_time = "2050-07-01T00:00:00", |
| 95 | + resolution = args$resolution, |
| 96 | + timescale = "1hr" |
| 97 | +) |
| 98 | + |
| 99 | +gridid <- caladaptaer::cae_grid_cells(centroids, caladapt_ref) |
| 100 | +if (!dir.exists(args$output_dir)) { |
| 101 | + dir.create(args$output_dir, recursive = TRUE) |
| 102 | +} |
| 103 | +gridid |> |
| 104 | + dplyr::mutate( |
| 105 | + dplyr::across( |
| 106 | + dplyr::contains(c("lon", "lat")), |
| 107 | + \(x) round(x, 5) |
| 108 | + ) |
| 109 | + ) |> |
| 110 | + write.csv( |
| 111 | + file = file.path( |
| 112 | + args$output_dir, |
| 113 | + paste0("parcel_to_grid_", args$resolution, ".csv") |
| 114 | + ), |
| 115 | + row.names = FALSE |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +cells_to_fetch <- gridid |> |
| 120 | + dplyr::distinct(cell_id, cell_lon, cell_lat) |> |
| 121 | + dplyr::rename(lon = cell_lon, lat = cell_lat, site_id = cell_id) |
| 122 | + |
| 123 | +get_one_model <- function(modelname) { |
| 124 | + caladaptaer::cae_build_met_drivers( |
| 125 | + sites = cells_to_fetch, |
| 126 | + model = modelname, |
| 127 | + scenario = args$scenario, |
| 128 | + start_year = args$start_year, |
| 129 | + end_year = args$end_year, |
| 130 | + outdir = args$output_dir, |
| 131 | + resolution = args$resolution |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +models |> |
| 136 | + purrr::walk(get_one_model) |
0 commit comments