Skip to content

Commit

Permalink
Add a method to load a VmecWout object from a NetCDF file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jons-pf committed Feb 6, 2025
1 parent 514e2da commit 78965cd
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/vmecpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,31 @@ def _to_cpp_wout(self) -> _vmecpp.WOutFileContents:

return cpp_wout

# TODO(eguiraud): implement from_wout_file
@staticmethod
def from_wout_file(wout_filename: str | Path) -> VmecWOut:
"""Load wout contents in NetCDF format.
This is the format used by Fortran VMEC implementations and the one expected by
SIMSOPT.
"""
with netCDF4.Dataset(wout_filename, "r") as fnc:
fnc.set_auto_mask(False)
attrs = {}
for key in fnc.variables:
if key.endswith("__logical__"):
attrs[key[:-11]] = fnc[key][()] != 0
elif key == "volume_p":
attrs["volume"] = fnc[key][()]
elif key in ["xm", "xn", "xm_nyq", "xn_nyq"]:
attrs[key] = np.array(fnc[key][()], dtype=int)
elif key in ["pmass_type", "piota_type", "pcurr_type", "mgrid_file"]:
attrs[key] = fnc[key][()].tobytes().decode("ascii")
else:
attrs[key] = fnc[key][()]
if "lmns_full" not in fnc.variables:
attrs["lmns_full"] = None
return VmecWOut(**attrs)
raise RuntimeError("Failed to load NetCDF wout file " + str(wout_filename))


class Threed1Volumetrics(pydantic.BaseModel):
Expand Down

0 comments on commit 78965cd

Please sign in to comment.