CLM5-Emulator / scripts /inference.py
zhangrenchao's picture
Publish CLM5-Emulator reproduction
835fa51 verified
Raw
History Blame Contribute Delete
921 Bytes
from pathlib import Path
import sys,numpy as np,torch
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT))
from model.clm5_emulator import *
c=load_config(ROOT);d=np.load(ROOT/c["data"]["path"]);ck=torch.load(ROOT/c["paths"]["checkpoint"],map_location="cpu",weights_only=True);m=CLM5Emulator(**ck["model_config"]);m.load_state_dict(ck["model"]);mask=d["split"]==2
with torch.no_grad():pc=m(torch.tensor(d["parameters"][mask])).numpy()
fields=reconstruct(pc,d["eof"],d["mean"]).reshape(-1,2,46,72);obs=d["pc"].mean(0);best,cost=bounded_search(m,obs,np.maximum(d["pc"].std(0),.1),np.ones((2,3)),seed=c["seed"]);path=ROOT/c["paths"]["predictions"];path.parent.mkdir(parents=True,exist_ok=True);np.savez_compressed(path,predicted_pc=pc,target_pc=d["pc"][mask],predicted_fields=fields,target_fields=d["fields"][mask],best_parameters=best,cost=cost,latitude=d["latitude"],longitude=d["longitude"]);print(path)