Skip to content

Commit

Permalink
updates to handle new neonUtilities functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kcawley committed Jul 30, 2022
1 parent 9cba5a8 commit 3b456d8
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 67 deletions.
22 changes: 11 additions & 11 deletions neonDissGas/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Package: neonDissGas
Title: Calculates Dissolved CO2, CH4, and N2O Concentrations in Surface Water
Version: 0.0.0.9001
Authors@R: person("Kaelin", "Cawley", email = "kcawley@battelleecology.org", role = c("aut", "cre"))
Description: Contains functions to format data and calculate dissolved CO2, CH4, and N2O concentrations in surface water from reference air and water equilibrated gas samples. The calculations require the volume of air and water that are equilibrated and the temperature and barometric pressure at the time of equilibration.
Depends: R (>= 3.3.2),neonUtilities
Remotes: NEONScience/NEON-utilities/neonUtilities
License: AGPL-3 + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
Package: neonDissGas
Title: Calculates Dissolved CO2, CH4, and N2O Concentrations in Surface Water
Version: 0.0.0.9002
Authors@R: person("Kaelin", "Cawley", email = "kcawley@battelleecology.org", role = c("aut", "cre"))
Description: Contains functions to format data and calculate dissolved CO2, CH4, and N2O concentrations in surface water from reference air and water equilibrated gas samples. The calculations require the volume of air and water that are equilibrated and the temperature and barometric pressure at the time of equilibration.
Depends: R (>= 3.3.2)
Remotes: NEONScience/NEON-utilities/neonUtilities
License: AGPL-3 + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.0
1 change: 0 additions & 1 deletion neonDissGas/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
export(def.calc.sdg.conc)
export(def.calc.sdg.sat)
export(def.format.sdg)
importFrom(neonUtilities,stackByTable)
importFrom(utils,read.csv)
2 changes: 2 additions & 0 deletions neonDissGas/R/def.calc.sdg.conc.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#' ("concentrationCO2Air", "concentrationCH4Air", "concentrationN2OAir") for those gases
#' should be set to 0.

#' @importFrom utils read.csv

#' @param inputFile Name of the data frame containing the information needed to calculate the
#' dissolved gas concentrations. If the headers are named: "gasVolume", "waterVolume",
#' "barometricPressure", "waterTemp", "concentrationCO2Gas", "concentrationCO2Air",
Expand Down
2 changes: 2 additions & 0 deletions neonDissGas/R/def.calc.sdg.sat.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#' water dissolved gas concentration (molar), water temperature (celsius), barometric pressure
#' (kPa), and reference air gas concentrations (ppmv).

#' @importFrom utils read.csv

#' @param inputFile Name of the data frame containing the information needed to calculate
#' the dissolved gas percent saturation. If the headers are named: barometricPressure",
#' "waterTemp", "dissolvedCO2", "concentrationCO2Air", "dissolvedCH4", "concentrationCH4Air",
Expand Down
42 changes: 15 additions & 27 deletions neonDissGas/R/def.format.sdg.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#' Kaelin M. Cawley \email{kcawley@battelleecology.org} \cr

#' @description This function reads in data from the NEON Dissolved Gas data product to calculate dissolved gas concentrations in surface water. For the best results download the expanded dissolved gas package. No need to unzip the downloaded files, just place them all in the smae directory.
#' @importFrom neonUtilities stackByTable
#' @importFrom utils read.csv

#' @param dataDir User identifies the directory that contains the zipped data
#' @param externalLabData Data frame containing external lab data for NEON
#' @param fieldDataProc Data frame containing field processing data for NEON
#' @param fieldSuperParent Data frame containing field collection data for NEON

#' @return This function returns one data frame formatted for use with def.calc.sdg.R to actually calculate the concentration of the gases in the surface water sample

Expand All @@ -19,16 +19,7 @@
#' @keywords dissolved gases, methane, CH4, carbon dioxide, CO2, nitrous oxide, N2O, surface water, aquatic, streams, lakes, rivers

#' @examples
#' #where the data .zip file is in the working directory and has the default name,
#' #sdgFormatted <- def.format.sdg()
#' #where the data.zip file is in the downloads folder and has default name,
#' #sdgFormatted <-
#' #def.format.sdg(dataDir = path.expand("~/Downloads/NEON_dissolved-gases-surfacewater.zip"))
#' #where the data.zip file is in the downloads folder and has a specified name,
#' #sdgFormatted <- def.format.sdg(dataDir = path.expand("~/Downloads/non-standard-name.zip"))
#' #Using the example data in this package
#' #dataDirectory <- paste(path.package("neonDissGas"),"inst\\extdata", sep = "\\")
#' #sdgFormatted <- def.format.sdg(dataDir = dataDirectory)
#' \dontrun{TBD}

