"""Make obs_data figure directories for the MDTF Wheeler_Kiladis and MJO_suite PODs from the new-obs runs. For each period it writes // containing Wheeler_Kiladis/, MJO_suite/ drop-in replacements for inputdata/obs_data// (PNGs named by source) html/.html the POD html template with its obs/ links pointing at those names The html is made from the committed template (git HEAD), so rerunning gives the same result. Sources in the names: CDR = NOAA daily OLR CDR v02r00 (rlut); ERA5 = ERA5/ERA5.1 (u200, u850, v200, v850, and pr in the old-period set); IMERG = GPM IMERG Final V07B (pr); TRMM = TRMM 3B42 V7 (pr). Figures that combine fields name both, e.g. IMERG_ERA5.MJO.lag.lat.lon.winter.png (pr shading, u850 lines), CDR_ERA5.MJO.life.cycle.* (OLR shading, u850 vectors). Usage: python build_obs_figures.py OUT_DIR [period ...] (periods: old trmm imerg; default all) """ import os import re import shutil import subprocess import sys OBSFIG = "/glade/work/bundy/mdtf/obs_wk_mjo/obsfig/runs" MJOIDX = "/glade/work/bundy/mdtf/obs_wk_mjo/obsfig/mjo_index" # /ERA5.MJO.png, made by run_mjo_index.pbs there MDTF = "/glade/u/home/bundy/mdtf/MDTF_current/MDTF-diagnostics.v4.3" PERIODS = { # key: (dir name, pr source, case, WK years, MJO years); runs in OBSFIG/ (WK) and OBSFIG/_mjo "old": ("ERA5pr_1979-1999", "ERA5", "CDR_ERA5", "1979-1999", "1980-1999"), "trmm": ("TRMM_1998-2019", "TRMM", "CDR_TRMM_ERA5", "1998-2019", "1998-2019"), "imerg": ("IMERG_2001-2024", "IMERG", "CDR_IMERG_ERA5", "2001-2024", "2001-2024"), } PR_LONG = {"ERA5": "ERA5 (ERA5.1 for 2000-2006)", "TRMM": "TRMM 3B42 V7", "IMERG": "GPM IMERG Final V07B"} WK_SRC = {"rlut": "CDR", "u200": "ERA5", "u850": "ERA5"} WK_FIGS = ["Fig1.Sym", "Fig1.Asym", "Fig2", "Fig3.Sym", "Fig3.Asym"] def copy(src, dst, done): if not os.path.exists(src): raise FileNotFoundError(src) shutil.copy2(src, dst) done.append(os.path.basename(dst)) def wk_figures(per, out): _, pr, case, *_ = PERIODS[per] model = f"{OBSFIG}/{case}/wkdir/MDTF_output/Wheeler_Kiladis/model" done = [] for fig in WK_FIGS: for var, src in dict(WK_SRC, pr=pr).items(): copy(f"{model}/{fig}.{case}_{var}.png", f"{out}/{fig}.{var}.{src}.png", done) return done def mjo_figures(per, out): _, pr, case, *_ = PERIODS[per] model = f"{OBSFIG}/{case}_mjo/wkdir/MDTF_output/MJO_suite/model" src = {"pr": pr, "rlut": "CDR", "u200": "ERA5", "u850": "ERA5", "v200": "ERA5", "v850": "ERA5"} done = [] for var, s in src.items(): for season in ("summer", "winter"): for n in (0, 1): copy(f"{model}/{case}.MJO.EOF.{var}.{season}-{n}.png", f"{out}/{s}.{var}.EOF.{season}-{n}.png", done) copy(f"{model}/{case}.MJO.wave.freq.{var}.{season}.png", f"{out}/{s}.{var}.MJO.wave.freq.{season}.png", done) for season in ("summer", "winter", "annual"): copy(f"{model}/{case}.MJO.lag.lat.lon.{season}.png", f"{out}/{pr}_ERA5.MJO.lag.lat.lon.{season}.png", done) for season in ("summer", "winter"): for v in ("", ".v2"): copy(f"{model}/{case}.MJO.life.cycle.{season}{v}.png", f"{out}/CDR_ERA5.MJO.life.cycle.{season}{v}.png", done) copy(f"{MJOIDX}/{per}/ERA5.MJO.png", f"{out}/ERA5.MJO.png", done) return done def template(pod): return subprocess.run(["git", "-C", MDTF, "show", f"HEAD:diagnostics/{pod}/{pod}.html"], check=True, capture_output=True, text=True).stdout def wk_html(per): _, pr, _, wky, _ = PERIODS[per] s = template("Wheeler_Kiladis") for var, src in WK_SRC.items(): s = re.sub(rf'obs/(Fig[0-9.A-Za-z]*?)\.{var}\.NCEP\.gif', rf'obs/\1.{var}.{src}.png', s) s = re.sub(r'obs/(Fig[0-9.A-Za-z]*?)\.TRMM_pr\.png', rf'obs/\1.pr.{pr}.png', s) # column headers: the obs column of each table blocks = s.split("") for i, b in enumerate(blocks): for var, src in list(WK_SRC.items()) + [("pr", pr)]: if f".{var}.{src}.png" in b and "OMEGA500 (500" not in b: # (not the "omega500 removed" comment) b = re.sub(r'(
)(NCEP|TRMM)\n', rf'\g<1>{src}\n', b, count=1) blocks[i] = b s = "".join(blocks) note = (f"

