Skip to content

Commit 7c55d15

Browse files
committed
fixing tests
1 parent a8b9978 commit 7c55d15

File tree

4 files changed

+252
-202
lines changed

4 files changed

+252
-202
lines changed

dms_datastore/download_ncro.py

+45-38
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ def download_ncro_inventory(dest, cache=True):
4040
url = "https://data.cnra.ca.gov/dataset/fcba3a88-a359-4a71-a58c-6b0ff8fdc53f/resource/cdb5dd35-c344-4969-8ab2-d0e2d6c00821/download/station-trace-download-links.csv"
4141

4242
max_attempt = 10
43-
for attempt in range(1,(max_attempt+1)):
43+
for attempt in range(1, (max_attempt + 1)):
4444
logger.info(f"Downloading inventory for NCRO attempt #{attempt}")
4545
try:
4646
response = urllib.request.urlopen(url, context=ctx).read()
47-
fio=io.BytesIO(response)
47+
fio = io.BytesIO(response)
4848
idf = pd.read_csv(
49-
fio,
50-
header=0,
51-
parse_dates=["start_time", "end_time"],
49+
fio,
50+
header=0,
51+
parse_dates=["start_time", "end_time"],
5252
)
5353

5454
idf = idf.loc[
55-
(idf.station_type != "Groundwater") & (idf.output_interval =="RAW"), :
56-
]
55+
(idf.station_type != "Groundwater") & (idf.output_interval == "RAW"), :
56+
]
5757
logger.info(idf)
58-
58+
5959
idf.to_csv(
6060
os.path.join(dest, ncro_inventory_file),
6161
sep=",",
@@ -68,6 +68,7 @@ def download_ncro_inventory(dest, cache=True):
6868
raise Exception("Could not open inventory.")
6969
continue
7070

71+
7172
def ncro_variable_map():
7273
varmap = pd.read_csv("variable_mappings.csv", header=0, comment="#")
7374
return varmap.loc[varmap.src_name == "wdl", :]
@@ -91,28 +92,28 @@ def ncro_variable_map():
9192
"Turbidity": "turbidity",
9293
"Flow": "flow",
9394
"Salinity": "salinity",
94-
"ECat25C":"ec",
95-
"StreamFlow":"flow",
95+
"ECat25C": "ec",
96+
"StreamFlow": "flow",
9697
"WaterTemp": "temp",
9798
"WaterTempADCP": "temp",
9899
"DissolvedOxygen": "do",
99100
"DissolvedOxygenPercentage": None,
100101
"StreamLevel": "elev",
101102
"WaterSurfaceElevationNAVD88": "elev",
102-
"fDOM": "fdom"
103+
"fDOM": "fdom",
103104
}
104105

105106

106107
def download_station_period_record(row, dbase, dest, variables, failures, ctx):
107-
"""Downloads station/param combo period of record """
108+
"""Downloads station/param combo period of record"""
108109
agency_id = row.station_number
109110
param = row.parameter
110111
if param in mappings.keys():
111112
var = mappings[param]
112113
if var is None:
113114
return
114115
if var not in variables:
115-
return
116+
return
116117
else:
117118
logger.info(f"Problem on row: {row}")
118119
if type(param) == float:
@@ -151,46 +152,47 @@ def download_station_period_record(row, dbase, dest, variables, failures, ctx):
151152
while attempt < max_attempt:
152153
attempt = attempt + 1
153154
try:
154-
if attempt > 16:
155+
if attempt > 16:
155156
logger.info(f"{station_id} attempt {attempt}")
156157
if attempt > 16:
157158
logger.info(fname)
158-
159+
159160
ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
160161
ctx.options |= 0x4
161162
response = urllib.request.urlopen(link_url, context=ctx)
162-
#response = urllib.request.get(url).content
163+
# response = urllib.request.get(url).content
163164
station_html = response.read().decode().replace("\r", "")
164-
165+
165166
break
166167
except Exception as e:
167168
if attempt == max_attempt:
168-
logger.warning(f"Failure in URL request or reading the response after {attempt} tries for station {station_id} param {param}. Link=\n{link_url}\nException below:")
169+
logger.warning(
170+
f"Failure in URL request or reading the response after {attempt} tries for station {station_id} param {param}. Link=\n{link_url}\nException below:"
171+
)
169172
logger.exception(e)
170173
failures.append((station_id, agency_id, var, param))
171174
attampt = 0
172175
return
173-
else:
174-
time.sleep(attempt) # Wait one second more second each time to clear any short term bad stuff
176+
else:
177+
time.sleep(
178+
attempt
179+
) # Wait one second more second each time to clear any short term bad stuff
175180
if len(station_html) > 30 and not "No sites found matching" in station_html:
176181
found = True
177-
if attempt > 1: logger.info(f"{station_id} found on attempt {attempt}")
182+
if attempt > 1:
183+
logger.info(f"{station_id} found on attempt {attempt}")
178184
with open(fpath, "w") as f:
179185
f.write(station_html)
180186
else:
181187
logger.info(f"{station_id} not found after attempt {attempt}")
182188
logger.info("Station %s produced no data" % station)
183189
failures.append((station_id, agency_id, var, param))
184-
return
190+
return
185191

186192

187-
def download_ncro_period_record(
188-
inventory,
189-
dbase,
190-
dest,
191-
variables=None):
192-
193-
if variables is None:
193+
def download_ncro_period_record(inventory, dbase, dest, variables=None):
194+
195+
if variables is None:
194196
variables = ["flow", "elev", "ec", "temp", "do", "ph", "turbidity", "cla"]
195197
global mappings
196198
# mappings = ncro_variable_map()
@@ -234,13 +236,18 @@ def download_ncro_por(dest, variables=None):
234236
| upper_station.isin(dbase.agency_id + "Q")
235237
)
236238
if variables is None:
237-
variables = ["flow","velocity","elev", "ec", "temp", "do", "ph", "turbidity", "cla"]
238-
download_ncro_period_record(
239-
idf.loc[is_in_dbase, :],
240-
dbase,
241-
dest,
242-
variables
243-
)
239+
variables = [
240+
"flow",
241+
"velocity",
242+
"elev",
243+
"ec",
244+
"temp",
245+
"do",
246+
"ph",
247+
"turbidity",
248+
"cla",
249+
]
250+
download_ncro_period_record(idf.loc[is_in_dbase, :], dbase, dest, variables)
244251

245252

246253
def create_arg_parser():
@@ -263,7 +270,7 @@ def create_arg_parser():
263270
nargs="+",
264271
default=None,
265272
help="Parameters to download.",
266-
)
273+
)
267274
return parser
268275

269276

@@ -274,7 +281,7 @@ def main():
274281
por = args.por
275282
variables = args.param
276283
dest = "."
277-
download_ncro_por(destdir,variables)
284+
download_ncro_por(destdir, variables)
278285

279286

280287
if __name__ == "__main__":

0 commit comments

Comments
 (0)