#---------------------------
# This function takes 10-minute averages in those periods which time stamp samplings are lower than 10 minutes
# Function arguments:
#   all.data: Column times must contain the UTC time stamps using POSIXct format (YYYY-MM-DD hh:mm:ss). 
#             Column freq must containg the time sampling interval for each observation (only these time stamp 
#             interval samplings must be considered: "30secondly","minutely","5minutely","6minutely","10minutely",
#             "15minutely","20minutely","30minutely","hourly"). Each of the other columns contain the meteorological 
#             variables 
#--------------------------- 
resize_time_stamps <- function(all.data) {
	#-------------------------------------
        # Only averages data with higher frequency than 10-minutely
        #-------------------------------------
	if (min(as.numeric(lapply(attr(all.data,"freq"),function(x) freq2sec(x)))) < 600) {
       		warning("Found data with very high resolution. Proceed to average them in 10-min intervals")
        	filtvars        <- attr(all.data,"filtvars")
        	heights         <- attr(all.data,"heights")
        	freq            <- attr(all.data,"freq")
        	sensors         <- attr(all.data,"sensors")

        	#-------------------------------------
        	# Average data in 10-min intervals
        	#-------------------------------------
        	# Average wind speed values
        	ws.means <- as.data.table(aggregate(all.data[,grep("^windagl.*",names(all.data),value=T),with=F],
                	                list(times = cut(all.data$times,breaks="10 min")),mean,na.rm = TRUE))

        	# Average wind direction values
        	if (grepl("^wdiragl.*",filtvars)) {
                	wd.means <- as.data.table(aggregate(all.data[,grep("^wdiragl.*",names(all.data),value=T),with=F],
                	                        list(times = cut(all.data$times, breaks="10 min")),circular.avg))

                	all.data <- merge(x = ws.means,y = wd.means,by = "times",all = TRUE)
        	}

        	else { all.data <- ws.means  }

		#-------------------------------------
	       	# Compute time stamp samplings
		#-------------------------------------
        	all.data$times <- ymd_hms(all.data$times)
        	all.data$sampling <- c(diff(all.data$times),diff(all.data$times)[length(diff(all.data$times))-1])

		#-------------------------------------
                # If the have two different time stamp samplings, we need correct the time stamp in the transition
                #-------------------------------------
		all.data$sampling[which(all.data$sampling == 10)[1]-1] <- all.data$sampling[which(all.data$sampling == 10)[1]-2]	
	
	    	#-------------------------------------
        	# Set time stamps in the middle of the averaging interval
        	#-------------------------------------
        	last.ts <- all.data$times[length(all.data$times)] + minutes(all.data$sampling[length(all.data$sampling)]/2)
        	all.data$times[all.data$sampling == 10] <- with_tz(c(rollapply(all.data$times[all.data$sampling == 10],2,
                	                                             function(x) mean.POSIXct(x)),last.ts),"UTC")

		#-------------------------------------
                # Set time stamp sampling using character convention
                #-------------------------------------
        	all.data$freq <- unlist(lapply(all.data$sampling,freq2string))

        	all.data$sampling <- NULL

        	attr(all.data,"freq")           <- unique(all.data$freq)
        	attr(all.data, "filtvars")      <- filtvars
        	attr(all.data, "heights")       <- heights
        	attr(all.data, "sensors")       <- sensors
	}
	
	else {
		print("Resolution of data is 10-minutely or higher. Data have not been averaged")
	}
	
	return(all.data)
}

