import numpy as np
import glob
import matplotlib
matplotlib.use('Agg')
import pylab as P

M1600=[]
for i in xrange(4000):
 if glob.glob('halo%05i.out'%i) ==  []:continue
 f=open('halo%05i.out'%i,'r')
 MAB=[]
 WL=[]
 for line in f:
    MAB.append(float(line.split()[4]))
    WL.append(float(line.split()[0]))
 f.close()
 WL=np.array(WL);MAB=np.array(MAB) 
 Mtemp,=(MAB[WL==1595.0]+MAB[WL==1605.0])/2
 M1600.append(Mtemp)

xmin = -21
xmax = -1
bin = 20
n, bins, patches = P.hist(M1600, bin,range=(xmin,xmax), normed=0, histtype='step',log=1) 
f=open('M1600.txt','w')
conv=1.0/(xmax-xmin)*bin/(6*6*6.125)
for M,ni in zip(bins,n):
    f.write(str(M)+" "+str(ni*conv)+"\n") 

f.close()
   
