From dede21a2be3af5a9a6bce60198357ceb4af9e462 Mon Sep 17 00:00:00 2001 From: Rohan Sasne Date: Fri, 21 Jul 2023 08:25:57 -0500 Subject: [PATCH 01/13] Add PRLabeler GitHub Action Add PR Labeler TemplateDd Add Labels to Directories fix PR file error Merge pull request #7 from RohanSasne/PRLabeler Pr labeler Test commit Merge pull request #9 from RohanSasne/PRLabeler Test commit Update labeler.yml Update labeler.yml Add workflow condition Merge branch 'PecanProject:develop' into develop Update prlabeler.yml Add Labels Add cahnges Update labeler.yml --- .github/labeler.yml | 55 +++++++++++++++++++++++++++++++++ .github/settings.yml | 30 ++++++++++++++++++ .github/workflows/prlabeler.yml | 22 +++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 .github/labeler.yml create mode 100644 .github/settings.yml create mode 100644 .github/workflows/prlabeler.yml diff --git a/.github/labeler.yml b/.github/labeler.yml new file mode 100644 index 00000000000..4f13bbbdda5 --- /dev/null +++ b/.github/labeler.yml @@ -0,0 +1,55 @@ +# +# Add project labels +# + +# Add 'Documentation' label to any changes in the documentation + +'Documentation': + - documentation/** + - CHANGELOG.md + - CONTRIBUTING.md + - DEBUGING.md + - DEV-INTRO.md + - README.md + +# Add 'Dockerfile' label to any changes in the docker directory +'Dockerfile': + - docker/** + + +# Add 'Website' label to any changes in the web directory + +'Website': + - web/** + +# Add 'Base' label to any changes in the base directory + +'Base': + - base/** + +# Add 'Models' label to any changes in the models directory + +'Models': + - models/** + +# Add 'Modules' label to any changes in the modules directory + +'Modules': + - modules/** + +# Add 'GitHub Actions' label to any changes in the .github/workflows directory + +'GitHub Actions': + - .github/workflows/** + +# Add 'Scripts' label to any changes in the scripts directory + +'Scripts': + - scripts/** + +# Add 'Tests' label to any changes in the tests directory + +'Tests': + - tests/** + + diff --git a/.github/settings.yml b/.github/settings.yml new file mode 100644 index 00000000000..a7d87f7d2f9 --- /dev/null +++ b/.github/settings.yml @@ -0,0 +1,30 @@ +#Define the colour of labels over here + +labels: + + - name: "Documentation" + color: a2dcf2 + + - name: "Dockerfile" + color: 0052CC + + - name: "Website" + color: 84b6eb + + - name: "Base" + color: 1ED626 + + - name: "Models" + color: C5DEF5 + + - name: "Modules" + color: FBCA04 + + - name: "GitHub Actions" + color: 84b6eb + + - name: "Scripts" + color: 3B8924 + + - name: "Tests" + color: ff8c00 diff --git a/.github/workflows/prlabeler.yml b/.github/workflows/prlabeler.yml new file mode 100644 index 00000000000..235cd581a1a --- /dev/null +++ b/.github/workflows/prlabeler.yml @@ -0,0 +1,22 @@ +# This workflow is based on github action official label action v4. +# This workflow action is triggered on pull request event(on both fork & inside repo) +# Labels will be applied based on filepath modification in PR. +# This workflow uses a regex based labeling config file(.github/labeler.yml) to take labeling decision. + +name: "PR Labeler" +on: +- pull_request_target +jobs: + label: + permissions: + contents: read + pull-requests: write + runs-on: ubuntu-latest + + steps: + - uses: actions/labeler@v4 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" + configuration-path: ".github/labeler.yml" + sync-labels: false + dot: true From 0838e950558b872702129094add0800731cb1f93 Mon Sep 17 00:00:00 2001 From: Joshua Ploshay Date: Thu, 10 Aug 2023 19:11:30 -0400 Subject: [PATCH 02/13] Add the down_scale function to pecan. --- .../assim.sequential/R/downscale_function.R | 93 +++++++++++++++++++ modules/assim.sequential/man/NA_downscale.Rd | 31 +++++++ 2 files changed, 124 insertions(+) create mode 100644 modules/assim.sequential/R/downscale_function.R create mode 100644 modules/assim.sequential/man/NA_downscale.Rd diff --git a/modules/assim.sequential/R/downscale_function.R b/modules/assim.sequential/R/downscale_function.R new file mode 100644 index 00000000000..42dd3e2102f --- /dev/null +++ b/modules/assim.sequential/R/downscale_function.R @@ -0,0 +1,93 @@ +##' @title North America Downscale Function +##' @name NA_downscale +##' @author Joshua Ploshay +##' +##' @param data In quotes, file path for .rds containing ensemble data. +##' @param focus_year In quotes, if SDA site run, format is yyyy/mm/dd, if NEON, yyyy-mm-dd. Restricted to years within file supplied to 'data'. +##' @param C_pool In quotes, carbon pool of interest. Name must match carbon pool name found within file supplied to 'data'. +##' @param covariates: In quotes, file path of SpatRaster stack, used as predictors in randomForest. Layers within stack should be named. +##' @param cords: In quotes, file path for .csv file containing the site coordinates, columns named "lon" and "lat". +##' @details This function will downscale forecast data to unmodeled locations using covariates and site locations +##' +##' @description This function uses the randomForest model. +##' +##' @return It returns the `downscale_output` list containing lists for the training and testing data sets, models, and predicted maps for each ensemble member. + + +NA_downscale <- function(data, cords, covariates, focus_year, C_pool){ + + # Read in the covariates and set CRS to EPSG:4326 + covariates <- terra::rast(covariates) # ADD package to every function + terra::crs(covariates) <- "EPSG:4326" + + # Read the input data and site coordinates + input_data <- readRDS(data) + site_coordinates <- terra::vect(readr::read_csv(cords), geom=c("lon", "lat"), crs="EPSG:4326") + + # Extract the carbon data for the specified focus year + index <- which(names(input_data) == focus_year) + data <- input_data[[index]] + carbon_data <- as.data.frame(t(data[which(names(data) == C_pool)])) + names(carbon_data) <- paste0("ensemble",seq(1:ncol(carbon_data))) + + # Extract predictors from covariates raster using site coordinates + predictors <- as.data.frame(terra::extract(covariates, site_coordinates)) + predictors <- dplyr::select(predictors, -1) + + # Combine each ensemble member with all predictors + ensembles <- list() + for (i in seq_along(carbon_data)) { + ensembles[[i]] <- cbind(carbon_data[[i]], predictors) + } + + # Rename the carbon_data column for each ensemble member + for (i in 1:length(ensembles)) { + ensembles[[i]] <- dplyr::rename(ensembles[[i]], "carbon_data" = "carbon_data[[i]]") + } + + # Split the observations in each data frame into two data frames based on the proportion of 3/4 + ensembles <- lapply(ensembles, function(df) { + sample <- sample(1:nrow(df), size = round(0.75*nrow(df))) + train <- df[sample, ] + test <- df[-sample, ] + split_list <- list(train, test) + return(split_list) + }) + + # Rename the training and testing data frames for each ensemble member + for (i in 1:length(ensembles)) { + # names(ensembles) <- paste0("ensemble",seq(1:length(ensembles))) + names(ensembles[[i]]) <- c("training", "testing") + } + + # Train a random forest model for each ensemble member using the training data + output <- list() + for (i in 1:length(ensembles)) { + output[[i]] <- randomForest::randomForest(ensembles[[i]][[1]][["carbon_data"]] ~ land_cover+tavg+prec+srad+vapr+nitrogen+phh2o+soc+sand, + data = ensembles[[i]][[1]], + ntree = 1000, + na.action = na.omit, + keep.forest = T, + importance = T) + } + + # Generate predictions (maps) for each ensemble member using the trained models + maps <- list(ncol(output)) + for (i in 1:length(output)) { + maps[[i]] <- terra::predict(object = covariates, + model = output[[i]],na.rm = T) + } + + # Organize the results into a single output list + downscale_output <- list(ensembles, output, maps) + + # Rename each element of the output list with appropriate ensemble numbers + for (i in 1:length(downscale_output)) { + names(downscale_output[[i]]) <- paste0("ensemble",seq(1:length(downscale_output[[i]]))) + } + + # Rename the main components of the output list + names(downscale_output) <- c("data", "models", "maps") + + return(downscale_output) +} diff --git a/modules/assim.sequential/man/NA_downscale.Rd b/modules/assim.sequential/man/NA_downscale.Rd new file mode 100644 index 00000000000..5dabf4738d2 --- /dev/null +++ b/modules/assim.sequential/man/NA_downscale.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/downscale_function.R +\name{NA_downscale} +\alias{NA_downscale} +\title{North America Downscale Function} +\usage{ +NA_downscale(data, cords, covariates, focus_year, C_pool) +} +\arguments{ +\item{data}{In quotes, file path for .rds containing ensemble data.} + +\item{focus_year}{In quotes, if SDA site run, format is yyyy/mm/dd, if NEON, yyyy-mm-dd. Restricted to years within file supplied to 'data'.} + +\item{C_pool}{In quotes, carbon pool of interest. Name must match carbon pool name found within file supplied to 'data'.} + +\item{covariates:}{In quotes, file path of SpatRaster stack, used as predictors in randomForest. Layers within stack should be named.} + +\item{cords:}{In quotes, file path for .csv file containing the site coordinates, columns named "lon" and "lat".} +} +\value{ +It returns the `downscale_output` list containing lists for the training and testing data sets, models, and predicted maps for each ensemble member. +} +\description{ +This function uses the randomForest model. +} +\details{ +This function will downscale forecast data to unmodeled locations using covariates and site locations +} +\author{ +Joshua Ploshay +} From 38513e640b99f84b97bbcd70b149c4b445a596ed Mon Sep 17 00:00:00 2001 From: Rohan Sasne Date: Fri, 18 Aug 2023 23:27:15 +0530 Subject: [PATCH 03/13] Update indentation --- .github/workflows/prlabeler.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/prlabeler.yml b/.github/workflows/prlabeler.yml index 235cd581a1a..1a29e659168 100644 --- a/.github/workflows/prlabeler.yml +++ b/.github/workflows/prlabeler.yml @@ -5,7 +5,7 @@ name: "PR Labeler" on: -- pull_request_target + - pull_request_target jobs: label: permissions: @@ -14,9 +14,9 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/labeler@v4 - with: - repo-token: "${{ secrets.GITHUB_TOKEN }}" - configuration-path: ".github/labeler.yml" - sync-labels: false - dot: true + - uses: actions/labeler@v4 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" + configuration-path: ".github/labeler.yml" + sync-labels: false + dot: true From 867b88a546b2020ee807cf39d088cd88a0ef44af Mon Sep 17 00:00:00 2001 From: Joshua Ploshay Date: Wed, 28 Feb 2024 15:41:28 -0500 Subject: [PATCH 04/13] updated documentation to include suggested packages --- docker/depends/pecan.depends.R | 2 ++ modules/assim.sequential/DESCRIPTION | 3 +++ 2 files changed, 5 insertions(+) diff --git a/docker/depends/pecan.depends.R b/docker/depends/pecan.depends.R index a4f2e489a4e..e49922d1ef6 100644 --- a/docker/depends/pecan.depends.R +++ b/docker/depends/pecan.depends.R @@ -104,9 +104,11 @@ wanted <- c( 'purrr', 'pwr', 'R.utils', +'randomForest', 'randtoolbox', 'raster', 'rcrossref', +'readr', 'REddyProc', 'redland', 'reshape', diff --git a/modules/assim.sequential/DESCRIPTION b/modules/assim.sequential/DESCRIPTION index acb9d7aff85..6c2e7052a73 100644 --- a/modules/assim.sequential/DESCRIPTION +++ b/modules/assim.sequential/DESCRIPTION @@ -45,11 +45,14 @@ Suggests: PEcAn.data.remote, plotrix, plyr (>= 1.8.4), + randomForest, raster, + readr, reshape2 (>= 1.4.2), rlist, sf, stats, + terra, testthat, tictoc, tidyr, From 5ae30363c1c1832805bb48b7971163eb4ac1ef36 Mon Sep 17 00:00:00 2001 From: Joshua Ploshay Date: Wed, 28 Feb 2024 15:53:34 -0500 Subject: [PATCH 05/13] resolve conflicts and update dependencies --- docker/depends/pecan.depends.R | 188 +++--------------- docker/depends/pecan_package_dependencies.csv | 3 + 2 files changed, 28 insertions(+), 163 deletions(-) diff --git a/docker/depends/pecan.depends.R b/docker/depends/pecan.depends.R index b03d95e3433..0f3057d896d 100644 --- a/docker/depends/pecan.depends.R +++ b/docker/depends/pecan.depends.R @@ -6,142 +6,6 @@ rlib <- Sys.getenv('R_LIBS_USER', '/usr/local/lib/R/site-library') Sys.setenv(RLIB = rlib) -<<<<<<< HEAD -# install all packages (depends, imports, suggests) -wanted <- c( -'abind', -'amerifluxr', -'arrow', -'assertthat', -'BayesianTools', -'BioCro', -'bit64', -'BrownDog', -'coda', -'corrplot', -'curl', -'data.table', -'dataone', -'datapack', -'DBI', -'dbplyr', -'devtools', -'doParallel', -'doSNOW', -'dplR', -'dplyr', -'ellipse', -'emdbook', -'foreach', -'fs', -'furrr', -'future', -'geonames', -'getPass', -'ggmap', -'ggmcmc', -'ggplot2', -'ggpubr', -'ggrepel', -'glue', -'graphics', -'grDevices', -'grid', -'gridExtra', -'hdf5r', -'here', -'httr', -'IDPmisc', -'jsonlite', -'knitr', -'lattice', -'linkages', -'lqmm', -'lubridate', -'Maeswrap', -'magic', -'magrittr', -'maps', -'maptools', -'markdown', -'MASS', -'Matrix', -'mclust', -'MCMCpack', -'methods', -'mgcv', -'minpack.lm', -'mlegp', -'mockery', -'MODISTools', -'mvbutils', -'mvtnorm', -'ncdf4', -'neonstore', -'neonUtilities', -'nimble', -'nneo', -'optparse', -'parallel', -'plotrix', -'plyr', -'png', -'prodlim', -'progress', -'purrr', -'pwr', -'R.utils', -'randomForest', -'randtoolbox', -'raster', -'rcrossref', -'readr', -'REddyProc', -'redland', -'reshape', -'reshape2', -'reticulate', -'rgdal', -'rjags', -'rjson', -'rlang', -'rlist', -'rmarkdown', -'RPostgres', -'RPostgreSQL', -'Rpreles', -'RSQLite', -'sf', -'SimilarityMeasures', -'sirt', -'sp', -'stats', -'stringi', -'stringr', -'swfscMisc', -'terra', -'testthat', -'tibble', -'tictoc', -'tidyr', -'tidyselect', -'tidyverse', -'tools', -'traits', -'TruncatedNormal', -'truncnorm', -'units', -'urltools', -'utils', -'vdiffr', -'withr', -'XML', -'xtable', -'xts', -'zoo' -) -missing <- wanted[!(wanted %in% installed.packages()[,'Package'])] -======= # Find the latest of several possible minimum package versions condense_version_requirements <- function(specs) { if (all(specs == "*")) { @@ -151,7 +15,7 @@ condense_version_requirements <- function(specs) { specs <- unique(specs[specs != "*"]) versions <- package_version( gsub("[^[:digit:].-]+", "", specs)) - + if ((length(unique(versions)) > 1) && any(!grepl(">", specs))) { # Can't assume the latest version works for all, so give up. # We *could* write more to handle this case if needed, but it seems very rare: @@ -175,30 +39,30 @@ condense_version_requirements <- function(specs) { # Install or newer, # upgrading dependencies only if needed to satisfy stated version requirements ensure_version <- function(pkg, version) { - vers <- gsub('[^[:digit:].-]+', '', version) - cmp <- get(gsub('[^<>=]+', '', version)) - ok <- requireNamespace(pkg, quietly = TRUE) && - cmp(packageVersion(pkg), vers) - if (!ok) { - # install pkg and any *missing* dependencies - remotes::install_version(pkg, version, dependencies = TRUE, upgrade = FALSE) - # Now check for installed but *incompatible* dependencies - # (install_version doesn't resolve these when upgrade=FALSE) - dep <- desc::desc_get_deps(system.file("DESCRIPTION", package = pkg)) - dep <- dep[ - dep$type %in% c("Depends", "Imports", "LinkingTo") - & dep$version != "*" - & dep$package != "R",] - invisible(Map(ensure_version, dep$package, dep$version)) - } - + vers <- gsub('[^[:digit:].-]+', '', version) + cmp <- get(gsub('[^<>=]+', '', version)) + ok <- requireNamespace(pkg, quietly = TRUE) && + cmp(packageVersion(pkg), vers) + if (!ok) { + # install pkg and any *missing* dependencies + remotes::install_version(pkg, version, dependencies = TRUE, upgrade = FALSE) + # Now check for installed but *incompatible* dependencies + # (install_version doesn't resolve these when upgrade=FALSE) + dep <- desc::desc_get_deps(system.file("DESCRIPTION", package = pkg)) + dep <- dep[ + dep$type %in% c("Depends", "Imports", "LinkingTo") + & dep$version != "*" + & dep$package != "R",] + invisible(Map(ensure_version, dep$package, dep$version)) + } + } # Read list of dependencies. # NOTE: These files are autogenerated -- # use scripts/generate_dependencies.R to edit them. all_deps <- read.csv("pecan_package_dependencies.csv") |> - subset(!is_pecan) + subset(!is_pecan) gh_repos <- readLines("pecan_deps_from_github.txt") @@ -210,16 +74,15 @@ remotes::install_github(gh_repos, lib = rlib) # For deps used by multiple packages, find a version that works for all uniq_deps <- tapply( - all_deps$version, - INDEX = all_deps$package, - FUN = condense_version_requirements) + all_deps$version, + INDEX = all_deps$package, + FUN = condense_version_requirements) # Install deps that declare no version restriction. # We'll install these with one plain old `install.packages()` call. unversioned <- names(uniq_deps[uniq_deps == "*"]) missing <- unversioned[!(unversioned %in% installed.packages()[,'Package'])] ->>>>>>> 48a3b259fd03512c145b4d6eb387467e56ab49b4 install.packages(missing, lib = rlib) @@ -229,9 +92,8 @@ install.packages(missing, lib = rlib) # it can't fill the version req from snapshot versions. # (Assumes our CRAN uses the same URL scheme as Posit package manager) options(repos = c( - getOption('repos'), - sub(r'(\d{4}-\d{2}-\d{2})', 'latest', getOption('repos')) + getOption('repos'), + sub(r'(\d{4}-\d{2}-\d{2})', 'latest', getOption('repos')) )) versioned <- uniq_deps[uniq_deps != "*"] -invisible(Map(ensure_version, names(versioned), versioned)) - +invisible(Map(ensure_version, names(versioned), versioned)) \ No newline at end of file diff --git a/docker/depends/pecan_package_dependencies.csv b/docker/depends/pecan_package_dependencies.csv index 8345db18103..eb6b8e0f47a 100644 --- a/docker/depends/pecan_package_dependencies.csv +++ b/docker/depends/pecan_package_dependencies.csv @@ -438,6 +438,7 @@ "purrr",">= 0.2.3","modules/data.atmosphere","Imports",FALSE "pwr","*","modules/rtm","Suggests",FALSE "R.utils","*","base/db","Imports",FALSE +"randomForest","*","modules/assim.sequential","Suggests",FALSE "randtoolbox","*","base/utils","Suggests",FALSE "randtoolbox","*","modules/uncertainty","Imports",FALSE "raster","*","base/visualization","Suggests",FALSE @@ -446,6 +447,7 @@ "raster","*","modules/data.land","Suggests",FALSE "raster","*","modules/data.remote","Suggests",FALSE "rcrossref","*","base/db","Suggests",FALSE +"readr","*","modules/assim.sequential","Suggests",FALSE "REddyProc","*","modules/data.atmosphere","Imports",FALSE "redland","*","modules/data.land","Suggests",FALSE "reshape","*","modules/data.remote","Suggests",FALSE @@ -554,6 +556,7 @@ "stringr",">= 1.1.0","modules/data.atmosphere","Imports",FALSE "suntools","*","modules/data.atmosphere","Imports",FALSE "swfscMisc","*","modules/data.land","Imports",FALSE +"terra","*","modules/assim.sequential","Suggests",FALSE "terra","*","modules/data.atmosphere","Imports",FALSE "terra","*","modules/data.land","Imports",FALSE "terra","*","modules/data.remote","Imports",FALSE From a967394ed77605b834d8bbc0cf948cb4e3a93913 Mon Sep 17 00:00:00 2001 From: Gandalf Date: Sat, 2 Mar 2024 01:47:24 +0530 Subject: [PATCH 06/13] Update labeler.yml --- .github/labeler.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/labeler.yml b/.github/labeler.yml index 4f13bbbdda5..e6830b30d9e 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -6,11 +6,11 @@ 'Documentation': - documentation/** - - CHANGELOG.md - CONTRIBUTING.md - DEBUGING.md - DEV-INTRO.md - README.md + - book_source/** # Add 'Dockerfile' label to any changes in the docker directory 'Dockerfile': From fe0aefeb7837cadc879779105e0382ffd14a21b4 Mon Sep 17 00:00:00 2001 From: Michael Dietze Date: Tue, 5 Mar 2024 09:35:21 -0500 Subject: [PATCH 07/13] Update modules/assim.sequential/R/downscale_function.R --- modules/assim.sequential/R/downscale_function.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/assim.sequential/R/downscale_function.R b/modules/assim.sequential/R/downscale_function.R index 42dd3e2102f..ce2428fd354 100644 --- a/modules/assim.sequential/R/downscale_function.R +++ b/modules/assim.sequential/R/downscale_function.R @@ -6,7 +6,7 @@ ##' @param focus_year In quotes, if SDA site run, format is yyyy/mm/dd, if NEON, yyyy-mm-dd. Restricted to years within file supplied to 'data'. ##' @param C_pool In quotes, carbon pool of interest. Name must match carbon pool name found within file supplied to 'data'. ##' @param covariates: In quotes, file path of SpatRaster stack, used as predictors in randomForest. Layers within stack should be named. -##' @param cords: In quotes, file path for .csv file containing the site coordinates, columns named "lon" and "lat". +##' @param cords In quotes, file path for .csv file containing the site coordinates, columns named "lon" and "lat". ##' @details This function will downscale forecast data to unmodeled locations using covariates and site locations ##' ##' @description This function uses the randomForest model. From 2e3d83293bfe7087db5cdb9a66de7b1c9fd4f156 Mon Sep 17 00:00:00 2001 From: Michael Dietze Date: Tue, 5 Mar 2024 09:35:35 -0500 Subject: [PATCH 08/13] Update modules/assim.sequential/R/downscale_function.R --- modules/assim.sequential/R/downscale_function.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/assim.sequential/R/downscale_function.R b/modules/assim.sequential/R/downscale_function.R index ce2428fd354..54551c1ee83 100644 --- a/modules/assim.sequential/R/downscale_function.R +++ b/modules/assim.sequential/R/downscale_function.R @@ -5,7 +5,7 @@ ##' @param data In quotes, file path for .rds containing ensemble data. ##' @param focus_year In quotes, if SDA site run, format is yyyy/mm/dd, if NEON, yyyy-mm-dd. Restricted to years within file supplied to 'data'. ##' @param C_pool In quotes, carbon pool of interest. Name must match carbon pool name found within file supplied to 'data'. -##' @param covariates: In quotes, file path of SpatRaster stack, used as predictors in randomForest. Layers within stack should be named. +##' @param covariates In quotes, file path of SpatRaster stack, used as predictors in randomForest. Layers within stack should be named. ##' @param cords In quotes, file path for .csv file containing the site coordinates, columns named "lon" and "lat". ##' @details This function will downscale forecast data to unmodeled locations using covariates and site locations ##' From f961b4680a9e5dd93a0c31dceb59a6d08709ade2 Mon Sep 17 00:00:00 2001 From: Chris Black Date: Thu, 7 Mar 2024 01:10:48 -0800 Subject: [PATCH 09/13] fix flipped multiply/divide in seq generation I bet the intention was to write 0:(23/output.dt)*output.dt, which produces same output as this seq call. --- .../data.atmosphere/R/temporal.downscaling.R | 2 +- .../tests/testthat/test.cf-downscaling.R | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/data.atmosphere/R/temporal.downscaling.R b/modules/data.atmosphere/R/temporal.downscaling.R index 76600f91420..4032fe61135 100644 --- a/modules/data.atmosphere/R/temporal.downscaling.R +++ b/modules/data.atmosphere/R/temporal.downscaling.R @@ -120,7 +120,7 @@ cfmet.downscale.subdaily <- function(subdailymet, output.dt = 1) { cfmet.downscale.daily <- function(dailymet, output.dt = 1, lat) { tint <- 24/output.dt - tseq <- 0:(23 * output.dt)/output.dt + tseq <- seq(from = 0, to = 23, by = output.dt) data.table::setkeyv(dailymet, c("year", "doy")) diff --git a/modules/data.atmosphere/tests/testthat/test.cf-downscaling.R b/modules/data.atmosphere/tests/testthat/test.cf-downscaling.R index 2d3d34be2f7..2b514f84b6a 100644 --- a/modules/data.atmosphere/tests/testthat/test.cf-downscaling.R +++ b/modules/data.atmosphere/tests/testthat/test.cf-downscaling.R @@ -21,6 +21,32 @@ test_that( expect_equal(b[,signif(range(wind), 2)], c(0.066, 6.60)) }) +test_that("downscaling with timestep", { + df <- data.table::data.table( + year = 2020, doy = 100, + air_temperature_min = 293.15, air_temperature_max = 303.15, air_temperature = 298.15, + surface_downwelling_shortwave_flux_in_air = 1000, + air_pressure = 1030, + wind_speed = 0, + relative_humidity = 0.5, + precipitation_flux = 2 / (60 * 60)) # units: mm/sec + + r1 <- cfmet.downscale.daily(df, output.dt = 1, lat = 40) + r6 <- cfmet.downscale.daily(df, output.dt = 6, lat = 40) + r12 <- cfmet.downscale.daily(df, output.dt = 12, lat = 40) + + expect_equal(nrow(r1), 24) + expect_equal(nrow(r6), 4) + expect_equal(nrow(r12), 2) + + list(r1, r6,r12) %>% + purrr::walk(~{ + expect_equal(mean(.$air_temperature), (df$air_temperature - 273.15)) # input is K, output is C + expect_equal(sum(.$precipitation_flux), df$precipitation_flux) + expect_true(all(.$air_pressure == df$air_pressure)) + }) + +}) test_that("get.ncvector works",{ run.dates <- data.table::data.table(index = 1:2, date = c(lubridate::ymd("1951-01-01 UTC"), lubridate::ymd("1951-01-02 UTC"))) From abd4e612b140ad53b0f544a1842a0a858c0fc32e Mon Sep 17 00:00:00 2001 From: Gandalf Date: Thu, 7 Mar 2024 21:09:30 +0530 Subject: [PATCH 10/13] Apply suggestions from code review Co-authored-by: Chris Black --- .github/labeler.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/labeler.yml b/.github/labeler.yml index e6830b30d9e..58bb5ccb80f 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -5,12 +5,12 @@ # Add 'Documentation' label to any changes in the documentation 'Documentation': + - book_source/** - documentation/** - CONTRIBUTING.md - DEBUGING.md - DEV-INTRO.md - README.md - - book_source/** # Add 'Dockerfile' label to any changes in the docker directory 'Dockerfile': @@ -51,5 +51,7 @@ 'Tests': - tests/** + - '**/tests/**' + - '!**/tests/Rcheck_reference.log' From 1daf41bbb46f9a522144fbb3bd62542810e27f06 Mon Sep 17 00:00:00 2001 From: Michael Dietze Date: Thu, 7 Mar 2024 11:07:06 -0500 Subject: [PATCH 11/13] Update modules/assim.sequential/R/downscale_function.R --- modules/assim.sequential/R/downscale_function.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/assim.sequential/R/downscale_function.R b/modules/assim.sequential/R/downscale_function.R index 54551c1ee83..4a64beef645 100644 --- a/modules/assim.sequential/R/downscale_function.R +++ b/modules/assim.sequential/R/downscale_function.R @@ -66,7 +66,7 @@ NA_downscale <- function(data, cords, covariates, focus_year, C_pool){ output[[i]] <- randomForest::randomForest(ensembles[[i]][[1]][["carbon_data"]] ~ land_cover+tavg+prec+srad+vapr+nitrogen+phh2o+soc+sand, data = ensembles[[i]][[1]], ntree = 1000, - na.action = na.omit, + na.action = stats::na.omit, keep.forest = T, importance = T) } From 4d032e844c27fef2862e6452ba60845c65b8ae8a Mon Sep 17 00:00:00 2001 From: Michael Dietze Date: Thu, 7 Mar 2024 11:07:19 -0500 Subject: [PATCH 12/13] Update modules/assim.sequential/man/NA_downscale.Rd --- modules/assim.sequential/man/NA_downscale.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/assim.sequential/man/NA_downscale.Rd b/modules/assim.sequential/man/NA_downscale.Rd index 5dabf4738d2..979229d30b2 100644 --- a/modules/assim.sequential/man/NA_downscale.Rd +++ b/modules/assim.sequential/man/NA_downscale.Rd @@ -13,7 +13,7 @@ NA_downscale(data, cords, covariates, focus_year, C_pool) \item{C_pool}{In quotes, carbon pool of interest. Name must match carbon pool name found within file supplied to 'data'.} -\item{covariates:}{In quotes, file path of SpatRaster stack, used as predictors in randomForest. Layers within stack should be named.} +\item{covariates}{In quotes, file path of SpatRaster stack, used as predictors in randomForest. Layers within stack should be named.} \item{cords:}{In quotes, file path for .csv file containing the site coordinates, columns named "lon" and "lat".} } From e98c4613e22e34a7d607bed5b2a410d81b57865d Mon Sep 17 00:00:00 2001 From: Michael Dietze Date: Thu, 7 Mar 2024 11:07:26 -0500 Subject: [PATCH 13/13] Update modules/assim.sequential/man/NA_downscale.Rd --- modules/assim.sequential/man/NA_downscale.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/assim.sequential/man/NA_downscale.Rd b/modules/assim.sequential/man/NA_downscale.Rd index 979229d30b2..8ab6a5ea946 100644 --- a/modules/assim.sequential/man/NA_downscale.Rd +++ b/modules/assim.sequential/man/NA_downscale.Rd @@ -15,7 +15,7 @@ NA_downscale(data, cords, covariates, focus_year, C_pool) \item{covariates}{In quotes, file path of SpatRaster stack, used as predictors in randomForest. Layers within stack should be named.} -\item{cords:}{In quotes, file path for .csv file containing the site coordinates, columns named "lon" and "lat".} +\item{cords}{In quotes, file path for .csv file containing the site coordinates, columns named "lon" and "lat".} } \value{ It returns the `downscale_output` list containing lists for the training and testing data sets, models, and predicted maps for each ensemble member.