-
Notifications
You must be signed in to change notification settings - Fork 0
/
platefile.py
executable file
·148 lines (130 loc) · 5 KB
/
platefile.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
class platefile(object):
def __init__(self, file):
self.file=file
self.parseFile()
def __eq__(self,other):
return self.file==other.file
def parseFile(self):
pass
def getSetup(self, setup):
return self.setups[setup]
def getnSetups(self):
return len(self.setups)
def getSetupNames(self):
return self.setups.keys()
def getLineNofSetup(self,n,setup):
if len(self.setups[setup]['setup_lines']) > n:
return self.setups[setup]['setup_lines'][n]
else:
return ''
class ascfile(platefile):
"""Loads and parses files with lines of format
-1.1706 -4.7180 -0.2321 0.1735 O B-01-01 B
word[0:3] plate coords
word[3] hole diameter
word[4] object type
word[5] oldFiber
word[6:] additional info
"""
def __init__(self, file):
platefile.__init__(self, file)
def parseFile(self):
self.setups={}
self.fid_thumb_lines=[]
with open(self.file,"r") as fin:
for line in fin:
words=line.split()
if words[0]=='Setup':
currsetup=words[0]+' '+words[1]
self.setups[currsetup]={'setup_nfo_str':[line],
'setup_lines':[]}
elif words[5][-2:]!='17':
self.setups[currsetup]['setup_lines'].append(line)
if currsetup=='Setup 1' and words[4] in 'FT':
self.fid_thumb_lines.append(line)
else:
self.seventeen=line
for s in self.setups:
self.parseSetup(self.setups[s])
def parseSetup(self,setup):
setup['nAqusition']=0
setup['nGuide']=0
setup['nScience']=0
for line in setup['setup_lines']:
words=line.split()
if (words[4] == 'O' or words[4]=='S'):
#Line is for a science fiber
setup['nScience']+=1
elif words[4]=='G':
#Line is for a guide fiber
setup['nGuide']+=1
elif words[4]=='A':
#Line is for an acquisition star
setup['nAqusition']+=1
else:
#Line is either F, T, or R, all are unused holes
pass
def getLineNumOfHole(self, hole, setup):
return self.setups[setup]['setup_lines'].index(hole.idstr)
def getLineOfHole(self, hole, setup):
linenum=self.getLineNumOfHole(hole, setup)
return self.getLineNofSetup(linenum, setup)
def writeWithChannels(self, colorforlessthan129='B'):
import os
os.rename( self.file, self.file+"~")
with open( self.file, "w" ) as fout:
for s in sorted(self.setups.keys()):
fout.write(self.setups[s]['setup_nfo_str'][0])
if s == 'Setup 1':
fout.write(self.seventeen)
if self.setups[s]['nScience'] < 129:
for i,l in enumerate(self.setups[s]['setup_lines']):
if i < self.setups[s]['nScience']:
fout.write(l[0:-1]+' '+colorforlessthan129+'\n')
else:
fout.write(l)
else:
for i,l in enumerate(self.setups[s]['setup_lines']):
if i < self.setups[s]['nScience']:
if i%2:
fout.write(l[0:-1]+' B\n')
else:
fout.write(l[0:-1]+' R\n')
else:
fout.write(l)
class resfile(platefile):
"""Loads and parses files with lines of format
B-01-01 01 08 10.42 -72 54 25.6 2000.0 O F00- 806 I=19.64
word[0] oldFiber
word[1:8] sky coords
word[8] object type
word[9:] additional info
"""
def __init__(self, file):
platefile.__init__(self, file)
def parseFile(self):
self.setups={}
with open(self.file,"r") as fin:
i=0
currsetup=1
for line in fin:
if i==0:
self.setups['Setup %d'%currsetup]={'setup_nfo_str':[line],
'setup_lines':[]}
i+=1
elif i<3:
self.setups['Setup %d'%currsetup]['setup_nfo_str'].append(line)
i+=1
else:
#is a hole line or end
words=line.split()
if words[0] == 'END':
i=0
currsetup+=1
else:
if words[0][-2:]!='17':
self.setups['Setup %d'%currsetup]['setup_lines'].append(line)
else:
self.seventeen=line
def prune(self, lineid):
pass