#!/usr/bin/env python

# To run it, type: python freqsteps.py

###### Defaults:
ipp=1800	#IPP length in s
freq1= 7.7000    # 7.70 # 7.37   #6.05   #6.7    #  5.3095   #  3.93 MHz
freq2= 8.0000    # 8.00 # 7.67   #6.35   #7.0    #  5.6002   #  4.23 MHz
up= 1   # up=1 means from freq1 to freq2, =-1 means freq2 to freq1
nsteps= 108    ;#96       # 108 for an 18-min stepping sequence
frston= 10  #10  #120    #s
per=10        # period per frequency, s
offtime=ipp-frston-(nsteps*per)


dfreq= (freq2-freq1)/(nsteps-1)

#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%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 2.0')
Print_Comm('%%%%    '+exp_name+' tlan file   %%%%')
Print_Comm('%%%%'*6 )
Print_Comm('AT   0.5 STMC*,RXP1ON,RXP2ON,RXSYNC,TXSYNC,RFON*')
for j in range(long(1),long(nsteps+1)):
    Print_Comm('UPD*,RXSYNC,TXSYNC',frston+(j-1)*per)
Print_Comm('RFOFF*',frston+(j-1)*per)
Print_Comm('REP',ipp)
Print_Comm('%%%%'*5 )
tf.close()


# write .frq file with the actual frequencies
freqname=tname+'.paf'
tf=open(freqname,'w')
Print_Comm('%%%%'*6 )
Print_Comm('PAFPAR_VS 2.0')
Print_Comm('%%%%    '+exp_name+' paf file   %%%%')
Print_Comm('%%%%'*6 )
#tf.write('%d\t%f\n'%(1,freq1))
for j in range(long(1),long(nsteps+1)):
 if up==1:
     fre=freq1+(j-1)*dfreq
 else: 
     fre=freq2-(j-1)*dfreq
     
 tf.write('%d\t%s\t%f\n'%(j,'F',fre))
 
j=long(nsteps+1)
if up==1:
    fre=freq1
else: 
    fre=freq2
tf.write('%d\t%s\t%f\n'%(j,'F',fre))

tf.close()

# Note that the tarlan file is written separately: trivial.
#freqname=tname+'dum.frq'
#tf=open(freqname,'w')
#Print_Comm('%%%%'*6 )
#Print_Comm('FRQPAR_VS 1.0')
#Print_Comm('%%%%    '+exp_name+'dummy frq file   %%%%')
#Print_Comm('%%%%'*6 )
#fre=freq1
#tf.write('%d\t%f\n'%(1,fre))
#tf.write('%d\t%f\n'%(1,fre))
#tf.write('%d\t%f\n'%(1,fre))
#tf.close()



# d=os.popen('tarlan -f '+tlanname).read()
