From bae66b3b40c3a9f6a34a9e32fb6bbca384a7bf06 Mon Sep 17 00:00:00 2001 From: Sam Gardner Date: Sun, 17 Aug 2025 07:35:15 -0500 Subject: [PATCH 1/3] make subset take keywords --- pyxlma/plot/xlma_plot_feature.py | 67 +++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/pyxlma/plot/xlma_plot_feature.py b/pyxlma/plot/xlma_plot_feature.py index 1f01800..73dc8d8 100644 --- a/pyxlma/plot/xlma_plot_feature.py +++ b/pyxlma/plot/xlma_plot_feature.py @@ -6,26 +6,65 @@ from matplotlib.collections import PatchCollection -def subset(lon_data, lat_data, alt_data, time_data, chi_data,station_data, - xlim, ylim, zlim, tlim, xchi, stationmin): +def subset(lon_data=None, lat_data=None, alt_data=None, time_data=None, chi_data=None, station_data=None, + xlim=None, ylim=None, zlim=None, tlim=None, xchi=None, stationmin=None): """ Generate a subset of x,y,z,t of sources based on maximum reduced chi squared and given x,y,z,t bounds Returns: longitude, latitude, altitude, time and boolean arrays """ - selection = ((alt_data>zlim[0])&(alt_dataxlim[0])&(lon_dataylim[0])&(lat_datatlim[0])&(time_data=stationmin) - ) - - alt_data = alt_data[selection] - lon_data = lon_data[selection] - lat_data = lat_data[selection] - time_data = time_data[selection] - return lon_data, lat_data, alt_data, time_data, selection + data_shape = None + for data in [lon_data, lat_data, alt_data, time_data, chi_data, station_data]: + if data is not None: + if data_shape is None: + data_shape = data.shape + elif data_shape != data.shape: + raise ValueError("All input arrays must have the same shape.") + if data_shape is None: + raise ValueError("At least one input array must be provided.") + selection = np.ones(data_shape, dtype=bool) + if xlim is not None: + if lon_data is None: + raise ValueError("Longitude data must be provided to filter by xlim") + else: + selection &= ((lon_data>xlim[0])&(lon_dataylim[0])&(lat_datazlim[0])&(alt_datatlim[0])&(time_data= stationmin) + + things_to_return = [] + if lon_data is not None: + things_to_return.append(lon_data[selection]) + if lat_data is not None: + things_to_return.append(lat_data[selection]) + if alt_data is not None: + things_to_return.append(alt_data[selection]) + if time_data is not None: + things_to_return.append(time_data[selection]) + return *things_to_return, selection def color_by_time(time_array, tlim=None): From 3e10ea4084e62eaacbf861710315cc5592aebe3d Mon Sep 17 00:00:00 2001 From: Sam Gardner Date: Sun, 17 Aug 2025 07:53:49 -0500 Subject: [PATCH 2/3] convert times to np dt64 (allows cross time data and limit types) --- pyxlma/plot/xlma_plot_feature.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyxlma/plot/xlma_plot_feature.py b/pyxlma/plot/xlma_plot_feature.py index 73dc8d8..d4a9019 100644 --- a/pyxlma/plot/xlma_plot_feature.py +++ b/pyxlma/plot/xlma_plot_feature.py @@ -43,7 +43,10 @@ def subset(lon_data=None, lat_data=None, alt_data=None, time_data=None, chi_data if time_data is None: raise ValueError("Time data must be provided to filter by tlim") else: - selection &= ((time_data>tlim[0])&(time_datatlim_array[0])&(time_array Date: Sun, 17 Aug 2025 08:15:51 -0500 Subject: [PATCH 3/3] add test filtering datetime64 by pydt object --- tests/test_plot_feature.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_plot_feature.py b/tests/test_plot_feature.py index 9823f7f..2b7624b 100644 --- a/tests/test_plot_feature.py +++ b/tests/test_plot_feature.py @@ -17,6 +17,16 @@ def test_subset(): '2023-12-24T00:57:07.814960674', '2023-12-24T00:57:07.826344209']).astype(np.datetime64).astype(float)) assert np.sum(selection) == 10 +def test_subset_time_mismatch(): + lma = xr.open_dataset('tests/truth/lma_netcdf/lma.nc') + time_subset, selection = subset(time_data=lma.event_time.data, tlim=(dt(2023, 12, 24, 0, 57, 0), dt(2023, 12, 24, 0, 57, 10))) + assert np.allclose(time_subset[0:10].astype(float), np.array(['2023-12-24T00:57:01.747284125', '2023-12-24T00:57:01.748099340', + '2023-12-24T00:57:01.748382054', '2023-12-24T00:57:01.749366380', + '2023-12-24T00:57:01.749571321', '2023-12-24T00:57:01.751596868', + '2023-12-24T00:57:01.752419634', '2023-12-24T00:57:01.753047708', + '2023-12-24T00:57:01.754500213', '2023-12-24T00:57:01.757822235']).astype(np.datetime64).astype(float)) + assert np.sum(selection) == 2590 + def test_color_by_time_datetime_nolimit(): 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)]) vmin, vmax, colors = color_by_time(some_datetimes)