from pathlib import Path
import xarray as xr
import numpy as np
domain_file = Path("/Users/mhoffman/Documents/PROJECTS/NGEE-Arctic/CC3/kuparuk_cc31/dapper_inputs/C0/kuparuk_C0/domain.nc")
out_file = Path("/Users/mhoffman/Documents/PROJECTS/NGEE-Arctic/repos/field-to-model-inputdata/E3SM/lnd/clm2/PTCLM/kuparuk_C0_sitedata.txt")
site_code = "kuparuk_C0"
name = "Kuparuk watershed"
state = "AK"
# OLMT metadata fields.
# These do not change the DaPPER surface/domain/MET files.
elev = 0
startyear = 1980
endyear = 2024
alignyear = 1980
timezone = -9
ds = xr.open_dataset(domain_file, decode_cf=False)
def get_scalar(names):
for name in names:
if name in ds:
value = np.asarray(ds[name].values).squeeze()
return float(value)
raise KeyError(f"Could not find any of these coordinate variables: {names}")
lon = get_scalar(["xc", "lon", "LONGXY"])
lat = get_scalar(["yc", "lat", "LATIXY"])
out_file.parent.mkdir(parents=True, exist_ok=True)
header = "site_code,name,state,lon,lat,elev,startyear,endyear,alignyear,timezone\n"
row = (
f"{site_code},{name},{state},"
f"{lon:.8f},{lat:.8f},"
f"{elev:.2f},{startyear},{endyear},{alignyear},{timezone}\n"
)
out_file.write_text(header + row)
print(f"Wrote {out_file}")
print(out_file.read_text())
OLMT looks for a site file at
inputdata/lnd/clm2/PTCLM/. It would be convenient if dapper created a file that can be placed there. Below is a script used to do that from the dapper-produced domain file.