forked from coljac/makestamps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_fits.py
119 lines (99 loc) · 3.59 KB
/
extract_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
#!env python
import os
import sys
#import coltools as ct
import h5py as h5
import astropy.io.fits as pyfits
def fal(filename, skip_comments=True):
with open(filename, "r") as f:
lines = f.readlines()
if skip_comments:
return [l.strip() for l in lines if not l.startswith("#")]
else:
return [l.strip() for l in lines]
def progbar(current, to, width=40, show=True, message=None, stderr=False):
percent = float(current) / float(to)
length = int(width * percent)
if show:
count = " (%d/%d) " % (current, to)
else:
count = ""
if message:
count += message
outstream = sys.stderr if stderr else sys.stdout
outstream.write(("\r[" + ("#" * length) + " " * (width - length) +
"] %0d" % (percent * 100)) + "%" + count)
outstream.flush()
def extract_objects(datastore, objids, outdir, bands):
if objids is None:
extract_all(datastore, outdir, bands)
return
else:
extract_some(objids, datastore, outdir, bands)
def extract_some(ids, datastore, outdir, bands):
todo = len(ids)
done = 0
# ct.progbar(done, todo)
with h5.File(datastore, "r") as f:
ds = f['/stamps/']
for tile in ds:
data = ds[tile + "/data"]
headers = ds[tile + "/header"]
objids = ds[tile + "/catalog"]
N = data.shape[0]
for i in range(N):
objid = objids[i].strip()
if objid in ids:
done += 1
# ct.progbar(done, todo)
heads = headers[i]
for b in range(len(bands)):
band_name = bands[b]
d = data[i, :, :, b]
head = pyfits.Header.fromstring(heads[b].strip())
filename = "%s/%s_%s.fits" % (outdir, objid, band_name)
outfits = pyfits.PrimaryHDU(data=d, header=head)
outfits.writeto(filename, overwrite=True)
def make_rgb(datastore, outdir):
pass
def extract_all(datastore, outdir, bands):
with h5.File(datastore, "r") as f:
ds = f['/stamps/']
for tile in ds:
data = ds[tile + "/data"]
headers = ds[tile + "/header"]
objids = ds[tile + "/catalog"]
N = data.shape[0]
for i in range(N):
objid = objids[i].strip()
heads = headers[i]
for b in range(len(bands)):
band_name = bands[b]
d = data[i, :, :, b]
head = pyfits.Header.fromstring(heads[b].strip())
filename = "%s/%s_%s.fits" % (outdir, objid, band_name)
outfits = pyfits.PrimaryHDU(data=d, header=head)
outfits.writeto(filename, overwrite=True)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("image_extract <datastore> <dest_dir> <bands> [objids]")
sys.exit(0)
datastore = sys.argv[1]
outdir = sys.argv[2]
# if len(sys.argv) > 3 (script+2args), bands should be there
bands = "r" # "my" default
# bands = "grizY" # old default
if len(sys.argv) > 3:
inputs = sys.argv[3]
# only check for inputs if len(sys.argv) == 5
inputs = None
if len(sys.argv) == 5:
inputs = sys.argv[4]
if not os.path.exists(outdir):
print("Output directory doesn't exist.")
sys.exit(0)
if inputs is not None:
objids = inputs.split(",")
else:
objids = None
extract_objects(datastore, objids, outdir, bands)