-
Notifications
You must be signed in to change notification settings - Fork 1
/
miriadClasses.py
230 lines (207 loc) · 6.63 KB
/
miriadClasses.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import struct
import miriad_functions as mfunc
"""
Module for classes which interact with miriad files
Current Version (this file): 1.1
Released with pipeline version: 1.1
Author: D. N. Friedel
"""
# class for data/description object
class ValueDescPair :
def __init__(self,data,description,type) :
self.data_ = data
self.description_ = description
self.type_ = type
# class for holding a miriad image header
class Header :
def __init__(self) :
self.data_ = dict()
def add(self,item,pair) :
""" Method to add a item/value pair
input :
item - the item to add
pair - the value to add
returns :
none
"""
if(isinstance(pair,ValueDescPair)) :
self.data_[item] = pair
def getValue(self,item) :
""" Method to return the value of a set
input :
item - the name of the item to return
returns :
the data value of the requested item
"""
if(item in self.data_) :
return self.data_[item].data_
return -1
def getDesc(self,item) :
""" Method to return the description of the item
input :
item - the item whose description is to be returned
returns :
the description of the item
"""
if(item in self.data_) :
return self.data_[item].description_
return ""
def getType(self,item) :
""" Method to return the type of the item
input :
item - the name of the item whose type is to be returned
returns :
the type of the item (real, int, etc)
"""
if(item in self.data_) :
return self.data_[item].type_
return "none"
def printHeader(self) :
""" Method to print the full header
input :
none
returns :
none
"""
for item in self.data_ :
print item,": ", self.data_[item].data_,self.data_[item].description_,self.data_[item].type_
# class for holding a miriad image
class Image :
def __init__(self,header = None,image = None, mask = None,pointings = [0.0,0.0]) :
if(isinstance(header,Header)) :
self.header = header
self.image = image
self.mask = mask
self.pointings = pointings
def setHeader(self,header) :
""" Method to set the header
input :
header - the image header
returns :
none
"""
self.header = header
def setImage(self,image,mask = None) :
""" Method to set the image
inputs :
image - the image array
mask - the mask binary array
returns :
none
"""
self.image = image
self.mask = mask
def getHeader(self) :
""" Method to return the image header
input :
none
returns :
the header
"""
return self.header
def getImage(self) :
""" Method to return the image and mask
input :
none
returns :
the image and mask as a tuple
"""
return self.image,self.mask
def getHeaderValue(self,item) :
""" Method to return a specific header value
input :
item - the item to return
returns :
the value of the item
"""
return self.header.getValue(item)
def getHeaderDesc(self,item) :
""" Method to get the description of a specific header item
input :
item - the name of the item to return
returns :
the decription of the item
"""
return self.header.getDesc(item)
# class to read in a miriad image to a python array
class ImageReader :
def __init__(self,file,cutoff) :
""" Initializer
file - the name of the file to read in
cutoff - only read in values above this one
"""
handle = mfunc.xyopen(file,"old")[0]
self.header = mfunc.getHeader(handle)
mfunc.xyclose(handle)
self.handle = open(file + "/image")
img = self.handle.read(4)
self.size = struct.unpack(">l",img)[0]
self.x = self.header.getValue("naxis1")
self.y = self.header.getValue("naxis2")
self.cutoff = cutoff
self.type = ">"
if(self.size == 4) :
for i in range(0,self.x) :
self.type += "f"
elif(size == 8) :
for i in range(0,self.x) :
self.type += "d"
def setHeader(self,header) :
""" Method to set the header
input :
header - the iamge header
returns :
none
"""
self.header = header
def getHeader(self) :
""" Method to return the image header
input :
none
returns :
the image header
"""
return self.header
def getHeaderValue(self,item) :
""" Method to return a specific header value
input :
item - the name of the item to query
returns :
the value of item
"""
return self.header.getValue(item)
def getHeaderDesc(self,item) :
""" Method to return the description of a specific header item
input :
item - the item to query
returns :
the description of the item
"""
return self.header.getDesc(item)
def getRow(self,plane,row) :
""" Method to read in a miriad image row
input :
plane - the plane to read
row - the row to read
returns :
a list containing the row data
"""
self.handle.seek(plane*self.size*self.x*self.y+self.size + self.size*row*self.x)
rawData = self.handle.read(self.size*self.x)
data = list(struct.unpack(self.type,rawData))
for d in range(0,len(data)) :
if(data[d] < self.cutoff) :
data[d] = 0.0
return data
def imhead(file,key) :
""" Method to read a specific header value from an image
input :
file - the name of the miriad image to read
key - the name of the header item
returns :
the value of the item
"""
handle,num = mfunc.xyopen(file,"old")
desc,typeX,nr = mfunc.hdprobe(handle,key)
mfunc.xyclose(handle)
return desc