#!/bin/env /usr/bin/python

import sys, math

def main (args):

        nargs = len (args)
        if nargs == 2:
                input_file = (args[0])
		output_file = (args[1])
        else:
                sys.stderr.write ('usage: convert_rpt_to_smith input output\n')
                sys.exit (1)

	rpt = open(input_file, "r")
	smith = open(output_file, "w")

	line = rpt.readline()
	while line <> '':
	   for s in range(0,len(line)):
	     if line[s]==' ':
	       freq_index = s
	       break
	   for s in range(freq_index+1,len(line)):
	     if line[s]==' ':
	       mag_index = s
	       break
	   for s in range(mag_index+1,len(line)):
	     if line[s]==' ':
	       phase_index = s
	       break

	   freq=float(line[0:freq_index])
	   mag=float(line[freq_index+1:mag_index])
	   phase=float(line[mag_index+1:phase_index])
	   sphase=float(line[phase_index+1:len(line)])

	   if (phase <= 10.) & (phase >= -10.): # weed out bad phase measure
           # output for smith chart. Convert magnitude and angle into rect
           # coords -1 to 1
	     x_pos = .15*mag*phase/10.
	     y_pos = .15*mag*sphase/10.
	     x_pos = .15*mag*phase/10.
	     y_pos = .15*mag*sphase/10.
             smith.write(str(freq)+' '+str(x_pos)+'  '+str(y_pos)+'\n')
	   line = rpt.readline()


	rpt.close()
	smith.close()

if __name__ == '__main__':
        main (sys.argv[1:])

