# Building 001 UPS System A Report 
# R, RMarkdown code by eric@focusnumeric.net
# Note: UPS data is not from a real system.  The data was created using Python for illustrative purposes.

```{r echo=FALSE, results='hide'}   
	# Usage in R GUI:  render("UPS.Report.Rmd", output_format="pdf_document",output_file=paste("UPS_Report", Sys.Date(), ".pdf"))
	# set required packages 
	library(rmarkdown)
	library(xtable)
	library(knitr)

	# set number of UPS modules
	# This has been tested - and works.
	numModules <- 4
	
	# set input filename
	inputFileName <- "UPS A Data.csv"
	
	# set these to make indexing the datasets easier.  
	# IMPORTANT!  The numbers refer to the *csv numbers in the inputFileName file COLUMN #1
	# Note:  These numbers refer to rows in the data file I made.  The data file has an entire year of data on it so I just took a subset.  Remember this is made up data anyway!
	SCC.FirstRow <- 2
	SCC.LastRow <- 54
	if (numModules >= 1){
	Mod1.FirstRow <- 678
	Mod1.LastRow <- 730
	}
	if (numModules >= 2){
	Mod2.FirstRow <- 1043
	Mod2.LastRow <- 1095
	}
	if (numModules >= 3){
	Mod3.FirstRow <- 1408
	Mod3.LastRow <- 1460
	}
	if (numModules >=4){
	Mod4.FirstRow <- 1773
	Mod4.LastRow <- 1825
	}
	
	# get file, setup dataframes for SCC, modules.
	UPS <- read.csv(inputFileName, header=TRUE, stringsAsFactors=FALSE)
	SCCData <- UPS[SCC.FirstRow:SCC.LastRow,]
	if (numModules >= 1){
	Mod1Data <- UPS[Mod1.FirstRow:Mod1.LastRow,]
	}
	if (numModules >= 2){
	Mod2Data <- UPS[Mod2.FirstRow:Mod2.LastRow,]
	}
	if (numModules >= 3){
	Mod3Data <- UPS[Mod3.FirstRow:Mod3.LastRow,]
	}
	if (numModules >= 4){
	Mod4Data <- UPS[Mod4.FirstRow:Mod4.LastRow,]
	}
	
	# For debugging
	#```{r, echo=TRUE}
	#print(SCCData$A.Out.V)
	#```
```


```{r, echo=FALSE}
	# Produce Power Factor vector for use later
	# if statement prevents division by zero
	PFLength <- length(SCCData$KVA)
	PF <- vector(length = PFLength)
	for (i in 1:PFLength){
		if (SCCData$KVA[i]==0){
		SCCData$KVA[i]=1
		}
		PF[i] = SCCData$KW[i] / SCCData$KVA[i]
		# print(PF) # For debugging
	}
```

```{r, echo=FALSE}
	# Get statistics for all sets. 
	# SYSTEM CONTROL CABINET
	# SCC Input Voltage
	SCCInputVoltage <- c(SCCData$A.In.V, SCCData$B.In.V, SCCData$C.In.V)
	meanSCCInputVoltage <- mean(SCCInputVoltage)
	minSCCInputVoltage <- min(SCCData$A.In.V, SCCData$B.In.V, SCCData$C.In.V)
	maxSCCInputVoltage <- max(SCCData$A.In.V, SCCData$B.In.V, SCCData$C.In.V)
	# SCC Bypass Voltage 
	SCCBypassVoltage <- c(SCCData$A.By.V, SCCData$B.By.V, SCCData$C.By.V)
	meanSCCBypassVoltage <- mean(SCCBypassVoltage)
	minSCCBypassVoltage <- min(SCCData$A.By.V, SCCData$B.By.V, SCCData$C.By.V)
	maxSCCBypassVoltage <- max(SCCData$A.By.V, SCCData$B.By.V, SCCData$C.By.V)
	# SCC Output Voltage
	SCCOutputVoltage <- c(SCCData$A.Out.V, SCCData$B.Out.V, SCCData$C.Out.V)
	meanSCCOutputVoltage <- mean(SCCOutputVoltage)
	minSCCOutputVoltage <- min(SCCData$A.Out.V, SCCData$B.Out.V, SCCData$C.Out.V)
	maxSCCOutputVoltage <- max(SCCData$A.Out.V, SCCData$B.Out.V, SCCData$C.Out.V)
	# SCC Output Current
	SCCOutputCurrent <- c(SCCData$A.Out.I, SCCData$B.Out.I, SCCData$C.Out.I)
	meanSCCOutputCurrent <- mean(SCCOutputCurrent)
	minSCCOutputCurrent <- min(SCCData$A.Out.I, SCCData$B.Out.I, SCCData$C.Out.I)
	maxSCCOutputCurrent <- max(SCCData$A.Out.I, SCCData$B.Out.I, SCCData$C.Out.I)
	# SCC KVA
	meanKVA <- mean(SCCData$KVA)
	minKVA <- min(SCCData$KVA)
	maxKVA <- max(SCCData$KVA)
	# SCC KW
	meanKW <- mean(SCCData$KW)
	minKW <- min(SCCData$KW)
	maxKW <- max(SCCData$KW)
	# SCC Power Factor
	meanPF <- mean(PF)
	minPF <- min(PF)
	maxPF <- max(PF)
	# SCC Three-Phase 
	meanVA <- mean(SCCData$A.Out.V)
	meanVB <- mean(SCCData$B.Out.V)
	meanVC <- mean(SCCData$C.Out.V)
	minVA <- min(SCCData$A.Out.V)
	minVB <- min(SCCData$B.Out.V)
	minVC <- min(SCCData$C.Out.V)
	maxVA <- max(SCCData$A.Out.V)
	maxVB <- max(SCCData$B.Out.V)
	maxVC <- max(SCCData$C.Out.V)
	allVoltageSCC <- c(SCCData$A.Out.V, SCCData$B.Out.V, SCCData$C.Out.V)
	sdVoltageSCC <- sd(allVoltageSCC)
	meanIA <- mean(SCCData$A.Out.I)
	meanIB <- mean(SCCData$B.Out.I)
	meanIC <- mean(SCCData$C.Out.I)
	minIA <- min(SCCData$A.Out.I)
	minIB <- min(SCCData$B.Out.I)
	minIC <- min(SCCData$C.Out.I)
	maxIA <- max(SCCData$A.Out.I)
	maxIB <- max(SCCData$B.Out.I)
	maxIC <- max(SCCData$C.Out.I)
	allCurrentSCC <- c(SCCData$A.Out.I, SCCData$B.Out.I, SCCData$C.Out.I)
	sdCurrentSCC <- sd(allCurrentSCC)
	
