示例#1
0
	def linear_phys(self,vf,dt):
		"""Adds linear ramp to waveform, starts at current last 
			value and goes to 'vf' in 'dt'"""
		
		if self.lastPhys == None:
			msg = "The last physics value is not available\n for this waveform."
			msg = msg + "\n\nProgram will be stopped."
			errormsg.box('wfm.linear_phys :: ' + self.name, msg)
			exit(1)
		
		print "...linear_phys last physical value is = %f" % self.lastPhys
		
		v0 = self.lastPhys
		
		#One could also obtain v0 via conversion, but it is not recommended		
		#v0=physics.cnv(self.name+"Phys",self.last())
		
		if dt == 0.0:
			self.y[ self.y.size -1] = physics.cnv(self.name,vf)
			return

			
		N = int(math.floor(dt/self.ss))

		hashbase = ''
		hashbase = hashbase + self.name
		hashbase = hashbase + '%.8f' % vf
		hashbase = hashbase + '%.8f' % v0
		hashbase = hashbase + '%.8f' % N
		hashbase = hashbase + '%.8f' % dt
		hashbase = hashbase + rawcalibdat( self.name ) 
		
		ramphash = seqconf.ramps_dir() + 'linearPhys_' \
			           + hashlib.md5( hashbase ).hexdigest()
	
		if not os.path.exists(ramphash) or True:
				print '...Making new linearPhys ramp for ' + self.name
				
				x = numpy.linspace( v0 + (vf-v0)/N , vf , N )
				ramp = physics.cnv( self.name, x )
				
				#yramp= numpy.array([cnv(self.name,v0 + 1.0*(vf-v0)*(i+1)/N) for i in range(N)])
				
				#if ( numpy.absolute( yramp - ramp ) > 0.0001 ).any():
				#	print "NOT EQUAL!"
				#else:
				#	print "EQUAL!"
					
					
				#ramp.tofile(ramphash,sep=',',format="%.4f")
				
				
		else:
				print '...Recycling previously calculated linearPhys ramp for '  + self.name
				ramp =  numpy.fromfile(ramphash,sep=',')	

		self.y=numpy.append(self.y, ramp)
		self.lastPhys = vf
		
		return
示例#2
0
    def linear_phys(self, vf, dt):
        """Adds linear ramp to waveform, starts at current last 
			value and goes to 'vf' in 'dt'"""

        if self.lastPhys == None:
            msg = "The last physics value is not available\n for this waveform."
            msg = msg + "\n\nProgram will be stopped."
            errormsg.box('wfm.linear_phys :: ' + self.name, msg)
            exit(1)

        print "...linear_phys last physical value is = %f" % self.lastPhys

        v0 = self.lastPhys

        #One could also obtain v0 via conversion, but it is not recommended
        #v0=physics.cnv(self.name+"Phys",self.last())

        if dt == 0.0:
            self.y[self.y.size - 1] = physics.cnv(self.name, vf)
            return

        N = int(math.floor(dt / self.ss))

        hashbase = ''
        hashbase = hashbase + self.name
        hashbase = hashbase + '%.8f' % vf
        hashbase = hashbase + '%.8f' % v0
        hashbase = hashbase + '%.8f' % N
        hashbase = hashbase + '%.8f' % dt
        hashbase = hashbase + rawcalibdat(self.name)

        ramphash = seqconf.ramps_dir() + 'linearPhys_' \
                    + hashlib.md5( hashbase ).hexdigest()

        if not os.path.exists(ramphash) or True:
            print '...Making new linearPhys ramp for ' + self.name

            x = numpy.linspace(v0 + (vf - v0) / N, vf, N)
            ramp = physics.cnv(self.name, x)

            #yramp= numpy.array([cnv(self.name,v0 + 1.0*(vf-v0)*(i+1)/N) for i in range(N)])

            #if ( numpy.absolute( yramp - ramp ) > 0.0001 ).any():
            #	print "NOT EQUAL!"
            #else:
            #	print "EQUAL!"

            #ramp.tofile(ramphash,sep=',',format="%.4f")

        else:
            print '...Recycling previously calculated linearPhys ramp for ' + self.name
            ramp = numpy.fromfile(ramphash, sep=',')

        self.y = numpy.append(self.y, ramp)
        self.lastPhys = vf

        return
示例#3
0
	def odt_linear(self,p0,pf,dt):
		"""Adds linear ramp to waveform, starts at 'p0' 
			value and goes to 'pf' in 'dt' 
			CAREFUL: This uses OdtpowConvert and is only valid for odtpow"""
		print "...ODT Linear from %.3f to %.3f" % (p0,pf)
		if dt == 0.0:
			self.y[ self.y.size -1] = physics.cnv('odtpow', pf)
			return
		else:
			N = int(math.floor(dt/self.ss))
			for i in range(N):
				self.y=numpy.append(self.y, [ physics.cnv('odtpow',  p0 + (pf-p0)*(i+1)/N )])
		return 
示例#4
0
	def linear(self,vf,dt,volt=-11):
		"""Adds linear ramp to waveform, starts at current last 
			value and goes to 'vf' in 'dt' 
			CAREFUL: This is linear in voltage, not in phys"""
		#~ if 'ir' in self.name  and 'pow' in self.name:
			#~ print "%s LINEAR: Conversion flag = %d, vf_in=%f" % (self.name, volt,vf)
			
		if volt >= -10.0 and volt <= 10.0:
			self.lastPhys = None
			vf=volt
		elif volt == -11:
			self.lastPhys = vf
			vf=physics.cnv(self.name,vf)
			
		#~ if 'ir' in self.name  and 'pow' in self.name:
			#~ print "%s  LINEAR: Conversion flag = %d, vf_out=%f" % (self.name, volt,vf)
			
		v0 = round(self.last(),4)
		if dt == 0.0:
			self.y[ self.y.size -1] = vf
			return
		else:
			N = int(math.floor(dt/self.ss))
			x = numpy.linspace( 1, N, N)
			ramp =   numpy.around(v0 + (vf-v0)*x/N, decimals=4)
			self.y = numpy.append( self.y, ramp )
		return
示例#5
0
	def insertlin_cnv(self,vf,dt,start):
		"""Inserts a linear ramp (vf,dt) at a time 'start' referenced from 
			the end of the current sate of the wfm.  
			
			start > 0 : appends a hold before doing the ramp
			"""
		self.lastPhys = vf
		vf=physics.cnv(self.name,vf)
		
		if start>0:
			self.apppendhold(start-self.ss)
			self.y = numpy.append(self.y,[vf])
			return
		elif -start > self.dt():
			exc = "wfm.py: Cannot insert ramp before the beggiging of the waveform"
                        print exc
                        raise Exception(exc)
			#exit(1)
		elif dt > -start:
			exc = "wfm.py: Ramp is too long for inserting"
                        print exc
                        raise Exception(exc)
			#exit(1)
		Nstart=int(math.floor(-start/self.ss))
		if dt==0. :
			N=0
			self.y[self.y.size -1 - Nstart]=vf
		else:
			N=int(math.floor(dt/self.ss))
			v0 = self.y[self.y.size -1 - Nstart]
			for i in range(N):
				self.y[self.y.size - Nstart + i] = v0 + (vf-v0)*(i+1)/N				
		for i in range( Nstart -N):
			self.y[self.y.size - Nstart + N + i] = vf
		return
示例#6
0
	def tanhRise(self,vf,dt,tau,shift):
		#print "---> Initializing lattice wave : %s" % self.name
		#print "convert.cnv(%.6f) = %.6f" % ( vf, cnv(self.name,vf))
		#print "physics.cnv(%.6f) = %.6f" % ( vf, physics.cnv(self.name,vf))
		#vf=cnv(self.name,vf)
		physf = vf
		vf=physics.cnv(self.name,vf)
		v0=self.last()
		if dt == 0.0:
			self.y[ self.y.size -1] = vf
			return
		else:
			N = int(math.floor(dt/self.ss))
			print '...Making new tanhRise ramp for ' + self.name + \
			       ' physf=%.3f, vf = %.3f, dt = %.3f, tau = %.2f, shift = %.2f' % (physf,vf, dt, tau, shift)
			x=numpy.arange(dt/N,dt+dt/N,dt/N) # arange is half open: [start,stop)
                        #print '\t x[ 0] =',x[ 0]
                        #print '\t x[-1] =',x[-1]
                        #print '\t    ss =',self.ss
                        #print '\tlen(x) =',x.size
                        #print '\t     N =',N
			tau = tau*dt
			shift = dt/2. + shift*dt/2.
			ramp= v0 + (vf-v0)* ( (1+numpy.tanh((x-shift)/tau)) - (1+numpy.tanh((-shift)/tau)) )\
							/ ( (1+numpy.tanh((dt-shift)/tau)) - (1+numpy.tanh((-shift)/tau)) )
			self.y=numpy.append(self.y, ramp)	
			return
示例#7
0
 def tanhRise(self, vf, dt, tau, shift):
     #print "---> Initializing lattice wave : %s" % self.name
     #print "convert.cnv(%.6f) = %.6f" % ( vf, cnv(self.name,vf))
     #print "physics.cnv(%.6f) = %.6f" % ( vf, physics.cnv(self.name,vf))
     #vf=cnv(self.name,vf)
     physf = vf
     vf = physics.cnv(self.name, vf)
     v0 = self.last()
     if dt == 0.0:
         self.y[self.y.size - 1] = vf
         return
     else:
         N = int(math.floor(dt / self.ss))
         print '...Making new tanhRise ramp for ' + self.name + \
                ' physf=%.3f, vf = %.3f, dt = %.3f, tau = %.2f, shift = %.2f' % (physf,vf, dt, tau, shift)
         x = numpy.arange(dt / N, dt + dt / N,
                          dt / N)  # arange is half open: [start,stop)
         #print '\t x[ 0] =',x[ 0]
         #print '\t x[-1] =',x[-1]
         #print '\t    ss =',self.ss
         #print '\tlen(x) =',x.size
         #print '\t     N =',N
         tau = tau * dt
         shift = dt / 2. + shift * dt / 2.
         ramp= v0 + (vf-v0)* ( (1+numpy.tanh((x-shift)/tau)) - (1+numpy.tanh((-shift)/tau)) )\
             / ( (1+numpy.tanh((dt-shift)/tau)) - (1+numpy.tanh((-shift)/tau)) )
         self.y = numpy.append(self.y, ramp)
         return
示例#8
0
    def insertlin_cnv(self, vf, dt, start):
        """Inserts a linear ramp (vf,dt) at a time 'start' referenced from 
			the end of the current sate of the wfm.  
			
			start > 0 : appends a hold before doing the ramp
			"""
        self.lastPhys = vf
        vf = physics.cnv(self.name, vf)

        if start > 0:
            self.apppendhold(start - self.ss)
            self.y = numpy.append(self.y, [vf])
            return
        elif -start > self.dt():
            exc = "wfm.py: Cannot insert ramp before the beggiging of the waveform"
            print exc
            raise Exception(exc)
            #exit(1)
        elif dt > -start:
            exc = "wfm.py: Ramp is too long for inserting"
            print exc
            raise Exception(exc)
            #exit(1)
        Nstart = int(math.floor(-start / self.ss))
        if dt == 0.:
            N = 0
            self.y[self.y.size - 1 - Nstart] = vf
        else:
            N = int(math.floor(dt / self.ss))
            v0 = self.y[self.y.size - 1 - Nstart]
            for i in range(N):
                self.y[self.y.size - Nstart + i] = v0 + (vf - v0) * (i + 1) / N
        for i in range(Nstart - N):
            self.y[self.y.size - Nstart + N + i] = vf
        return
示例#9
0
    def linear(self, vf, dt, volt=-11):
        """Adds linear ramp to waveform, starts at current last 
			value and goes to 'vf' in 'dt' 
			CAREFUL: This is linear in voltage, not in phys"""
        #~ if 'ir' in self.name  and 'pow' in self.name:
        #~ print "%s LINEAR: Conversion flag = %d, vf_in=%f" % (self.name, volt,vf)

        if volt >= -10.0 and volt <= 10.0:
            self.lastPhys = None
            vf = volt
        elif volt == -11:
            self.lastPhys = vf
            vf = physics.cnv(self.name, vf)

        #~ if 'ir' in self.name  and 'pow' in self.name:
        #~ print "%s  LINEAR: Conversion flag = %d, vf_out=%f" % (self.name, volt,vf)

        v0 = round(self.last(), 4)
        if dt == 0.0:
            self.y[self.y.size - 1] = vf
            return
        else:
            N = int(math.floor(dt / self.ss))
            x = numpy.linspace(1, N, N)
            ramp = numpy.around(v0 + (vf - v0) * x / N, decimals=4)
            self.y = numpy.append(self.y, ramp)
        return
示例#10
0
	def follow(self, bfield, detuning):
		### Levitation voltage:
		###
		### Vlev = slope * I + offset
		###
		### wherer I is the current on the bias coils
		### slope and offset have been calibrated and are set below:
		
		slope = 0.0971
		offset = -2.7232
		
		if self.ss != bfield.ss:
			msg = "ERROR in HFIMG wave:  step size does not match the bfield ramp!"
			print msg
			errormsg.box('hfimg_wave.follow', msg)
			exit(1)
		
		print "...Setting ANALOGHFIMG to follow bfield ramp"
		
		bfieldV = numpy.copy(bfield.y)
		bfieldG = physics.inv( 'bfield', bfieldV)* 6.8
		
		hfimg0 = -1.*(100.0 + 163.7 - 1.414*bfieldG)
		
		self.y = physics.cnv( 'analogimg', hfimg0 - detuning)
		
		return hfimg0[-1]
示例#11
0
    def sinhRise(self, vf, dt, tau):
        """Inserts a hyperbolic-sine-like ramp to the waveform.  
			tau is the ramp scale, it is understood in units of dt
			
			if tau is equal or greater to dt the ramp approaches a
			linear ramp
		
			as tau gets smaller than dt the ramp starts to deviate from 
			linear
			
			a real difference starts to be seen wheh tau = dt/3
			
			from tau=dt/20 it doesn't make a difference to keep making
			tau smaller
			
			good values to vary it are tau=dt/20, dt/5, dt/3, dt/2, dt"""
        ### WARNING: in general the conversion should be done for every point
        ### i.e. inside of the for loop below.
        ### This one just does it on the endpoints.  It is ok because it is used
        ### for the magnetic field which has a mostly linear relationship between
        ### set voltage and current in the coils.  But don't use this as an example
        ### for other things.
        self.lastPhys = vf
        vf = physics.cnv(self.name, vf)

        v0 = self.last()
        if dt == 0.0:
            self.y[self.y.size - 1] = vf
            return
        else:
            N = int(math.floor(dt / self.ss))

            hashbase = ''
            hashbase = hashbase + self.name
            hashbase = hashbase + '%.8f' % vf
            hashbase = hashbase + '%.8f' % v0
            hashbase = hashbase + '%.8f' % N
            hashbase = hashbase + '%.8f' % dt
            hashbase = hashbase + '%.8f' % tau
            ramphash = seqconf.ramps_dir() + 'sinhRise_' + hashlib.md5(
                hashbase).hexdigest()

            if not os.path.exists(ramphash) or True:
                print '...Making new sinhRise ramp'

                x = numpy.linspace(dt / N, dt, N)
                ramp = v0 + (vf - v0) * (x / tau + (x / tau)**3.0 / 6.0) / (
                    dt / tau + (dt / tau)**3.0 / 6.0)

                #ramp.tofile(ramphash,sep=',',format="%.4f")

            else:
                print '...Recycling previously calculated sinhRise ramp'
                ramp = numpy.fromfile(ramphash, sep=',')

            self.y = numpy.append(self.y, ramp)
        return
示例#12
0
	def Evap8(self, p0, p1, t1, tau, beta, offset, t2, tau2, smoothdt, duration,scale = 1.0):
		"""Evaporation ramp v8 same as v7 with scale"""
		if True:
			print ""
			print "----- EVAPORATION RAMP Version 8-----"
			
		if duration <=0:
			return
		else:
			N=int(round(duration*scale/self.ss))
			print '\t...Evap nsteps = ' + str(N)
			ramp_phys=numpy.array([])
			ramp=numpy.array([])
			
			hashbase = ''
			hashbase = hashbase + '%.s' % self.name
			hashbase = hashbase + '%.8f' % self.ss
			hashbase = hashbase + '%.8f' % duration
			hashbase = hashbase + '%.8f' % p0
			hashbase = hashbase + '%.8f' % p1
			hashbase = hashbase + '%.8f' % t1
			hashbase = hashbase + '%.8f' % tau
			hashbase = hashbase + '%.8f' % beta
			hashbase = hashbase + '%.8f' % offset
			hashbase = hashbase + '%.8f' % t2
			hashbase = hashbase + '%.8f' % tau2
			hashbase = hashbase + '%.8f' % smoothdt
			hashbase = hashbase + '%.8f' % scale

			ramphash = seqconf.ramps_dir() +'Evap8_' \
						+ hashlib.md5( hashbase).hexdigest()
						
			#Here, go ahead and save the trajectory path to the report
			gen.save_to_report('EVAP','ramp', ramphash+'_phys')
			
			if not os.path.exists(ramphash) or True:
				print '\t...Making new Evap8 ramp'
				for xi in range(N):
					t = (xi+1)*self.ss/scale
					phys = evap.v6(t,p0,p1,t1,tau,beta, offset,t2,tau2,smoothdt)                    
					volt = physics.cnv( 'odtpow', phys)
					
					ramp_phys = numpy.append( ramp_phys, [ phys])
					ramp = numpy.append( ramp, numpy.around([ volt],decimals=4))
				ramp_phys.tofile(ramphash+'_phys',sep='\n',format="%.4f")
				#ramp.tofile(ramphash,sep=',',format="%.4f")
			
			else:
				print '\t...Recycling previously calculated Evap8 ramp'
				ramp = numpy.fromfile(ramphash,sep=',')

			self.y=numpy.append(self.y,ramp)

		#This returns the last value of the ramp
		print ""
		return evap.v6(N*self.ss/scale,p0,p1,t1,tau,beta,offset,t2,tau2,smoothdt)
示例#13
0
	def sinhRise(self,vf,dt,tau):
		"""Inserts a hyperbolic-sine-like ramp to the waveform.  
			tau is the ramp scale, it is understood in units of dt
			
			if tau is equal or greater to dt the ramp approaches a
			linear ramp
		
			as tau gets smaller than dt the ramp starts to deviate from 
			linear
			
			a real difference starts to be seen wheh tau = dt/3
			
			from tau=dt/20 it doesn't make a difference to keep making
			tau smaller
			
			good values to vary it are tau=dt/20, dt/5, dt/3, dt/2, dt"""
		### WARNING: in general the conversion should be done for every point
		### i.e. inside of the for loop below.
		### This one just does it on the endpoints.  It is ok because it is used
		### for the magnetic field which has a mostly linear relationship between
		### set voltage and current in the coils.  But don't use this as an example
		### for other things.   
		self.lastPhys = vf
		vf=physics.cnv(self.name,vf)
		
		v0=self.last()
		if dt == 0.0:
			self.y[ self.y.size -1] = vf
			return
		else:
			N = int(math.floor(dt/self.ss))
			
			hashbase = ''
			hashbase = hashbase + self.name
			hashbase = hashbase + '%.8f' % vf
			hashbase = hashbase + '%.8f' % v0
			hashbase = hashbase + '%.8f' % N
			hashbase = hashbase + '%.8f' % dt
			hashbase = hashbase + '%.8f' % tau
			ramphash = seqconf.ramps_dir() + 'sinhRise_' + hashlib.md5(hashbase).hexdigest()
			
			if not os.path.exists(ramphash) or True:
				print '...Making new sinhRise ramp'
				
				x = numpy.linspace( dt/N, dt, N)
				ramp = v0 + (vf-v0)*(x/tau+(x/tau)**3.0/6.0)/(dt/tau+(dt/tau)**3.0/6.0)
				
				#ramp.tofile(ramphash,sep=',',format="%.4f")
				
				
			else:
				print '...Recycling previously calculated sinhRise ramp'
				ramp =  numpy.fromfile(ramphash,sep=',')
			
			self.y=numpy.append(self.y, ramp)
		return
示例#14
0
	def __init__(self,name,val,stepsize,N=1,volt=-11):
		"""Initialize the waveform  """
		self.idnum = time.time()*100
		self.name = name
		if volt != -11:
			val=volt
		else:
			val=physics.cnv('odtpow', val)
				
		self.y= numpy.array(N*[val])
		self.ss=stepsize
示例#15
0
    def __init__(self, name, val, stepsize, N=1, volt=-11):
        """Initialize the waveform  """

        self.idnum = time.time() * 100 + 1e3 * numpy.random.randint(0, 1e8)
        self.name = name

        self.lastPhys = None

        if volt != -11:
            val = volt
        else:

            #print "INITIALIZING WFM:  %s, val=%f,  volt=%f" % (name,val,physics.cnv(self.name,val))

            self.lastPhys = val
            val = physics.cnv(self.name, val)

        self.y = numpy.array(N * [val])
        self.ss = stepsize
示例#16
0
	def __init__(self,name,val,stepsize,N=1,volt=-11):
		"""Initialize the waveform  """

		self.idnum = time.time()*100 + 1e3*numpy.random.randint(0,1e8)
		self.name = name
		
		self.lastPhys = None
		
		if volt != -11:
			val=volt
		else:
			
			#print "INITIALIZING WFM:  %s, val=%f,  volt=%f" % (name,val,physics.cnv(self.name,val))
			
			self.lastPhys = val
			val=physics.cnv(self.name,val)
			
				
		self.y= numpy.array(N*[val])
		self.ss=stepsize
