-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.py
More file actions
144 lines (109 loc) · 3.21 KB
/
reader.py
File metadata and controls
144 lines (109 loc) · 3.21 KB
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
import numpy as np
class File:
'''
Loads a data parameters file and let you querry the parameters and values.
Useful to read tep files and run files.
SYNTAX:
filedata = File(file)
PURPOSE:
To read, parse, and return the contents of a tep file
in a 2D list form.
INPUTS:
file:
OUTPUTS:
RESTRICTIONS:
This class automatically interprets the type of the values. If they
can be cast into a numeric value retuns a numeric value, otherwise
returns a string.
The parameter 'tepfile' must be of base class FILE with READ privledges!
This can be obtained by using the built in function 'open'.
Input MUST be a text file in tep format (.tep). A similar algorithm
can be used to read any text file, only requiring a few modest changes
to this function.
SIDE EFFECTS:
Examples:
--------
>>> import reader as rd
>>> reload(rd)
>>>
>>> t = rd.File('wasp18b.ev')
>>> t.listparams()
>>> t.getvalue('planet')
>>> t.getvalue('npos')
>>> t.getvalue('photap')
>>> t.getvalue('sigma')
>>> t.getvalue('aorname')
>>> t.getstr('aorname')
>>>
>>> t.getstr('nnod')
>>> t.getvalue('nnod')
>>> tep = rd.File('wasp18b.tep')
>>> tep.getvalue('Rs')
>>> tep.getvalue('RA')
MODIFICATION HISTORY:
2010-03-08 Patricio Cubillos, UCF Modified from CCampo version
pcubillos@fulbrightmail.org
2009-01-02 0.1 Christopher Campo, UCF Initial Version
ccampo@gmail.com
'''
def __init__(self, file):
# List for the parameter and values
self.params = np.array([])
self.values = []
# Read the file
file = open(file, 'r')
lines = file.readlines()
file.close()
for line in lines:
try:
line = line[0:line.index('#')].strip()
except:
line = line.strip()
if len(line) > 0:
self.params = np.append(self.params, line.split()[0])
self.values.append( np.array(line.split()[1:]) )
def evaluate(self, value):
'''
determines if the value is a numeric expression,
if not, return a string.
if it is, returns the numeric value.
'''
try:
v = eval(value)
return v
except:
return value
def checkpar(self, par):
'''
check if the input parameter exists.
If it does, returns the reference to the values,
If not, returns NaN.
'''
try:
id = np.where(self.params == par)[0]
value = self.values[id]
return value[0] if value.size == 1 else value
except:
return np.nan
def getvalue(self, par):
'''
Get the values of a parameter, if it has more than one value,
returns an array, if not, returns the value.
'''
val = self.checkpar(par)
if val is np.nan:
return np.nan
if val.size > 1:
value = [] # list that contains the values
for i in np.arange(val.size):
value.append( self.evaluate(val[i]) )
return np.array(value)
else:
value = self.getvalue(val)
return self.evaluate(val) if np.isnan(value) else value
def getstr(self, par):
'''
Get the values of a parameter as strings.
'''
val = self.checkpar(par)
return val