```

	
```{r, echo=FALSE}
	# Graphics Parameters 
	par(mfrow=c(3,1))
	par(xpd=FALSE)
	par(mar=c(5,5,5,5))
```

```{r fig.width=8, fig.height=3.5, echo=FALSE, results='asis'}

	# Plots SCC input voltage

	t <- 1:length(SCCData$A.In.V)
	minVIPlot <- min(c(SCCData$A.In.V, SCCData$B.In.V, SCCData$C.In.V)) - 5
	maxVIPlot <- max(c(SCCData$A.In.V, SCCData$B.In.V, SCCData$C.In.V)) + 5
	# minVIPlot <- 470
	# maxVIPlot <- 485
	
    plot(t, SCCData$A.In.V, main="System Control Cabinet Input Voltage, 2018", xlab = "Week", ylab = "Volts AC", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minVIPlot,maxVIPlot))
	lines(t, SCCData$A.In.V, lty=3)
	points(t, SCCData$A.In.V, pch=0, bg="brown")
	lines(t, SCCData$B.In.V, col="orange", lwd=3)
	lines(t, SCCData$B.In.V, lty=3)
	points(t, SCCData$B.In.V, pch=1, bg="orange")
	lines(t, SCCData$C.In.V, col="yellow", lwd=3)
	lines(t, SCCData$C.In.V, lty=3)
	points(t, SCCData$C.In.V, pch=2, bg="yellow")
    box()
```

<ul> **Input Voltage Summary - System Control Cabinet**
<li> *Mean Input Voltage :* $\mu(V_\text{in})=$ `r round(meanSCCInputVoltage,2)` </li>
<li> *Minimum Input Voltage :* $\text{min}(V_{\text{in}})=$ `r minSCCInputVoltage` </li>
<li> *Maximum Input Voltage :* $\text{max}(V_\text{in})=$ `r maxSCCInputVoltage` </li>
</ul>

```{r fig.width=8, fig.height=3.5, echo=FALSE, results='asis'}

	# Plots SCC bypass voltage

	t <- 1:length(SCCData$A.By.V)
	minVBPlot <- min(c(SCCData$A.By.V, SCCData$B.By.V, SCCData$C.By.V)) - 5
	maxVBPlot <- max(c(SCCData$A.By.V, SCCData$B.By.V, SCCData$C.By.V)) + 5
	# minVBPlot <- 470
	# maxVBPlot <- 485
	
    plot(t, SCCData$A.By.V, main="System Control Cabinet Bypass Voltage, 2018", xlab = "Week", ylab = "Volts AC", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minVBPlot,maxVBPlot))
	lines(t, SCCData$A.By.V, lty=3)
	points(t, SCCData$A.By.V, pch=0, bg="brown")
	lines(t, SCCData$B.By.V, col="orange", lwd=3)
	lines(t, SCCData$B.By.V, lty=3)
	points(t, SCCData$B.By.V, pch=1, bg="orange")
	lines(t, SCCData$C.By.V, col="yellow", lwd=3)
	lines(t, SCCData$C.By.V, lty=3)
	points(t, SCCData$C.By.V, pch=2, bg="yellow")
    box()
