|
4 | 4 |
|
5 | 5 | import numpy as np
|
6 | 6 |
|
| 7 | +from astropy import units as u |
| 8 | + |
7 | 9 | from ..timing import function_timer, Timer
|
8 | 10 |
|
9 | 11 |
|
10 | 12 | @function_timer
|
11 |
| -def simulate_hwp_angle( |
12 |
| - obs, obs_key, hwp_start_s, hwp_rpm, hwp_step_deg, hwp_step_time_m |
| 13 | +def simulate_hwp_response( |
| 14 | + ob, |
| 15 | + ob_angle_key=None, |
| 16 | + ob_mueller_key=None, |
| 17 | + hwp_start=None, |
| 18 | + hwp_rpm=None, |
| 19 | + hwp_step=None, |
| 20 | + hwp_step_time=None, |
13 | 21 | ):
|
14 | 22 | """Simulate and store the HWP angle for one observation.
|
15 | 23 |
|
16 | 24 | Args:
|
17 |
| - obs (Observation): The observation to populate. |
18 |
| - obs_key (str): The observation key for the HWP angle. |
19 |
| - hwp_start_s (float): The mission starting time in seconds of the HWP rotation. |
| 25 | + ob (Observation): The observation to populate. |
| 26 | + ob_time_key (str): The observation shared key for timestamps. |
| 27 | + ob_angle_key (str): (optional) The output observation key for the HWP angle. |
| 28 | + ob_mueller_key (str): (optional) The output observation key for the full |
| 29 | + Mueller matrix. |
| 30 | + hwp_start (Quantity): The mission starting time of the HWP rotation. |
20 | 31 | hwp_rpm (float): The HWP rotation rate in Revolutions Per Minute.
|
21 |
| - hwp_step_deg (float): The HWP step size in degrees. |
22 |
| - hwp_step_time_m (float): The time in minutes between steps. |
| 32 | + hwp_step (Quantity): The HWP step size. |
| 33 | + hwp_step_time (Quantity): The time between steps. |
23 | 34 |
|
24 | 35 | Returns:
|
25 | 36 | None
|
26 | 37 |
|
27 | 38 | """
|
28 |
| - if hwp_rpm is None and hwp_step_deg is None: |
| 39 | + if ob_mueller_key is not None: |
| 40 | + raise NotImplementedError("Mueller matrix not yet implemented") |
| 41 | + |
| 42 | + if hwp_rpm is None and hwp_step is None: |
29 | 43 | # Nothing to do!
|
30 | 44 | return
|
31 | 45 |
|
32 |
| - if (hwp_rpm is not None) and (hwp_step_deg is not None): |
| 46 | + if (hwp_rpm is not None) and (hwp_step is not None): |
33 | 47 | raise RuntimeError("choose either continuously rotating or stepped HWP")
|
34 | 48 |
|
35 |
| - if hwp_step_deg is not None and hwp_step_time_m is None: |
| 49 | + if hwp_step is not None and hwp_step_time is None: |
36 | 50 | raise RuntimeError("for a stepped HWP, you must specify the time between steps")
|
37 | 51 |
|
| 52 | + hwp_start_s = hwp_start.to_value(u.second) |
| 53 | + |
38 | 54 | # compute effective sample rate
|
39 |
| - times = obs.times |
| 55 | + times = ob.shared[ob_time_key] |
40 | 56 | dt = np.mean(times[1:-1] - times[0:-2])
|
41 | 57 | rate = 1.0 / dt
|
42 | 58 |
|
43 | 59 | hwp_rate = None
|
44 |
| - hwp_step = None |
45 |
| - hwp_step_time = None |
| 60 | + hwp_step_rad = None |
| 61 | + hwp_step_time_s = None |
46 | 62 |
|
47 | 63 | if hwp_rpm is not None:
|
48 | 64 | # convert to radians / second
|
49 | 65 | hwp_rate = hwp_rpm * 2.0 * np.pi / 60.0
|
50 | 66 |
|
51 | 67 | if hwp_step_deg is not None:
|
52 | 68 | # convert to radians and seconds
|
53 |
| - hwp_step = hwp_step_deg * np.pi / 180.0 |
54 |
| - hwp_step_time = hwp_step_time_m * 60.0 |
55 |
| - |
56 |
| - first_samp, n_samp = obs.local_samples |
57 |
| - |
58 |
| - obs.shared.create( |
59 |
| - obs_key, shape=(n_samp,), dtype=np.float64, comm=obs.grid_comm_col |
60 |
| - ) |
| 69 | + hwp_step_rad = hwp_set.to_value(u.radian) |
| 70 | + hwp_step_time_s = hwp_step_time.to_value(u.second) |
61 | 71 |
|
62 | 72 | # Only the first process in each grid column simulates the common HWP angle
|
63 | 73 |
|
64 | 74 | start_sample = int(hwp_start_s * rate)
|
| 75 | + first_sample = ob.local_index_offset |
| 76 | + n_sample = ob.n_local_samples |
| 77 | + |
65 | 78 | hwp_angle = None
|
| 79 | + hwp_mueller = None |
66 | 80 |
|
67 |
| - if obs.grid_comm_col is None or obs.grid_comm_col.rank == 0: |
| 81 | + if ob.grid_comm_col is None or ob.grid_comm_col.rank == 0: |
68 | 82 | if hwp_rate is not None:
|
69 | 83 | # continuous HWP
|
70 | 84 | # HWP increment per sample is:
|
71 | 85 | # (hwprate / samplerate)
|
72 | 86 | hwpincr = hwp_rate / rate
|
73 |
| - startang = np.fmod((start_sample + first_samp) * hwpincr, 2 * np.pi) |
74 |
| - hwp_angle = hwpincr * np.arange(n_samp, dtype=np.float64) |
| 87 | + startang = np.fmod((start_sample + first_sample) * hwpincr, 2 * np.pi) |
| 88 | + hwp_angle = hwpincr * np.arange(n_sample, dtype=np.float64) |
75 | 89 | hwp_angle += startang
|
76 | 90 | elif hwp_step is not None:
|
77 | 91 | # stepped HWP
|
78 |
| - hwp_angle = np.ones(n_samp, dtype=np.float64) |
79 |
| - stepsamples = int(hwp_step_time * rate) |
80 |
| - wholesteps = int((start_sample + first_samp) / stepsamples) |
81 |
| - remsamples = (start_sample + first_samp) - wholesteps * stepsamples |
82 |
| - curang = np.fmod(wholesteps * hwp_step, 2 * np.pi) |
| 92 | + hwp_angle = np.ones(n_sample, dtype=np.float64) |
| 93 | + stepsamples = int(hwp_step_time_s * rate) |
| 94 | + wholesteps = int((start_sample + first_sample) / stepsamples) |
| 95 | + remsamples = (start_sample + first_sample) - wholesteps * stepsamples |
| 96 | + curang = np.fmod(wholesteps * hwp_step_rad, 2 * np.pi) |
83 | 97 | curoff = 0
|
84 | 98 | fill = remsamples
|
85 |
| - while curoff < n_samp: |
86 |
| - if curoff + fill > n_samp: |
87 |
| - fill = n_samp - curoff |
| 99 | + while curoff < n_sample: |
| 100 | + if curoff + fill > n_sample: |
| 101 | + fill = n_sample - curoff |
88 | 102 | hwp_angle[curoff:fill] *= curang
|
89 | 103 | curang += hwp_step
|
90 | 104 | curoff += fill
|
91 | 105 | fill = stepsamples
|
92 |
| - if hwp_angle is not None: |
93 |
| - # Choose the HWP angle between [0, 2*pi) |
94 |
| - hwp_angle %= 2 * np.pi |
| 106 | + # Choose the HWP angle between [0, 2*pi) |
| 107 | + hwp_angle %= 2 * np.pi |
| 108 | + |
| 109 | + # Create a Mueller matrix if we will be writing that... |
95 | 110 |
|
96 |
| - obs.shared[obs_key].set(hwp_angle, offset=(0,), fromrank=0) |
| 111 | + # Store the angle and / or the Mueller matrix |
| 112 | + if ob_angle_key is not None: |
| 113 | + ob.shared.create( |
| 114 | + ob_angle_key, shape=(n_sample,), dtype=np.float64, comm=ob.grid_comm_col |
| 115 | + ) |
| 116 | + ob.shared[ob_angle_key].set(hwp_angle, offset=(0,), fromrank=0) |
97 | 117 |
|
98 | 118 | return
|
0 commit comments