library(readr) library(reshape) library(ggplot2) # Read in PVWatts yearly data and get rid of total row at the end # Array MP4 PVWatts <- read_csv("~/Documents/Solar/pvwatts_hourly123.csv", col_types = cols_only(Day = col_integer(), Hour = col_integer(), Month = col_integer(), `AC System Output (W)` = col_number()), skip = 17) # Array MP1:3 PVWatts213 <- read_csv("~/Documents/Solar/pvwatts_hourly213.csv", col_types = cols_only(`AC System Output (W)` = col_number()), skip = 17) # Combine the NREL data for the two array azimuths PVWatts$NRELkWh <- (PVWatts$`AC System Output (W)` + PVWatts213$`AC System Output (W)`)/1000 # Remove aggregated total, last item in PVWatts PVWatts <- PVWatts[1:(nrow(PVWatts)-1),] # Create a DateTime for PVWatts PVWatts$DateTime <- as.POSIXct(paste('2019-', PVWatts$Month, '-', PVWatts$Day, ' ', PVWatts$Hour, ':00:00', sep = ''), format = "%Y-%m-%d %H:%M:%S", tz= "America/Los_Angeles") attr(PVWatts$DateTime, "tzone") <- "US/Pacific" # Fix for DST - bring data during DST 1 hour forward start<- which((PVWatts$Month == 3) & (PVWatts$Day == 10) & (PVWatts$Hour == 2)) + 1 end <- which((PVWatts$Month == 11) & (PVWatts$Day == 3) & (PVWatts$Hour == 2)) PVWatts$NRELkWh[start:end] <- PVWatts$NRELkWh[(start-1):(end-1)] PVWatts <- PVWatts[,c(6,5)] # Read in Sense Yearly data Sense <- read_csv("~/Documents/Solar/1-hour data from Jan 01, 2019 to Jan 01, 2020.csv", col_types = cols_only(DateTime = col_datetime(format = "%Y-%m-%d %H:%M:%S"), Name = col_character(), kWh = col_number()), locale = locale(tz = "US/Pacific"), skip = 1) attr(Sense$DateTime, "tzone") <- "US/Pacific" # Filter out just the Total Usage and Solar Production data totals <- c("Solar Production", "Total Usage") Sense <- Sense[Sense$Name %in% totals,] # Reshape to wide format. Fill in empty Sense solar data with NA colnames(Sense) <- c("DateTime", "variable", "value") Sense <- cast(Sense, DateTime ~ variable, sum, fill=NA) # Fix column names for easier plotting later colnames(Sense)[2:3] <- c('SolarProduction', 'TotalUsage') # Merge Sense data witn NREL predictions sense <- merge(Sense, PVWatts, by = 'DateTime') # Create a version of NREL data that matches Sense (- kWh) sense$SolarPredict <- sense$`AC System Output (W)` # Tricky way to zero out energy feeding inverter during night sense$SolarProduction <- as.numeric (!(sense$SolarProduction > 0) & (sense$SolarProduction < 0.004)) * sense$SolarProduction # Convert Sense solar production to positive number sense$SolarProduction <- abs(sense$SolarProduction) # Factorize Hour for coloring plotting sense$Hour <- as.factor(substr(sense$DateTime, 12, 16)) sense$SolarProductionO <- c(sense$SolarProduction[2:nrow(sense)], 0) sense$Month <- factor(months(sense$DateTime, abbr=TRUE), levels = month.abb) # Fix a few July entries with bad data night <- c('18', '19', '20', '21', '22', '23', '00', '01', '02', '03', '04', '05') sense$SolarProduction[((substr(sense$DateTime, 12, 13) %in% night) & (sense$SolarProduction > 0.2) & (!is.na(sense$SolarProduction)))] <- 0.0 # Plot hourly comparison ggplot(sense, aes(x=NRELkWh, y=SolarProduction, color=Hour)) + geom_point() + theme_bw() ggplot(sense, aes(x=NRELkWh, y=SolarProduction, color=Month)) + geom_point() + theme_bw() # Plot Daily comparison sensedays <- aggregate(cbind(SolarProduction, NRELkWh) ~ as.Date(DateTime), sense, FUN = sum) sensedays$Month <- as.factor(substr(sensedays$`as.Date(DateTime)`, 1,7)) ggplot(sensedays, aes(x=SolarProduction, y=NRELkWh, color=Month)) + geom_point() + theme_bw() # Plot monthly comparison sensemonths <- aggregate(cbind(SolarProduction, NRELkWh) ~ substr(DateTime,1,7), sense, FUN = sum) colnames(sensemonths)[1] <- 'Month' ggplot(sensemonths, aes(x=SolarProduction, y=NRELkWh, color=Month)) + geom_point() + theme_bw() # Create linear model (Sense-offset vs. NREL) for just the DST portion of hourly data prodmod1 <- lm(sense$SolarProduction ~ sense$NRELkWh) summary(prodmod1) ggplot(sense[sense$SolarProduction < 3.7,], aes(x=NRELkWh, y=SolarProduction, color=Month)) + geom_point() + theme_bw()