-
Notifications
You must be signed in to change notification settings - Fork 0
/
txtobj.py
160 lines (147 loc) · 5.4 KB
/
txtobj.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
#!/usr/bin/env python
# D. Jones - 5/14/14
# diffimmagstats.py --cmpfile=/datascope/ps1sn1/data/v10.0/GPC1v3/eventsv1/workspace/PSc360052/tmpl/g/PSc360052.md04s047.g.stack_44.sw.icmp --psffile=/datascope/ps1sn1/data/v10.0/GPC1v3/eventsv1/workspace/PSc360052/g/PSc360052.md04s047.g.ut091126f.648816_44.sw.dao.psf.fits --diffim=/datascope/ps1sn1/data/v10.0/GPC1v3/eventsv1/workspace/PSc360052_tmpl/g/PSc360052.md04s047.g.ut091126f.648816_44_md04s047.g.stack_44.diff.fits
"""Calculate increase in uncertainty due
to bright host galaxies
Usage: diffimmagstats.py --cmpfile=cmpfile --psffile=psffile --diffim=diffimfile
"""
import glob
import os
import numpy as np
import astropy.io.fits as pyfits
class txtobj:
def __init__(self,filename,allstring=False,
cmpheader=False,sexheader=False,
useloadtxt=True,fitresheader=False,
delimiter=' ',skiprows=0,tabsep=False,
rowprfx='SN'):
if cmpheader:
hdu = pyfits.open(filename)
hdu.verify('silentfix')
hdr = hdu[0].header
skiprows=1
#if fitresheader: skiprows=6
coldefs = np.array([])
if cmpheader:
for k,v in zip(hdr.keys(),hdr.values()):
if 'COLTBL' in k and k != 'NCOLTBL':
coldefs = np.append(coldefs,v)
elif sexheader:
fin = open(filename,'r')
lines = fin.readlines()
for l in lines:
if l.startswith('#'):
coldefs = np.append(coldefs,l.replace('#','').split()[1])
elif fitresheader:
self.rdfitres(filename,rowprfx=rowprfx)
return
else:
fin = open(filename,'r')
lines = fin.readlines()
if not tabsep:
if delimiter == ' ':
coldefs = np.array(lines[0].replace('#','').split())
#coldefs = coldefs[np.where(coldefs != '#')]
else:
coldefs = np.array(lines[0].replace('#','').split(delimiter))[np.where(np.array(lines[0].split(delimiter)) != '')[0]]
#coldefs = coldefs[np.where(coldefs != '#')]
else:
l = lines[0].replace('#','').replace('\n','')
coldefs = np.array(l.replace('#','').split('\t')[l.split('\t') != ''])
#coldefs = coldefs[np.where(coldefs != '#')]
for i in range(len(coldefs)):
coldefs[i] = coldefs[i].replace('\n','').replace('\t','').replace(' ','')
if coldefs[i]:
self.__dict__[coldefs[i].replace('\r','')] = np.array([])
self.filename = np.array([])
if useloadtxt:
for c,i in zip(coldefs,range(len(coldefs))):
c = c.replace('\n','')
if c:
if not delimiter or delimiter == ' ':
self.__dict__[c] = np.atleast_1d(np.genfromtxt(filename,unpack=True,usecols=[i],dtype='str',skip_header=skiprows))
try: self.__dict__[c] = self.__dict__[c].astype(float)
except: continue
else:
self.__dict__[c] = np.atleast_1d(np.genfromtxt(filename,unpack=True,usecols=[i],dtype='str',delimiter=delimiter,skip_header=skiprows))
try: self.__dict__[c] = self.__dict__[c].astype(float)
except: continue
self.filename = np.array([filename]*len(self.__dict__[c]))
else:
fin = open(filename,'r')
count = 0
for line in fin:
if count >= 1 and not line.startswith('#'):
entries = line.split()
for e,c in zip(entries,coldefs):
e = e.replace('\n','')
c = c.replace('\n','')
if not allstring:
try:
self.__dict__[c] = np.append(self.__dict__[c],float(e))
except:
self.__dict__[c] = np.append(self.__dict__[c],e)
else:
self.__dict__[c] = np.append(self.__dict__[c],e)
self.filename = np.append(self.filename,filename)
else: count += 1
fin.close()
def rdfitres(self,filename,rowprfx='SN'):
import numpy as np
fin = open(filename,'r')
lines = fin.readlines()
for l in lines:
if l.startswith('VARNAMES:'):
l = l.replace('\n','')
coldefs = l.split()
break
with open(filename) as f:
reader = [x.split() for x in f if x.startswith('%s:'%rowprfx)]
i = 0
for column in zip(*reader):
try:
self.__dict__[coldefs[i]] = np.array(column[:]).astype(float)
except:
self.__dict__[coldefs[i]] = np.array(column[:])
i += 1
def addcol(self,col,data):
self.__dict__[col] = data
def cut_inrange(self,col,minval,maxval,rows=[]):
if not len(rows):
rows = np.where((self.__dict__[col] > minval) &
(self.__dict__[col] < maxval))[0]
return(rows)
else:
rows2 = np.where((self.__dict__[col][rows] > minval) &
(self.__dict__[col][rows] < maxval))[0]
return(rows[rows2])
def appendfile(self,filename,usegenfromtxt=False):
if usegenfromtxt:
fin = open(filename,'r')
for line in fin:
if line.startswith('#'):
coldefs = line.split('#')[1].split('\n')[0].split()
break
fin.close()
for c,i in zip(coldefs,range(len(coldefs))):
try:
self.__dict__[c] = np.concatenate((self.__dict__[c],np.genfromtxt(filename,unpack=True,usecols=[i])))
except:
self.__dict__[c] = np.concatenate((self.__dict__[c],np.genfromtxt(filename,unpack=True,
usecols=[i],dtype='str')))
self.filename = np.append(self.filename,np.array([filename]*len(np.genfromtxt(filename,unpack=True,usecols=[i],dtype='str'))))
return()
fin = open(filename,'r')
for line in fin:
if line.startswith('#'):
coldefs = line.split('#')[1].split('\n')[0].split()
else:
entries = line.split()
for e,c in zip(entries,coldefs):
e = e.replace('\n','')
c = c.replace('\n','')
try:
self.__dict__[c] = np.append(self.__dict__[c],float(e))
except:
self.__dict__[c] = np.append(self.__dict__[c],e)
self.filename = np.append(self.filename,filename)