```

<ul> **Bypass Voltage Summary - System Control Cabinet**
<li> *Mean Input Voltage :* $\mu(V_\text{in})=$ `r round(meanSCCBypassVoltage,2)` </li>
<li> *Minimum Input Voltage :* $\text{min}(V_{\text{in}})=$ `r minSCCBypassVoltage` </li>
<li> *Maximum Input Voltage :* $\text{max}(V_\text{in})=$ `r maxSCCBypassVoltage` </li>
</ul>

\pagebreak

```{r fig.width=8, fig.height=4., echo=FALSE, results='asis'}

	# Plots SCC output voltage

	t <- 1:length(SCCData$A.Out.V)
	minVOPlot <- min(c(SCCData$A.Out.V, SCCData$B.Out.V, SCCData$C.Out.V)) - 5
	maxVOPlot <- max(c(SCCData$A.Out.V, SCCData$B.Out.V, SCCData$C.Out.V)) + 5
	# minVOPlot <- 470
	# maxVOPlot <- 485
	
    plot(t, SCCData$A.Out.V, main="System Control Cabinet Output Voltage, 2018", xlab = "Week", ylab = "Volts AC", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minVOPlot,maxVOPlot))
	lines(t, SCCData$A.Out.V, lty=3)
	points(t, SCCData$A.Out.V, pch=0, bg="brown")
	lines(t, SCCData$B.Out.V, col="orange", lwd=3)
	lines(t, SCCData$B.Out.V, lty=3)
	points(t, SCCData$B.Out.V, pch=1, bg="orange")
	lines(t, SCCData$C.Out.V, col="yellow", lwd=3)
	lines(t, SCCData$C.Out.V, lty=3)
	points(t, SCCData$C.Out.V, pch=2, bg="yellow")
    box()
```

<ul> **Output Voltage Summary - System Control Cabinet**
<li> *Mean Output Voltage :* $\mu(V_\text{Out})=$ `r round(meanSCCOutputVoltage,2)` </li>
<li> *Minimum Output Voltage :* $\text{min}(V_{\text{Out}})=$ `r minSCCOutputVoltage` </li>
<li> *Maximum Output Voltage :* $\text{max}(V_\text{Out})=$ `r maxSCCOutputVoltage` </li>
</ul>

```{r fig.width=8, fig.height=4., echo=FALSE, results='asis'}

	# Plots SCC output current

	t <- 1:length(SCCData$A.Out.I)
	minIOPlot <- min(c(SCCData$A.Out.I, SCCData$B.Out.I, SCCData$C.Out.I)) - 5
	maxIOPlot <- max(c(SCCData$A.Out.I, SCCData$B.Out.I, SCCData$C.Out.I)) + 5
	# minIOPlot <- 470
	# maxIOPlot <- 485
	
    plot(t, SCCData$A.Out.I, main="System Control Cabinet Output Current, 2018", xlab = "Week", ylab = "Amperage", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minIOPlot,maxIOPlot))
	lines(t, SCCData$A.Out.I, lty=3)
	points(t, SCCData$A.Out.I, pch=0, bg="brown")
	lines(t, SCCData$B.Out.I, col="orange", lwd=3)
	lines(t, SCCData$B.Out.I, lty=3)
	points(t, SCCData$B.Out.I, pch=1, bg="orange")
	lines(t, SCCData$C.Out.I, col="yellow", lwd=3)
	lines(t, SCCData$C.Out.I, lty=3)
	points(t, SCCData$C.Out.I, pch=2, bg="yellow")
    box()
```

<ul> **Output Current Summary - System Control Cabinet**
<li> *Mean Output Current :* $\mu(I_\text{Out})=$ `r round(meanSCCOutputCurrent,2)` </li>
<li> *Minimum Output Current :* $\text{min}(I_{\text{Out}})=$ `r minSCCOutputCurrent` </li>
<li> *Maximum Output Current :* $\text{max}(I_\text{Out})=$ `r maxSCCOutputCurrent` </li>
</ul>

\pagebreak

<ul> **3-Phase Voltage and Current Output Summary**
<li> **Phase A:**
<li> *Mean Voltage Output:* $\mu(V_A)=$ `r round(meanVA,2)` </li> 
<li> *Minimum Voltage Output:* $\text{min}(V_A)=$ `r minVA` </li>
<li> *Maximum Voltage Output:* $\text{max}(V_A)=$ `r maxVA` </li>
</ul>
<ul>
<li> *Mean Current Output:* $\mu(I_A)=$ `r round(meanIA,2)` </li> 
<li> *Minimum Current Output:* $\text{min}(I_A)=$ `r minIA` </li>
<li> *Maximum Current Output:* $\text{max}(I_A)=$ `r maxIA` </li>
</ul>
<ul> **Phase B:**
<li> *Mean Voltage Output:* $\mu(V_B)=$ `r round(meanVB,2)` </li> 
<li> *Minimum Voltage Output:* $\text{min}(V_B)=$ `r minVB` </li>
<li> *Maximum Voltage Output:* $\text{max}(V_B)=$ `r maxVB` </li>
</ul>
<ul>
<li> *Mean Current Output:* $\mu(I_B)=$ `r round(meanIB,2)` </li> 
<li> *Minimum Current Output:* $\text{min}(I_B)=$ `r minIB` </li>
<li> *Maximum Current Output:* $\text{max}(I_B)=$ `r maxIB` </li>
</ul>
<ul> **Phase C:**
<li> *Mean Voltage Output:* $\mu(V_C)=$ `r round(meanVC,2)` </li> 
<li> *Minimum Voltage Output:* $\text{min}(V_C)=$ `r minVC` </li>
<li> *Maximum Voltage Output:* $\text{max}(V_C)=$ `r maxVC` </li>
</ul>
<ul>
<li> *Mean Current Output:* $\mu(I_C)=$ `r round(meanIC,2)` </li>
<li> *Minimum Current Output:* $\text{min}(I_C)=$ `r minIC` </li>
<li> *Maximum Current Output:* $\text{max}(I_C)=$ `r maxIC` </li>
</ul>
- - - - - - - - - - - 
<ul>
<li> *Standard Deviation for voltage output (all phases):* $\sigma(V)=$ `r sdVoltageSCC` </li>
<li> *Standard Deviation for current output (all phases):* $\sigma(I)=$ `r sdCurrentSCC` </li>
</ul>
- - - - - - - - - - - 

# Summary Plots for UPS Output Voltage & Output Current
```{r fig.width=8, fig.height=10, echo=FALSE, results='asis'}

