|
| 1 | +# Copyright (c) 2024 Radio Astronomy Software Group |
| 2 | +# Licensed under the 2-clause BSD License |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pyuvdata import ShortDipoleBeam |
| 8 | +from pyuvdata.utils.plotting import get_az_za_grid, plot_beam_arrays |
| 9 | + |
| 10 | + |
| 11 | +def test_plot_arrays(tmp_path): |
| 12 | + pytest.importorskip("matplotlib") |
| 13 | + import matplotlib |
| 14 | + |
| 15 | + matplotlib.use("Agg") # Must be before importing matplotlib.pyplot or pylab! |
| 16 | + |
| 17 | + dipole_beam = ShortDipoleBeam() |
| 18 | + |
| 19 | + az_grid, za_grid = get_az_za_grid() |
| 20 | + az_array, za_array = np.meshgrid(az_grid, za_grid) |
| 21 | + |
| 22 | + beam_vals = dipole_beam.efield_eval( |
| 23 | + az_array=az_array.flatten(), |
| 24 | + za_array=za_array.flatten(), |
| 25 | + freq_array=np.asarray(np.asarray([100e6])), |
| 26 | + ) |
| 27 | + beam_vals = beam_vals.reshape(2, 2, za_grid.size, az_grid.size) |
| 28 | + |
| 29 | + savefile = str(tmp_path / "test.png") |
| 30 | + |
| 31 | + plot_beam_arrays( |
| 32 | + beam_vals, |
| 33 | + az_array, |
| 34 | + za_array, |
| 35 | + complex_type="real", |
| 36 | + beam_type_label="E-field", |
| 37 | + beam_name="short dipole", |
| 38 | + savefile=savefile, |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def test_plot_arrays_errors(): |
| 43 | + pytest.importorskip("matplotlib") |
| 44 | + import matplotlib |
| 45 | + |
| 46 | + matplotlib.use("Agg") # Must be before importing matplotlib.pyplot or pylab! |
| 47 | + |
| 48 | + dipole_beam = ShortDipoleBeam() |
| 49 | + |
| 50 | + az_grid, za_grid = get_az_za_grid() |
| 51 | + az_array, za_array = np.meshgrid(az_grid, za_grid) |
| 52 | + |
| 53 | + beam_vals = dipole_beam.efield_eval( |
| 54 | + az_array=az_array.flatten(), |
| 55 | + za_array=za_array.flatten(), |
| 56 | + freq_array=np.asarray(np.asarray([100e6])), |
| 57 | + ) |
| 58 | + |
| 59 | + with pytest.raises( |
| 60 | + ValueError, |
| 61 | + match="az_array and za_array must be shaped like the last dimension " |
| 62 | + "of beam_vals for irregular beam_vals", |
| 63 | + ): |
| 64 | + plot_beam_arrays(beam_vals[:, :, 0], az_array, za_array) |
| 65 | + |
| 66 | + beam_vals = beam_vals.reshape(2, 2, za_grid.size, az_grid.size) |
| 67 | + |
| 68 | + with pytest.raises(ValueError, match="beam_vals must be 3 or 4 dimensional."): |
| 69 | + plot_beam_arrays(beam_vals[0, 0], az_array, za_array) |
| 70 | + |
| 71 | + with pytest.raises( |
| 72 | + ValueError, |
| 73 | + match="feedpol_label must have the same number of elements as the 1st", |
| 74 | + ): |
| 75 | + plot_beam_arrays(beam_vals, az_array, za_array, feedpol_label=["foo"]) |
| 76 | + |
| 77 | + with pytest.raises( |
| 78 | + ValueError, |
| 79 | + match="az_array and za_array must be shaped like the last 2 dimensions " |
| 80 | + "of beam_vals for regularly gridded beam_vals", |
| 81 | + ): |
| 82 | + plot_beam_arrays(beam_vals, az_array[0], za_array) |
0 commit comments