#Loop over z
for i in range(z_min,len(z_arr)):
    print 'z = %.3f'% (z_arr[i])
    #density_file = ''.join(glob.glob(density_path+'%.3fn_all.dat'%(z_arr[i])))
    #if not density_file:
    #    density_file = ''.join(glob.glob(density_path+'%.3fn_all.dat'%(z_arr[i-1])))
    #print 'Desnity file = %s' % density_file
    #dfile = c2t.DensityFile(density_file)

    temper_file = ''.join(glob.glob(xfrac_path+'Temper3D_%.3f.bin'%(z_arr[i])))
    print 'Temperature file = %s' % temper_file
    
    tfile = c2t.TemperFile(temper_file)
   
    T_mean = tfile.temper.mean()
    T_rms = c2t.rootmeansquare(tfile.temper)
    T_var = np.mean(pow(tfile.temper-T_mean,2))
#avg_mG = c2t.mass_weighted_mean_xi(ifile.irate,dfile.raw_density)

    print 'min T = ',tfile.temper.min(),' and max T = ',tfile.temper.max()
    print 'T_CMB = ',2.725*(1+z_arr[i])
    print 'z, avg_vG, rms = %.3f %.3f %.4e' % (z_arr[i], T_mean, T_rms)
    out.write('%.3f %.4f %.4e\n' % (z_arr[i], T_mean, T_rms))

    log_bins = np.logspace(1.,5.,100)
    T_hist, T_bin_edges = np.histogram(tfile.temper,bins=log_bins,density=True)
    T_bins = np.zeros(len(T_hist))

    for j in range(0,len(T_hist)):
        T_bins[j] = (T_bin_edges[j+1] - T_bin_edges[j])/2 + T_bin_edges[j]
        pdf_out.write('%.4f %.4e %.4e %.4e\n' % (T_bins[j], T_hist[j], T_mean, T_var))
    print 'mesh = ', mesh

    dT_box = c2t.calc_dt(xfile, dfile, xfile.z)
    dT_file = './dT_boxes/dT_%.3f.cbin' % (z_arr[i])
    print 'dT_box file = %s' % dT_file
    
    #rho = dfile.cgs_density
    #print 'rho_crit*OmegaB',c2t.const.rho_crit_0*c2t.const.OmegaB#*(1+z_arr[i])**3
    #print 'rho.mean()',rho.mean()
    c2t.save_cbin(dT_file, dT_box, bits=64, order='F')

    dT_pv_box = c2t.get_distorted_dt(dT_box, kms, xfile.z, num_particles=40)
    dT_pv_file = './dT_pv_boxes/dT_pv_%.3f.cbin' % (z_arr[i])
    print 'dT_pv_box file = %s' % dT_pv_file
    c2t.save_cbin(dT_pv_file, dT_pv_box, bits=64, order='F')

    dT_mean = dT_box.mean()
    dT_rms_box = np.sqrt((dT_box - dT_mean)**2)
    dT_rms = dT_rms_box.mean()
    dT_rms_other = c2t.rootmeansquare(dT_box)
    dT_pv_mean = dT_pv_box.mean()
    dT_pv_rms_box = np.sqrt((dT_pv_box - dT_pv_mean)**2)
    dT_pv_rms = dT_pv_rms_box.mean()

    print 'z, nu, dT_mean, dT_rms %.3f %.3f %.4f %.4f %.4f' % (z_arr[i], nu[i], dT_mean, dT_rms, dT_rms_other)
    out.write('%.3f %.3f %.4f %.4f\n' % (z_arr[i], nu[i], dT_mean, dT_rms))
    out1.write('%.3f %.3f %.4f %.4f\n' % (z_arr[i], nu[i], dT_pv_mean, dT_pv_rms))

out.close()
out1.close()
# Loop over z and read in dT. Calculate away!
for i in range(z_min,len(z_arr)):
    print 'z = %.3f'% (z_arr[i])
    dT_file = ''.join(glob.glob('./dT_boxes/dT_%.3f.cbin' % (z_arr[i])))
    print 'dT file = %s' % dT_file
    dT_box = c2t.read_cbin(dT_file, bits=64, order='F')

    dT_rsd_file = ''.join(glob.glob('./dT_pv_boxes/dT_pv_%.3f.cbin' % (z_arr[i])))
    print 'Distorted dT file = %s' % dT_rsd_file
    dT_rsd_box = c2t.read_cbin(dT_rsd_file, bits=64, order='F')

    dT_mean = dT_box.mean()
    #print dT_mean
    dT_rsd_mean = dT_rsd_box.mean()
    #print dT_mean, dT_rsd_mean
    dT_rms = c2t.rootmeansquare(dT_box)
    dT_rsd_rms = c2t.rootmeansquare(dT_rsd_box)
    dT_skew = c2t.skewness(dT_box)
    dT_rsd_skew = c2t.skewness(dT_rsd_box)
    dT_kurt = kurtosis(dT_box.flatten())
    dT_rsd_kurt = kurtosis(dT_rsd_box.flatten())
    #print dT_skew, dT_rsd_kurt
    print 'z, nu, dT_mean, dT_rms %.3f %.3f %.4f %.4f %.4f' % (z_arr[i], nu[i], dT_mean, dT_rms, dT_kurt)
    #print 'z, nu, dT_mean, dT_rms %.3f %.3f %.4f %.4f' % (z_arr[i], nu[i], dT_rsd_mean, dT_rsd_rms)

    # Set the bandwidth and convert to cells
    nu_low = c2t.z_to_nu(z_arr[i]) - dnu/2
    nu_high = c2t.z_to_nu(z_arr[i]) + dnu/2
    z_low = c2t.nu_to_z(nu_high)
    z_high = c2t.nu_to_z(nu_low)
    n_cells = (c2t.z_to_cdist(z_high) - c2t.z_to_cdist(z_low))/c2t.conv.LB*len(dT_box)