-
Notifications
You must be signed in to change notification settings - Fork 2
/
spectrum.py
executable file
·61 lines (49 loc) · 1.65 KB
/
spectrum.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
DESCRIPTION
definition of Spectrum class
"""
import pandas as pd
def readSpectrum(filename,colWave=0,colFlux=1,skipRows=0):
#print(filename)
df = pd.read_csv(filename,header=None,delim_whitespace=True,comment='#',skiprows=skipRows)
return Spectrum(wave=df[colWave].values,flux=df[colFlux].values,name=filename)
def saveSpectrum(filename,spectrum):
print(spectrum.wave)
if spectrum.wave is None:
print("Spectrum must have wavelength table.")
else:
saveSpec=pd.DataFrame({'wave': spectrum.wave, 'flux': spectrum.flux})
roundDict = {"wave": 4,\
"flux" : 6,\
}
saveSpec = saveSpec.round(roundDict)
with open(filename, 'w') as f:
#f.write('# wave , flux\n')
saveSpec.to_csv(f,columns=['wave','flux'],index=None,sep=' ',header=True)
class Spectrum:
def __init__(self,name=None,
wave=None,
flux=None,
lines_identification=None,
):
self.name=name
self.wave=wave
self.flux=flux
self.lines_identification=lines_identification
def __repr__(self):
return "SPECTRUM: " + str(self.name)
################################################################################
### TESTS
################################################################################
def main():
a=readSpectrum("exampleData/803432iuw.txt",skipRows=1)
print(a)
b=[]
for i in range(10):
b.append(Spectrum(name='lalal'))
for i in range(10):
print(id(b[i]))
if __name__ == '__main__':
main()