Sumanshekhar17 / Geophysical-Data-Analysis

This repository consist of some example to get started with python (examples and materials to begin with python is taken from Kristin Thyng's course on python in geosciences) and focus will be on the Geophysical Data Analysis course lectures, taught by Professor John Wilkin and Professor Bob Chant at Rutgers University.
MIT License
1 stars 0 forks source link

Smooting the Isobath indices #6

Open Sumanshekhar17 opened 1 year ago

Sumanshekhar17 commented 1 year ago

my isobath_coords is a two dimensional array with first column being i indecies extent being 0-242 and second column being j coordinates extent being 0-106, Now when I plot it it produces a transect however the transect is very rough so I went to smooth it by applying gaussian_flitering from scipy on the indices - gaussian_filter(isobath_coords, sigma)

but the indices it gave me was out of the extent, why so? what would be the right way to smoothen my transect?

Sumanshekhar17 commented 1 year ago

The Gaussian filter is not designed to work on indices directly as it can produce values that are out of the array's bounds. Instead, you should apply the Gaussian filter to the data you want to smooth and then extract the indices from the smoothed data.

However, if you need to smooth the indices themselves, you can use a different approach, such as the Savitzky-Golay filter from the SciPy library. The Savitzky-Golay filter is suitable for smoothing unevenly spaced data, such as your isobath_coords.

Here's how you can use the Savitzky-Golay filter to smooth your isobath_coords:

import numpy as np
from scipy.signal import savgol_filter

# Assuming isobath_coords is a 2D numpy array with the first column being i indices and the second column being j indices

# Set the window size and polynomial order for the Savitzky-Golay filter
window_size = 5  # Must be an odd integer
poly_order = 2   # Must be less than or equal to window_size

# Apply the Savitzky-Golay filter to smooth the i and j indices separately
smoothed_i_indices = savgol_filter(isobath_coords[:, 0], window_size, poly_order)
smoothed_j_indices = savgol_filter(isobath_coords[:, 1], window_size, poly_order)

# Combine the smoothed i and j indices into a single 2D array
smoothed_isobath_coords = np.column_stack((smoothed_i_indices, smoothed_j_indices))

# If you need the smoothed indices to be integers, you can round them to the nearest integer
smoothed_isobath_coords = np.round(smoothed_isobath_coords).astype(int)

# Make sure the smoothed indices are within the bounds of your data
smoothed_isobath_coords[:, 0] = np.clip(smoothed_isobath_coords[:, 0], 0, 242)
smoothed_isobath_coords[:, 1] = np.clip(smoothed_isobath_coords[:, 1], 0, 106)

This code will apply the Savitzky-Golay filter to smooth your isobath_coords indices. Note that you might need to adjust the window_size and poly_order parameters to obtain the desired level of smoothing.