-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataread.py
184 lines (150 loc) · 5 KB
/
dataread.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import numpy as np
from matplotlib import rc
import matplotlib.pyplot as plt
def extract_headers_bet_spaces(filename):
f = open(filename,"r")
raw_data = f.read().split('\n')
f.close()
Row1, Headers = raw_data[0].strip(), []
while len(Row1)>1:
h = Row1.find(' ')
Headers.append(Row1[:h])
Row1 = Row1[h:].strip() + ' '
return Headers
def extract_data_columns_bet_spaces(filename):
f = open(filename,"r")
raw_data = f.read().split('\n')
f.close()
Columns, Headers = [], extract_headers_bet_spaces(filename)
for i in Headers:
Columns.append([])
for row in raw_data[1:-1]:
for col in Columns:
h = row.find(' ')
col.append(float(row[:h]))
row = row[h:].strip() + ' '
return Columns
def extract_data_columns(R_data,ncols,sep):
Columns = []
for i in range(ncols):
Columns.append([])
for col in R_data[1:-1]:
split_col = col.split(sep)
for j in enumerate(Columns):
try:
j[1].append(float(split_col[j[0]]))
except:
j[1].append(split_col[j[0]])
return Columns
##### For general datafiles with normal separation (variable 'sep') #####
def extract_data(filename,sep=' ',pheaders=True):
f = open(filename,"r")
raw_data = f.read().split('\n')
if sep == ' ':
headers = extract_headers_bet_spaces(filename)
columns = extract_data_columns_bet_spaces(filename)
#############################################################
else:
headers = raw_data[0].split(sep)
columns = extract_data_columns(raw_data,len(headers),sep)
if pheaders == True:
print(headers)
formatted_columns = []
for col in enumerate(columns):
formatted_columns.append(np.array(col[1]))
Data = {}
for h,c in zip(headers, formatted_columns):
Data.update({h:c})
return Data
##### For phantom .ev files, headers type ===> [1 XX] [2 YY] [3 ZZ] #####
def phantom_evdata(filename,pheaders=True):
f = open(filename,"r")
raw_data = f.read().split('\n')
f.close()
Row1 = raw_data[0]
ncols = len(Row1.split(']'))-1
headers, columns, = [], []
for i in Row1.split("]")[:-1]:
columns.append([])
headers.append(i.strip('#').strip().strip("[").strip().lstrip('1234567890').strip())
# l_side, r_side = Row1.find('[')+3, Row1.find(']')-1
# width_header = Row1.find('[',Row1.find(']')) - Row1.find('[')
# headers, columns, = [], []
# for i in range(ncols):
# headers.append(Row1[l_side+i*width_header:r_side+i*width_header+1].strip())
# columns.append([])
if pheaders==True:
print(headers)
for i in raw_data[1:]:
if i.strip()=='':
continue
S = i
for j in range(ncols):
S = S.lstrip()
if j < ncols-1:
try:
columns[j].append(float(S[:S.find(' ')]))
except:
columns[j].append(S[:S.find(' ')])
else:
try:
columns[j].append(float(S))
except:
columns[j].append(S)
S = S[S.find(' '):]
formatted_columns = []
for col in enumerate(columns):
formatted_columns.append(np.array(col[1]))
Data = {}
for h,c in zip(headers, formatted_columns):
Data.update({h:c})
return Data
##### This will be a new module for a personalized default plot style #####
def plot_format(xlab,ylab, labeling=False):
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
plt.xlabel(r'$' + xlab + '$', fontsize='x-large')
plt.ylabel(r'$' + ylab + '$', fontsize='x-large')
plt.tick_params(labelsize='15')
if labeling == True:
plt.legend(fontsize=15)
elif labeling == False:
pass
else:
print('Option not valid for labeling. Set as True for show.')
#Conversion of units from Phantom to cgs, day, year...
class constants:
mass = 1.989E33
time = 1.594E3
dist = 6.96E10
vel = dist/time
dens = mass/dist**3
spangmom = dist**2/time
spener = (dist/time)**2
ener = mass*spener
angmom = mass*spangmom
pressure = ener/dist**3
yr = time/(24*3600*365)
day = time/(24*3600)
hr = time/(3600)
au = 215
def __init__(self,mass=mass,time=time,dist=dist,yr=yr,day=day,
spangmom=spangmom,ener=ener,spener=spener,vel=vel,
angmom=angmom,dens=dens, pressure=pressure, au=au,hr=hr):
"""Units used in cgs"""
self.mass = mass
self.time = time
self.dist = dist
self.vel = vel
self.dens = dens
self.spangmom = spangmom
self.spener = spener
self.angmom = angmom
self.ener = ener
self.pressure = pressure
self.yr = yr
self.day = day
self.hr = hr
self.au = au