Skip to content

Commit 850d259

Browse files
EliEli
authored andcommitted
Update units lookup and formatting.
1 parent 1888d5c commit 850d259

File tree

1 file changed

+77
-10
lines changed

1 file changed

+77
-10
lines changed

dms_datastore/reformat.py

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from dms_datastore.write_ts import *
1313
from dms_datastore.populate_repo import interpret_fname
1414
from dms_datastore.dstore_config import config_file, station_dbase
15-
from .logging_config import logger
15+
from dms_datastore.logging_config import logger
1616

1717

1818
__all__ = ["reformat","reformat_main",
@@ -97,19 +97,28 @@ def noaa_unit(fname):
9797
header = read_yaml_header(fname)
9898
return header['unit']
9999

100-
101-
def ncro_unit(header_text, param):
100+
def ncro_unit_json(header_text,param):
101+
"""Use this if the file is translated from json and marked as format dwr-ncro-json"""
102102
unit_reformat = {"feet": "feet",
103-
"microsiemens/cm": "microS/cm",
103+
"foot": "feet",
104+
"microsiemens/cm": "uS/cm",
105+
"mgl":"mg/l,",
106+
"ut":"ug/l",
104107
"mg/l": "mg/l",
105108
"milligrams/litre": "mg/l", "cfs": "ft^3/s",
106109
"cubic feet/second": "ft^3/s",
107-
"ph": "pH", "PSU": "psu",
110+
"cfs": "ft^3/s",
111+
"fts": "ft/s",
112+
"ph": "pH",
113+
"PSU": "psu",
114+
"degf": "deg_f",
108115
"degrees farenheit": "deg_f",
116+
"degc": "deg_c",
109117
"degrees celsius": "deg_c",
110118
"feet/second": "ft/s",
111119
"us/cm": "uS/cm",
112-
"ntu": "NTU", "fnu": "FNU",
120+
"ntu": "NTU",
121+
"fnu": "FNU",
113122
"degrees c": "deg_c",
114123
"micrograms/litre": "ug/l",
115124
"pss-15": "psu",
@@ -122,7 +131,64 @@ def ncro_unit(header_text, param):
122131
"velocity": "ft/s",
123132
"flow": "ft^3/s",
124133
"fdom": "ug/l",
125-
"do": "mg/l"}
134+
"do": "mg/l"}
135+
136+
yml = parse_yaml_header(header_text)
137+
if 'agency_unit' in yml:
138+
agency_unit = yml['agency_unit']
139+
if agency_unit.lower() == "misc":
140+
unit = param_defaults[param] if param in param_defaults else "unknown"
141+
else:
142+
unit = unit_reformat[agency_unit.lower()] if agency_unit.lower() in unit_reformat else "unknown"
143+
else:
144+
unit = param_defaults[param] if param in param_defaults else "unknown"
145+
146+
#print("agency_unit ", agency_unit," ", unit)
147+
148+
if agency_unit.lower() in unit_reformat:
149+
return unit_reformat[agency_unit.lower()]
150+
elif agency_variable in var_to_unit:
151+
return var_to_unit[agency_variable]
152+
else:
153+
print(f"Unrecognized variable/unit {agency_variable}, {agency_unit}")
154+
raise ValueError(f"unrecognized variable/unit {agency_variable}, {agency_unit}")
155+
return("unknown")
156+
157+
def ncro_unit(header_text, param):
158+
if len(header_text) > 15 and 'format: dwr-ncro-json' in header_text:
159+
return ncro_unit_json(header_text,param)
160+
161+
unit_reformat = {"feet": "feet",
162+
"foot": "feet",
163+
"microsiemens/cm": "uS/cm",
164+
"mgl":"mg/l,",
165+
"ut":"ug/l",
166+
"mg/l": "mg/l",
167+
"milligrams/litre": "mg/l", "cfs": "ft^3/s",
168+
"cfs": "ft^3/s",
169+
"cubic feet/second": "ft^3/s",
170+
"fts": "ft/s",
171+
"ph": "pH",
172+
"PSU": "psu",
173+
"degrees farenheit": "deg_f",
174+
"degrees celsius": "deg_c",
175+
"feet/second": "ft/s",
176+
"us/cm": "uS/cm",
177+
"ntu": "NTU", "fnu": "FNU",
178+
"degrees c": "deg_c",
179+
"micrograms/litre": "ug/l",
180+
"pss-15": "psu",
181+
"quinine sulfate unit": "ug/l" # for fdom
182+
}
183+
var_to_unit = {"pH": "pH", "Flow": "ft^3/s", "Conductivity": "uS/cm", }
184+
param_defaults = {"ec": "uS/cm",
185+
"turbidity": "NTU",
186+
"elev": "feet",
187+
"temp": "deg_c",
188+
"velocity": "ft/s",
189+
"flow": "ft^3/s",
190+
"fdom": "ug/l",
191+
"do": "mg/l"}
126192
# 860.00 - pH () " header_text comes in without comments \s\((\.?)\)
127193
var = re.compile("[0-9\.]+\s-\s(.+)\((.*)\)")
128194
parsing = False
@@ -160,7 +226,7 @@ def ncro_unit(header_text, param):
160226
def usgs_unit(header_text):
161227

162228
unit_remap = {"ft":"feet","ft3/s":"ft^3/s",
163-
"uS/cm @25C":"microS/cm","ft":"feet",
229+
"uS/cm @25C":"uS/cm","ft":"feet",
164230
"ft/sec":"ft/s","m/sec":"m/s",
165231
"ng/m3":"ng/m^3","deg C":"deg_c"}
166232
parsing = False
@@ -208,8 +274,9 @@ def infer_internal_meta_for_file(fpath):
208274
meta_out["projection_y_coordinate"] = slookup.loc[station_id, 'y']
209275
meta_out["projection_authority_id"] = "epsg:26910"
210276
meta_out["crs_note"] = "Reported lat-lon are agency provided. Projected coordinates may have been revised based on additional information."
211-
original_hdr = ncro_header(
212-
fpath) if meta_out["source"] == "ncro" else original_header(fpath, "#")
277+
original_hdr = original_header(fpath, '#')
278+
if meta_out["source"] == "ncro" and len(original_hdr) == 0:
279+
original_hdr = ncro_header(fpath)
213280
if source == "cdec":
214281
unit, agency_unit = cdec_unit(fpath)
215282
meta_out["unit"] = unit

0 commit comments

Comments
 (0)