@@ -514,3 +514,125 @@ moveSliversToOtherELFs <- function(lostPixels, lp, ca, i, r) {
514514 list (ca = ca , r = r )
515515}
516516
517+
518+
519+ # ' Run ELFs modules and optionally update googledrive png
520+ # '
521+ # ' Extracting only the "ELF" module, runs the projectmodule
522+ # ' via \code{SpaDES.core::simInitAndSpades2()} with caching (optionally
523+ # ' updates a Google Drive file if the current user is \code{"emcintir"}), and
524+ # ' returns the vector of polygon IDs from \code{spreadFitPreRun}.
525+ # '
526+ # ' @details
527+ # ' The function:
528+ # ' \enumerate{
529+ # ' \item Restricts \code{preRunSetupProject$modules} to entries matching \code{"ELFs"}.
530+ # ' \item Collects all \code{.R} source files under each module directory (recursively),
531+ # ' excluding any under \code{tests/}, and uses these as part of the cache key.
532+ # ' \item Retrieves remote metadata (hash) from a Google Drive file URL and assigns
533+ # ' the remote hash to \code{preRunSetupProject$params$fireSense_ELFs$hashSpreadFitRemoteFile}.
534+ # ' \item Initializes and runs the simulation with \code{SpaDES.core::simInitAndSpades2()},
535+ # ' wrapping the call in \code{Cache()} so that the run is skipped unless inputs
536+ # ' (source files and remote hash) have changed.
537+ # ' \item If the current \code{SpaDES.project::user()} is \code{"emcintir"}, updates
538+ # ' the Google Drive file at the provided URL with any output files whose names
539+ # ' contain \code{"ELF"}, caching the update by \code{cacheId(sim)}.
540+ # ' \item Returns \code{sim$spreadFitPreRun$polygonID}.
541+ # ' }
542+ # '
543+ # ' Internal `Cache`ing relies on two extra keys:
544+ # ' \itemize{
545+ # ' \item \code{src}: paths to module \code{.R} files (excluding \code{tests/}).
546+ # ' \item \code{remoteHash}: the remote metadata hash obtained from the URL.
547+ # ' }
548+ # '
549+ # ' The function expects \code{preRunSetupProject} to be a SpaDES project list with at least:
550+ # ' \itemize{
551+ # ' \item \code{$modules}: character vector of module names.
552+ # ' \item \code{$paths$modulePath}: base directory containing module folders.
553+ # ' \item \code{$params$fireSense_ELFs}: a list where \code{hashSpreadFitRemoteFile} can be set.
554+ # ' }
555+ # '
556+ # ' @param preRunSetupProject list. A SpaDES project object prepared for
557+ # ' \code{SpaDES.core::simInitAndSpades2()}, containing \code{$modules},
558+ # ' \code{$paths$modulePath}, and \code{$params$fireSense_ELFs}.
559+ # ' @param urlELFresults A googledrive url where the ELF data.frame that contains
560+ # ' geometries and fitted SpreadFit parameters and several other
561+ # ' attributes.
562+ # ' @param onlyFittedELFs logical. If `TRUE`, the default, then this will only
563+ # ' return the ELF indices that have been successfully fitted via `fireSense_SpreadFit`.
564+ # ' Whether individual ELFs have been fitted or not will be determined based on
565+ # ' downloading the `urlELFresults`.
566+ # '
567+ # ' @return
568+ # ' A vector corresponding to \code{sim$spreadFitPreRun$polygonID}. Currently, no high arctic
569+ # ' ELFs are returned (e.g., Ecoprovinces starting with either 1. or 2.)
570+ # '
571+ # ' @section Side Effects:
572+ # ' Updates a Google Drive file when the current user is \code{"emcintir"} and when
573+ # ' the run produces outputs with names containing \code{"ELF"}.
574+ # '
575+ # ' @section Notes:
576+ # ' \itemize{
577+ # ' \item The remote file URL is hard-coded as:
578+ # ' \code{"https://drive.google.com/file/d/1tkC944mPzR9-y-qCMDB5o2cAR_1MoDz4/view?usp=drive_link"}.
579+ # ' \item \code{Cache()} and \code{cacheId()} are assumed to be available (typically from
580+ # ' \pkg{reproducible} in SpaDES workflows).
581+ # ' \item \code{asPath()} is expected to be available in your environment (commonly from
582+ # ' \pkg{reproducible}); adjust if your project defines it elsewhere.
583+ # ' }
584+ # '
585+ # ' @examples
586+ # ' \dontrun{
587+ # ' # Assuming 'prj' is a valid SpaDES project list:
588+ # ' # prj <- somePreparationFunction(...)
589+ # ' ids <- runELFs(prj)
590+ # ' }
591+ # '
592+ # ' @importFrom SpaDES.core simInitAndSpades2
593+ # ' @importFrom reproducible Cache
594+ # ' @importFrom googledrive drive_update
595+ # ' @importFrom SpaDES.project user
596+ # ' @export
597+ # '
598+ # ' @seealso
599+ # ' \code{\link[SpaDES.core]{simInitAndSpades2}},
600+ # ' \code{\link[reproducible]{Cache}},
601+ # ' \code{\link[googledrive]{drive_update}}
602+ # '
603+ # ' @author
604+ # ' Based on the provided code snippet.
605+ # '
606+ runELFs <- function (preRunSetupProject , onlyFittedELFs = TRUE ,
607+ urlELFresults = " https://drive.google.com/file/d/1tkC944mPzR9-y-qCMDB5o2cAR_1MoDz4/view?usp=drive_link" ) {
608+ preRunSetupProject $ modules <- grep(" ELFs" , preRunSetupProject $ modules , value = TRUE )
609+ # For digesting; i.e., whether it needs to be re-run
610+ srcFiles <- asPath(dir(file.path(preRunSetupProject $ paths $ modulePath , preRunSetupProject $ modules ),
611+ pattern = " \\ .R$" , recursive = TRUE , full.names = TRUE ) | >
612+ grep(pattern = " tests\\ /" , invert = TRUE , value = TRUE ))
613+ md <- reproducible ::: getRemoteMetadata(url = urlELFresults )
614+ preRunSetupProject $ params $ fireSense_ELFs $ hashSpreadFitRemoteFile <- md $ remoteHas
615+
616+ # Run the module
617+ sim <- SpaDES.core :: simInitAndSpades2(preRunSetupProject ) | >
618+ Cache(.cacheExtra = list (src = srcFiles , remoteHash = md $ remoteHash ),
619+ omitArgs = " l" )
620+
621+ # Upload if it is emcintir; and if it has changed
622+ if (SpaDES.project :: user() %in% " emcintir" ) {
623+ cid <- cacheId(sim )
624+ ll <- googledrive :: drive_update(file = urlELFresults ,
625+ media = grep(" ELF" , sim @ outputs $ file , value = TRUE )) | >
626+ Cache(omitArgs = c(" file" , " media" ), .cacheExtra = list (cacheId = cid ))
627+ }
628+ # })
629+ if (isTRUE(onlyFittedELFs )) {
630+ .ELFinds <- sim $ spreadFitPreRun $ polygonID
631+ } else {
632+ .ELFinds <- names(sim $ ELFs $ rasCentered )
633+ }
634+ # remove Arctic that is far from treeline
635+ .ELFinds <- grep(" ^1\\ .|^2\\ ." , invert = TRUE , value = TRUE , .ELFinds )
636+
637+ .ELFinds
638+ }
0 commit comments