# plots 4 figures arranged in 3 rows and 2 columns.  These are summary statistics plots for UPS output
plotAllVoltage <- 1:length(allVoltageSCC)
plotAllCurrent <- 1:length(allCurrentSCC)
bPhaseMark <- 1/3*length(allVoltageSCC)+1
cPhaseMark <- 2/3*length(allVoltageSCC)+1
par(mfrow=c(3,2))
plot(plotAllVoltage,allVoltageSCC, main="Scatterplot of SCC Output Voltage", col="cadetblue", ylab="Voltage", xlab="Phase A, B, C")
abline(v=bPhaseMark, lty=3)
abline(v=cPhaseMark, lty=3)
plot(plotAllCurrent,allCurrentSCC, main="Scatterplot of SCC Output Current", col="coral3", ylab="Current", xlab="Phase A, B, C")
abline(v=bPhaseMark, lty=3)
abline(v=cPhaseMark, lty=3)
hist(allVoltageSCC, main="Histogram of SCC Output Voltage", col="cadetblue", xlab="Recorded Voltage")
hist(allCurrentSCC, main="Histogram of SCC Output Current", col="coral3", xlab="Recorded Current")
boxplot(allVoltageSCC, main="Boxplot of SCC Output Voltage", col="cadetblue")
boxplot(allCurrentSCC, main="Boxplot of SCC Output Current", col="coral3")

```

\pagebreak

```{r fig.width=8, fig.height=3., echo=FALSE, results='asis'}

	# Plots SCC Load (KVA)

	t <- 1:length(SCCData$KVA)
	minKVAPlot <- min(SCCData$KVA) - 5
	maxKVAPlot <- max(SCCData$KVA) + 5
	# minKVAPlot <- 470
	# maxKVAPlot <- 485
	
    plot(t, SCCData$KVA, main="System Control Cabinet Output Load (KVA), 2018", xlab = "Week", ylab = "Power (KVA)", type="l", col="darkorange", axes=TRUE, lwd = 4, bty='L', ylim=c(minKVAPlot,maxKVAPlot))
	lines(t, SCCData$KVA, lty=3)
	points(t, SCCData$KVA, pch=0, bg="black")
    box()
```

<ul> **Load (KVA) Summary - System Control Cabinet**
<li> *Mean Load (KVA) :* $\mu(P_\text{KVA})=$ `r round(meanKVA,2)` </li>
<li> *Minimum Load (KVA) :* $\text{min}(P_{\text{KVA}})=$ `r minKVA` </li>
<li> *Maximum Load (KVA) :* $\text{max}(P_\text{KVA})=$ `r maxKVA` </li>
</ul>

```{r fig.width=8, fig.height=3., echo=FALSE, results='asis'}

	# Plots SCC Load (KW)

	t <- 1:length(SCCData$KW)
	minKWPlot <- min(SCCData$KW) - 5
	maxKWPlot <- max(SCCData$KW) + 5
	# minKWPlot <- 470
	# maxKWPlot <- 485
	
    plot(t, SCCData$KW, main="System Control Cabinet Output Load (KW), 2018", xlab = "Week", ylab = "Power (KW)", type="l", col="darkred", axes=TRUE, lwd = 4, bty='L', ylim=c(minKWPlot,maxKWPlot))
	lines(t, SCCData$KW, lty=3)
	points(t, SCCData$KW, pch=0, bg="black")
    box()
```

<ul> **Load (KW) Summary - System Control Cabinet**
<li> *Mean Load (KW) :* $\mu(P_\text{KW})=$ `r round(meanKW,2)` </li>
<li> *Minimum Load (KW) :* $\text{min}(P_{\text{KW}})=$ `r minKW` </li>
<li> *Maximum Load (KW) :* $\text{max}(P_\text{KW})=$ `r maxKW` </li>
</ul>

\pagebreak

# Summary Plots for UPS Load in KVA & KW
```{r fig.width=8, fig.height=8, echo=FALSE, results='asis'}

# plots 4 figures arranged in 3 rows and 2 columns.  These are summary statistics plots for UPS Load

