Skip to content

Commit d7e5b1a

Browse files
authored
Merge pull request #177 from knutfrode/dev
Updated traj.velocity_components to return Xarray DataArrays, with Xa…
2 parents 13ae34a + 4c01c2c commit d7e5b1a

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

trajan/traj.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -770,20 +770,39 @@ def azimuth_to_next(self):
770770
return azimuth_forward
771771

772772
def velocity_components(self):
773-
"""Returns velocity components [m/s] from one position to the next.
773+
"""
774+
Calculate velocity components [m/s] from one position to the next.
774775
775-
Last time is repeated for last position (which has no next position)
776+
The last time step is repeated for the last position (which has no next position).
776777
777778
Returns
778779
-------
779-
(u, v) : array_like
780-
East and north components of velocities at given position along trajectories.
780+
u : xarray.DataArray
781+
Eastward velocity component [m/s] at each position along trajectories.
782+
Dimensions: same as the dataset's trajectory and observation dimensions.
783+
v : xarray.DataArray
784+
Northward velocity component [m/s] at each position along trajectories.
785+
Dimensions: same as the dataset's trajectory and observation dimensions.
786+
787+
See Also
788+
--------
789+
speed : Calculate the speed along trajectories.
790+
azimuth_to_next : Calculate the azimuth (direction) to the next position.
781791
"""
782792
speed = self.speed()
783793
azimuth = self.azimuth_to_next()
784794

785-
u = speed * np.cos(azimuth)
786-
v = speed * np.sin(azimuth)
795+
# Calculate velocity components
796+
u = speed * np.cos(np.radians(azimuth))
797+
v = speed * np.sin(np.radians(azimuth))
798+
799+
# Assign names and attributes
800+
u.name = "u_velocity"
801+
v.name = "v_velocity"
802+
u.attrs["units"] = "m/s"
803+
v.attrs["units"] = "m/s"
804+
u.attrs["description"] = "Eastward velocity component"
805+
v.attrs["description"] = "Northward velocity component"
787806

788807
return u, v
789808

0 commit comments

Comments
 (0)