def make_plot(huc12, scenario): """Make the map""" import seaborn as sns os.chdir("/i/%s/slp/%s/%s" % (scenario, huc12[:8], huc12[8:])) res = [] for fn in glob.glob("*.slp"): slp = read_slp(fn) bulk = (slp[-1]['y'][-1]) / slp[-1]['x'][-1] length = slp[-1]['x'][-1] if bulk < -1: print("Greater than 100%% slope, %s %s" % (fn, bulk)) continue res.append([(0 - bulk) * 100., length]) data = np.array(res) g = sns.jointplot(data[:, 1], data[:, 0], s=40, stat_func=None, zorder=1, color='tan').plot_joint(sns.kdeplot, n_levels=6) g.ax_joint.set_xlabel("Slope Length [m]") g.ax_joint.set_ylabel("Bulk Slope [%]") g.fig.subplots_adjust(top=.8, bottom=0.2, left=0.15) g.ax_joint.grid() g.ax_marg_x.set_title( ("HUC12 %s DEP Hillslope\n" "Kernel Density Estimate (KDE) Overlain") % (huc12, ), fontsize=10) ram = BytesIO() plt.gcf().set_size_inches(3.6, 2.4) plt.savefig(ram, format='png', dpi=100) ram.seek(0) return ram.read()
def make_plot(huc12, scenario): """Make the map""" import seaborn as sns os.chdir("/i/%s/slp/%s/%s" % (scenario, huc12[:8], huc12[8:])) res = [] for fn in glob.glob("*.slp"): slp = read_slp(fn) bulk = (slp[-1]['y'][-1]) / slp[-1]['x'][-1] length = slp[-1]['x'][-1] if bulk < -1: print("Greater than 100%% slope, %s %s" % (fn, bulk)) continue res.append([(0 - bulk) * 100., length]) data = np.array(res) g = sns.jointplot( data[:, 1], data[:, 0], s=40, stat_func=None, zorder=1, color='tan' ).plot_joint(sns.kdeplot, n_levels=6) g.ax_joint.set_xlabel("Slope Length [m]") g.ax_joint.set_ylabel("Bulk Slope [%]") g.fig.subplots_adjust(top=.8, bottom=0.2, left=0.15) g.ax_joint.grid() g.ax_marg_x.set_title(( "HUC12 %s DEP Hillslope\n" "Kernel Density Estimate (KDE) Overlain") % (huc12, ), fontsize=10) ram = BytesIO() plt.gcf().set_size_inches(3.6, 2.4) plt.savefig(ram, format='png', dpi=100) ram.seek(0) return ram.read()
def main(): """Go Main Go""" # ['id', 'CropRotationString', 'slope', # 'rainfall', 'runoff', 'detach', 'delivery']) rows = [] for root, _dirs, files in tqdm(os.walk("/i/0/ofe")): for filename in files: ofedf = read_ofe("%s/%s" % (root, filename)) # Drop any 2007 or 2018+ data ofedf = ofedf[(ofedf['date'] < datetime.date(2018, 1, 1)) & (ofedf['date'] >= datetime.date(2008, 1, 1))] # Figure out the crop string man = "%s/%s" % (root.replace( "ofe", "man"), filename.replace("ofe", "man")) try: manres = read_man(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] slp = ((slpres[ofe - 1]['y'][0] - slpres[ofe - 1]['y'][-1]) / length) rows.append({ 'id': "%s_%s" % (filename[:-4], ofe), 'huc12': filename[:12], 'fpath': filename.split("_")[1][:-4], 'ofe': ofe, 'CropRotationString': (get_rotation_string(manres, ofe)), 'slope[1]': slp, 'soil_mukey': soils[ofe - 1], 'rainfall': -1, 'runoff[mm/yr]': myofe['runoff'].sum() / YEARS, 'detach': -1, 'length[m]': length, 'delivery[t/a/yr]': (myofe['sedleave'].sum() / YEARS / length * 4.463) }) df = pd.DataFrame(rows) df.to_csv('results.csv', index=False)
def main(): """Go Main Go""" # ['id', 'CropRotationString', 'slope', # 'rainfall', 'runoff', 'detach', 'delivery']) rows = [] for root, _dirs, files in tqdm(os.walk("/i/0/ofe")): for filename in files: ofedf = read_ofe("%s/%s" % (root, filename)) # Drop any 2007 or 2018+ data ofedf = ofedf[(ofedf['date'] < datetime.date(2018, 1, 1)) & (ofedf['date'] >= datetime.date(2008, 1, 1))] # Figure out the crop string man = "%s/%s" % (root.replace("ofe", "man"), filename.replace("ofe", "man")) try: manres = read_man(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] slp = ((slpres[ofe - 1]['y'][0] - slpres[ofe - 1]['y'][-1]) / length) rows.append({ 'id': "%s_%s" % (filename[:-4], ofe), 'huc12': filename[:12], 'fpath': filename.split("_")[1][:-4], 'ofe': ofe, 'CropRotationString': ( get_rotation_string(manres, ofe)), 'slope[1]': slp, 'soil_mukey': soils[ofe - 1], 'rainfall': -1, 'runoff[mm/yr]': myofe['runoff'].sum() / YEARS, 'detach': -1, 'length[m]': length, 'delivery[t/a/yr]': ( myofe['sedleave'].sum() / YEARS / length * 4.463) }) df = pd.DataFrame(rows) df.to_csv('results.csv', index=False)
def main(argv): """Go Main Go""" SCENARIO = int(argv[1]) SLOPE_THRESHOLD = float(argv[2]) # make needed mods # use old one with mods if slope less than threshold # replace rotation if slope greater os.chdir("/i/0/prj") hits = 0 total = 0 for huc8 in tqdm(glob.glob("*")): os.chdir(huc8) for huc4 in glob.glob("*"): os.chdir(huc4) for fn in glob.glob("*.prj"): total += 1 hillslope = fn.split("_")[1].split(".")[0] newfn = "/i/%s/prj/%s/%s/%s" % (SCENARIO, huc8, huc4, fn) # Figure out the slope slpfn = "/i/0/slp/%s/%s/%s%s_%s.slp" % ( huc8, huc4, huc8, huc4, hillslope, ) slp = read_slp(slpfn) bulk = (slp[-1]["y"][-1]) / slp[-1]["x"][-1] slope_percent = (0 - bulk) * 100.0 if slope_percent >= SLOPE_THRESHOLD: hits += 1 lines = open(fn).readlines() for i, line in enumerate(lines): if line.find("EventFile") > 0: lines[i] = line.replace("/i/0/env", "/i/%s/env" % (SCENARIO, )) if (slope_percent >= SLOPE_THRESHOLD and line.find('File = "IDEP2/') > 0): lines[i] = (" File = " '"IDEP2/CSCAP/SWITCHGRASS.rot"\n') fp = open(newfn, "w") fp.write("".join(lines)) fp.close() os.chdir("..") os.chdir("..") print(("Slope: %s hits: %s total: %s ratio: %.2f") % (SLOPE_THRESHOLD, hits, total, hits / float(total) * 100.0))
def read_data(): """Do intensive stuff""" os.chdir("/i/0/slp") res = [] for huc8 in glob.glob("*"): os.chdir(huc8) for huc12 in glob.glob("*"): os.chdir(huc12) for fn in glob.glob("*.slp"): slp = read_slp(fn) bulk = (slp[-1]['y'][-1]) / slp[-1]['x'][-1] if bulk < -1: print("Greater than 100%% slope, %s %s" % (fn, bulk)) continue res.append((0 - bulk) * 100.) os.chdir("..") os.chdir("..") df = pd.DataFrame({'slopes': res}) df.to_csv('/tmp/slopes.csv', index=False)
def main(argv): """Go Main Go""" SCENARIO = int(argv[1]) SLOPE_THRESHOLD = float(argv[2]) # make needed mods # use old one with mods if slope less than threshold # replace rotation if slope greater os.chdir("/i/0/prj") hits = 0 total = 0 for huc8 in tqdm(glob.glob("*")): os.chdir(huc8) for huc4 in glob.glob("*"): os.chdir(huc4) for fn in glob.glob("*.prj"): total += 1 hillslope = fn.split("_")[1].split(".")[0] newfn = "/i/%s/prj/%s/%s/%s" % (SCENARIO, huc8, huc4, fn) # Figure out the slope slpfn = "/i/0/slp/%s/%s/%s%s_%s.slp" % (huc8, huc4, huc8, huc4, hillslope) slp = read_slp(slpfn) bulk = (slp[-1]['y'][-1]) / slp[-1]['x'][-1] slope_percent = (0 - bulk) * 100. if slope_percent >= SLOPE_THRESHOLD: hits += 1 lines = open(fn).readlines() for i, line in enumerate(lines): if line.find("EventFile") > 0: lines[i] = line.replace("/i/0/env", "/i/%s/env" % (SCENARIO, )) if (slope_percent >= SLOPE_THRESHOLD and line.find('File = "IDEP2/') > 0): lines[i] = (' File = ' '"IDEP2/CSCAP/SWITCHGRASS.rot"\n') fp = open(newfn, 'w') fp.write("".join(lines)) fp.close() os.chdir("..") os.chdir("..") print(("Slope: %s hits: %s total: %s ratio: %.2f" ) % (SLOPE_THRESHOLD, hits, total, hits / float(total) * 100.))
def test_slp(): """Read a slope file""" slp = dep.read_slp(get_path('slp.txt')) assert len(slp) == 5 assert abs(slp[4]['y'][-1] + 8.3) < 0.1
def test_slp(): """Read a slope file""" slp = dep.read_slp(get_path("slp.txt")) assert len(slp) == 5 assert abs(slp[4]["y"][-1] + 2.91) < 0.01 assert abs(slp[4]["slopes"][-1] - 0.033) < 0.01
def test_slp(self): """Read a slope file""" slp = dep.read_slp(get_path('slp.txt')) self.assertEquals(len(slp), 5) self.assertAlmostEquals(slp[4]['y'][-1], -8.3, 1)
def read_data(): """Do intensive stuff""" scenarios = [] res = [] peakslopes = [] lengths = [] for huc12 in [ "070600060701", "070801020905", "101702040703", "070600020602", "071000090201", "102300020202", "102300060407", "071000091101", "101702032003", "071000081503", "102300031404", "070801021001", "101702031903", "071000070402", "102400030501", "070600040401", "102300030402", "070802050304", "070801060604", "071000061401", "102802010402", "102400090404", "070802070602", "071000050703", "070802090401", "102400050501", "070802060403", ]: for scenario in [0, 1002]: os.chdir("/i/%s/slp/%s/%s" % (scenario, huc12[:8], huc12[8:])) for fn in glob.glob("*.slp"): slp = read_slp(fn) # The peak instantaneous slope maxval = 0 for seg in slp: maxval = max([maxval, np.max(seg["slopes"])]) # The bulk slope bulk = (slp[-1]["y"][-1]) / slp[-1]["x"][-1] res.append((0 - bulk) * 100.0) scenarios.append(scenario) peakslopes.append(maxval * 100.0) lengths.append(slp[-1]["x"][-1]) # check for sanity if (res[-1] - 0.01) > peakslopes[-1]: print("max: %s bulk: %s" % (res[-1], peakslopes[-1])) print(slp) print(fn) sys.exit(1) df = pd.DataFrame( { "slopes": res, "scenario": scenarios, "maxslope": peakslopes, "length": lengths, } ) df.to_csv("/tmp/slopes.csv", index=False)