-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutilities.R
More file actions
180 lines (119 loc) · 4.39 KB
/
utilities.R
File metadata and controls
180 lines (119 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
expandpoly <- function(mypol, fact) {
m1 <- mean(mypol[, 1])
m2 <- mean(mypol[, 2])
cbind((mypol[, 1] - m1) * fact + m1, (mypol[, 2] - m2) * fact + m2)
}
getDataInfo <- function(csite_list) {
data_list <- list()
# Loop over the data list and extract some useful information.
for (i in 1:length(csite_list)) {
sname <- csite_list[[i]]$GWSDAT_Options$SiteName
aname <- csite_list[[i]]$Aquifer
cnames <- csite_list[[i]]$All.Data$cont_names
wnames <- csite_list[[i]]$All.Data$sample_loc$names
donotdel <- FALSE
# If the DO_NOT_MODIFY Flag exists, copy it.
if (!is.null(csite_list[[i]]$DO_NOT_MODIFY))
donotdel <- csite_list[[i]]$DO_NOT_MODIFY
if (is.null(data_list[[sname]]))
data_list[[sname]] <- list(Aquifer = aname, csite_idx = i,
contaminants = cnames,
wells = wnames,
do_not_del = donotdel)
else {
data_list[[sname]]$Aquifer[[length(data_list[[sname]]$Aquifer) + 1]] <- aname
data_list[[sname]]$csite_idx = c(data_list[[sname]]$csite_idx, i)
data_list[[sname]]$contaminants = cnames
data_list[[sname]]$wells = wnames
data_list[[sname]]$do_not_del = donotdel
}
}
return(data_list)
}
getValidDataName <- function(csite_list = NULL, template = "Area", propose_name = NULL) {
if (is.null(csite_list))
return(template)
# If a name was provided (proposed_name not NULL), check if it already in use.
if (!is.null(propose_name)) {
name_conflicted <- FALSE
for (j in 1:length(csite_list)) {
if (propose_name == csite_list[[j]]$GWSDAT_Options$SiteName) {
name_conflicted <- TRUE
break
}
}
# Return 'propose_name' if no equal name found in 'csite_list'.
if (!name_conflicted)
return(propose_name)
}
# Try a new name using 'template' as prefix for the name.
for (i in 1:1000) {
propose_name <- paste0(template, " ", i)
name_conflicted <- FALSE
# Loop over data sets and check if name already exists.
for (j in 1:length(csite_list)) {
if (propose_name == csite_list[[j]]$GWSDAT_Options$SiteName) {
name_conflicted <- TRUE
break
}
}
if (!name_conflicted) break
}
return(propose_name)
}
getDataIndexByID <- function(csite_list, data_id) {
for (i in 1:length(csite_list))
if (csite_list[[i]]$data_id == data_id)
return(i)
return(-1)
}
createDataID <- function(csite_list = NULL) {
# If no data list was specified, just return a random integer.
if (is.null(csite_list))
return(sample.int(100000, 1))
new_id <- 0
# Loop as long as no unique data id can be found.
while (1) {
new_id <- sample.int(100000, 1)
# Check if the new id already exists.
for (i in 1:length(csite_list))
if (csite_list[[i]]$data_id == new_id)
new_id = -1 # flag as existing
if (new_id != -1)
break # leave while
}
return(new_id)
}
excelDate2Date <- function(excelDate) {
Date <- excelDate + as.Date("1900-01-01") - 2
return(Date)
}
rm_spaces <- function(x){
#Function to remove trailing and leading spaces!
if (!is.character(x)) { stop("not of class character") }
x <- sub('[[:blank:]]+?','',x)
x <- sub(" *$","",x)
return(x)
}
existsNAPL <- function(All.Data, well, solute) {
Well.Data <- All.Data$Cont.Data[as.character(All.Data$Cont.Data$WellName) %in%
well & All.Data$Cont.Data$Constituent %in% solute,]
NAPL.Present <- any("napl" %in% tolower(as.character(Well.Data$Result))) ||
nrow(All.Data$NAPL.Thickness.Data[as.character(All.Data$NAPL.Thickness.Data$WellName) %in% well,]) > 0
if (is.na( NAPL.Present)) { NAPL.Present <- FALSE }
return(NAPL.Present)
}
# Convert array of strings in 'astr' to a single string separated by 'collapse',
# but only include the first 'limit' elements.
pasteLimit <- function(astr, limit = NULL, collapse = ", ") {
if (is.null(limit))
return(paste(astr, collapse = collapse))
if (!is.numeric(limit))
return("Error: limit must be an integer")
limit <- as.integer(limit)
if (limit > length(astr))
return(paste(astr, collapse = collapse))
outstr <- paste(astr[1:limit], collapse = collapse)
outstr <- paste0(outstr, ", ... (", length(astr), ")")
return(outstr)
}