-
Notifications
You must be signed in to change notification settings - Fork 0
/
fits.py
executable file
·302 lines (225 loc) · 7.47 KB
/
fits.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'arnault'
import pymongo
import numpy as np
from bson import CodecOptions, SON, BSON
from bson.binary import Binary
import pickle
import glob
import os
from astropy import wcs as pywcs
from astropy.io import fits as pyfits
import re
CC = True
if os.name == 'nt':
MONGO_URL = r'mongodb://127.0.0.1:27017'
FILES_ROOT = 'data/'
else:
MONGO_URL = r'mongodb://lsst:[email protected]:27017/lsst'
FILES_ROOT = '/sps/lsst/data/CFHT/D3/input/raw/'
FILES = FILES_ROOT + '/*/*/*/*/*.fits.fz'
class MyWCS:
"""
Class to implement the transformation algorithm to convert pixel coordinate into WCS coordinates
this reflects what is provided in the wcslib module
"""
def __init__(self, header):
"""
Constructor: fetch all WCS information to setup parameters needed for the
conversion algorithm
:param header: this is the FITS header
:return: Nonz
"""
CRPIX1 = header['CRPIX1']
CRPIX2 = header['CRPIX2']
CD1_1 = header['CD1_1']
CD1_2 = header['CD1_2']
CRVAL1 = header['CRVAL1']
CD2_1 = header['CD2_1']
CD2_2 = header['CD2_2']
CRVAL2 = header['CRVAL2']
# prepare the transfromation matrix
self.xy0 = np.array([CRPIX1, CRPIX2], np.float64)
a = CD1_1
b = CD1_2
c = CRVAL1
d = CD2_1
e = CD2_2
f = CRVAL2
self.matrix = np.array([[a, b], [d, e]], np.float64)
self.ra_dec0 = np.array([c, f], np.float64)
def xy_to_radec(self, xy):
"""
Function to convert a pixel coordinates into a WCS coordonates
:param xy: a 1D ndarray
:return: ra_dec the result of the conversion as a 1d-array
"""
xy -= self.xy0
ra_dec = self.matrix.dot(xy)
dec = self.ra_dec0[1] + ra_dec[1]
dec_radians = 2.0 * np.pi * dec/360.0
scale = np.array([np.cos(dec_radians), 1.0])
ra_dec /= scale
ra_dec += self.ra_dec0
return ra_dec
def radec_to_xy(self, ra_dec):
# not yet done
return None
def get_corners(header, wcs, mywcs):
detsize = header['DATASEC']
m = re.match('[^\[]*\[([0-9]+):([0-9]+),([0-9]+):([0-9]+).*', detsize)
low1 = np.float64(m.group(1))
high1 = np.float64(m.group(2))
low2 = np.float64(m.group(3))
high2 = np.float64(m.group(4))
low = [low1, low2]
high = [high1, high2]
pixel = np.array([low, high], np.float64)
try:
sky = wcs.wcs_pix2world(pixel, 0)
except:
print "Unexpected error:", sys.exc_info()[0]
ra_dec_low = mywcs.xy_to_radec(low)
ra_dec_high = mywcs.xy_to_radec(high)
# print 'low=', low, 'high=', high
# print 'mat=', ra_dec_low, ra_dec_high
# print 'wcs=', sky[0], sky[1]
d_low = (sky[0] - ra_dec_low)/sky[0]
d_high = (sky[1] - ra_dec_high)/sky[1]
# print 'd =', d_low, d_high
return (ra_dec_low[0], ra_dec_low[1]), (ra_dec_high[0], ra_dec_high[1])
def read_hdus(fitsfile):
""" pixels from FITS file
cf http://stsdas.stsci.edu/stsci_python_epydoc/pyfits/api_hdulists.html
Return a HDUList()
"""
data_fits = None
try:
with pyfits.open(fitsfile) as data_fits:
try:
data_fits.verify('silentfix')
return data_fits
except ValueError as err:
print 'Error: %s' % err
except EnvironmentError as err:
print 'Cannot open the data fits file. - %s' % err
return data_fits
class PointRange:
def __init__(self):
self.min_ra = None
self.max_ra = None
self.min_dec = None
self.max_dec = None
def update(self, ra_dec):
ra = ra_dec[0]
dec = ra_dec[1]
changed = False
if self.min_ra is None or ra < self.min_ra:
changed = True
self.min_ra = ra
if self.max_ra is None or ra > self.max_ra:
changed = True
self.max_ra = ra
if self.min_dec is None or dec < self.min_dec:
changed = True
self.min_dec = dec
if self.max_dec is None or dec > self.max_dec:
changed = True
self.max_dec = dec
if changed:
self.show()
def show(self):
print 'Range: RA=[%f %f] DEC=[%f %f]' % (self.min_ra, self.max_ra, self.min_dec, self.max_dec)
myrange = PointRange()
def fits_to_mongo(fits, name):
global myrange
# print FILES_ROOT + name
hdulist = read_hdus(FILES_ROOT + name)
for index, hdu in enumerate(hdulist):
""" handle all hdus of this file """
hdr = hdu.header
ra_dec0, ra_dec1 = None, None
ra, dec = None, None
wcs = pywcs.WCS(hdr)
try:
mywcs = MyWCS(hdr)
ra_dec0, ra_dec1 = get_corners(hdr, wcs, mywcs)
ra = (ra_dec0[0] + ra_dec1[0])/2.0 - 360.0
dec = (ra_dec0[1] + ra_dec1[1])/2.0
myrange.update(ra_dec0)
myrange.update(ra_dec1)
except:
# print 'no WCS data in this header'
pass
""" just consider the header of this hdu """
# print hdr.tostring(sep='\n', padding=False)
object = SON()
object['header_index'] = index
object['where'] = name.split('/')
thebytes = pickle.dumps(wcs, protocol=2)
object['wcs'] = Binary(thebytes)
object['top_left'] = ra_dec0
object['bottom_right'] = ra_dec1
if ra is not None:
object['center'] = { 'type':'Point', 'coordinates':[ ra, dec ] }
for card in hdr._cards:
comment = card.comment
key = card.keyword
value = card.value
# print '[%s] = [%s] | %s' % (key, value, comment)
if key == '':
continue
if key == 'COMMENT':
continue
pass
if key == 'HISTORY':
continue
pass
if isinstance(value, long):
value = str(value)
# print key, value
object[key] = (value, comment)
try:
_id = fits.insert_one(object)
except Exception as e:
print 'oups'
print e.message
exit()
pass
if __name__ == '__main__':
client = pymongo.MongoClient(MONGO_URL)
lsst = client.lsst
recreate = True
if recreate:
try:
fits = lsst.fits
lsst.drop_collection('fits')
except:
pass
try:
fits = lsst.fits
except:
opts = CodecOptions(document_class=SON)
fits = lsst.create_collection('fits', codec_options=opts)
for coll in lsst.collection_names():
c = lsst[coll]
print coll, c.count()
# we killed at /sps/lsst/data/CFHT/input/raw/06AL01/D3/2006-06-02/g/850592p.fits.fz
for file in glob.glob(FILES):
file = file.replace('\\', '/')
f = file.replace(FILES_ROOT, '')
# print 'existing file', f
out = fits.find( { 'where': { '$in': [file.split('/')[-1]] } } )
if out.count() == 0:
# print 'encode file', f
fits_to_mongo(fits, f)
pass
else:
# print f, 'is encoded'
for x in out:
print x[u'where']
print '# of objects in collection', fits.count()
out = fits.find( { 'where': { '$in': [u'732190p.fits.fz'] } } )
for x in out:
print x[u'where']