forked from SgmAstro/DESI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgama_gold.py
194 lines (134 loc) · 5.63 KB
/
gama_gold.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
import os
import sys
import argparse
import runtime
import numpy as np
import astropy.io.fits as fits
from config import Configuration
from findfile import findfile, overwrite_check, write_desitable
from astropy.table import Table
from cosmo import cosmo, distmod
from gama_limits import gama_field
from cartesian import cartesian, rotate
from bitmask import BitMask, lumfn_mask
from config import Configuration
from ddp_zlimits import ddp_zlimits
def gama_gold(argset):
if argset.log:
logfile = findfile(ftype='gold', dryrun=False, survey='gama', log=True)
print(f'Logging to {logfile}')
sys.stdout = open(logfile, 'w')
root = os.environ['TILING_CATDIR']
fpath = root + '/TilingCatv46.fits'
opath = findfile(ftype='gold', dryrun=False, survey='gama')
if argset.dryrun:
dpath = findfile(ftype='gold', dryrun=True, survey='gama')
if os.path.isfile(dpath):
print('Dryrun gama_gold created on full run; Exiting.')
return 0
if argset.nooverwrite:
overwrite_check(opath)
dat = Table.read(fpath)
dat = Table(dat, masked=False)
keys = list(dat.meta.keys())
for x in keys:
if x not in ['VERSION', 'DATE']:
del dat.meta[x]
dat.meta['AREA'] = 180.
# print(dat.dtype.names)
dat.rename_column('Z', 'ZGAMA')
for band in 'UGRIZ':
dat.rename_column('{}_MODEL'.format(band), '{}MAG_DRED_SDSS'.format(band))
minimal_cols = ['CATAID', 'OBJID', 'RA', 'DEC', 'R_PETRO', 'ZGAMA', 'NQ', 'SPECID', 'SURVEY_CLASS']
for band in ['U', 'G', 'R', 'I', 'Z']:
minimal_cols += ['{}MAG_DRED_SDSS'.format(band)]
# Minimal catalogue.
dat = dat[minimal_cols]
# 'SURVEY_CLASS' < 4 for GAMA-II (rpet <= 19.8 by extension.
# r=12 bright cut;
# 1 cat. per field (G9, 12, 15).
zlow = ddp_zlimits['DDP1'][0]
zhigh = ddp_zlimits['DDP1'][1]
sclass_cut = (dat['SURVEY_CLASS'] >= 4)
z_cut = (dat['ZGAMA'] > zlow) & (dat['ZGAMA'] < zhigh)
r_cut = (dat['R_PETRO'] > 12)
nq_cut = (dat['NQ'] >= 3)
print(np.mean(sclass_cut))
print(np.mean(z_cut))
print(np.mean(r_cut))
print(np.mean(nq_cut))
dat = dat[sclass_cut & z_cut & r_cut & nq_cut]
dat['ZSURV'] = dat['ZGAMA']
dat['LUMDIST'] = cosmo.luminosity_distance(dat['ZGAMA'].data)
dat['DISTMOD'] = distmod(dat['ZGAMA'].data)
dat['FIELD'] = gama_field(dat['RA'], dat['DEC'])
dat['IN_D8LUMFN'] = np.zeros_like(dat['FIELD'], dtype=int)
dat['CONSERVATIVE'] = np.zeros_like(dat['FIELD'], dtype=int)
xyz = cartesian(dat['RA'], dat['DEC'], dat['ZGAMA'])
dat['CARTESIAN_X'] = xyz[:,0]
dat['CARTESIAN_Y'] = xyz[:,1]
dat['CARTESIAN_Z'] = xyz[:,2]
xyz = rotate(dat['RA'], dat['DEC'], xyz)
dat['ROTCARTESIAN_X'] = xyz[:,0]
dat['ROTCARTESIAN_Y'] = xyz[:,1]
dat['ROTCARTESIAN_Z'] = xyz[:,2]
dat['GMR'] = dat['GMAG_DRED_SDSS'] - dat['RMAG_DRED_SDSS']
dat['DETMAG'] = dat['R_PETRO']
'''
Note: survey_specifics is deprecated
if argset.in_bgsbright:
offset = survey_specifics('desi')['pet_offset']
update_bit(dat['IN_D8LUMFN'], lumfn_mask, 'INBGSBRIGHT', dat['DETMAG'].data + offset < 19.5)
'''
# Randomise rows.
idx = np.arange(len(dat))
idx = np.random.choice(idx, size=len(idx), replace=False)
dat = dat[idx]
print('Solved for GAMA gold')
if not os.path.isdir(os.environ['GOLD_DIR']):
print('Creating {}'.format(os.environ['GOLD_DIR']))
os.makedirs(os.environ['GOLD_DIR'])
# 113687 vs TMR 80922.
dat.meta['GOLD_NGAL'] = len(dat)
dat.pprint()
dat.meta = {'AREA': dat.meta['AREA'],\
'GOLD_NGAL': dat.meta['GOLD_NGAL'],\
'IMMUTABLE': 'FALSE',\
'RLIM': 19.8,\
'RMAX': 12.0,\
'MAX_SEP': 70.0}
print('Writing {}.'.format(opath))
write_desitable(opath, dat)
# Dryrun: 2x2 sq. patch of sky.
# G12
delta_deg = 0.5
isin = (dat['RA'] > 180. - delta_deg) & (dat['RA'] < 180. + delta_deg)
isin &= (dat['DEC'] > 0.0 - delta_deg) & (dat['DEC'] < 0.0 + delta_deg)
allin = isin
# G9
isin = (dat['RA'] > 135. - delta_deg) & (dat['RA'] < 135. + delta_deg)
isin &= (dat['DEC'] > 0.0 - delta_deg) & (dat['DEC'] < 0.0 + delta_deg)
allin |= isin
# G15
isin = (dat['RA'] > 217. - delta_deg) & (dat['RA'] < 217. + delta_deg)
isin &= (dat['DEC'] > 0.0 - delta_deg) & (dat['DEC'] < 0.0 + delta_deg)
allin |= isin
dat = dat[allin]
opath = findfile(ftype='gold', dryrun=True, survey='gama')
write_desitable(opath, dat)
print('Writing {}.'.format(opath))
if argset.log:
sys.stdout.close()
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Gen GAMA gold cat.')
parser.add_argument('--log', help='Create a log file of stdout.', action='store_true')
parser.add_argument('--config', help='Path to configuration file', type=str, default=findfile('config'))
parser.add_argument('--dryrun', help='Dryrun of 5k galaxies', action='store_true')
parser.add_argument('--nooverwrite', help='Do not overwrite outputs if on disk', action='store_true')
parser.add_argument('--in_bgsbright', help='Add flag for IN_BGSBRIGHT', action='store_true')
args = parser.parse_args()
config = Configuration(args.config)
config.update_attributes('gold', args)
config.write()
gama_gold(args)