Skip to content

Commit e9fc3ba

Browse files
authored
Merge pull request #35 from wx4stg/colorbytime
Improve color_by_time
2 parents 46981de + ede97c8 commit e9fc3ba

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

pyxlma/plot/xlma_plot_feature.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@ def subset(lon_data, lat_data, alt_data, time_data, chi_data,station_data,
2525
return lon_data, lat_data, alt_data, time_data, selection
2626

2727

28-
def color_by_time(time_array, tlim):
28+
def color_by_time(time_array, tlim=None):
2929
"""
30-
Generates colormap values for plotting VHF sources by time in a
30+
Generates colormap values for plotting scatter points by time in a
3131
given time window
3232
3333
Returns: min, max values, array by time
3434
"""
35-
vmax = (tlim[1] - time_array.min()).total_seconds()
36-
ldiff = time_array - time_array.min()
37-
ldf = []
38-
for df in ldiff:
39-
ldf.append(df.total_seconds())
40-
c = np.array(ldf)
35+
if tlim is None:
36+
tlim = np.array([np.atleast_1d(time_array.min())[0],
37+
np.atleast_1d(time_array.max())[0]])
38+
nsToS = 1e9
39+
time_array = np.array(time_array).astype('datetime64[ns]').astype(float)/nsToS
40+
tlim = np.atleast_1d(tlim).astype('datetime64[ns]').astype(float)/nsToS
41+
42+
ldiff = time_array - tlim[0]
43+
44+
vmax = tlim[1] - tlim[0]
45+
c = ldiff.astype(float)
4146
vmin = 0
4247

4348
return vmin, vmax, c

tests/test_plot_feature.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,29 @@ def test_subset():
1717
'2023-12-24T00:57:07.814960674', '2023-12-24T00:57:07.826344209']).astype(np.datetime64).astype(float))
1818
assert np.sum(selection) == 10
1919

20-
def test_color_by_time_datetime():
20+
def test_color_by_time_datetime_nolimit():
21+
some_datetimes = np.array([dt(2021, 4, 9, 1, 51, 0), dt(2021, 4, 9, 1, 52, 0), dt(2021, 4, 9, 1, 53, 0), dt(2021, 4, 9, 1, 54, 0), dt(2021, 4, 9, 1, 59, 0)])
22+
vmin, vmax, colors = color_by_time(some_datetimes)
23+
assert vmin == 0
24+
assert vmax == 480
25+
assert np.allclose(colors, np.array([0, 60, 120, 180, 480]))
26+
27+
28+
def test_color_by_time_datetime_limit():
2129
some_datetimes = np.array([dt(2021, 4, 9, 1, 51, 0), dt(2021, 4, 9, 1, 52, 0), dt(2021, 4, 9, 1, 53, 0), dt(2021, 4, 9, 1, 54, 0), dt(2021, 4, 9, 1, 59, 0)])
2230
limits = [dt(2021, 4, 9, 1, 50, 0), dt(2021, 4, 9, 2, 0, 0)]
2331
vmin, vmax, colors = color_by_time(some_datetimes, limits)
2432
assert vmin == 0
25-
assert vmax == 540
26-
assert np.allclose(colors, np.array([0, 60, 120, 180, 480]))
33+
assert vmax == 600
34+
assert np.allclose(colors, np.array([60, 120, 180, 240, 540]))
2735

36+
def test_color_by_time_xarray():
37+
dataset = xr.open_dataset('tests/truth/lma_netcdf/lma.nc')
38+
vmin, vmax, colors = color_by_time(dataset.event_time)
39+
assert vmin == 0
40+
assert np.isclose(vmax, 57.943683385849)
41+
assert np.isclose(np.mean(colors), 30.483982899376258)
42+
assert np.isclose(np.std(colors), 17.25687093241869)
2843

2944
def test_setup_hist():
3045
lma = xr.open_dataset('tests/truth/lma_netcdf/lma.nc')

0 commit comments

Comments
 (0)