Skip to content

Commit

Permalink
modifiy to avoid inplace overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
ssolson committed Sep 19, 2024
1 parent 5417cfe commit 2fc8527
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions mhkit/acoustics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def apply_calibration(
Returns
-------
spsd: xarray.DataArray (time, freq)
spsd_calibrated: xarray.DataArray (time, freq)
Spectral density in Pa^2/Hz, indexed by time and frequency.
"""

Expand All @@ -199,22 +199,25 @@ def apply_calibration(
"'sensitivity_curve' must have 'freq' as one of its dimensions."
)

# Create a copy of spsd to avoid in-place modification
spsd_calibrated = spsd.copy()

# Read calibration curve
freq = sensitivity_curve.dims[0]
# Interpolate calibration curve to desired value
calibration = sensitivity_curve.interp({freq: spsd["freq"]}, method="linear").drop(
freq
)
calibration = sensitivity_curve.interp(
{freq: spsd_calibrated["freq"]}, method="linear"
).drop(freq)
# Fill missing with provided value
calibration = calibration.fillna(fill_value)

# Subtract from sound pressure spectral density
sensitivity_ratio = 10 ** (calibration / 10) # V^2/uPa^2
spsd /= sensitivity_ratio # uPa^2/Hz
spsd /= 1e12 # Pa^2/Hz
spsd.attrs["units"] = "Pa^2/Hz"
spsd_calibrated /= sensitivity_ratio # uPa^2/Hz
spsd_calibrated /= 1e12 # Pa^2/Hz
spsd_calibrated.attrs["units"] = "Pa^2/Hz"

return spsd
return spsd_calibrated


def sound_pressure_spectral_density_level(spsd: xr.DataArray) -> xr.DataArray:
Expand Down

0 comments on commit 2fc8527

Please sign in to comment.