#!/usr/bin/env python3 # # meteo_enter.py --- run User Entered Meteorological Data # See HYSPLIT Tutorial Sec 3.5 # # Change history: # 10 May 2025 (BB) - initial, cross-platform version import os import sys import platform from pathlib import Path import subprocess import webbrowser 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 "parm_case.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_TUTORIAL_DIRECTORY' For Linux users (csh or tcsh shell): setenv TTR 'YOUR_HYSPLIT_TUTORIAL_DIRECTORY' """) sys.exit(1) # Work from OUT os.chdir(OUT) 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) # Set parameters syr, smo, smn = "83", "09", "00" # start year, month, minute days = ["25", "26"] hours = ["00", "06", "12", "18"] wdir, spd, mix, stb = "225", "6.0", "1500.0", "3" # wind dir, speed, mixed depth, stability lat, lon = 40.7, -83.5 stn_txt = OUT / "stndata.txt" stn_bin = "stndata.bin" profile_txt = OUT / "profile.txt" stn2arl_exe = PGM / "exec" / f"stn2arl{exe_suffix}" profile_exe = PGM / "exec" / f"profile{exe_suffix}" for exe in (stn2arl_exe, profile_exe): if not exe.exists(): print(f"ERROR: executable not found at {exe}") sys.exit(1) # Build station data file if stn_txt.exists(): stn_txt.unlink() with open(stn_txt, "w", encoding="ascii") as f: for day in days: for hr in hours: f.write(f"{syr} {smo} {day} {hr} {smn} {wdir} {spd} {mix} {stb}\n") print(stn_txt.read_text(encoding="ascii")) # Run stn2arl cmd = [ str(stn2arl_exe), stn_txt.name, stn_bin, str(lat), str(lon), ] run_cmd(cmd, "Converting station data to ARL format") input("Press Enter to continue . . .") # Run profile cmd = [ str(profile_exe), f"-d.{os.sep}", f"-f{stn_bin}", ] run_cmd(cmd, "Running HYSPLIT profile analysis") # Output results if profile_txt.exists(): print("\nProfile Output:\n" + "=" * 50) print(profile_txt.read_text(encoding="ascii")) else: print("Warning: profile.txt not found. Check HYSPLIT execution and inputs.")