@@ -830,7 +830,31 @@ def _to_cpp_wout(self) -> _vmecpp.WOutFileContents:
830830
831831 return cpp_wout
832832
833- # TODO(eguiraud): implement from_wout_file
833+ @staticmethod
834+ def from_wout_file (wout_filename : str | Path ) -> VmecWOut :
835+ """Load wout contents in NetCDF format.
836+
837+ This is the format used by Fortran VMEC implementations and the one expected by
838+ SIMSOPT.
839+ """
840+ with netCDF4 .Dataset (wout_filename , "r" ) as fnc :
841+ fnc .set_auto_mask (False )
842+ attrs = {}
843+ for key in fnc .variables :
844+ if key .endswith ("__logical__" ):
845+ attrs [key [:- 11 ]] = fnc [key ][()] != 0
846+ elif key == "volume_p" :
847+ attrs ["volume" ] = fnc [key ][()]
848+ elif key in ["xm" , "xn" , "xm_nyq" , "xn_nyq" ]:
849+ attrs [key ] = np .array (fnc [key ][()], dtype = int )
850+ elif key in ["pmass_type" , "piota_type" , "pcurr_type" , "mgrid_file" ]:
851+ attrs [key ] = fnc [key ][()].tobytes ().decode ("ascii" )
852+ else :
853+ attrs [key ] = fnc [key ][()]
854+ if "lmns_full" not in fnc .variables :
855+ attrs ["lmns_full" ] = None
856+ return VmecWOut (** attrs )
857+ raise RuntimeError ("Failed to load NetCDF wout file " + str (wout_filename ))
834858
835859
836860class Threed1Volumetrics (pydantic .BaseModel ):
0 commit comments