plotAllKVA <- 1:length(SCCData$KVA)
plotAllKW <- 1:length(SCCData$KW)
par(mfrow=c(3,2))
plot(plotAllKVA,SCCData$KVA, main="Scatterplot of SCC Load (KVA)", col="darkorange", ylab="Load (KVA)", xlab="Record")
plot(plotAllKW,SCCData$KW, main="Scatterplot of SCC Load (KW)", col="darkred", ylab="Load (KW)", xlab="Record")
hist(SCCData$KVA, main="Histogram of SCC Load (KVA)", col="darkorange", xlab="Recorded Load")
hist(SCCData$KW, main="Histogram of SCC Load (KW)", col="darkred", xlab="Recorded Load")
boxplot(SCCData$KVA, main="Boxplot of SCC Load (KVA)", col="darkorange")
boxplot(SCCData$KW, main="Boxplot of SCC Load (KW)", col="darkred")
```

```{r fig.width=8, fig.height=5., echo=FALSE, results='asis'}

	# Plots SCC Power Factor (calculated)
	
	t <- 1:length(PF)
	minPFPlot <- min(PF) - .05
	maxPFPlot <- max(PF) + .05
	# minPFPlot <- 470
	# maxPFPlot <- 485
	
    plot(t, PF, main="System Control Cabinet Power Factor (calculated), 2018", xlab = "Week", ylab = "Power Factor", type="l", col="purple", axes=TRUE, lwd = 4, bty='L', ylim=c(minPFPlot,maxPFPlot))
	lines(t, PF, lty=3)
	points(t, PF, pch=0, bg="black")
    box()
```

\pagebreak

```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}

# Module 1 Voltage
if (numModules >= 1){
	t <- 1:length(Mod1Data$A.Out.V)
	minMod1VPlot <- min(c(Mod1Data$A.Out.V, Mod1Data$B.Out.V, Mod1Data$C.Out.V)) - 5
	maxMod1VPlot <- max(c(Mod1Data$A.Out.V, Mod1Data$B.Out.V, Mod1Data$C.Out.V)) + 5
	# minMod1VPlot <- 470
	# maxMod1VPlot <- 485
	
    plot(t, Mod1Data$A.Out.V, main="Module 1 Output Voltage, 2018", xlab = "Week", ylab = "Volts AC", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod1VPlot,maxMod1VPlot))
	lines(t, Mod1Data$A.Out.V, lty=3)
	points(t, Mod1Data$A.Out.V, pch=0, bg="brown")
	lines(t, Mod1Data$B.Out.V, col="orange", lwd=3)
	lines(t, Mod1Data$B.Out.V, lty=3)
	points(t, Mod1Data$B.Out.V, pch=1, bg="orange")
	lines(t, Mod1Data$C.Out.V, col="yellow", lwd=3)
	lines(t, Mod1Data$C.Out.V, lty=3)
	points(t, Mod1Data$C.Out.V, pch=2, bg="yellow")
    box()
}
```
```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}

# Module 1 Current
if (numModules >= 1){
	t <- 1:length(Mod1Data$A.Out.I)
	minMod1IPlot <- min(c(Mod1Data$A.Out.I, Mod1Data$B.Out.I, Mod1Data$C.Out.I)) - 5
	maxMod1IPlot <- max(c(Mod1Data$A.Out.I, Mod1Data$B.Out.I, Mod1Data$C.Out.I)) + 5
	# minMod1IPlot <- 470
	# maxMod1IPlot <- 485
	
    plot(t, Mod1Data$A.Out.I, main="Module 1 Output Current, 2018", xlab = "Week", ylab = "Amperes", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod1IPlot,maxMod1IPlot))
	lines(t, Mod1Data$A.Out.I, lty=3)
	points(t, Mod1Data$A.Out.I, pch=0, bg="brown")
	lines(t, Mod1Data$B.Out.I, col="orange", lwd=3)
	lines(t, Mod1Data$B.Out.I, lty=3)
	points(t, Mod1Data$B.Out.I, pch=1, bg="orange")
	lines(t, Mod1Data$C.Out.I, col="yellow", lwd=3)
	lines(t, Mod1Data$C.Out.I, lty=3)
	points(t, Mod1Data$C.Out.I, pch=2, bg="yellow")
    box()
}
```

```{r fig.width=8, fig.height=3, echo=FALSE, results='asis'}
# Module 1 Load (KVA / KW)
if (numModules >= 1){
	t <- 1:length(Mod1Data$KVA)
	loadM1All <- c(Mod1Data$KVA, Mod1Data$KW)
	minMod1PPlot <- min(loadM1All) - 5
	maxMod1PPlot <- max(loadM1All) + 10
	# minMod1PPlot <- 470
	# maxMod1PPlot <- 481
	
    plot(t, Mod1Data$KVA, main="Module 1 Output Load, 2018", xlab = "Week", ylab = "Load", type="l", col="darkorange", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod1PPlot,maxMod1PPlot))
	lines(t, Mod1Data$KVA, lty=3)
	points(t, Mod1Data$KVA, pch=0, bg="darkorange")
	lines(t, Mod1Data$KW, col="darkred", lwd=3)
	lines(t, Mod1Data$KW, lty=3)
	points(t, Mod1Data$KW, pch=1, bg="darkred")
	legend("topleft", legend=c("KVA", "KW"), col=c("darkorange", "darkred"), lty=1:1, cex=0.8)
    box()
}
```

```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}
# Module 1 VDC plot

