#One file!
#inputs a database
#outputs the dance type: chacha or rumba.

#Mini functions

setwd("/home/perovich/Desktop/finalProject/danceStats")
library(plyr)


cutter<-function(x,y){
  #x is the dataset
  #y is the total percentage of the data you want to drop (half from each end) (as a decimal)
  rowcount<-nrow(x)
  rowcutends<-floor(rowcount*y/2)
  x2<-x[rowcutends:(rowcount-rowcutends),]
  return(x2)
}

thecombinator<-function(data, sensorkeep){
  #I'm only doing one sensor in these datasets!  So get rid of the other person for now.
  #(possibly this is different make sensor for one vs the other)
  data<-subset(data, data$sensor==sensorkeep)
  return(data)
}

theanalyzer<-function(data){
  #Analyze--sloppily count maximum in ACF function.
  dataACF<-acf(data$press)
  dataACFcount<-as.data.frame(dataACF$acf)
  dataACFcount$inflect<-0
  for (j in 2:(nrow(dataACFcount)-1))
       dataACFcount$inflect[[j]]<-ifelse(dataACFcount[j, 1]> dataACFcount[j-1, 1] & dataACFcount[j, 1]> dataACFcount[j+1, 1], 1, 0)
  dataACFcountOverall<-sum(dataACFcount$inflect)
  dataresult<-ifelse(dataACFcountOverall<=2, "Rumba", "ChaCha")
  return(dataresult)
  }

getdance<-function(file, sensorkeep){
  data<-read.csv(file)
  data<-cutter(data, 0.05)  
  data<-thecombinator(data, sensorkeep)
  result<-theanalyzer(data)
  #print(result)
  png("/home/perovich/Pictures/dance.png", 1600, 1000)
  plot(0,0, axes=FALSE, type="n", xlab="", ylab="")
  text(0,0, result, cex=18)
  dev.off()
  print(result)
  }
  

#Check that it works on the old stuff
getdance("dancedata.csv", 2)
#getdance("data/laura/irumbaT2.csv", 2)

#Eventually set these constant-ish in here
#file<-
#sensor<-  

#ALRIGHT.  So what now...
#make it be friends with rPy2 somehow
#test it on new data 




#