Ocean / ClimateAndMSP /scripts /download_data.R
Srijan-Upadhyay's picture
Add files using upload-large-folder tool
e618ab8 verified
Raw
History Blame Contribute Delete
6.43 kB
#!/usr/bin/env Rscript
# Download publicly available data for ClimateAndMSP project
# This script attempts to download OceanAdapt, BCO-DMO, NatCap, and EEZ data
library(data.table)
library(utils)
# Create directory structure
dirs_to_create <- c(
"dataDL/oceanadapt",
"dataDL/bco_dmo",
"dataDL/natcap",
"dataDL/eez",
"temp",
"NatCap_temp"
)
for (d in dirs_to_create) {
if (!dir.exists(d)) {
dir.create(d, recursive = TRUE, showWarnings = FALSE)
cat("Created:", d, "\n")
}
}
# Set timeout for large files
options(timeout = 600)
# ============================================================================
# 1. OceanAdapt data (Zenodo)
# ============================================================================
cat("\n=== Downloading OceanAdapt data ===\n")
oceanadapt_url <- "https://zenodo.org/record/3890214/files/all-regions-full.rds"
oceanadapt_file <- "dataDL/oceanadapt/all-regions-full.rds"
if (!file.exists(oceanadapt_file)) {
cat("Downloading OceanAdapt RDS file (~500MB)...\n")
tryCatch({
download.file(oceanadapt_url, oceanadapt_file, mode = "wb", quiet = FALSE)
cat("✓ OceanAdapt data downloaded successfully\n")
}, error = function(e) {
cat("✗ Failed to download OceanAdapt:", conditionMessage(e), "\n")
})
} else {
cat("✓ OceanAdapt data already exists\n")
}
# ============================================================================
# 2. BCO-DMO Morley et al. species projections
# ============================================================================
cat("\n=== Downloading BCO-DMO Morley species projections ===\n")
# Create a data.frame of BCO-DMO files to download
bco_dmo_files <- data.frame(
url = c(
"https://www.bco-dmo.org/sites/default/files/attachment/Morley_2018_projection_OA_pH_5.csv.gz",
"https://www.bco-dmo.org/sites/default/files/attachment/Morley_2018_projection_O2_5.csv.gz",
"https://www.bco-dmo.org/sites/default/files/attachment/Morley_2018_projection_SST_5.csv.gz",
"https://www.bco-dmo.org/sites/default/files/attachment/Morley_2018_projection_MLD_5.csv.gz"
),
filename = c(
"dataDL/bco_dmo/Morley_2018_projection_OA_pH_5.csv.gz",
"dataDL/bco_dmo/Morley_2018_projection_O2_5.csv.gz",
"dataDL/bco_dmo/Morley_2018_projection_SST_5.csv.gz",
"dataDL/bco_dmo/Morley_2018_projection_MLD_5.csv.gz"
),
stringsAsFactors = FALSE
)
for (i in 1:nrow(bco_dmo_files)) {
url <- bco_dmo_files$url[i]
file <- bco_dmo_files$filename[i]
if (!file.exists(file)) {
cat("Downloading:", basename(file), "\n")
tryCatch({
download.file(url, file, mode = "wb", quiet = FALSE)
cat("✓", basename(file), "downloaded\n")
}, error = function(e) {
cat("✗ Failed:", conditionMessage(e), "\n")
})
} else {
cat("✓", basename(file), "already exists\n")
}
}
# ============================================================================
# 3. EEZ (Exclusive Economic Zones) shapefiles
# ============================================================================
cat("\n=== Downloading EEZ shapefiles ===\n")
eez_url <- "https://www.marineregions.org/download_file.php?name=eez.zip"
eez_zip <- "dataDL/eez/eez.zip"
if (!file.exists(eez_zip)) {
cat("Downloading EEZ shapefiles (~50MB)...\n")
tryCatch({
download.file(eez_url, eez_zip, mode = "wb", quiet = FALSE)
unzip(eez_zip, exdir = "dataDL/eez")
cat("✓ EEZ shapefiles extracted\n")
}, error = function(e) {
cat("✗ Failed to download EEZ:", conditionMessage(e), "\n")
})
} else if (!dir.exists("dataDL/eez/eez")) {
cat("Extracting EEZ shapefiles...\n")
unzip(eez_zip, exdir = "dataDL/eez")
}
# ============================================================================
# 4. Natural Capital / InVEST reference data (if available)
# ============================================================================
cat("\n=== Checking for NatCap reference data ===\n")
# Create placeholder for NatCap wind/wave energy outputs
# (Note: Full InVEST runs produce these; we'll document expected structure)
natcap_regions <- c("westcoastwind", "eastcoastwind", "gulfwind", "alaskawind",
"westcoastwave", "eastcoastwave", "gulfwave")
for (region in natcap_regions) {
dir_path <- file.path("NatCap_temp", region, "output")
if (!dir.exists(dir_path)) {
dir.create(dir_path, recursive = TRUE, showWarnings = FALSE)
cat("Created:", dir_path, "\n")
}
}
cat("NatCap_temp directory structure created (actual .tif files require InVEST computation)\n")
# ============================================================================
# 5. WDPA (World Database of Protected Areas) - requires registration
# ============================================================================
cat("\n=== WDPA Protected Areas Database ===\n")
cat("Note: WDPA requires free registration at https://www.protectedplanet.net/\n")
cat("Download 'WDPA_Feb2025_marine_shp.zip' and extract to: dataDL/WDPA_marine/\n")
cat("Instructions:\n")
cat(" 1. Visit: https://www.protectedplanet.net/en/thematic-areas/wdpa?tab=data-download\n")
cat(" 2. Create free account\n")
cat(" 3. Download 'World Database of Protected Areas (WDPA) - February 2025' (marine)\n")
cat(" 4. Extract to: dataDL/WDPA_marine/\n\n")
# ============================================================================
# Summary
# ============================================================================
cat("\n=== Download Summary ===\n")
cat("Directory structure created at: dataDL/ and NatCap_temp/\n")
cat("Successfully downloaded:\n")
downloaded_files <- list.files(recursive = TRUE, path = "dataDL", full.names = TRUE)
if (length(downloaded_files) > 0) {
for (f in downloaded_files) {
size_mb <- file.size(f) / (1024^2)
cat(sprintf(" ✓ %s (%.1f MB)\n", f, size_mb))
}
} else {
cat(" (No files downloaded yet - check network and URLs)\n")
}
cat("\nNext steps:\n")
cat(" 1. Check output above for any failed downloads\n")
cat(" 2. Manually download WDPA from ProtectedPlanet (requires registration)\n")
cat(" 3. Verify files in dataDL/ and NatCap_temp/ directories\n")
cat(" 4. Run: Rscript scripts/validate_inputs.R\n")
cat(" 5. Run: Rscript code/0.2_averageSBT.r (first step in pipeline)\n\n")