def main(argv): """Get the tillage date.""" huc12 = argv[1] fpath = argv[2] wbfn = "/i/59/wb/%s/%s/%s_%s.wb" % (huc12[:8], huc12[8:], huc12, fpath) wbdf = read_wb(wbfn) wbdf = wbdf[wbdf["ofe"] == 1] (fig, ax) = plt.subplots(1, 1) for threshold in range(15, 50, 5): df = wbdf[wbdf["sw1"] < threshold].groupby("jday").count() df = df.reindex(list(range(1, 367))).fillna(0) vals = (df["sw1"].rolling(7).mean() / 13.0 * 100, ) ax.plot(df.index.values, vals[0], label="%s%%" % (threshold, )) ax.set_xticks([1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]) ax.set_xticklabels(calendar.month_abbr[1:]) ax.grid(True) ax.set_xlabel("* 7 Day Smoother Applied") ax.set_xlim(90, 153) ax.set_ylabel("Frequency [%]") ax.set_title( "huc12: %s fpath: %s\n2007-2019 Frequency of 0-10 cm Soil Moisture below threshold" % (huc12, fpath)) ax.legend() fig.savefig("test.png")
def readfile(huc12, fn): """Read one file please""" try: df = dep_utils.read_wb(fn) except Exception as exp: print("\nABORT: Attempting to read: %s resulted in: %s\n" % (fn, exp)) return None return df
def compute_tillage_date(scenario, row): """Get the tillage date.""" wbfn = "/i/0/wb/%s/%s/%s_%s.wb" % ( row["huc_12"][:8], row["huc_12"][8:], row["huc_12"], row["fpath"], ) wbdf = read_wb(wbfn) wbdf2 = wbdf[((wbdf["ofe"] == 1) & (wbdf["date"] >= APR15) & (wbdf["date"] <= MAY30))] if scenario in THRESHOLDS: the_threshold = THRESHOLDS[scenario] else: the_threshold = get_threshold_bypl(scenario, row) wbdf3 = wbdf2[wbdf2["sw1"] < the_threshold] if len(wbdf3.index) > 0: tillage_date = wbdf3.iloc[0]["date"] else: tillage_date = MAY30 return tillage_date
def main(argv): """Do things""" filename = argv[1] wb = read_wb(filename) (fig, ax) = plt.subplots(2, 1) ax[0].set_title("Water Balance for %s" % (filename.split("/")[-1], )) ax[0].plot(wb['date'], wb['sw']) ax[0].set_ylabel("Total Soil Water [mm]") ax[0].grid(True) for year in [2009, 2012, 2015]: df2 = wb[wb['year'] == year] ax[1].plot(df2['jday'], df2['sw'], label=str(year)) ax[1].legend(ncol=10) ax[1].set_xticks( (1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 365)) ax[1].set_xticklabels(calendar.month_abbr[1:]) ax[1].grid(True) ax[1].set_ylabel("Total Soil Water [mm]") fig.savefig('test.png')
def main(argv): """Do things""" filename = argv[1] wb = read_wb(filename) (fig, ax) = plt.subplots(2, 1) ax[0].set_title("Water Balance for %s" % (filename.split("/")[-1], )) ax[0].plot(wb['date'], wb['sw']) ax[0].set_ylabel("Total Soil Water [mm]") ax[0].grid(True) for year in [2009, 2012, 2015]: df2 = wb[wb['year'] == year] ax[1].plot(df2['jday'], df2['sw'], label=str(year)) ax[1].legend(ncol=10) ax[1].set_xticks((1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 365)) ax[1].set_xticklabels(calendar.month_abbr[1:]) ax[1].grid(True) ax[1].set_ylabel("Total Soil Water [mm]") fig.savefig('test.png')
def do_scenario(scenario, plantdate, hucdf): """Process this scenario.""" index = pd.MultiIndex.from_product( [range(2008, 2019), range(1, 74)], names=["year", "period"]) df = pd.DataFrame(index=index).reset_index() def f(row): """Make date.""" return datetime.date(row["year"], 1, 1) + datetime.timedelta( days=int(row["period"] - 1) * 5 + 2) df["5day_middle_date"] = df.apply(f, axis=1) df = df.set_index(["year", "period"]) smdfs = [] flowpaths = 0 for _, row in hucdf.iterrows(): huc12 = row["HUC12"] for fn in glob.glob("/i/%s/wb/%s/%s/*" % (scenario, huc12[:8], huc12[8:])): smdfs.append(read_wb(fn)) flowpaths += 1 smdf = pd.concat(smdfs) del smdfs envdfs = [] for _, row in hucdf.iterrows(): huc12 = row["HUC12"] for fn in glob.glob("/i/%s/env/%s/%s/*" % (scenario, huc12[:8], huc12[8:])): envdfs.append(read_env(fn)) envdf = pd.concat(envdfs) envdf["jday"] = pd.to_numeric(envdf["date"].dt.strftime("%j"), downcast="integer") del envdfs # only one ofe 1 smdf = smdf[smdf["ofe"] == 1] smdf["period"] = (smdf["jday"] + 5) // 5 envdf["period"] = (envdf["jday"] + 5) // 5 # only consider 2008 thru 2018 data smdf = smdf[(smdf["year"] > 2007) & (smdf["year"] < 2019)] envdf = envdf[(envdf["year"] > 2007) & (envdf["year"] < 2019)] gdf = envdf.groupby(["year", "period"]).mean() df["5day_precip_mm"] = gdf["precip"] df["5day_detach_kgm2"] = gdf["av_det"] gdf = smdf.groupby(["year", "period"]).mean() df["5day_soilmoist"] = gdf["sw1"] gdf = envdf.groupby("year").sum() / flowpaths df = df.join(gdf[["precip", "av_det"]]) df = df.rename( { "precip": "annual_precip_mm", "av_det": "annual_detach_kgm2" }, axis=1) gdf = (smdf[smdf["jday"] == int(plantdate.strftime("%j"))].groupby( "year").mean()) df = df.join(gdf["sw1"]) df = df.rename({"sw1": "plant_soilmoist"}, axis=1) df["plant_date"] = plantdate.strftime("%m %d") df["mlra_id"] = hucdf.iloc[0]["MLRA"] df = df.fillna(0) LOG.info("done with %s %s", plantdate, hucdf.iloc[0]["MLRA"]) return df
def main(argv): """Do things""" dfs = [] for fn in glob.glob("/i/0/wb/07100004/0704/*"): df = read_wb(fn) df["fpath"] = int(fn.split("_")[1][:-3]) dfs.append(df) df = pd.concat(dfs) ranges = df.groupby(["fpath", "ofe"]).describe() year = 2018 for doy in tqdm(range(1, 365)): date = datetime.date(year, 1, 1) + datetime.timedelta(days=(doy - 1)) wb = df[(df["year"] == year) & (df["jday"] == doy)].copy() wb = wb.set_index(["fpath", "ofe"]) for f2 in ["sw1", "sw2", "sw"]: for f1 in ["min", "max"]: wb["%s_%s" % (f2, f1)] = ranges[f2, f1] wb["%s_range" % (f2, )] = (wb["%s_max" % (f2, )] - wb["%s_min" % (f2, )]) wb["%s_percent" % (f2, )] = ((wb[f2] - wb["%s_min" % (f2, )]) / wb["%s_range" % (f2, )] * 100.0) sns.set(style="white", palette="muted", color_codes=True) (fig, ax) = plt.subplots(3, 2, figsize=(7, 7)) sns.despine(left=True) fig.text( 0.5, 0.98, "%s :: Water Balance for 071000040704" % (date.strftime("%d %B %Y"), ), ha="center", ) # --------------------------------------------------- myax = ax[0, 0] sns.distplot(wb["sw1"], hist=False, color="g", ax=myax, rug=True) myax.set_xlabel("0-10cm Soil Water [mm]") myax.axvline(wb["sw1"].mean(), color="r") myax.set_xlim(0, 60) myax = ax[0, 1] sns.distplot(wb["sw1_percent"], hist=False, color="g", ax=myax, rug=True) myax.set_xlabel("0-10cm Soil Water of Capacity [%]") myax.axvline(wb["sw1_percent"].mean(), color="r") myax.set_xlim(0, 100) # --------------------------------------------------------- myax = ax[1, 0] sns.distplot(wb["sw2"], hist=False, color="g", ax=myax, rug=True) myax.set_xlabel("10-20cm Soil Water [mm]") myax.axvline(wb["sw2"].mean(), color="r") myax.set_xlim(0, 60) myax = ax[1, 1] sns.distplot(wb["sw2_percent"], hist=False, color="g", ax=myax, rug=True) myax.set_xlabel("10-20cm Soil Water of Capacity [%]") myax.axvline(wb["sw2_percent"].mean(), color="r") myax.set_xlim(0, 100) # ------------------------------------------------------- myax = ax[2, 0] sns.distplot(wb["sw"], hist=False, color="g", ax=myax, rug=True) myax.set_xlabel("Total Soil Water [mm]") myax.axvline(wb["sw"].mean(), color="r") myax.set_xlim(150, 650) myax = ax[2, 1] sns.distplot(wb["sw_percent"], hist=False, color="g", ax=myax, rug=True) myax.set_xlabel("Total Soil Water of Capacity [%]") myax.axvline(wb["sw_percent"].mean(), color="r") myax.set_xlim(0, 100) plt.setp(ax, yticks=[]) plt.tight_layout() fig.savefig("frames/%05i.png" % (doy - 1, )) plt.close()
def test_wb(): """read a WB file please""" df = dep.read_wb(get_path('wb.txt')) assert abs(df['precip'].max() - 162.04) < 0.01
def test_wb(): """read a WB file please""" df = dep.read_wb(get_path("wb.txt")) assert abs(df["precip"].max() - 162.04) < 0.01
def test_wb(self): """read a WB file please""" df = dep.read_wb(get_path('wb.txt')) self.assertAlmostEquals(df['precip'].max(), 162.04, 2)