# ServiceTimes-ScatterPlot.R
#/----------------------------------------------------------------------------------------------\
#|	Summary:			With a .txt as input, convert line separated values to scatter plot		
#|	Author: 			Ramón L De Jesús				
#|	Start date:			6/25/2013 8:49am						
#|	Last version:		.01								
#\ ---------------------------------------------------------------------------------------------/
#
# This script
# 	-	Creates a .csv file besides the original .txt
# 	-	Displays and saves a .png picture of the scatter plot using R
#
#=====================================================
# Note: R and its dependencies must be installed on the computer
# 			Script only works on linux machines
#			Tested on a VMWare running Ubuntu 12.04 LTS 64-bit
#	~~~~TO RUN:
#	By standalone;
#		Open a terminal and type:
#	$ Rscript ServiceTimes-ScatterPlot.R [nameOfTrace] [folder path] [nameOfFile.csv]
#	WARNING: "/Desktop/TraceFiles/nameOfTrace-DATA/ServiceTimes.csv" MUST EXIST!!!
#=====================================================


#~ Pre-do, aquire Arguments!!!
Args <- commandArgs(TRUE);      # retrieve args

#~Assign #s strings and #i ints
traceName <- Args[1]	#s
newPath <- Args[2]	#s User Working Path
newName <- Args[3]	#s User File Name
title <- paste(traceName, "service elapsed times")	#s Title of plot
xTitle <- "Tests"	#s Title of x axis in plot
yTitle <- "Service Elapsed Times in usec"	#s Title of y axis in plot
pchVal <- 18	#i pch value of plot (see http://2.bp.blogspot.com/_GNAN4W4t4Lk/TJ04wsjoCyI/AAAAAAAAAGQ/sBHowv2D1qg/s1600/pch.png)
imageName <- paste(traceName, "-ServiceTimes-ScatterPlot.png", sep="")	#s Name of image
workingPath <- sprintf("~/Desktop/TraceFiles/%s-DATA",traceName)	#s Path of the .csv
nameOfFile <- paste(traceName, "-ServiceTimes.csv", sep="")	#s name of the file to plot

#~
if(!is.na(newPath))
{
workingPath <- newPath	
}
if(!is.na(newName))
{
nameOfFile <- newName
}
setwd(workingPath)	# cd to .csv file
data<-read.csv(nameOfFile)	# read from .csv file 
maxNumber <- quantile(data$ServiceTimes,0.99)	# to get 99% of the data, else use max(x) x being the Actual Data var for all data
png(filename=imageName, width=800, height=800);  # Tell R it's going to output on an image file
plot(data$ServiceTimes, main=title, xlab=xTitle, ylab=yTitle, pch=pchVal, col="red4", ylim=c(min(data$ServiceTimes),maxNumber));	# plot the graph and output it to imageName
