#!/usr/bin/env python
#   This produces the .paf and .tlan files for dds-based frequency
#   stepping programs for HF. 
#
#  Also pulsed modulation possible, i.e. on/off at each frequency step.
#
#   MTR 20151020
#
# To run it, type: python freqampsteps.py under unix or py freqphasteps.py under Windows (Python 3.5)


###### Defaults:
c= 299792458.0    # speed of light
ipp= 1800   #2730	#IPP length in s
freq1= 6.70    # 7.70 # 7.37   #6.05   #6.7    #  5.3095   #  3.93 MHz
freq2= 7.00    # 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)

array= 1       # 1, 2 or 3
basefreq= 4.040 # 6.77  # For this freq the phase offsets should be zero.
baseangle= 12  # tilt from zenith for the basefrequency
lmda= c/(basefreq*1E6)

if array==1:
           spacing=64     # row spacing in meters for array#1
elif array==2:
           spacing=48     # row spacing in meters for array#2
elif array==3:
           spacing=24     # row spacing in meters for array#3


pulse_len=0.1  # Seconds
noamp=0        # 0 for amplitude on pulses shorter than freq step interval, 1 for equal length
if pulse_len==per:
  noamp=1


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

#   calculate wavelength change required to compensate for the change in wavelength with one frequency step
#    dlambda= -c*dfreq/(basefreq*basefreq*1E6)
#calculate phase change in degrees (dp) required for each row with respect to the middle of the array
unitdp= -360*spacing/(lmda*lmda)
dp12= 5*unitdp/2 
dp34= 3*unitdp/2
dp56= unitdp/2
dp78=  -dp56
dp910= -dp34
dp1112=-dp12

#calculate phase change in degrees (dptx) required for each transmitter with respect to the row

# lengths A1     A2     A3
ltx0 =[0,0,0]
ltx1 =[395.42, 194.95, 249.80]
ltx2 =[383.42, 215,    263.05]
ltx3 =[334.17, 243.55, 283.8 ]
ltx4 =[321.12, 263.15, 297.1 ]
ltx5 =[272.96, 295.2,  322.95]
ltx6 =[258.74, 313.8,  336.2 ]
ltx7 =[208.06, 344.45, 357.8 ]
ltx8 =[192.71, 361.85, 370.95]
ltx9 =[142.85, 397.4,  392.3 ]
ltx10=[126.55, 413.8,  405.2 ]
ltx11=[81.4,   445.3,  427.2 ]
ltx12=[64.05,  461.2,  440.2 ]
#lengths=[ltx0,ltx1,ltx2,ltx3,ltx4,ltx5,ltx6,ltx7,ltx8,ltx9,ltx10,ltx11,ltx12]

dp=[0,1,2,3,4,5,6,7,8,9,10,11,12]
dp[1]= -360*ltx1[array-1]/(lmda*lmda)
dp[2]= -360*ltx2[array-1]/(lmda*lmda)
dp[3]= -360*ltx3[array-1]/(lmda*lmda)
dp[4]= -360*ltx4[array-1]/(lmda*lmda)
dp[5]= -360*ltx5[array-1]/(lmda*lmda)
dp[6]= -360*ltx6[array-1]/(lmda*lmda)
dp[7]= -360*ltx7[array-1]/(lmda*lmda)
dp[8]= -360*ltx8[array-1]/(lmda*lmda)
dp[9]= -360*ltx9[array-1]/(lmda*lmda)
dp[10]= -360*ltx10[array-1]/(lmda*lmda)
dp[11]= -360*ltx11[array-1]/(lmda*lmda)
dp[12]= -360*ltx12[array-1]/(lmda*lmda)

