#=====================================
# Compute average of wind direction vectors
# data: data.table or vector containing wind direction values in degrees respect to N
# var_name: code to identify wind direction data 
#=====================================
circular.avg <- function(data,var_name="wdiragl",units="met_degrees") {

        dir.data <- NULL

        #-------------------------------------
        # Look to see what data was passed in to the function
        #-------------------------------------
        if (is.null(dim(data)) == TRUE)
        {       #-------------------------------------
                # assume that we've been a vector (1-dimensional) of direction vectors, 
                # so we want to average all data contained in this vector
                #-------------------------------------
                # Filter wind direction data
                #-------------------------------------
                dir.data <- list()
                dir.data$values <- data

                #-------------------------------------
                # Compute cos and sin averages over height
                #-------------------------------------
                dir.data$avgsin <- mean(sin(dir.data$values*pi/180),na.rm=TRUE)
                dir.data$avgcos <- mean(cos(dir.data$values*pi/180),na.rm=TRUE)

                #-------------------------------------
                # Averaged direction from 0 to 360
                #-------------------------------------
                data$meandir <- atan2(dir.data$avgsin,dir.data$avgcos)*180/pi
                data$meandir[data$meandir < 0 & is.na(data$meandir) == FALSE] =
                                data$meandir[data$meandir < 0 & is.na(data$meandir) == FALSE] + 360
        }

        else if (is.null(dim(data)) == FALSE)
        {       #-------------------------------------
                # Assume we have given a data table containing one or more columns, to average data     
                #-------------------------------------
                dir.data <- data[,grep(var_name,names(data),value=TRUE),with=FALSE]

                #-------------------------------------
                # In this case, we only have 1 in the whole tower. No averaging.     
                #-------------------------------------
                if (dim(dir.data)[2] == 1) {
                        data$meandir <- dir.data

                }

                else if (dim(dir.data)[2] > 1) {
                        #-------------------------------------
                        # Compute cos and sin averages over height
                        #-------------------------------------
                        dir.data$avgsin <- rowMeans(sin(dir.data*pi/180))
                        dir.data$avgcos <- rowMeans(cos(dir.data*pi/180))

                        #-------------------------------------
                        # Averaged direction from 0 to 360
                        #-------------------------------------
                        data$meandir <- atan2(dir.data$avgsin,dir.data$avgcos)*180/pi
                        data$meandir[data$meandir < 0 & is.na(data$meandir) == FALSE] =
                                        data$meandir[data$meandir < 0 & is.na(data$meandir) == FALSE] + 360
                }
        }

        else
        {       stop("Bad inputs")
        }

        return(data$meandir)
}

