forked from SgmAstro/DESI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_rand_ddp_N8.py
156 lines (109 loc) · 6.19 KB
/
gen_rand_ddp_N8.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
import os
import gc
import sys
import time
import tqdm
import fitsio
import numpy as np
import matplotlib.pyplot as plt
import argparse
from astropy.table import Table
from scipy.spatial import KDTree
from cartesian import cartesian
from delta8_limits import d8_limits, delta8_tier
from runtime import calc_runtime
from findfile import fetch_fields, findfile, overwrite_check
from config import Configuration
from volfracs import volfracs
from bitmask import lumfn_mask, consv_mask, update_bit
from params import fillfactor_threshold
parser = argparse.ArgumentParser(description='Calculate DDP1 N8 for all randoms.')
parser.add_argument('--log', help='Create a log file of stdout.', action='store_true')
parser.add_argument('-f', '--field', type=str, help='Select equatorial GAMA field: G9, G12, G15', required=True)
parser.add_argument('-d', '--dryrun', help='Dryrun.', action='store_true')
parser.add_argument('--prefix', help='filename prefix', default='randoms')
parser.add_argument('--realz', help='randoms realization number', default=0)
parser.add_argument('--nooverwrite', help='Do not overwrite outputs if on disk', action='store_true')
parser.add_argument('-s', '--survey', help='Select survey', default='gama')
args = parser.parse_args()
log = args.log
field = args.field.upper()
dryrun = args.dryrun
prefix = args.prefix
realz = args.realz
survey = args.survey.lower()
nooverwrite = args.nooverwrite
fields = fetch_fields(survey)
if log:
logfile = findfile(ftype='randoms_bd_ddp_n8', dryrun=False, field=field, survey=survey, prefix=prefix, log=True)
print(f'Logging to {logfile}')
sys.stdout = open(logfile, 'w')
assert field in fields, f'Provided {field} field is not compatible with those available for {survey} survey ({fields})'
start = time.time()
fpath = findfile(ftype='ddp', dryrun=dryrun, survey=survey, prefix=prefix)
dat = Table.read(fpath)
dat = dat[dat['FIELD'] == field]
runtime = calc_runtime(start, 'Reading {:.2f}M Gold DDP'.format(len(dat) / 1.e6), xx=dat)
fpath = findfile(ftype='randoms_bd', dryrun=dryrun, field=field, survey=survey, prefix=prefix)
opath = findfile(ftype='randoms_bd_ddp_n8', dryrun=dryrun, field=field, survey=survey, prefix=prefix)
if nooverwrite:
overwrite_check(opath)
# Should be solid angle and DDP1 z-limit defined randoms.
rand = Table.read(fpath)
runtime = calc_runtime(start, 'Reading {:.2f}M randoms'.format(len(rand) / 1.e6), xx=rand)
# Remove anything already in randoms header.
present = list(dat.meta.keys())
for pp in present:
if pp in rand.meta.keys():
del dat.meta[pp]
assert 'AREA' not in dat.meta.keys()
# Propagate header 'DDP1_ZMIN' etc. to randoms.
rand.meta.update(dat.meta)
points = np.c_[rand['CARTESIAN_X'], rand['CARTESIAN_Y'], rand['CARTESIAN_Z']]
points = np.array(points, copy=True)
kd_tree_rand = KDTree(points)
del points
gc.collect()
# Calculate DDP1/2/3 8-sphere counts for each random.
for idx in range(3):
ddp_idx = idx + 1
runtime = calc_runtime(start, 'Solving for DDP {}'.format(ddp_idx))
ddp = dat[dat['DDP'][:,idx] == 1]
points_ddp = np.c_[ddp['CARTESIAN_X'], ddp['CARTESIAN_Y'], ddp['CARTESIAN_Z']]
points_ddp = np.array(points_ddp, copy=True)
kd_tree_ddp = KDTree(points_ddp)
indexes_ddp = kd_tree_rand.query_ball_tree(kd_tree_ddp, r=8.)
rand['DDP{:d}_N8'.format(ddp_idx)] = np.array([len(idx) for idx in indexes_ddp])
ddp1_zmin = dat.meta['DDP1_ZMIN']
ddp1_zmax = dat.meta['DDP1_ZMAX']
ddp2_zmin = dat.meta['DDP2_ZMIN']
ddp2_zmax = dat.meta['DDP2_ZMAX']
ddp3_zmin = dat.meta['DDP3_ZMIN']
ddp3_zmax = dat.meta['DDP3_ZMAX']
print('Found redshift limits: {:.3f} < z < {:.3f}'.format(ddp1_zmin, ddp1_zmax))
rand['DDPZLIMS'] = np.zeros(len(rand) * 3, dtype=int).reshape(len(rand), 3)
rand['DDPZLIMS'][:,0] = (rand['Z'].data > ddp1_zmin) & (rand['Z'].data < ddp1_zmax)
rand['DDPZLIMS'][:,1] = (rand['Z'].data > ddp2_zmin) & (rand['Z'].data < ddp2_zmax)
rand['DDPZLIMS'][:,2] = (rand['Z'].data > ddp3_zmin) & (rand['Z'].data < ddp3_zmax)
rand['DDP1_DELTA8'] = (rand['DDP1_N8'] / (rand.meta['VOL8'] * dat.meta['DDP1_DENS']) / rand['FILLFACTOR']) - 1.
rand['DDP2_DELTA8'] = (rand['DDP2_N8'] / (rand.meta['VOL8'] * dat.meta['DDP2_DENS']) / rand['FILLFACTOR']) - 1.
rand['DDP3_DELTA8'] = (rand['DDP3_N8'] / (rand.meta['VOL8'] * dat.meta['DDP3_DENS']) / rand['FILLFACTOR']) - 1.
rand['DDP1_DELTA8_TIER'] = delta8_tier(rand['DDP1_DELTA8'].data)
# Same again, but random included in sphere-8 count for volume fractions used within DDP1 magnitude limits.
rand['DDP1_DELTA8_ZEROPOINT'] = ((1 + rand['DDP1_N8']) / (rand.meta['VOL8'] * dat.meta['DDP1_DENS']) / rand['FILLFACTOR']) - 1.
rand['DDP2_DELTA8_ZEROPOINT'] = ((1 + rand['DDP2_N8']) / (rand.meta['VOL8'] * dat.meta['DDP2_DENS']) / rand['FILLFACTOR']) - 1.
rand['DDP3_DELTA8_ZEROPOINT'] = ((1 + rand['DDP3_N8']) / (rand.meta['VOL8'] * dat.meta['DDP3_DENS']) / rand['FILLFACTOR']) - 1.
rand['DDP1_DELTA8_TIER_ZEROPOINT'] = delta8_tier(rand['DDP1_DELTA8_ZEROPOINT'])
# Meeting sphere-completeness cut. Ultimately, this will correct VMAX from solid angle and DDP1
# redshift limits to that meeting the completeness cut (in volume).
update_bit(rand['IN_D8LUMFN'], lumfn_mask, 'FILLFACTOR', rand['FILLFACTOR'].data < fillfactor_threshold)
print('Fraction of randoms meeting IN_D8LUMFN cut: {}'.format(np.mean(rand['IN_D8LUMFN'])))
for ii, xx in enumerate(d8_limits):
rand.meta['D8{}LIMS'.format(ii)] = str(xx)
# Single-field values defined in header.
rand = volfracs(rand, bitmasks=['IN_D8LUMFN'])
runtime = calc_runtime(start, 'Writing {}'.format(opath), xx=rand)
rand.write(opath, format='fits', overwrite=True)
runtime = calc_runtime(start, 'Finished')
if log:
sys.stdout.close()