Skip to content

Commit

Permalink
Fixes #381
Browse files Browse the repository at this point in the history
  • Loading branch information
ldecicco-USGS committed Oct 25, 2024
1 parent 2ecb8ee commit de4ea92
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 49 deletions.
29 changes: 16 additions & 13 deletions R/compressData.r
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,28 @@ compressData <- function(data, verbose = TRUE){
i <- i + 1
}

names(data) <- c('dateTime', 'code', 'value')
returnDataFrame <- as.data.frame(matrix(ncol=3,nrow=nrow(data)))
names(returnDataFrame) <- c('dateTime', 'ConcLow', 'ConcHigh')
names(data)[1:3] <- c('dateTime', 'code', 'value')

data$dateTime <- as.character(data$dateTime)
if(dateFormatCheck(data$dateTime)){
returnDataFrame$dateTime <- as.Date(data$dateTime)
data$dateTime <- as.Date(data$dateTime)
} else {
data$dateTime <- as.Date(data$dateTime,format="%m/%d/%Y")
returnDataFrame$dateTime <- as.Date(data$dateTime,format="%m/%d/%Y")
}
returnDataFrame$ConcLow <- as.numeric(lowConcentration)
returnDataFrame$ConcHigh <- as.numeric(highConcentration)
Uncen1<-ifelse(returnDataFrame$ConcLow==returnDataFrame$ConcHigh,1,0)
returnDataFrame$Uncen<-ifelse(is.na(returnDataFrame$ConcLow)|is.na(returnDataFrame$ConcHigh),0,Uncen1)


data$ConcLow <- as.numeric(lowConcentration)
data$ConcHigh <- as.numeric(highConcentration)
Uncen1<-ifelse(data$ConcLow==data$ConcHigh,1,0)
data$Uncen<-ifelse(is.na(data$ConcLow)|is.na(data$ConcHigh),0,Uncen1)

flaggedData1 <- returnDataFrame[(returnDataFrame$ConcLow == 0 & returnDataFrame$ConcHigh == 0),]
returnDataFrame <- returnDataFrame[!(returnDataFrame$ConcLow == 0 & returnDataFrame$ConcHigh == 0),]
return(data)
}