示例#17
0
	def tanhRise(self,vf,dt,tau,shift):
		#print "---> Initializing lattice wave : %s" % self.name
		#print "convert.cnv(%.6f) = %.6f" % ( vf, cnv(self.name,vf))
		#print "physics.cnv(%.6f) = %.6f" % ( vf, physics.cnv(self.name,vf))
		#vf=cnv(self.name,vf)
		vf=physics.cnv(self.name,vf)
		v0=self.last()
		if dt == 0.0:
			self.y[ self.y.size -1] = vf
			return
		else:
			N = int(math.floor(dt/self.ss))
			print '...Making new tanhRise ramp for ' + self.name + \
			       ' vf = %.2f, dt = %.3f, tau = %.2f, shift = %.2f' % (vf, dt, tau, shift)
			x=numpy.arange(dt/N,dt,dt/N)
			tau = tau*dt
			shift = dt/2. + shift*dt/2.
			ramp= v0 + (vf-v0)* ( (1+numpy.tanh((x-shift)/tau)) - (1+numpy.tanh((-shift)/tau)) )\
							/ ( (1+numpy.tanh((dt-shift)/tau)) - (1+numpy.tanh((-shift)/tau)) )
			self.y=numpy.append(self.y, ramp)	
			return
示例#18
0
	def follow(self, bfield):
		### Levitation voltage:
		###
		### Vlev = slope * I + offset
		###
		### wherer I is the current on the bias coils
		### slope and offset have been calibrated and are set below:
		
		## These numbers are now store in physics.py
		## slope = 0.0971
		## offset = -2.7232
		
		if self.ss != bfield.ss:
			msg = "ERROR in GRADIENT wave:  step size does not match the bfield ramp!"
			print msg
			errormsg.box('gradient_wave.follow', msg)
			exit(1)
		
		print "...Setting GRADIENT to follow bfield ramp"
		
		bfieldV = numpy.copy(bfield.y)
		bfieldA = physics.inv( 'bfield', bfieldV)
		#~ self.y = slope * bfieldA + offset
		self.y= np.array([physics.cnv( 'gradientfield', bA) for bA in bfieldA])
示例#19
0
def dimple_to_lattice(s, cpowend):

    print "----- LATTICE LOADING RAMPS -----"

    # Find out which is the longest of the ramps we are dealing with:
    maxX =max( [xdomain(DL.latticeV0)[1] ,\
         xdomain(DL.irpow)[1],\
         xdomain(DL.grpow1)[1],\
         xdomain(DL.grpow2)[1],\
         xdomain(DL.grpow3)[1],\
         xdomain(DL.a_s)[1]] )
    print "Largest x value = %.3f ms\n" % maxX

    # We define the times for which all functions will be evaluated
    # MIN TIME TO DO DIGITAL EXTENSION
    DIGEXTENSION = 2050.
    if DL.image >= DIGEXTENSION:
        Xendtime = DIGEXTENSION
    else:
        Xendtime = DL.image

    Nnew = int(math.floor(Xendtime / DL.ss))
    Xnew = numpy.arange(Xendtime / Nnew, DL.image, Xendtime / Nnew)
    print "X array defined from dt:"
    print "DL.ss =", DL.ss
    print "x0  = ", Xnew[0]
    print "xf  = ", Xnew[-1]
    print "xdt = ", Xnew[1] - Xnew[0]
    print "%d samples" % Nnew
    print 'x shape = ', Xnew.shape

    # Define how we want to ramp up the lattice depth
    v0_ramp, xy_v0, v0set = interpolate_ramp(DL.latticeV0)
    v0 = v0_ramp(Xnew)

    ###########################################
    #### AXIS DEFINITIONS FOR PLOTS ###
    ###########################################

    fig = plt.figure(figsize=(4.5 * 1.05, 8. * 1.1))
    ax0 = fig.add_axes([0.18, 0.76, 0.76, 0.20])
    ax2 = fig.add_axes([0.18, 0.645, 0.76, 0.11])
    ax3 = fig.add_axes([0.18, 0.53, 0.76, 0.11])
    ax1 = fig.add_axes([0.18, 0.415, 0.76, 0.11])
    ax5 = fig.add_axes([0.18, 0.30, 0.76, 0.11])
    ax4 = fig.add_axes([0.18, 0.185, 0.76, 0.11])
    ax6 = fig.add_axes([0.18, 0.07, 0.76, 0.11])

    allax = [ax0, ax1, ax2, ax3, ax4, ax5, ax6]
    for ax in allax:
        ax.axvline(DL.image, linewidth=1., color='black', alpha=0.6)

    lw = 1.5
    labelx = -0.12
    legsz = 8.

    xymew = 0.5
    xyms = 9

    ax0.plot(Xnew, v0, 'b', lw=2.5, label='Lattice depth')
    ax0.plot(xy_v0[:, 0], xy_v0[:, 1], 'x', color='blue', ms=5.)
    ax0.plot(v0set[:, 0], v0set[:, 1], '.', mew=xymew, ms=xyms, color='blue')

    ###########################################
    #### USER DEFINED RAMPS: IR, GR, and U ###
    ###########################################

    # Define how we want to ramp up the IR power
    if DIMPLE.allirpow > 0.:
        ir_offset = DIMPLE.allirpow
    else:
        ir_offset = DIMPLE.ir1pow2
    ir_ramp, xy_ir, ir = interpolate_ramp(DL.irpow, yoffset=ir_offset)

    dt_ir = numpy.amax(ir[:, 0]) - numpy.amin(ir[:, 0])
    N_ir = int(math.floor(dt_ir / DL.ss))
    x_ir = numpy.arange(dt_ir / N_ir, dt_ir, dt_ir / N_ir)

    y_ir = ir_ramp(Xnew)

    if v0.size > y_ir.size:
        y_ir = numpy.append(y_ir, (v0.size - y_ir.size) * [y_ir[-1]])
    elif v0.size < y_ir.size:
        y_ir = y_ir[0:v0.size]

    if v0.size != y_ir.size:
        msg = "IRPOW ERROR: number of samples in IR ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR', msg)
        exit(1)

    alpha_clip_range = 0.1
    if (v0 > y_ir + alpha_clip_range).any():
        msg = "IRPOW ERROR:  not enough power to get desired lattice depth"
        print msg
        bad = numpy.where(v0 > y_ir + alpha_clip_range)
        timefail = int(bad[0][0]) * float(DL.ss)
        msg = msg + "\nFirst bad sample = %d out of %d" % (bad[0][0], v0.size)
        msg = msg + "\n  t = %f " % timefail
        msg = msg + "\n v0 = %f " % v0[bad[0][0]]
        msg = msg + "\n ir = %f " % y_ir[bad[0][0]]
        print v0[bad[0][0]]
        print y_ir[bad[0][0]]
        errormsg.box('LATTICE LOADING ERROR', msg)
        exit(1)

    ax0.plot(xy_ir[:, 0], xy_ir[:, 1], 'x', color='darkorange', ms=5.)
    ax0.plot(ir[:, 0], ir[:, 1], '.', mew=xymew, ms=xyms, color='darkorange')
    ax0.plot(Xnew, y_ir, lw=lw, color='darkorange', label='irpow')

    # Define how we want to ramp up the GR power
    grwfms = {}
    splmrkr = ['x', '+', 'd']
    ptsmrkr = ['^', 's', 'p']
    for i, grramp in enumerate([(DL.grpow1, DIMPLE.gr1pow2),
                                (DL.grpow2, DIMPLE.gr2pow2),
                                (DL.grpow3, DIMPLE.gr3pow2)]):
        ramppts = grramp[0]
        ramp0 = grramp[1]

        print 'gr' + '%d' % i + ' offset = %f' % ramp0

        gr_ramp, xy_gr, gr = interpolate_ramp(ramppts, yoffset=ramp0)

        dt_gr = numpy.amax(gr[:, 0]) - numpy.amin(gr[:, 0])
        N_gr = int(math.floor(dt_gr / DL.ss))
        x_gr = numpy.arange(dt_gr / N_gr, dt_gr, dt_gr / N_gr)

        y_gr = gr_ramp(Xnew)
        if DL.signal == 0:
            y_gr = y_gr / 2.0

        if v0.size > y_gr.size:
            y_gr = numpy.append(y_gr, (v0.size - y_gr.size) * [y_gr[-1]])
        elif v0.size < y_gr.size:
            y_gr = y_gr[0:v0.size]

        if v0.size != y_gr.size:
            msg = "GRPOW ERROR: number of samples in GR ramp and V0 ramp does not match!"
            errormsg.box('LATTICE LOADING ERROR', msg)
            exit(1)

        grwfms['greenpow' + '%1d' % (i + 1)] = y_gr

        ax0.plot(xy_gr[:, 0],
                 xy_gr[:, 1],
                 marker=splmrkr[i],
                 mec='green',
                 mfc='None',
                 ms=3.)
        ax0.plot(gr[:, 0],
                 gr[:, 1],
                 marker=ptsmrkr[i],
                 mew=xymew,
                 ms=xyms / 2.,
                 mfc='None',
                 mec='green')  #, label='grpow dat')
        ax0.plot(Xnew, y_gr, lw=lw, color='green', label='grpow')

    for grch in grwfms.keys():
        print grch, " = ", grwfms[grch].shape

    ax0.set_xlim(left=-10., right=ax0.get_xlim()[1] * 1.1)
    plt.setp(ax0.get_xticklabels(), visible=False)
    ylim = ax0.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax0.set_ylim(ylim[0] - extra, ylim[1] + extra)
    ax0.grid(True)
    ax0.set_ylabel('$E_{r}$', size=16, labelpad=0)
    ax0.yaxis.set_label_coords(labelx, 0.5)
    ax0.set_title('Lattice Loading')
    ax0.legend(loc='best', numpoints=1, prop={'size': legsz * 0.8})

    # Define how we want to ramp up the scattering length (control our losses)
    a_s_ramp, xy_a_s, a_s = interpolate_ramp(DL.a_s)

    dt_a_s = numpy.amax(a_s[:, 0]) - numpy.amin(a_s[:, 0])
    N_a_s = int(math.floor(dt_a_s / DL.ss))
    x_a_s = numpy.arange(dt_a_s / N_a_s, dt_a_s, dt_a_s / N_a_s)
    y_a_s = a_s_ramp(Xnew)

    if v0.size > y_a_s.size:
        y_a_s = numpy.append(y_a_s, (v0.size - y_a_s.size) * [y_a_s[-1]])
    elif v0.size < y_a_s.size:
        y_a_s = y_a_s[0:v0.size]

    if v0.size != y_a_s.size:
        msg = "a_s ERROR: number of samples in a_s ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR', msg)
        exit(1)

    ax1.plot(xy_a_s[:, 0], xy_a_s[:, 1] / 100., 'x', color='#C10087', ms=5.)
    ax1.plot(a_s[:, 0],
             a_s[:, 1] / 100.,
             '.',
             mew=xymew,
             ms=xyms,
             color='#C10087')
    ax1.plot(Xnew,
             y_a_s / 100.,
             lw=lw,
             color='#C10087',
             label=r'$a_s\mathrm{(100 a_{0})}$')
    ax1.set_ylabel(r'$a_s\mathrm{(100 a_{0})}$', size=16, labelpad=0)
    ax1.yaxis.set_label_coords(labelx, 0.5)

    ax1.set_xlim(ax0.get_xlim())
    ylim = ax1.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax1.set_ylim(ylim[0] - extra, ylim[1] + extra)
    plt.setp(ax1.get_xticklabels(), visible=False)
    ax1.grid(True)
    ax1.legend(loc='best', numpoints=1, prop={'size': legsz})

    #######################################################################
    #### CALCULATED RAMPS:  ALPHA, TUNNELING, SCATTERING LENGTH, BFIELD ###
    #######################################################################

    alpha = (v0 / y_ir)**2.

    alpha_advance = 100.
    N_adv = int(math.floor(alpha_advance / DL.ss))

    alpha = alpha.clip(0., 1.)
    alpha_desired = numpy.copy(alpha)

    if N_adv < v0.size:
        alpha = alpha[N_adv:]
        alpha = numpy.append(alpha, (v0.size - alpha.size) * [alpha[-1]])
    else:
        alpha = numpy.array(v0.size * [alpha[-1]])

    #alpha = alpha.clip(0., 1.)

    ax2.plot(Xnew, alpha, lw=lw, color='saddlebrown', label='alpha adv')
    ax2.plot(Xnew,
             alpha_desired,
             ':',
             lw=lw,
             color='saddlebrown',
             label='alpha')

    ax2.set_xlim(ax0.get_xlim())
    ax2.set_ylim(-0.05, 1.05)
    plt.setp(ax2.get_xticklabels(), visible=False)
    ax2.grid()
    ax2.set_ylabel('$\\alpha$', size=16, labelpad=0)
    ax2.yaxis.set_label_coords(labelx, 0.5)

    ax2.legend(loc='best', numpoints=1, prop={'size': legsz})

    tunneling_Er = physics.inv('t_to_V0', v0)
    tunneling_kHz = tunneling_Er * 29.2

    ax3.plot(Xnew, tunneling_kHz, lw=lw, color='red', label='$t$ (kHz)')

    ax3.set_xlim(ax0.get_xlim())
    ylim = ax3.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax3.set_ylim(ylim[0] - extra, ylim[1] + extra)
    plt.setp(ax3.get_xticklabels(), visible=False)
    ax3.grid(True)
    ax3.set_ylabel(r'$t\,\mathrm{(kHz)}$', size=16, labelpad=0)
    ax3.yaxis.set_label_coords(labelx, 0.5)
    ax3.legend(loc='best', numpoints=1, prop={'size': legsz})

    wannierF = physics.inv('wF_to_V0', v0)

    bohrRadius = 5.29e-11  #meters
    lattice_spacing = 1.064e-6 / 2.  #meters

    bfieldG = physics.cnv('as_to_B', y_a_s)
    print
    print "The last value of the scattering length ramp is:"
    print 'a_s =', y_a_s[-1]
    print 'B   =', bfieldG[-1]
    print

    U_over_t = y_a_s * bohrRadius / lattice_spacing * wannierF / tunneling_Er

    ax4.plot(Xnew, U_over_t, lw=lw, color='k', label=r'$U/t$')

    ax4.set_xlim(ax0.get_xlim())
    ylim = ax4.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax4.set_ylim(ylim[0] - extra, ylim[1] + extra)
    plt.setp(ax4.get_xticklabels(), visible=False)
    ax4.grid(True)
    ax4.set_ylabel(r'$U/t$', size=16, labelpad=0)
    ax4.yaxis.set_label_coords(labelx, 0.5)

    ax4.legend(loc='best', numpoints=1, prop={'size': legsz})

    ax5.plot(Xnew, bfieldG, lw=lw, color='purple', label='$B$ (G)')

    ax5.set_xlim(ax0.get_xlim())
    ylim = ax5.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax5.set_ylim(ylim[0] - extra, ylim[1] + extra)
    ax5.grid(True)
    plt.setp(ax5.get_xticklabels(), visible=False)
    ax5.set_ylabel(r'$B\,\mathrm{(G)}$', size=16, labelpad=0)
    ax5.yaxis.set_label_coords(labelx, 0.5)

    ax5.legend(loc='best', numpoints=1, prop={'size': legsz})

    ax6.plot(Xnew, (tunneling_Er / U_over_t),
             lw=lw,
             color='#25D500',
             label=r'$t^{2}/U\,(E_{r)}$')
    #ax6.set_yscale('log')

    ax6.set_xlim(ax0.get_xlim())
    ylim = ax6.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax6.set_ylim(ylim[0] * 0.5, ylim[1])
    ax6.grid(True)
    ax6.set_ylabel(r'$t^{2}/U\,(E_{r)}$', size=16, labelpad=0)
    ax6.yaxis.set_label_coords(labelx, 0.5)

    ax6.legend(loc='best', numpoints=1, prop={'size': legsz})

    ax6.set_xlabel('time (ms)')

    figfile = seqconf.seqtxtout().split('.')[0] + '_latticeRamp.png'
    plt.savefig(figfile, dpi=120)

    #Save all ramps to a txt file for later plotting.
    datfile = seqconf.seqtxtout().split('.')[0] + '_latticeRamp.dat'
    allRamps = numpy.transpose(numpy.vstack((Xnew, v0, y_ir, grwfms['greenpow1'], y_a_s, alpha, alpha_desired, \
                                    tunneling_kHz, U_over_t, bfieldG)))
    header = '# Column index'
    header = header + '\n#\t0\t' + 'time(ms)'
    header = header + '\n#\t1\t' + 'Lattice Depth (Er)'
    header = header + '\n#\t2\t' + 'Ir power (Er)'
    header = header + '\n#\t3\t' + 'GR power (Er)'
    header = header + '\n#\t4\t' + 'a_s (a0)'
    header = header + '\n#\t5\t' + 'alpha - advance'
    header = header + '\n#\t6\t' + 'alpha - desired'
    header = header + '\n#\t7\t' + 'tunneling (kHz)'
    header = header + '\n#\t8\t' + 'U/t'
    header = header + '\n#\t9\t' + 'bfield (Gauss)'
    header = header + '\n'

    numpy.savetxt(datfile, allRamps)

    with open(datfile, 'w') as f:
        X = numpy.asarray(allRamps)
        f.write(bytes(header))

        format = '%.6e'
        ncol = X.shape[1]
        format = [
            format,
        ] * ncol
        format = ' '.join(format)

        newline = '\n'
        for row in X:
            f.write(numpy.compat.asbytes(format % tuple(row) + newline))

    shutil.copyfile(
        figfile,
        seqconf.savedir() + 'expseq' + seqconf.runnumber() +
        '_latticeRamp.png')
    shutil.copyfile(
        datfile,
        seqconf.savedir() + 'expseq' + seqconf.runnumber() +
        '_latticeRamp.dat')
    #plt.savefig( seqconf.savedir() + 'expseq' + seqconf.runnumber() + '_latticeRamp.png', dpi=120)

    #################################
    #### APPEND RAMPS TO SEQUENCE ###
    #################################

    wfms = []

    if DL.signal == 0:
        print " LOCK VALUE FOR SIGNAL / NOSIGNAL "
        print " before = ", DL.lock_Er
        DL.lock_Er = DL.lock_Er / 1.8
        print " after  = \n", DL.lock_Er

    for ch in ['ir1pow', 'ir2pow', 'ir3pow']:
        n = filter(str.isdigit, ch)[0]
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        w.y = physics.cnv(ch, y_ir)
        if DL.lock:
            endval = w.y[-1]
            w.insertlin_cnv(DL.lock_Er, DL.lock_dtUP, DL.lock_t0)
        elif DL.lightassist_lock:
            endval = w.y[-1]
            w.linear(DL.lightassist_lockpowIR, DL.lightassist_lockdtUP)
            w.appendhold(DL.lightassist_t0 + DL.lightassistdt)
            if DL.endvalIR >= 0.:
                w.linear(DL.endvalIR, DL.lightassist_lockdtDOWN)
            else:
                w.linear(None, DL.lightassist_lockdtDOWN, volt=endval)
        wfms.append(w)

    for ch in ['greenpow1', 'greenpow2', 'greenpow3']:
        n = filter(str.isdigit, ch)[0]
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden

        correction = DIMPLE.__dict__['gr' + n + 'correct']

        w.y = physics.cnv(ch, correction * grwfms[ch])
        if DL.lightassist_lock:
            endval = w.y[-1]
            w.linear(DL.lightassist_lockpowGR, DL.lightassist_lockdtUP)
            w.appendhold(DL.lightassist_t0 + DL.lightassistdt)
            if DL.endvalGR >= 0.:
                w.linear(DL.endvalGR, DL.lightassist_lockdtDOWN)
            else:
                w.linear(None, DL.lightassist_lockdtDOWN, volt=endval)
        wfms.append(w)

    for ch in ['lcr1', 'lcr2', 'lcr3']:
        n = filter(str.isdigit, ch)[0]
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        force = DL.__dict__['force_' + ch]
        if force >= 0 and force <= 1:
            print "...Forcing LCR%s = %f during lattice ramp" % (n, force)
            w.y = physics.cnv(ch, numpy.array(alpha.size * [force]))
        elif DL.signal == 0:
            print "...Forcing LCR%s = 0. so that it does NOT rotate to LATTICE" % n
            w.y = physics.cnv(ch, numpy.array(alpha.size * [0.0]))
        else:
            w.y = physics.cnv(ch, alpha)
        wfms.append(w)

    bfieldA = bfieldG / 6.8

    ##ADD field
    bfield = wfm.wave('bfield', 0.0, DL.ss)
    bfield.y = physics.cnv('bfield', bfieldA)
    print "The last value of the bfield voltage is =", bfield.y[-1]
    print
    wfms.append(bfield)

    ##ADD gradient field
    gradient = gradient_wave('gradientfield', 0.0, DL.ss, volt=0.0)
    gradient.follow(bfield)
    wfms.append(gradient)

    buffer = 40.
    s.wait(buffer)

    #~ odtpow = odt.odt_wave('odtpow', cpowend, DL.ss)
    #~ if DIMPLE.odt_t0 > buffer :
    #~ odtpow.appendhold( DIMPLE.odt_t0 - buffer)
    #~ if DIMPLE.odt_pow < 0.:
    #~ odtpow.appendhold( DIMPLE.odt_dt)
    #~ else:
    #~ odtpow.tanhRise( DIMPLE.odt_pow, DIMPLE.odt_dt, DIMPLE.odt_tau, DIMPLE.odt_shift)

    #~ if numpy.absolute(DIMPLE.odt_pow) < 0.0001:
    #~ s.wait( odtpow.dt() )
    #~ s.digichg('odtttl',0)
    #~ s.wait(-odtpow.dt() )

    #~ wfms.append(odtpow)

    # RF sweep
    if DL.rf == 1:
        rfmod = wfm.wave('rfmod', 0., DL.ss)
        rfmod.appendhold(bfield.dt() + DL.rftime)
        rfmod.linear(DL.rfvoltf, DL.rfpulsedt)
        wfms.append(rfmod)

    if DL.round_trip == 1:
        bindex = 0  # Calculate detunings using starting field
    else:
        bindex = -1  # Calculate detunings using field at the end of ramps

    bfieldG = physics.inv('bfield', bfield.y[bindex]) * 6.8
    hfimg0 = -1. * (100.0 + 163.7 - 1.414 * bfieldG)

    # Find bindex for braggkill time
    bindex_BK = math.floor(-DL.braggkilltime / bfield.ss)
    bfieldG_BK = physics.inv('bfield', bfield.y[-1 - bindex_BK]) * 6.8
    hfimg0_BK = -1. * (100.0 + 163.7 - 1.414 * bfieldG_BK)
    DL.braggkill_hfimg = hfimg0_BK - DL.braggkill_hfimg
    print "\n...Braggkill hfimg modification:\n"
    print "\tNEW braggkill_hfimg = %.2f MHz" % DL.braggkill_hfimg

    # Find bindex for bragg2kill time
    bindex_B2K = math.floor(-DL.bragg2killtime / bfield.ss)
    bfieldG_B2K = physics.inv('bfield', bfield.y[-1 - bindex_B2K]) * 6.8
    hfimg0_B2K = -1. * (100.0 + 163.7 - 1.414 * bfieldG_B2K)
    DL.bragg2kill_hfimg1 = hfimg0_B2K - DL.bragg2kill_hfimg1
    DL.bragg2kill_hfimg2 = hfimg0_B2K - DL.bragg2kill_hfimg2
    print "\n...Bragg2kill hfimg modification:\n"
    print "\tNEW brag2gkill_hfimg1 = %.2f MHz" % DL.bragg2kill_hfimg1
    print "\tNEW brag2gkill_hfimg2 = %.2f MHz" % DL.bragg2kill_hfimg2

    print "\n...ANDOR:hfimg and hfimg0 will be modified  in report\n"
    print "\tNEW  ANDOR:hfimg  = %.2f MHz" % (hfimg0 - DL.imgdet)
    print "\tNEW  ANDOR:hfimg0 = %.2f MHz\n" % hfimg0
    gen.save_to_report('ANDOR', 'hfimg', hfimg0 - DL.imgdet)
    gen.save_to_report('ANDOR', 'hfimg0', hfimg0)

    newANDORhfimg = hfimg0 - DL.imgdet

    # THIS DEFINES THE TIME IT TAKES THE OFFSET LOCK TO SWITCH TO
    # A NEW SETPOINT
    hfimgdelay = 50.  #ms

    # Kill hfimg
    if DL.probekill == 1 or DL.braggkill == 1 or DL.bragg2kill == 1 or DL.lightassist or DL.lightassist_lock:
        analogimg = wfm.wave('analogimg', newANDORhfimg, DL.ss)

        if DL.probekill == 1:
            if (-DL.probekilltime + hfimgdelay) < DL.image:
                analogimg.appendhold(bfield.dt() + DL.probekilltime -
                                     hfimgdelay)
                analogimg.linear(DL.probekill_hfimg, 0.0)
                analogimg.appendhold(hfimgdelay + DL.probekilldt + 3 * DL.ss)

        elif DL.braggkill == 1:
            print "Setting up analogimg for braggkill"
            if (-DL.braggkilltime + hfimgdelay) < DL.image:
                analogimg.appendhold(bfield.dt() + DL.braggkilltime -
                                     hfimgdelay)
                analogimg.linear(DL.braggkill_hfimg, 0.0)
                analogimg.appendhold(hfimgdelay + DL.braggkilldt + 3 * DL.ss)

        elif DL.bragg2kill == 1:
            print "Setting up analogimg for bragg2kill"
            if (-DL.bragg2killtime + hfimgdelay) < DL.image:
                # This sets up the detuning for the first pulse
                analogimg.appendhold(bfield.dt() + DL.bragg2killtime -
                                     hfimgdelay)
                analogimg.linear(DL.bragg2kill_hfimg1, 0.0)
                analogimg.appendhold(hfimgdelay + DL.bragg2killdt + 3 * DL.ss)

                # Then set up the detuning for the second pulse
                analogimg.linear(DL.bragg2kill_hfimg2, 0.0)
                analogimg.appendhold(hfimgdelay + DL.bragg2killdt + 3 * DL.ss)

        elif DL.lightassist == 1 or DL.lightassist_lock:
            analogimg.appendhold(bfield.dt() - hfimgdelay)
            analogimg.linear(DL.lightassist_hfimg, 0.0)
            duration = DL.lightassist_lockdtUP + DL.lightassist_t0 + DL.lightassistdt + DL.lightassist_lockdtDOWN
            analogimg.appendhold(hfimgdelay + duration + 3 * DL.ss)

        analogimg.linear(newANDORhfimg, 0.)
        analogimg.extend(10)
        wfms.append(analogimg)

    #analogimg = bfieldwfm.hfimg_wave('analogimg', ANDOR.hfimg, DL.ss)
    #andorhfimg0 = analogimg.follow(bfield, DL.imgdet)
    #wfms.append(analogimg)

    # If we are doing round trip END, then mirror all the ramps
    # before adding them to the sequence
    if DL.round_trip == 1:
        if DL.round_trip_type == 1:
            maxdt = 0.
            maxi = -1
            for i, w in enumerate(wfms):
                if w.dt() > maxdt:
                    maxdt = w.dt()
                    maxi = i

            maxdt = maxdt + DL.wait_at_top / 2.

            for w in wfms:
                w.extend(maxdt)
                if 'lcr' in w.name:
                    yvals = w.y

                    #Get the reverse of the alpha desired array
                    alpha_mirror = numpy.copy(alpha_desired[::-1])

                    #Add the wait at top part so that it has same length as yvals
                    if alpha_mirror.size > yvals.size:
                        print "Error making mirror ramp for LCR."
                        print "Program will exit."
                        exit(1)
                    alpha_mirror = numpy.append(
                        (yvals.size - alpha_mirror.size) * [alpha_mirror[0]],
                        alpha_mirror)

                    #This is how much the mirror ramp will be advanced
                    N_adv = int(math.floor(DL.lcr_mirror_advance / DL.ss))

                    if N_adv < alpha_mirror.size:
                        alpha_mirror = alpha_mirror[N_adv:]
                        alpha_mirror = numpy.append(
                            alpha_mirror, (yvals.size - alpha_mirror.size) *
                            [alpha_mirror[-1]])
                    else:
                        alpha_mirror = numpy.array(yvals.size *
                                                   [alpha_mirror[-1]])

                    w.y = numpy.concatenate(
                        (yvals, physics.cnv(w.name, alpha_mirror)))
                else:
                    w.mirror()
                w.appendhold(DL.wait_at_end)

    N_adv = int(math.floor(alpha_advance / DL.ss))

    alpha_desired = numpy.copy(alpha)

    for wavefm in wfms:
        print "%s dt = %f" % (wavefm.name, wavefm.dt())

    duration = s.analogwfm_add(DL.ss, wfms)

    if DL.image < DIGEXTENSION:
        s.wait(duration)
    else:
        print "...DL.image = %f  >= %.2f  Digital seq extension will be used." % (
            DL.image, DIGEXTENSION)
        s.wait(DL.image)

    ### Prepare the parts of the ramps that are going to be used to mock
    ### the conditions for the noatoms shot
    ### 1. get dt = [noatoms] ms from the end of the lattice ramps.
    if 'manta' in DL.camera:
        noatomsdt = MANTA.noatoms
    else:
        noatomsdt = ANDOR.noatoms
    noatomswfms = []
    for wavefm in wfms:
        cp = copy.deepcopy(wavefm)
        cp.idnum = time.time() * 100
        cp.retain_last(DL.bgRetainDT)
        noatomswfms.append(cp)

    ### Figure out when to turn interlock back on, using alpha information
    #~ if duration > DL.t0 + DL.dt:
    #~ s.wait(-DL.lattice_interlock_time)
    #~ if DL.use_lattice_interlock == 1:
    #~ s.digichg('latticeinterlockbypass',0)
    #~ else:
    #~ s.digichg('latticeinterlockbypass',1)
    #~ s.wait( DL.lattice_interlock_time)

    #########################################
    ## OTHER TTL EVENTS: probekill, braggkill, rf, quick2
    #########################################
    # Braggkill
    if DL.braggkill == 1:
        print "Using Bragg Kill"
        s.wait(DL.braggkilltime)
        s = manta.OpenShutterBragg(s, DL.shutterdelay)
        s.digichg('bragg', 1)
        s.wait(DL.braggkilldt)
        s.digichg('brshutter', 1)  # to close shutter
        s.digichg('bragg', 0)

        s.wait(-DL.braggkilldt)
        s.wait(-DL.braggkilltime)

    if DL.bragg2kill == 1:
        print "Using Bragg 2 Kill"
        tcur = s.tcur
        s.wait(DL.bragg2killtime)
        s = manta.OpenShutterBragg(s, DL.shutterdelay)
        s.digichg('bragg', 1)
        s.wait(DL.bragg2killdt)
        s.digichg('brshutter', 1)  # to close shutter
        s.digichg('bragg', 0)

        s.wait(hfimgdelay + 3 * DL.ss)
        s = manta.OpenShutterBragg(s, DL.shutterdelay)
        s.digichg('bragg', 1)
        s.wait(DL.bragg2killdt)
        s.digichg('brshutter', 1)  # to close shutter
        s.digichg('bragg', 0)

        # Revert to current time after pulses have been added in the past
        s.tcur = tcur

    # Probe Kill
    if DL.probekill == 1:
        s.wait(DL.probekilltime)

        s.wait(-10)
        s.digichg('prshutter', 0)
        s.wait(10)
        s.digichg('probe', 1)
        s.wait(DL.probekilldt)
        s.digichg('probe', 0)

        s.digichg('prshutter', 1)
        s.wait(-DL.probekilltime)

    # Pulse RF
    if DL.rf == 1:
        s.wait(DL.rftime)
        s.digichg('rfttl', 1)
        s.wait(DL.rfpulsedt)
        s.digichg('rfttl', 0)
        s.wait(-DL.rfpulsedt)
        s.wait(-DL.rftime)

    # QUICK2
    if DL.quick2 == 1:
        s.wait(DL.quick2time)
        s.digichg('quick2', 1)
        s.wait(-DL.quick2time)

    # Light-assisted collisions
    if DL.lightassist == 1 or DL.lightassist_lock:
        s.wait(-DL.lightassist_lockdtUP - DL.lightassist_t0 -
               DL.lightassistdt - DL.lightassist_lockdtDOWN - 3 * DL.ss)

        s.wait(DL.lightassist_lockdtUP + DL.lightassist_t0)
        s.wait(-10)
        s.digichg('prshutter', 0)
        s.wait(10)
        s.digichg('probe', DL.lightassist)
        s.wait(DL.lightassistdt)
        s.digichg('probe', 0)

        s.digichg('prshutter', 1)
        s.wait(DL.lightassist_lockdtDOWN)
        s.wait(3 * DL.ss)
        # After the collisions happen we still need to wait some time
        # for the probe frequency to come back to the desired value
        s.wait(hfimgdelay)

    #########################################
    ## GO BACK IN TIME IF DOING ROUND-TRIP START
    #########################################
    if DL.round_trip == 1:
        if DL.round_trip_type == 0:
            s.wait(-DL.image)
            s.stop_analog()

    #########################################
    ## TURN GREEN OFF BEFORE PICTURES
    #########################################
    if DL.greenoff == 1:
        s.wait(DL.greenoff_t0)
        s.digichg('greenttl1', 0)
        s.digichg('greenttl2', 0)
        s.digichg('greenttl3', 0)
        s.wait(-DL.greenoff_t0)

    #########################################
    ## LATTICE LOCK WITH POSSIBILITY OF RF
    #########################################
    bufferdt = 5.0
    lastIR = y_ir[-1]

    lockwfms = []
    if DL.locksmooth == 1 and DL.lock == 0:
        s.wait(bufferdt)
        for ch in ['ir1pow', 'ir2pow', 'ir3pow']:
            n = filter(str.isdigit, ch)[0]
            w = wfm.wave(ch, lastIR,
                         DL.lockss)  #Start value will be overrriden
            w.tanhRise(DL.lock_Er, DL.lock_dtUP, 0.4, 0.2)
            lockwfms.append(w)
        print "...LOCKING LATTICE TO %f Er" % DL.lock_Er
        print "...lastIR = %.4f" % lastIR
        duration = s.analogwfm_add(DL.lockss, lockwfms)
        print "...duration = %.2f" % duration
        s.wait(duration)
        #~ if DL.lockrf:
        #~ s.digichg('rfttl',1)
        #~ s.wait(DL.rfpulsedt)
        #~ s.digichg('rfttl',0)
        #~ s.wait(0.036)
    #else:
    #    s.wait(bufferdt)

    lockwfmscopy = []
    for wavefm in lockwfms:
        cp = copy.deepcopy(wavefm)
        cp.idnum = time.time() * 100 + 1e3 * numpy.random.randint(0, 1e8)
        lockwfmscopy.append(cp)

    #########################################
    ## IMAGING AT LOW FIELD
    #########################################
    if DL.lowfieldimg == 1:
        s.wait(DL.lowfieldimg_t0)
        s.digichg('field', 0)
        s.wait(-DL.lowfieldimg_t0)

    #########################################
    ## TTL RELEASE FROM ODT and LATTICE
    #########################################
    #INDICATE WHICH CHANNELS ARE TO BE CONSIDERED FOR THE BACKGROUND
    bg = [
        'odtttl', 'irttl1', 'irttl2', 'irttl3', 'greenttl1', 'greenttl2',
        'greenttl3'
    ]
    bgdictPRETOF = {}
    for ch in bg:
        bgdictPRETOF[ch] = s.digistatus(ch)
    bgdictPRETOF['tof'] = DL.tof
    print "\nChannel status for pictures: PRE-TOF"
    print bgdictPRETOF
    print

    #RELEASE FROM LATTICE
    if DL.tof <= 0.:
        s.wait(1.0 + ANDOR.exp)
    s.digichg('greenttl1', 0)
    s.digichg('greenttl2', 0)
    s.digichg('greenttl3', 0)
    s.digichg('irttl1', 0)
    s.digichg('irttl2', 0)
    s.digichg('irttl3', 0)
    #RELEASE FROM IR TRAP
    s.digichg('odtttl', 0)
    if DL.tof <= 0.:
        s.wait(-1.0 + ANDOR.exp)

    print "TIME WHEN RELEASED FROM LATTICE = ", s.tcur
    s.wait(DL.tof)

    return s, noatomswfms, lockwfmscopy, bgdictPRETOF
