-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
148 lines (111 loc) · 3.9 KB
/
util.py
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
# ===============================================================================
# Copyright 2022 ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
import json
import datetime
import requests
from constants import ST2, DEBUG_OBS, DTFORMAT, DEBUG_LIMIT_OBS, AQUIFER_PVACD_MAP
def floatfmt(t, n=2):
if t is None:
return ""
try:
f = float(t)
return f"{f:0.{n}f}"
except ValueError:
return t
FORMATION_MAP = None
def get_formation_name(code):
global FORMATION_MAP
if FORMATION_MAP is None:
with open("./data/formations.json") as rfile:
FORMATION_MAP = json.load(rfile)
def get(c):
for f in FORMATION_MAP:
if f["Code"] == c:
return f["Meaning"]
else:
return c
fs = []
for gf in code.split("/"):
gfname = get(gf)
# fs.append(f"{gfname} ({gf})")
fs.append(gfname)
formation = "/".join(fs)
return formation
def extract_usgs_timeseries(obj):
# print(obj.keys())
ts = obj["value"]["timeSeries"]
# print(len(ts))
data = []
xs = []
ys = []
for i, ti in enumerate(ts):
# print(ti.keys(), ti['variable'].keys(), ti['variable']['variableCode'][0])
# print(ti['variable']['variableName'])
# if ti['variable']['variableCode'][0]['variableID'] == 52331280:
if (
ti["variable"]["variableName"]
== "Depth to water level, ft below land surface"
):
for j, tj in enumerate(ti["values"]):
values = tj["value"]
data.append(
{
"phenomenonTime": values[0]["dateTime"],
"result": values[0]["value"],
}
)
return data
# if data:
# xs, ys = zip(*[(x["dateTime"], x["value"]) for x in data])
# return xs, ys
def prep_hydrovu_name(n):
return n.split("level")[0].split("Level")[0]
def make_customdata(locations, tag):
fs = []
for l in locations:
tprops = l["Things"][0]["properties"]
if tag == "pvacd_hydrovu":
code = "313SADR"
else:
code = tprops.get("geologic_formation")
customdata = []
name = ""
if code:
name = get_formation_name(code)
aquifer = tprops.get("aquifer", "")
aquifer_pvacd_name = AQUIFER_PVACD_MAP.get(aquifer, "")
customdata.append(f"Aquifer: {aquifer_pvacd_name}")
# customdata.append(f"Formation: {name}")
# customdata.append(f"Aquifer: {aquifer}")
# model_formation = tprops.get("model_formation", "")
# customdata.append(f"Model Formation: {model_formation}")
welldepth = tprops.get("well_depth", "")
if welldepth:
welldepth = f"{welldepth} (ft)"
customdata.append(f"Well Depth: {welldepth}")
customdata = "<br>".join(customdata)
fs.append(customdata)
return fs
def get_tprop(prop, k):
def camelcase(n):
args = (ni.capitalize() for ni in n.split("_"))
return "".join(args)
return prop.get(k, prop.get(camelcase(k)))
def todatetime(t, fmt=DTFORMAT):
if isinstance(t, dict):
t = t["phenomenonTime"]
return datetime.datetime.strptime(t, fmt)
# ============= EOF =============================================