|
| 1 | +import sys |
| 2 | +import yaml |
| 3 | +from pathlib import Path |
| 4 | +import subprocess |
| 5 | +import os |
| 6 | + |
| 7 | +from access_mopper import ACCESS_ESM_CMORiser |
| 8 | +from access_mopper.tracking import TaskTracker |
| 9 | +from parsl import python_app, Config, HighThroughputExecutor |
| 10 | +from parsl.providers import PBSProProvider |
| 11 | +from parsl.addresses import address_by_hostname |
| 12 | +from importlib.resources import files |
| 13 | +import parsl |
| 14 | + |
| 15 | + |
| 16 | +def start_dashboard(dashboard_path: str, db_path: str): |
| 17 | + env = os.environ.copy() |
| 18 | + env["CMOR_TRACKER_DB"] = db_path |
| 19 | + subprocess.Popen( |
| 20 | + ["streamlit", "run", dashboard_path], |
| 21 | + env=env, |
| 22 | + stdout=subprocess.DEVNULL, |
| 23 | + stderr=subprocess.DEVNULL, |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +@python_app |
| 28 | +def run_cmor(variable, config, db_path): |
| 29 | + from access_mopper import ACCESS_ESM_CMORiser |
| 30 | + from access_mopper.tracking import TaskTracker |
| 31 | + from pathlib import Path |
| 32 | + |
| 33 | + exp = config["experiment_id"] |
| 34 | + tracker = TaskTracker(Path(db_path)) |
| 35 | + tracker.add_task(variable, exp) |
| 36 | + |
| 37 | + if tracker.is_done(variable, exp): |
| 38 | + return f"Skipped: {variable} (already done)" |
| 39 | + |
| 40 | + try: |
| 41 | + tracker.mark_running(variable, exp) |
| 42 | + cmoriser = ACCESS_ESM_CMORiser( |
| 43 | + input_paths=Path(config["input_folder"]), |
| 44 | + compound_name=variable, |
| 45 | + experiment_id=config["experiment_id"], |
| 46 | + source_id=config["source_id"], |
| 47 | + variant_label=config["variant_label"], |
| 48 | + grid_label=config["grid_label"], |
| 49 | + activity_id=config.get("activity_id"), |
| 50 | + output_path=config["output_folder"], |
| 51 | + drs_root=config.get("drs_root"), |
| 52 | + ) |
| 53 | + cmoriser.run() |
| 54 | + tracker.mark_done(variable, exp) |
| 55 | + return f"Completed: {variable}" |
| 56 | + except Exception as e: |
| 57 | + tracker.mark_failed(variable, exp, str(e)) |
| 58 | + raise |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + if len(sys.argv) != 2: |
| 63 | + print("Usage: mopper-cmorise path/to/batch_config.yml") |
| 64 | + sys.exit(1) |
| 65 | + |
| 66 | + config_path = Path(sys.argv[1]) |
| 67 | + if not config_path.exists(): |
| 68 | + print(f"Error: config file not found: {config_path}") |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + with config_path.open() as f: |
| 72 | + config_data = yaml.safe_load(f) |
| 73 | + |
| 74 | + tracker = TaskTracker() |
| 75 | + DB_PATH = tracker.db_path |
| 76 | + |
| 77 | + # Start Streamlit dashboard |
| 78 | + DASHBOARD_SCRIPT = files("access_mopper.dashboard").joinpath("cmor_dashboard.py") |
| 79 | + start_dashboard(str(DASHBOARD_SCRIPT), str(DB_PATH)) |
| 80 | + |
| 81 | + # Configure Parsl |
| 82 | + parsl_config = Config( |
| 83 | + executors=[ |
| 84 | + HighThroughputExecutor( |
| 85 | + label="htex_pbs", |
| 86 | + address=address_by_hostname(), |
| 87 | + max_workers=1, |
| 88 | + provider=PBSProProvider( |
| 89 | + queue="normal", |
| 90 | + launcher=None, |
| 91 | + walltime="01:00:00", |
| 92 | + select_options="1:ncpus=4:mem=16GB", |
| 93 | + scheduler_options="#PBS -P your_project", |
| 94 | + worker_init="module load netcdf-python", |
| 95 | + nodes_per_block=1, |
| 96 | + init_blocks=1, |
| 97 | + max_blocks=10, |
| 98 | + ), |
| 99 | + ) |
| 100 | + ], |
| 101 | + strategy="simple", |
| 102 | + ) |
| 103 | + |
| 104 | + parsl.load(parsl_config) |
| 105 | + |
| 106 | + futures = [run_cmor(var, config_data, str(DB_PATH)) for var in config_data["variables"]] |
| 107 | + results = [f.result() for f in futures] |
| 108 | + print("\n".join(results)) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + main() |
0 commit comments