Skip to content

Commit 379318c

Browse files
Create IsochroneMakerReformatted.py
I have written a draft of the new stellar model reformatt-er. I will be testing this over the next few days. Questions I have: - Do you think that rewriting some of the stellar models files (originally in .dat) into fits file would be sufficient for the job once I try to run the extractor?
1 parent d4eb143 commit 379318c

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import numpy as np
2+
import astropy
3+
import pandas as pd
4+
from astropy.io import fits
5+
from astropy.table import Table
6+
import os
7+
import hoki
8+
import time
9+
from hoki import load
10+
import glob
11+
12+
""" Simplifies the stellar evolution models relevant to cluster into one place.
13+
May be slow later. But better than dealing with many many,
14+
Files all at once manually.
15+
colsToElim are columns whose values' significance I don't know?
16+
We eliminate those. """
17+
"""Source: https://stackoverflow.com/questions/44369504/
18+
how-to-convert-entire-dataframe-values-to-float-in-pandas"""
19+
20+
colsToElim = [x for x in range(96) if x not in [hoki.dummy_dict[y]
21+
for y in hoki.dummy_dict.keys()]]
22+
23+
24+
def reformatter(SinOrBin, model, metallicity='z020', evo_dir):
25+
"""I will expedite the process of writing and saving files by
26+
making the BPASS stellar input files binary fits tables """
27+
t1 = time.time()
28+
Entries = []
29+
dummyDic = hoki.dummy_dict
30+
entries = load.model_input(bpass_dir +
31+
'BPASSv2.2.1_' + SinOrBin + '-' +
32+
model + '/' + 'input_bpass_' +
33+
metallicity + '_' + SinOrBin +
34+
'_' + model)
35+
i = 0
36+
for x in entries.filenames:
37+
if x[0] != '.':
38+
try:
39+
f = pd.read_csv(hoki.MODELS_PATH + x, sep="\s+",
40+
header=None)
41+
f.apply(pd.to_numeric, errors='coerce')
42+
astroTable = Table.from_pandas(astroTable)
43+
if not os.path.isdir(evo_dir + "iso/" + metallicity + '/' +
44+
model + "/" + SinOrBin +
45+
"/" + "FitsModels"):
46+
os.makedirs(evo_dir +
47+
'iso/' + metallicity + '/' + model +
48+
"/" + SinOrBin + "/" + "FitsModels")
49+
f.write(evo_dir +
50+
'iso/' + metallicity+"/" + model +
51+
"/" + SinOrBin + "/" + "FitsModels/" +
52+
fileName[:-4] + ".fits", format='fits')
53+
except FileNotFoundError:
54+
print("File not Readable/Found")
55+
56+
57+
def extractor(SinOrBin, model, age, metallicity='z020', bpass_evo_dir,
58+
margin_of_logAge_error):
59+
t1 = time.time()
60+
entries = glob.glob(evo_dir +
61+
'iso/' + metallicity+"/" + model +
62+
"/" + SinOrBin + "/" + "FitsModels/*")
63+
Entries = []
64+
dummyDic = hoki.dummy_dict
65+
for x in entries:
66+
if x[0] != '.':
67+
try:
68+
f = Table.read(bpass_evo_dir)
69+
f = f.to_pandas()
70+
if f[f.columns[1]].tolist() != []:
71+
72+
# Make sure that whatever that is being put into the table
73+
# is practically the desired age
74+
f = f[(f[f.columns[1]] < 10 ** (age + 0.5)) &
75+
(f[f.columns[1]] > 10 ** (age - 0.5))]
76+
f.transpose()
77+
Entries.append(f)
78+
except FileNotFoundError:
79+
print('File Not Found')
80+
print('Onto Reducing the data')
81+
reduced = reducing(Entries)
82+
try:
83+
if not isinstance(reduced, type(None)) and not reduced.empty:
84+
reduced = Table.from_pandas(reduced)
85+
if not os.path.isdir('/g/lu/models/evolution/BPASS/v2.2/' +
86+
'iso/' + metallicity + '/'):
87+
os.makedirs('/g/lu/models/evolution/BPASS/v2.2/' +
88+
'iso/' + metallicity + '/')
89+
if not os.path.isfile('/g/lu/models/evolution/BPASS/v2.2/' +
90+
'iso/' + metallicity + '/' + str(age) +
91+
'.fits'):
92+
reduced.write('/g/lu/models/' +
93+
'evolution/BPASS/v2.2/' +
94+
'iso/' + metallicity +
95+
'/' + 'iso' + str(age) +
96+
'.fits', format='fits')
97+
return reduced
98+
except IndexError:
99+
print ('It looks like there are no stars in out input file' +
100+
' that have the specified age...')
101+
102+
103+
def reducing(lis):
104+
if lis != [] and not isinstance(lis[0], type(None)) and not lis[0].empty:
105+
temp = pd.DataFrame(lis.pop()).transpose()
106+
while lis != [] and not isinstance(lis[0], type(None)) and
107+
not lis[0].empty:
108+
popd = pd.DataFrame(lis.pop()).transpose()
109+
temp = pd.DataFrame(temp)
110+
temp = pd.concat([temp, popd], axis=0)
111+
# Make a reverse map
112+
# Citation: https://stackoverflow.com/questions/483666/reverse-invert-a
113+
# dictionary-mapping
114+
temp.drop(columns=[temp.columns[x] for x in colsToElim], inplace=True)
115+
invmap = {u: v for v, u in hoki.dummy_dict.items()}
116+
temp.rename(columns=invmap, inplace=True)
117+
for x in temp.columns:
118+
temp[x] = pd.to_numeric(temp[x], errors='coerce')
119+
return temp
120+
else:
121+
print("Empty list inputted!")

0 commit comments

Comments
 (0)