Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Add plot that shows the max depth of each profile #159

Merged
merged 10 commits into from
Dec 16, 2024
39 changes: 39 additions & 0 deletions glidertest/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,3 +1299,42 @@ def plot_ioosqc(data, suspect_threshold=[25], fail_threshold=[50], title='', ax=
if force_plot:
plt.show()
return fig, ax

def plot_max_depth_per_profile(ds: xr.Dataset, bins= 20, ax = None, **kw: dict) -> tuple({plt.Figure, plt.Axes}):
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add ax as a parameter
ax=None

And add below the

if ax is None:
            fig, ax = plt.subplots()
            force_plot = True
else: 
            fig = plt.gcf()
            force_plot = False

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check the other examples and the end you would put also

if force_plot:
            plt.show()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to:
if ax is None:
fig, ax = plt.subplots(1,2)
force_plot = True
I hope this is correct, since I want to plots, like in plot_outlier_duration()

This function can be used to plot the maximum depth of each profile in a dataset.

Parameters
----------
ds: xarray on OG1 format containing the profile number and the maximum depth.
bins: int, optional (default=20)

Returns
-------
One figure with two plots illustrating the max depth of each profile and a histogram of the max depths

Original author
----------------
Till Moritz
"""
max_depths = tools.max_depth_per_profile(ds)
with plt.style.context(glidertest_style_file):
if ax is None:
fig, ax = plt.subplots(1, 2)
force_plot = True
else:
fig = plt.gcf()
force_plot = False

ax[0].plot(max_depths.profile_num, max_depths,**kw)
ax[0].set_xlabel('Profile number')
ax[0].set_ylabel(f'Max depth ({max_depths.units})')
ax[0].set_title('Max depth per profile')
ax[1].hist(max_depths, bins=bins)
ax[1].set_xlabel(f'Max depth ({max_depths.units})')
ax[1].set_ylabel('Number of profiles')
ax[1].set_title('Histogram of max depth per profile')
[a.grid() for a in ax]
if force_plot:
plt.show()
return fig, ax
22 changes: 22 additions & 0 deletions glidertest/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,25 @@ def compute_global_range(ds: xr.Dataset, var='DOXY', min_val=-5, max_val=600):
out_range = ds[var].where((ds[var]<min_val )| (ds[var]>max_val ))
return out_range.dropna(dim='N_MEASUREMENTS')

def max_depth_per_profile(ds: xr.Dataset):
"""
This function computes the maximum depth for each profile in the dataset

Parameters
----------
ds: xarray on OG1 format containing at least depth and profile_number. Data
should not be gridded.

Returns
-------
max_depths: pandas dataframe containing the profile number and the maximum depth of that profile

Original author
----------------
Till Moritz
"""
max_depths = ds.groupby('profile_num').apply(lambda x: x['DEPTH'].max())
### add the unit to the dataarray
max_depths.attrs['units'] = ds['DEPTH'].attrs['units']
return max_depths

13 changes: 12 additions & 1 deletion notebooks/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@
"plots.plot_glider_track(ds)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "814f48b1",
"metadata": {},
"outputs": [],
"source": [
"# Basic plot of the maximum depth of the dataset per profile\n",
"plots.plot_max_depth_per_profile(ds)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -936,7 +947,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.13.0"
}
},
"nbformat": 4,
Expand Down
6 changes: 5 additions & 1 deletion tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,8 @@ def test_sop():
def test_plot_sampling_period_all():
ds = fetchers.load_sample_dataset()
plots.plot_sampling_period_all(ds)
plots.plot_sampling_period(ds, variable='CHLA')
plots.plot_sampling_period(ds, variable='CHLA')

def test_plot_max_depth():
ds = fetchers.load_sample_dataset()
plots.plot_max_depth_per_profile(ds)
4 changes: 4 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ def test_hyst():
def test_sop():
ds = fetchers.load_sample_dataset()
tools.compute_global_range(ds, var='DOXY', min_val=-5, max_val=600)

def test_maxdepth():
ds = fetchers.load_sample_dataset()
tools.max_depth_per_profile(ds)
Loading