Skip to content

Commit

Permalink
Merge pull request #5 from bluegreen-labs/patch-hwsd-v2
Browse files Browse the repository at this point in the history
Patch hwsd v2
  • Loading branch information
khufkens committed Aug 6, 2023
2 parents af18a50 + 23ec5c2 commit 6575b1c
Show file tree
Hide file tree
Showing 18 changed files with 681 additions and 209 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
^CONDUCT\.md$
^NEWS\.md$
^docs$
^data-raw$
^analysis$
^pkgdown$
^LICENSE$
^.*-logo\..*$
Expand Down
85 changes: 20 additions & 65 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches:
- main
- master
branches: [main, master]
pull_request:
branches:
- main
- master
branches: [main, master]

name: R-CMD-check

Expand All @@ -22,73 +18,32 @@ jobs:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v1
- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-pandoc@v1

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Restore R package cache
if: runner.os != 'Windows'
uses: actions/cache@v2
- uses: r-lib/actions/setup-r-dependencies@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install system dependencies
if: runner.os == 'Linux'
run: |
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
- name: Install system dependencies on MacOS (X11, gdal)
if: runner.os == 'macOS'
run: |
brew install --cask xquartz
brew install pkg-config
brew install proj
brew install gdal
- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("rcmdcheck")
shell: Rscript {0}

- name: Check
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
run: |
options(crayon.enabled = TRUE)
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
shell: Rscript {0}
extra-packages: any::rcmdcheck
needs: check

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
- uses: r-lib/actions/check-r-package@v2
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
upload-snapshots: true
33 changes: 14 additions & 19 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ name: test-coverage

jobs:
test-coverage:
runs-on: macOS-latest
timeout-minutes: 120
runs-on: windows-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
PASS: ${{secrets.PASS}}

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v1

- uses: r-lib/actions/setup-pandoc@v1
- uses: r-lib/actions/setup-r@v2
- uses: r-lib/actions/setup-pandoc@v2

- name: Query dependencies
run: |
Expand All @@ -29,20 +30,14 @@ jobs:
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Restore R package cache
uses: actions/cache@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install system dependencies on MacOS (X11, gdal)
if: runner.os == 'macOS'
- name: Install system dependencies
if: runner.os == 'Linux'
run: |
brew install --cask xquartz
brew install pkg-config
brew install proj@7
brew install gdal
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
sudo apt-get install libgdal-dev libproj-dev libgeos-dev libudunits2-dev netcdf-bin libsodium-dev libsodium23
- name: Install dependencies
run: |
Expand All @@ -53,4 +48,4 @@ jobs:

- name: Test coverage
run: covr::codecov()
shell: Rscript {0}
shell: Rscript {0}
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ Description: Programmatic interface to the Harmonized World Soil Database
or your computer. Routines for both single pixel data downloads and
gridded data are provided.
Depends:
R (>= 3.6)
R (>= 4.2)
Imports:
sf,
terra,
httr
httr,
dplyr,
utils
License: AGPL-3
LazyData: true
ByteCompile: true
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
Encoding: UTF-8
Suggests:
ncdf4,
magrittr,
knitr,
markdown,
covr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(ws_download)
export(ws_get)
export(ws_subset)
14 changes: 12 additions & 2 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' HWSD meta-data
#' HWSD v1.2 (ORNL DAAC) meta-data
#'
#' Data frame with meta-data on the ORNL DAAC parameters one can query
#' using the THREDDS server. In addition a brief description of the
Expand All @@ -11,4 +11,14 @@
#' \item{description}{general description of the variable}
#' \item{units}{units of the variable}
#' }
"hwsd_meta_data"
"hwsd_meta_data"


#' HWSD v2.0 database
#'
#' Database holding the full HWSD v2.0 database layer information
#' for the main soil type specified. For the fields included (i.e.
#' the column names I refer to the FAO documentation).
#'
#' @format data.frame
"hwsd2"
120 changes: 120 additions & 0 deletions R/ws_download.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#' Download HWSD v2.0 data
#'
#' Downloads both the database and gridded HWSD v2.0 data products
#' to a desired output path for subsetting.
#'
#' When an existing path is used which is not the temporary directory
#' an environmental variable WS_PATH will be set. This variable will
#' override the default temporary directory if it exists. This allows
#' the gridded data to be stored elsewhere and be forgotten (while using the
#' {hwsdr} package for HWSD v2.0).
#'
#' Should you delete the gridded file, than it can be downloaded again but
#' the environmental variable should be set again using the new_path = TRUE
#' flag.
#'
#' @param ws_path the path / directory where to store the HWSD v2.0 database
#' @param new_path update the path, i.e. dowload data to a new local
#' location (logical, default FALSE)
#'
#' @return current data path
#' @export
#'
#' @examples
#'
#' \dontrun{
#'
#' # Download the gridded soil map of
#' # HWSD v2.0 to the temporary directory
#' ws_download()
#'
#' # download the same data to a specific
#' # directory (which should exist)
#' ws_download(
#' ws_path = "~/my_path"
#' )
#'
#' # download the same data to a specific
#' # directory (which should exist) and
#' # update the environmental variable
#' ws_download(
#' ws_path = "~/my_path",
#' new_path = TRUE
#' )
#' }

ws_download <- function(
ws_path = file.path(tempdir(), "ws_db"),
new_path = FALSE
) {

# check if environmental variable is set
# if so use dir as overriding location
if(Sys.getenv("WS_PATH") != "" & !new_path ) {
ws_path <- Sys.getenv("WS_PATH")
}

if (ws_path == file.path(tempdir(), "ws_db")) {

message(
sprintf(
"Creating temporary HWSD v2.0 files at:\n %s", ws_path )
)

# create storage path
if (!dir.exists(ws_path)) {
dir.create(ws_path, recursive = TRUE)
}

} else {

if (!dir.exists(ws_path)) {
stop(
"Database directory does not exist, please create the directory first!"
)
} else {

if(Sys.getenv("WS_PATH") == "" | new_path) {
Sys.setenv("WS_PATH" = ws_path)
}

if (file.exists(file.path(ws_path, "HWSD2.bil"))) {
message("Grid file exists, skipping download!")
return(ws_path)
}
}
}

# grab urls
urls <- server(version = "2.0")

# download zipped gridded data
httr::GET(
urls$grid,
httr::write_disk(
file.path(ws_path, "hwsd2_raster.zip"),
overwrite = TRUE
)
)

utils::unzip(
file.path(ws_path, "hwsd2_raster.zip"),
exdir = ws_path
)

# clean up zip files
status <- file.remove(
c(
file.path(ws_path, "hwsd2_raster.zip")
)
)

message(
sprintf(
"Saving HWSD v2.0 files in at:\n %s", ws_path )
)

# exit statement
message("Downloaded HWSD v2.0 files")
return(ws_path)
}
8 changes: 4 additions & 4 deletions R/ws_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ ws_get <- function(
# formulate query to pass to httr
query <- list(
"var" = var,
"south" = location[1],
"west" = location[2],
"east" = location[4],
"north" = location[3],
"south" = location[2],
"west" = location[1],
"east" = location[3],
"north" = location[4],
"disableProjSubset" = "on",
"horizStride"= 1,
"accept"="netcdf4"
Expand Down
Loading

0 comments on commit 6575b1c

Please sign in to comment.