Skip to content

Commit

Permalink
Add filter_pace function (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs authored May 6, 2024
1 parent 52e80d7 commit 64d306b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
36 changes: 36 additions & 0 deletions docs/examples/pace.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,42 @@
"source": [
"hypercoast.viz_pace(dataset, wavelengths=[500, 510, 520, 530], ncols=2, crs=\"default\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot a spectral signature."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"latitude = 25.493961\n",
"longitude = -91.25617\n",
"hypercoast.filter_pace(dataset, latitude, longitude, return_plot=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot multiple spectral signatures."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"latitude = (25.49, 25.50)\n",
"longitude = (-92, -91.055)\n",
"hypercoast.filter_pace(dataset, latitude, longitude, return_plot=True)"
]
}
],
"metadata": {
Expand Down
41 changes: 41 additions & 0 deletions hypercoast/pace.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,44 @@ def viz_pace(

plt.tight_layout()
plt.show()


def filter_pace(dataset, latitude, longitude, drop=True, return_plot=False, **kwargs):
"""
Filters a PACE dataset based on latitude and longitude.
Args:
dataset (xr.Dataset): The PACE dataset to filter.
latitude (float or tuple): The latitude to filter by. If a tuple or list, it represents a range.
longitude (float or tuple): The longitude to filter by. If a tuple or list, it represents a range.
drop (bool, optional): Whether to drop the filtered out data. Defaults to True.
Returns:
xr.DataArray: The filtered PACE data.
"""
if isinstance(latitude, list) or isinstance(latitude, tuple):
lat_con = (dataset["latitude"] > latitude[0]) & (
dataset["latitude"] < latitude[1]
)
else:
lat_con = dataset["latitude"] == latitude

if isinstance(longitude, list) or isinstance(longitude, tuple):
lon_con = (dataset["longitude"] > longitude[0]) & (
dataset["longitude"] < longitude[1]
)
else:
lon_con = dataset["longitude"] == longitude

da = dataset["Rrs"].where(lat_con & lon_con, drop=drop, **kwargs)
da_filtered = da.dropna(dim="latitude", how="all")
da_filtered = da_filtered.dropna(dim="longitude", how="all")

if return_plot:
rrs_stack = da_filtered.stack(
{"pixel": ["latitude", "longitude"]},
create_index=False,
)
rrs_stack.plot.line(hue="pixel")
else:
return da_filtered

0 comments on commit 64d306b

Please sign in to comment.