From b1e2e04a8209071f0e68604d3b43769f2170a02e Mon Sep 17 00:00:00 2001 From: akrherz Date: Mon, 16 Apr 2018 12:17:31 -0500 Subject: [PATCH] updated OFE dump per #57 --- scripts/util/dump_ofe_results.py | 48 ++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/scripts/util/dump_ofe_results.py b/scripts/util/dump_ofe_results.py index c7552353..dc72029b 100644 --- a/scripts/util/dump_ofe_results.py +++ b/scripts/util/dump_ofe_results.py @@ -4,6 +4,7 @@ import datetime import pandas as pd +from tqdm import tqdm from pyiem.dep import read_ofe, read_man, read_slp """" @@ -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'} @@ -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] @@ -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,