#----- IPS magnet power supply ------

proc mfLayout args {
  Group mf "magnetic field control"
}

proc makeIps args {
  scanargs $args var -limit 0 -startRamp 0.5 -startScript 0 -port 0 -args
  if {$limit == 0} {
    error "limit argument required"
  }
  makenv mf ips -port $port
  mf limit $limit
  mf ramp $startRamp
  mf startScript $startScript
  Layout mf
  appendVars mf.current/Tesla/Current mf/Tesla/Mag_Field
  if {$startScript != 0} {
  clientput "INIT ${startScript}_init $args"
    ${startScript}_init $args
  }
}

proc mfAdvancedGroup {} {
  Label "before changing the field limit look at the web page of [result device name] for the restrictions on [result Instrument]"
  Tip max. limit [result mf maxlimit] \[Tesla\]
  Input "field limit" "mf limit"
  Tip "delay for auto persistent mode" 
  Input "pers. mode delay" "mf persdelay"
  Tip "for fields higher than this value, slow ramp is used"
  Input "trained up to" "mf trainedTo"
  Newline
  Label "Workaround for persistent switch problems:"
  Newline
  Tip "current \[in Tesla\] to go first in persistent mode"
  Input "persistent current" "mf perscurrent"
  Tip "wait time before going to zero \[sec\]"
  Input "wait time" "mf perswait"
  Tip "when field is below this value \[Tesla\]: normal operation"
  Input "do workaround above" "mf perslimit"
}

proc mfGroup {} {
  set startscript [result mf startScript]
  if {$startscript eq "0"} {
    showStatus mf
  } else {
    ${startscript}_showStatus  
  }
  Tip magnetic field set point \[Tesla\]
  Input "set field" "run mf/[result mf targetValue]"
  Tip do not wait on a drive command
  CheckBox "no wait" "mf nowait"
  Newline
  RadioGroup "mf persmode"
  Label "persistent mode:"
  NoNewline
  Tip "do not go into persistent mode for [result mf persdelay] seconds after any field change"
  RadioButton 0 off
  NoNewline
  Tip "go into persistent mode immediately after changing the field"
  RadioButton 1 on
  if {[result mf persmode] != 1} {
    NoNewline
    Tip "go never into persistent mode (not recommended)"
    RadioButton 2 "completely off"
  }
  if {[result mf perswitch] == 1} {
    Label "the switch heater is on"
  } else {
    Label "the switch heater is off"
  }
  Tip ramp rate \[Tesla/min.\]
  Tip the actual rate may be lower
  Tip because of magnet limitations 
  Input "ramp rate" "mf ramp"
  Group mfAdvanced "advanced settings"
}

proc checkMA7_init {arglist} {
  if {[sicsdescriptor checkMA7_pars] eq "notfound"} {
    makeobject checkMA7_pars array logged
    checkMA7_pars makeitem mode
    checkMA7_pars makeitem status
  }
  if {"[lindex $arglist 0]" eq ""} {
    error "checkMA7_init needs asymmetric limit as argument"
  }
  checkMA7_pars makeitem asymmetric_limit [lindex $arglist 0]
  checkMA7_pars makeitem symmetric_limit [result mf limit]
  checkMA7_pars mode unknown
  checkMA7_pars status 0
}

proc checkMA7_confirm {{value 0}} {
  if {$value == 0} {return 0}
  instconfig makeitem checkMA7_expected [result checkMA7_pars mode]
  instconfig makeitem checkMA7_last [clock seconds]
  checkMA7_pars status 0
  mf reconnect
  return 0
}

publishLazy checkMA7_confirm

proc checkMA7_restart {{value 0}} {
  if {$value == 0} {return 0}
  checkMA7_pars status 0
  mf restart 1
  return 0
}

publishLazy checkMA7_restart

proc checkMA7_showStatus {} {
  Newline
  set mode [result checkMA7_pars mode]
  Label "MA7 in $mode mode"
  Newline
  set status [result checkMA7_pars status]
  if {$status eq "0"} {
    showStatus mf
  } else {
    if {$mode eq "symmetric"} {
      set lab SYM
      set other asymmetric
      set olab ASY
    } else {
      set lab ASY
      set other symmetric
      set olab SYM
    }
    if {$status eq "check"} {
      Newline
      Style warning
      Label "for keeping $mode mode:"
      Newline
      Label "check that the current cables are connected to COM and $lab"
      Newline
      Style warning
      Label "for changing to $other mode:"
      Newline
      Label "change IPS parameters S11,S02 and S01 (see MA7 web page)"
      Newline
      Label "connect the current cables to COM and $olab"
      Newline
      Style warning
      CheckBox "done" "checkMA7_confirm"
      Newline
    } else {
      Newline
      Style warning
      Label "see the MA7 web page for the correct parameters on the IPS"
      Newline
      Style warning
      Label $status
      Style warning
      CheckBox "click here when the parameters are correctly set" "checkMA7_restart"
      Newline
    }
  }
}

proc checkMA7 {} {
  set expected [silent undefined result instconfig checkMA7_expected]
  if {[silent 0 result instconfig checkMA7_last] < [clock seconds] - 3600} {
    set expected undefined
  }

  set ind [result mf inductance]
  set rampt [result mf ramp]
  set rampa [result mf ampRamp]
  set sym [expr abs($rampt - $rampa * 6.8 / 96.636) < 0.001]
  set asym [expr abs($rampt - $rampa * 6.2 / 100.657) < 0.001]
  checkMA7_pars status 0
  mf startScript checkMA7
  if {$ind == 112} {
    set mode symmetric
    if {! $sym} {
      checkMA7_pars status "S11 = $ind, S02/S01 do not match" 
    }
  } elseif {$ind == 78.8} {
    set mode asymmetric
    if {! $asym} {
      checkMA7_pars status "S11 = $ind, S02/S01 do not match"
    }
  } elseif {$ind == 0.0} {
    error [result mf status]
  } else {
    if {$sym && !$asym} {
      checkMA7_pars status "S11: $ind, S02: 96.636, S01.2: 6.8"
    } elseif  {$asym && !$sym} {
      checkMA7_pars status "S11: $ind, S02: 100.657, S01.2: 6.2"
    } else {
      checkMA7_pars status "S11: $ind, S02/S01 bad"
    }
  }
  if {[result checkMA7_pars status] ne "0"} {
    error "inconsistent IPS parameters. please open 'magnetic field control'"
  }
  checkMA7_pars mode $mode
  checkMA7_pars status 0
  if {$expected ne $mode} {
    checkMA7_pars status check
    error "please open 'magnetic field control' to check the MA7 mode"
  }
  mf limit [result checkMA7_pars ${mode}_limit]
  return done
}