示例#20
0
def do_roundtrip(s, wfms):
    """ creates roundtrips to characterize cooling in lattice.
        wfms are the lattice wfms, they can be mirrored here if a mirror 
        ramp is desired. """

    s.wait(RT.wait_at_top)

    # This case is for mirrored ramps
    if RT.mirror == 1:
        alpha_desired = wfms[1]
        wfms = wfms[0]
        mirrorwfms = []
        for wavefm in wfms:
            cp = copy.deepcopy(wavefm)
            cp.idnum = time.time() * 100
            mirrorwfms.append(cp)

        for w in mirrorwfms:
            if 'lcr' in w.name:
                yvals = w.y

                # Get the reverse of the alpha desired array
                alpha_mirror = numpy.copy(alpha_desired[::-1])
                makeplot([w, alpha_mirror], name=w.name)

                # It needs to have the same length as yvals, so append
                # constant samples at the beginning if needed
                if alpha_mirror.size > yvals.size:
                    print "Error making mirror ramp for LCR."
                    print "Program will exit."
                    exit(1)
                alpha_mirror = numpy.append(
                    (yvals.size - alpha_mirror.size) * [alpha_mirror[0]],
                    alpha_mirror)

                # This is how much the mirror ramp will be advanced
                N_adv = int(math.floor(RT.lcr_mirror_advance / DL.ss))

                if N_adv < alpha_mirror.size:
                    alpha_mirror = alpha_mirror[N_adv:]
                    alpha_mirror = numpy.append(
                        alpha_mirror,
                        (yvals.size - alpha_mirror.size) * [alpha_mirror[-1]])
                else:
                    alpha_mirror = numpy.array(yvals.size * [alpha_mirror[-1]])

                if RT.lcr_snap == 1:
                    alpha_mirror[alpha_mirror > 0.01] = 1.
                w.y = physics.cnv(w.name, alpha_mirror)
            else:
                w.y = w.y[::-1]
            w.appendhold(RT.wait_at_end)
        wfms = mirrorwfms

    # This case is for custom ramps
    else:
        # There need to be 10 waveforms
        # 3IR, 3LCR, 3GR, BFIELD
        # This follows the procedure done for the lattice ramps

        Xendtime = float(RT.image)
        Nnew = int(math.floor(Xendtime / DL.ss))
        Xnew = numpy.arange(Xendtime / Nnew, RT.image, Xendtime / Nnew)
        ir_ramp, xy_ir, ir = lattice.interpolate_ramp(RT.irpow, yoffset=0.)
        y_ir = ir_ramp(Xnew)

        grwfms = {}
        for i, grramp in enumerate([RT.grpow1, RT.grpow2, RT.grpow3]):
            gr_ramp, xy_gr, gr = lattice.interpolate_ramp(grramp, yoffset=0.)
            y_gr = gr_ramp(Xnew)
            grwfms['greenpow' + '%1d' % (i + 1)] = y_gr

        alpha_ramp, xy_alpha, alpha = lattice.interpolate_ramp(RT.alpha,
                                                               yoffset=0.)
        y_alpha = alpha_ramp(Xnew)

        wfms = []
        for ch in ['ir1pow', 'ir2pow', 'ir3pow']:
            n = filter(str.isdigit, ch)[0]
            w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
            w.y = physics.cnv(ch, y_ir)
            wfms.append(w)
        for ch in ['greenpow1', 'greenpow2', 'greenpow3']:
            n = filter(str.isdigit, ch)[0]
            w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden

            correction = DIMPLE.__dict__['gr' + n + 'correct']
            w.y = physics.cnv(ch, correction * grwfms[ch])
            wfms.append(w)

        for ch in ['lcr1', 'lcr2', 'lcr3']:
            n = filter(str.isdigit, ch)[0]
            w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
            #print "Obtaining LCR waveform for ROUNDTRIP..."
            w.y = physics.cnv(ch, y_alpha)
            wfms.append(w)

        ##ADD field
        bfieldG = physics.cnv('as_to_B', DL.knob05)
        bfieldA = bfieldG / 6.8
        bfield = wfm.wave('bfield', bfieldA, DL.ss)
        bfield.extend(wfms[-1].dt())
        wfms.append(bfield)

    # Include ramp to zerocrossing, valid for mirror and custom cases
    # Final field can be given in amps (default) or in a0
    if RT.enable_zc == 1:
        if isinstance(RT.zc_bias, str):
            if 'a0' in RT.zc_bias:
                print "Using scattering length units for final field"
                scattlen = float(re.sub('a0', '', RT.zc_bias))
                bfieldG = physics.cnv('as_to_B', scattlen)
                bfieldA = bfieldG / 6.8
            elif 'knob05' in RT.zc_bias:
                print "Using knob05 as the final scattering length, amounts to no change"
                bfieldG = physics.cnv('as_to_B', DL.knob05)
                bfieldA = bfieldG / 6.8
        else:
            bfieldA = RT.zc_bias
            bfieldG = bfieldA * 6.8
    else:
        print "Using knob05 as the final scattering length, amounts to no change"
        bfieldG = physics.cnv('as_to_B', DL.knob05)
        bfieldA = bfieldG / 6.8

    # Set hfimg value for the given final field
    hfimg0 = -1. * (100.0 + 163.7 - 1.414 * bfieldG)
    print "\n...ANDOR:hfimg and hfimg0 will be modified  in report\n"
    print "\tNEW  ANDOR:hfimg  = %.2f MHz" % (hfimg0 - RT.imgdet)
    print "\tNEW  ANDOR:hfimg0 = %.2f MHz\n" % hfimg0
    gen.save_to_report('ANDOR', 'hfimg', hfimg0 - RT.imgdet)
    gen.save_to_report('ANDOR', 'hfimg0', hfimg0)
    newANDORhfimg = hfimg0 - RT.imgdet

    for w in wfms:
        if 'bfield' in w.name:
            bfieldw = w
            if RT.enable_zc == 1:
                if RT.zc_mirror_t0 < 0.:
                    dt = w.dt()
                    w.chop(w.dt() + RT.zc_mirror_t0)
                if RT.zc_mirror_t0 > 0.:
                    w.appendhold(RT.zc_mirror_t0)
                w.linear(bfieldA, RT.zc_mirror_rampdt)
                w.appendhold(RT.zc_mirror_holddt)

    for i, w in enumerate(wfms):
        if 'gradientfield' in w.name:
            gradientfield_i = i
    print "REMOVING GRADIENT FIELD"
    print len(wfms)
    wfms = [w for w in wfms if w.name != 'gradientfield']
    print len(wfms)
    #Add gradientfield ramp to have levitation all the time
    from bfieldwfm import gradient_wave
    gradient = gradient_wave('gradientfield', 0.0, bfieldw.ss, volt=0.0)
    gradient.follow(bfieldw)
    wfms.append(gradient)

    duration = s.analogwfm_add(DL.ss, wfms)
    s.wait(duration)

    ### Prepare the parts of the ramps that are going to be used to mock
    ### the conditions for the noatoms shot
    ### 1. get dt = [noatoms] ms from the end of the lattice ramps.
    if 'manta' in DL.camera:
        noatomsdt = MANTA.noatoms
    else:
        noatomsdt = ANDOR.noatoms
    noatomswfms = []
    for wavefm in wfms:
        cp = copy.deepcopy(wavefm)
        cp.idnum = time.time() * 100
        cp.retain_last(DL.bgRetainDT)
        noatomswfms.append(cp)

    #INDICATE WHICH CHANNELS ARE TO BE CONSIDERED FOR THE BACKGROUND
    bg = [
        'odtttl', 'irttl1', 'irttl2', 'irttl3', 'greenttl1', 'greenttl2',
        'greenttl3'
    ]
    bgdictPRETOF = {}
    for ch in bg:
        bgdictPRETOF[ch] = s.digistatus(ch)
    bgdictPRETOF['tof'] = RT.tof
    print "\nChannel status for pictures: PRE-TOF"
    print bgdictPRETOF
    print

    #RELEASE FROM LATTICE
    if RT.tof <= 0.:
        s.wait(1.0 + ANDOR.exp)
    s.digichg('greenttl1', 0)
    s.digichg('greenttl2', 0)
    s.digichg('greenttl3', 0)
    s.digichg('irttl1', 0)
    s.digichg('irttl2', 0)
    s.digichg('irttl3', 0)
    #RELEASE FROM IR TRAP
    s.digichg('odtttl', 0)
    if RT.tof <= 0.:
        s.wait(-1.0 + ANDOR.exp)

    print "TIME WHEN RELEASED FROM LATTICE = ", s.tcur
    s.wait(RT.tof)

    return s, noatomswfms, bgdictPRETOF