#calculate the total phase change in degrees
dph=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
dph[1]=dp12+dp[1]
dph[2]=dp12+dp[2]
dph[3]=dp34+dp[3]
dph[4]=dp34+dp[4]
dph[5]=dp56+dp[5]
dph[6]=dp56+dp[6]
dph[7]=dp78+dp[7]
dph[8]=dp78+dp[8]
dph[9]=dp910+dp[9]
dph[10]=dp910+dp[10]
dph[11]=dp1112+dp[11]
dph[12]=dp1112+dp[12]
dph[13]=0
dph[14]=0


#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))

def Print_Comm_secs(Command,At=-1):
	if At<0:
		tf.write(Command+'\n')
	else:	
		tf.write('AT\t%.1fs\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*,RXP1ON,RXP2ON,RXSYNC,TXSYNC,RFON*')
if noamp==1:
  for j in range(1,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()
else:
  Print_Comm_secs(' UPD*,RFOFF*',pulse_len)
  for j in range(1,nsteps):
      Print_Comm_secs('UPD*,RXSYNC,TXSYNC,RFON*',frston+(j-1)*per)
      Print_Comm_secs('UPD*,RFOFF*',frston+(j-1)*per+pulse_len)

  Print_Comm_secs('RFOFF*',frston+(j-1)*per+pulse_len)
  Print_Comm('REP',ipp)
  Print_Comm('%%%%'*5 )
  tf.close()    
  

# write .paf files for tx modules with the actual frequencies and phase offsets
pafname=tname+'.paf'
tf=open(pafname,'w')
Print_Comm('%%%%'*6 )
Print_Comm('PAFPAR_VS 3.0')
Print_Comm('%%%%    '+exp_name+' paf file for all txs  %%%%')
Print_Comm('%%%%'*6 )
# 
if noamp==1:
  for j in range(1,nsteps+1):
    if up==1:
       fre=freq1+(j-1)*dfreq
    else: 
       fre=freq2-(j-1)*dfreq
     
    dlambda=c*1E-6*((1/fre) - (1/basefreq))
    tf.write('%d\t%s\t%f\n'%(j,'F',fre))
    tf.write('%d\t%s\t%f\n'%(j,'A',1))

else:
  tf.write('%d\t%s\t%f\n'%(1  ,'F',freq2))
  tf.write('%d\t%s\t%f\n'%(1  ,'A',0))
  for j in range(1,nsteps+1):
    if up==1:
       fre=freq1+(j)*dfreq
    else: 
       fre=freq2-(j)*dfreq
     
    dlambda=c*1E-6*((1/fre)-(1/basefreq)) 
    tf.write('%d\t%s\t%f\n'%(2*j,  'F',fre))
    tf.write('%d\t%s\t%f\n'%(2*j,  'A',1))
    tf.write('%d\t%s\t%f\n'%(2*j+1,'F',fre))
    tf.write('%d\t%s\t%f\n'%(2*j+1,'A',0))

  j=nsteps+1
  if up==1:
    fre=freq1
  else: 
    fre=freq2
tf.close()

# write .paf files for the ref synthesizers with the actual frequencies
for i in range(1,3):
  pafname=tname+'m'+str(i)+'.paf'
  tf=open(pafname,'w')
  Print_Comm('%%%%'*6 )
  Print_Comm('PAFPAR_VS 3.0')
  Print_Comm('%%%%    '+exp_name+' paf file for m#'+str(i)+'  %%%%')
  Print_Comm('%%%%'*6 )

  if noamp==1:
    for j in range(1,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))

  else:
    tf.write('%d\t%s\t%f\n'%(1  ,'F',freq2))
    for j in range(1,nsteps+1):
      if up==1:
         fre=freq1+(j)*dfreq
      else: 
         fre=freq2-(j)*dfreq
       
      dlambda=c*1E-6*((1/fre)-(1/basefreq)) 
      tf.write('%d\t%s\t%f\n'%(2*j,  'F',fre))
      tf.write('%d\t%s\t%f\n'%(2*j+1,'F',fre))

    j=nsteps+1
    if up==1:
      fre=freq1
    else: 
      fre=freq2

  tf.close()
  


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