code: def _write_forward_solution(fid, fwd):
      start_block(fid, FIFF.FIFFB_MNE)
  
      #
      # MNE env
      #
      start_block(fid, FIFF.FIFFB_MNE_ENV)
      write_id(fid, FIFF.FIFF_BLOCK_ID)
      data = fwd["info"].get("working_dir", None)
      if data is not None:
          write_string(fid, FIFF.FIFF_MNE_ENV_WORKING_DIR, data)
      data = fwd["info"].get("command_line", None)
      if data is not None:
          write_string(fid, FIFF.FIFF_MNE_ENV_COMMAND_LINE, data)
      end_block(fid, FIFF.FIFFB_MNE_ENV)
  
      #
      # Information from the MRI file
      #
      start_block(fid, FIFF.FIFFB_MNE_PARENT_MRI_FILE)
      write_string(
          fid, FIFF.FIFF_MNE_FILE_NAME, fwd["info"]["mri_file"]
      )
      if fwd["info"]["mri_id"] is not None:
          write_id(fid, FIFF.FIFF_PARENT_FILE_ID, fwd["info"]["mri_id"])
      # store the MRI to HEAD transform in MRI file
      write_coord_trans(fid, fwd["info"]["mri_head_t"])
      end_block(fid, FIFF.FIFFB_MNE_PARENT_MRI_FILE)
  
      # write measurement info
      write_forward_meas_info(fid, fwd["info"])
  
      # invert our original source space transform
      src = list()
      for s in fwd["src"]:
          s = deepcopy(s)
          try:
              # returns source space to original coordinate frame
              # usually MRI
              s = transform_surface_to(
                  s, fwd["mri_head_t"]["from"], fwd["mri_head_t"]
              )
          except Exception as inst:
              raise ValueError(
                  "Could not transform source space (%s)" % inst
              )
          src.append(s)
  
      #
      # Write the source spaces (again)
      #
      _write_source_spaces_to_fid(fid, src)
      n_vert = sum([ss["nuse"] for ss in src])
      if fwd["_orig_source_ori"] == FIFF.FIFFV_MNE_FIXED_ORI:
          n_col = n_vert
      else:
          n_col = 3 * n_vert
  
      # Undo transformations
      sol = fwd["_orig_sol"].copy()
      if fwd["sol_grad"] is not None:
          sol_grad = fwd["_orig_sol_grad"].copy()
      else:
          sol_grad = None
  
      if fwd["surf_ori"] is True:
          if fwd["_orig_source_ori"] == FIFF.FIFFV_MNE_FIXED_ORI:
              warn(
                  "The forward solution, which is stored on disk now, is based "
                  "on a forward solution with fixed orientation. Please note "
                  "that the transformation to surface-based, fixed orientation "
                  "cannot be reverted after loading the forward solution with "
                  "read_forward_solution.",
                  RuntimeWarning,
              )
          else:
              warn(
                  "This forward solution is based on a forward solution with "
                  "free orientation. The original forward solution is stored "
                  "on disk in X/Y/Z RAS coordinates. Any transformation "
                  "(surface orientation or fixed orientation) will be "
                  "reverted. To reapply any transformation to the forward "
                  "operator please apply convert_forward_solution after "
                  "reading the forward solution with read_forward_solution.",
                  RuntimeWarning,
              )
  
      #
      # MEG forward solution
      #
      picks_meg = pick_types(
          fwd["info"], meg=True, eeg=False, ref_meg=False, exclude=[]
      )
      picks_eeg = pick_types(
          fwd["info"], meg=False, eeg=True, ref_meg=False, exclude=[]
      )
      n_meg = len(picks_meg)
      n_eeg = len(picks_eeg)
      row_names_meg = [fwd["sol"]["row_names"][p] for p in picks_meg]
      row_names_eeg = [fwd["sol"]["row_names"][p] for p in picks_eeg]
  
      if n_meg > 0:
          meg_solution = dict(
              data=sol[picks_meg],
              nrow=n_meg,
              ncol=n_col,
              row_names=row_names_meg,
              col_names=[],
          )
          _transpose_named_matrix(meg_solution)
          start_block(fid, FIFF.FIFFB_MNE_FORWARD_SOLUTION)
          write_int(
              fid, FIFF.FIFF_MNE_INCLUDED_METHODS, FIFF.FIFFV_MNE_MEG
          )
          write_int(fid, FIFF.FIFF_MNE_COORD_FRAME, fwd["coord_frame"])
          write_int(
              fid,
              FIFF.FIFF_MNE_SOURCE_ORIENTATION,
              fwd["_orig_source_ori"],
          )
          write_int(fid, FIFF.FIFF_MNE_SOURCE_SPACE_NPOINTS, n_vert)
          write_int(fid, FIFF.FIFF_NCHAN, n_meg)
          write_named_matrix(
              fid, FIFF.FIFF_MNE_FORWARD_SOLUTION, meg_solution
          )
          if sol_grad is not None:
              meg_solution_grad = dict(
                  data=sol_grad[picks_meg],
                  nrow=n_meg,
                  ncol=n_col * 3,
                  row_names=row_names_meg,
                  col_names=[],
              )
              _transpose_named_matrix(meg_solution_grad)
              write_named_matrix(
                  fid,
                  FIFF.FIFF_MNE_FORWARD_SOLUTION_GRAD,
                  meg_solution_grad,
              )
          end_block(fid, FIFF.FIFFB_MNE_FORWARD_SOLUTION)
  
      #
      #  EEG forward solution
      #
      if n_eeg > 0:
          eeg_solution = dict(
              data=sol[picks_eeg],
              nrow=n_eeg,
              ncol=n_col,
              row_names=row_names_eeg,
              col_names=[],
          )
          _transpose_named_matrix(eeg_solution)
          start_block(fid, FIFF.FIFFB_MNE_FORWARD_SOLUTION)
          write_int(
              fid, FIFF.FIFF_MNE_INCLUDED_METHODS, FIFF.FIFFV_MNE_EEG
          )
          write_int(fid, FIFF.FIFF_MNE_COORD_FRAME, fwd["coord_frame"])
          write_int(
              fid,
              FIFF.FIFF_MNE_SOURCE_ORIENTATION,
              fwd["_orig_source_ori"],
          )
          write_int(fid, FIFF.FIFF_NCHAN, n_eeg)
          write_int(fid, FIFF.FIFF_MNE_SOURCE_SPACE_NPOINTS, n_vert)
          write_named_matrix(
              fid, FIFF.FIFF_MNE_FORWARD_SOLUTION, eeg_solution
          )
          if sol_grad is not None:
              eeg_solution_grad = dict(
                  data=sol_grad[picks_eeg],
                  nrow=n_eeg,
                  ncol=n_col * 3,
                  row_names=row_names_eeg,
                  col_names=[],
              )
              _transpose_named_matrix(eeg_solution_grad)
              write_named_matrix(
                  fid,
                  FIFF.FIFF_MNE_FORWARD_SOLUTION_GRAD,
                  eeg_solution_grad,
              )
          end_block(fid, FIFF.FIFFB_MNE_FORWARD_SOLUTION)
  
      end_block(fid, FIFF.FIFFB_MNE)
  