if (numModules >= 1){

t <- 1:length(Mod1Data$VDC)

	minMod1VDC <- min(Mod1Data$VDC) - 5
	maxMod1VDC <- max(Mod1Data$VDC) + 5
	# minMod1VDC <- 470
	# maxMod1VDC <- 485
	
    plot(t, Mod1Data$VDC, main="Module 1 DC Bus Voltage, 2018", xlab = "Week", ylab = "Load", type="l", col="darkorchid", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod1VDC,maxMod1VDC))
	lines(t, Mod1Data$VDC, lty=3)
	points(t, Mod1Data$VDC, pch=0, bg="darkorchid")
}
```

\pagebreak

```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}

# Module 2 Voltage
if (numModules >= 2){
	t <- 1:length(Mod2Data$A.Out.V)
	minMod2VPlot <- min(c(Mod2Data$A.Out.V, Mod2Data$B.Out.V, Mod2Data$C.Out.V)) - 5
	maxMod2VPlot <- max(c(Mod2Data$A.Out.V, Mod2Data$B.Out.V, Mod2Data$C.Out.V)) + 5
	# minMod2VPlot <- 470
	# maxMod2VPlot <- 485
	
    plot(t, Mod2Data$A.Out.V, main="Module 2 Output Voltage, 2018", xlab = "Week", ylab = "Volts AC", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod2VPlot,maxMod2VPlot))
	lines(t, Mod2Data$A.Out.V, lty=3)
	points(t, Mod2Data$A.Out.V, pch=0, bg="brown")
	lines(t, Mod2Data$B.Out.V, col="orange", lwd=3)
	lines(t, Mod2Data$B.Out.V, lty=3)
	points(t, Mod2Data$B.Out.V, pch=1, bg="orange")
	lines(t, Mod2Data$C.Out.V, col="yellow", lwd=3)
	lines(t, Mod2Data$C.Out.V, lty=3)
	points(t, Mod2Data$C.Out.V, pch=2, bg="yellow")
    box()
}
```
```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}

# Module 2 Current
if (numModules >= 2){
	t <- 1:length(Mod2Data$A.Out.I)
	minMod2IPlot <- min(c(Mod2Data$A.Out.I, Mod2Data$B.Out.I, Mod2Data$C.Out.I)) - 5
	maxMod2IPlot <- max(c(Mod2Data$A.Out.I, Mod2Data$B.Out.I, Mod2Data$C.Out.I)) + 5
	# minMod2IPlot <- 470
	# maxMod2IPlot <- 485
	
    plot(t, Mod2Data$A.Out.I, main="Module 2 Output Current, 2018", xlab = "Week", ylab = "Amperes", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod2IPlot,maxMod2IPlot))
	lines(t, Mod2Data$A.Out.I, lty=3)
	points(t, Mod2Data$A.Out.I, pch=0, bg="brown")
	lines(t, Mod2Data$B.Out.I, col="orange", lwd=3)
	lines(t, Mod2Data$B.Out.I, lty=3)
	points(t, Mod2Data$B.Out.I, pch=1, bg="orange")
	lines(t, Mod2Data$C.Out.I, col="yellow", lwd=3)
	lines(t, Mod2Data$C.Out.I, lty=3)
	points(t, Mod2Data$C.Out.I, pch=2, bg="yellow")
    box()
}
```

```{r fig.width=8, fig.height=3, echo=FALSE, results='asis'}
# Module 2 Load (KVA / KW)
if (numModules >= 2){
	t <- 1:length(Mod2Data$KVA)
	loadM2All <- c(Mod2Data$KVA, Mod2Data$KW)
	minMod2PPlot <- min(loadM2All) - 5
	maxMod2PPlot <- max(loadM2All) + 10
	# minMod2PPlot <- 470
	# maxMod2PPlot <- 485
	
    plot(t, Mod2Data$KVA, main="Module 2 Output Load, 2018", xlab = "Week", ylab = "Load", type="l", col="darkorange", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod2PPlot,maxMod2PPlot))
	lines(t, Mod2Data$KVA, lty=3)
	points(t, Mod2Data$KVA, pch=0, bg="darkorange")
	lines(t, Mod2Data$KW, col="darkred", lwd=3)
	lines(t, Mod2Data$KW, lty=3)
	points(t, Mod2Data$KW, pch=1, bg="darkred")
	legend("topleft", legend=c("KVA", "KW"), col=c("darkorange", "darkred"), lty=1:1, cex=0.8)
    box()
}
```

```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}
# Module 2 VDC plot

if (numModules >= 2){

t <- 1:length(Mod2Data$VDC)

	minMod2VDC <- min(Mod2Data$VDC) - 5
	maxMod2VDC <- max(Mod2Data$VDC) + 5
	# minMod1VDC <- 470
	# maxMod1VDC <- 485
	
    plot(t, Mod2Data$VDC, main="Module 2 DC Bus Voltage, 2018", xlab = "Week", ylab = "Load", type="l", col="darkorchid", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod2VDC,maxMod2VDC))
	lines(t, Mod2Data$VDC, lty=3)
	points(t, Mod2Data$VDC, pch=0, bg="darkorchid")
}
```

\pagebreak

```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}

