forked from dailyerosion/dep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dailyerosion#221 from akrherz/240408
Omnibus
- Loading branch information
Showing
5 changed files
with
236 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""A four panel plot diagnostic showing the limiting factor.""" | ||
|
||
import click | ||
import geopandas as gpd | ||
import numpy as np | ||
import pandas as pd | ||
from pyiem.database import get_sqlalchemy_conn | ||
from pyiem.plot import figure | ||
from pyiem.util import load_geodf | ||
|
||
|
||
@click.command() | ||
@click.option("--date", "dt", type=click.DateTime(), help="Date to plot.") | ||
def main(dt): | ||
"""Go Main Go.""" | ||
with get_sqlalchemy_conn("idep") as conn: | ||
huc12df = gpd.read_postgis( | ||
"SELECT huc_12, st_transform(simple_geom, 4326) as geom " | ||
"from huc12 where scenario = 0", | ||
conn, | ||
geom_col="geom", | ||
index_col="huc_12", | ||
) | ||
|
||
huc12data = pd.read_feather( | ||
f"/mnt/idep2/data/huc12status/{dt:%Y}/{dt:%Y%m%d}.feather" | ||
) | ||
huc12df = huc12df.join(huc12data, how="inner") | ||
|
||
states = load_geodf("us_states", 4326) | ||
|
||
fig = figure( | ||
title=f"{dt:%d %b %Y} Dynamic Tillage Limiting Factor", | ||
subtitle="Red indicates limiting factor", | ||
logo="dep", | ||
figsize=(10.24, 7.68), | ||
) | ||
opts = [ | ||
[0.03, 0.03, "Plastic Limit", "limited_by_soilmoisture"], | ||
[0.5, 0.03, "Combined Limit", "limited"], | ||
[0.03, 0.48, "Soil Temp Limit", "limited_by_soiltemp"], | ||
[0.5, 0.48, "Rainfall Limit", "limited_by_precip"], | ||
] | ||
for opt in opts: | ||
ax = fig.add_axes([opt[0], opt[1], 0.42, 0.38]) | ||
ax.set_xticklabels([]) | ||
ax.set_yticklabels([]) | ||
title = ( | ||
f"{opt[2]} {huc12df[opt[3]].sum()}/{len(huc12df)} " | ||
f"{huc12df[opt[3]].sum() / len(huc12df):.1%}" | ||
) | ||
ax.set_title(title, fontsize=16) | ||
# plot in red when limited is true, else blue | ||
huc12df.plot( | ||
ax=ax, | ||
aspect=None, | ||
color=np.where(huc12df[opt[3]], "r", "b"), | ||
) | ||
xlim = ax.get_xlim() | ||
ylim = ax.get_ylim() | ||
states.plot(ax=ax, color="None", edgecolor="k", lw=0.5, aspect=None) | ||
ax.set_xlim(xlim) | ||
ax.set_ylim(ylim) | ||
fig.savefig(f"huc12limiter_{dt:%Y%m%d}.png") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
"""Plot our planting progress vs that of NASS.""" | ||
|
||
import click | ||
import geopandas as gpd | ||
import pandas as pd | ||
from pyiem.database import get_sqlalchemy_conn | ||
from pyiem.plot import figure_axes | ||
from sqlalchemy import text | ||
|
||
XREF = { | ||
"nw": "IAC001", | ||
"nc": "IAC002", | ||
"ne": "IAC003", | ||
"wc": "IAC004", | ||
"c": "IAC005", | ||
"ec": "IAC006", | ||
"sw": "IAC007", | ||
"sc": "IAC008", | ||
"se": "IAC009", | ||
} | ||
|
||
|
||
@click.command() | ||
@click.option("--year", type=int, help="Year to plot") | ||
@click.option("--district", type=str, help="NASS District") | ||
def main(year, district): | ||
"""Go Main Go.""" | ||
charidx = year - 2007 + 1 | ||
with get_sqlalchemy_conn("coop") as conn: | ||
climdiv = gpd.read_postgis( | ||
text( | ||
""" | ||
select t.iemid, r.geom from climodat_regions r JOIN stations t | ||
on (r.iemid = t.iemid) where t.id = :sid | ||
""" | ||
), | ||
conn, | ||
params={"sid": XREF[district]}, | ||
geom_col="geom", | ||
index_col="iemid", | ||
) | ||
|
||
# Get the NASS data | ||
with get_sqlalchemy_conn("coop") as conn: | ||
nass = pd.read_sql( | ||
text( | ||
""" | ||
select * from nass_iowa where metric = 'corn planted' | ||
and extract(year from valid) = :year ORDER by valid ASc | ||
""" | ||
), | ||
conn, | ||
params={"year": year}, | ||
) | ||
|
||
# Figure out the planting dates | ||
with get_sqlalchemy_conn("idep") as conn: | ||
fields = gpd.read_postgis( | ||
text(""" | ||
select st_transform(geom, 4326) as geom, plant, huc12, fbndid, | ||
acres from fields f LEFT JOIN field_operations o | ||
on (f.field_id = o.field_id and o.year = :year) | ||
where scenario = 0 and | ||
substr(landuse, :charidx, 1) = 'C' | ||
"""), | ||
conn, | ||
params={"year": year, "charidx": charidx}, | ||
geom_col="geom", | ||
parse_dates="plant", | ||
) | ||
|
||
# Spatially filter fields that are inside the climdiv region | ||
fields = gpd.sjoin(fields, climdiv, predicate="intersects") | ||
|
||
# accumulate acres planted by date | ||
accum = fields[["plant", "acres"]].groupby("plant").sum().cumsum() | ||
accum["percent"] = accum["acres"] / fields["acres"].sum() * 100.0 | ||
accum = accum.reindex( | ||
pd.date_range(f"{year}-04-15", f"{year}-06-23") | ||
).ffill() | ||
|
||
fig, ax = figure_axes( | ||
logo="dep", | ||
title=( | ||
f"{year} Corn Planting Progress vs " | ||
f"NASS Iowa Crop District {district.upper()}" | ||
), | ||
figsize=(10.24, 7.68), | ||
) | ||
ax.set_ylim(0, 100) | ||
ax.set_ylabel("Percent of Acres Planted") | ||
ax.set_xlabel("Date") | ||
ax.grid(True) | ||
ax.plot( | ||
accum.index, | ||
accum["acres"] / fields["acres"].sum() * 100.0, | ||
label="DEP", | ||
) | ||
|
||
ax.scatter( | ||
nass["valid"], | ||
nass[district], | ||
marker="*", | ||
s=30, | ||
color="r", | ||
label="NASS", | ||
) | ||
ax.legend(loc=2) | ||
|
||
# create a table in the lower right corner with the values from | ||
txt = ["Date | NASS | DEP"] | ||
for _, row in nass.iterrows(): | ||
dep = accum.at[pd.Timestamp(row["valid"]), "percent"] | ||
txt.append(f"{row['valid']} | {row['se']:.0f} | {dep:.1f}") | ||
print("\n".join(txt)) | ||
ax.text( | ||
0.99, | ||
0.01, | ||
"\n".join(txt), | ||
transform=ax.transAxes, | ||
fontsize=10, | ||
va="bottom", | ||
ha="right", | ||
bbox=dict(facecolor="white", edgecolor="black", pad=5), | ||
) | ||
|
||
fig.savefig(f"corn_progress_{year}_{district}.png") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters