-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
336 lines (243 loc) · 10.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import astropy.units as u
import numpy as np
import astropy.io.fits as fits
from astropy.cosmology import FlatLambdaCDM
from astropy.table import Table
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
from sklearn.neighbors import BallTree
from sklearn.preprocessing import MinMaxScaler
import astropy.constants as const
# constants and units:
cosmoUNIT = FlatLambdaCDM(
H0=70.0 * u.km / u.s / u.Mpc,
Om0=0.30)
h = 0.70
cosmo = cosmoUNIT
z_array = np.arange(0, 7.5, 0.001) # for the redshift, from now up to z=7.5 put the intervals of 0.001
d_C = cosmo.comoving_distance(z_array) # Co-moving distance of the given intervals' redshift. IT IS NOT THE DISTANCE OF THE OBJECTS. in unit of Mpc
dc_mpc = (d_C).value # Co-moving distance of the given intervals' redshift in Mega Parsec
dc_interpolation = interp1d(z_array, dc_mpc) # find the Co-moving distance for each interval of the redshift that we defined
# Coordinate conversion:
def get_xyz(RARA, DECDEC, ZZZZ):
# from RA, DE and r => polar coordinate:
phi = ( RARA - 180 ) * np.pi / 180.
theta = ( DECDEC + 90 ) * np.pi / 180.
rr = dc_interpolation( ZZZZ )
# convert polar coordinate to cartesian:
xx = rr * np.cos( phi ) * np.sin( theta )
yy = rr * np.sin( phi ) * np.sin( theta )
zz = rr * np.cos( theta )
return np.array(list(xx)), np.array(list(yy)), np.array(list(zz))
clu = fits.open('/home/farnoosh/farnoosh/Master_Thesis/Data/eFEDS/efeds_clusters_full_20210814.fits')[1].data
GAMA = fits.open('/home/farnoosh/farnoosh/Master_Thesis/Data/GAMA/gkvScienceCatv02_mask_stellarMass.fits')[1].data
# PC MPE:
# clu = fits.open('/home/safiye/safiye/data1/eFEDS/efeds_clusters_full_20210814.fits')[1].data
# GAMA = fits.open('/home/safiye/safiye/data1/GAMA/gkvScienceCatv02_mask_stellarMass.fits')[1].data
print('files are opened')
# based on Driver et al. 2022
Z_min = 0.0
Z_max = 0.1
# properties of galaxies that we want:
# |mass of the star |the redshift must |and a redshift |science sample |normalized |the object is inside
# |must be bigger than |be bigger than 0 |smaller than 0.1 |better than 7 |redshift |of the GAMA survey
# |mass of the sun |quality
z_sel = ( GAMA['logmstar']>0 ) & (GAMA['Z']> Z_min) & (GAMA['Z']< Z_max) & (GAMA['SC']>=7) & (GAMA['NQ']>2) & ( GAMA['duplicate']==False ) & ( GAMA['mask']==False ) & ( GAMA['starmask']==False ) & ( GAMA['Tycho20Vmag10']==False ) & ( GAMA['Tycho210Vmag11'] == False ) & ( GAMA['Tycho211Vmag115']==False )& ( GAMA['Tycho2115Vmag12']==False )
f_sky = 60/(129600/np.pi) #ratio of the survey area to the total area of the celestial sphere
total_volume_G = f_sky * (cosmo.comoving_volume(Z_max) - cosmo.comoving_volume(Z_min))
total_volume_C = f_sky * (cosmo.comoving_volume(Z_max-0.02) - cosmo.comoving_volume(Z_min+0.02)) # z=0.02 ~= 0.6 Mpc; define a narrower range
#make a table from the aproperiate galaxies:
GAL = Table(GAMA[z_sel])
# properties of clusters that we want:
c_ok = (clu['z_final']>Z_min+0.02) & (clu['z_final']<Z_max-0.02)
CLU = Table(clu[c_ok])
print('number of acceptable galaxies:', len(GAL))
print('number of acceptable clusters:', len(CLU))
# get the position of the objects in cartesian coordinate
x_C, y_C, z_C = get_xyz( CLU['RA'], CLU['DEC'], CLU['z_final'])
x_G, y_G, z_G = get_xyz( GAL['RAcen'], GAL['Deccen'], GAL['Z'])
dist_C = (x_C**2+ y_C**2 + z_C**2)**0.5
dist_G = (x_G**2+ y_G**2 + z_G**2)**0.5 #Euclidean distance between the position of the galaxies in Cartesian coordinate system
# physical distance of the objects:
coord_cat_C = np.transpose([x_C, y_C, z_C])
coord_cat_G = np.transpose([x_G, y_G, z_G])
tree_G = BallTree(coord_cat_G)
tree_C = BallTree(coord_cat_C)
#if the different distance between the galaxy and the radius of the cluster is less than 1 mega parsec then it is accepted
Q1, D1 = tree_G.query_radius(coord_cat_C, r=1.7, return_distance = True)
#galaxies that Are in the cluster:
GiC = GAL[np.hstack((Q1))]
#define mass bins in "Log(M_star/M_sun)"
mbins = np.arange(4,12,0.1)
x_hist = (mbins[:-1]+mbins[1:])/2
H1 = np.histogram(GAL['logmstar'], bins=mbins)[0]
H2 = np.histogram(GiC['logmstar'], bins=mbins)[0]
#print("H1: galaxy catalouge =", H1)
print('Total number of the Glaxies from sum(H1):', sum(H1))
print('Total number of galaxies that are in the clustrs, sum(H2)):',sum(H2))
# Plot 1:
plt.title("1_ Number of Galaxies and Clusters in each mass-bin")
plt.hist(GAL['logmstar'], bins=100, color="slateblue", edgecolor="darkslateblue", label='all galaxies')[0]
plt.hist(GiC['logmstar'], bins=30, color="turquoise", edgecolor="teal", label='in cluster')[0]
plt.xlabel("Log(M/$M_{\odot}$)")
plt.ylabel("Number")
plt.yscale('log')
plt.legend()
plt.show()
plt.savefig('stellar_mass_histogram.png')
plt.clf()
def y_D22(x):
"""
Richards curve from GAMA based on Table 5, Eq. 2 from Driver et al. 2022
:param x: log10 of stellar mass limit
:return: co moving distance in Mpc
"""
A = -0.016
K = 2742.0
C = 0.9412
B = 1.1483
M = 11.815
nu = 1.691
y = A + (K - A) / (C + np.e ** (-B * (x - M))) ** (1 / nu)
return y
def inverted_y_D22(y):
"""
returns the inverted function of the richards curve
:param y: comoving distance
:return: log mass
"""
A = -0.016
K = 2742.0
C = 0.9412
B = 1.1483
M = 11.815
nu = 1.691
fraction_part = (A - K)/(A - y)
logarithm_part = np.log(fraction_part**nu - C)
x = (B * M - logarithm_part) / B
return x
def plot_adjusted_richards_curve(log_stellar_mass, logM_min, logM_max):
adjusted_richards_curve = np.zeros(len(log_stellar_mass))
for i, log_mass in enumerate(log_stellar_mass):
if log_mass < logM_min:
adjusted_richards_curve[i] = y_D22(logM_min)
elif log_mass > logM_max:
adjusted_richards_curve[i] = y_D22(logM_max)
else:
adjusted_richards_curve[i] = y_D22(log_mass)
return adjusted_richards_curve
def find_logMmin_andmax(z_min, z_max):
comoving_min, comoving_max = cosmo.comoving_distance(z_min).value, cosmo.comoving_distance(z_max).value
logm_min, logm_max = inverted_y_D22(comoving_min), inverted_y_D22(comoving_max)
return logm_min, logm_max
x_array = np.arange(0,12,0.1)
yARR = y_D22(x_array)
logm_min, logm_max = find_logMmin_andmax(z_min=0.0013, z_max=0.1)
adjusted_richards = plot_adjusted_richards_curve(log_stellar_mass=x_array, logM_min=logm_min, logM_max=logm_max)
# volume of the galaxies (V_G) and the cluster (V_C)
# co-moving volume of the mass limits based on their co-moving distances
v_G = 4 / 3 * np.pi * (y_D22(GAL['logmstar'])) ** 3
v_C = 4 / 3 * np.pi * (y_D22(GiC['logmstar'])) ** 3
# make an array of galaxy masses => then divide them to the volume of the all galaxies
Hv_G = np.histogram(GAL['logmstar'], bins=mbins, weights=np.ones_like(GAL['logmstar']) / total_volume_G.value)[0]
Hv_C = np.histogram(GiC['logmstar'], bins=mbins, weights=np.ones_like(GiC['logmstar']) / total_volume_C.value)[0]
# Plot 2: H1_V = (H1 / v_G) number density of galaxies per co-moving volume
plt.title("2_ Number-Density of galaxies and clusters in each mass-bin")
plt.step(x_hist, Hv_G, color="slateblue", label='all galaxies')
plt.step(x_hist, Hv_C, color="turquoise", label='in cluster')
plt.yscale('log')
plt.xlabel("Log(M/$M_{\odot}$)")
plt.ylabel("Number Density(Mpc^-3)")
plt.show()
plt.savefig('stellar_mass_histogram_over_volume.png')
plt.clf()
# Plot 3: CoMoving Distance based on stellar masses
plt.plot((GAL['logmstar']), (dist_G ), 'o', markersize=0.5)
plt.xlabel('Stellar Mass (Log(M/$M_{\odot}$) $h_{70}^{-2}$)')
plt.ylabel('CoMoving Distance(Mpc $h_{70}^{-1}$)')
plt.plot(x_array, adjusted_richards, 'k--')
plt.title('3_ CoMoving Distance vs Stellar Mass for All Data')
plt.xlim(0,12)
plt.ylim(0, 450)
yticks = np.arange(0, 451, 100)
plt.yticks(yticks)
plt.grid(color='gray', linestyle='dashed', alpha=0.3)
plt.show()
plt.savefig('CoMoving Distance vs Stellar Mass')
plt.clf()
#mass to light ratio:
#1) read the masses,
Ms = GAL['logmstar'] #read the mass of the stars, unit:
print("Ms[1]",Ms[1])
#2) read the flux
#3) find luminosity from flux
#4) devide mass over lominosity
#5) plot
#
# # Mass to Light ratio ( r_band):
# Ms_over_Msun = GAL['mstar']
# # three elements are non-positive, so we need to remove them, length= 23097-3 = 23094
# mask = Ms_over_Msun <= 0
# non_positive_elements = Ms_over_Msun[mask]
# remove_indices = np.where(mask)[0]
# Ms_over_Msun_corrected = np.delete(Ms_over_Msun, remove_indices)
#
#
# flux_den_r = GAL['flux_rt'] #31
# flux_den_r_corrected = np.delete(flux_den_r, remove_indices)
# #Convert flux from Jy to erg/s/cm^2
# flux_r = flux_den_r_corrected * 1.0e-23
#
# wavelength_range = 750e-7 - 600e-7 # Convert nm to cm
#
# CoMov_Gal_objects = GAL['comovingdist']
# CoMov_Gal_objects_corrected = np.delete(CoMov_Gal_objects, remove_indices)
#
# Z_corrected = np.delete(GAL['Z'], remove_indices)
#
#
# # physical distance:
# d_G_physical = CoMov_Gal_objects_corrected * (1 + Z_corrected)
#
# # luminosity distance:
# d_G_Lum_corrected = cosmo.luminosity_distance(Z_corrected)
#
# # luminosity
# lum_G = 4 * np.pi * (d_G_Lum_corrected)**2 * (flux_r * wavelength_range) #lum in erg/s
# print("lum_G[1]",lum_G[1])
#
#
#
# #lum_G_over_lSun = lum_G / const.L_sun
#
# print(lum_G)
#
# ML_ratio_solar = np.log10 ( Ms_over_Msun_corrected / lum_G_over_lSun )
#
#
# mass_mask = Ms_over_Msun_corrected < 1e10
# Ms_over_Msun_corrected_new = Ms_over_Msun_corrected[mass_mask]
# lum_G_over_lSun_new = lum_G_over_lSun[mass_mask]
# ML_ratio_massLimitted = np.log10(Ms_over_Msun_corrected_new / lum_G_over_lSun_new)
#
# average_ML_ratio = np.mean(ML_ratio_solar)
# print("Average mass to light ratio: ", average_ML_ratio)
#
# average_ML_ratio_limitted = np.mean(ML_ratio_massLimitted)
# print("Average mass to light ratio for limitted mass: ", average_ML_ratio_limitted)
#
#
# #Plot 4:
# scaler = MinMaxScaler(feature_range=(-1, 1))
# scaled_ML = scaler.fit_transform(ML_ratio_solar.reshape(-1, 1))
# scaled_MLRlim = scaler.fit_transform(ML_ratio_massLimitted.reshape(-1, 1))
#
# plt.title("4_ Mass-to-Light ratio.")
# plt.hist(scaled_ML, bins=100 , color="green", edgecolor="blue", label= "all data-3\navg= 14.76")[0]
# plt.hist(scaled_MLRlim, bins=100, color="pink", edgecolor="red", label='M < 10**10 $M_{\odot}$\navg= 14.68')[0]
# plt.xlabel("Log(M/L) [$M_{\odot}$/$L_{\odot}$]")
# plt.ylabel("Number")
# plt.legend()
# plt.show()
# plt.savefig('Mass-to-Light.png')
# plt.clf()