# Authors: Fuyu Frank Xu and Kate Beard, School of Computing and Information Science, University of Maine, Orono, Maine
# Function name: STS.eventize3().
# This function can be used to eventize a univariate spatial time series matrix considering event magnitude based on different levels of threshold.
# These thresholds can be either user-specified numerical values or percentiles. This eventization is to categorize the original data to different
# levels of event.
# Description of variables: evtDef--event definition, takes either percentile or absolute value for threshold with the default percentile 0.95.
# event: takes either "greater" or "lower" depending on how to define the event greater or lower than the threshold.
# Modified date: May, 2019
STS.eventize3 <- function(data,evtDef="percentile",magn=2,thres=c(0.85,0.95),event="greater"){
        # ----------------------------------------------- Test for errors if input data is not matrix format ------------------------------ 
        #   
        if(!is.matrix(data)){
            print("!---------------------  ERROR #001  -----------------!")
            print("!     Input data file 'data' is not a matrix!        !")
            print("!----------------------------------------------------!")
            return()
        } # end of matrix input check
        # ----------------------------------------------- Eventization --------------------------------------------------------------------
        tmp = magn
        nr = nrow
        nl = ncol
        
        if(evtDef=="percentile"){
        
            if(event=="greater"){ #ascending percentiles in threshold vector
            
                for(i in 1:nr){
                    for(j in 1:nl){
                        if(data[i,j] >= quantile(data,thres[tmp],na.rm=TRUE)) {data[i,j] = tmp}
                        else if (data[i,j] < quantile(data,thres[1],na.rm=TRUE)) {data[i,j] = 0}
                        else for(k in 1:tmp-1){
                                if (data[i,j] >= quantile(data,thres[k],na.rm=TRUE) && data[i,j] < quantile(data,thres[k+1],na.rm=TRUE)) {data[i,j]= k}
                             } #end of for loop
                    } # end of J loop
                } # end of I loop
                    
            } # end if greater
            
            if(event=="smaller"){ #descending percentiles in threshold vector
                data[data<=quantile(data,thres[tmp],na.rm=TRUE)]=level[tmp]
                data[data>quantile(data,thres[1],na.rm=TRUE)]=0
                for(i in 1:tmp-1){
                    data[data<=quantile(data,thres[i],na.rm=TRUE) & data>quantile(data,thres[i+1],na.rm=TRUE)]=level[i]
                } #end of for loop
            } # end if smaller
        } # end if percentile
        if(evtDef=="absolute"){
        
            if(event=="greater"){ #ascending values in threshold vector
            
                for(i in 1:nr){
                    for(j in 1:nl){
                        if(data[i,j] >= thres[tmp]) {data[i,j] = tmp}
                        else if (data[i,j] < thres[1]) {data[i,j] = 0}
                        else { for(k in 1:tmp-1){
                                if (data[i,j] >= thres[k] && data[i,j] < thres[k+1]) {catv= k} #identify the category value for the observed measurement
                             } #end of for loop
                             data[i,j] = catv }
                    } # end of J loop
                } # end of I loop
            } # end if greater
            
            if(event=="smaller"){
                data[data<=thres[tmp]]=level[tmp]
                data[data>thres[1]]=0
                for(i in 1:tmp-1){
                    data[data<=thres[i] & data>thres[i+1]]=level[i]
                } #end of for loop
            } # end if smaller
        } # end if absolute
        
    return(data)
    } # end function STS.eventize3