#!/usr/bin/env python3 # # conc_multi.py --- HYSPLIT Simultaneous Multiple Grids # See HYSPLIT Tutorial Sec. 8.6 # # Change history: # 05 Oct 2025 (BB) - Certified cross-platform Python version # 19 Nov 2025 (SZ) - create image files when DSP=NO # 20 Nov 2025 (SZ) - rename output html files # 05 Jan 2026 (BB) - fix variable casing and runtime consistency # 18 Jun 2026 (SZ) - standardize code for run_cmd(), convert_html_to_img(), # etc. across Python scripts # - ensure encoding is specified when opening files import os import sys import platform import subprocess import webbrowser from pathlib import Path # Version check if sys.version_info < (3, 8): print("Please use Python 3.8 or higher.") sys.exit(1) system = platform.system().lower() if system == "windows": PGM = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("C:\\hysplit") OUT = Path(sys.argv[2]) if len(sys.argv) > 2 else PGM / "working" DSP = sys.argv[3] if len(sys.argv) > 3 else "YES" TTR = Path(os.environ.get("TTR", "C:\\Tutorial")) exe_suffix = ".exe" else: PGM = Path(sys.argv[1]) if len(sys.argv) > 1 else Path.home() / "hysplit" OUT = Path(sys.argv[2]) if len(sys.argv) > 2 else PGM / "working" DSP = sys.argv[3] if len(sys.argv) > 3 else "YES" TTR = Path(os.environ.get("TTR", Path.home() / "tutorial")) exe_suffix = "" if not PGM.exists(): script = Path(__file__).name if "__file__" in globals() else "conc_multi.py" print(f"ERROR: HYSPLIT not found at {PGM}") print(f""" Please specify the path to your HYSPLIT installation using a command-line argument: python {script} YOUR_HYSPLIT_DIRECTORY [OUT] [DSP (YES|NO)] Example: python {script} C:\\hysplit C:\\hysplit\\working YES """) sys.exit(1) if not TTR.exists(): print(f"ERROR: HYSPLIT tutorial not found at {TTR}") print(""" Please set the TTR environment variable to point to your HYSPLIT tutorial directory. For Windows users (Command Prompt): SET TTR=YOUR_HYSPLIT_TUTORIAL_DIRECTORY For Linux users (bash shell): export TTR='YOUR_HYSPLIT_TOTURIAL_DIRECTORY' For Linux users (csh or tcsh shell): setenv TTR 'YOUR_HYSPLIT_TOTURIAL_DIRECTORY'""") sys.exit(1) os.chdir(OUT) # ------------------------------------------------------------------ # Helper functions # ------------------------------------------------------------------ def run_cmd(cmd, desc="", allow_fail=False, stdout_file=None, append=False): if desc: print(f"{desc}...") exitcode = 0 with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) as p: if stdout_file: mode="a" if append else "w" with open(stdout_file, mode, encoding="utf-8") as f_out: for ln in p.stdout: print(ln, end="") f_out.write(ln) else: with p.stdout as f: for ln in iter(f.readline, b'\n'): if len(ln) == 0: break print(ln.rstrip()) done = False while not done: try: exitcode = p.wait(timeout=1) done = True except subprocess.TimeoutExpired: print("WARN: subprocess timed out.") if exitcode != 0: if allow_fail: print(f"WARN: command failed with exit code {exitcode} -> {' '.join(cmd)}") else: print(f"ERROR: command failed with exit code {exitcode} -> {' '.join(cmd)}") sys.exit(1) def convert_html_to_img(html_file, img_name_frame_nums): # Split SVG into Linux-consistent naming: F00-sec71_001.svg, etc. temp_img_name = "temp" run_cmd([str(PGM / "exec" / f"splitsvg{exe_suffix}"), f"-i{html_file}", f"-o{temp_img_name}.svg"]) # Locate ImageMagick for cross-platform conversion if system == "windows": convert_exe = None imgk_dir = next( (k for k in os.environ.get('PATH', '').split(os.pathsep) if "ImageMagick" in k), None ) if imgk_dir: # Use magick.exe for ImageMagick 7.x and later. Use convert.exe for earlier versions. for cmd in ('magick', 'convert'): candidate = os.path.join(imgk_dir, f'{cmd}{exe_suffix}') if os.path.exists(candidate): convert_exe = candidate break else: convert_exe = "convert" if convert_exe: # Match Linux naming for DSP=NO mode exactly for img_name, frame_num in img_name_frame_nums: svg = Path(f"F{frame_num:02d}-{temp_img_name}.svg") run_cmd([convert_exe, str(svg), img_name]) print(f"Converted {svg} -> {img_name}") svg.unlink() else: print("ImageMagick not found — skipping .svg conversion") # Build ASCDATA.CFG with open("ASCDATA.CFG", "w", encoding="ascii") as f: f.write("-90.0 -180.0 lat/lon of lower left corner\n") f.write("1.0 1.0 lat/lon spacing in degrees\n") f.write("180 360 lat/lon number of data points\n") f.write("2 default land use category\n") f.write("0.2 default roughness length (m)\n") f.write(f"'{PGM / 'bdyfiles'}{os.sep}' directory of files\n") # CONTROL file syr, smo, sda, shr = 83, 9, 25, 17 lat, lon, lvl = 39.90, -84.22, 10.0 run, top = 25, 10000.0 METDIR = TTR / "captex" METFILE = "captex2_wrf27uw.bin" control_lines = [ f"{syr} {smo} {sda} {shr}", "1", f"{lat} {lon} {lvl}", f"{run}", "0", f"{top}", "1", f"{METDIR}{os.sep}", METFILE, "1", "PMCH", "67000.0", "3.0", "00 00 00 00 00", "2", "42.0 -78.0", "0.25 0.25", "15.0 25.0", "./", "hysplit2.bin", "1", "100", "83 09 25 18 00", "83 09 28 15 00", "00 03 00", "42.0 -82.5", "0.05 0.05", "5.0 5.0", "./", "hysplit2FG.bin", "1", "100", "83 09 25 18 00", "83 09 26 06 00", "00 03 00", "1", "0.0 0.0 0.0", "0.0 0.0 0.0 0.0 0.0", "0.0 0.0 0.0", "0.0", "0.0", ] with open("CONTROL", "w", encoding="ascii") as f: for line in control_lines: f.write(line + "\n") # SETUP.CFG with open("SETUP.CFG", "w", encoding="ascii") as f: f.write("&SETUP\n") f.write(" numpar = 10000,\n") f.write(" maxpar = 100000,\n") f.write(" ndump = 1,\n") f.write(" ncycl = 3,\n") f.write("/\n") # Run model for fn in ("hysplit2.bin", "hysplit2FG.bin"): if Path(fn).exists(): Path(fn).unlink() run_cmd( [str(PGM / "exec" / f"hycs_std{exe_suffix}")], "Running hycs_std with multiple grids" ) # Fine grid plot run_cmd([ str(PGM / "exec" / f"concplot{exe_suffix}"), "+g1", "-ihysplit2FG.bin", "-ofinegrid", "-x1.0E+12", "-z100", "-upg", f"-j{PGM / 'graphics' / 'arlmap'}", "-g4:50", "-h39.90:-84.22" ], "Generating fine grid concplot") FG_HTML = OUT / "finegrid.html" if FG_HTML.exists(): if DSP.upper() == "YES": webbrowser.open(FG_HTML.as_uri()) else: convert_html_to_img(str(FG_HTML), [("sgrid004.png", 2)]) if DSP.upper() == "YES": input("Press Enter to continue with coarse grid output...") # Coarse grid plot run_cmd([ str(PGM / "exec" / f"concplot{exe_suffix}"), "+g1", "-ihysplit2.bin", "-ocoarsegrid", "-x1.0E+12", "-z100", "-upg", f"-j{PGM / 'graphics' / 'arlmap'}", "-g4:50", "-h39.90:-84.22" ], "Generating coarse grid concplot") CG_HTML = OUT / "coarsegrid.html" if CG_HTML.exists(): if DSP.upper() == "YES": webbrowser.open(CG_HTML.as_uri()) else: convert_html_to_img(str(CG_HTML), [("sgrid005.png", 2)])