Skip to content

Read and write files ​

WannierIO follows a consistent IO pattern across Wannier90 formats:

  1. read with a read_* function,

  2. if needed, modify the returned Julia data,

  3. write back with the matching write_* function.

IO conventions ​

  • Function pairs are usually named read_xxx and write_xxx.

  • Most readers/writers accept a filename or an IO stream.

  • For several Wannier90 files, writing supports binary=true to emit Fortran unformatted files.

  • Tight-binding operators additionally support backend formats (HDF5/JLD2/Zarr) via read_operator and write_operator.

Common workflows ​

1. Round-trip a plain-text Wannier90 file ​

julia
using WannierIO

amn = read_amn("silicon.amn")
write_amn("silicon_copy.amn", amn.A; header=amn.header)

The same pattern applies to other files such as read_mmn/write_mmn, read_eig/write_eig, and read_nnkp/write_nnkp.

2. Write Fortran binary files ​

julia
using WannierIO

chk = read_chk("silicon.chk")
write_chk("silicon_binary.chk", chk; binary=true)

spn = read_spn("silicon.spn")
write_spn("silicon_binary.spn", spn; binary=true)

This is useful when interoperating with workflows that expect native Fortran unformatted output.

3. Work with win input files ​

julia
using WannierIO

win = read_win("silicon.win")
win["num_iter"] = 200
write_win("silicon_new.win", win)

read_win can parse both text and TOML-style inputs via format detection.

The projections block is kept as its raw lines, so that write_win reproduces it. parse_projections expands it into the orbitals that wannier90.x -pp writes to the nnkp file, and format_projections writes orbitals back as a projection block:

julia
projections = parse_projections(win)  # Vector{HydrogenOrbital}
print(format_projections(projections))

Lines of a block can also be parsed against a Crystal without a win, e.g. to try other projections on the same structure:

julia
using CrystalBase

crystal = Crystal(win)
parse_projections(["Si: sp3"], crystal)

4. Read and write tight-binding files ​

julia
using WannierIO

tbdat = read_w90_tb_dat("silicon_tb.dat")
wsvec = read_w90_wsvec_dat("silicon_wsvec.dat")
write_w90_tb_dat("silicon_out_tb.dat", tbdat)

For the operator container and sparse backends, see Tight-binding operators and Sparse storage.

5. Volumetric data formats ​

julia
using WannierIO

xsf = read_xsf("charge.xsf")
write_xsf("charge_copy.xsf", xsf)

cube = read_cube("density.cube")
write_cube("density_copy.cube", cube)

Supported volumetric formats include XSF, CUBE, and BXSF.

Tips ​

  • Preserve metadata fields (for example headers/lattice metadata) when writing modified data.

  • If you rely on binary files, test round-trip compatibility on your target compiler/platform because unformatted Fortran records are not universally portable.

  • For Wannier interpolation from tb.dat/wsvec.dat files, use Wannier.jl, which applies the R degeneracies and MDRS replicas that WannierIO keeps as stored.

For a deeper API-level reference, see Wannier90 and Tight binding.