{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "kernelspec": { "name": "ir", "display_name": "R", "language": "R" }, "language_info": { "name": "R" }, "colab": { "provenance": [] } }, "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 0 · Check your workspace\n", "**SoilFER Training** · [Course page](https://training.yigini.net/modules/00-welcome/)\n", "\n", "Run the **Setup** cell, then the check cells, with **Shift + Enter**.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "# ==== SoilFER · Setup (run first) ==============================================\n", "# Works in the SoilFER Lab (everything pre-installed) and in any other Jupyter with R\n", "on_colab <- nzchar(Sys.getenv(\"COLAB_RELEASE_TAG\")) || dir.exists(\"/content/sample_data\")\n", "if (on_colab) options(repos = c(CRAN = \"https://cloud.r-project.org\"))\n", "options(timeout = 3600)\n", "root <- if (on_colab) \"/content/SoilFER-Training-Resources\" else\n", " Sys.getenv(\"SOILFER_ROOT\", path.expand(\"~/SoilFER-Training-Resources\"))\n", "Sys.setenv(SOILFER_ROOT = root)\n", "\n", "# 1. Training project (scripts, small data, outputs, assignments)\n", "if (!dir.exists(root))\n", " system(paste(\"git clone --depth 1 https://github.com/SoilFER/SoilFER-Training-Resources\", root))\n", "\n", "# 2. R packages (already installed in the SoilFER Lab; Colab installs ready-made binaries)\n", "pkgs <- c(\"terra\", \"sf\", \"tidyverse\", \"readxl\")\n", "need <- setdiff(pkgs, rownames(installed.packages()))\n", "if (length(need)) install.packages(need)\n", "\n", "# 3. Course rasters + MIR spectra (Google Drive folder of the SoilFER training, ≈1.2 GB)\n", "td <- file.path(root, \"01_data/module1/training_data\")\n", "if (!file.exists(file.path(td, \"MIR_KANSAS_data.xlsx\"))) {\n", " system(\"python3 -m pip -q install gdown\")\n", " drv <- file.path(dirname(root), \"soilfer_drive\")\n", " system(paste(\"python3 -m gdown --folder --quiet https://drive.google.com/drive/folders/1K7tq9zX5HsqbqWcNoT27WtfPtehcKBCu -O\", shQuote(drv)))\n", " f <- list.files(drv, recursive = TRUE, full.names = TRUE)\n", " file.copy(f, td, overwrite = FALSE)\n", "}\n", "\n", "# Notebook display: plain tables/plots (no extra map widgets needed)\n", "options(jupyter.display_mimetypes = c(\"text/plain\", \"text/html\", \"text/markdown\", \"text/latex\", \"image/png\", \"image/svg+xml\"))\n", "\n", "setwd(root)\n", "cat(\"Ready. Working folder:\", getwd(), \"\\n\")\n", "missing <- setdiff(pkgs, rownames(installed.packages()))\n", "if (length(missing)) message(\"Not installed: \", paste(missing, collapse = \", \"))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. R and the spatial packages" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "R.version.string\n", "library(terra); library(sf); library(tidyverse); library(readxl)\n", "cat(\"terra\", as.character(packageVersion(\"terra\")), \"| GDAL\", sf::sf_extSoftVersion()[[\"GDAL\"]], \"\\n\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. The training project and data" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "list.files()\n", "list.files(\"01_data/module1/training_data\", pattern = \"\\\\.(tif|xlsx)$\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Soil data: KSSL topsoil table" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "kssl <- read_csv(\"03_outputs/module1/KSSL_DSM_0-30.csv\", show_col_types = FALSE)\n", "glimpse(kssl)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. A first map: elevation and soil sampling points" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "dem <- rast(\"01_data/module1/training_data/DEM_KANSAS.tif\")\n", "pts <- vect(kssl, geom = c(\"lon\", \"lat\"), crs = \"EPSG:4326\")\n", "pts <- project(pts, crs(dem))\n", "plot(dem, main = \"Kansas: elevation and KSSL sampling points\")\n", "points(pts, pch = 20, cex = .5)\n", "cat(\"All good, you are ready for Module 1.\\n\")\n" ] } ] }