Skip to content

Commit

Permalink
updated OFE dump per #57
Browse files Browse the repository at this point in the history
  • Loading branch information
akrherz committed Apr 16, 2018
1 parent df4dc21 commit b1e2e04
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions scripts/util/dump_ofe_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime

import pandas as pd
from tqdm import tqdm
from pyiem.dep import read_ofe, read_man, read_slp

""""
Expand All @@ -23,7 +24,15 @@
N - ??? *see 27 Sep 2016 email from dave, it is unused, so treat as I*
"""
LABEL2CODE = {'soybean2': 'B',
'Soy_2191': 'B',
'Soy_2192': 'B',
'Soy_2193': 'B',
'Soy_2194': 'B',
'Corn': 'C',
'Cor_0967': 'C',
'Cor_0966': 'C',
'Cor_0965': 'C',
'Cor_0964': 'C',
'Tre_2932': 'I',
'bromegr1': 'P',
'Bar_8319': 'W'}
Expand All @@ -40,29 +49,55 @@ def get_rotation_string(manres, ofe):
return "".join(codes)


def get_soils(prjfn):
"""Hack to get soil names from prj and soil file"""
soils = []
for line in open(prjfn):
if line.find('/sol_input/') == -1:
continue
soils.append(line.split("_")[2].split(".")[0])
# now read sol file
names = []
for line in open(prjfn.replace('prj', 'sol')):
if not line.startswith("'"):
continue
names.append(line[1:].split("'")[0])
if len(names) == len(soils):
return soils
# uh oh, more work to do
mapping = {}
i = 0
for name in names:
if name in mapping:
continue
mapping[name] = soils[i]
i += 1
return [mapping[name] for name in names]


def main():
"""Go Main Go"""
# ['id', 'CropRotationString', 'slope',
# 'rainfall', 'runoff', 'detach', 'delivery'])
rows = []
for root, _dirs, files in os.walk("/i/0/ofe"):
for root, _dirs, files in tqdm(os.walk("/i/0/ofe")):
for filename in files:
ofedf = read_ofe("%s/%s" % (root, filename))
# Drop any 2017+ data
ofedf = ofedf[ofedf['date'] < datetime.date(2017, 1, 1)]
# Drop any 2018+ data
ofedf = ofedf[ofedf['date'] < datetime.date(2018, 1, 1)]
# Figure out the crop string
man = "%s/%s" % (root.replace("ofe", "man"),
filename.replace("ofe", "man"))
try:
manres = read_man(man)
except:
print("failure reading %s" % (man,))
except Exception as exp:
print("failure reading %s\n%s" % (man, exp))
continue

slp = "%s/%s" % (root.replace("ofe", "slp"),
filename.replace("ofe", "slp"))
slpres = read_slp(slp)

soils = get_soils(slp.replace('slp', 'prj'))
for ofe in ofedf['ofe'].unique():
myofe = ofedf[ofedf['ofe'] == ofe]
length = slpres[ofe - 1]['x'][-1] - slpres[ofe - 1]['x'][0]
Expand All @@ -76,6 +111,7 @@ def main():
'CropRotationString': (
get_rotation_string(manres, ofe)[:YEARS]),
'slope[1]': slp,
'soil_mukey': soils[ofe - 1],
'rainfall': -1,
'runoff[mm/yr]': myofe['runoff'].sum() / YEARS,
'detach': -1,
Expand Down

0 comments on commit b1e2e04

Please sign in to comment.