tabsim 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tabsim/tools.py ADDED
@@ -0,0 +1,111 @@
1
+ import argparse
2
+ import os
3
+ from pathlib import Path
4
+
5
+ import jax.numpy as jnp
6
+ import numpy as np
7
+
8
+ pkg_dir = Path(__file__).parent.absolute()
9
+
10
+
11
+ import signal
12
+ from contextlib import contextmanager
13
+
14
+
15
+ class TimeoutException(Exception):
16
+ pass
17
+
18
+
19
+ @contextmanager
20
+ def time_limit(seconds):
21
+ def signal_handler(signum, frame):
22
+ raise TimeoutException("Timed out!")
23
+
24
+ signal.signal(signal.SIGALRM, signal_handler)
25
+ signal.alarm(seconds)
26
+ try:
27
+ yield
28
+ finally:
29
+ signal.alarm(0)
30
+
31
+
32
+ def beam_size(diameter: float, frequency: float, fwhp: bool = True):
33
+ """
34
+ Calculate the beam size of an antenna or an array. For an array use
35
+ fwhp = True. This assumes an Airy disk primary beam pattern.
36
+
37
+ Parameters:
38
+ -----------
39
+ diameter: float
40
+ Diameter of the dish or baseline in metres.
41
+ frequency: float
42
+ Observation frequency in Hertz.
43
+ fwhp: bool
44
+ True if you want the field of view to be the full width at half power
45
+ and False if you want the first null.
46
+
47
+ Returns:
48
+ --------
49
+ beam_width: float
50
+ beam_width in degrees.
51
+ """
52
+ diameter = jnp.asarray(diameter)
53
+ frequency = jnp.asarray(frequency)
54
+
55
+ c = 299792458.0
56
+ lamda = c / frequency
57
+ beam_width = 1.02 * lamda / diameter if fwhp else 1.22 * lamda / diameter
58
+
59
+ return jnp.rad2deg(beam_width)
60
+
61
+
62
+ def load_antennas(telescope: str = "MeerKAT"):
63
+ """
64
+ Load the ENU coordinates for a telescope. Currently only MeerKAT is
65
+ included.
66
+
67
+ Parameters:
68
+ -----------
69
+ telescope: str
70
+ The name of the telescope.
71
+
72
+ Returns:
73
+ --------
74
+ enu: array_like (n_ant, 3)
75
+ The East, North, Up coordinates of each antenna relative to a reference
76
+ position.
77
+ """
78
+ if telescope == "MeerKAT":
79
+ enu = np.loadtxt(os.path.join(pkg_dir, "../data/Meerkat.enu.txt"))
80
+ else:
81
+ print("Only MeerKAT antennas are currentyl available.")
82
+ enu = None
83
+ return enu
84
+
85
+
86
+ def str2bool(v: str):
87
+ """
88
+ Convert string to boolean.
89
+
90
+ Parameters:
91
+ -----------
92
+ v: str
93
+ String to convert to boolean.
94
+
95
+ Raises:
96
+ -------
97
+ argparse.ArgumentTypeError: If the string is not a boolean.
98
+
99
+ Returns:
100
+ --------
101
+ bool: bool
102
+ The boolean value of the string.
103
+ """
104
+ if isinstance(v, bool):
105
+ return v
106
+ if v.lower() in ("yes", "true", "t", "y", "1"):
107
+ return True
108
+ elif v.lower() in ("no", "false", "f", "n", "0"):
109
+ return False
110
+ else:
111
+ raise argparse.ArgumentTypeError("Boolean value expected.")