|
| 1 | +# Create example data for cd package tests and vignettes |
| 2 | +# |
| 3 | +# Crops real ERA5-Land NC data to a small anonymous bbox. |
| 4 | +# Run interactively — requires bc_climate_anomaly repo at ../bc_climate_anomaly/ |
| 5 | +# |
| 6 | +# Output: |
| 7 | +# inst/extdata/example_aoi.gpkg — simple bbox polygon (no place names) |
| 8 | +# inst/extdata/example_climate.tif — tmean annual, 10 years, COG format |
| 9 | +# inst/extdata/example_catalog.json — minimal STAC catalog pointing to the COG |
| 10 | + |
| 11 | +library(terra) |
| 12 | +library(sf) |
| 13 | +library(jsonlite) |
| 14 | + |
| 15 | +# -- Source data --------------------------------------------------------------- |
| 16 | +nc_path <- "../bc_climate_anomaly/ano_clm_trn_data/tmean_ano_annual_1950_+_res25.nc" |
| 17 | +stopifnot(file.exists(nc_path)) |
| 18 | + |
| 19 | +# -- Create AOI (anonymous bbox) ---------------------------------------------- |
| 20 | +# Small area in northern BC — just coordinates, no place names |
| 21 | +bbox <- c(xmin = -126.75, ymin = 54.1, xmax = -125.75, ymax = 54.7) |
| 22 | +aoi <- st_as_sfc(st_bbox(bbox, crs = 4326)) |
| 23 | +aoi <- st_sf(geometry = aoi) |
| 24 | + |
| 25 | +sf::st_write(aoi, "inst/extdata/example_aoi.gpkg", delete_dsn = TRUE, quiet = TRUE) |
| 26 | +message("Wrote: inst/extdata/example_aoi.gpkg") |
| 27 | + |
| 28 | +# -- Crop and subset raster --------------------------------------------------- |
| 29 | +r <- rast(nc_path) |
| 30 | + |
| 31 | +# Use first 10 years (bands 1-10) for manageable size |
| 32 | +r_sub <- r[[1:10]] |
| 33 | + |
| 34 | +# Crop to AOI |
| 35 | +r_crop <- crop(r_sub, vect(aoi), snap = "out") |
| 36 | + |
| 37 | +# Set band names to years |
| 38 | +names(r_crop) <- 1951:1960 |
| 39 | + |
| 40 | +# Write as COG |
| 41 | +writeRaster( |
| 42 | + r_crop, |
| 43 | + "inst/extdata/example_climate.tif", |
| 44 | + filetype = "COG", |
| 45 | + overwrite = TRUE, |
| 46 | + gdal = c("COMPRESS=DEFLATE") |
| 47 | +) |
| 48 | +message("Wrote: inst/extdata/example_climate.tif (", |
| 49 | + file.size("inst/extdata/example_climate.tif"), " bytes)") |
| 50 | + |
| 51 | +# -- Create minimal STAC catalog ---------------------------------------------- |
| 52 | +catalog <- list( |
| 53 | + type = "Catalog", |
| 54 | + id = "cd-example", |
| 55 | + stac_version = "1.0.0", |
| 56 | + description = "Example climate data for cd package tests", |
| 57 | + links = list( |
| 58 | + list(rel = "root", href = "./example_catalog.json", type = "application/json"), |
| 59 | + list(rel = "item", href = "#tmean-annual", type = "application/json") |
| 60 | + ), |
| 61 | + items = list( |
| 62 | + list( |
| 63 | + type = "Feature", |
| 64 | + stac_version = "1.0.0", |
| 65 | + id = "tmean-annual", |
| 66 | + geometry = NULL, |
| 67 | + bbox = as.numeric(bbox), |
| 68 | + properties = list( |
| 69 | + `cd:variable` = "tmean", |
| 70 | + `cd:period` = "annual", |
| 71 | + datetime = NULL, |
| 72 | + start_datetime = "1951-01-01T00:00:00Z", |
| 73 | + end_datetime = "1960-12-31T23:59:59Z" |
| 74 | + ), |
| 75 | + links = list(), |
| 76 | + assets = list( |
| 77 | + data = list( |
| 78 | + href = "./example_climate.tif", |
| 79 | + type = "image/tiff; application=geotiff; profile=cloud-optimized", |
| 80 | + title = "Mean temperature annual values" |
| 81 | + ) |
| 82 | + ) |
| 83 | + ) |
| 84 | + ) |
| 85 | +) |
| 86 | + |
| 87 | +write_json(catalog, "inst/extdata/example_catalog.json", pretty = TRUE, auto_unbox = TRUE) |
| 88 | +message("Wrote: inst/extdata/example_catalog.json") |
| 89 | +message("Done.") |
0 commit comments