示例#21
0
def dimple_to_lattice(s,cpowend):
    
    print "----- LATTICE LOADING RAMPS -----"
    
    dt = DL.dt
    tau = DL.tau
    shift = DL.shift
    
    N0 = 0
    
    N = int(math.floor( dt/ DL.ss))
    x = numpy.arange(dt/N, dt, dt/N)
    tau = tau*dt
    shift = dt/2. + shift*dt/2.
    
    # Define how we want to ramp up the lattice depth
    v0_ramp, xy_v0, v0set =  interpolate_ramp( DL.latticeV0)
    
    
    v0  = v0_ramp(x)
    
    #v0 = 0. + DL.latticeV0 * ( (1+numpy.tanh((x-shift)/tau)) - (1+numpy.tanh((-shift)/tau)) )\
    #                    / ( (1+numpy.tanh((dt-shift)/tau)) - (1+numpy.tanh((-shift)/tau)) )
                        
    NH = int(math.floor( DL.dthold/ DL.ss))
    
    v0 = numpy.concatenate(( numpy.zeros(N0), v0, numpy.array(NH*[v0[-1]])  ))
    
    x_v0 = numpy.arange( v0.size )
    x_v0 = x_v0*DL.ss
    
    # Number of samples to keep
    NS = int(math.floor( DL.image / DL.ss))
    if NS > v0.size:
        x_v0 = numpy.append(x_v0, (NS-v0.size)*[x_v0[-1]])
        v0 = numpy.append(v0, (NS-v0.size)*[v0[-1]])
        
    else:
        x_v0 = x_v0[:NS]
        v0 = v0[:NS]
    
    ###########################################
    #### AXIS DEFINITIONS FOR PLOTS ###
    ###########################################    
    
    fig = plt.figure( figsize=(4.5*1.05,8.*1.1))
    ax0 = fig.add_axes( [0.18,0.76,0.76,0.20]) 
    ax2 = fig.add_axes( [0.18,0.645,0.76,0.11])
    ax3 = fig.add_axes( [0.18,0.53,0.76,0.11])
    ax1 = fig.add_axes( [0.18,0.415,0.76,0.11])
    ax5 = fig.add_axes( [0.18,0.30,0.76,0.11])
    ax4 = fig.add_axes( [0.18,0.185,0.76,0.11])
    ax6 = fig.add_axes( [0.18,0.07,0.76,0.11])
    
    lw=1.5
    labelx=-0.12
    legsz =8.
    
    xymew=0.5
    xyms=9

    ax0.plot( x_v0, v0, 'b', lw=2.5, label='Lattice depth')
    ax0.plot(xy_v0[:,0],xy_v0[:,1], 'x', color='blue', ms=5.)
    ax0.plot(v0set[:,0],v0set[:,1], '.', mew=xymew, ms=xyms, color='blue')
    
    
    ###########################################
    #### USER DEFINED RAMPS: IR, GR, and U ###
    ###########################################      
    
    # Define how we want to ramp up the IR power
    ir_ramp, xy_ir, ir =  interpolate_ramp( DL.irpow, yoffset=DIMPLE.ir1pow)
    
    dt_ir = numpy.amax( ir[:,0]) - numpy.amin( ir[:,0])
    N_ir = int(math.floor( dt_ir / DL.ss ))
    x_ir = numpy.arange( dt_ir/N_ir, dt_ir, dt_ir/N_ir)
    
    #y_ir = ir_spline(x_ir) 
    y_ir = ir_ramp(x_ir)
    
    if v0.size > y_ir.size:
        y_ir = numpy.append(y_ir, (v0.size-y_ir.size)*[y_ir[-1]])
    elif v0.size < y_ir.size:
        y_ir = y_ir[0:v0.size]
        
    if v0.size != y_ir.size:
        msg = "IRPOW ERROR: number of samples in IR ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR',msg)
        exit(1)
        
    
    if (v0 > y_ir).any():
        msg = "IRPOW ERROR:  not enough power to get desired lattice depth"
        print msg
        bad = numpy.where( v0 > y_ir)
        msg = msg + "\nFirst bad sample = %d out of %d" % (bad[0][0], v0.size)
        msg = msg + "\n v0 = %f " %   v0[ bad[0][0] ]
        msg = msg + "\n ir = %f " % y_ir[ bad[0][0] ]
        print v0[bad[0][0]]
        print y_ir[bad[0][0]]
        errormsg.box('LATTICE LOADING ERROR',msg)
        exit(1)
    
    ax0.plot(xy_ir[:,0],xy_ir[:,1], 'x', color='darkorange', ms=5.)
    ax0.plot(ir[:,0],ir[:,1], '.', mew=xymew, ms=xyms, color='darkorange')
    ax0.plot(x_v0, y_ir, lw=lw, color='darkorange',label='irpow')
    
    
    # Define how we want to ramp up the GR power
    gr_ramp, xy_gr, gr =  interpolate_ramp( DL.grpow, yoffset=DIMPLE.gr1pow)
    
    dt_gr = numpy.amax( gr[:,0]) - numpy.amin( gr[:,0])
    N_gr = int(math.floor( dt_gr / DL.ss ))
    x_gr = numpy.arange( dt_gr/N_gr, dt_gr, dt_gr/N_gr)
    
    y_gr = gr_ramp(x_gr) 
    
    if v0.size > y_gr.size:
        y_gr = numpy.append(y_gr, (v0.size-y_gr.size)*[y_gr[-1]])
    elif v0.size < y_gr.size:
        y_gr = y_gr[0:v0.size]
        
    if v0.size != y_gr.size:
        msg = "GRPOW ERROR: number of samples in GR ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR',msg)
        exit(1)


    ax0.plot(xy_gr[:,0],xy_gr[:,1], 'x', color='green', ms=5.)
    ax0.plot(gr[:,0],gr[:,1],'.', mew=xymew, ms=xyms, color='green')#, label='grpow dat')
    ax0.plot(x_v0, y_gr, lw=lw, color='green', label='grpow')
    
    ax0.set_xlim(left=-10., right= ax0.get_xlim()[1]*1.1)   
    plt.setp( ax0.get_xticklabels(), visible=False)
    ylim = ax0.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax0.set_ylim( ylim[0]-extra, ylim[1]+extra )
    ax0.grid(True)
    ax0.set_ylabel('$E_{r}$',size=16, labelpad=0)
    ax0.yaxis.set_label_coords(labelx, 0.5)
    ax0.set_title('Lattice Loading')
    ax0.legend(loc='best',numpoints=1,prop={'size':legsz*0.8})
    
    
    # Define how we want to ramp up the scattering length (control our losses)
    a_s_ramp, xy_a_s, a_s =  interpolate_ramp( DL.a_s)
    
    
    dt_a_s = numpy.amax( a_s[:,0]) - numpy.amin( a_s[:,0])
    N_a_s = int(math.floor( dt_a_s / DL.ss ))
    x_a_s = numpy.arange( dt_a_s/N_a_s, dt_a_s, dt_a_s/N_a_s)
    y_a_s = a_s_ramp(x_a_s)
    
    if v0.size > y_a_s.size:
        y_a_s = numpy.append(y_a_s, (v0.size-y_a_s.size)*[y_a_s[-1]])
    elif v0.size < y_a_s.size:
        y_a_s = y_a_s[0:v0.size]
        
    if v0.size != y_a_s.size:
        msg = "a_s ERROR: number of samples in a_s ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR',msg)
        exit(1)
    
    
    
    ax1.plot(xy_a_s[:,0],xy_a_s[:,1]/100., 'x', color='#C10087', ms=5.)
    ax1.plot(a_s[:,0],a_s[:,1]/100., '.', mew=xymew, ms=xyms, color='#C10087')
    ax1.plot(x_v0, y_a_s/100., lw=lw, color='#C10087', label=r'$a_s\mathrm{(100 a_{0})}$')
    ax1.set_ylabel(r'$a_s\mathrm{(100 a_{0})}$',size=16, labelpad=0)
    ax1.yaxis.set_label_coords(labelx, 0.5)

    
    ax1.set_xlim( ax0.get_xlim()) 
    ylim = ax1.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax1.set_ylim( ylim[0]-extra, ylim[1]+extra )
    plt.setp( ax1.get_xticklabels(), visible=False)
    ax1.grid(True)
    ax1.legend(loc='best',numpoints=1,prop={'size':legsz})
    

    #######################################################################
    #### CALCULATED RAMPS:  ALPHA, TUNNELING, SCATTERING LENGTH, BFIELD ###
    #######################################################################
    
    alpha = (v0/y_ir)**2.
    
    alpha_advance = 100.
    N_adv = int(math.floor( alpha_advance / DL.ss))
    
    alpha_desired = numpy.copy(alpha)
    
    if N_adv < v0.size:
        alpha = alpha[N_adv:]
        alpha = numpy.append(alpha, (v0.size-alpha.size)*[alpha[-1]])
    else:
        alpha = numpy.array( v0.size*[alpha[-1]] )
    
    
    
    ax2.plot( x_v0, alpha, lw=lw, color='saddlebrown', label='alpha adv')
    ax2.plot( x_v0, alpha_desired,':', lw=lw, color='saddlebrown', label='alpha')
    
    ax2.set_xlim( ax0.get_xlim()) 
    ax2.set_ylim(-0.05,1.05)
    plt.setp( ax2.get_xticklabels(), visible=False)
    ax2.grid()
    ax2.set_ylabel('$\\alpha$',size=16, labelpad=0)
    ax2.yaxis.set_label_coords(labelx, 0.5)
    
    ax2.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    
    tunneling_Er  = physics.inv('t_to_V0', v0)
    tunneling_kHz = tunneling_Er * 29.2
    
    ax3.plot( x_v0, tunneling_kHz, lw=lw, color='red', label='$t$ (kHz)')
    
    ax3.set_xlim( ax0.get_xlim()) 
    ylim = ax3.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax3.set_ylim( ylim[0]-extra, ylim[1]+extra )
    plt.setp( ax3.get_xticklabels(), visible=False)
    ax3.grid(True)
    ax3.set_ylabel(r'$t\,\mathrm{(kHz)}$',size=16, labelpad=0)
    ax3.yaxis.set_label_coords(labelx, 0.5)
    ax3.legend(loc='best',numpoints=1,prop={'size':legsz})

    
    wannierF = physics.inv('wF_to_V0', v0)
     
    bohrRadius = 5.29e-11 #meters
    lattice_spacing = 1.064e-6 / 2. #meters
    
    bfieldG = physics.cnv('as_to_B', y_a_s)
    
    U_over_t = y_a_s * bohrRadius / lattice_spacing * wannierF / tunneling_Er
    
    
    
    
    ax4.plot( x_v0, U_over_t, lw=lw, color='k', label=r'$U/t$')
    
    ax4.set_xlim( ax0.get_xlim()) 
    ylim = ax4.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax4.set_ylim( ylim[0]-extra, ylim[1]+extra )
    plt.setp( ax4.get_xticklabels(), visible=False)
    ax4.grid(True)
    ax4.set_ylabel(r'$U/t$',size=16, labelpad=0)
    ax4.yaxis.set_label_coords(labelx, 0.5)
    
    ax4.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    
    ax5.plot( x_v0, bfieldG, lw=lw, color='purple', label='$B$ (G)')
    
    ax5.set_xlim( ax0.get_xlim()) 
    ylim = ax5.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax5.set_ylim( ylim[0]-extra, ylim[1]+extra )
    ax5.grid(True)
    plt.setp( ax5.get_xticklabels(), visible=False)
    ax5.set_ylabel(r'$B\,\mathrm{(G)}$',size=16, labelpad=0)
    ax5.yaxis.set_label_coords(labelx, 0.5)
    
    
    ax5.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    
    ax6.plot( x_v0, (tunneling_Er / U_over_t), lw=lw, color='#25D500', label=r'$t^{2}/U\,(E_{r)}$')
    #ax6.set_yscale('log')
    
    ax6.set_xlim( ax0.get_xlim()) 
    ylim = ax6.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax6.set_ylim( ylim[0]*0.5, ylim[1] )
    ax6.grid(True)
    ax6.set_ylabel(r'$t^{2}/U\,(E_{r)}$',size=16, labelpad=0)
    ax6.yaxis.set_label_coords(labelx, 0.5)
    
    
    ax6.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    ax6.set_xlabel('time (ms)')

    figfile = seqconf.seqtxtout().split('.')[0]+'_latticeRamp.png'    
    plt.savefig(figfile , dpi=120 )
    
    #Save all ramps to a txt file for later plotting. 
    datfile = seqconf.seqtxtout().split('.')[0]+'_latticeRamp.dat'
    allRamps = numpy.transpose(numpy.vstack((x_v0, v0, y_ir, y_gr, y_a_s, alpha, alpha_desired, \
                                    tunneling_kHz, U_over_t, bfieldG)))
    header = '# Column index'
    header = header + '\n#\t0\t' + 'time(ms)'
    header = header + '\n#\t1\t' + 'Lattice Depth (Er)'
    header = header + '\n#\t2\t' + 'Ir power (Er)'
    header = header + '\n#\t3\t' + 'GR power (Er)'
    header = header + '\n#\t4\t' + 'a_s (a0)'
    header = header + '\n#\t5\t' + 'alpha - advance'
    header = header + '\n#\t6\t' + 'alpha - desired'
    header = header + '\n#\t7\t' + 'tunneling (kHz)'
    header = header + '\n#\t8\t' + 'U/t'
    header = header + '\n#\t9\t' + 'bfield (Gauss)'
    header = header + '\n'
    
    numpy.savetxt( datfile, allRamps)
    
    with open(datfile, 'w') as f:
        X = numpy.asarray( allRamps )
        f.write(bytes(header))
        
        format = '%.6e'
        ncol = X.shape[1]
        format = [format ,] *ncol
        format = ' '.join(format)
        
        newline = '\n'
        for row in X:
            f.write(numpy.compat.asbytes(format % tuple(row) + newline))

    
    shutil.copyfile( figfile,  seqconf.savedir() + 'expseq' + seqconf.runnumber() + '_latticeRamp.png')
    #plt.savefig( seqconf.savedir() + 'expseq' + seqconf.runnumber() + '_latticeRamp.png', dpi=120)
    
    
    #################################
    #### APPEND RAMPS TO SEQUENCE ###
    #################################
    
    wfms=[]
    
    for ch in ['ir1pow','ir2pow','ir3pow']:
        n = filter( str.isdigit, ch)[0] 
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        w.y = physics.cnv( ch, y_ir )
        if DL.lock:
            endval = w.y[-1]
            w.insertlin_cnv(DL.lock_Er, DL.lock_dtUP, DL.lock_t0 )
            if DL.camera == 'manta' or DL.camera == 'both':
                w.appendhold( MANTA.exp + 1.0 ) 
                w.insertlin( endval, 0., 0.)
                w.appendhold( MANTA.noatoms - MANTA.exp - 1.0)
                w.insertlin_cnv(DL.lock_Er, DL.lock_dtUP, DL.lock_t0 )
        elif DL.lightassist_lock:
            endval = w.y[-1]
            w.linear(DL.lightassist_lockpowIR, DL.lightassist_lockdtUP)
            w.appendhold( DL.lightassist_t0 + DL.lightassistdt )
            if DL.endvalIR >= 0.:
                w.linear(  DL.endvalIR, DL.lightassist_lockdtDOWN)
            else:
                w.linear(  None, DL.lightassist_lockdtDOWN, volt=endval)
        wfms.append(w)
        
    for ch in ['greenpow1','greenpow2','greenpow3']:
        n = filter( str.isdigit, ch)[0] 
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        w.y = physics.cnv( ch, y_gr )
        if DL.lightassist_lock:
            endval = w.y[-1]
            w.linear(DL.lightassist_lockpowGR, DL.lightassist_lockdtUP)
            w.appendhold( DL.lightassist_t0 + DL.lightassistdt )
            if DL.endvalGR >= 0.:
                w.linear(  DL.endvalGR, DL.lightassist_lockdtDOWN)
            else:
                w.linear(  None, DL.lightassist_lockdtDOWN, volt=endval)
        wfms.append(w)
        
    for ch in ['lcr1','lcr2','lcr3']:
        n = filter( str.isdigit, ch)[0] 
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        force = DL.__dict__['force_'+ch]
        if force >= 0 and force <=1:
            print "...Forcing LCR%s = %f during lattice ramp" % (n,force)
            w.y = physics.cnv( ch, numpy.array( alpha.size*[force] )  )
        else:
            w.y = physics.cnv( ch, alpha )
        wfms.append(w)
    

    
    bfieldA = bfieldG/6.8
    
    ##ADD field
    bfield = wfm.wave('bfield', 0.0, DL.ss)
    bfield.y = physics.cnv( 'bfield', bfieldA)
    wfms.append(bfield)
    
    
    ##ADD gradient field
    gradient = gradient_wave('gradientfield', 0.0, DL.ss,volt = 0.0)
    gradient.follow(bfield)
    wfms.append(gradient)
    
    
    buffer = 20.
    s.wait(buffer)
    
    
    #~ odtpow = odt.odt_wave('odtpow', cpowend, DL.ss)
    #~ if DIMPLE.odt_t0 > buffer :
        #~ odtpow.appendhold( DIMPLE.odt_t0 - buffer)
    #~ if DIMPLE.odt_pow < 0.:
        #~ odtpow.appendhold( DIMPLE.odt_dt)
    #~ else:
        #~ odtpow.tanhRise( DIMPLE.odt_pow, DIMPLE.odt_dt, DIMPLE.odt_tau, DIMPLE.odt_shift)    
        
    #~ if numpy.absolute(DIMPLE.odt_pow) < 0.0001:
        #~ s.wait( odtpow.dt() )
        #~ s.digichg('odtttl',0)
        #~ s.wait(-odtpow.dt() )
    
    #~ wfms.append(odtpow)
        
    
    # RF sweep
    if DL.rf == 1:   
        rfmod  = wfm.wave('rfmod', 0., DL.ss)
        rfmod.appendhold( bfield.dt() + DL.rftime )
        rfmod.linear( DL.rfvoltf, DL.rfpulsedt)
        wfms.append(rfmod)


    bfieldG = physics.inv( 'bfield', bfield.y[-1]) * 6.8
    hfimg0 = -1.*(100.0 + 163.7 - 1.414*bfieldG)
    
    print "...ANDOR:hfimg and hfimg0 will be modified  in report\n"
    print "\tNEW  ANDOR:hfimg  = %.2f MHz" % ( hfimg0 - DL.imgdet)
    print "\tNEW  ANDOR:hfimg0 = %.2f MHz\n" %  hfimg0
    gen.save_to_report('ANDOR','hfimg', hfimg0 - DL.imgdet)
    gen.save_to_report('ANDOR','hfimg0', hfimg0)
    
    newANDORhfimg = hfimg0 - DL.imgdet
        
    # Kill hfimg
    if DL.probekill ==1 or DL.braggkill ==1 or DL.lightassist or DL.lightassist_lock:
        hfimgdelay = 50. #ms
        analogimg = wfm.wave('analogimg', newANDORhfimg, DL.ss)
        
        if DL.probekill == 1:
            if (-DL.probekilltime+hfimgdelay) < DL.image:
                analogimg.appendhold( bfield.dt() + DL.probekilltime - hfimgdelay)
                analogimg.linear( DL.probekill_hfimg , 0.0)
                analogimg.appendhold( hfimgdelay + DL.probekilldt + 3*DL.ss)
        
        elif DL.braggkill == 1:
            if (-DL.braggkilltime+hfimgdelay) < DL.image:
                analogimg.appendhold( bfield.dt() + DL.braggkilltime - hfimgdelay)
                analogimg.linear( DL.braggkill_hfimg , 0.0)
                analogimg.appendhold( hfimgdelay + DL.braggkilldt + 3*DL.ss)
        
        elif DL.lightassist == 1 or DL.lightassist_lock:
            analogimg.appendhold( bfield.dt()  - hfimgdelay)
            analogimg.linear( DL.lightassist_hfimg , 0.0)
            duration = DL.lightassist_lockdtUP + DL.lightassist_t0 + DL.lightassistdt + DL.lightassist_lockdtDOWN
            analogimg.appendhold( hfimgdelay + duration + 3*DL.ss)
            
        analogimg.linear( newANDORhfimg, 0.)
        analogimg.extend(10)
        wfms.append(analogimg)
    
        

    
    #analogimg = bfieldwfm.hfimg_wave('analogimg', ANDOR.hfimg, DL.ss)
    #andorhfimg0 = analogimg.follow(bfield, DL.imgdet)
    #wfms.append(analogimg)
    

    # If we are doing round trip END, then mirror all the ramps 
    # before adding them to the sequence
    if DL.round_trip == 1:
        if DL.round_trip_type == 1:
            maxdt = 0.
            maxi = -1
            for i,w in enumerate(wfms):
                if w.dt() > maxdt:
                    maxdt = w.dt()
                    maxi = i
    
            maxdt = maxdt + DL.wait_at_top
    
            for w in wfms:
                w.extend(maxdt)
                if 'lcr' in w.name:
                    yvals = w.y
                    
                    alpha_mirror = numpy.copy(alpha_desired[::-1])
                    
                    N_adv = int(math.floor( DL.lcr_mirror_advance / DL.ss))
                    
                    if N_adv < v0.size:
                        alpha_mirror = alpha_mirror[N_adv:]
                        alpha_mirror = numpy.append(alpha_mirror, (yvals.size-alpha_mirror.size)*[alpha_mirror[-1]])
                    else:
                        alpha_mirror = numpy.array( yvals.size*[alpha_mirror[-1]] )
                    
                    
                    
                    w.y = numpy.concatenate((yvals,physics.cnv( w.name, alpha_mirror )))
                else:
                    w.mirror()
                w.appendhold( DL.wait_at_end)
            
        
    
    N_adv = int(math.floor( alpha_advance / DL.ss))
    
    alpha_desired = numpy.copy(alpha)
    
    
    
    
    duration = s.analogwfm_add(DL.ss,wfms)
    
        
    s.wait( duration )
    
    if DL.lock:
        if DL.camera == 'manta' or DL.camera == 'both':
            s.wait( - MANTA.noatoms)
    
    
    ### Figure out when to turn interlock back on, using alpha information
    #~ if duration > DL.t0 + DL.dt:
        #~ s.wait(-DL.lattice_interlock_time)
        #~ if DL.use_lattice_interlock == 1:
            #~ s.digichg('latticeinterlockbypass',0)
        #~ else:
            #~ s.digichg('latticeinterlockbypass',1)
        #~ s.wait( DL.lattice_interlock_time)
    
    
    return s 
