import numpy as np
import glob
import re

filelist1 = glob.glob("XrayLuminosity_512*")
#filelist2 = glob.glob("XrayLuminosity_WV_??.??.dat")
filelist1.sort(reverse=True)
#filelist2.sort(reverse=True)
#filelist3 = glob.glob("XrayLuminosity_512_?.??.dat")
#filelist4 = glob.glob("XrayLuminosity_WV_?.??.dat")
#filelist3.sort(reverse=True)

#filelist1+=filelist3

n=2
outarray=np.empty(shape=[0,3])
for name in filelist1:

  redshift=float(re.findall(r"[-+]?\d*\.\d+|\d+",name)[1])
  Xray=np.fromfile(name).reshape(512,512,512)
  Density=np.fromfile('Density_%04i_%4.1f_normal.dat'%(n,redshift)).reshape(512,512,512) 
  #Density=np.fromfile('Density_%04i_%4.2f_normal.dat'%(n,redshift)).reshape(512,512,512)

  #index = np.argwhere(Xray > 0)
  #if len(index) ==0: 
  #    n+=1
  #    continue
  xmin = 232 #index[:][:,0].min()
  xmax = 283 #index[:][:,0].max()
  ymin = 219 #index[:][:,1].min()
  ymax = 290 #index[:][:,1].max()
  zmin = 218 #index[:][:,2].min()
  zmax = 304 #index[:][:,2].max()

  Density = Density[xmin:xmax,ymin:ymax,zmin:zmax].reshape(-1)
  Xray = Xray[xmin:xmax,ymin:ymax,zmin:zmax].reshape(-1)
  Xray[Xray>0]=1.0
  z=np.ones_like(Xray)*redshift
  temp = np.array([z,Density,Xray])
  print temp.T
  outarray=np.append(outarray,temp.T,axis=0)
  #print outarray
  #Xray=np.zeros([512,512,512])
  #Xray[Density>Density_mean] = Xmean

  #np.savetxt('Density_Xray_%4.2f.dat'%redshift,outarray.T)
  n+=1

np.save('Density_Xray_exist',outarray)