#' @seealso def.calc.sdg.conc.R and def.calc.sdg.sat.R for calculating dissolved gas
#' concentrations and percent saturation, respectively
Expand All @@ -40,27 +31,24 @@
# Update to use revised stackByTable function that is part of neonUtilities package
# Kaelin M. Cawley (2020-01-27)
# Update to work with loadByProduct in latest release of neonUtilities
# Kaelin M. Cawley (2022-07-30)
# Updated to be a little more agnostic to download process and not require neonUtilities
##############################################################################################
def.format.sdg <- function(
dataDir = paste0(getwd(),"/NEON_dissolved-gases-surfacewater.zip")
externalLabData = NA,
fieldDataProc = NA,
fieldSuperParent = NA
) {

#Check if the data is loaded already using loadByProduct
if(all(is.na(externalLabData)) | all(is.na(fieldDataProc)) | all(is.na(fieldSuperParent))){
print("externalLabData, fieldDataProd, and fieldSuperParent data tables are required to proceed.")
}

##### Default values #####
volH2O <- 40 #mL
volGas <- 20 #mL

#Check if the data is loaded already using loadByProduct
if(!exists("externalLabData")&!exists("fieldDataProc")&!exists("fieldSuperParent")){
#If not, stack field and external lab data
if(!dir.exists(gsub("\\.zip","",dataDir))){
stackByTable(dpID = "DP1.20097.001", filepath = dataDir)
}

externalLabData <- read.csv(paste(gsub("\\.zip","",dataDir),"stackedFiles","sdg_externalLabData.csv", sep = "/"), stringsAsFactors = F)
fieldDataProc <- read.csv(paste(gsub("\\.zip","",dataDir),"stackedFiles","sdg_fieldDataProc.csv", sep = "/"), stringsAsFactors = F)
fieldSuperParent <- read.csv(paste(gsub("\\.zip","",dataDir),"stackedFiles","sdg_fieldSuperParent.csv", sep = "/"), stringsAsFactors = F)
}

#Flag and set default field values
fieldDataProc$volH2OSource <- ifelse(is.na(fieldDataProc$waterVolumeSyringe),1,0)
fieldDataProc$volGasSource <- ifelse(is.na(fieldDataProc$gasVolumeSyringe),1,0)
Expand Down Expand Up @@ -100,7 +88,7 @@ def.format.sdg <- function(
outputDF[,k] <- fieldDataProc[,names(fieldDataProc) == names(outputDF)[k]]
}
outputDF$headspaceTemp <- fieldDataProc$storageWaterTemp
outputDF$barometricPressure <- fieldDataProc$ptBarometricPressure
outputDF$barometricPressure <- ifelse(!is.na(fieldDataProc$ptBarometricPressure), fieldDataProc$ptBarometricPressure, fieldDataProc$procBarometricPressure)
outputDF$waterVolume <- fieldDataProc$waterVolumeSyringe
outputDF$gasVolume <- fieldDataProc$gasVolumeSyringe
outputDF$stationID <- fieldDataProc$namedLocation
Expand Down
20 changes: 14 additions & 6 deletions neonDissGas/man/def.calc.sdg.conc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions neonDissGas/man/def.calc.sdg.sat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 7 additions & 13 deletions neonDissGas/man/def.format.sdg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions neonDissGas/man/sdgFormatted.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions testingAndDev/exampleScript_sdg.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

sdgData <- neonUtilities::loadByProduct(dpID = "DP1.20097.001",
site = c('COMO','WALK','MART','CARI','KING'),
startdate = "2022-01",
check.size = FALSE)

externalLabData <- sdgData$sdg_externalLabData
fieldDataProc <- sdgData$sdg_fieldDataProc
fieldSuperParent <- sdgData$sdg_fieldSuperParent

# #Add in the L0 data
# names(externalLabData)
#
# l0Data <- read.table("C:/Users/kcawley/Documents/forHannah/sdg_externalLabData_in/Dissolved_gases_in_surface_water,_Level_0_sdg_externalLabData_in.txt",
# header = TRUE)
# l0Data$siteID <- substr(l0Data$sampleID,1,4)
#
# sitesForHannah <- c('COMO','WALK','MART','CARI','KING')
# dataForHannah <- l0Data[l0Data$siteID %in% sitesForHannah & l0Data$collectDate >= "2022-01-01",]
#
# names(dataForHannah)[names(dataForHannah) %in% names(externalLabData)]
# names(externalLabData)[!names(externalLabData) %in% names(dataForHannah)]
#
# dataForHannah$namedLocation <- dataForHannah$stationID
#
# externalLabData <- dataForHannah

sdgFormatted <- neonDissGas::def.format.sdg(externalLabData = externalLabData,
fieldDataProc = fieldDataProc,
fieldSuperParent = fieldSuperParent)
sdgConcentrations <- neonDissGas::def.calc.sdg.conc(inputFile = sdgFormatted)
sdgDataPlusSat <- neonDissGas::def.calc.sdg.sat(inputFile = sdgConcentrations)

# write.csv(sdgDataPlusSat,
# "C:/Users/kcawley/Documents/forHannah/sdg_externalLabData_in/sdgConcentrationsAndSat.csv",
# row.names = FALSE)
4 changes: 2 additions & 2 deletions testingAndDev/testingScript_sdg.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ install_github("NEONScience/NEON-utilities/neonUtilities", force = TRUE, depende
library(roxygen2)
setwd("C:/Users/kcawley/Documents/GitHub/NEON-dissolved-gas")
#setwd("C:/Users/Kaelin/Documents/GitHub/NEON-dissolved-gas")
install("neonDissGas")
devtools::install("neonDissGas")
library(neonDissGas)

#dataDir <- "C:/Users/kcawley/Desktop/NEON_dissolved-gases-surfacewater.zip"
Expand All @@ -24,5 +24,5 @@ sdgFormatted <- sdgFormatted[1:25,]
#WRite out new sdgFormatted.rda file
use_data(sdgFormatted,internal = FALSE,overwrite = TRUE)
#setwd("C:/Users/Kaelin/Documents/GitHub/NEON-dissolved-gas/neonDissGas")
document()
devtools::document()
devtools::check()

0 comments on commit 3b456d8

Please sign in to comment.