def dimple_to_lattice(s,cpowend):
    
    print "----- LATTICE LOADING RAMPS -----"
    
    dt = DL.dt
    N0 = 0
    
    N = int(math.floor( dt/ DL.ss))
    x = numpy.arange(dt/N, dt, dt/N)

    print "%d samples" % N
    print x.shape
    
    # Define how we want to ramp up the lattice depth
    v0_ramp, xy_v0, v0set =  interpolate_ramp( DL.latticeV0)
    v0  = v0_ramp(x)
                        
    NH = int(math.floor( DL.dthold/ DL.ss))
    
    v0 = numpy.concatenate(( numpy.zeros(N0), v0, numpy.array(NH*[v0[-1]])  ))
    
    x_v0 = numpy.arange( v0.size )
    x_v0 = x_v0*DL.ss
    
    # Number of samples to keep
    NS = int(math.floor( DL.image / DL.ss))
    if NS > v0.size and DL.image < 2500.:
        x_v0 = numpy.append(x_v0, (NS-v0.size)*[x_v0[-1]])
        v0 = numpy.append(v0, (NS-v0.size)*[v0[-1]])
        
    else:
        x_v0 = x_v0[:NS]
        v0 = v0[:NS]
    
    ###########################################
    #### AXIS DEFINITIONS FOR PLOTS ###
    ###########################################    
    
    fig = plt.figure( figsize=(4.5*1.05,8.*1.1))
    ax0 = fig.add_axes( [0.18,0.76,0.76,0.20]) 
    ax2 = fig.add_axes( [0.18,0.645,0.76,0.11])
    ax3 = fig.add_axes( [0.18,0.53,0.76,0.11])
    ax1 = fig.add_axes( [0.18,0.415,0.76,0.11])
    ax5 = fig.add_axes( [0.18,0.30,0.76,0.11])
    ax4 = fig.add_axes( [0.18,0.185,0.76,0.11])
    ax6 = fig.add_axes( [0.18,0.07,0.76,0.11])
    
    lw=1.5
    labelx=-0.12
    legsz =8.
    
    xymew=0.5
    xyms=9

    ax0.plot( x_v0, v0, 'b', lw=2.5, label='Lattice depth')
    ax0.plot(xy_v0[:,0],xy_v0[:,1], 'x', color='blue', ms=5.)
    ax0.plot(v0set[:,0],v0set[:,1], '.', mew=xymew, ms=xyms, color='blue')
    
    
    ###########################################
    #### USER DEFINED RAMPS: IR, GR, and U ###
    ###########################################      
    
    # Define how we want to ramp up the IR power
    if DIMPLE.allirpow > 0.:
      ir_offset = DIMPLE.allirpow
    else:
      ir_offset = DIMPLE.ir1pow2
    ir_ramp, xy_ir, ir =  interpolate_ramp( DL.irpow, yoffset=ir_offset)
    
    dt_ir = numpy.amax( ir[:,0]) - numpy.amin( ir[:,0])
    N_ir = int(math.floor( dt_ir / DL.ss ))
    x_ir = numpy.arange( dt_ir/N_ir, dt_ir, dt_ir/N_ir)
    
    #y_ir = ir_spline(x_ir) 
    y_ir = ir_ramp(x_ir)
    
    if v0.size > y_ir.size:
        y_ir = numpy.append(y_ir, (v0.size-y_ir.size)*[y_ir[-1]])
    elif v0.size < y_ir.size:
        y_ir = y_ir[0:v0.size]
        
    if v0.size != y_ir.size:
        msg = "IRPOW ERROR: number of samples in IR ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR',msg)
        exit(1)
        
   
    alpha_clip_range = 0.1 
    if (v0 > y_ir+ alpha_clip_range).any():
        msg = "IRPOW ERROR:  not enough power to get desired lattice depth"
        print msg
        bad = numpy.where( v0 > y_ir + alpha_clip_range)
        timefail =  int(bad[0][0])*float(DL.ss)
        msg = msg + "\nFirst bad sample = %d out of %d" % (bad[0][0], v0.size)
        msg = msg + "\n  t = %f " %  timefail
        msg = msg + "\n v0 = %f " %   v0[ bad[0][0] ]
        msg = msg + "\n ir = %f " % y_ir[ bad[0][0] ]
        print v0[bad[0][0]]
        print y_ir[bad[0][0]]
        errormsg.box('LATTICE LOADING ERROR',msg)
        exit(1)
    
    ax0.plot(xy_ir[:,0],xy_ir[:,1], 'x', color='darkorange', ms=5.)
    ax0.plot(ir[:,0],ir[:,1], '.', mew=xymew, ms=xyms, color='darkorange')
    ax0.plot(x_v0, y_ir, lw=lw, color='darkorange',label='irpow')
    
    
    # Define how we want to ramp up the GR power
    grwfms = {}
    splmrkr = ['x','+','d']
    ptsmrkr = ['^','s','p']
    for i,grramp in enumerate([(DL.grpow1,DIMPLE.gr1pow2), (DL.grpow2,DIMPLE.gr2pow2), (DL.grpow3,DIMPLE.gr3pow2)]):
      ramppts = grramp[0]
      ramp0 = grramp[1] 

      print 'gr'+'%d'%i +' offset = %f' % ramp0

      gr_ramp, xy_gr, gr =  interpolate_ramp( ramppts, yoffset=ramp0)
    
      dt_gr = numpy.amax( gr[:,0]) - numpy.amin( gr[:,0])
      N_gr = int(math.floor( dt_gr / DL.ss ))
      x_gr = numpy.arange( dt_gr/N_gr, dt_gr, dt_gr/N_gr)
    
      y_gr = gr_ramp(x_gr)
    
      if v0.size > y_gr.size:
          y_gr = numpy.append(y_gr, (v0.size-y_gr.size)*[y_gr[-1]])
      elif v0.size < y_gr.size:
          y_gr = y_gr[0:v0.size]
        
      if v0.size != y_gr.size:
          msg = "GRPOW ERROR: number of samples in GR ramp and V0 ramp does not match!"
          errormsg.box('LATTICE LOADING ERROR',msg)
          exit(1)

      grwfms[ 'greenpow' + '%1d' % (i+1) ] = y_gr 

      ax0.plot(xy_gr[:,0],xy_gr[:,1], marker= splmrkr[i] ,mec='green', mfc='None', ms=3.)
      ax0.plot(gr[:,0],gr[:,1], marker=ptsmrkr[i], mew=xymew, ms=xyms/2., mfc='None', mec='green')#, label='grpow dat')
      ax0.plot(x_v0, y_gr, lw=lw, color='green', label='grpow')

    for grch in grwfms.keys():
      print grch, " = ", grwfms[grch].shape
    
    ax0.set_xlim(left=-10., right= ax0.get_xlim()[1]*1.1)   
    plt.setp( ax0.get_xticklabels(), visible=False)
    ylim = ax0.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax0.set_ylim( ylim[0]-extra, ylim[1]+extra )
    ax0.grid(True)
    ax0.set_ylabel('$E_{r}$',size=16, labelpad=0)
    ax0.yaxis.set_label_coords(labelx, 0.5)
    ax0.set_title('Lattice Loading')
    ax0.legend(loc='best',numpoints=1,prop={'size':legsz*0.8})
    
    
    # Define how we want to ramp up the scattering length (control our losses)
    a_s_ramp, xy_a_s, a_s =  interpolate_ramp( DL.a_s)
    
    
    dt_a_s = numpy.amax( a_s[:,0]) - numpy.amin( a_s[:,0])
    N_a_s = int(math.floor( dt_a_s / DL.ss ))
    x_a_s = numpy.arange( dt_a_s/N_a_s, dt_a_s, dt_a_s/N_a_s)
    y_a_s = a_s_ramp(x_a_s)
    
    if v0.size > y_a_s.size:
        y_a_s = numpy.append(y_a_s, (v0.size-y_a_s.size)*[y_a_s[-1]])
    elif v0.size < y_a_s.size:
        y_a_s = y_a_s[0:v0.size]
        
    if v0.size != y_a_s.size:
        msg = "a_s ERROR: number of samples in a_s ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR',msg)
        exit(1)
    
    
    
    ax1.plot(xy_a_s[:,0],xy_a_s[:,1]/100., 'x', color='#C10087', ms=5.)
    ax1.plot(a_s[:,0],a_s[:,1]/100., '.', mew=xymew, ms=xyms, color='#C10087')
    ax1.plot(x_v0, y_a_s/100., lw=lw, color='#C10087', label=r'$a_s\mathrm{(100 a_{0})}$')
    ax1.set_ylabel(r'$a_s\mathrm{(100 a_{0})}$',size=16, labelpad=0)
    ax1.yaxis.set_label_coords(labelx, 0.5)

    
    ax1.set_xlim( ax0.get_xlim()) 
    ylim = ax1.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax1.set_ylim( ylim[0]-extra, ylim[1]+extra )
    plt.setp( ax1.get_xticklabels(), visible=False)
    ax1.grid(True)
    ax1.legend(loc='best',numpoints=1,prop={'size':legsz})
    

    #######################################################################
    #### CALCULATED RAMPS:  ALPHA, TUNNELING, SCATTERING LENGTH, BFIELD ###
    #######################################################################
    
    alpha = (v0/y_ir)**2.
    
    alpha_advance = 100.
    N_adv = int(math.floor( alpha_advance / DL.ss))
    
    alpha  = alpha.clip(0.,1.)
    alpha_desired = numpy.copy(alpha)
    
    if N_adv < v0.size:
        alpha = alpha[N_adv:]
        alpha = numpy.append(alpha, (v0.size-alpha.size)*[alpha[-1]])
    else:
        alpha = numpy.array( v0.size*[alpha[-1]] )
    
    #alpha = alpha.clip(0., 1.)
    
    ax2.plot( x_v0, alpha, lw=lw, color='saddlebrown', label='alpha adv')
    ax2.plot( x_v0, alpha_desired,':', lw=lw, color='saddlebrown', label='alpha')
    
    ax2.set_xlim( ax0.get_xlim()) 
    ax2.set_ylim(-0.05,1.05)
    plt.setp( ax2.get_xticklabels(), visible=False)
    ax2.grid()
    ax2.set_ylabel('$\\alpha$',size=16, labelpad=0)
    ax2.yaxis.set_label_coords(labelx, 0.5)
    
    ax2.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    
    tunneling_Er  = physics.inv('t_to_V0', v0)
    tunneling_kHz = tunneling_Er * 29.2
    
    ax3.plot( x_v0, tunneling_kHz, lw=lw, color='red', label='$t$ (kHz)')
    
    ax3.set_xlim( ax0.get_xlim()) 
    ylim = ax3.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax3.set_ylim( ylim[0]-extra, ylim[1]+extra )
    plt.setp( ax3.get_xticklabels(), visible=False)
    ax3.grid(True)
    ax3.set_ylabel(r'$t\,\mathrm{(kHz)}$',size=16, labelpad=0)
    ax3.yaxis.set_label_coords(labelx, 0.5)
    ax3.legend(loc='best',numpoints=1,prop={'size':legsz})

    
    wannierF = physics.inv('wF_to_V0', v0)
     
    bohrRadius = 5.29e-11 #meters
    lattice_spacing = 1.064e-6 / 2. #meters
    
    bfieldG = physics.cnv('as_to_B', y_a_s)
    
    U_over_t = y_a_s * bohrRadius / lattice_spacing * wannierF / tunneling_Er
    
    
    
    
    ax4.plot( x_v0, U_over_t, lw=lw, color='k', label=r'$U/t$')
    
    ax4.set_xlim( ax0.get_xlim()) 
    ylim = ax4.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax4.set_ylim( ylim[0]-extra, ylim[1]+extra )
    plt.setp( ax4.get_xticklabels(), visible=False)
    ax4.grid(True)
    ax4.set_ylabel(r'$U/t$',size=16, labelpad=0)
    ax4.yaxis.set_label_coords(labelx, 0.5)
    
    ax4.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    
    ax5.plot( x_v0, bfieldG, lw=lw, color='purple', label='$B$ (G)')
    
    ax5.set_xlim( ax0.get_xlim()) 
    ylim = ax5.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax5.set_ylim( ylim[0]-extra, ylim[1]+extra )
    ax5.grid(True)
    plt.setp( ax5.get_xticklabels(), visible=False)
    ax5.set_ylabel(r'$B\,\mathrm{(G)}$',size=16, labelpad=0)
    ax5.yaxis.set_label_coords(labelx, 0.5)
    
    
    ax5.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    
    ax6.plot( x_v0, (tunneling_Er / U_over_t), lw=lw, color='#25D500', label=r'$t^{2}/U\,(E_{r)}$')
    #ax6.set_yscale('log')
    
    ax6.set_xlim( ax0.get_xlim()) 
    ylim = ax6.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax6.set_ylim( ylim[0]*0.5, ylim[1] )
    ax6.grid(True)
    ax6.set_ylabel(r'$t^{2}/U\,(E_{r)}$',size=16, labelpad=0)
    ax6.yaxis.set_label_coords(labelx, 0.5)
    
    
    ax6.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    ax6.set_xlabel('time (ms)')

    figfile = seqconf.seqtxtout().split('.')[0]+'_latticeRamp.png'    
    plt.savefig(figfile , dpi=120 )
    
    #Save all ramps to a txt file for later plotting. 
    datfile = seqconf.seqtxtout().split('.')[0]+'_latticeRamp.dat'
    allRamps = numpy.transpose(numpy.vstack((x_v0, v0, y_ir, grwfms['greenpow1'], y_a_s, alpha, alpha_desired, \
                                    tunneling_kHz, U_over_t, bfieldG)))
    header = '# Column index'
    header = header + '\n#\t0\t' + 'time(ms)'
    header = header + '\n#\t1\t' + 'Lattice Depth (Er)'
    header = header + '\n#\t2\t' + 'Ir power (Er)'
    header = header + '\n#\t3\t' + 'GR power (Er)'
    header = header + '\n#\t4\t' + 'a_s (a0)'
    header = header + '\n#\t5\t' + 'alpha - advance'
    header = header + '\n#\t6\t' + 'alpha - desired'
    header = header + '\n#\t7\t' + 'tunneling (kHz)'
    header = header + '\n#\t8\t' + 'U/t'
    header = header + '\n#\t9\t' + 'bfield (Gauss)'
    header = header + '\n'
    
    numpy.savetxt( datfile, allRamps)
    
    with open(datfile, 'w') as f:
        X = numpy.asarray( allRamps )
        f.write(bytes(header))
        
        format = '%.6e'
        ncol = X.shape[1]
        format = [format ,] *ncol
        format = ' '.join(format)
        
        newline = '\n'
        for row in X:
            f.write(numpy.compat.asbytes(format % tuple(row) + newline))

    
    shutil.copyfile( figfile,  seqconf.savedir() + 'expseq' + seqconf.runnumber() + '_latticeRamp.png')
    shutil.copyfile( datfile,  seqconf.savedir() + 'expseq' + seqconf.runnumber() + '_latticeRamp.dat')
    #plt.savefig( seqconf.savedir() + 'expseq' + seqconf.runnumber() + '_latticeRamp.png', dpi=120)
    
    
    #################################
    #### APPEND RAMPS TO SEQUENCE ###
    #################################
    
    wfms=[]
    
    for ch in ['ir1pow','ir2pow','ir3pow']:
        n = filter( str.isdigit, ch)[0] 
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        w.y = physics.cnv( ch, y_ir )
        wfms.append(w)
        
    for ch in ['greenpow1','greenpow2','greenpow3']:
        n = filter( str.isdigit, ch)[0] 
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        
        correction = DIMPLE.__dict__['gr'+n+'correct']
        
        w.y = physics.cnv( ch, correction * grwfms[ch] )
        wfms.append(w)

    for ch in ['lcr1','lcr2','lcr3']:
        n = filter( str.isdigit, ch)[0] 
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        force = DL.__dict__['force_'+ch]
        if force >= 0 and force <=1:
            print "...Forcing LCR%s = %f during lattice ramp" % (n,force)
            w.y = physics.cnv( ch, numpy.array( alpha.size*[force] )  )
        else:
            w.y = physics.cnv( ch, alpha )
        wfms.append(w)
    
    bfieldA = bfieldG/6.8
    
    ##ADD field
    bfield = wfm.wave('bfield', 0.0, DL.ss)
    bfield.y = physics.cnv( 'bfield', bfieldA)
    wfms.append(bfield)
    
    ##ADD gradient field
    gradient = gradient_wave('gradientfield', 0.0, DL.ss,volt = 0.0)
    gradient.follow(bfield)
    wfms.append(gradient)
     
    
    # RF sweep
    if DL.rf == 1:   
        rfmod  = wfm.wave('rfmod', 0., DL.ss)
        rfmod.appendhold( bfield.dt() + DL.rftime )
        rfmod.linear( DL.rfvoltf, DL.rfpulsedt)
        wfms.append(rfmod)

    
    #Take care of imaging frequencies, including various kill experiments
    bfieldG = physics.inv( 'bfield', bfield.y[-1]) * 6.8
    hfimg0 = -1.*(100.0 + 163.7 - 1.414*bfieldG)
    
    print "...ANDOR:hfimg and hfimg0 will be modified  in report\n"
    print "\tNEW  ANDOR:hfimg  = %.2f MHz" % ( hfimg0 - DL.imgdet)
    print "\tNEW  ANDOR:hfimg0 = %.2f MHz\n" %  hfimg0
    gen.save_to_report('ANDOR','hfimg', hfimg0 - DL.imgdet)
    gen.save_to_report('ANDOR','hfimg0', hfimg0)
    newANDORhfimg = hfimg0 - DL.imgdet
        
    # Kill hfimg
    if DL.probekill ==1 or DL.braggkill ==1 or DL.lightassist or DL.lightassist_lock:
        hfimgdelay = 50. #ms
        analogimg = wfm.wave('analogimg', newANDORhfimg, DL.ss)
        
        if DL.probekill == 1:
            if (-DL.probekilltime+hfimgdelay) < DL.image:
                analogimg.appendhold( bfield.dt() + DL.probekilltime - hfimgdelay)
                analogimg.linear( DL.probekill_hfimg , 0.0)
                analogimg.appendhold( hfimgdelay + DL.probekilldt + 3*DL.ss)
        
        elif DL.braggkill == 1:
            if (-DL.braggkilltime+hfimgdelay) < DL.image:
                analogimg.appendhold( bfield.dt() + DL.braggkilltime - hfimgdelay)
                analogimg.linear( DL.braggkill_hfimg , 0.0)
                analogimg.appendhold( hfimgdelay + DL.braggkilldt + 3*DL.ss)
        
        #elif DL.lightassist == 1 or DL.lightassist_lock:
        #    analogimg.appendhold( bfield.dt()  - hfimgdelay)
        #    analogimg.linear( DL.lightassist_hfimg , 0.0)
        #    duration = DL.lightassist_lockdtUP + DL.lightassist_t0 + DL.lightassistdt + DL.lightassist_lockdtDOWN
        #    analogimg.appendhold( hfimgdelay + duration + 3*DL.ss)
            
        analogimg.linear( newANDORhfimg, 0.)
        analogimg.extend(10)
        wfms.append(analogimg)
    
        

    
    #analogimg = bfieldwfm.hfimg_wave('analogimg', ANDOR.hfimg, DL.ss)
    #andorhfimg0 = analogimg.follow(bfield, DL.imgdet)
    #wfms.append(analogimg)
    

    # If we are doing round trip END, then mirror all the ramps 
    # before adding them to the sequence
    if DL.round_trip == 1:
        if DL.round_trip_type == 1:
            maxdt = 0.
            maxi = -1
            for i,w in enumerate(wfms):
                if w.dt() > maxdt:
                    maxdt = w.dt()
                    maxi = i
    
            maxdt = maxdt + DL.wait_at_top / 2. 
    
            for w in wfms:
                w.extend(maxdt)
                if 'lcr' in w.name:
                    yvals = w.y
                    
                    #Get the reverse of the alpha desired array
                    alpha_mirror = numpy.copy(alpha_desired[::-1])
                    
                    #Add the wait at top part so that it has same length as yvals
                    if alpha_mirror.size > yvals.size:
                        print "Error making mirror ramp for LCR."
                        print "Program will exit."
                        exit(1)
                    alpha_mirror = numpy.append( (yvals.size - alpha_mirror.size)*[ alpha_mirror[0] ], alpha_mirror )
                    
                                   
                    #This is how much the mirror ramp will be advanced
                    N_adv = int(math.floor( DL.lcr_mirror_advance / DL.ss))
                    
                    if N_adv < alpha_mirror.size:
                        alpha_mirror = alpha_mirror[N_adv:]
                        alpha_mirror = numpy.append(alpha_mirror, (yvals.size-alpha_mirror.size)*[alpha_mirror[-1]])
                    else:
                        alpha_mirror = numpy.array( yvals.size*[alpha_mirror[-1]] )
                    
                    
                    
                    w.y = numpy.concatenate((yvals,physics.cnv( w.name, alpha_mirror )))
                else:
                    w.mirror()
                w.appendhold( DL.wait_at_end)
            
        
    
    N_adv = int(math.floor( alpha_advance / DL.ss))
    
    alpha_desired = numpy.copy(alpha)
    
    
    for wavefm in wfms:
        print "%s dt = %f" % (wavefm.name, wavefm.dt())
   
     
    #Wait the buffer for the lattice loading ramps before adding them
    bufferdt = 20.
    s.wait(bufferdt)
    #Add lattice wfms
    duration = s.analogwfm_add(DL.ss,wfms)
    
    
    if DL.image < 2500.:
        s.wait(duration)
    else:
        print "...DL.image = %f  >= 2500.  Digital seq extension will be used." % DL.image
        s.wait( DL.image )

    #Here give some buffer time to do lock, RF etc. 
    #It has to be minimum 5.0 ms 
    
    
    ### Figure out when to turn interlock back on, using alpha information
    #~ if duration > DL.t0 + DL.dt:
        #~ s.wait(-DL.lattice_interlock_time)
        #~ if DL.use_lattice_interlock == 1:
            #~ s.digichg('latticeinterlockbypass',0)
        #~ else:
            #~ s.digichg('latticeinterlockbypass',1)
        #~ s.wait( DL.lattice_interlock_time)
    
    
    return s, y_ir[-1]  
