This R script was developed by Samuel Safran to generate personal maps of potential lifer birds using ebirdst and terra. See his post here for overview: https://smsfrn.github.io/posts/2024/01/lifer-mapper/
This repository is a fork of smsfrn/lifeR.
| File | Description |
|---|---|
LifeR_US.R |
Current working version — all improvements applied, US/CONUS |
LifeR_NL.R |
Netherlands adaptation of the same improved script |
install_packages.R |
Standalone R package installer |
config_local.R.example |
Template for local API key config (copy → config_local.R) |
All changes are relative to Sam Safran's original code at smsfrn/lifeR.
- User R library path — added
.libPaths()so packages installed to~/R/libraryare found without modifying system R - API keys removed from source — keys are now loaded from a gitignored
config_local.R(see setup below); no credentials are ever committed - Project-local eBird S&T cache —
EBIRDST_DATA_DIRset todata/ebirdst/inside the project folder so rasters are shared with any Python tools in the same workspace; not stored in the hidden per-user R directory - NaturalEarth data cached locally —
ne_download()result saved tone_admin1_large.rdson first run and reloaded instantly on reruns; avoids repeated network calls install_packages.R— standalone installer script listing all required packages and system library dependencies
separate(..., fill = "right")— suppresses spurious NA warning for country-only region codes (e.g."US")mapview::mapview()call removed — was interactive-only; blocked unattended/scripted runs- Robust species matching —
sp_ebst_for_runis now built with a union join on both common name and species code, then deduplicated; catches taxonomy mismatches betweenrebirdandebirdst_runs(e.g. "Common Hoopoe" vs "Eurasian Hoopoe") - Additional exclusions —
rocpigandcompeaexcluded from runs in addition tolaugulandyebsap-example; these are introduced/domestic species with eBird S&T models that are not meaningful lifers - Week date labels decoupled from raster objects — saved to a character vector before rasters are freed, so the rendering loop does not rely on a live
SpatRasterobject
- RAM availability check — aborts before loading rasters if available RAM is below a resolution-dependent threshold (150 GB for 3km, 20 GB for 9km, 4 GB for 27km); prevents OOM crashes
terraOptions(memfrac = 0.7)— limits terra's in-process memory ceiling to 70% of total RAMcheck_memory_pressure()watchdog — checked at three points (after raster load, after crop+mask, after accumulation); raises an error with a clear message if RSS exceeds 85% of total RAM- Free large raster stacks after accumulation —
rm(occ_combined, occ_crop_combined); gc()releases ~40–120 GB (resolution-dependent) before the rendering loop; the original code held all species rasters in memory for the full duration of the run - Disk space guard — aborts if free disk space is below 2 GB before rendering begins
- eBird S&T download skip — validates that the local
.tifis present and readable before attempting any download; completely skips species already cached - Combined crop+mask in one pass — single
terra::mask(terra::crop(...))call per species instead of two sequentialsapplypasses - Hardware detection — physical and logical core count detected at startup;
n_workerscomputed for future parallel steps - Skip existing outputs — weekly PNG frames and the final animated GIF are skipped if they already exist; safe to resume interrupted runs or rerun at a different resolution
load_raster_safe()— wrapsload_raster()in atryCatch; logs a warning and skips the species instead of halting the whole run if a raster fails to load- Elapsed-time logging — all major phases (raster load, crop+mask, accumulation, reprojection, rendering) are timed and printed
- Progress messages throughout — step counts, species totals, and memory/disk status printed at each stage
magickreplaced bygifski—image_write_gif()(gifski backend) instead ofimage_animate()+image_write(); eliminates an ImageMagick segfault and pixel-cache exhaustion that occurs when assembling large animated GIFsmapviewremoved — interactive call was removed; no longer a dependency
LifeR_NL.R— Netherlands adaptation; uses NL regional checklist and appropriate CRS/boundary
sudo apt-get install -y libharfbuzz-dev libfribidi-devsource("install_packages.R")You need two keys:
| Key | Purpose | Request at |
|---|---|---|
| eBird Status & Trends key | Download occurrence rasters | https://ebird.org/st/request |
| eBird API key | Fetch regional species lists via rebird |
https://ebird.org/api/keygen |
Copy config_local.R.example to config_local.R and fill in your keys:
cp config_local.R.example config_local.RThen edit config_local.R:
ebirdst_key <- "YOUR_EBIRDST_KEY_HERE"
ebird_api_key <- "YOUR_EBIRD_API_KEY_HERE"config_local.R is gitignored and will never be committed.
Export your eBird data from https://ebird.org/downloadMyData and place the CSV as MyEBirdData.csv in the project directory.
Edit the parameters at the top of LifeR_US.R (region, resolution, theme, etc.), then:
Rscript LifeR_US.ROutput is saved to Results/<username>/<region>/<needs_type>/<resolution>/.
Original code by Sam Safran — https://github.com/smsfrn/lifeR
Fork maintained by @hydrospheric0