#inputs 
print 'reading in data...'
folder = "/Users/chrisfuller/Dropbox/phd/herchel/coma/final_outputs/" # input/output folder
cat_name = 'coma_supercluster_cal12.fits' #input name
cat = Table(pj(folder,cat_name))

#bands 
bands = ['500', '350', '250'] #f250_1a

#find where sep is greater than 3.5"
w_sp = where(cat.Separation_mine_npg > 3.5)[0]

#loop through bands
for band in bands:
	print 'starting ' + band

	#extract fluxes
	new_fluxes = cat['f' + band + '_1a']
	w_sn = where((new_fluxes / cat['e' + band + '_1a']) < 3.0)[0]

	#set all fluxes greater than 3.5 to 0
	new_fluxes[w_sp] = 0.0

	#set all fluxes to 0 that are less than s/n 3
	new_fluxes[w_sn] = 0.0

	#add new col to table with fluxes
	cat.add_column('NGPFLUX' + band, new_fluxes, unit='Jy', dtype=float)

cat.write(pj(folder, 'test.fits'), overwrite=True)
print 'program complete'
cat['SMASS'] = 1.0
cat['SMASS'][w] = log_mass

#extract all galaxies without colours and K band magnites
if True:
    for i in range(0, len(cat)):
        #test to see if there is already a stellar mass there
        if cat.SMASS[i] == 1.0: cat.SMASS[i] = BT2Stellar(cat.BTmag[i])
        else:
            print 'mass already present from K band and colour'
        
#load complete cols into mass and BT
mass = cat.SMASS
BT = cat.BTmag

cat.write(pj(folder,'stellar-mass-fornax_final.fits'),overwrite=True)

figM = plt.figure(figsize=(4.5, 4.5), facecolor='w', edgecolor='k')
subfi = plt.subplot(1,1,1)
#plot
subfi.plot(BT, BT2Stellar(BT), 'k')
#subfi.scatter(BT, mass, s=30, c='r', marker='+')
subfi.scatter(BT[w], mass[w], s=30, c='b', marker='+')
#subfi.xaxis_inverted()
subfi.set_ylabel('$\log_{10}$$(M_{star} / M_{\odot})$')
subfi.set_xlabel('$m_{BT}$')
subfi.set_xlim(9.8 ,15.8)
subfi.set_ylim(8.4 ,11.5)
plt.subplots_adjust(left =0.175, bottom= 0.125, hspace=0.0,wspace=0.0)
figM.savefig(pj('/Users/chrisfuller/Dropbox/phd/papers/fornax','BT2Stellarmass.pdf'))
plt.show()
#program to create dust-to-stars ratio
# Chris Fuller, April 2014

#import mods
from atpy import Table
import numpy as np
from os.path import join as pj



#Inputs
folder = "/Users/chrisfuller/Dropbox/phd/herchel/coma/final_outputs/" # input/output folder
fname = 'coma_supercluster_cal12.fits' #input name
cat = Table(pj(folder,fname))

w1 = np.where(cat.D250==1)[0]

new_col = np.array([0.0]*len(cat))

new_col[w1] = cat.DMASS[w1] - cat.SMASS[w1]

cat.add_column('DUST_STARS', new_col)
cat.write(pj(folder,'test-dust-v2.fits'))
total = cat.D500 + cat.D350 + cat.D250 + cat.D160 + cat.D100


five_cluster = cat.where((total == 5) & (cat.RADIUS_VIR <= 1.0))
five_filament = cat.where((total == 5) & (cat.RADIUS_VIR > 1.0))

""" create new columum dmass and dmass_type """

dmass, dmass_type = np.array([0.0]*len(cat)) , np.array([0]*len(cat))

w_five = np.where(total == 5)[0]
w_250 = np.where((total != 5) & (total != 0))[0]

dmass[w_five] = cat.DMASS_SED[w_five]
dmass_type[w_five] = 2
dmass[w_250]  = cat.DMASS_250[w_250]
dmass_type[w_250] = 1


cat.add_column('DMASS', dmass)
cat.add_column('DMASS_TYPE', dmass_type)

cat.write('/Users/chrisfuller/Dropbox/phd/herchel/coma/sed-fits/test.fits',  overwrite=True)

cluster = cat.





#Inputs
folder = "/Users/chrisfuller/Dropbox/phd/herchel/coma/final_outputs/" # input/output folder
ngp = Table(pj(folder, 'coma_supercluster_cal12.fits'),type='fits')

#create a kdtree
tree_npg = spatial.cKDTree(zip(ngp['GRA2000'], ngp['GDEC2000']))

