code: def _create_meg_coil(coilset, ch, acc, do_es):
      """Create a coil definition using templates, transform if necessary."""
      # Also change the coordinate frame if so desired
      if ch["kind"] not in [FIFF.FIFFV_MEG_CH, FIFF.FIFFV_REF_MEG_CH]:
          raise RuntimeError("%s is not a MEG channel" % ch["ch_name"])
  
      # Simple linear search from the coil definitions
      for coil in coilset:
          if (
              coil["coil_type"] == (ch["coil_type"] & 0xFFFF)
              and coil["accuracy"] == acc
          ):
              break
      else:
          raise RuntimeError(
              "Desired coil definition not found "
              "(type = %d acc = %d)" % (ch["coil_type"], acc)
          )
  
      # Apply a coordinate transformation if so desired
      coil_trans = _loc_to_coil_trans(ch["loc"])
  
      # Create the result
      res = dict(
          chname=ch["ch_name"],
          coil_class=coil["coil_class"],
          accuracy=coil["accuracy"],
          base=coil["base"],
          size=coil["size"],
          type=ch["coil_type"],
          w=coil["w"],
          desc=coil["desc"],
          coord_frame=FIFF.FIFFV_COORD_DEVICE,
          rmag_orig=coil["rmag"],
          cosmag_orig=coil["cosmag"],
          coil_trans_orig=coil_trans,
          r0=coil_trans[:3, 3],
          rmag=apply_trans(coil_trans, coil["rmag"]),
          cosmag=apply_trans(coil_trans, coil["cosmag"], False),
      )
      if do_es:
          r0_exey = (
              np.dot(coil["rmag"][:, :2], coil_trans[:3, :2].T)
              + coil_trans[:3, 3]
          )
          res.update(
              ex=coil_trans[:3, 0],
              ey=coil_trans[:3, 1],
              ez=coil_trans[:3, 2],
              r0_exey=r0_exey,
          )
      return res
  
imports: import os
  import os.path
  from contextlib import contextmanager
  from copy import deepcopy
  from pathlib import Path
  import numpy
  from .._fiff.compensator import get_current_comp, make_compensator
  from .._fiff.constants import FIFF, FWD
  from .._fiff.meas_info import Info, read_info
  from .._fiff.pick import _has_kit_refs, pick_info, pick_types
  from .._fiff.tag import _loc_to_coil_trans, _loc_to_eeg_loc
  from ..bem import ConductorModel, _bem_find_surface, read_bem_solution
  from ..source_estimate import VolSourceEstimate
  from ..source_space._source_space import _complete_vol_src, _ensure_src, _filter_source_spaces, _make_discrete_source_space
  from ..surface import _CheckInside, _normalize_vectors
  from ..transforms import Transform, _coord_frame_name, _ensure_trans, _get_trans, _print_coord_trans, apply_trans, invert_transform, transform_surface_to
  from ..utils import _check_fname, _pl, _validate_type, logger, verbose, warn
  from ._compute_forward import _compute_forwards
  from .forward import _FWD_ORDER, Forward, _merge_fwds, convert_forward_solution
label_desc: Use image node_id 3 for calling a global function with example usage: _create_meg_coil(coilset, ch, acc, do_es) and returns: res