\nObservations (obs column), {wky}: OLR from the NOAA daily OLR CDR v02r00 (CDR), " f"precipitation from {PR_LONG[pr]} ({pr}), winds from ERA5 (ERA5.1 for 2000-2006), all daily means " "on a common 1 deg grid. See the POD documentation for details.\n") return s.replace("

\n

", note + "

\n

", 1) def mjo_html(per): _, pr, _, _, mjy = PERIODS[per] s = template("MJO_suite") # the WGNE MJO Task Force page is gone; cite the CLIVAR MJO WG diagnostics paper instead s = s.replace('WGNE\n MJO Task Force', 'CLIVAR MJO Working Group\n (Waliser et al. 2009)') s = s.replace('obs/NCEP.MJO.gif">NCEP Reanalysis', 'obs/ERA5.MJO.png">ERA5') s = re.sub(r'obs/TRMM\.pr\.', f'obs/{pr}.pr.', s).replace(" TRMM", f" {pr}") s = re.sub(r'obs/ERA\.rlut\.([^"]*)">(rlut [a-z]+) ERA<', r'obs/CDR.rlut.\1">\2 CDR<', s) s = re.sub(r'obs/ERA\.MJO\.lag\.lat\.lon\.([a-z]+)\.png">(.*?) ERA<', rf'obs/{pr}_ERA5.MJO.lag.lat.lon.\1.png">\2 {pr}/ERA5<', s) for season, n in (("summer", 0), ("winter", 1)): s = s.replace(f'
{season} ERA', f'{season} CDR/ERA5\n' f'{season} CDR/ERA5') s = re.sub(r'obs/ERA\.(u200|u850|v200|v850)\.([^"]*)">(.*?) ERA<', r'obs/ERA5.\1.\2">\3 ERA5<', s) note = (f"

\nObservations (obs rows and columns), {mjy}: OLR (rlut) from the NOAA daily OLR CDR v02r00 (CDR), " f"precipitation from {PR_LONG[pr]} ({pr}), winds from ERA5 (ERA5.1 for 2000-2006), all daily means on " "a common 1 deg grid. See the POD documentation for details.\n") return s.replace("

\n", note + "

\n

", 1) def check_links(html, figdir): links = set(re.findall(r'HREF="obs/([^"]+)"', re.sub(r"", "", html, flags=re.S))) missing = sorted(l for l in links if not os.path.exists(os.path.join(figdir, l))) return links, missing def main(): out = os.path.abspath(sys.argv[1]) for per in sys.argv[2:] or list(PERIODS): d = os.path.join(out, PERIODS[per][0]) for pod, figs, html in (("Wheeler_Kiladis", wk_figures, wk_html), ("MJO_suite", mjo_figures, mjo_html)): fd = os.path.join(d, pod) if os.path.isdir(fd): shutil.rmtree(fd) os.makedirs(fd) done = figs(per, fd) os.makedirs(os.path.join(d, "html"), exist_ok=True) h = html(per) with open(os.path.join(d, "html", pod + ".html"), "w") as fh: fh.write(h) links, missing = check_links(h, fd) unused = sorted(set(done) - links) print(f"{per} {pod}: {len(done)} figures, {len(links)} obs links, missing {missing}, not linked {unused}") if __name__ == "__main__": main()