forked from SgmAstro/DESI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
randoms.py
274 lines (195 loc) · 10.4 KB
/
randoms.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
import os
import sys
import time
import numpy as np
import argparse
from cosmo import cosmo, volcom
from scipy.interpolate import interp1d
from astropy.table import Table
from cartesian import cartesian, rotate
from runtime import calc_runtime
from desi_randoms import desi_randoms
from findfile import fetch_fields, findfile, overwrite_check, call_signature
from gama_limits import gama_limits, gama_field
from ddp_zlimits import ddp_zlimits
from config import Configuration
from params import oversample_nrealisations
def randoms(field='G9', survey='gama', density=1., zmin=ddp_zlimits['DDP1'][0], zmax=ddp_zlimits['DDP1'][1], dryrun=False, prefix='', seed=None, oversample=2, realz=0):
start = time.time()
fields = fetch_fields(survey)
assert field in fields, f'Provided {field} field is not compatible with those available for {survey} survey ({fields})'
opath = findfile(ftype='randoms', dryrun=dryrun, field=field, survey=survey, prefix=prefix, realz=realz, oversample=oversample)
if args.nooverwrite:
overwrite_check(opath)
if seed == None:
seed = seed + realz + 50 * oversample
np.random.seed(seed)
call_signature(dryrun, sys.argv)
print('Solving for redshift limits: {} < z < {}.'.format(zmin, zmax))
## ras and decs.
if survey == 'gama':
Area = 60.
ra_min = gama_limits[field]['ra_min']
ra_max = gama_limits[field]['ra_max']
dec_min = gama_limits[field]['dec_min']
dec_max = gama_limits[field]['dec_max']
ctheta_min = np.cos(np.pi/2. - np.radians(dec_min))
ctheta_max = np.cos(np.pi/2 - np.radians(dec_max))
vol = volcom(zmax, Area) - volcom(zmin, Area)
nrand = int(np.ceil(vol * density * oversample))
cos_theta = np.random.uniform(ctheta_min, ctheta_max, nrand)
theta = np.arccos(cos_theta)
decs = np.pi/2. - theta
decs = np.degrees(decs)
ras = np.random.uniform(ra_min, ra_max, nrand)
randoms = Table(np.c_[ras, decs], names=['RANDOM_RA', 'RANDOM_DEC'])
nrand = len(randoms)
if dryrun:
# Dryrun: 2x2 sq. patch of sky.
# G12
delta_deg = 0.5
isin = (randoms['RANDOM_RA'] > 180. - delta_deg) & (randoms['RANDOM_RA'] < 180. + delta_deg)
isin &= (randoms['RANDOM_DEC'] > 0. - delta_deg) & (randoms['RANDOM_DEC'] < 0. + delta_deg)
allin = isin
# G9
isin = (randoms['RANDOM_RA'] > 135. - delta_deg) & (randoms['RANDOM_RA'] < 135. + delta_deg)
isin &= (randoms['RANDOM_DEC'] > 0. - delta_deg) & (randoms['RANDOM_DEC'] < 0. + delta_deg)
allin |= isin
# G15
isin = (randoms['RANDOM_RA'] > 217. - delta_deg) & (randoms['RANDOM_RA'] < 217. + delta_deg)
isin &= (randoms['RANDOM_DEC'] > 0.0 - delta_deg) & (randoms['RANDOM_DEC'] < 0.0 + delta_deg)
allin |= isin
randoms = randoms[allin]
nrand = len(randoms)
elif survey == 'desi':
if 'NERSC_HOST' in os.environ.keys():
# Support to run on nersc only.
randoms = desi_randoms(int(field[1:]), oversample=oversample, dryrun=dryrun)
nrand = randoms.meta['NRAND']
Area = randoms.meta['AREA']
elif 'ddp1' in prefix:
rpath = findfile(ftype='randoms', dryrun=dryrun, field=field, survey=survey, prefix=None, realz=realz, oversample=oversample)
randoms = Table.read(rpath)
nrand = randoms.meta['NRAND']
Area = randoms.meta['AREA']
else:
print(f'As you are not running on nersc, the output of this script is assumed to be present at {opath} for dryrun: {dryrun}.')
return 0
else:
raise NotImplementedError(f'No implementation for survey: {survey}')
## Vs and zs.
dz = 1.e-4
Vmin = volcom(zmin, Area)
Vmax = volcom(zmax, Area)
vol = Vmax - Vmin
density = nrand / vol
rand_dir = os.path.dirname(opath)
if not os.path.isdir(rand_dir):
print('Creating {}'.format(rand_dir))
os.makedirs(rand_dir)
print('Volume [1e6]: {:.2f}; oversample: {:.2f}; density: {:.2e}; nrand [1e6]: {:.2f}'.format(vol/1.e6, oversample, density, nrand / 1.e6))
zs = np.arange(0.0, zmax+dz, dz)
Vs = volcom(zs, Area)
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html
Vz = interp1d(Vs, zs, kind='linear', copy=True, bounds_error=True, fill_value=np.NaN, assume_sorted=False)
Vdraws = np.random.uniform(0., 1., nrand)
Vdraws = Vmin + Vdraws * (Vmax - Vmin)
zs = Vz(Vdraws)
print('Solved {:d} for field {}'.format(nrand, field))
ras = randoms['RANDOM_RA']
decs = randoms['RANDOM_DEC']
randoms['Z'] = zs
randoms['V'] = Vdraws
randoms['RANDID'] = np.arange(len(randoms))
randoms['FIELD'] = field
# TODO/HACK/RESTORE
randoms['GAMA_FIELD'] = gama_field(ras, decs)
print('Applying rotation.')
xyz = cartesian(ras, decs, zs)
randoms['CARTESIAN_X'] = xyz[:,0]
randoms['CARTESIAN_Y'] = xyz[:,1]
randoms['CARTESIAN_Z'] = xyz[:,2]
xyz = rotate(randoms['RANDOM_RA'], randoms['RANDOM_DEC'], xyz)
randoms['ROTCARTESIAN_X'] = xyz[:,0]
randoms['ROTCARTESIAN_Y'] = xyz[:,1]
randoms['ROTCARTESIAN_Z'] = xyz[:,2]
'''
elif survey == 'desi':
randoms['IS_BOUNDARY'][randoms['ROS_DIST'] > np.percentile(randoms['ROS_DIST'], 100. - boundary_percent)] = 1
randoms['IS_BOUNDARY'][randoms['ROS_DIST'] < np.percentile(randoms['ROS_DIST'], boundary_percent)] = 1
'''
randoms['ZSURV'] = randoms['Z']
randoms['CONSERVATIVE'] = np.zeros_like(randoms['FIELD'], dtype=int)
if 'IN_D8LUMFN' not in randoms.dtype.names:
randoms['IN_D8LUMFN'] = np.zeros_like(randoms['FIELD'], dtype=int)
updates = {'ZMIN': zmin,\
'ZMAX': zmax,\
'DZ': dz,\
'NRAND': nrand,\
'FIELD': field,\
'AREA': Area,\
'VOL': vol,\
'RAND_DENS': density,\
'VOL8': (4./3.)*np.pi*(8.**3.),\
'OVERSAMPLE': oversample,\
'SEED': seed,\
'PREFIX': prefix,\
'REALZ': realz,\
'FPATH': opath}
randoms.meta.update(updates)
randoms.meta['NRAND8'] = randoms.meta['VOL8'] * randoms.meta['RAND_DENS']
randoms.meta['NRAND8_PERR'] = np.sqrt(randoms.meta['NRAND8'])
for key in randoms.meta.keys():
print(key, randoms.meta[key])
runtime = calc_runtime(start, 'Writing {}'.format(opath), xx=randoms)
randoms.write(opath, format='fits', overwrite=True)
runtime = calc_runtime(start, 'Finished'.format(opath))
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Select GAMA field.')
parser.add_argument('--log', help='Create a log file of stdout.', action='store_true')
parser.add_argument('-f', '--field', type=str, help='select GAMA field [G9, G12, G15] or DESI rosette [R1...]', default='G9')
parser.add_argument('-d', '--dryrun', help='Dryrun.', action='store_true')
parser.add_argument('-s', '--survey', help='Survey, e.g. GAMA, DESI, etc.', type=str, default='gama')
parser.add_argument('--realz', help='Realization', default=0, type=int)
parser.add_argument('--prefix', help='filename prefix', default='randoms')
parser.add_argument('--config', help='Path to configuration file', type=str, default=findfile('config'))
parser.add_argument('--nooverwrite', help='Do not overwrite outputs if on disk', action='store_true')
parser.add_argument('--density', help='Random density per (Mpc/h)^3', default=1., type=float)
parser.add_argument('--oversample', help='Oversampling factor for fillfactor counting.', default=2, type=int)
parser.add_argument('--seed', help='Random seed.', default=0, type=int)
# Defaults to GAMA Gold limits.
parser.add_argument('--zmin', type=float, help='Minimum redshift limit', default=ddp_zlimits['DDP1'][0])
parser.add_argument('--zmax', type=float, help='Maximum redshift limit', default=ddp_zlimits['DDP1'][1])
args = parser.parse_args()
log = args.log
field = args.field.upper()
dryrun = args.dryrun
survey = args.survey.lower()
zmin = args.zmin
zmax = args.zmax
prefix = args.prefix
realz = args.realz
seed = args.seed
density = args.density
oversample = args.oversample
assert oversample < 9, f'Oversample of {oversample} is not supported.'
assert realz < oversample_nrealisations, f'Provided realization number is inconsistent with that expected in params; consult there and bin/rand_pipeline scripts.'
if log:
logfile = findfile(ftype='randoms', dryrun=False, field=field, survey=survey, prefix=prefix, realz=realz, log=True)
print(f'Logging to {logfile}')
sys.stdout = open(logfile, 'w')
'''
config = Configuration(args.config)
config.update_attributes('randoms', args)
config.write()
'''
for xx in [1, oversample]:
seed = seed
# only generate independent realizations for oversample.
if oversample > 1:
seed += realz
seed += 50 * oversample
randoms(field=field, survey=survey, density=density, zmin=zmin, zmax=zmax, dryrun=dryrun, prefix=prefix, seed=seed, oversample=xx, realz=realz)
if log:
sys.stdout.close()