remove_zeros <- function(data, verbose){
flaggedData1 <- data[(data$ConcLow == 0 & data$ConcHigh == 0),]
data <- data[!(data$ConcLow == 0 & data$ConcHigh == 0),]

if (nrow(flaggedData1) > 0){
WarningMessage <- paste("Deleted", nrow(flaggedData1), "rows of data because concentration was reported as 0.0, the program is unable to interpret that result and is therefore deleting it.")
Expand All @@ -78,6 +82,5 @@ compressData <- function(data, verbose = TRUE){
print(flaggedData1)
}
}

return(returnDataFrame)
return(data)
}
7 changes: 4 additions & 3 deletions R/populateSampleColumns.r
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#' dataInput <- data.frame(dateTime, ConcLow, ConcHigh, Uncen, stringsAsFactors=FALSE)
#' Sample <- populateSampleColumns(dataInput)
populateSampleColumns <- function(rawData){ # rawData is a dataframe with dateTime, ConcLow, ConcHigh, Uncen
Sample <- as.data.frame(matrix(ncol=3,nrow=length(rawData$dateTime)))
colnames(Sample) <- c('Date', 'ConcLow','ConcHigh')
Sample <- rawData

Sample$Date <- rawData$dateTime
Sample$ConcLow <- rawData$ConcLow
Sample$ConcHigh <- rawData$ConcHigh
Expand All @@ -29,6 +29,7 @@ populateSampleColumns <- function(rawData){ # rawData is a dataframe with dateT

Sample$SinDY <- sin(2*pi*Sample$DecYear)
Sample$CosDY <- cos(2*pi*Sample$DecYear)
# Sample2 <- subset(Sample, (!is.na(Sample$ConcHigh))) # Was just ConcHigh.....

Sample <- Sample[,names(Sample)[!names(Sample) %in% c("code", "value")]]
return (Sample)
}
6 changes: 6 additions & 0 deletions R/readUserSample.r
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ readUserSample <- function (filePath, fileName,
format = format)

compressedData <- compressData(data, verbose = verbose)
compressedData <- remove_zeros(compressedData, verbose = verbose)
Sample <- populateSampleColumns(compressedData)
orig_Sample <- c("Date", "ConcLow", "ConcHigh", "Uncen", "ConcAve",
"Julian", "Month", "Day", "DecYear", "waterYear", "MonthSeq",
"SinDY", "CosDY")
Sample <- Sample[, c(orig_Sample, names(Sample)[!names(Sample) %in% orig_Sample])]
Sample <- Sample[order(Sample$Date), ]
return(Sample)
}
72 changes: 39 additions & 33 deletions R/readWQPSample.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ readWQPSample <- function(siteNumber,
endDate = "",
verbose = TRUE){


extra_cols <- c("ActivityStartDateTime",
"USGSPCode",
"ActivityMediaSubdivisionName",
"ActivityMediaName",
"ResultSampleFractionText",
"ResultStatusIdentifier",
"ResultValueTypeName")

if(packageVersion("dataRetrieval") >= "2.7.17"){
data <- suppressMessages(dataRetrieval::readWQPqw(siteNumbers = siteNumber,
parameterCd = characteristicName,
Expand All @@ -54,29 +61,23 @@ readWQPSample <- function(siteNumber,
legacy = FALSE) )

conversion_names <- data.frame(legacy_names = c("ResultDetectionConditionText",
"ResultMeasureValue",
"DetectionQuantitationLimitMeasure.MeasureValue",
"CharacteristicName",
"ActivityStartDate",
"ActivityStartDateTime",
"USGSPCode",
"ActivityMediaSubdivisionName",
"ActivityMediaName",
"ResultSampleFractionText",
"ResultStatusIdentifier",
"ResultValueTypeName"),
new_names = c("Result_ResultDetectionCondition",
"Result_Measure",
"DetectionLimit_TypeA",
"Result_Characteristic",
"Activity_StartDate",
"Activity_StartDateTime",
"USGSpcode",
"Activity_MediaSubdivisionName",
"Activity_Media",
"Result_SampleFraction",
"Result_MeasureStatusIdentifier",
"Result_MeasureType"))
"ResultMeasureValue",
"DetectionQuantitationLimitMeasure.MeasureValue",
"CharacteristicName",
"ActivityStartDate",
extra_cols),
new_names = c("Result_ResultDetectionCondition",
"Result_Measure",
"DetectionLimit_MeasureA",
"Result_Characteristic",
"Activity_StartDate",
"Activity_StartDateTime",
"USGSpcode",
"Activity_MediaSubdivisionName",
"Activity_Media",
"Result_SampleFraction",
"Result_MeasureStatusIdentifier",
"Result_MeasureType"))

for(i in seq_len(nrow(conversion_names))){
names(data)[which(names(data) == conversion_names$new_names[i])] <- conversion_names$legacy_names[i]
Expand All @@ -97,15 +98,20 @@ readWQPSample <- function(siteNumber,
if(nrow(data) > 0){

data <- processQWData(data)
first_three <- c("dateTime",
"qualifier",
"value")
compressedData <- compressData(data[, first_three],
verbose = verbose)
combined_data <- cbind(compressedData, data[, names(data)[!names(data) %in% first_three]])
combined_data <- remove_zeros(combined_data, verbose = verbose)

compressedData <- compressData(data[, c("dateTime",
"qualifier",
"value")],
verbose=verbose)
Sample <- populateSampleColumns(compressedData)
Sample <- cbind(Sample, data[, names(data)[!names(data) %in% c("dateTime",
"qualifier",
"value")]])
Sample <- populateSampleColumns(combined_data)
orig_Sample <- c("Date", "ConcLow", "ConcHigh", "Uncen", "ConcAve",
"Julian", "Month", "Day", "DecYear", "waterYear", "MonthSeq",
"SinDY", "CosDY")
Sample <- Sample[, c(orig_Sample, names(Sample)[!names(Sample) %in% orig_Sample])]
Sample <- Sample[order(Sample$Date), ]
} else {
Sample <- data.frame(Date=as.Date(character()),
ConcLow=numeric(),
Expand All @@ -121,6 +127,6 @@ readWQPSample <- function(siteNumber,
CosDY=numeric(),
stringsAsFactors=FALSE)
}
Sample <- Sample[order(Sample$Date), ]

return(Sample)
}

0 comments on commit de4ea92

Please sign in to comment.