|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Fri Jun 21 19:29:57 2024 |
| 4 | +
|
| 5 | +@author: DELL |
| 6 | +""" |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | +import matplotlib.pyplot as plt |
| 11 | + |
| 12 | +def z(x,y): |
| 13 | + return np.abs((x-y)/y) |
| 14 | + |
| 15 | +df=pd.read_csv('data.csv') |
| 16 | + |
| 17 | +print(df) |
| 18 | + |
| 19 | +#angular spread |
| 20 | +a=df['Angular Spread'] |
| 21 | + |
| 22 | +#distance from earth in megaparsec Mpc |
| 23 | +dist=[] |
| 24 | +for i in range (0,len(a)): |
| 25 | + dist.append(22/a[i]) |
| 26 | +print (dist) |
| 27 | + |
| 28 | +#wavelength of Ca-K line |
| 29 | +lk=df['L-K'] |
| 30 | +lk_ori=3933.7 |
| 31 | +c=3*10**8 |
| 32 | +#redshift calculation |
| 33 | +z_lk=[] |
| 34 | +for i in range (0,len(lk)): |
| 35 | + z_lk.append(z(lk[i],lk_ori)) |
| 36 | + |
| 37 | + |
| 38 | +#wavelength of Ca-H line |
| 39 | +lh=df['L-H'] |
| 40 | +lh_ori=3968.5 |
| 41 | +#redshift calculation |
| 42 | +z_lh=[] |
| 43 | +for i in range (0,len(lh)): |
| 44 | + z_lh.append(z(lh[i],lh_ori)) |
| 45 | + |
| 46 | +#wavelength of H-alpha line |
| 47 | +la=df['L-alpha'] |
| 48 | +la_ori=6562.8 |
| 49 | +#redshift calculation |
| 50 | +z_la=[] |
| 51 | +for i in range (0,len(la)): |
| 52 | + z_la.append(z(la[i],la_ori)) |
| 53 | + |
| 54 | +#average redshift |
| 55 | +red=[] |
| 56 | +for i in range (0,len(la)): |
| 57 | + term=(z_lk[i]+z_lh[i]+z_la[i])/3.0 |
| 58 | + red.append(term) |
| 59 | + |
| 60 | +#average velocity |
| 61 | +vel=[] |
| 62 | +for i in range (0,len(red)): |
| 63 | + vel.append(red[i]*c) |
| 64 | + |
| 65 | +#fitting the data in a linear form |
| 66 | +coeff=np.polyfit(dist,vel,1) |
| 67 | +fit=np.poly1d(coeff) |
| 68 | +x_fit=np.linspace(np.min(dist),np.max(dist),1000) |
| 69 | +y_fit=fit(x_fit) |
| 70 | +print (coeff) |
| 71 | + |
| 72 | +plt.plot(x_fit,y_fit) |
| 73 | +plt.plot(dist,vel,"*") |
| 74 | +#creating dictionary of new columns |
| 75 | +new={'Distance':dist,'Redshift of L-K':z_lk, 'Redshift of L-H':z_lh, 'Redshift of L-alpha':z_la, |
| 76 | + 'Average redshift':red, 'Average velocity':vel} |
| 77 | + |
| 78 | +#adding the new columns to the DataFrame |
| 79 | +for column_name, column_data in new.items(): |
| 80 | + df[column_name] = column_data |
| 81 | + |
| 82 | +#saving the updated DataFrame back to the same CSV file |
| 83 | +df.to_csv('data.csv', index=False) |
| 84 | + |
0 commit comments