forked from damonge/CoLoRe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coadd_cls.py
47 lines (37 loc) · 1.04 KB
/
coadd_cls.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
import os,glob,sys
from astropy.io import fits
from astropy.table import Table
import argparse
parser = argparse.ArgumentParser(description='coadd the cls from a directory')
parser.add_argument('clpattern', help='dir/cls_ like pattern - *.fits is appended')
args= parser.parse_args()
pattern=args.clpattern+"*.fits"
files=glob.glob(pattern)
Ntot=len(files)
print(Ntot,files)
hdulist = fits.open(files[0])
h0=hdulist[0].header
h=hdulist[1].header
d=hdulist[1].data
names=d.dtype.names
cls=[]
[cls.append(d[n]) for n in names]
hdulist.close()
#now loop on remainder add
for file in files[1:]:
hdulist = fits.open(file)
d=hdulist[1].data
for cl,n in zip(cls[1:],names[1:]):
cl+=d[n]
#normalize
for cl in cls[1:]:
cl/=Ntot
# write bintable with same header
t=Table(cls,names=names)
primary_hdu = fits.PrimaryHDU(header=h0)
hh=fits.BinTableHDU(data=t, header=h)
hdul = fits.HDUList([primary_hdu, hh])
d=os.path.dirname(files[0])
fout=os.path.join(d,"clmean.fits")
print("-> output={}".format(fout))
hdul.writeto(fout,overwrite=True)