#!/usr/bin/env python

# To run it, type: python freqsteps.py

###### Defaults:
ipp=1000000  #720 #IPP length in us
freq1= 4.0  #4.02 #4.13 up # 4.15 # 5.56   #5.52 up  MHz
freq2= 4.08  #4.48  #3.91    # 3.93 # 5.34   #5.30     MHz
up=  1   # up=1 means from freq1 to freq2, =-1 means freq2 to freq1
nsteps= 1024  #25   ;#96       # 108 for an 18-min stepping sequence
frston= 10   #10  #120    #s
per=10         # period per frequency, us
offtime=ipp-frston-(nsteps*per)


dfreq= (freq2-freq1)/(nsteps)

#per1=1.00E6/(2*freq1)
#per2=1.0E6/(2*freq2)

txlist=[1,2,3,4,5,6,7,8,9,10,11,12]
site='h'	#Name of site
start_tx=1
t_start=1
no_frq=1

###### Setup section
tname='temp'		#Name of tarlan file
exp_name=tname          #Name of experient
tlan='hf'	            #Tarlan extensions
######

def Print_Comm(Command,At=-1):
	if At<0:
		tf.write(Command+'\n')
	elif At%1>0:
		tf.write('AT\t%.1f\t%s\n'%(At,Command))
	else:	
                tf.write('AT\t%.1f\t%s\n'%(At,Command))
#		tf.write('AT\t%ds\t%s\n'%(At,Command))


#write the tlan file with the times for the 'UPD' commands
tlanname=tname+'.tlan'
tf=open(tlanname,'w')
Print_Comm('%%%%'*6 )
Print_Comm('%PAFPAR_VS 3.0')
Print_Comm('%%%%    '+exp_name+' tlan file   %%%%')
Print_Comm('%%%%'*6 )
Print_Comm('AT     0.5      STMC*,RFON*')
Print_Comm('AT     4.0      UPD*,RXP1ON,RXP2ON,RXSYNC,TXSYNC')
for j in range(long(0),long(nsteps)):
    Print_Comm('UPD*,RXSYNC,TXSYNC',frston+(j)*per)
Print_Comm('UPD*,RXSYNC,TXSYNC,RFOFF*',frston+(nsteps)*per)
#Print_Comm('PSAVON*',frston+1+(nsteps)*per)
#Print_Comm('PSAVOFF*',ipp-1)
Print_Comm('REP',ipp)
Print_Comm('%%%%'*5 )
tf.close()


# write .frq or .paf file with the actual frequencies
freqname=tname+'.paf'
tf=open(freqname,'w')
Print_Comm('%%%%'*6 )
Print_Comm('PAFPAR_VS 3.0')
Print_Comm('%%%%    '+exp_name+' paf file   %%%%')
Print_Comm('%%%%'*6 )

# next 2 lines and the 'j+1' in the next tf.write are for 20141015 expts
#tf.write('%d\t%s\t%f\n'%(1,'F',freq1))
#tf.write('%d\t%s\t%f\n'%(1,'A',1))
for j in range(long(0),long(nsteps+1)):
 if up==1:
     fre=freq1+(j)*dfreq
 else: 
     fre=freq2-(j)*dfreq
     
 tf.write('%d\t%s\t%f\n'%(j+1,'F',fre))
# tf.write('%d\t%s\t%f\n'%(j+1,'A',1))
 
if up==1:
    fre=freq1
else:
    fre=freq2
tf.write('%d\t%s\t%f\n'%((j+2),'F',fre))
#tf.write('%d\t%s\t%f\n'%((j+2),'A',0))
tf.close()

