On 7/6/2015 1:29 PM, Jon Paris wrote:
Good points Buck - thanks.
Maybe you can share some of your procedural Python because everything I’ve ever seen was completely OO.
I'm part of a ham radio project which uses a PIC to record analogue
voltages from an HP141T spectrum analyser and transmits the values via a
simple terminal emulator to a text file.  The general idea is to be able
to capture a CRT image and put it in your documentation or post it on a
web page, etc.  Quite a trick for a machine which was made in the 1970s.
This program plots the values from that text file - in essence
reconstructing the analogue CRT display in a way that can be saved as a
.png or .pdf file.
The original code was written by a French radio amateur, which explains
the comments and naming convention.  It's very procedural, and yet it
uses several Python libraries like NumPy.
#       drawsan201.py: Spectrum drawing from SAN2PC rev 2.xx recorded files
#                     Roland Cordesses,F2DC, Feb.28, 2009
#
============================================================================
# Version 200 (Feb. 2009 and following days) process the new file format
generated
# by SAN2PC re v2.xx. This format stores only amplitudes as 8 bits words
# and no longer the associated frequencies information, thus greatly
improving the time to
# transfer data from SAN2PC to the PC. Moreover, this new format stores
more than
# 3 kpoints instead of 1 kpoints.
# This mitigates the amplitude errors related to the K ratio
(K=TotalScanWidth/Bandwidth
# analyzer settings) being larger than 1000 in the old SAN2PC program.
You can now use a K
# value up to 3000 without any significant error.
# 200A Computes P(dbm) from the 8 bits values of the recorded file
# 200C Computes the frequencies from fdeb,ffin, scan and nbdata read in
the header
# 200C Reads the reflev info from the file header
# 200C Includes automatic switching of freq axis in accordance with stepfreq
# 200D Should work with any Fcentr and Scan combination !!!
# 200D Fix a small issue in the frequency interpolation
# 200E Doesn't display frequency labels values on X axis but only the
Center Frequency below
# the Frequency axis
# 200F Displays Fdeb and Ffin below the frequency axis
# 200G Handles the wrong file names and file types in a more friendly
way than before !
# 200H Some minor improvements
# 200I Now the power scale is similar to the one displayed on the S.A screen
# 200J Convert filename and answer1 to lower case.
# Rev 2.00 is the first distributed beta version
# Rev 2.00.1 fixes a problem when the scanwidth unit is 'KHz'
#
# Rev 2.01 Modified to accept parameters in batch
#          First argument is file name, including extension.
#          If the first arg is specified, the program will not prompt to
save; it will
#          automatically save as .png
## I use it with a batch file that processes all the capture files
within a directory:
#          KC2HIZ
#================================================================================
# import pdb # : for debug ONLY !!!
import sys
from pylab import *
import numpy as np
from os import getcwd
cur_rep=getcwd()
from os import chdir
chdir(cur_rep)
# pdb.set_trace() # for debug ONLY !!!
# assign extensions
# for input, if prompted, assume .dat
# for output, assume .png unless prompted
# beware of some lossy formats (eg .png)!
ext= ".dat"
# if the first arg is specified, use that instead of asking for the file
name
if len(sys.argv)>1:
       nomfic1=sys.argv[1]
       ext1=".png"
       nomfic=nomfic1
       nomfic1=nomfic1+ext1
else:
       nomfic1=raw_input("File name without ext : ")
       ext1=".pdf" # .ps, .pdf, .png, .emf, .eps, .svg, .raw, .rgba
available
       nomfic=nomfic1.lower()+ext
       nomfic1=nomfic1+ext1
err=0
while (err==0):
   try:
       in_file=open(nomfic,'r')
   except:
       print("File '"+nomfic+ "' not found!!!")
       answer2=raw_input("Hit Enter to quit")
       break
       err=1
   in_file.seek(0,0)
   textdeb=in_file.read(30)
   chset=textdeb
   chset1=chset.split()
   chtst=chset1[0]# read the first character of the SAN2PC generated
file type (2 means new type)
   if chtst!="2": # it's the first rev 1.1x SAN2PC file type
       print("SAN2PC rev 1.1x file type : please use Drawsan114 ")
       answer2=raw_input("Hit Enter to quit")
       break
   else: # that's OK : it's the rev 2.xx (new) file type
       chfrc=chset1[1]
       chscan=chset1[2]
       chbw=chset1[3]
       chvideo=chset1[4]
       chreflev=chset1[5]
   lengthchfrc=len(chfrc)
   unit=chfrc[lengthchfrc-1]
   chsetfin="Fc:" +chfrc + "Hz"+ "  ScanW:" + chscan+"Hz/div"
   chsetfin=chsetfin+"  BW:"+chbw+"Hz"+"  Vid.Fltr:"+chvideo+"Hz"
   for index in range(len(textdeb)):
       if textdeb[index]=='*':
           inddeb=index+3
   long=len(chfrc)
   chfrc1=chfrc[0:long-1]
   chunitfcr=chfrc[long-1]
# computes the center frequency multiplier value from the Fcenter unit
(M, G)
   if (chunitfcr=='M'):
       fmult=1
   else:
       if (chunitfcr=='G'):
           fmult=1000
   fcentr=float(chfrc1)
# computes the scan multiplier value from the chscan unit
   long2=len(chscan)
   chcoeff=chscan[long2-1]
   if (chcoeff=='k' or chcoeff=='K'):
       mult=0.001
   else:
       if (chcoeff=='M'):
           mult=1
       else:
           if (chcoeff=='G'):
               mult=1000
   chscan1=chscan[0:len(chscan)-1]
   scan1=float(chscan1)
   scan=10*scan1
   freqratio=fmult/mult
   scan=scan/freqratio
   scan3=scan/10
   reflev=float(chreflev)
   fdeb=(fcentr-(scan/2))
   ffin=(fcentr+(scan/2))
   locs=range(10)
   for i in range (10):
       locs[i]= fdeb+i*scan3
   in_file.seek(inddeb,0)
   newtext=in_file.read()
   indexfin=newtext.index("*")
   finaltext=newtext[:indexfin]
   fichtempo=open('tempo','w')
   fichtempo.write(finaltext)
   fichtempo.close()
   in_file.seek(0,0)
   in_file.close()
   nbmajmarker=scan/10
# Automatic computing of frequency string format "majform"
   if (nbmajmarker<0.001):
       majform='%3.4f'
   else:
       if (nbmajmarker<0.006):
           majform='%3.3f'
       else :
           majform='%3.2f'
       if len(sys.argv)>1:
               answer1="y"
       else:
               answer1=raw_input("Save picture as "+ext1+" (y/n) : ")
       answer1=answer1.lower()
   fichtempo=open('tempo','r')
   X=load(fichtempo)
   s=X
   nbdata=len(s)
   stepfreq=scan/nbdata
   t=np.arange(nbdata,dtype=np.float)
   maxpower=-170
   minPower=100
   orig=800
# computes frequencies from fdeb to ffin
   for i in range(nbdata):
       t[i]=fdeb+(float(i)/float(nbdata-1))*scan # WB8DNO adds this line
to compute interpolated values.
   #  Feb 2009 fix : (nbdata-1) instead of (nbdata) for a proper
frequency estimation
# computes power in dBm
       s[i]=reflev-(orig-s[i]*4)/10
       if (s[i]>=maxpower):
          maxpower=s[i]
   nbmaj_y=10 # number of freq grid lines
   displminpower=reflev-80
   from matplotlib.ticker import MultipleLocator, FormatStrFormatter
   majLocator_y=MultipleLocator(nbmaj_y)
   minLocator_y=MultipleLocator(1)
   majorFormatter = FormatStrFormatter(majform)
   grid(True)
   ax=subplot(111)
   plot(t,s,linewidth=1)
   xlabel(chfrc+'Hz',fontsize=16)
   ylabel('dBm',fontsize=16, rotation=0)
   xlim(fdeb, ffin) # set x axis limits
   ylim(displminpower,reflev)
   xticks(locs)
   chfdeb=str(fdeb)
   chffin=str(ffin)
# Display Fdeb and Ffin
   text(fdeb-(nbmajmarker/10),displminpower-4,chfdeb+chunitfcr+'Hz')
   text(ffin-(nbmajmarker/1.5),displminpower-4,chffin+chunitfcr+'Hz')
# Axis properties
   ax.xaxis.set_major_formatter(majorFormatter)
   ax.yaxis.set_minor_locator(minLocator_y)
   ax.yaxis.set_major_locator(majLocator_y)
# No X axis labels
   setp(gca(), xticklabels=[])
# Display S.A settings
   title(chsetfin)
   if (answer1=='y'):
       savefig(nomfic1)
       if len(sys.argv)==1:
               show()
   fichtempo.close()
   err=1# the end
As an Amazon Associate we earn from qualifying purchases.