示例#23
0
def dimple_to_lattice(s, cpowend):

    print "----- LATTICE LOADING RAMPS -----"

    dt = DL.dt
    N0 = 0

    N = int(math.floor(dt / DL.ss))
    x = numpy.arange(dt / N, dt, dt / N)

    print "%d samples" % N
    print x.shape

    # Define how we want to ramp up the lattice depth
    v0_ramp, xy_v0, v0set = interpolate_ramp(DL.latticeV0)
    v0 = v0_ramp(x)

    NH = int(math.floor(DL.dthold / DL.ss))

    v0 = numpy.concatenate((numpy.zeros(N0), v0, numpy.array(NH * [v0[-1]])))

    x_v0 = numpy.arange(v0.size)
    x_v0 = x_v0 * DL.ss

    # Number of samples to keep
    NS = int(math.floor(DL.image / DL.ss))
    if NS > v0.size and DL.image < 2500.:
        x_v0 = numpy.append(x_v0, (NS - v0.size) * [x_v0[-1]])
        v0 = numpy.append(v0, (NS - v0.size) * [v0[-1]])

    else:
        x_v0 = x_v0[:NS]
        v0 = v0[:NS]

    ###########################################
    #### AXIS DEFINITIONS FOR PLOTS ###
    ###########################################

    fig = plt.figure(figsize=(4.5 * 1.05, 8. * 1.1))
    ax0 = fig.add_axes([0.18, 0.76, 0.76, 0.20])
    ax2 = fig.add_axes([0.18, 0.645, 0.76, 0.11])
    ax3 = fig.add_axes([0.18, 0.53, 0.76, 0.11])
    ax1 = fig.add_axes([0.18, 0.415, 0.76, 0.11])
    ax5 = fig.add_axes([0.18, 0.30, 0.76, 0.11])
    ax4 = fig.add_axes([0.18, 0.185, 0.76, 0.11])
    ax6 = fig.add_axes([0.18, 0.07, 0.76, 0.11])

    lw = 1.5
    labelx = -0.12
    legsz = 8.

    xymew = 0.5
    xyms = 9

    ax0.plot(x_v0, v0, 'b', lw=2.5, label='Lattice depth')
    ax0.plot(xy_v0[:, 0], xy_v0[:, 1], 'x', color='blue', ms=5.)
    ax0.plot(v0set[:, 0], v0set[:, 1], '.', mew=xymew, ms=xyms, color='blue')

    ###########################################
    #### USER DEFINED RAMPS: IR, GR, and U ###
    ###########################################

    # Define how we want to ramp up the IR power
    if DIMPLE.allirpow > 0.:
        ir_offset = DIMPLE.allirpow
    else:
        ir_offset = DIMPLE.ir1pow2
    ir_ramp, xy_ir, ir = interpolate_ramp(DL.irpow, yoffset=ir_offset)

    dt_ir = numpy.amax(ir[:, 0]) - numpy.amin(ir[:, 0])
    N_ir = int(math.floor(dt_ir / DL.ss))
    x_ir = numpy.arange(dt_ir / N_ir, dt_ir, dt_ir / N_ir)

    #y_ir = ir_spline(x_ir)
    y_ir = ir_ramp(x_ir)

    if v0.size > y_ir.size:
        y_ir = numpy.append(y_ir, (v0.size - y_ir.size) * [y_ir[-1]])
    elif v0.size < y_ir.size:
        y_ir = y_ir[0:v0.size]

    if v0.size != y_ir.size:
        msg = "IRPOW ERROR: number of samples in IR ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR', msg)
        exit(1)

    alpha_clip_range = 0.1
    if (v0 > y_ir + alpha_clip_range).any():
        msg = "IRPOW ERROR:  not enough power to get desired lattice depth"
        print msg
        bad = numpy.where(v0 > y_ir + alpha_clip_range)
        timefail = int(bad[0][0]) * float(DL.ss)
        msg = msg + "\nFirst bad sample = %d out of %d" % (bad[0][0], v0.size)
        msg = msg + "\n  t = %f " % timefail
        msg = msg + "\n v0 = %f " % v0[bad[0][0]]
        msg = msg + "\n ir = %f " % y_ir[bad[0][0]]
        print v0[bad[0][0]]
        print y_ir[bad[0][0]]
        errormsg.box('LATTICE LOADING ERROR', msg)
        exit(1)

    ax0.plot(xy_ir[:, 0], xy_ir[:, 1], 'x', color='darkorange', ms=5.)
    ax0.plot(ir[:, 0], ir[:, 1], '.', mew=xymew, ms=xyms, color='darkorange')
    ax0.plot(x_v0, y_ir, lw=lw, color='darkorange', label='irpow')

    # Define how we want to ramp up the GR power
    grwfms = {}
    splmrkr = ['x', '+', 'd']
    ptsmrkr = ['^', 's', 'p']
    for i, grramp in enumerate([(DL.grpow1, DIMPLE.gr1pow2),
                                (DL.grpow2, DIMPLE.gr2pow2),
                                (DL.grpow3, DIMPLE.gr3pow2)]):
        ramppts = grramp[0]
        ramp0 = grramp[1]

        print 'gr' + '%d' % i + ' offset = %f' % ramp0

        gr_ramp, xy_gr, gr = interpolate_ramp(ramppts, yoffset=ramp0)

        dt_gr = numpy.amax(gr[:, 0]) - numpy.amin(gr[:, 0])
        N_gr = int(math.floor(dt_gr / DL.ss))
        x_gr = numpy.arange(dt_gr / N_gr, dt_gr, dt_gr / N_gr)

        y_gr = gr_ramp(x_gr)

        if v0.size > y_gr.size:
            y_gr = numpy.append(y_gr, (v0.size - y_gr.size) * [y_gr[-1]])
        elif v0.size < y_gr.size:
            y_gr = y_gr[0:v0.size]

        if v0.size != y_gr.size:
            msg = "GRPOW ERROR: number of samples in GR ramp and V0 ramp does not match!"
            errormsg.box('LATTICE LOADING ERROR', msg)
            exit(1)

        grwfms['greenpow' + '%1d' % (i + 1)] = y_gr

        ax0.plot(xy_gr[:, 0],
                 xy_gr[:, 1],
                 marker=splmrkr[i],
                 mec='green',
                 mfc='None',
                 ms=3.)
        ax0.plot(gr[:, 0],
                 gr[:, 1],
                 marker=ptsmrkr[i],
                 mew=xymew,
                 ms=xyms / 2.,
                 mfc='None',
                 mec='green')  #, label='grpow dat')
        ax0.plot(x_v0, y_gr, lw=lw, color='green', label='grpow')

    for grch in grwfms.keys():
        print grch, " = ", grwfms[grch].shape

    ax0.set_xlim(left=-10., right=ax0.get_xlim()[1] * 1.1)
    plt.setp(ax0.get_xticklabels(), visible=False)
    ylim = ax0.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax0.set_ylim(ylim[0] - extra, ylim[1] + extra)
    ax0.grid(True)
    ax0.set_ylabel('$E_{r}$', size=16, labelpad=0)
    ax0.yaxis.set_label_coords(labelx, 0.5)
    ax0.set_title('Lattice Loading')
    ax0.legend(loc='best', numpoints=1, prop={'size': legsz * 0.8})

    # Define how we want to ramp up the scattering length (control our losses)
    a_s_ramp, xy_a_s, a_s = interpolate_ramp(DL.a_s)

    dt_a_s = numpy.amax(a_s[:, 0]) - numpy.amin(a_s[:, 0])
    N_a_s = int(math.floor(dt_a_s / DL.ss))
    x_a_s = numpy.arange(dt_a_s / N_a_s, dt_a_s, dt_a_s / N_a_s)
    y_a_s = a_s_ramp(x_a_s)

    if v0.size > y_a_s.size:
        y_a_s = numpy.append(y_a_s, (v0.size - y_a_s.size) * [y_a_s[-1]])
    elif v0.size < y_a_s.size:
        y_a_s = y_a_s[0:v0.size]

    if v0.size != y_a_s.size:
        msg = "a_s ERROR: number of samples in a_s ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR', msg)
        exit(1)

    ax1.plot(xy_a_s[:, 0], xy_a_s[:, 1] / 100., 'x', color='#C10087', ms=5.)
    ax1.plot(a_s[:, 0],
             a_s[:, 1] / 100.,
             '.',
             mew=xymew,
             ms=xyms,
             color='#C10087')
    ax1.plot(x_v0,
             y_a_s / 100.,
             lw=lw,
             color='#C10087',
             label=r'$a_s\mathrm{(100 a_{0})}$')
    ax1.set_ylabel(r'$a_s\mathrm{(100 a_{0})}$', size=16, labelpad=0)
    ax1.yaxis.set_label_coords(labelx, 0.5)

    ax1.set_xlim(ax0.get_xlim())
    ylim = ax1.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax1.set_ylim(ylim[0] - extra, ylim[1] + extra)
    plt.setp(ax1.get_xticklabels(), visible=False)
    ax1.grid(True)
    ax1.legend(loc='best', numpoints=1, prop={'size': legsz})

    #######################################################################
    #### CALCULATED RAMPS:  ALPHA, TUNNELING, SCATTERING LENGTH, BFIELD ###
    #######################################################################

    alpha = (v0 / y_ir)**2.

    alpha_advance = 100.
    N_adv = int(math.floor(alpha_advance / DL.ss))

    alpha = alpha.clip(0., 1.)
    alpha_desired = numpy.copy(alpha)

    if N_adv < v0.size:
        alpha = alpha[N_adv:]
        alpha = numpy.append(alpha, (v0.size - alpha.size) * [alpha[-1]])
    else:
        alpha = numpy.array(v0.size * [alpha[-1]])

    #alpha = alpha.clip(0., 1.)

    ax2.plot(x_v0, alpha, lw=lw, color='saddlebrown', label='alpha adv')
    ax2.plot(x_v0,
             alpha_desired,
             ':',
             lw=lw,
             color='saddlebrown',
             label='alpha')

    ax2.set_xlim(ax0.get_xlim())
    ax2.set_ylim(-0.05, 1.05)
    plt.setp(ax2.get_xticklabels(), visible=False)
    ax2.grid()
    ax2.set_ylabel('$\\alpha$', size=16, labelpad=0)
    ax2.yaxis.set_label_coords(labelx, 0.5)

    ax2.legend(loc='best', numpoints=1, prop={'size': legsz})

    tunneling_Er = physics.inv('t_to_V0', v0)
    tunneling_kHz = tunneling_Er * 29.2

    ax3.plot(x_v0, tunneling_kHz, lw=lw, color='red', label='$t$ (kHz)')

    ax3.set_xlim(ax0.get_xlim())
    ylim = ax3.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax3.set_ylim(ylim[0] - extra, ylim[1] + extra)
    plt.setp(ax3.get_xticklabels(), visible=False)
    ax3.grid(True)
    ax3.set_ylabel(r'$t\,\mathrm{(kHz)}$', size=16, labelpad=0)
    ax3.yaxis.set_label_coords(labelx, 0.5)
    ax3.legend(loc='best', numpoints=1, prop={'size': legsz})

    wannierF = physics.inv('wF_to_V0', v0)

    bohrRadius = 5.29e-11  #meters
    lattice_spacing = 1.064e-6 / 2.  #meters

    bfieldG = physics.cnv('as_to_B', y_a_s)

    U_over_t = y_a_s * bohrRadius / lattice_spacing * wannierF / tunneling_Er

    ax4.plot(x_v0, U_over_t, lw=lw, color='k', label=r'$U/t$')

    ax4.set_xlim(ax0.get_xlim())
    ylim = ax4.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax4.set_ylim(ylim[0] - extra, ylim[1] + extra)
    plt.setp(ax4.get_xticklabels(), visible=False)
    ax4.grid(True)
    ax4.set_ylabel(r'$U/t$', size=16, labelpad=0)
    ax4.yaxis.set_label_coords(labelx, 0.5)

    ax4.legend(loc='best', numpoints=1, prop={'size': legsz})

    ax5.plot(x_v0, bfieldG, lw=lw, color='purple', label='$B$ (G)')

    ax5.set_xlim(ax0.get_xlim())
    ylim = ax5.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax5.set_ylim(ylim[0] - extra, ylim[1] + extra)
    ax5.grid(True)
    plt.setp(ax5.get_xticklabels(), visible=False)
    ax5.set_ylabel(r'$B\,\mathrm{(G)}$', size=16, labelpad=0)
    ax5.yaxis.set_label_coords(labelx, 0.5)

    ax5.legend(loc='best', numpoints=1, prop={'size': legsz})

    ax6.plot(x_v0, (tunneling_Er / U_over_t),
             lw=lw,
             color='#25D500',
             label=r'$t^{2}/U\,(E_{r)}$')
    #ax6.set_yscale('log')

    ax6.set_xlim(ax0.get_xlim())
    ylim = ax6.get_ylim()
    extra = (ylim[1] - ylim[0]) * 0.1
    ax6.set_ylim(ylim[0] * 0.5, ylim[1])
    ax6.grid(True)
    ax6.set_ylabel(r'$t^{2}/U\,(E_{r)}$', size=16, labelpad=0)
    ax6.yaxis.set_label_coords(labelx, 0.5)

    ax6.legend(loc='best', numpoints=1, prop={'size': legsz})

    ax6.set_xlabel('time (ms)')

    figfile = seqconf.seqtxtout().split('.')[0] + '_latticeRamp.png'
    plt.savefig(figfile, dpi=120)

    #Save all ramps to a txt file for later plotting.
    datfile = seqconf.seqtxtout().split('.')[0] + '_latticeRamp.dat'
    allRamps = numpy.transpose(numpy.vstack((x_v0, v0, y_ir, grwfms['greenpow1'], y_a_s, alpha, alpha_desired, \
                                    tunneling_kHz, U_over_t, bfieldG)))
    header = '# Column index'
    header = header + '\n#\t0\t' + 'time(ms)'
    header = header + '\n#\t1\t' + 'Lattice Depth (Er)'
    header = header + '\n#\t2\t' + 'Ir power (Er)'
    header = header + '\n#\t3\t' + 'GR power (Er)'
    header = header + '\n#\t4\t' + 'a_s (a0)'
    header = header + '\n#\t5\t' + 'alpha - advance'
    header = header + '\n#\t6\t' + 'alpha - desired'
    header = header + '\n#\t7\t' + 'tunneling (kHz)'
    header = header + '\n#\t8\t' + 'U/t'
    header = header + '\n#\t9\t' + 'bfield (Gauss)'
    header = header + '\n'

    numpy.savetxt(datfile, allRamps)

    with open(datfile, 'w') as f:
        X = numpy.asarray(allRamps)
        f.write(bytes(header))

        format = '%.6e'
        ncol = X.shape[1]
        format = [
            format,
        ] * ncol
        format = ' '.join(format)

        newline = '\n'
        for row in X:
            f.write(numpy.compat.asbytes(format % tuple(row) + newline))

    shutil.copyfile(
        figfile,
        seqconf.savedir() + 'expseq' + seqconf.runnumber() +
        '_latticeRamp.png')
    shutil.copyfile(
        datfile,
        seqconf.savedir() + 'expseq' + seqconf.runnumber() +
        '_latticeRamp.dat')
    #plt.savefig( seqconf.savedir() + 'expseq' + seqconf.runnumber() + '_latticeRamp.png', dpi=120)

    #################################
    #### APPEND RAMPS TO SEQUENCE ###
    #################################

    wfms = []

    for ch in ['ir1pow', 'ir2pow', 'ir3pow']:
        n = filter(str.isdigit, ch)[0]
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        w.y = physics.cnv(ch, y_ir)
        wfms.append(w)

    for ch in ['greenpow1', 'greenpow2', 'greenpow3']:
        n = filter(str.isdigit, ch)[0]
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden

        correction = DIMPLE.__dict__['gr' + n + 'correct']

        w.y = physics.cnv(ch, correction * grwfms[ch])
        wfms.append(w)

    for ch in ['lcr1', 'lcr2', 'lcr3']:
        n = filter(str.isdigit, ch)[0]
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        force = DL.__dict__['force_' + ch]
        if force >= 0 and force <= 1:
            print "...Forcing LCR%s = %f during lattice ramp" % (n, force)
            w.y = physics.cnv(ch, numpy.array(alpha.size * [force]))
        else:
            w.y = physics.cnv(ch, alpha)
        wfms.append(w)

    bfieldA = bfieldG / 6.8

    ##ADD field
    bfield = wfm.wave('bfield', 0.0, DL.ss)
    bfield.y = physics.cnv('bfield', bfieldA)
    wfms.append(bfield)

    ##ADD gradient field
    gradient = gradient_wave('gradientfield', 0.0, DL.ss, volt=0.0)
    gradient.follow(bfield)
    wfms.append(gradient)

    # RF sweep
    if DL.rf == 1:
        rfmod = wfm.wave('rfmod', 0., DL.ss)
        rfmod.appendhold(bfield.dt() + DL.rftime)
        rfmod.linear(DL.rfvoltf, DL.rfpulsedt)
        wfms.append(rfmod)

    #Take care of imaging frequencies, including various kill experiments
    bfieldG = physics.inv('bfield', bfield.y[-1]) * 6.8
    hfimg0 = -1. * (100.0 + 163.7 - 1.414 * bfieldG)

    print "...ANDOR:hfimg and hfimg0 will be modified  in report\n"
    print "\tNEW  ANDOR:hfimg  = %.2f MHz" % (hfimg0 - DL.imgdet)
    print "\tNEW  ANDOR:hfimg0 = %.2f MHz\n" % hfimg0
    gen.save_to_report('ANDOR', 'hfimg', hfimg0 - DL.imgdet)
    gen.save_to_report('ANDOR', 'hfimg0', hfimg0)
    newANDORhfimg = hfimg0 - DL.imgdet

    # Kill hfimg
    if DL.probekill == 1 or DL.braggkill == 1 or DL.lightassist or DL.lightassist_lock:
        hfimgdelay = 50.  #ms
        analogimg = wfm.wave('analogimg', newANDORhfimg, DL.ss)

        if DL.probekill == 1:
            if (-DL.probekilltime + hfimgdelay) < DL.image:
                analogimg.appendhold(bfield.dt() + DL.probekilltime -
                                     hfimgdelay)
                analogimg.linear(DL.probekill_hfimg, 0.0)
                analogimg.appendhold(hfimgdelay + DL.probekilldt + 3 * DL.ss)

        elif DL.braggkill == 1:
            if (-DL.braggkilltime + hfimgdelay) < DL.image:
                analogimg.appendhold(bfield.dt() + DL.braggkilltime -
                                     hfimgdelay)
                analogimg.linear(DL.braggkill_hfimg, 0.0)
                analogimg.appendhold(hfimgdelay + DL.braggkilldt + 3 * DL.ss)

        #elif DL.lightassist == 1 or DL.lightassist_lock:
        #    analogimg.appendhold( bfield.dt()  - hfimgdelay)
        #    analogimg.linear( DL.lightassist_hfimg , 0.0)
        #    duration = DL.lightassist_lockdtUP + DL.lightassist_t0 + DL.lightassistdt + DL.lightassist_lockdtDOWN
        #    analogimg.appendhold( hfimgdelay + duration + 3*DL.ss)

        analogimg.linear(newANDORhfimg, 0.)
        analogimg.extend(10)
        wfms.append(analogimg)

    #analogimg = bfieldwfm.hfimg_wave('analogimg', ANDOR.hfimg, DL.ss)
    #andorhfimg0 = analogimg.follow(bfield, DL.imgdet)
    #wfms.append(analogimg)

    # If we are doing round trip END, then mirror all the ramps
    # before adding them to the sequence
    if DL.round_trip == 1:
        if DL.round_trip_type == 1:
            maxdt = 0.
            maxi = -1
            for i, w in enumerate(wfms):
                if w.dt() > maxdt:
                    maxdt = w.dt()
                    maxi = i

            maxdt = maxdt + DL.wait_at_top / 2.

            for w in wfms:
                w.extend(maxdt)
                if 'lcr' in w.name:
                    yvals = w.y

                    #Get the reverse of the alpha desired array
                    alpha_mirror = numpy.copy(alpha_desired[::-1])

                    #Add the wait at top part so that it has same length as yvals
                    if alpha_mirror.size > yvals.size:
                        print "Error making mirror ramp for LCR."
                        print "Program will exit."
                        exit(1)
                    alpha_mirror = numpy.append(
                        (yvals.size - alpha_mirror.size) * [alpha_mirror[0]],
                        alpha_mirror)

                    #This is how much the mirror ramp will be advanced
                    N_adv = int(math.floor(DL.lcr_mirror_advance / DL.ss))

                    if N_adv < alpha_mirror.size:
                        alpha_mirror = alpha_mirror[N_adv:]
                        alpha_mirror = numpy.append(
                            alpha_mirror, (yvals.size - alpha_mirror.size) *
                            [alpha_mirror[-1]])
                    else:
                        alpha_mirror = numpy.array(yvals.size *
                                                   [alpha_mirror[-1]])

                    w.y = numpy.concatenate(
                        (yvals, physics.cnv(w.name, alpha_mirror)))
                else:
                    w.mirror()
                w.appendhold(DL.wait_at_end)

    N_adv = int(math.floor(alpha_advance / DL.ss))

    alpha_desired = numpy.copy(alpha)

    for wavefm in wfms:
        print "%s dt = %f" % (wavefm.name, wavefm.dt())

    #Wait the buffer for the lattice loading ramps before adding them
    bufferdt = 20.
    s.wait(bufferdt)
    #Add lattice wfms
    duration = s.analogwfm_add(DL.ss, wfms)

    if DL.image < 2500.:
        s.wait(duration)
    else:
        print "...DL.image = %f  >= 2500.  Digital seq extension will be used." % DL.image
        s.wait(DL.image)

    #Here give some buffer time to do lock, RF etc.
    #It has to be minimum 5.0 ms

    ### Figure out when to turn interlock back on, using alpha information
    #~ if duration > DL.t0 + DL.dt:
    #~ s.wait(-DL.lattice_interlock_time)
    #~ if DL.use_lattice_interlock == 1:
    #~ s.digichg('latticeinterlockbypass',0)
    #~ else:
    #~ s.digichg('latticeinterlockbypass',1)
    #~ s.wait( DL.lattice_interlock_time)

    return s, y_ir[-1]