# Module 3 Voltage
if (numModules >= 3){
	t <- 1:length(Mod3Data$A.Out.V)
	minMod3VPlot <- min(c(Mod3Data$A.Out.V, Mod3Data$B.Out.V, Mod3Data$C.Out.V)) - 5
	maxMod3VPlot <- max(c(Mod3Data$A.Out.V, Mod3Data$B.Out.V, Mod3Data$C.Out.V)) + 5
	# minMod3VPlot <- 470
	# maxMod3VPlot <- 485
	
    plot(t, Mod3Data$A.Out.V, main="Module 3 Output Voltage, 2018", xlab = "Week", ylab = "Volts AC", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod3VPlot,maxMod3VPlot))
	lines(t, Mod3Data$A.Out.V, lty=3)
	points(t, Mod3Data$A.Out.V, pch=0, bg="brown")
	lines(t, Mod3Data$B.Out.V, col="orange", lwd=3)
	lines(t, Mod3Data$B.Out.V, lty=3)
	points(t, Mod3Data$B.Out.V, pch=1, bg="orange")
	lines(t, Mod3Data$C.Out.V, col="yellow", lwd=3)
	lines(t, Mod3Data$C.Out.V, lty=3)
	points(t, Mod3Data$C.Out.V, pch=2, bg="yellow")
    box()
}
```
```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}

# Module 3 Current
if (numModules >= 3){
	t <- 1:length(Mod3Data$A.Out.I)
	minMod3IPlot <- min(c(Mod3Data$A.Out.I, Mod3Data$B.Out.I, Mod3Data$C.Out.I)) - 5
	maxMod3IPlot <- max(c(Mod3Data$A.Out.I, Mod3Data$B.Out.I, Mod3Data$C.Out.I)) + 5
	# minMod3IPlot <- 470
	# maxMod3IPlot <- 485
	
    plot(t, Mod3Data$A.Out.I, main="Module 3 Output Current, 2018", xlab = "Week", ylab = "Amperes", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod3IPlot,maxMod3IPlot))
	lines(t, Mod3Data$A.Out.I, lty=3)
	points(t, Mod3Data$A.Out.I, pch=0, bg="brown")
	lines(t, Mod3Data$B.Out.I, col="orange", lwd=3)
	lines(t, Mod3Data$B.Out.I, lty=3)
	points(t, Mod3Data$B.Out.I, pch=1, bg="orange")
	lines(t, Mod3Data$C.Out.I, col="yellow", lwd=3)
	lines(t, Mod3Data$C.Out.I, lty=3)
	points(t, Mod3Data$C.Out.I, pch=2, bg="yellow")
    box()
}
```

```{r fig.width=8, fig.height=3, echo=FALSE, results='asis'}
# Module 3 Load (KVA / KW)
if (numModules >= 3){
	t <- 1:length(Mod3Data$KVA)
	loadM3All <- c(Mod3Data$KVA, Mod3Data$KW)
	minMod3PPlot <- min(loadM3All) - 5
	maxMod3PPlot <- max(loadM3All) + 10
	# minMod3PPlot <- 470
	# maxMod3PPlot <- 485
	
    plot(t, Mod3Data$KVA, main="Module 3 Output Load, 2018", xlab = "Week", ylab = "Load", type="l", col="darkorange", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod3PPlot,maxMod3PPlot))
	lines(t, Mod3Data$KVA, lty=3)
	points(t, Mod3Data$KVA, pch=0, bg="darkorange")
	lines(t, Mod3Data$KW, col="darkred", lwd=3)
	lines(t, Mod3Data$KW, lty=3)
	points(t, Mod3Data$KW, pch=1, bg="darkred")
	legend("topleft", legend=c("KVA", "KW"), col=c("darkorange", "darkred"), lty=1:1, cex=0.8)
    box()
}
```
```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}
# Module 3 VDC plot

if (numModules >= 3){

t <- 1:length(Mod3Data$VDC)

	minMod3VDC <- min(Mod3Data$VDC) - 5
	maxMod3VDC <- max(Mod3Data$VDC) + 5
	# minMod3VDC <- 470
	# maxMod3VDC <- 485
	
    plot(t, Mod3Data$VDC, main="Module 3 DC Bus Voltage, 2018", xlab = "Week", ylab = "Volts DC", type="l", col="darkorchid", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod3VDC,maxMod3VDC))
	lines(t, Mod3Data$VDC, lty=3)
	points(t, Mod3Data$VDC, pch=0, bg="darkorchid")
}
```

\pagebreak

```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}

