#!/usr/bin/env python
# Similar to roseplot2.py, but here the radius of the sectors 
# is adjusted so that the area of the sectors, rather than radius,
# is proportional to the relative frequency.
import sys, math
import vplot
infilename='rosebin2.dat'
bindat=open(infilename,'r')
wf=[] #will be a list of total events if an angle bin
wfs=[] #will be a list of lists of events in windspeed bins
colors=[(0.,.5,0.),(0.,.7,.7),(.5,.6,.6),(.8,.6,.4),(1.,0.,0.),(1.,1.,0.),(0.,1.,0.),(0.,1.,1.)]
sumb=0. #this will be the sum of all the bins 
#read in number of calm and directionless incidents:
calm,dirless=eval(bindat.readline())
iw=eval(bindat.readline())
#read in binned data: angle bin number, followed by events in windspeed bins 
while 1:
	aline=bindat.readline()
	if not aline: break
	i,bb=eval(aline) #
	b=reduce(lambda x, y: x+y, bb) #sums the windspeed bins, see "help(reduce)"
	wf.append(b) #list of events in each angle bin 
	wfs.append(bb) #list of lists of windspeed bins
	sumb=sumb+b #total of all windspeed bins
sumb=sumb+calm+dirless #total of all events
n=len(wf) #should be 36
mwf=max(wf)
rsize=170 #pt size for maximum "petal" length
sc=float(rsize)**2/mwf #scale factor, pts per number
da=360./n
dah=.5*da
a1=90+dah #convert meteorology angle to postscript angle
r0=10 #an integer, so unit is pts
r0=int(math.sqrt((calm+dirless)*sc/n))
#now plot using vplot:
a=vplot.eps_class()
a.scale(xmin=-1,xmax=1,ymin=-1,ymax=1)
a.linewidth(25L) #25L means .25 pts, a very thin line
for i in range(0,n):
	a2=a1
	a1=a2-da  #notice -da; meteorology is clockwise, postscript is anticlockwise
	r2=r0
	for n in range(len(wfs[i])):
		f=wfs[i][n]
		r1=r2 #last outer radius becomes current inner radius of sector
		r2=int(math.sqrt(r1**2+sc*f))
		a.color(colors[n])
		a.sector(0.,0.,r1,r2,a1,a2,'F')
#		a.color(0,0,0)
#		a.sector(0.,0.,r1,r2,a1,a2)
a.color(0,0,1.)
for i in range(1,7): #make the blue frequency circles
	rfreq=i*.02
	lab="%5.2f" % (rfreq)
	r=math.sqrt(rfreq*sumb*sc+r0**2)
	a.circle(0.,0.,int(r))
	a.text(int(a.ix(0.)-24),int(a.jy(0.)-r-10),0,12,lab)
a.text(.05j,.90j,0.,18,'blue circles are frequency per 10 degrees')
a.color(0,0,0)
a.text(.25j,.95j,0.,24,'SFO 2 m winds, 1998')
calmf=float(calm)/sumb
calmfs="   calm freq= %f" % calmf 
a.text(.1j,.05j,0.,12,calmfs)
dirlessf=float(dirless)/sumb
dirlessfs="dirless freq= %f" % dirlessf 
a.text(.1j,.02j,0.,12,dirlessfs)
x=.5j
y=.02j
dx=.07j
dy=.07j
for n in range(len(iw)):
	a.color(colors[n])
	a.rect(x+n*dx,y,x+(n+1)*dx,y+dy,'F')
	a.color()
	a.text(x+(n+1)*dx-.02j,y+dy+.01j,0.,12,str(iw[n]))
a.close()
