Ocean / ClimateAndMSP /scripts /validate_inputs.R
Srijan-Upadhyay's picture
Add files using upload-large-folder tool
e618ab8 verified
Raw
History Blame Contribute Delete
12.3 kB
#!/usr/bin/env Rscript
# Validate and report on required inputs for ClimateAndMSP project
library(data.table)
cat("
╔══════════════════════════════════════════════════════════════════════════════╗
β•‘ ClimateAndMSP INPUT VALIDATION REPORT β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
")
# Function to check file existence
check_file <- function(path, description, required = TRUE) {
exists <- file.exists(path)
status <- if (exists) "βœ“ FOUND" else "βœ— MISSING"
severity <- if (required) "[REQUIRED]" else "[OPTIONAL]"
cat(sprintf("%s %s: %s\n", status, severity, description))
return(exists)
}
# Function to check directory
check_dir <- function(path, description, required = TRUE) {
exists <- dir.exists(path)
status <- if (exists) "βœ“ EXISTS" else "βœ— MISSING"
severity <- if (required) "[REQUIRED]" else "[OPTIONAL]"
cat(sprintf("%s %s: %s\n", status, severity, description))
return(exists)
}
# ============================================================================
# Section 1: Base infrastructure
# ============================================================================
cat("\n[1] DIRECTORIES\n")
cat("─────────────────────────────────────────────────────────────────────────────\n")
dirs_ok <- c(
check_dir("code/", "code/ (scripts)"),
check_dir("data/", "data/ (static data)"),
check_dir("output/", "output/ (results)"),
check_dir("dataDL/", "dataDL/ (downloaded data)")
)
# ============================================================================
# Section 2: Data in output/ (usually pre-computed)
# ============================================================================
cat("\n[2] OUTPUT FILES (Pre-computed, mostly available)\n")
cat("─────────────────────────────────────────────────────────────────────────────\n")
output_files <- c(
"output/climatology.csv.gz",
"output/fishery_spps.csv",
"output/landgridpts_northamerica.csv",
"output/region_grid.csv.gz",
"output/wdpa_cov_by_grid0.05.csv.gz",
"output/grid0.05_cov_by_wdpa.csv.gz",
"output/turnover_by_CMSPgrid.csv",
"output/randMPAs_byBT.csv",
"output/wind_npv.csv.gz",
"output/wave_npv.csv.gz",
"output/goalsmetbymod_hist_all.csv",
"output/goalsmetbymod_2per_all.csv",
"output/goalsmetbyensemble_hist_all.csv",
"output/goalsmetbyensemble_2per_all.csv"
)
output_ok <- sapply(output_files, function(f) {
exists <- file.exists(f)
status <- if (exists) "βœ“" else "βœ—"
cat(sprintf(" %s %s\n", status, f))
return(exists)
})
# ============================================================================
# Section 3: Morley et al. species projections (CRITICAL)
# ============================================================================
cat("\n[3] MORLEY ET AL. SPECIES PROJECTIONS (dataDL/morley/)\n")
cat("─────────────────────────────────────────────────────────────────────────────\n")
morley_dir <- "dataDL/morley"
if (dir.exists(morley_dir)) {
morley_files <- list.files(morley_dir, recursive = TRUE)
if (length(morley_files) > 0) {
cat(sprintf(" βœ“ Found %d Morley projection files\n", length(morley_files)))
for (f in head(morley_files, 5)) {
cat(sprintf(" - %s\n", f))
}
if (length(morley_files) > 5) cat(sprintf(" ... and %d more\n", length(morley_files) - 5))
} else {
cat(" βœ— Directory exists but is empty\n")
}
} else {
cat(" βœ— MISSING: dataDL/morley/\n")
cat(" Required: Morley et al. 2018 species projection RData files\n")
cat(" Source: BCO-DMO (dataset ID 753124)\n")
cat(" URL: https://www.bco-dmo.org/dataset/753124\n")
}
# ============================================================================
# Section 4: WDPA marine protected areas (CRITICAL)
# ============================================================================
cat("\n[4] WDPA (WORLD DATABASE OF PROTECTED AREAS)\n")
cat("─────────────────────────────────────────────────────────────────────────────\n")
wdpa_dirs <- c(
"dataDL/WDPA/",
"dataDL/WDPA_marine/",
"dataDL/wdpa/"
)
wdpa_found <- FALSE
for (d in wdpa_dirs) {
if (dir.exists(d)) {
shp_files <- list.files(d, pattern = "\\.shp$", recursive = TRUE)
if (length(shp_files) > 0) {
cat(sprintf(" βœ“ Found WDPA shapefile in: %s\n", d))
wdpa_found <- TRUE
}
}
}
if (!wdpa_found) {
cat(" βœ— MISSING: WDPA marine protected areas shapefile\n")
cat(" Required: WDPA_Aug2019_marine-shapefile or similar\n")
cat(" Source: Protected Planet (https://www.protectedplanet.net/)\n")
cat(" Action: Download 'WDPA_marine' shapefile and extract to dataDL/WDPA/\n")
}
# ============================================================================
# Section 5: OceanAdapt species data (OPTIONAL)
# ============================================================================
cat("\n[5] OCEANADAPT DATA (dataDL/oceanadapt/)\n")
cat("─────────────────────────────────────────────────────────────────────────────\n")
oa_file <- "dataDL/oceanadapt/all-regions-full.rds"
if (file.exists(oa_file)) {
cat(" βœ“ all-regions-full.rds exists\n")
} else {
cat(" βœ— MISSING: all-regions-full.rds\n")
cat(" Source: Zenodo (https://zenodo.org/record/3890214)\n")
cat(" Status: OPTIONAL (needed for 1.4_compare_WDPA_projections_data.r)\n")
}
# ============================================================================
# Section 6: Temporary files (generated during pipeline)
# ============================================================================
cat("\n[6] TEMPORARY FILES (Generated during pipeline execution)\n")
cat("─────────────────────────────────────────────────────────────────────────────\n")
temp_files <- c(
"temp/SPsf2.rds" = "Species projection grid",
"temp/wdpa_by_grid0.05_intersect.rds" = "WDPA-grid intersection",
"temp/presmap_Atl_rcp26_2007-2020.csv.gz" = "Atlantic presence/absence map",
"temp/presmap_Pac_rcp26_2007-2020.csv.gz" = "Pacific presence/absence map",
"temp/biomassmap_Atl_rcp26_2007-2020.csv.gz" = "Atlantic biomass map",
"temp/biomassmap_Pac_rcp26_2007-2020.csv.gz" = "Pacific biomass map",
"temp/wdpaturnbyMPAbymod.csv.gz" = "MPA turnover by model",
"temp/wdpaturnbynetbymod.csv.gz" = "Network turnover by model"
)
temp_dir_exists <- dir.exists("temp")
cat(sprintf(" temp/ directory: %s\n", if (temp_dir_exists) "βœ“ EXISTS" else "βœ— MISSING"))
for (file in names(temp_files)) {
exists <- file.exists(file)
status <- if (exists) "βœ“" else "β—‹" # circle = will be generated
cat(sprintf(" %s %s (%s)\n", status, file, temp_files[file]))
}
# ============================================================================
# Section 7: NatCap data (InVEST outputs)
# ============================================================================
cat("\n[7] NATCAP / INVEST OUTPUTS\n")
cat("─────────────────────────────────────────────────────────────────────────────\n")
natcap_regions <- c("westcoastwind", "eastcoastwind", "gulfwind", "alaskawind",
"westcoastwave", "eastcoastwave", "gulfwave")
natcap_found <- 0
for (region in natcap_regions) {
tif_pattern <- file.path("NatCap_temp", region, "output", "*.tif")
tif_files <- list.files(file.path("NatCap_temp", region, "output"),
pattern = "\\.tif$", full.names = TRUE)
if (length(tif_files) > 0) {
cat(sprintf(" βœ“ %s: found %d .tif files\n", region, length(tif_files)))
natcap_found <- natcap_found + length(tif_files)
} else {
cat(sprintf(" β—‹ %s: (awaiting InVEST computation)\n", region))
}
}
cat(sprintf("\nTotal NatCap files found: %d\n", natcap_found))
# ============================================================================
# Section 8: Data in data/ directory
# ============================================================================
cat("\n[8] STATIC DATA (data/ directory)\n")
cat("─────────────────────────────────────────────────────────────────────────────\n")
data_files <- list.files("data/", recursive = TRUE)
if (length(data_files) > 0) {
cat(sprintf(" βœ“ Found %d files in data/ directory:\n", length(data_files)))
for (f in data_files) {
cat(sprintf(" - %s\n", f))
}
} else {
cat(" β—‹ data/ directory is empty (may not be needed)\n")
}
# ============================================================================
# Summary & Recommendations
# ============================================================================
cat("\n")
cat("╔══════════════════════════════════════════════════════════════════════════════╗\n")
cat("β•‘ SUMMARY & NEXT STEPS β•‘\n")
cat("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•\n\n")
missing_critical <- 0
if (!dir.exists("dataDL/morley")) {
cat("❌ CRITICAL: Morley et al. projections missing\n")
missing_critical <- missing_critical + 1
}
if (!check_file("dataDL/WDPA/WDPA_Aug2019_marine-shapefile/WDPA_Aug2019_marine-shapefile-polygons.shp",
"WDPA shapefile", required = FALSE)) {
cat("❌ CRITICAL: WDPA shapefile missing\n")
missing_critical <- missing_critical + 1
}
cat("\n")
if (missing_critical == 0) {
cat("βœ… All CRITICAL inputs are available!\n\n")
cat("Recommended next steps:\n")
cat(" 1. Run: Rscript code/1.0_processWDPA.r\n")
cat(" 2. Then: Rscript code/plot_figures.r (to generate visualizations)\n")
} else {
cat(sprintf("⚠️ %d critical input(s) are missing\n\n", missing_critical))
cat("To run the full pipeline, you need:\n")
cat(" 1. Morley et al. species projection files (from BCO-DMO)\n")
cat(" 2. WDPA marine shapefile (from ProtectedPlanet)\n")
cat("\nOptional (for extended analysis):\n")
cat(" 3. OceanAdapt RDS file (from Zenodo)\n")
cat(" 4. InVEST output .tif files (requires separate NatCap computation)\n")
}
cat("\n")
cat("═════════════════════════════════════════════════════════════════════════════════\n")