def mod(x):
	return np.sqrt(x**2)

#function to caculate veldis for nth nearst neigbours
def vel_dis_n(n):
	#query kdtree
	distance, ind  = tree_npg.query(zip(ngp['GRA2000'], ngp['GDEC2000']), k=n)

	#create empty list of velocity dispersions
	vel_dis = []

	#loop through cat
	for i in range(len(ind)): vel_dis.append(np.mean(mod(ngp['velocity'][ind[i]]-7000.0)))

	#add empty col to cat
	ngp.add_column('dispersion_'+str(n), vel_dis, unit='km/s', null='', dtype=np.float)

#pool = Pool(4)
#pool.map(vel_dis_n, [5,10,20,30,40,50,100])

for n in [5,10,20,30,40,50,100]: vel_dis_n(n)

ngp.write(pj('/Users/chrisfuller/Dropbox/phd/herchel/coma/final_outputs/', 'test.fits'), overwrite=True)
for i in range(0,len(cat)): 
	#cluster
	if (vel[i] > vmin) and (vel[i] < vmax) and (rad[i] < rmax):
		cat['TYPE'][i] = 1

	#filament
	elif (vel[i] > vmin) and (vel[i] < vmax) and (rad[i] > rmax):
		cat['TYPE'][i] = 2

	#bg
	elif (vel[i] < vmin) or (vel[i] > vmax):
		cat['TYPE'][i] = 3

	else: raise 'error'

cat.write(pj(folder, outname), overwrite=True)

##### functions #####
def catsplit(val, name):
	newcat = deepcopy(cat.where(cat['TYPE'] == val))
	newcat.write(pj(folder,name), overwrite=True)

def catnot(val, name):
	newcat = deepcopy(cat.where(cat['TYPE'] != val))
	newcat.write(pj(folder,name), overwrite=True)
# split catalouge into filmament and cluster
if True:
	catsplit(1, 'coma_cluster-mgbg-130114-3.1arcsec.fits')
	catsplit(2, 'filament-mgbg-130114-3.1arcsec.fits')
	catnot(3,'cluster+filament-mybg-130114-3.1arcsec.fits')
#edit it table


#100
cat.F100[rem_100] = 0.0
cat.SN100[rem_100] = 0.0
cat.R100[rem_100] = 0.0
cat.D100[rem_100] = 0
cat.DMASS_TYPE[rem_100] = 1
cat.DMASS[rem_100]  = cat.DMASS_250[rem_100]
cat.DUST_STARS[rem_100] = cat.DMASS[rem_100] - cat.SMASS[rem_100]


#100
cat.F160[rem_160] = 0.0
cat.SN160[rem_160] = 0.0
cat.R160[rem_160] = 0.0
cat.D160[rem_160] = 0
cat.DMASS_TYPE[rem_160] = 1
cat.DMASS[rem_160]  = cat.DMASS_250[rem_160]
cat.DUST_STARS[rem_160] = cat.DMASS[rem_160] - cat.SMASS[rem_160]

#change dmass_type
#cat.DMASS_TYPE[sed_change_type] = 1
#cat.DMASS[sed_change_type]  = cat.DMASS_250[sed_change_type]
#cat.DUST_STARS[sed_change_type] = cat.DMASS[sed_change_type] - cat.SMASS[sed_change_type]


cat.write(pj(folder,"coma_supercluster_cal12_pacscorrected.fits"), overwrite=True)















"""

ecols = ["SPIRE250_e","SPIRE350_e","SPIRE500_e"]
ecols2 = ["SPIRE250_error","SPIRE350_error","SPIRE500_error"]
flux = ["SPIRE250","SPIRE350","SPIRE500"]


for col in range(0,len(ecols)):
    #cat.add_empty_column(ecols2[col],dtype=np.string_)
    for i in range(0,len(cat)):
        
        f = cat[flux[col]][i]
        e = cat[ecols[col]][i]
        
        if str(f).strip() == "-":
            print f,e," vals"
fname = 'coma_supercluster_cal12.fits' #input name
cat = Table(pj(folder,fname))



gas1 = np.nan_to_num(cat.GMASS2)
gas2 = np.nan_to_num(cat.logMsun)

gasTot = np.array([0.0]*len(cat))

#loop through col and select gas data
g1 = 0
g2 = 0
for i in range(len(cat)):
	if gas2[i] > 0.0:
		gasTot[i] = gas2[i]
		g2 += 1


	elif gas1[i] > 0.0:
		gasTot[i] = gas1[i]
		g1 += 1


print g1
print g2
cat.add_column('HI_ALL2', gasTot)
cat.write(pj(folder,'test1.fits'))