#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# generated by wxGlade 0.3.5.1 on Tue Nov 15 14:15:58 2005

import wx, sys, math, Gnuplot
from gnuradio.wxgui import powermate
from gnuradio import eng_notation
from optparse import OptionParser

ID_EXIT = wx.NewId()

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        
        # Menu Bar
        self.frame_1_menubar = wx.MenuBar()
        self.SetMenuBar(self.frame_1_menubar)
        self.File = wx.Menu()
        self.Exit = wx.MenuItem(self.File, ID_EXIT, "Exit", "", wx.ITEM_NORMAL)
        self.File.AppendItem(self.Exit)
        self.frame_1_menubar.Append(self.File, "File")
        # Menu Bar end
        self.panel_1 = wx.Panel(self, -1)
        self.panel_2 = wx.Panel(self, -1)
        self.panel_3 = wx.Panel(self, -1)
        self.label_1 = wx.StaticText(self, -1, "Frequency:  ")
        self.text_ctrl_1 = wx.TextCtrl(self, -1, "")
        self.panel_5 = wx.Panel(self, -1)
        self.panel_6 = wx.Panel(self, -1)
        self.panel_7 = wx.Panel(self, -1)
        self.panel_8 = wx.Panel(self, -1)
        self.label_2 = wx.StaticText(self, -1, "  Gamma real   ")
        self.text_ctrl_2 = wx.TextCtrl(self, -1, "")
        self.label_3 = wx.StaticText(self, -1, "  Gamma imag  ")
        self.text_ctrl_3 = wx.TextCtrl(self, -1, "")
        self.label_4 = wx.StaticText(self, -1, "     R / Zo")
        self.text_ctrl_4 = wx.TextCtrl(self, -1, "")
        self.label_5 = wx.StaticText(self, -1, "    X / Zo")
        self.text_ctrl_5 = wx.TextCtrl(self, -1, "")
        self.label_6 = wx.StaticText(self, -1, "  R (Zo = 50)")
        self.text_ctrl_6 = wx.TextCtrl(self, -1, "")
        self.label_7 = wx.StaticText(self, -1, "     L or C")
        self.text_ctrl_7 = wx.TextCtrl(self, -1, "")

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

        parser = OptionParser ()
        parser.add_option ("-f", "--file", type="string", default='',
                           help="Input data file to display")
        (options, args) = parser.parse_args ()

	self.pm = powermate.powermate(self)

        wx.EVT_MENU(self, ID_EXIT, self.MenuExit)
        powermate.EVT_POWERMATE_ROTATE(self, self.on_rotate)

	# system impedance
	self.Zo = 50.

        self.g = Gnuplot.Gnuplot()
        self.g('set size square')
        self.g('set time')
        self.g('set clip')
        self.g('set xtics axis nomirror')
        self.g('set ytics axis nomirror')
        self.g('unset grid')
        self.g('unset polar')
        self.g('set title "Primitive Smith Chart"')
        self.g('unset key')
        self.g('set xlabel "Impedance or Admittance Coordinates"')
        self.g('set para')
        self.g('set rrange [-0 : 10]')
        self.g('set trange [-pi : pi]')
        self.g('set xrange [-1:1]')
        self.g('set yrange [-1:1]')
        self.g('tv(t,r) = sin(t)/(1+r)')
        self.g('tu(t,r) = (cos(t) +r)/(1+r)')
        self.g('cu(t,x) = 1 + cos(t)/x')
        self.g('cv(t,x) = (1+ sin(t))/x')


        fp = open("plot_data","w")
        fp.write('0. 0.')
        fp.close()

        self.g('plot cu(t,.1),cv(t,.1),cu(t,.1),-cv(t,.1),cu(t,1),cv(t,1),cu(t,1),-cv(t,1),cu(t,10),cv(t,10),cu(t,10),-cv(t,10),tu(t,.1),tv(t,.1),tu(t,.5),tv(t,.5),tu(t,1),tv(t,1),tu(t,5),tv(t,5),tu(t,10),tv(t,10),cu(t,.5),cv(t,.5),cu(t,.5),-cv(t,.5),tu(t,0),tv(t,0),"plot_data"')


	if options.file == '':
	   print "No input file\n"
	else:
	   file_fh = open(options.file,"r")

	   self.data = []
           sep=' '
	   self.i = 0
           in_line = file_fh.readline()
           while in_line <> '': 

              # parse a line of uncompensated measured data
              field = in_line[0:in_line.find(sep)]
              self.data.append(float(field))
              in_line = in_line[len(field)+1:len(in_line)]
              field = in_line[0:in_line.find(sep)]
              self.data.append(float(field))
              in_line = in_line[len(field)+1:len(in_line)]
              field = in_line[0:in_line.find(sep)]
              self.data.append(float(field))
              in_line = in_line[len(field)+1:len(in_line)]
              self.data.append(float(in_line))
	      in_line = file_fh.readline()

           self.text_ctrl_1.SetValue(eng_notation.num_to_str(self.data[self.i]))


    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("Smith Data Display")
        self.text_ctrl_1.SetSize((120, 25))
        self.text_ctrl_7.SetSize((140, 25))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_2 = wx.FlexGridSizer(3, 4, 0, 0)
        grid_sizer_1 = wx.GridSizer(3, 3, 0, 0)
        grid_sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.panel_2, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.panel_3, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.label_1, 0, wx.ALIGN_RIGHT|wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.text_ctrl_1, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_1.Add(self.panel_5, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.panel_6, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.panel_7, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.panel_8, 1, wx.EXPAND, 0)
        sizer_1.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        grid_sizer_2.Add(self.label_2, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.text_ctrl_2, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.label_3, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.text_ctrl_3, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.label_4, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.text_ctrl_4, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.label_5, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.text_ctrl_5, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.label_6, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.text_ctrl_6, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.label_7, 0, wx.FIXED_MINSIZE, 0)
        grid_sizer_2.Add(self.text_ctrl_7, 0, wx.FIXED_MINSIZE, 0)
        sizer_1.Add(grid_sizer_2, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        sizer_1.SetSizeHints(self)
        self.Layout()
        # end wxGlade

    def MenuExit(self, event):
        self.Close()

    def on_rotate(self, evt):
	self.i += evt.delta
	if self.i < 0 : self.i = 0
	if self.i * 4 > ( len(self.data) - 1 ) : self.i -= evt.delta
	freq = self.data[self.i*4]
	self.text_ctrl_1.SetValue(eng_notation.num_to_str(freq))
        x_pos = self.data[self.i*4+1]*self.data[self.i*4+2] # mag * phase
        y_pos = self.data[self.i*4+1]*self.data[self.i*4+3] # mag * sphase
	x_pos_str = str(x_pos)
	y_pos_str = str(y_pos)
	self.text_ctrl_2.SetValue(x_pos_str[0:5])
	self.text_ctrl_3.SetValue(y_pos_str[0:5])
	Rn = (1.-x_pos**2-y_pos**2)/(1.+x_pos**2-2.*x_pos+y_pos**2)
	Xn = (2.*y_pos)/(1.+x_pos**2-2.*x_pos+y_pos**2)
	Rn_str = str(Rn)
	Xn_str = str(Xn)
	self.text_ctrl_4.SetValue(Rn_str[0:5])
	self.text_ctrl_5.SetValue(Xn_str[0:5])
	R = self.Zo * Rn
        X = self.Zo * Xn
        if X < 0 : # capacitive, leave - sign to indicate capacitive
            LC = 1. / ( 2. * math.pi * freq * X )
        else:      # inductive
            LC = X / ( 2. * math.pi * freq )
	R_str = str(R)
	self.text_ctrl_6.SetValue(R_str[0:5])
	self.text_ctrl_7.SetValue(str(LC))
        point = complex(x_pos,y_pos)
        fp = open("plot_data","w")
        fp.write('0. 0.\n')
        fp.write(str(x_pos)+' '+str(y_pos))
        fp.close()
        self.g('plot cu(t,.1),cv(t,.1),cu(t,.1),-cv(t,.1),cu(t,1),cv(t,1),cu(t,1),-cv(t,1),cu(t,10),cv(t,10),cu(t,10),-cv(t,10),tu(t,.1),tv(t,.1),tu(t,.5),tv(t,.5),tu(t,1),tv(t,1),tu(t,5),tv(t,5),tu(t,10),tv(t,10),cu(t,.5),cv(t,.5),cu(t,.5),-cv(t,.5),tu(t,0),tv(t,0),"plot_data" with lines')


# end of class MyFrame

class MyApp(wx.App):
  def OnInit(self):
    frame = MyFrame(None, -1, "Automatic Testing")
    frame.Show(True)
    self.SetTopWindow(frame)
    return True

app = MyApp(0)
app.MainLoop()