示例#24
0
def dimple_to_lattice(s,cpowend):
    
    print "----- LATTICE LOADING RAMPS -----"

    # Find out which is the longest of the ramps we are dealing with:
    maxX =max( [xdomain(DL.latticeV0)[1] ,\
         xdomain(DL.irpow)[1],\
         xdomain(DL.grpow1)[1],\
         xdomain(DL.grpow2)[1],\
         xdomain(DL.grpow3)[1],\
         xdomain(DL.a_s)[1]] )
    print "Largest x value = %.3f ms\n" %  maxX
   
    # We define the times for which all functions will be evaluated
    # MIN TIME TO DO DIGITAL EXTENSION
    DIGEXTENSION = 2050.
    if DL.image >= DIGEXTENSION:
        Xendtime = DIGEXTENSION
    else:
        Xendtime = DL.image

    Nnew = int(math.floor( Xendtime / DL.ss) )  
    Xnew = numpy.arange( Xendtime/Nnew, DL.image, Xendtime/Nnew ) 
    print "X array defined from dt:"
    print "DL.ss =", DL.ss
    print "x0  = ",Xnew[0]
    print "xf  = ",Xnew[-1]
    print "xdt = ",Xnew[1]-Xnew[0]
    print "%d samples" % Nnew
    print 'x shape = ', Xnew.shape
    
    # Define how we want to ramp up the lattice depth
    v0_ramp, xy_v0, v0set =  interpolate_ramp( DL.latticeV0)
    v0  = v0_ramp(Xnew)
                        
    
    
    ###########################################
    #### AXIS DEFINITIONS FOR PLOTS ###
    ###########################################    
    
    fig = plt.figure( figsize=(4.5*1.05,8.*1.1))
    ax0 = fig.add_axes( [0.18,0.76,0.76,0.20]) 
    ax2 = fig.add_axes( [0.18,0.645,0.76,0.11])
    ax3 = fig.add_axes( [0.18,0.53,0.76,0.11])
    ax1 = fig.add_axes( [0.18,0.415,0.76,0.11])
    ax5 = fig.add_axes( [0.18,0.30,0.76,0.11])
    ax4 = fig.add_axes( [0.18,0.185,0.76,0.11])
    ax6 = fig.add_axes( [0.18,0.07,0.76,0.11])

    allax = [ax0, ax1, ax2, ax3, ax4, ax5, ax6]
    for ax in allax:
        ax.axvline( DL.image, linewidth = 1., color='black', alpha=0.6)
    
    lw=1.5
    labelx=-0.12
    legsz =8.
    
    xymew=0.5
    xyms=9
    
    
    ax0.plot( Xnew, v0, 'b', lw=2.5, label='Lattice depth')
    ax0.plot(xy_v0[:,0],xy_v0[:,1], 'x', color='blue', ms=5.)
    ax0.plot(v0set[:,0],v0set[:,1], '.', mew=xymew, ms=xyms, color='blue')
    
    
    ###########################################
    #### USER DEFINED RAMPS: IR, GR, and U ###
    ###########################################      
    
    # Define how we want to ramp up the IR power
    if DIMPLE.allirpow > 0.:
      ir_offset = DIMPLE.allirpow
    else:
      ir_offset = DIMPLE.ir1pow2
    ir_ramp, xy_ir, ir =  interpolate_ramp( DL.irpow, yoffset=ir_offset)
    
    dt_ir = numpy.amax( ir[:,0]) - numpy.amin( ir[:,0])
    N_ir = int(math.floor( dt_ir / DL.ss ))
    x_ir = numpy.arange( dt_ir/N_ir, dt_ir, dt_ir/N_ir)
    
    y_ir = ir_ramp(Xnew)
    
    if v0.size > y_ir.size:
        y_ir = numpy.append(y_ir, (v0.size-y_ir.size)*[y_ir[-1]])
    elif v0.size < y_ir.size:
        y_ir = y_ir[0:v0.size]
        
    if v0.size != y_ir.size:
        msg = "IRPOW ERROR: number of samples in IR ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR',msg)
        exit(1)
        
   
    alpha_clip_range = 0.1 
    if (v0 > y_ir+ alpha_clip_range).any():
        msg = "IRPOW ERROR:  not enough power to get desired lattice depth"
        print msg
        bad = numpy.where( v0 > y_ir + alpha_clip_range)
        timefail =  int(bad[0][0])*float(DL.ss)
        msg = msg + "\nFirst bad sample = %d out of %d" % (bad[0][0], v0.size)
        msg = msg + "\n  t = %f " %  timefail
        msg = msg + "\n v0 = %f " %   v0[ bad[0][0] ]
        msg = msg + "\n ir = %f " % y_ir[ bad[0][0] ]
        print v0[bad[0][0]]
        print y_ir[bad[0][0]]
        errormsg.box('LATTICE LOADING ERROR',msg)
        exit(1)
    
    ax0.plot(xy_ir[:,0],xy_ir[:,1], 'x', color='darkorange', ms=5.)
    ax0.plot(ir[:,0],ir[:,1], '.', mew=xymew, ms=xyms, color='darkorange')
    ax0.plot(Xnew, y_ir, lw=lw, color='darkorange',label='irpow')
    
    
    # Define how we want to ramp up the GR power
    grwfms = {}
    splmrkr = ['x','+','d']
    ptsmrkr = ['^','s','p']
    for i,grramp in enumerate([(DL.grpow1,DIMPLE.gr1pow2), (DL.grpow2,DIMPLE.gr2pow2), (DL.grpow3,DIMPLE.gr3pow2)]):
      ramppts = grramp[0]
      ramp0 = grramp[1] 

      print 'gr'+'%d'%i +' offset = %f' % ramp0

      gr_ramp, xy_gr, gr =  interpolate_ramp( ramppts, yoffset=ramp0)
    
      dt_gr = numpy.amax( gr[:,0]) - numpy.amin( gr[:,0])
      N_gr = int(math.floor( dt_gr / DL.ss ))
      x_gr = numpy.arange( dt_gr/N_gr, dt_gr, dt_gr/N_gr)
    
      y_gr = gr_ramp(Xnew)
      if DL.signal == 0:
          y_gr = y_gr / 2.0
    
      if v0.size > y_gr.size:
          y_gr = numpy.append(y_gr, (v0.size-y_gr.size)*[y_gr[-1]])
      elif v0.size < y_gr.size:
          y_gr = y_gr[0:v0.size]
        
      if v0.size != y_gr.size:
          msg = "GRPOW ERROR: number of samples in GR ramp and V0 ramp does not match!"
          errormsg.box('LATTICE LOADING ERROR',msg)
          exit(1)

      grwfms[ 'greenpow' + '%1d' % (i+1) ] = y_gr 

      ax0.plot(xy_gr[:,0],xy_gr[:,1], marker= splmrkr[i] ,mec='green', mfc='None', ms=3.)
      ax0.plot(gr[:,0],gr[:,1], marker=ptsmrkr[i], mew=xymew, ms=xyms/2., mfc='None', mec='green')#, label='grpow dat')
      ax0.plot(Xnew, y_gr, lw=lw, color='green', label='grpow')

    for grch in grwfms.keys():
      print grch, " = ", grwfms[grch].shape
    
    ax0.set_xlim(left=-10., right= ax0.get_xlim()[1]*1.1)   
    plt.setp( ax0.get_xticklabels(), visible=False)
    ylim = ax0.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax0.set_ylim( ylim[0]-extra, ylim[1]+extra )
    ax0.grid(True)
    ax0.set_ylabel('$E_{r}$',size=16, labelpad=0)
    ax0.yaxis.set_label_coords(labelx, 0.5)
    ax0.set_title('Lattice Loading')
    ax0.legend(loc='best',numpoints=1,prop={'size':legsz*0.8})
    
    
    # Define how we want to ramp up the scattering length (control our losses)
    a_s_ramp, xy_a_s, a_s =  interpolate_ramp( DL.a_s)
    
    
    dt_a_s = numpy.amax( a_s[:,0]) - numpy.amin( a_s[:,0])
    N_a_s = int(math.floor( dt_a_s / DL.ss ))
    x_a_s = numpy.arange( dt_a_s/N_a_s, dt_a_s, dt_a_s/N_a_s)
    y_a_s = a_s_ramp(Xnew)

   
    
    if v0.size > y_a_s.size:
        y_a_s = numpy.append(y_a_s, (v0.size-y_a_s.size)*[y_a_s[-1]])
    elif v0.size < y_a_s.size:
        y_a_s = y_a_s[0:v0.size]
        
    if v0.size != y_a_s.size:
        msg = "a_s ERROR: number of samples in a_s ramp and V0 ramp does not match!"
        errormsg.box('LATTICE LOADING ERROR',msg)
        exit(1)
    
    
    
    ax1.plot(xy_a_s[:,0],xy_a_s[:,1]/100., 'x', color='#C10087', ms=5.)
    ax1.plot(a_s[:,0],a_s[:,1]/100., '.', mew=xymew, ms=xyms, color='#C10087')
    ax1.plot(Xnew, y_a_s/100., lw=lw, color='#C10087', label=r'$a_s\mathrm{(100 a_{0})}$')
    ax1.set_ylabel(r'$a_s\mathrm{(100 a_{0})}$',size=16, labelpad=0)
    ax1.yaxis.set_label_coords(labelx, 0.5)

    
    ax1.set_xlim( ax0.get_xlim()) 
    ylim = ax1.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax1.set_ylim( ylim[0]-extra, ylim[1]+extra )
    plt.setp( ax1.get_xticklabels(), visible=False)
    ax1.grid(True)
    ax1.legend(loc='best',numpoints=1,prop={'size':legsz})
    

    #######################################################################
    #### CALCULATED RAMPS:  ALPHA, TUNNELING, SCATTERING LENGTH, BFIELD ###
    #######################################################################
    
    alpha = (v0/y_ir)**2.
    
    alpha_advance = 100.
    N_adv = int(math.floor( alpha_advance / DL.ss))
    
    alpha  = alpha.clip(0.,1.)
    alpha_desired = numpy.copy(alpha)
    
    if N_adv < v0.size:
        alpha = alpha[N_adv:]
        alpha = numpy.append(alpha, (v0.size-alpha.size)*[alpha[-1]])
    else:
        alpha = numpy.array( v0.size*[alpha[-1]] )
    
    #alpha = alpha.clip(0., 1.)
    
    ax2.plot( Xnew, alpha, lw=lw, color='saddlebrown', label='alpha adv')
    ax2.plot( Xnew, alpha_desired,':', lw=lw, color='saddlebrown', label='alpha')
    
    ax2.set_xlim( ax0.get_xlim()) 
    ax2.set_ylim(-0.05,1.05)
    plt.setp( ax2.get_xticklabels(), visible=False)
    ax2.grid()
    ax2.set_ylabel('$\\alpha$',size=16, labelpad=0)
    ax2.yaxis.set_label_coords(labelx, 0.5)
    
    ax2.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    
    tunneling_Er  = physics.inv('t_to_V0', v0)
    tunneling_kHz = tunneling_Er * 29.2
    
    ax3.plot( Xnew, tunneling_kHz, lw=lw, color='red', label='$t$ (kHz)')

    
    ax3.set_xlim( ax0.get_xlim()) 
    ylim = ax3.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax3.set_ylim( ylim[0]-extra, ylim[1]+extra )
    plt.setp( ax3.get_xticklabels(), visible=False)
    ax3.grid(True)
    ax3.set_ylabel(r'$t\,\mathrm{(kHz)}$',size=16, labelpad=0)
    ax3.yaxis.set_label_coords(labelx, 0.5)
    ax3.legend(loc='best',numpoints=1,prop={'size':legsz})

    
    wannierF = physics.inv('wF_to_V0', v0)
     
    bohrRadius = 5.29e-11 #meters
    lattice_spacing = 1.064e-6 / 2. #meters
    
    bfieldG = physics.cnv('as_to_B', y_a_s)
    print 
    print "The last value of the scattering length ramp is:"
    print 'a_s =', y_a_s[-1]
    print 'B   =', bfieldG[-1]
    print 
    
    U_over_t = y_a_s * bohrRadius / lattice_spacing * wannierF / tunneling_Er
    
    
    
    
    ax4.plot( Xnew, U_over_t, lw=lw, color='k', label=r'$U/t$')
    
    ax4.set_xlim( ax0.get_xlim()) 
    ylim = ax4.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax4.set_ylim( ylim[0]-extra, ylim[1]+extra )
    plt.setp( ax4.get_xticklabels(), visible=False)
    ax4.grid(True)
    ax4.set_ylabel(r'$U/t$',size=16, labelpad=0)
    ax4.yaxis.set_label_coords(labelx, 0.5)
    
    ax4.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    
    ax5.plot( Xnew, bfieldG, lw=lw, color='purple', label='$B$ (G)')
    
    ax5.set_xlim( ax0.get_xlim()) 
    ylim = ax5.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax5.set_ylim( ylim[0]-extra, ylim[1]+extra )
    ax5.grid(True)
    plt.setp( ax5.get_xticklabels(), visible=False)
    ax5.set_ylabel(r'$B\,\mathrm{(G)}$',size=16, labelpad=0)
    ax5.yaxis.set_label_coords(labelx, 0.5)
    
    
    ax5.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    
    ax6.plot( Xnew, (tunneling_Er / U_over_t), lw=lw, color='#25D500', label=r'$t^{2}/U\,(E_{r)}$')
    #ax6.set_yscale('log')
    
    ax6.set_xlim( ax0.get_xlim()) 
    ylim = ax6.get_ylim()
    extra = (ylim[1]-ylim[0])*0.1
    ax6.set_ylim( ylim[0]*0.5, ylim[1] )
    ax6.grid(True)
    ax6.set_ylabel(r'$t^{2}/U\,(E_{r)}$',size=16, labelpad=0)
    ax6.yaxis.set_label_coords(labelx, 0.5)
    
    
    ax6.legend(loc='best',numpoints=1,prop={'size':legsz})
    
    ax6.set_xlabel('time (ms)')

    figfile = seqconf.seqtxtout().split('.')[0]+'_latticeRamp.png'    
    plt.savefig(figfile , dpi=120 )
    
    #Save all ramps to a txt file for later plotting. 
    datfile = seqconf.seqtxtout().split('.')[0]+'_latticeRamp.dat'
    allRamps = numpy.transpose(numpy.vstack((Xnew, v0, y_ir, grwfms['greenpow1'], y_a_s, alpha, alpha_desired, \
                                    tunneling_kHz, U_over_t, bfieldG)))
    header = '# Column index'
    header = header + '\n#\t0\t' + 'time(ms)'
    header = header + '\n#\t1\t' + 'Lattice Depth (Er)'
    header = header + '\n#\t2\t' + 'Ir power (Er)'
    header = header + '\n#\t3\t' + 'GR power (Er)'
    header = header + '\n#\t4\t' + 'a_s (a0)'
    header = header + '\n#\t5\t' + 'alpha - advance'
    header = header + '\n#\t6\t' + 'alpha - desired'
    header = header + '\n#\t7\t' + 'tunneling (kHz)'
    header = header + '\n#\t8\t' + 'U/t'
    header = header + '\n#\t9\t' + 'bfield (Gauss)'
    header = header + '\n'
    
    numpy.savetxt( datfile, allRamps)
    
    with open(datfile, 'w') as f:
        X = numpy.asarray( allRamps )
        f.write(bytes(header))
        
        format = '%.6e'
        ncol = X.shape[1]
        format = [format ,] *ncol
        format = ' '.join(format)
        
        newline = '\n'
        for row in X:
            f.write(numpy.compat.asbytes(format % tuple(row) + newline))

    
    shutil.copyfile( figfile,  seqconf.savedir() + 'expseq' + seqconf.runnumber() + '_latticeRamp.png')
    shutil.copyfile( datfile,  seqconf.savedir() + 'expseq' + seqconf.runnumber() + '_latticeRamp.dat')
    #plt.savefig( seqconf.savedir() + 'expseq' + seqconf.runnumber() + '_latticeRamp.png', dpi=120)
    
    
    #################################
    #### APPEND RAMPS TO SEQUENCE ###
    #################################
    
    wfms=[]

    if DL.signal == 0:
          print " LOCK VALUE FOR SIGNAL / NOSIGNAL "
          print " before = ", DL.lock_Er
          DL.lock_Er = DL.lock_Er / 1.8
          print " after  = \n", DL.lock_Er
    
    for ch in ['ir1pow','ir2pow','ir3pow']:
        n = filter( str.isdigit, ch)[0] 
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        w.y = physics.cnv( ch, y_ir )
        if DL.lock:
            endval = w.y[-1]
            w.insertlin_cnv(DL.lock_Er, DL.lock_dtUP, DL.lock_t0 )
        elif DL.lightassist_lock:
            endval = w.y[-1]
            w.linear(DL.lightassist_lockpowIR, DL.lightassist_lockdtUP)
            w.appendhold( DL.lightassist_t0 + DL.lightassistdt )
            if DL.endvalIR >= 0.:
                w.linear(  DL.endvalIR, DL.lightassist_lockdtDOWN)
            else:
                w.linear(  None, DL.lightassist_lockdtDOWN, volt=endval)
        wfms.append(w)
        
    for ch in ['greenpow1','greenpow2','greenpow3']:
        n = filter( str.isdigit, ch)[0] 
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        
        correction = DIMPLE.__dict__['gr'+n+'correct']
        
        w.y = physics.cnv( ch, correction * grwfms[ch] )
        if DL.lightassist_lock:
            endval = w.y[-1]
            w.linear(DL.lightassist_lockpowGR, DL.lightassist_lockdtUP)
            w.appendhold( DL.lightassist_t0 + DL.lightassistdt )
            if DL.endvalGR >= 0.:
                w.linear(  DL.endvalGR, DL.lightassist_lockdtDOWN)
            else:
                w.linear(  None, DL.lightassist_lockdtDOWN, volt=endval)
        wfms.append(w)
     

    for ch in ['lcr1','lcr2','lcr3']:
        n = filter( str.isdigit, ch)[0] 
        w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
        force = DL.__dict__['force_'+ch]
        if force >= 0 and force <=1:
            print "...Forcing LCR%s = %f during lattice ramp" % (n,force)
            w.y = physics.cnv( ch, numpy.array( alpha.size*[force] )  )
        elif DL.signal == 0:
            print "...Forcing LCR%s = 0. so that it does NOT rotate to LATTICE" % n 
            w.y = physics.cnv( ch, numpy.array( alpha.size*[0.0] )  )
        else:
            w.y = physics.cnv( ch, alpha )
        wfms.append(w)
    

    
    bfieldA = bfieldG/6.8
    
    ##ADD field
    bfield = wfm.wave('bfield', 0.0, DL.ss)
    bfield.y = physics.cnv( 'bfield', bfieldA)
    print "The last value of the bfield voltage is =", bfield.y[-1]
    print 
    wfms.append(bfield)
    
    
    ##ADD gradient field
    gradient = gradient_wave('gradientfield', 0.0, DL.ss,volt = 0.0)
    gradient.follow(bfield)
    wfms.append(gradient)
   
    
    buffer = 40.
    s.wait(buffer)
    
    
    #~ odtpow = odt.odt_wave('odtpow', cpowend, DL.ss)
    #~ if DIMPLE.odt_t0 > buffer :
        #~ odtpow.appendhold( DIMPLE.odt_t0 - buffer)
    #~ if DIMPLE.odt_pow < 0.:
        #~ odtpow.appendhold( DIMPLE.odt_dt)
    #~ else:
        #~ odtpow.tanhRise( DIMPLE.odt_pow, DIMPLE.odt_dt, DIMPLE.odt_tau, DIMPLE.odt_shift)    
        
    #~ if numpy.absolute(DIMPLE.odt_pow) < 0.0001:
        #~ s.wait( odtpow.dt() )
        #~ s.digichg('odtttl',0)
        #~ s.wait(-odtpow.dt() )
    
    #~ wfms.append(odtpow)
        
    
    # RF sweep
    if DL.rf == 1:   
        rfmod  = wfm.wave('rfmod', 0., DL.ss)
        rfmod.appendhold( bfield.dt() + DL.rftime )
        rfmod.linear( DL.rfvoltf, DL.rfpulsedt)
        wfms.append(rfmod)


    if DL.round_trip == 1:
        bindex = 0  # Calculate detunings using starting field
    else: 
        bindex = -1 # Calculate detunings using field at the end of ramps

    bfieldG = physics.inv( 'bfield', bfield.y[bindex]) * 6.8
    hfimg0 = -1.*(100.0 + 163.7 - 1.414*bfieldG)
   
    # Find bindex for braggkill time 
    bindex_BK =  math.floor(-DL.braggkilltime / bfield.ss)
    bfieldG_BK = physics.inv( 'bfield', bfield.y[-1-bindex_BK]) * 6.8
    hfimg0_BK =  -1.*(100.0 + 163.7 - 1.414*bfieldG_BK) 
    DL.braggkill_hfimg = hfimg0_BK - DL.braggkill_hfimg
    print "\n...Braggkill hfimg modification:\n"
    print "\tNEW braggkill_hfimg = %.2f MHz" % DL.braggkill_hfimg

    # Find bindex for bragg2kill time 
    bindex_B2K =  math.floor(-DL.bragg2killtime / bfield.ss)
    bfieldG_B2K = physics.inv( 'bfield', bfield.y[-1-bindex_B2K]) * 6.8
    hfimg0_B2K =  -1.*(100.0 + 163.7 - 1.414*bfieldG_B2K) 
    DL.bragg2kill_hfimg1 = hfimg0_B2K - DL.bragg2kill_hfimg1
    DL.bragg2kill_hfimg2 = hfimg0_B2K - DL.bragg2kill_hfimg2
    print "\n...Bragg2kill hfimg modification:\n"
    print "\tNEW brag2gkill_hfimg1 = %.2f MHz" % DL.bragg2kill_hfimg1
    print "\tNEW brag2gkill_hfimg2 = %.2f MHz" % DL.bragg2kill_hfimg2
    
    
    print "\n...ANDOR:hfimg and hfimg0 will be modified  in report\n"
    print "\tNEW  ANDOR:hfimg  = %.2f MHz" % ( hfimg0 - DL.imgdet)
    print "\tNEW  ANDOR:hfimg0 = %.2f MHz\n" %  hfimg0
    gen.save_to_report('ANDOR','hfimg', hfimg0 - DL.imgdet)
    gen.save_to_report('ANDOR','hfimg0', hfimg0)
    
    newANDORhfimg = hfimg0 - DL.imgdet

    # THIS DEFINES THE TIME IT TAKES THE OFFSET LOCK TO SWITCH TO
    # A NEW SETPOINT
    hfimgdelay = 50. #ms
        
    # Kill hfimg
    if DL.probekill ==1 or DL.braggkill ==1 or  DL.bragg2kill==1 or DL.lightassist or DL.lightassist_lock:
        analogimg = wfm.wave('analogimg', newANDORhfimg, DL.ss)
        
        if DL.probekill == 1:
            if (-DL.probekilltime+hfimgdelay) < DL.image:
                analogimg.appendhold( bfield.dt() + DL.probekilltime - hfimgdelay)
                analogimg.linear( DL.probekill_hfimg , 0.0)
                analogimg.appendhold( hfimgdelay + DL.probekilldt + 3*DL.ss)
        
        elif DL.braggkill == 1:
            print "Setting up analogimg for braggkill"
            if (-DL.braggkilltime+hfimgdelay) < DL.image:
                analogimg.appendhold( bfield.dt() + DL.braggkilltime - hfimgdelay)
                analogimg.linear( DL.braggkill_hfimg , 0.0)
                analogimg.appendhold( hfimgdelay + DL.braggkilldt + 3*DL.ss)
 
        elif DL.bragg2kill == 1:
            print "Setting up analogimg for bragg2kill"
            if (-DL.bragg2killtime+hfimgdelay) < DL.image:
                # This sets up the detuning for the first pulse
                analogimg.appendhold( bfield.dt() + DL.bragg2killtime - hfimgdelay)
                analogimg.linear( DL.bragg2kill_hfimg1 , 0.0)
                analogimg.appendhold( hfimgdelay + DL.bragg2killdt + 3*DL.ss)
 
                # Then set up the detuning for the second pulse
                analogimg.linear( DL.bragg2kill_hfimg2 , 0.0)
                analogimg.appendhold( hfimgdelay + DL.bragg2killdt + 3*DL.ss)
                
            
        
        elif DL.lightassist == 1 or DL.lightassist_lock:
            analogimg.appendhold( bfield.dt()  - hfimgdelay)
            analogimg.linear( DL.lightassist_hfimg , 0.0)
            duration = DL.lightassist_lockdtUP + DL.lightassist_t0 + DL.lightassistdt + DL.lightassist_lockdtDOWN
            analogimg.appendhold( hfimgdelay + duration + 3*DL.ss)
            
        analogimg.linear( newANDORhfimg, 0.)
        analogimg.extend(10)
        wfms.append(analogimg)
    
        

    
    #analogimg = bfieldwfm.hfimg_wave('analogimg', ANDOR.hfimg, DL.ss)
    #andorhfimg0 = analogimg.follow(bfield, DL.imgdet)
    #wfms.append(analogimg)
    

    # If we are doing round trip END, then mirror all the ramps 
    # before adding them to the sequence
    if DL.round_trip == 1:
        if DL.round_trip_type == 1:
            maxdt = 0.
            maxi = -1
            for i,w in enumerate(wfms):
                if w.dt() > maxdt:
                    maxdt = w.dt()
                    maxi = i
    
            maxdt = maxdt + DL.wait_at_top / 2. 
    
            for w in wfms:
                w.extend(maxdt)
                if 'lcr' in w.name:
                    yvals = w.y
                    
                    #Get the reverse of the alpha desired array
                    alpha_mirror = numpy.copy(alpha_desired[::-1])
                    
                    #Add the wait at top part so that it has same length as yvals
                    if alpha_mirror.size > yvals.size:
                        print "Error making mirror ramp for LCR."
                        print "Program will exit."
                        exit(1)
                    alpha_mirror = numpy.append( (yvals.size - alpha_mirror.size)*[ alpha_mirror[0] ], alpha_mirror )
                    
                                   
                    #This is how much the mirror ramp will be advanced
                    N_adv = int(math.floor( DL.lcr_mirror_advance / DL.ss))
                    
                    if N_adv < alpha_mirror.size:
                        alpha_mirror = alpha_mirror[N_adv:]
                        alpha_mirror = numpy.append(alpha_mirror, (yvals.size-alpha_mirror.size)*[alpha_mirror[-1]])
                    else:
                        alpha_mirror = numpy.array( yvals.size*[alpha_mirror[-1]] )
                    
                    
                    
                    w.y = numpy.concatenate((yvals,physics.cnv( w.name, alpha_mirror )))
                else:
                    w.mirror()
                w.appendhold( DL.wait_at_end)
            
        
    
    N_adv = int(math.floor( alpha_advance / DL.ss))
    
    alpha_desired = numpy.copy(alpha)
    
    
    for wavefm in wfms:
        print "%s dt = %f" % (wavefm.name, wavefm.dt())
   
     
    
    duration = s.analogwfm_add(DL.ss,wfms)
    
    if DL.image < DIGEXTENSION:
        s.wait(duration)
    else:
        print "...DL.image = %f  >= %.2f  Digital seq extension will be used." % (DL.image, DIGEXTENSION)
        s.wait( DL.image )
        
    
    ### Prepare the parts of the ramps that are going to be used to mock
    ### the conditions for the noatoms shot
    ### 1. get dt = [noatoms] ms from the end of the lattice ramps.
    if 'manta' in DL.camera:
        noatomsdt = MANTA.noatoms
    else:
        noatomsdt = ANDOR.noatoms
    noatomswfms = []
    for wavefm in wfms:
        cp = copy.deepcopy( wavefm ) 
        cp.idnum = time.time()*100
        cp.retain_last( DL.bgRetainDT )
        noatomswfms.append( cp ) 
    
        
        
    
    
    
    ### Figure out when to turn interlock back on, using alpha information
    #~ if duration > DL.t0 + DL.dt:
        #~ s.wait(-DL.lattice_interlock_time)
        #~ if DL.use_lattice_interlock == 1:
            #~ s.digichg('latticeinterlockbypass',0)
        #~ else:
            #~ s.digichg('latticeinterlockbypass',1)
        #~ s.wait( DL.lattice_interlock_time)
        
        
    #########################################
    ## OTHER TTL EVENTS: probekill, braggkill, rf, quick2
    #########################################
    # Braggkill
    if DL.braggkill == 1:
        print "Using Bragg Kill"
        s.wait( DL.braggkilltime)
        s = manta.OpenShutterBragg(s,DL.shutterdelay)
        s.digichg('bragg',1)
        s.wait( DL.braggkilldt)
        s.digichg('brshutter',1) # to close shutter
        s.digichg('bragg',0)
        
        s.wait( -DL.braggkilldt)
        s.wait( -DL.braggkilltime )

    if DL.bragg2kill == 1:
        print "Using Bragg 2 Kill"
        tcur = s.tcur 
        s.wait( DL.bragg2killtime )
        s = manta.OpenShutterBragg(s,DL.shutterdelay)
        s.digichg('bragg',1)
        s.wait( DL.bragg2killdt)
        s.digichg('brshutter',1) # to close shutter
        s.digichg('bragg',0)
  
        s.wait( hfimgdelay + 3*DL.ss )
        s = manta.OpenShutterBragg(s,DL.shutterdelay)
        s.digichg('bragg',1)
        s.wait( DL.bragg2killdt)
        s.digichg('brshutter',1) # to close shutter
        s.digichg('bragg',0)

        # Revert to current time after pulses have been added in the past
        s.tcur = tcur

        
       
        
         

    # Probe Kill
    if DL.probekill == 1:
        s.wait(DL.probekilltime)
        
        s.wait(-10)
        s.digichg('prshutter',0)
        s.wait(10)
        s.digichg('probe',1)
        s.wait(DL.probekilldt)
        s.digichg('probe',0)

        s.digichg('prshutter',1)
        s.wait(-DL.probekilltime)


    # Pulse RF
    if DL.rf == 1:
        s.wait(DL.rftime)
        s.digichg('rfttl',1)
        s.wait(DL.rfpulsedt)
        s.digichg('rfttl',0)
        s.wait(-DL.rfpulsedt)
        s.wait(-DL.rftime)
        



    # QUICK2
    if DL.quick2 == 1:
        s.wait( DL.quick2time)
        s.digichg('quick2',1)
        s.wait(-DL.quick2time)
        

    # Light-assisted collisions
    if DL.lightassist == 1 or DL.lightassist_lock:
        s.wait( -DL.lightassist_lockdtUP -DL.lightassist_t0 -DL.lightassistdt -DL.lightassist_lockdtDOWN - 3*DL.ss)
        
        s.wait(DL.lightassist_lockdtUP + DL.lightassist_t0)
        s.wait(-10)
        s.digichg('prshutter',0)
        s.wait(10)
        s.digichg('probe', DL.lightassist)
        s.wait(DL.lightassistdt)
        s.digichg('probe',0)

        s.digichg('prshutter',1)
        s.wait(DL.lightassist_lockdtDOWN)
        s.wait(3*DL.ss)
        # After the collisions happen we still need to wait some time 
        # for the probe frequency to come back to the desired value
        s.wait(hfimgdelay)


    #########################################
    ## GO BACK IN TIME IF DOING ROUND-TRIP START
    #########################################
    if DL.round_trip == 1:
        if DL.round_trip_type == 0:
            s.wait( -DL.image )
            s.stop_analog()


    #########################################
    ## TURN GREEN OFF BEFORE PICTURES
    #########################################
    if DL.greenoff == 1:
      s.wait( DL.greenoff_t0 ) 
      s.digichg('greenttl1', 0)
      s.digichg('greenttl2', 0)
      s.digichg('greenttl3', 0)
      s.wait(-DL.greenoff_t0 ) 


    
    #########################################
    ## LATTICE LOCK WITH POSSIBILITY OF RF
    #########################################
    bufferdt = 5.0
    lastIR = y_ir[-1]
   
    lockwfms=[]
    if DL.locksmooth == 1 and DL.lock == 0:
        s.wait(bufferdt)
        for ch in ['ir1pow','ir2pow','ir3pow']:
            n = filter( str.isdigit, ch)[0]
            w = wfm.wave(ch, lastIR, DL.lockss)  #Start value will be overrriden
            w.tanhRise( DL.lock_Er, DL.lock_dtUP, 0.4,0.2)
            lockwfms.append(w)
        print "...LOCKING LATTICE TO %f Er" % DL.lock_Er
        print "...lastIR = %.4f" % lastIR
        duration = s.analogwfm_add(DL.lockss,lockwfms)
        print "...duration = %.2f" % duration
        s.wait(duration)
        #~ if DL.lockrf:
            #~ s.digichg('rfttl',1)
            #~ s.wait(DL.rfpulsedt)
            #~ s.digichg('rfttl',0)
        #~ s.wait(0.036)
    #else:
    #    s.wait(bufferdt)
  
    lockwfmscopy = []
    for wavefm in lockwfms:
        cp = copy.deepcopy( wavefm ) 
        cp.idnum = time.time()*100 + 1e3*numpy.random.randint(0,1e8)
        lockwfmscopy.append( cp ) 
        
    
    #########################################
    ## IMAGING AT LOW FIELD
    #########################################
    if DL.lowfieldimg == 1:
        s.wait(DL.lowfieldimg_t0)
        s.digichg('field',0)
        s.wait(-DL.lowfieldimg_t0)
    
    
    #########################################
    ## TTL RELEASE FROM ODT and LATTICE
    #########################################
    #INDICATE WHICH CHANNELS ARE TO BE CONSIDERED FOR THE BACKGROUND
    bg = ['odtttl','irttl1','irttl2','irttl3','greenttl1','greenttl2','greenttl3']
    bgdictPRETOF={}
    for ch in bg:
        bgdictPRETOF[ch] = s.digistatus(ch)
    bgdictPRETOF['tof'] = DL.tof
    print "\nChannel status for pictures: PRE-TOF"
    print bgdictPRETOF
    print
        
    #RELEASE FROM LATTICE
    if DL.tof <= 0.:
        s.wait(1.0+ANDOR.exp)
    s.digichg('greenttl1',0)
    s.digichg('greenttl2',0)
    s.digichg('greenttl3',0)
    s.digichg('irttl1',0)
    s.digichg('irttl2',0)
    s.digichg('irttl3',0)
    #RELEASE FROM IR TRAP
    s.digichg('odtttl',0)
    if DL.tof <= 0.:
        s.wait(-1.0+ANDOR.exp)

    print "TIME WHEN RELEASED FROM LATTICE = ",s.tcur
    s.wait(DL.tof)
    
    return s, noatomswfms, lockwfmscopy, bgdictPRETOF
