|
| 1 | +##### |
| 2 | +#Validate query input |
| 3 | +##### |
| 4 | +wiDB_validate = function(minLat, maxLat, minLong, maxLong, minElev, maxElev, |
| 5 | + minDate, maxDate, countries, states, types, projects){ |
| 6 | + |
| 7 | + qStr = "" |
| 8 | + |
| 9 | + if(!is.null(minLat)){ |
| 10 | + if(class(minLat) != "numeric"){stop("minLat must be numeric")} |
| 11 | + qStr = paste0(qStr, "&minLat=", minLat) |
| 12 | + } |
| 13 | + if(!is.null(maxLat)){ |
| 14 | + if(class(maxLat) != "numeric"){stop("maxLat must be numeric")} |
| 15 | + qStr = paste0(qStr, "&maxLat=", maxLat) |
| 16 | + } |
| 17 | + if(!is.null(minLong)){ |
| 18 | + if(class(minLong) != "numeric"){stop("minLong must be numeric")} |
| 19 | + qStr = paste0(qStr, "&minLong=", minLong) |
| 20 | + } |
| 21 | + if(!is.null(maxLong)){ |
| 22 | + if(class(maxLong) != "numeric"){stop("maxLong must be numeric")} |
| 23 | + qStr = paste0(qStr, "&maxLong=", maxLong) |
| 24 | + } |
| 25 | + if(!is.null(minElev)){ |
| 26 | + if(class(minElev) != "numeric"){stop("minElev must be numeric")} |
| 27 | + qStr = paste0(qStr, "&minElev=", minElev) |
| 28 | + } |
| 29 | + if(!is.null(maxElev)){ |
| 30 | + if(class(maxElev) != "numeric"){stop("maxElev must be numeric")} |
| 31 | + qStr = paste0(qStr, "&maxElev=", maxElev) |
| 32 | + } |
| 33 | + if(!is.null(minDate)){ |
| 34 | + if(class(minDate) != "character"){stop("minDate must be string")} |
| 35 | + td = c(as.numeric(substr(minDate, 1, 4)), as.numeric(substr(minDate, 6, 7)), |
| 36 | + as.numeric(substr(minDate, 9, 10))) |
| 37 | + if(NA %in% td){stop("minDate format must be YYYY-MM-DD")} |
| 38 | + qStr = paste0(qStr, "&minDate=", minDate) |
| 39 | + } |
| 40 | + if(!is.null(maxDate)){ |
| 41 | + if(class(maxDate) != "character"){stop("maxDate must be string")} |
| 42 | + td = c(as.numeric(substr(maxDate, 1, 4)), as.numeric(substr(maxDate, 6, 7)), |
| 43 | + as.numeric(substr(maxDate, 9, 10))) |
| 44 | + if(NA %in% td){stop("maxDate format must be YYYY-MM-DD")} |
| 45 | + qStr = paste0(qStr, "&maxDate=", maxDate) |
| 46 | + } |
| 47 | + if(!is.null(countries)){ |
| 48 | + if(class(countries) != "character"){stop("countries must be string")} |
| 49 | + countries = gsub(" ", "", countries) |
| 50 | + countries = gsub(" ", "", countries) |
| 51 | + if(length(countries > 1)){ |
| 52 | + countries = paste0(countries, collapse = ",") |
| 53 | + } |
| 54 | + qStr = paste0(qStr, "&countries=", countries) |
| 55 | + } |
| 56 | + if(!is.null(states)){ |
| 57 | + if(class(states) != "character"){stop("states must be string")} |
| 58 | + states = gsub(" ", "", states) |
| 59 | + states = gsub(" ", "", states) |
| 60 | + if(length(states > 1)){ |
| 61 | + states = paste0(states, collapse = ",") |
| 62 | + } |
| 63 | + qStr = paste0(qStr, "&states=", states) |
| 64 | + } |
| 65 | + if(!is.null(types)){ |
| 66 | + if(class(types) != "character"){stop("types must be string")} |
| 67 | + types = gsub(" ", "", types) |
| 68 | + types = gsub(" ", "", types) |
| 69 | + if(length(types > 1)){ |
| 70 | + types = paste0(types, collapse = ",") |
| 71 | + } |
| 72 | + qStr = paste0(qStr, "&types=", types) |
| 73 | + } |
| 74 | + if(!is.null(projects)){ |
| 75 | + if(class(projects) != "character"){stop("projects must be string")} |
| 76 | + projects = gsub(" ", "", projects) |
| 77 | + projects = gsub(" ", "", projects) |
| 78 | + if(length(types > 1)){ |
| 79 | + types = paste0(types, collapse = ",") |
| 80 | + } |
| 81 | + qStr = paste0(qStr, "&projects=", projects) |
| 82 | + } |
| 83 | + |
| 84 | + if(nchar(qStr) == 0){stop("No query arguments provided")} |
| 85 | + |
| 86 | + qStr = paste0("?", substr(qStr, 2, nchar(qStr))) |
| 87 | + |
| 88 | + return(qStr) |
| 89 | +} |
| 90 | + |
| 91 | +##### |
| 92 | +#Find sites |
| 93 | +##### |
| 94 | +wiDB_sites = function(minLat = NULL, maxLat = NULL, minLong = NULL, maxLong = NULL, |
| 95 | + minElev = NULL, maxElev = NULL, minDate = NULL, maxDate = NULL, |
| 96 | + countries = NULL, states = NULL, types = NULL, projects = NULL){ |
| 97 | + |
| 98 | + qStr = wiDB_validate(minLat, maxLat, minLong, maxLong, minElev, maxElev, |
| 99 | + minDate, maxDate, countries, states, types, projects) |
| 100 | + |
| 101 | + baseStr = "https://wateriso.utah.edu/api/v1/sites.php" |
| 102 | + q = paste0(baseStr, qStr) |
| 103 | + d = GET(q) |
| 104 | + |
| 105 | + if(d$status_code != 200){stop(paste("Request returned error code", d$status_code))} |
| 106 | + |
| 107 | + resp = fromJSON(content(d, as = "text", encoding = "UTF-8")) |
| 108 | + |
| 109 | + if(length(resp$sites) == 0){ |
| 110 | + warning("No sites returned") |
| 111 | + return(NULL) |
| 112 | + } |
| 113 | + |
| 114 | + return(resp$sites) |
| 115 | +} |
| 116 | + |
| 117 | +##### |
| 118 | +#Obtain data |
| 119 | +##### |
| 120 | +wiDB_data = function(minLat = NULL, maxLat = NULL, minLong = NULL, maxLong = NULL, |
| 121 | + minElev = NULL, maxElev = NULL, minDate = NULL, maxDate = NULL, |
| 122 | + countries = NULL, states = NULL, types = NULL, projects = NULL, |
| 123 | + fields = NULL, tmpdir = tempdir(), clean = TRUE){ |
| 124 | + |
| 125 | + qStr = wiDB_validate(minLat, maxLat, minLong, maxLong, minElev, maxElev, |
| 126 | + minDate, maxDate, countries, states, types, projects) |
| 127 | + |
| 128 | + if(!dir.exists(tmpdir)){ |
| 129 | + warning("Directory doesn't exist, trying to create") |
| 130 | + dir.create(tmpdir) |
| 131 | + if(!dir.exists(tmpdir)){stop("Unable to create directory")} |
| 132 | + } |
| 133 | + |
| 134 | + if(class(clean) != "logical"){stop("clean must be TRUE/FALSE")} |
| 135 | + |
| 136 | + flist = c("Site_ID", "Site_Name", "Latitude", "Longitude", "Elevation", |
| 137 | + "Sample_ID", "Type", "Start_Date", "Start_Time_Zone", |
| 138 | + "Collection_Date", "Collection_Time_Zone", "Phase", |
| 139 | + "Depth_meters", "Sample_Comments", "d2H", "d18O", |
| 140 | + "d2H_Analytical_SD", "d18O_Analytical_SD", "WI_Analysis_Source", |
| 141 | + "Project_ID") |
| 142 | + |
| 143 | + if(!is.null(fields)){ |
| 144 | + if(class(fields) != "character"){stop("fields must be a string")} |
| 145 | + fields = gsub(" ", "", fields) |
| 146 | + fields = gsub(" ", "", fields) |
| 147 | + fels = strsplit(fields, ",") |
| 148 | + fels = fels[[1]] |
| 149 | + for(i in 1:length(fels)){ |
| 150 | + if(!(fels[i] %in% flist)){stop(paste("Value", i, "in fields is not a valid field name"))} |
| 151 | + } |
| 152 | + qStr = paste0(qStr, "&return=", fields) |
| 153 | + } |
| 154 | + |
| 155 | + baseStr = "https://wateriso.utah.edu/api/v1/download.php" |
| 156 | + q = paste0(baseStr, qStr) |
| 157 | + g = GET(q) |
| 158 | + |
| 159 | + if(g$status_code != 200){stop(paste("Request returned error code", g$status_code))} |
| 160 | + |
| 161 | + fn = g$headers$`content-disposition` |
| 162 | + fn = strsplit(fn, "=")[[1]][2] |
| 163 | + writeBin(g$content, paste0(tmpdir, "/", fn)) |
| 164 | + |
| 165 | + #unzip and output .csv |
| 166 | + unzip(paste0(tmpdir, "/", fn), exdir = paste0(tmpdir, "/downloads")) |
| 167 | + |
| 168 | + #get and order file list |
| 169 | + froot = strsplit(fn, "-")[[1]][1] |
| 170 | + df = paste0(tmpdir, "/downloads/", froot, "-data.csv") |
| 171 | + pf = paste0(tmpdir, "/downloads/", froot, "-project.csv") |
| 172 | + |
| 173 | + if(file.size(df) == 0){ |
| 174 | + file.remove(c(paste0(tmpdir, "/", fn), df, pf)) |
| 175 | + warning("No records returned") |
| 176 | + return(NULL) |
| 177 | + } |
| 178 | + |
| 179 | + #read in data |
| 180 | + d = read.csv(df) |
| 181 | + |
| 182 | + #read in projects |
| 183 | + p = read.csv(pf) |
| 184 | + |
| 185 | + file.remove(paste0(tmpdir, "/", fn)) |
| 186 | + |
| 187 | + if(clean){ |
| 188 | + file.remove(c(df, pf)) |
| 189 | + } |
| 190 | + |
| 191 | + return(list("data" = d, "projects" = p)) |
| 192 | +} |
| 193 | + |
| 194 | +##### |
| 195 | +#Get field values |
| 196 | +##### |
| 197 | +wiDB_values = function(fields){ |
| 198 | + |
| 199 | + if(!is.character(fields)){ |
| 200 | + stop("fields values must be character strings") |
| 201 | + } |
| 202 | + for(i in 1:length(fields)){ |
| 203 | + if(!(fields[i] %in% c("countries", "states", "types", "projects", |
| 204 | + "Site_ID", "Sample_ID", "WI_Analysis_ID", |
| 205 | + "Climate_ID"))){ |
| 206 | + stop("One or more fields values not supported") |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + baseStr = "https://wateriso.utah.edu/api/v1/values.php?fields=" |
| 211 | + q = baseStr |
| 212 | + for(i in fields){ |
| 213 | + q = paste0(q, i, ",") |
| 214 | + } |
| 215 | + q = substr(q, 1, nchar(q) - 1) |
| 216 | + d = GET(q) |
| 217 | + |
| 218 | + if(d$status_code != 200){stop(paste("Request returned error code", d$status_code))} |
| 219 | + |
| 220 | + resp = fromJSON(content(d, as = "text", encoding = "UTF-8")) |
| 221 | + |
| 222 | + return(resp) |
| 223 | +} |
0 commit comments