Skip to content

Commit

Permalink
pass crop code to sweep magic.R
Browse files Browse the repository at this point in the history
  • Loading branch information
akrherz committed Jun 13, 2022
1 parent d8a0267 commit 2b4671b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions scripts/RT/magic.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ if (length(args) == 0) {
weppsoilfile <- 'p0.sol';
Year <- 2019;
DayofYear <- 365;
CropCode <- 'C';
} else {
# get arguments from the command line
generic_template_in=args[1];
weppgraphfile=args[2];
weppsoilfile=args[3];
Year=as.numeric(args[4]);
DayofYear=as.numeric(args[5]);
CropCode=args[6];
}
generic_template_in.in<-paste(generic_template_in, ".in", sep="")
print(paste('Template File In: ', generic_template_in.in, sep=""))
Expand Down
28 changes: 27 additions & 1 deletion scripts/RT/proctor_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import subprocess
from multiprocessing import Pool

from pyiem.dep import man2df, read_man
from pyiem.util import get_sqlalchemy_conn, logger
import pandas as pd
import requests
Expand Down Expand Up @@ -73,6 +74,26 @@ def get_wind_obs(date, lon, lat):
return hourly


def compute_crop(manfn: str, year: int) -> str:
"""Compute the crop code used for magic.R in sweep workflow."""
try:
mandf = man2df(read_man(manfn), 2007)
except Exception as exp:
LOG.info("%s generated %s", manfn, exp)
return "0"
cropname = (
mandf.query(f"year == {year} and ofe == 1")
.iloc[0]["crop_name"]
.lower()
)
cropcode = "0"
if cropname.startswith("Cor"):
cropcode = "C"
elif cropname.startswith("Soy"):
cropcode = "B"
return cropcode


def workflow(arg):
"""Do what we need to do."""
(idx, row) = arg
Expand All @@ -81,6 +102,11 @@ def workflow(arg):
f"/i/{row['scenario']}/sweepin/{row['huc_12'][:8]}/"
f"{row['huc_12'][8:12]}/{row['huc_12']}_{row['fpath']}.sweepin"
)
cropcode = compute_crop(
sweepinfn.replace("sweepin", "man"),
row["date"].year,
)
# Compute the management df to get the crop code needed for downstream
if not os.path.isfile(sweepinfn):
LOG.warning("%s does not exist, copying template", sweepinfn)
shutil.copyfile("sweepin_template.txt", sweepinfn)
Expand Down Expand Up @@ -110,7 +136,7 @@ def workflow(arg):
f"Rscript --vanilla magic.R {sweepinfn} "
f"{sweepinfn.replace('sweepin', 'grph')} "
f"{sweepinfn.replace('sweepin', 'sol')} {row['date'].year} "
f"{row['date']:%j}"
f"{row['date']:%j} {cropcode}"
)
if not run_command(cmd):
return None, None
Expand Down

0 comments on commit 2b4671b

Please sign in to comment.