imports: import os
  import re
  import shutil
  import tempfile
  from copy import deepcopy
  from os import PathLike
  from os import path
  from pathlib import Path
  from time import time
  import numpy
  from scipy import sparse
  from .._fiff.constants import FIFF
  from .._fiff.matrix import _read_named_matrix, _transpose_named_matrix, write_named_matrix
  from .._fiff.meas_info import Info, _make_ch_names_mapping, _read_bad_channels, _read_extended_ch_info, _write_bad_channels, _write_ch_infos, write_info
  from .._fiff.open import fiff_open
  from .._fiff.pick import pick_channels, pick_channels_forward, pick_info, pick_types
  from .._fiff.tag import find_tag, read_tag
  from .._fiff.tree import dir_tree_find
  from .._fiff.write import end_block, start_and_end_file, start_block, write_coord_trans, write_id, write_int, write_string
  from ..epochs import BaseEpochs
  from ..evoked import Evoked, EvokedArray
  from ..html_templates import _get_html_template
  from ..io import BaseRaw, RawArray
  from ..label import Label
  from ..source_estimate import _BaseSourceEstimate, _BaseVectorSourceEstimate
  from ..source_space._source_space import _get_src_nn, _read_source_spaces_from_tree, _set_source_space_vertices, _src_kind_dict, _write_source_spaces_to_fid, find_source_space_hemi
  from ..surface import _normal_orth
  from ..transforms import invert_transform, transform_surface_to, write_trans
  from ..utils import _check_compensation_grade, _check_fname, _check_option, _check_stc_units, _import_h5io_funcs, _on_missing, _stamp_to_dt, _validate_type, check_fname, fill_doc, get_subjects_dir, has_mne_c, logger, repr_html, run_subprocess, verbose, warn
label_desc: Use image node_id 12 for calling a global function with example usage: _write_forward_solution(fid, fwd) without return types