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
|
#!/usr/bin/env python3
#
# Copyright (c) 2017 Weitian LI <weitian@aaronly.me>
# MIT License
#
"""
Convert redshifts to the observed frequency of the 21 cm signal,
and vice versa.
"""
import os
import sys
import argparse
import numpy as np
prog = os.path.basename(sys.argv[0])
freq21cm = 1420405751.7667 / 1e6 # [MHz]
def z2freq(redshifts, print_=False):
redshifts = np.asarray(redshifts)
freqs = freq21cm / (redshifts + 1.0)
if print_:
print("# redshift frequency[MHz]")
for z, f in zip(redshifts, freqs):
print("%.4f %.2f" % (z, f))
return freqs
def freq2z(freqs, print_=False):
freqs = np.asarray(freqs)
redshifts = freq21cm / freqs - 1.0
if print_:
print("# frequency[MHz] redshift")
for f, z in zip(freqs, redshifts):
print("%.2f %.4f" % (f, z))
return redshifts
def parse_inputs(inputs):
values = []
for inp in inputs:
try:
v = float(inp)
values += [v]
except ValueError:
begin, step, stop = inp.split(":")
begin, step, stop = float(begin), float(step), float(stop)
v = np.arange(start=begin, stop=stop+step/2, step=step)
values += list(v)
return values
def main():
if prog == "z2freq.py":
description = "Convert redshifts to observed 21 cm frequencies"
parser = argparse.ArgumentParser(description=description)
parser.add_argument("inputs", nargs="+",
help="input redshifts: <z1> <begin:step:stop>")
args = parser.parse_args()
redshifts = parse_inputs(args.inputs)
z2freq(redshifts, print_=True)
elif prog == "freq2z.py":
description = "Convert observed 21 cm frequencies to redshifts"
parser = argparse.ArgumentParser(description=description)
parser.add_argument("inputs", nargs="+",
help="input frequencies [MHz]: <freq1> " +
"<begin:step:stop>")
args = parser.parse_args()
freqs = parse_inputs(args.inputs)
freq2z(freqs, print_=True)
else:
raise RuntimeError("unknown program name: %s" % prog)
if __name__ == "__main__":
main()
|