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

proposed fix for #229 #232

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions clouddrift/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,17 +612,15 @@ def position_from_velocity(
f" {len(x.shape) - 1}])."
)

# Nominal order of axes on input, i.e. (0, 1, 2, ..., N-1)
target_axes = list(range(len(u.shape)))

# If time_axis is not the last one, transpose the inputs
# If time_axis is not the last one, swap axes
if time_axis != -1 and time_axis < len(u.shape) - 1:
target_axes.append(target_axes.pop(target_axes.index(time_axis)))

# Reshape the inputs to ensure the time axis is last (fast-varying)
u_ = np.transpose(u, target_axes)
v_ = np.transpose(v, target_axes)
time_ = np.transpose(time, target_axes)
u_ = np.swapaxes(u, time_axis , -1)
v_ = np.swapaxes(v, time_axis , -1)
time_ = np.swapaxes(time, time_axis , -1)
else:
u_ = u
v_ = v
time_ = time

x = np.zeros(u_.shape, dtype=u.dtype)
y = np.zeros(v_.shape, dtype=v.dtype)
Expand Down Expand Up @@ -659,10 +657,11 @@ def position_from_velocity(
else:
raise ValueError('coord_system must be "spherical" or "cartesian".')

if target_axes == list(range(len(u.shape))):
return x, y
# this was tested before, should we save that test to reuse here?
if time_axis != -1 and time_axis != len(u.shape) - 1:
return np.swapaxes(x, time_axis, -1), np.swapaxes(x, time_axis, -1)
else:
return np.transpose(x, target_axes), np.transpose(y, target_axes)
return x, y


def velocity_from_position(
Expand Down Expand Up @@ -754,17 +753,15 @@ def velocity_from_position(
f" {len(x.shape) - 1}])."
)

# Nominal order of axes on input, i.e. (0, 1, 2, ..., N-1)
target_axes = list(range(len(x.shape)))

# If time_axis is not the last one, transpose the inputs
# If time_axis is not the last one, swap axes
if time_axis != -1 and time_axis < len(x.shape) - 1:
target_axes.append(target_axes.pop(target_axes.index(time_axis)))

# Reshape the inputs to ensure the time axis is last (fast-varying)
x_ = np.transpose(x, target_axes)
y_ = np.transpose(y, target_axes)
time_ = np.transpose(time, target_axes)
x_ = np.swapaxes(x, time_axis, -1)
y_ = np.swapaxes(y, time_axis, -1)
time_ = np.swapaxes(time, time_axis, -1)
else:
x_ = x
y_ = y
time_ = time

dx = np.empty(x_.shape)
dy = np.empty(y_.shape)
Expand Down Expand Up @@ -873,10 +870,11 @@ def velocity_from_position(
'difference_scheme must be "forward", "backward", or "centered".'
)

if target_axes == list(range(len(x.shape))):
return dx / dt, dy / dt
# this was tested before, should we save that test to reuse here?
if time_axis != -1 and time_axis != len(x.shape) - 1:
return np.swapaxes(dx / dt, time_axis, -1), np.swapaxes(dy / dt, time_axis, -1)
else:
return np.transpose(dx / dt, target_axes), np.transpose(dy / dt, target_axes)
return dx / dt, dy / dt


def mask_var(
Expand Down
Loading