############################################################################################
############################################################################################
# set of functions for ADV coordinate transformation
############################################################################################
###########################################################################################
## created by Marcus Klaus, Swedish University of Agricultural Sciences (SLU), Umeå, Sweden
## contact: marcus.klaus@slu.se
## last update: 2026/04/30
###########################################################################################

# Details:
# Beam coordinates are defined as the velocity measured along the three 
# beams of the instrument.
# ENU coordinates are defined in an earth coordinate system, where
# E represents the East-West component, N represents the North-South
# component and U represents the Up-Down component.

## INPUTS:
# trans_matrix... matrix of transformation values (static) in 3 x 3 matrix
# data_v... velocity matrix (a data.frame)
# position_data... positional data as a data.frame(heading, pitch, roll)

## OUTPUTS:
# ENU.df... transformed velocities

## References:
# code adapted from Jordan Read, R package GDopp ( https://github.com/USGS-R/GDopp )
# which is modified from Transform.m (from http://www.nortek-as.com/en/knowledge-center), which is a Matlab script that shows how 
# velocity data can be transformed between beam coordinates and ENU coordinates.

# Lohrmann, Atle, Ramon Cabrera, and Nicholas C. Kraus. (1994): Acoustic-Doppler velocimeter (ADV) for laboratory use. 
# In Fundamentals and advancements in hydraulic measurements and experimentation, pp. 351-365. ASCE, 1994.

###########################################################################################


transformCoordinatesADV <- function(trans_matrix, data_v, position_data){
  
  trans_matrix1 <- trans_matrix
  
  x <- data_v$velocity.X
  y <- data_v$velocity.Y
  z <- data_v$velocity.Z
  
  xyz <- matrix(data=c(x,y,z), ncol = 3) 
  
  heading <- position_data$heading
  pitch <- position_data$pitch
  roll <- position_data$roll 
  res_trans <- resultant_trans(trans_matrix1, heading, pitch, roll)
  
  ENU <- apply(X = t(xyz), MARGIN = 2, FUN = xyz_2_enu,  
               trans_matrix = trans_matrix1, res_trans = res_trans)#orig
  
  ENU.df <- data.frame(t(ENU)) # orig
  names(ENU.df) <- c('East','North','Up')
  return(ENU.df)
}

resultant_trans <- function(trans_matrix1, heading, pitch, roll){
  
  hh <- pi * (heading - 90) / 180
  pp <- pi * pitch / 180
  rr <- pi * roll / 180
  
  # Make heading matrix
  H <- matrix(data = c(cos(hh), sin(hh), 0, 
                       -sin(hh), cos(hh), 0,   
                       0, 0, 1), ncol = 3)
  
  # Make tilt matrix
  p_data <- c(cos(pp), -sin(pp)*sin(rr), -cos(rr)*sin(pp),
              0, cos(rr), -sin(rr),
              sin(pp), sin(rr)*cos(pp),  cos(pp)*cos(rr))
  
  P <- t(matrix(data = p_data, nrow = 3, ncol = 3)) # orig
  
  # Make resulting transformation matrix
  R <- H %*% P %*% trans_matrix1
  
  return(R)
}

xyz_2_enu <- function(trans_matrix1, res_trans, xyz){
  
  beam <- xyz_2_beam(trans_matrix1, xyz)
  
  # Given beam velocities, ENU coordinates are calculated as
  enu <- beam_2_enu(res_trans, beam)
  return(enu)
}

enu_2_xyz <- function(trans_matrix1, res_trans, enu){
  
  xyz <- trans_matrix1 %*% solve(res_trans) %*% enu
  
  return(xyz)
}

beam_2_enu <- function(res_trans, beam){
  
  enu <- res_trans %*% beam
  
  return(enu)
}

beam_2_xyz <- function(trans_matrix1, beam){
  xyz <- trans_matrix1 %*% beam
}

xyz_2_beam <- function(trans_matrix1, xyz){
  
  beam <- solve(trans_matrix1) %*% xyz 
  return(beam)
}

enu_2_beam <- function(res_trans, enu){
  
  beam <- solve(res_trans) %*% enu 
  return(beam)
  
}
########################################################################################
