import numpy as np
import glob
import re

filelist1 = glob.glob("XrayLuminosity_512_??.??.dat")
#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=0
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)
  if n<15:
      Density=np.fromfile('Density_%04i_%4.1f_normal.dat'%(n,redshift)).reshape(512,512,512)
  else: 
      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 = index[:][:,0].min()
  xmax = index[:][:,0].max()
  ymin = index[:][:,1].min()
  ymax = index[:][:,1].max()
  zmin = index[:][:,2].min()
  zmax = 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.savetxt('Density_Xray_exist.dat',outarray)