示例#25
0
def do_roundtrip( s, wfms ):
    """ creates roundtrips to characterize cooling in lattice.
        wfms are the lattice wfms, they can be mirrored here if a mirror 
        ramp is desired. """

    s.wait( RT.wait_at_top)

    # This case is for mirrored ramps
    if RT.mirror == 1 :
        alpha_desired = wfms[1]
        wfms = wfms[0] 
        mirrorwfms = []
        for wavefm in wfms:
            cp = copy.deepcopy( wavefm ) 
            cp.idnum = time.time()*100
            mirrorwfms.append( cp ) 

        for w in mirrorwfms:
            if 'lcr' in w.name:
                yvals = w.y
                
                # Get the reverse of the alpha desired array
                alpha_mirror = numpy.copy(alpha_desired[::-1])
                makeplot( [w, alpha_mirror], name=w.name ) 
  
                # It needs to have the same length as yvals, so append 
                # constant samples at the beginning if needed
                if alpha_mirror.size > yvals.size:
                    print "Error making mirror ramp for LCR."
                    print "Program will exit."
                    exit(1)
                alpha_mirror = numpy.append( (yvals.size - alpha_mirror.size)*[ alpha_mirror[0] ], alpha_mirror )
                
                # This is how much the mirror ramp will be advanced
                N_adv = int(math.floor( RT.lcr_mirror_advance / DL.ss))
                
                if N_adv < alpha_mirror.size:
                    alpha_mirror = alpha_mirror[N_adv:]
                    alpha_mirror = numpy.append(alpha_mirror, (yvals.size-alpha_mirror.size)*[alpha_mirror[-1]])
                else:
                    alpha_mirror = numpy.array( yvals.size*[alpha_mirror[-1]] )
                
                if RT.lcr_snap == 1:
                    alpha_mirror[ alpha_mirror > 0.01 ] = 1.  
                w.y = physics.cnv( w.name, alpha_mirror)  
            else:
                w.y = w.y[::-1]
            w.appendhold( RT.wait_at_end)
        wfms = mirrorwfms

    # This case is for custom ramps
    else:
        # There need to be 10 waveforms
        # 3IR, 3LCR, 3GR, BFIELD 
        # This follows the procedure done for the lattice ramps

        Xendtime = float( RT.image ) 
        Nnew = int(math.floor( Xendtime / DL.ss) ) 
        Xnew = numpy.arange( Xendtime/Nnew, RT.image, Xendtime/Nnew ) 
        ir_ramp, xy_ir, ir =  lattice.interpolate_ramp( RT.irpow, yoffset=0.)
        y_ir = ir_ramp(Xnew)

        grwfms = {}
        for i,grramp in enumerate([RT.grpow1, RT.grpow2, RT.grpow3]):
            gr_ramp, xy_gr, gr =  lattice.interpolate_ramp( grramp, yoffset=0.)
            y_gr = gr_ramp(Xnew)
            grwfms[ 'greenpow' + '%1d' % (i+1) ] = y_gr

        alpha_ramp, xy_alpha, alpha = lattice.interpolate_ramp( RT.alpha, yoffset=0. ) 
        y_alpha =  alpha_ramp(Xnew)

        wfms = []
        for ch in ['ir1pow', 'ir2pow', 'ir3pow']:
            n = filter( str.isdigit, ch)[0] 
            w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
            w.y = physics.cnv( ch, y_ir )
            wfms.append(w)
        for ch in ['greenpow1','greenpow2','greenpow3']:
            n = filter( str.isdigit, ch)[0] 
            w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
            
            correction = DIMPLE.__dict__['gr'+n+'correct']
            w.y = physics.cnv( ch, correction * grwfms[ch] )
            wfms.append(w)

        for ch in ['lcr1','lcr2','lcr3']:
            n = filter( str.isdigit, ch)[0] 
            w = wfm.wave(ch, 0.0, DL.ss)  #Start value will be overrriden
            print "Obtaining LCR waveform for ROUNDTRIP..."
            print y_alpha
            w.y = physics.cnv( ch, y_alpha )
            wfms.append(w)
 
        ##ADD field
        bfieldG = physics.cnv('as_to_B', DL.knob05)
        bfieldA = bfieldG/6.8
        bfield = wfm.wave('bfield', bfieldA, DL.ss)
        bfield.extend( wfms[-1].dt() ) 
        wfms.append(bfield)

    # Include ramp to zerocrossing, valid for mirror and custom cases
    # Final field can be given in amps (default) or in a0
    if RT.enable_zc == 1 :
        if isinstance(RT.zc_bias, str):
            if 'a0' in RT.zc_bias: 
                print "Using scattering length units for final field"
                scattlen = float(re.sub('a0','', RT.zc_bias))
                bfieldG = physics.cnv('as_to_B', scattlen)
                bfieldA = bfieldG/6.8
            elif 'knob05' in RT.zc_bias:
                print "Using knob05 as the final scattering length, amounts to no change"
                bfieldG = physics.cnv('as_to_B', DL.knob05)
                bfieldA = bfieldG/6.8
        else:
            bfieldA = RT.zc_bias 
            bfieldG = bfieldA*6.8
    else:
        print "Using knob05 as the final scattering length, amounts to no change"
        bfieldG = physics.cnv('as_to_B', DL.knob05)
        bfieldA = bfieldG/6.8

    # Set hfimg value for the given final field
    hfimg0 = -1.*(100.0 + 163.7 - 1.414*bfieldG)
    print "\n...ANDOR:hfimg and hfimg0 will be modified  in report\n"
    print "\tNEW  ANDOR:hfimg  = %.2f MHz" % ( hfimg0 - RT.imgdet)
    print "\tNEW  ANDOR:hfimg0 = %.2f MHz\n" %  hfimg0
    gen.save_to_report('ANDOR','hfimg', hfimg0 - RT.imgdet)
    gen.save_to_report('ANDOR','hfimg0', hfimg0)
    newANDORhfimg = hfimg0 - RT.imgdet
        
            
    for w in wfms:
        if 'bfield' in w.name:
            bfieldw = w
            if RT.enable_zc == 1 :
                if RT.zc_mirror_t0 < 0.:
                    dt = w.dt()
                    w.chop( w.dt() + RT.zc_mirror_t0 ) 
                if RT.zc_mirror_t0 > 0.:
                    w.appendhold( RT.zc_mirror_t0 )
                w.linear( bfieldA, RT.zc_mirror_rampdt)
                w.appendhold( RT.zc_mirror_holddt ) 

    for i,w in enumerate(wfms):
        if 'gradientfield' in w.name:
            gradientfield_i = i
    print "REMOVING GRADIENT FIELD"
    print len(wfms)  
    wfms = [ w for w in wfms if  w.name != 'gradientfield' ]
    print len(wfms)
    #Add gradientfield ramp to have levitation all the time
    from bfieldwfm import gradient_wave
    gradient = gradient_wave('gradientfield', 0.0, bfieldw.ss,volt = 0.0)
    gradient.follow( bfieldw)
    wfms.append(gradient)
      




    duration = s.analogwfm_add(DL.ss,wfms)
    s.wait(duration)
            
    ### Prepare the parts of the ramps that are going to be used to mock
    ### the conditions for the noatoms shot
    ### 1. get dt = [noatoms] ms from the end of the lattice ramps.
    if 'manta' in DL.camera:
        noatomsdt = MANTA.noatoms
    else:
        noatomsdt = ANDOR.noatoms
    noatomswfms = []
    for wavefm in wfms:
        cp = copy.deepcopy( wavefm ) 
        cp.idnum = time.time()*100
        cp.retain_last( DL.bgRetainDT )
        noatomswfms.append( cp )

 
    #INDICATE WHICH CHANNELS ARE TO BE CONSIDERED FOR THE BACKGROUND
    bg = ['odtttl','irttl1','irttl2','irttl3','greenttl1','greenttl2','greenttl3']
    bgdictPRETOF={}
    for ch in bg:
        bgdictPRETOF[ch] = s.digistatus(ch)
    bgdictPRETOF['tof'] = RT.tof
    print "\nChannel status for pictures: PRE-TOF"
    print bgdictPRETOF
    print
        
    #RELEASE FROM LATTICE
    if RT.tof <= 0.:
        s.wait(1.0+ANDOR.exp)
    s.digichg('greenttl1',0)
    s.digichg('greenttl2',0)
    s.digichg('greenttl3',0)
    s.digichg('irttl1',0)
    s.digichg('irttl2',0)
    s.digichg('irttl3',0)
    #RELEASE FROM IR TRAP
    s.digichg('odtttl',0)
    if RT.tof <= 0.:
        s.wait(-1.0+ANDOR.exp)

    print "TIME WHEN RELEASED FROM LATTICE = ",s.tcur
    s.wait(RT.tof)
    
    return s, noatomswfms, bgdictPRETOF