proc makeHeFill {levcmd} {
  if {$levcmd eq "ccu"} {
    makenv lev he_ccu ccu
    GraphAdd lev % HeLevel brown
  } elseif {$levcmd eq "ccu4ilm"} {
    GraphAdd lev % HeLevel brown
  } else {
    GraphAdd [join $levcmd "."] % HeLevel brown
  }
# LHe fill is disabled by default
  set c [silent 1 result instconfig hefill]
  if {$c} {
#    GraphAdd hefill.smooth % HeCalc orange
    set fast_cmd [list]
    set slow_cmd [list]
    if {$levcmd eq "ccu"} {
      makenv hefill he_ext_ccu ccu
    } elseif {$levcmd eq "ccu4ilm"} {
      makenv hefill -driver ccu4ext he lev
      lappend fast_cmd {lev mode 1}
      lappend slow_cmd {lev mode 0}
    } else {
      makenv hefill -driver he_ext_ccu -controller ccu $levcmd
      if {$levcmd eq "lev" && [sicsdescriptor lev] eq "ilm200"} {
        lappend fast_cmd {lev mode 1}
        lappend slow_cmd {lev mode 0}
      } 
    }
    set vessel [silent 0 result instconfig hevessel]
    if {$vessel eq "ccu" || $vessel eq "1"} {
      GraphAdd cc.h0  % HeVessel blue
      lappend fast_cmd {cc hf 1}
      lappend slow_cmd {cc hf 0}
      hsetprop /hefill vessel_cmd "cc h0"
    } elseif {$vessel eq "ami136"} {
      makenv vhe -driver ami136 cm 
      GraphAdd vhe % HeVessel blue
      lappend fast_cmd {vhe mode 1}
      lappend slow_cmd {vhe mode 0}
      hsetprop /hefill vessel_cmd "vhe"
    } elseif {$vessel ne "0"} {
      error "'instconfig hvessel' must be either 'ami136', 'ccu' or 0"
    }
    if {[llength $slow_cmd] > 0} {
      hsetprop /hefill slow_cmd [join $slow_cmd "\n"]
    }
    if {[llength $fast_cmd] > 0} {
      hsetprop /hefill fast_cmd [join $fast_cmd "\n"]
    }
  }
}

proc check_vessel {read_vessel limit} {
  global vessel_vars
  if {[result $read_vessel] < $limit} {
    return 0
  }
  
}

proc makeN2Fill {{option ccu} {code 0}} {

# the first arg one of:
#   ccu (for old ccu)
#   ccu4 (for new ccu),
#   ccu4ilm (ilm and autofill with ccu4)
#   a port specification containing ':' (for additional ccu3)
#   a command for reading the n2 level
# code is executed when the n2 filling is normally activated on the instrument

  set c [silent 1 result instconfig n2fill]
  if {$option eq "ccu" || [string match *:* $option]} {
    # with ccu N2 fill is enabled by default
    if {$option eq "ccu"} {
      set ctrl ccu
      set port 0
    } else {
      set ctrl ccu2
      set port $option
    }
    makenv n2 -driver n2_ccu -controller $ctrl -port $port
    if {! $c} {
      # switch silently to inactive
      hupdate /n2 4
      set code "0"
    }
    GraphAdd n2.upper K_2 N2_Upper red
    GraphAdd n2.lower K_2 N2_Lower green
  } elseif {$option eq "ccu4ilm"} {
    makenv ln2fill -driver ccu4ext n2 "lev n2"
    GraphAdd lev.n2 % "N2_level" black
    if {! $c} {
      # switch silently to inactive
      hupdate /ln2fill 2
      set code "0"
    }
  } elseif {$option eq "ccu4"} {
    # to be implemented
  } else {
    # else N2 fill is disabled by default
    if {$c} {
      makenv ln2fill n2_ext_ccu ccu
      hupdate /ln2fill/readlevel $option
    } else {
      set code "0"
    }
  }
  if {$code ne "0"} {
    catch {uplevel $code; return 0} msg
    if {$msg ne "0"} {
      clientput $msg
    }
  }
}