# Module 4 Voltage
if (numModules >= 4){
	t <- 1:length(Mod4Data$A.Out.V)
	minMod4VPlot <- min(c(Mod4Data$A.Out.V, Mod4Data$B.Out.V, Mod4Data$C.Out.V)) - 5
	maxMod4VPlot <- max(c(Mod4Data$A.Out.V, Mod4Data$B.Out.V, Mod4Data$C.Out.V)) + 5
	# minMod3VPlot <- 470
	# maxMod3VPlot <- 485
	
    plot(t, Mod4Data$A.Out.V, main="Module 4 Output Voltage, 2018", xlab = "Week", ylab = "Volts AC", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod4VPlot,maxMod4VPlot))
	lines(t, Mod4Data$A.Out.V, lty=3)
	points(t, Mod4Data$A.Out.V, pch=0, bg="brown")
	lines(t, Mod4Data$B.Out.V, col="orange", lwd=3)
	lines(t, Mod4Data$B.Out.V, lty=3)
	points(t, Mod4Data$B.Out.V, pch=1, bg="orange")
	lines(t, Mod4Data$C.Out.V, col="yellow", lwd=3)
	lines(t, Mod4Data$C.Out.V, lty=3)
	points(t, Mod4Data$C.Out.V, pch=2, bg="yellow")
    box()
}
```
```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}

# Module 4 Current
if (numModules >= 4){
	t <- 1:length(Mod4Data$A.Out.I)
	minMod4IPlot <- min(c(Mod4Data$A.Out.I, Mod4Data$B.Out.I, Mod4Data$C.Out.I)) - 5
	maxMod4IPlot <- max(c(Mod4Data$A.Out.I, Mod4Data$B.Out.I, Mod4Data$C.Out.I)) + 5
	# minMod4IPlot <- 470
	# maxMod4IPlot <- 485
	
    plot(t, Mod4Data$A.Out.I, main="Module 4 Output Current, 2018", xlab = "Week", ylab = "Amperes", type="l", col="brown", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod4IPlot,maxMod4IPlot))
	lines(t, Mod4Data$A.Out.I, lty=3)
	points(t, Mod4Data$A.Out.I, pch=0, bg="brown")
	lines(t, Mod4Data$B.Out.I, col="orange", lwd=3)
	lines(t, Mod4Data$B.Out.I, lty=3)
	points(t, Mod4Data$B.Out.I, pch=1, bg="orange")
	lines(t, Mod4Data$C.Out.I, col="yellow", lwd=3)
	lines(t, Mod4Data$C.Out.I, lty=3)
	points(t, Mod4Data$C.Out.I, pch=2, bg="yellow")
    box()
}
```

```{r fig.width=8, fig.height=3, echo=FALSE, results='asis'}
# Module 4 Load (KVA / KW)
if (numModules >= 4){
	t <- 1:length(Mod4Data$KVA)
	loadM4All <- c(Mod4Data$KVA, Mod4Data$KW)
	minMod4PPlot <- min(loadM4All) - 5
	maxMod4PPlot <- max(loadM4All) + 10
	# minMod4PPlot <- 470
	# maxMod4PPlot <- 485
	
    plot(t, Mod4Data$KVA, main="Module 4 Output Load, 2018", xlab = "Week", ylab = "Load", type="l", col="darkorange", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod4PPlot,maxMod4PPlot))
	lines(t, Mod4Data$KVA, lty=3)
	points(t, Mod4Data$KVA, pch=0, bg="darkorange")
	lines(t, Mod4Data$KW, col="darkred", lwd=3)
	lines(t, Mod4Data$KW, lty=3)
	points(t, Mod4Data$KW, pch=1, bg="darkred")
	legend("topleft", legend=c("KVA", "KW"), col=c("darkorange", "darkred"), lty=1:1, cex=0.8)
    box()
}
```
```{r fig.width=8, fig.height=2.5, echo=FALSE, results='asis'}
# Module 4 VDC plot

if (numModules >= 4){

t <- 1:length(Mod4Data$VDC)

	minMod4VDC <- min(Mod4Data$VDC) - 5
	maxMod4VDC <- max(Mod4Data$VDC) + 5
	# minMod4VDC <- 470
	# maxMod4VDC <- 485
	
    plot(t, Mod4Data$VDC, main="Module 4 DC Bus Voltage, 2018", xlab = "Week", ylab = "Volts DC", type="l", col="darkorchid", axes=TRUE, lwd = 3, bty='L', ylim=c(minMod4VDC,maxMod4VDC))
	lines(t, Mod4Data$VDC, lty=3)
	points(t, Mod4Data$VDC, pch=0, bg="darkorchid")
}
```

# Dataset check to verify that the correct set was analyzed
```{r echo=TRUE, results='asis'}

	print(c(SCCData$Bld[1:3], SCCData$Date[1:3],SCCData$Name[1:3],SCCData$AssetID[1:3]))
if (numModules >= 1){	 
	print(c(Mod1Data$Bld[1:3], Mod1Data$Date[1:3],Mod1Data$Name[1:3],Mod1Data$AssetID[1:3]))
}
if (numModules >= 2){
	print(c(Mod2Data$Bld[1:3], Mod2Data$Date[1:3],Mod2Data$Name[1:3],Mod2Data$AssetID[1:3]))
}
if (numModules >= 3){
	print(c(Mod3Data$Date[1:3], Mod3Data$Date[1:3],Mod3Data$Name[1:3],Mod3Data$AssetID[1:3]))
}
if (numModules >= 4){
	print(c(Mod4Data$Date[1:3], Mod4Data$Date[1:3],Mod4Data$Name[1:3],Mod4Data$AssetID[1:3]))
}
```

	
