code: def _write_dipole_text(fname, dip):
      fmt = "  %7.1f %7.1f %8.2f %8.2f %8.2f %8.3f %8.3f %8.3f %8.3f %6.2f"
      header = (
          "#   begin     end   X (mm)   Y (mm)   Z (mm)"
          "   Q(nAm)  Qx(nAm)  Qy(nAm)  Qz(nAm)    g/%"
      )
      t = dip.times[:, np.newaxis] * 1000.0
      gof = dip.gof[:, np.newaxis]
      amp = 1e9 * dip.amplitude[:, np.newaxis]
      out = (t, t, dip.pos / 1e-3, amp, dip.ori * amp, gof)
  
      # optional fields
      fmts = dict(
          khi2=("    khi^2", " %8.1f", 1.0),
          nfree=("  free", " %5d", 1),
          vol=("  vol/mm^3", " %9.3f", 1e9),
          depth=("  depth/mm", " %9.3f", 1e3),
          long=("  long/mm", " %8.3f", 1e3),
          trans=("  trans/mm", " %9.3f", 1e3),
          qlong=("  Qlong/nAm", " %10.3f", 1e9),
          qtrans=("  Qtrans/nAm", " %11.3f", 1e9),
      )
      for key in ("khi2", "nfree"):
          data = getattr(dip, key)
          if data is not None:
              header += fmts[key][0]
              fmt += fmts[key][1]
              out += (data[:, np.newaxis] * fmts[key][2],)
      for key in ("vol", "depth", "long", "trans", "qlong", "qtrans"):
          data = dip.conf.get(key)
          if data is not None:
              header += fmts[key][0]
              fmt += fmts[key][1]
              out += (data[:, np.newaxis] * fmts[key][2],)
      out = np.concatenate(out, axis=-1)
  
      # NB CoordinateSystem is hard-coded as Head here
      with open(fname, "wb") as fid:
          fid.write(b'# CoordinateSystem "Head"\n')
          fid.write((header + "\n").encode("utf-8"))
          np.savetxt(fid, out, fmt=fmt)
          if dip.name is not None:
              fid.write(
                  (
                      '## Name "%s dipoles" Style "Dipoles"' % dip.name
                  ).encode("utf-8")
              )
  
imports: import functools
  import re
  from copy import deepcopy
  from functools import partial
  import numpy
  from scipy.linalg import eigh
  from scipy.optimize import fmin_cobyla
  from ._fiff.constants import FIFF
  from ._fiff.pick import pick_types
  from ._fiff.proj import _needs_eeg_average_ref_proj, make_projector
  from ._freesurfer import _get_aseg, head_to_mni, head_to_mri, read_freesurfer_lut
  from .bem import _bem_find_surface, _bem_surf_name, _fit_sphere
  from .cov import _ensure_cov, compute_whitener
  from .evoked import _aspect_rev, _read_evoked, _write_evokeds
  from .fixes import _safe_svd, pinvh
  from .forward._compute_forward import _compute_forwards_meeg, _prep_field_computation
  from .forward._make_forward import _get_trans, _prep_eeg_channels, _prep_meg_channels, _setup_bem
  from .parallel import parallel_func
  from .source_space._source_space import SourceSpaces, _make_volume_source_space
  from .surface import _compute_nearest, _points_outside_surface, transform_surface_to
  from .transforms import _coord_frame_name, _print_coord_trans, apply_trans
  from .utils import ExtendedTimeMixin, TimeMixin, _check_fname, _check_option, _get_blas_funcs, _pl, _repeated_svd, _svd_lwork, _time_mask, _validate_type, _verbose_safe_false, check_fname, copy_function_doc_to_method_doc, fill_doc, logger, verbose, warn
  from .viz import plot_dipole_amplitudes, plot_dipole_locations
  from .viz.evoked import _plot_evoked
label_desc: Use image node_id 4 for calling a global function with example usage: _write_dipole_text(fname, dip) without return types