"""
Example -- Compensating Arrays for Vacuum Coupling to 3D Coils
==============================================================
This script tests array compensation by using
vacuum and plasma shots with identical 3D fields.
These shots where taken from J. King's 2013-23-01
experiment validating 3D Magnetics responses.
"""
import magnetics
############################################## Variables
vshot = 153482 # vacuum I-Coils at 10kHz
pshot = 153480 # corresponding plasma shot
xlim = (3.5e3,4.5e3) # spans 2 amplitudes
base = (3.5e3,3.9e3)
############################################## Comparison Function (stick to 1D Arrays for clarity)
def check_compensation(arrayname,vshot,shot,base,xlim,smooth=1,exclude=[]):
"""
Compare 1D array mode fit's using the standard compensation
and direct compansation (subtracting the vacuum shot data).
"""
# setup
magarray = magnetics.differenced_arrays[arrayname].deepcopy()
vacarray = magarray.deepcopy()
# First off, check the vacuum shot compensation
fvac,avac = magnetics.plt.subplots(2,figsize=(8,6))
vacarray.set_data(vshot)
vacarray = vacarray.remove_baseline(*base)
fvac = vacarray.plotdata(axes=avac[0])
vacarray = vacarray.compensate(c=False,pair=True,fun_type='transfer',xlim=xlim)
vacarray = vacarray.remove_baseline(*base)
fvac = vacarray.plotdata(axes=avac[1])
avac[0].set_title('Coupling Signals')
avac[1].set_title('Compensated Signals')
avac[0].set_xlim(*xlim)
# fit plasma resp. using transfer function compensation
magarray.set_data(shot)
magarray = magarray.compensate(c=False,pair=True,fun_type='transfer',xlim=xlim)
magarray = magarray.smooth(smooth)
magarray = magarray.remove_baseline(*base)
magarray.fit.update(ns=[1],xlim=xlim,exclude=exclude)
ffit, = magarray.fit.plot(dim=1)
# fit plasma resp. using direct compensation (subtracting out vacuum data)
magarray.set_data(shot)
vacarray.set_data(vshot)
magarray = magarray.smooth(smooth)
vacarray = vacarray.smooth(smooth)
magarray = magarray.remove_baseline(*base)
vacarray = vacarray.remove_baseline(*base)
for name,sensor in magarray.sensors.iteritems():
magarray.sensors[name] = sensor-vacarray.sensors[name]
magarray.fit.update(ns=[1],xlim=xlim,exclude=exclude)
ffit, = magarray.fit.plot(dim=1,figure=ffit)
magnetics.plt.show()
return fvac,ffit
############################################## Run some cases
# Check arrays right in front of the driving coils (tough cases!)
# radial field sesnors should have largest coupling
frvac,frfit = check_compensation('ISLD67B',vshot,pshot,base,xlim)
# poloidal field should have less coupling
fpvac,fpfit = check_compensation('MPID67B',vshot,pshot,base,xlim)
# In each case, the transfer function reduces the vacuum data
# by an order of magnitude, which is a good start.
# The plasma reponse modes look identical using both compensation
# techniques, from which we conclude the standard compensation is
# doing a sufficient job for physics analysis. Hooray!