text:
<repo_name>Bai-Li-2018/Species_Distribution_Modeling_Tutorial<file_sep>/code/SDM_one_stage.R
setwd("/Users/Bai/Desktop/Species_Distribution_Modeling_Tutorial")
#### Install and library packages ####
list_of_packages <- c("maptools", "rgdal", "mgcv", "classInt","RColorBrewer","maps", "mapdata",  "mgcv", "raster", "SDMTools", "dplyr", "akima", "viridis", "sp", "rgeos")
new_packages <- list_of_packages[!(list_of_packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

library(maptools)
library(rgdal)
library(classInt)
library(RColorBrewer)
library(maps)
library(mapdata)
library(mgcv)
library(raster)
library(SDMTools)
library(dplyr)
library(akima)
library(viridis)
library(sp)
library(rgeos)
#### Load bottom trawl survey data and map shapefile####
load("./data/one_stage_trawl_survey.RData")
head(lobster_catch_data)
summary(lobster_catch_data)

statistical_areas <- readShapePoly("./data/gis/Statistical_Areas_2010.shp", proj4string=CRS("+proj=longlat +datum=WGS84"))
sa511_513 <- statistical_areas[which(statistical_areas@data$Id %in% c(511:513)),]

#### Plot distribution of raw data ####
lobster_catch_data <- lobster_catch_data[-which(is.na(lobster_catch_data[,c("end_depth", "Water_Temp_DegC", "Salinity_psu", "end_lat", "end_lon", "Year", "Month")])),]

map_data <- lobster_catch_data
map_data$log_data <- ifelse(map_data$juv_num==0, NA, log(map_data$juv_num))

var="log_data"
plotvar <- map_data[,var]
nclr=6
plotclr <- rev(brewer.pal(nclr,"RdBu"))
class <- classIntervals(plotvar, nclr, style="equal")
class$brks = round(class$brks, digits=2)
colcode <- findColours(class, plotclr)

start_x <- range(lobster_catch_data$end_lon)[1]
end_x <- range(lobster_catch_data$end_lon)[2]
start_y <- range(lobster_catch_data$end_lat)[1]
end_y <- range(lobster_catch_data$end_lat)[2]

jpeg(filename = paste("./plot/juv_catch_map.jpeg", sep=""), width=100, height=100, units = "mm", res = 600)
layout(matrix(c(1,1,1,3,2,2,2,3), 2, 4, byrow = TRUE))
par(mar=c(4,4,1,1))
plot(map_data$end_lon, map_data$end_lat, pch=16, col=colcode, cex=0.3, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, type="n", xlab="Longitude", ylab="Latitude")
map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE)
#map(coastline, add =T, fill = TRUE, col = "gray95", border="black", lwd=0.2)
points(map_data$end_lon[which(map_data$Month<7&is.na(map_data$log_data))], map_data$end_lat[which(map_data$Month<7&is.na(map_data$log_data))], pch=4, cex=0.3, col="gray")
points(map_data$end_lon[which(map_data$Month<7)], map_data$end_lat[which(map_data$Month<7)], pch=16, col=colcode[which(map_data$Month<7)], cex=0.3)
box()
degAxis(1)
degAxis(2)
legend("topleft", "Spring", bty="n")

plot(map_data$end_lon, map_data$end_lat, pch=16, col=colcode, cex=0.3, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, type="n", xlab="Longitude", ylab="Latitude")
map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE)
#map(coastline, add =T, fill = TRUE, col = "gray95", border="black", lwd=0.2)
points(map_data$end_lon[which(map_data$Month>8&is.na(map_data$log_data))], map_data$end_lat[which(map_data$Month>8&is.na(map_data$log_data))], pch=4, cex=0.3, col="gray")
points(map_data$end_lon[which(map_data$Month>8)], map_data$end_lat[which(map_data$Month>8)], pch=16, col=colcode[which(map_data$Month>8)], cex=0.3)
box()
degAxis(1)
degAxis(2)
legend("topleft", "Fall", bty="n")

par(mar=c(0.5,0.5,0.5,0.5))
plot.new()
legend("left", legend=c(names(attr(colcode, "table")), "No lobster"), fill=c(attr(colcode, "palette"), "gray"), cex=0.9, bty="n", title="Log(#)/Tow")
dev.off()

#### Temperature map ####
var="Water_Temp_DegC"
plotvar <- map_data[,var]
nclr=6
plotclr <- rev(brewer.pal(nclr,"RdBu"))
class <- classIntervals(plotvar, nclr, style="equal")
class$brks = round(class$brks, digits=2)
colcode <- findColours(class, plotclr)

start_x <- range(lobster_catch_data$end_lon)[1]
end_x <- range(lobster_catch_data$end_lon)[2]
start_y <- range(lobster_catch_data$end_lat)[1]
end_y <- range(lobster_catch_data$end_lat)[2]

jpeg(filename = paste("./plot/temperature_map.jpeg", sep=""), width=100, height=100, units = "mm", res = 600)
layout(matrix(c(1,1,1,3,2,2,2,3), 2, 4, byrow = TRUE))
par(mar=c(4,4,1,1))
plot(map_data$end_lon, map_data$end_lat, pch=16, col=colcode, cex=0.3, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, type="n", xlab="Longitude", ylab="Latitude")
map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE)
#map(coastline, add =T, fill = TRUE, col = "gray95", border="black", lwd=0.2)
points(map_data$end_lon[which(map_data$Month<7)], map_data$end_lat[which(map_data$Month<7)], pch=16, col=colcode[which(map_data$Month<7)], cex=0.3)
box()
degAxis(1)
degAxis(2)
legend("topleft", "Spring", bty="n")

plot(map_data$end_lon, map_data$end_lat, pch=16, col=colcode, cex=0.3, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, type="n", xlab="Longitude", ylab="Latitude")
map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE)
#map(coastline, add =T, fill = TRUE, col = "gray95", border="black", lwd=0.2)
points(map_data$end_lon[which(map_data$Month>8)], map_data$end_lat[which(map_data$Month>8)], pch=16, col=colcode[which(map_data$Month>8)], cex=0.3)
box()
degAxis(1)
degAxis(2)
legend("topleft", "Fall", bty="n")

par(mar=c(0.5,0.5,0.5,0.5))
plot.new()
legend("left", legend=c(names(attr(colcode, "table"))), fill=c(attr(colcode, "palette")), cex=0.9, bty="n", title="Temperature")
dev.off()
#### Salinity map ####
var="Salinity_psu"
plotvar <- map_data[,var]
nclr=6
plotclr <- rev(brewer.pal(nclr,"RdBu"))
class <- classIntervals(plotvar, nclr, style="equal")
class$brks = round(class$brks, digits=2)
colcode <- findColours(class, plotclr)

start_x <- range(lobster_catch_data$end_lon)[1]
end_x <- range(lobster_catch_data$end_lon)[2]
start_y <- range(lobster_catch_data$end_lat)[1]
end_y <- range(lobster_catch_data$end_lat)[2]

jpeg(filename = paste("./plot/salinity_map.jpeg", sep=""), width=100, height=100, units = "mm", res = 600)
layout(matrix(c(1,1,1,3,2,2,2,3), 2, 4, byrow = TRUE))
par(mar=c(4,4,1,1))
plot(map_data$end_lon, map_data$end_lat, pch=16, col=colcode, cex=0.3, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, type="n", xlab="Longitude", ylab="Latitude")
map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE)
#map(coastline, add =T, fill = TRUE, col = "gray95", border="black", lwd=0.2)
points(map_data$end_lon[which(map_data$Month<7)], map_data$end_lat[which(map_data$Month<7)], pch=16, col=colcode[which(map_data$Month<7)], cex=0.3)
box()
degAxis(1)
degAxis(2)
legend("topleft", "Spring", bty="n")

plot(map_data$end_lon, map_data$end_lat, pch=16, col=colcode, cex=0.3, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, type="n", xlab="Longitude", ylab="Latitude")
map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE)
#map(coastline, add =T, fill = TRUE, col = "gray95", border="black", lwd=0.2)
points(map_data$end_lon[which(map_data$Month>8)], map_data$end_lat[which(map_data$Month>8)], pch=16, col=colcode[which(map_data$Month>8)], cex=0.3)
box()
degAxis(1)
degAxis(2)
legend("topleft", "Fall", bty="n")

par(mar=c(0.5,0.5,0.5,0.5))
plot.new()
legend("left", legend=c(names(attr(colcode, "table"))), fill=c(attr(colcode, "palette")), cex=0.9, bty="n", title="Salinity")
dev.off()

#### Depth map####
var="end_depth"
plotvar <- map_data[,var]
nclr=6
plotclr <- rev(brewer.pal(nclr,"RdBu"))
class <- classIntervals(plotvar, nclr, style="equal")
class$brks = round(class$brks, digits=2)
colcode <- findColours(class, plotclr)

start_x <- range(lobster_catch_data$end_lon)[1]
end_x <- range(lobster_catch_data$end_lon)[2]
start_y <- range(lobster_catch_data$end_lat)[1]
end_y <- range(lobster_catch_data$end_lat)[2]

jpeg(filename = paste("./plot/depth_map.jpeg", sep=""), width=100, height=100, units = "mm", res = 600)
layout(matrix(c(1,1,1,3,2,2,2,3), 2, 4, byrow = TRUE))
par(mar=c(4,4,1,1))
plot(map_data$end_lon, map_data$end_lat, pch=16, col=colcode, cex=0.3, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, type="n", xlab="Longitude", ylab="Latitude")
map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE)
#map(coastline, add =T, fill = TRUE, col = "gray95", border="black", lwd=0.2)
points(map_data$end_lon[which(map_data$Month<7)], map_data$end_lat[which(map_data$Month<7)], pch=16, col=colcode[which(map_data$Month<7)], cex=0.3)
box()
degAxis(1)
degAxis(2)
legend("topleft", "Spring", bty="n")

plot(map_data$end_lon, map_data$end_lat, pch=16, col=colcode, cex=0.3, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, type="n", xlab="Longitude", ylab="Latitude")
map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE)
#map(coastline, add =T, fill = TRUE, col = "gray95", border="black", lwd=0.2)
points(map_data$end_lon[which(map_data$Month>8)], map_data$end_lat[which(map_data$Month>8)], pch=16, col=colcode[which(map_data$Month>8)], cex=0.3)
box()
degAxis(1)
degAxis(2)
legend("topleft", "Fall", bty="n")

par(mar=c(0.5,0.5,0.5,0.5))
plot.new()
legend("left", legend=c(names(attr(colcode, "table"))), fill=c(attr(colcode, "palette")), cex=0.9, bty="n", title="Depth (Fathom)")
dev.off()

#### GAM ####
summary(lobster_catch_data$juv_num)
hist(lobster_catch_data$juv_num)
trawl_points <- SpatialPoints(cbind(lobster_catch_data$end_lon, lobster_catch_data$end_lat), proj4string=CRS("+proj=longlat +datum=WGS84"))
polygon_points <- over(trawl_points , sa511_513)

gam_model_511 <- gam(juv_num~s(Water_Temp_DegC, k=5)+s(Salinity_psu, k=5)+s(end_lat, k=5)+s(end_lon, k=5), family = tw(), data = lobster_catch_data[which(polygon_points$Id=="511" & lobster_catch_data$Year<2017 & (lobster_catch_data$Month==6 | lobster_catch_data$Month>8)),])
summary(gam_model_511)

jpeg(filename = paste("./plot/gam511_relationship.jpeg", sep=""), width=100, height=100, units = "mm", res = 600)
par(mfrow=c(2,2), mar=c(4,4,1,1))
#plot.gam(gam_model_511, xlab=c("Depth"), ylab="Lobster density", select = 1)
plot.gam(gam_model_511, xlab=c("Temperature"), ylab="Lobster density", select = 2)
plot.gam(gam_model_511, xlab=c("Salinity"), ylab="Lobster density", select = 3)
plot.gam(gam_model_511, xlab=c("Latitude"), ylab="Lobster density", select = 3)
plot.gam(gam_model_511, xlab=c("Longitude"), ylab="Lobster density", select = 4)
dev.off()

gam_model_512 <- gam(juv_num~s(Water_Temp_DegC, k=5)+s(Salinity_psu, k=5)+s(end_lat, k=5)+s(end_lon, k=5), family = tw(), data = lobster_catch_data[which(polygon_points$Id=="512"& lobster_catch_data$Year<2017 & (lobster_catch_data$Month==6 |lobster_catch_data$Month>8)),])
summary(gam_model_512)

jpeg(filename = paste("./plot/gam512_relationship.jpeg", sep=""), width=100, height=100, units = "mm", res = 600)
par(mfrow=c(2,2), mar=c(4,4,1,1))
plot.gam(gam_model_512, xlab=c("Temperature"), ylab="Lobster density", select = 2)
plot.gam(gam_model_512, xlab=c("Salinity"), ylab="Lobster density", select = 3)
plot.gam(gam_model_512, xlab=c("Latitude"), ylab="Lobster density", select = 3)
plot.gam(gam_model_512, xlab=c("Longitude"), ylab="Lobster density", select = 4)
dev.off()

gam_model_513 <- gam(juv_num~s(Water_Temp_DegC, k=5)+s(Salinity_psu, k=5)+s(end_lat, k=5)+s(end_lon, k=5), family = tw(), data = lobster_catch_data[which(polygon_points$Id=="513"& lobster_catch_data$Year<2017 & (lobster_catch_data$Month==6 |lobster_catch_data$Month>8)),])
summary(gam_model_513)

jpeg(filename = paste("./plot/gam513_relationship.jpeg", sep=""), width=100, height=100, units = "mm", res = 600)
par(mfrow=c(2,2), mar=c(4,4,1,1))
plot.gam(gam_model_513, xlab=c("Temperature"), ylab="Lobster density", select = 2)
#plot.gam(gam_model_513, xlab=c("Salinity"), ylab="Lobster density", select = 3)
plot.gam(gam_model_513, xlab=c("Latitude"), ylab="Lobster density", select = 3)
plot.gam(gam_model_513, xlab=c("Longitude"), ylab="Lobster density", select = 4)
dev.off()

save(gam_model_511, gam_model_512, gam_model_513, file="./output/gam_fit.RData")

#### Grid time ID ####
yrs<-2012
dates<-as.data.frame(matrix(NA,length(yrs)*3,3))
colnames(dates)<-c('y','m','d')
dates$y <- rep(yrs, each=3)
dates$d<-rep(16,length(yrs))
dates$m<-rep(c(6,7,8),length(yrs))
dates$date <- do.call(paste, list(dates$m, dates$d, dates$y))
dates$date <- as.Date(dates$date, format=c("%m %d %Y"))
dates$julian_date <- as.numeric(dates$date) + 2440588
dates$modified_julian_date <- floor(dates$julian_date-2400000.5)
dates$modified_julian_date <- dates$modified_julian_date+0.17
#### Download FVCOM time ID ####
fvcom_data <-as.data.frame(read.csv("http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3.ascii?time[0:1:342347]"))
fvcom_time <- as.data.frame(fvcom_data$Dataset..[5:nrow(fvcom_data)])
names(fvcom_time) <-"modified_julian_date"
fvcom_time$modified_julian_date <- as.numeric(as.character(fvcom_time$modified_julian_date))
fvcom_time$id <- 0:(nrow(fvcom_time)-1)

#### Match time ####
time_id <- c()
for(i in 1:nrow(dates)){
    temp <- fvcom_time$id[which(round(fvcom_time$modified_julian_date,2)==round(dates$modified_julian_date[i],2))]
    if(length(temp)==0) time_id[i] <- NA
    else time_id[i] <- temp
}
#### Download FVCOM location data ####
lat <- read.csv("http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3.ascii?lat[0:1:48450]")
lon <- read.csv("http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3.ascii?lon[0:1:48450]")
names(lat) <- "lat"
names(lon) <- "lon"

latitude <- lat$lat[5:nrow(lat)]
longitude <- lon$lon[5:nrow(lon)]
latitude <- as.numeric(as.character(latitude))
longitude <- as.numeric(as.character(longitude))
#### Grid location####
start_x <- floor(range(lobster_catch_data$end_lon)[1])
end_x <- ceiling(range(lobster_catch_data$end_lon)[2])
start_y <- floor(range(lobster_catch_data$end_lat)[1])
end_y <- ceiling(range(lobster_catch_data$end_lat)[2])
my_mesh=expand.grid(seq(start_x, end_x, by=0.01), seq(start_y, end_y, by=0.01))
coordinates(my_mesh) <- ~Var1 + Var2
grid_data <-as.data.frame(my_mesh@coords)
colnames(grid_data) <-c("lon", "lat")
grid_data <- grid_data[which(grid_data$lat>42.9),]

#### Download depth data ####
## Download ARC ASCII from https://www.ngdc.noaa.gov/mgg/coastal/grddas01/grddas01.htm
depth_raster <- raster("./data/gis/ne_atl_crm_v1.asc")
#grid_data$depth <- extract.data(grid_data, depth_raster)
grid_data$depth <- raster::extract(depth_raster, grid_data)
grid_data$fathom <- grid_data$depth/1.8288
write.csv(grid_data, file="./output/grid_depth_data.csv")

#### Download temperature data ####
temperature_fvcom_data<-list()
for (i in 1:length(time_id)){
    print(i)
    temp_data <- read.csv(paste("http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3.ascii?temp[", time_id[i], ":1:", time_id[i], "][44:1:44][0:1:48450]", sep=""))
    temp_data <- as.numeric(as.character(temp_data[(6: nrow(temp_data)),1]))
    temperature_fvcom_data[[i]] <- cbind(longitude, latitude, temp_data)
    colnames(temperature_fvcom_data[[i]]) <- c("lon", "lat", "temperature")
}
save(temperature_fvcom_data, file="./data/temperature_data.RData")

load("./data/temperature_data.RData")
temperature_raster_data <- list()
coordinates(grid_data) <- ~lon + lat
for(i in 1:length(time_id)){
    print(i)
    temp_data <- as.data.frame(temperature_fvcom_data[[i]])
    rast_col <- ceiling((range(temp_data$lon)[2]-range(temp_data$lon)[1])/0.01)
    rast_row <- ceiling((range(temp_data$lat)[2]-range(temp_data$lat)[1])/0.01)
    akima.smooth <- with(temp_data, interp(lon, lat, temperature, nx=rast_col, ny=rast_row))
    rast <- raster(akima.smooth)
    #temperature_raster_data[[i]] <- extract.data(grid_data, rast)
    temperature_raster_data[[i]] <- raster::extract(rast, grid_data)
}
save(temperature_raster_data, file="./output/temperature_raster_data.RData")
#### Download salinity data ####
salinity_fvcom_data<-list()
for (i in 1:length(time_id)){
    print(i)
    temp_data <- read.csv(paste("http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3.ascii?salinity[", time_id[i], ":1:", time_id[i], "][44:1:44][0:1:48450]", sep=""))
    temp_data <- as.numeric(as.character(temp_data[(6: nrow(temp_data)),1]))
    salinity_fvcom_data[[i]] <- cbind(longitude, latitude, temp_data)
    colnames(salinity_fvcom_data[[i]]) <- c("lon", "lat", "salinity")
}
save(salinity_fvcom_data, file="./data/salinity_data.RData")

load("./data/salinity_data.RData")
salinity_raster_data <- list()
for(i in 1:length(time_id)){
    print(i)
    temp_data <- as.data.frame(salinity_fvcom_data[[i]])
    rast_col <- ceiling((range(temp_data$lon)[2]-range(temp_data$lon)[1])/0.01)
    rast_row <- ceiling((range(temp_data$lat)[2]-range(temp_data$lat)[1])/0.01)
    akima.smooth <- with(temp_data, interp(lon, lat, salinity, nx=rast_col, ny=rast_row))
    rast <- raster(akima.smooth)
    #salinity_raster_data[[i]] <- extract.data(grid_data, rast)
    salinity_raster_data[[i]] <- raster::extract(rast, grid_data)
}
save(salinity_raster_data, file="./output/salinity_raster_data.RData")
#### Plot depth grid map ####
#grid_data <- read.csv(file="./output/grid_depth_data.csv")
depth_grid_plot <- read.csv("./output/grid_depth_data.csv")
plot_data <- as.data.frame(cbind(depth_grid_plot$lon, depth_grid_plot$lat, -depth_grid_plot$fathom))
colnames(plot_data) <- c("Longitude", "Latitude", "Y")
depth_odd_id <- which(plot_data$Y<=0)
summary(plot_data)
plot_data <- na.omit(plot_data)
plot_data <- plot_data[which(plot_data$Y>0),]
summary(plot_data)

plotvar <- plot_data$Y
nclr=8
plotclr <- brewer.pal(nclr+1,"Blues")[2:length(brewer.pal(nclr+1,"Blues"))]
class <- classIntervals(plotvar, nclr, style="equal")
class$brks = round(class$brks, digits=2)
colcode <- findColours(class, plotclr)

start_x <- range(lobster_catch_data$end_lon)[1]
end_x <- range(lobster_catch_data$end_lon)[2]
start_y <- range(lobster_catch_data$end_lat)[1]
end_y <- range(lobster_catch_data$end_lat)[2]
jpeg(filename = paste("./plot/grid_depth_map.jpeg", sep=""), width=100, height=50, units = "mm", res = 600)
layout(matrix(c(1,1,1,2), 1, 4, byrow = TRUE))
par(mar=c(4,4,1,1))
plot(plot_data$Longitude, plot_data$Latitude, pch=16, col=colcode, cex=0.1, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, xlab="Longitude", ylab="Latitude")
map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE, lwd=0.1)
box()
degAxis(1)
degAxis(2)
par(mar=c(0.5,0.5,0.5,0.5))
plot.new()
legend("left", legend=c(names(attr(colcode, "table"))), fill=c(attr(colcode, "palette")), cex=0.75, bty="n", title="Depth (Fathom)")
dev.off()

#### Plot temperature grid map ####
load("./output/temperature_raster_data.RData")

jpeg(filename = paste("./plot/grid_temperature_map.jpeg", sep=""), width=120, height=140, units = "mm", res = 600)
par(mar=c(4,4,0,0), mfrow=c(2,2))
plotvar=unlist(temperature_raster_data)
nclr=10
plotclr <- rev(brewer.pal(nclr,"RdYlBu"))
class <- classIntervals(plotvar, nclr, style="equal")
fix_break<-round(class$brks, digits = 2)
for(i in 1:length(temperature_raster_data)){
    print(i)
    plotvar <- temperature_raster_data[[i]]
    nclr=10
    plotclr <- rev(brewer.pal(nclr,"RdYlBu"))
    class <- classIntervals(plotvar, nclr, style="fixed", fixedBreaks=fix_break)
    colcode <- findColours(class, plotclr)

    start_x <- range(lobster_catch_data$end_lon)[1]
    end_x <- range(lobster_catch_data$end_lon)[2]
    start_y <- range(lobster_catch_data$end_lat)[1]
    end_y <- range(lobster_catch_data$end_lat)[2]
    plot(depth_grid_plot$lon, depth_grid_plot$lat, pch=16, col=colcode, cex=0.1, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, xlab="Longitude", ylab="Latitude")
    map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE, lwd=0.1)
    #plot(sa511_513, add=T)
    box()
    legend("topleft", paste(dates$y[i], "-", dates$m[i], sep=""), bty="n", cex=0.7)
    axis(1, cex=0.5)
    axis(2, cex=0.5)
}

par(mar=c(0.1,2,0.1,0.1))
plot.new()
legend("left", legend=c(names(attr(colcode, "table"))), fill=c(attr(colcode, "palette")), cex=0.7, bty="n", title="Temperature (?C)")
dev.off()

#### Plot salinity grid map ####
load("./output/salinity_raster_data.RData")

jpeg(filename = paste("./plot/grid_salinity_map.jpeg", sep=""), width=120, height=140, units = "mm", res = 600)
par(mar=c(4,4,0,0), mfrow=c(2,2))

plotvar=unlist(salinity_raster_data)
nclr=10
plotclr <- rev(brewer.pal(nclr,"RdYlBu"))
class <- classIntervals(plotvar, nclr, style="quantile")
fix_break<-round(class$brks, digits = 2)
for(i in 1:length(salinity_raster_data)){
    print(i)
    plotvar <- salinity_raster_data[[i]]

    nclr=10
    plotclr <- rev(brewer.pal(nclr,"RdYlBu"))
    class <- classIntervals(plotvar, nclr, style="fixed", fixedBreaks=fix_break)
    colcode <- findColours(class, plotclr)

    start_x <- range(lobster_catch_data$end_lon)[1]
    end_x <- range(lobster_catch_data$end_lon)[2]
    start_y <- range(lobster_catch_data$end_lat)[1]
    end_y <- range(lobster_catch_data$end_lat)[2]
    plot(depth_grid_plot$lon, depth_grid_plot$lat, pch=16, col=colcode, cex=0.1, xlim=c(start_x, end_x), ylim=c(start_y, end_y), axes=F, xlab="Longitude", ylab="Latitude")
    map(database = "worldHires", ylim=c(start_y, end_y), xlim=c(start_x, end_x), col = "gray90", fill = TRUE, add = TRUE, lwd=0.1)
    #plot(sa511_513, add=T)
    box()
    legend("topleft", paste(dates$y[i], "-", dates$m[i], sep=""), bty="n", cex=0.7)

    axis(1, cex=0.5)
    axis(2, cex=0.5)
}
par(mar=c(0.1,2,0.1,0.1))
plot.new()
legend("left", legend=c(names(attr(colcode, "table"))), fill=c(attr(colcode, "palette")), cex=0.7, bty="n", title="Salinity (psu)")
dev.off()

<file_sep>/code/Zuur_example.r
#    Mixed Effects Models and Extensions in Ecology with R (2009)
#    Zuur, Ieno, Walker, Saveliev and Smith.    Springer
#    This file was produced by <NAME> (<EMAIL>)
#    www.highstat.com

#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#### Install and load packages ####
list_of_packages <- c("remotes", "car")
new_packages <- list_of_packages[!(list_of_packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

#remotes::install_github("romunov/AED")
library(AED)
library(car)
setwd("/Users/Bai/Desktop/Species_Distribution_Modeling_Tutorial")

Loyn <- read.table(file = "./data/Loyn.txt", header=TRUE, dec = ".")
Loyn$fGRAZE <- factor(Loyn$GRAZE)

op<- par(mfrow=c(4,2),mar=c(3,3,3,1))
dotchart(Loyn$ABUND,main="ABUND",group=Loyn$fGRAZE)
plot(0,0,type="n",axes=F)
dotchart(Loyn$AREA,main="AREA",group=Loyn$fGRAZE)
dotchart(Loyn$DIST,main="DIST",group=Loyn$fGRAZE)
dotchart(Loyn$LDIST,main="LDIST",group=Loyn$fGRAZE)
dotchart(Loyn$YR.ISOL,main="YR.ISOL",group=Loyn$fGRAZE)
dotchart(Loyn$ALT,main="ALT",group=Loyn$fGRAZE)
dotchart(Loyn$GRAZE,main="GRAZE",group=Loyn$fGRAZE)
par(op)

Loyn[which(Loyn$AREA>900),]

Loyn$L.AREA<-log10(Loyn$AREA)
Loyn$L.DIST<-log10(Loyn$DIST)
Loyn$L.LDIST<-log10(Loyn$LDIST)

Z<-cbind(Loyn$ABUND,Loyn$L.AREA,Loyn$L.DIST,Loyn$L.LDIST,Loyn$YR.ISOL,Loyn$ALT,Loyn$GRAZE)
colnames(Z)<-c("ABUND","L.AREA","L.DIST","L.LDIST","YR.ISOL","ALT","GRAZE")
pairs(Z)
?pairs
panel.hist <- function(x, ...)
{
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(usr[1:2], 0, 1.5) )
    h <- hist(x, plot = FALSE)
    breaks <- h$breaks; nB <- length(breaks)
    y <- h$counts; y <- y/max(y)
    rect(breaks[-nB], 0, breaks[-1], y, col = "cyan", ...)
}
panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
{
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r <- abs(cor(x, y))
    txt <- format(c(r, 0.123456789), digits = digits)[1]
    txt <- paste0(prefix, txt)
    if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
    text(0.5, 0.5, txt, cex = cex.cor * r)
}
pairs(Z, panel=panel.smooth, diag.panel = panel.hist, upper.panel = panel.cor)

M1 <- lm(ABUND ~ L.AREA + L.DIST + L.LDIST + YR.ISOL + ALT +
     fGRAZE, data = Loyn)
vif(M1)

summary(M1)
drop1(M1,test="F")

#Verify drop1

M1A <- lm(ABUND ~ L.AREA + L.DIST + L.LDIST + YR.ISOL + ALT +
     fGRAZE, data = Loyn)

M1B <- lm(ABUND ~ L.DIST + L.LDIST + YR.ISOL + ALT +
     fGRAZE, data = Loyn)

anova(M1A,M1B)

#Model 1: ABUND ~ L.AREA + L.DIST + L.LDIST + YR.ISOL + ALT + fGRAZE
#Model 2: ABUND ~ L.DIST + L.LDIST + YR.ISOL + ALT + fGRAZE
#  Res.Df     RSS Df Sum of Sq      F   Pr(>F)
#1     46 1714.43
#2     47 2484.44 -1   -770.01 20.660 3.97e-05 ***

M3 <- lm(ABUND ~ L.AREA + fGRAZE, data = Loyn)
op <- par(mfrow = c(2, 2))
plot(M3) #standard graphical output
op <- par(mfrow = c(2, 2))
#Check for normality
E <- rstandard(M3)
hist(E)
#qqnorm(E)
#Check for independence: residuals versus individual #explanatory variables
plot(y = E, x = Loyn$L.AREA, xlab = "AREA", ylab = "Residuals")
abline(0,0)
plot(E ~ Loyn$fGRAZE, xlab = "GRAZE", ylab = "Residuals")
abline(0, 0)
par(op)

#Loyn$SL.AREA<-sort(Loyn$L.AREA)
D1<-data.frame(L.AREA=Loyn$L.AREA[Loyn$GRAZE==1],fGRAZE="1")
D2<-data.frame(L.AREA=Loyn$L.AREA[Loyn$GRAZE==2],fGRAZE="2")
D3<-data.frame(L.AREA=Loyn$L.AREA[Loyn$GRAZE==3],fGRAZE="3")
D4<-data.frame(L.AREA=Loyn$L.AREA[Loyn$GRAZE==4],fGRAZE="4")
D5<-data.frame(L.AREA=Loyn$L.AREA[Loyn$GRAZE==5],fGRAZE="5")

P1<-predict(M3,newdata=D1)
P2<-predict(M3,newdata=D2)
P3<-predict(M3,newdata=D3)
P4<-predict(M3,newdata=D4)
P5<-predict(M3,newdata=D5)

D1<-data.frame(L.AREA = Loyn$L.AREA, fGRAZE = "1")
P1<-predict(M3, newdata = D1)

plot(Loyn$L.AREA,Loyn$ABUND)
lines(D1$L.AREA,P1,lty=1)
lines(D2$L.AREA,P2,lty=2)
lines(D3$L.AREA,P3,lty=3)
lines(D4$L.AREA,P4,lty=4)
lines(D5$L.AREA,P5,lty=5)

library(mgcv)
AM1<-gam(ABUND~s(L.AREA)+s(L.DIST)+s(L.LDIST)+
    s(YR.ISOL)+s(ALT)+fGRAZE, data = Loyn)
anova(AM1)

AM2<-gam(ABUND ~ s(L.AREA, bs = "cs") + s(L.DIST, bs = "cs") +
                 s(L.LDIST,bs = "cs") + s(YR.ISOL, bs = "cs") +
                 s(ALT, bs = "cs") + fGRAZE, data = Loyn)
anova(AM2)

AM3 <- gam(ABUND ~ s(L.AREA, bs = "cs") + fGRAZE, data = Loyn)
plot(AM3)

E.AM3 <- resid(AM3)
Fit.AM3 <- fitted(AM3)
scatter.smooth(Fit.AM3, E.AM3, xlab = "Fitted values",
               ylab = "Residuals")
legend("bottomleft", "GAM")
scatter.smooth(M3$fitted.values, M3$residuals, xlab = "Fitted values", ylab = "Residuals")
legend("bottomleft", "LM")

M3<-lm(ABUND ~ L.AREA + fGRAZE, data = Loyn)
AM3<-gam(ABUND ~ s(L.AREA, bs = "cs") + fGRAZE, data = Loyn)
anova(M3, AM3)

<file_sep>/README.md
# Species_Distribution_Modeling_Tutorial
Here contains a few simple examples in species distribution modeling.

**Before you arrive:**
Please **make sure your R and Rstudio are installed**

1. Download [R](http://archive.linux.duke.edu/cran/) for Windows, (Mac) OS X, or Linux.

2. Downlad [Rstudio](https://rstudio.com/products/rstudio/download/#download).

3. You can download (green download zip button above) this repositority directly to your computer.

4. The input data, code, output, and figure are saved in different folders. Feel free to explore it before the class.

5. Open "SDM_two_stages.R" file with Rstudio.

6. Modify line 23 to set up your working directory:

```R
setwd("/Users/Bai/Google Drive/Bai/Forms/2020/UMAINE_Teaching_0429/Species_Distribution_Modelling_Tutorial")
```

7. Run the script.
<file_sep>/code/SDM_two_stages.R
#### Example Description ####
## In this example, we developed a generalized additive model (GAM) to quantify the relationship between habitat variables and presence & density of juvenile lobsters using the data from ME-NH Inshore Bottom Trawl Survey.
## The first stage of the model is to predict the presence/absence of juvenile lobster at each site, and the second stage of the model is to predict the density of lobsters at each site.
## Input data:
## 1) two_stages_trawl_survey.csv
## 2) two_stages_environmental_data.csv

#### Install and load packages ####
list_of_packages <- c("rstudioapi", "maptools", "mgcv", "classInt", "RColorBrewer", "maps", "mapdata", "fields")
new_packages <- list_of_packages[!(list_of_packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

library(rstudioapi)
library(maptools)
library(maps)
library(mapdata)
library(mgcv)
library(RColorBrewer)
library(classInt)
library(fields)

#### Set working directory and library required packages ####
setwd("/Users/Bai/Desktop/Species_Distribution_Modeling_Tutorial")

mndata<-read.csv('./data/two_stages_trawl_survey.csv') # Read Maine-New Hampshier Bottom Trawl Survey data. Lobster (< 60 mm CL) abundance/ habitat variables (Bottom water temperature/ Salinity/ Sediment Type/ Latitude/ Longitude/ Depth/ Distance Offshore/ Distance to Sediment Boundary). There are 473 observations.

predictiondata<-read.csv('./data/two_stages_environmental_data.csv') # Read potential sampling station data. Station Latitude/ Longitude/ Bottom water temperature/ Salinity/ Sediment Type/ Depth/ Distance Offshore/ Distance to Sediment Boundary. The temperature data are from September 2000. There are 700 observations.

#### 1.1 Build GAM ####
# Presence/ Absence GAM
mndata$presence <- (mndata$abundance > 0) * 1 # Presence = 1 if the station has lobster, otherwise presence = 0

presence.full<-gam(presence ~ s(Latitude, k=5)+s(Longitude, k=5)+s(Temperature, k=5)+s(Salinity, k=5)+s(Distance_Offshore, k=5)+s(Depth, k=5)+s(Distance_Sediment_Boundary, k=5)+as.factor(Sediment_Type), family=binomial(logit), data=mndata) # Build first stage GAM with all possible habitat variables
summary(presence.full) # Find significant variables based on p-value

presence.sig<-gam(presence ~ s(Latitude, k=5)+s(Longitude, k=5)+s(Temperature, k=5)+s(Distance_Offshore, k=5),family=binomial(logit),data=mndata) # Build first stage GAM with significant habitat variables
summary(presence.sig)
par(mfrow=c(2,2))
gam.check(presence.sig)

# Abundance GAM
abundance.full<-gam(log(abundance) ~ s(Latitude, k=5)+s(Longitude, k=5)+s(Temperature, k=5)+s(Salinity, k=5)+s(Distance_Offshore, k=5)+s(Depth, k=5)+s(Distance_Sediment_Boundary, k=5)+as.factor(Sediment_Type), family=gaussian,data=subset(mndata,abundance>0)) # Build second stage GAM with all possible habitat variables
summary(abundance.full) # Find significant variables based on p-value

abundance.sig<-gam(log(abundance) ~s(Latitude, k=5)+s(Longitude, k=5)+s(Temperature, k=5)+s(Depth, k=5), family=gaussian, data=subset(mndata,abundance>0)) # Build first stage GAM with significant habitat variables
summary(abundance.sig)
par(mfrow=c(2,2))
gam.check(abundance.sig)

# Plot GAM response curves
par(mar=c(4,4,1,1))
layout(matrix(1:8, ncol=2, byrow=FALSE))
plot(presence.sig, select =1, scale =0,ylab = expression(bold(Presence)), xlab = expression(bold(Latitude)))
plot(presence.sig, select =2, scale =0,ylab = expression(bold(Presence)), xlab = expression(bold(Longitude)))
plot(presence.sig, select =3, scale =0,ylab = expression(bold(Presence)), xlab = expression(bold(Temperature)))
plot(presence.sig, select =4, scale =0,ylab = expression(bold(Presence)), xlab = expression(bold(Distance~Offshore)))
plot(abundance.sig, select =1, scale =0,ylab = expression(bold(Abundance)), xlab = expression(bold(Latitude)))
plot(abundance.sig, select =2, scale =0,ylab = expression(bold(Abundance)), xlab = expression(bold(Longitude)))
plot(abundance.sig, select =3, scale =0,ylab = expression(bold(Abundance)), xlab = expression(bold(Temperature)))
plot(abundance.sig, select =4, scale =0,ylab = expression(bold(Abundance)), xlab = expression(bold(Depth)))

#### 1.2 Cross Validation ####
mndata_cv <- mndata

mndata_cv  = mndata_cv [!is.na(mndata_cv $Latitude) & !is.na(mndata_cv $Longitude) & !is.na(mndata_cv $Temperature) & !is.na(mndata_cv $Depth) & !is.na(mndata_cv $Sediment_Type) & !is.na(mndata_cv $Distance_Offshore) & !is.na(mndata_cv $Salinity) & !is.na(mndata_cv $Distance_Sediment_Boundary),]#Remove NAs

N = nrow(mndata_cv )
res = matrix(0, ncol = 3, nrow = 100)
colnames(res) = c("Intercept", "Slope", "R.Squared")
for (i in 1:100){
    print(i)
    sub = sample(1:N, size = N/4)
    train = mndata_cv[-sub,]
    test = mndata_cv[sub,]
    for (j in 1: length(test$abundance)) {
        if (test$abundance[j] == 0 | test$abundance[j] < 0){
            test$abundance[j] = 0.0001
        } else {
            test$abundance[j] = test$abundance[j]
        }
    }
    logtest = log(test$abundance)

    presence_cv <- gam(presence ~ s(Latitude, k=5)+s(Longitude,k=5)+s(Temperature,k=5)+s(Distance_Offshore),family=binomial(logit), data=train, select=TRUE, method="REML")

    abundance_cv <- gam(log(abundance) ~ s(Latitude, k=5)+s(Longitude,k=5)+s(Temperature,k=5)+s(Depth), family=gaussian, data=subset(train,abundance>0), select=TRUE, method="REML")

    prepresence = predict.gam(presence_cv, test, se.fit=TRUE, type = "response")
    prep = as.matrix(prepresence$fit)
    prev = as.matrix(prepresence$se.fit)
    predensity=predict.gam(abundance_cv, test, se.fit=TRUE, type = "response")
    preden=as.matrix(predensity$fit)
    prevar=as.matrix(predensity$se.fit)
    #newdensity=prep * exp(preden)
    newdensity = log(prep) + preden
    #lognewdensity = log(newdensity)
    result = cbind(logtest, newdensity) #lognewdensity)
    final = result[result[,1]>0 & result[,2]>0,]
    q = lm(final[,1] ~ final[,2], data = as.data.frame(final))
    res[i,] = c(summary(q)$coefficient[,1], summary(q)$r.squared)
}
summary(res)
par(mfrow=c(1,1))
plot(NULL,xlim=c(0,7),ylim=c(0,7), xlab = expression(bold("Predicted Lobsters (log #)")),
     ylab = expression(bold("Observed Lobsters (log #)")), xaxt="n", yaxt="n")
axis(side=1, font=2)
axis(side=2, font=2)
addlines <- function(x) abline(x, col="gray70")
apply(res[,-3],1,addlines)

abline(a = mean(res[,1]), b = mean(res[,2]), col="black", lwd=3)
abline(0,1,lty =2, lwd=3)
text(2, 6.5, expression(bold(italic(y) == 0.65 + 0.86 %.% italic(x)*","~~italic(r)^2~"="~0.36)))

#### 1.3 GAM Prediction ####
prepresence = predict.gam(presence.sig, predictiondata, se.fit=TRUE, type = "response") # Fit first stage GAM with potential sampling station data
p.presence = as.matrix(prepresence$fit) # Presence values of lobsters at potential sampling stations
p.presencevar = as.matrix(prepresence$se.fit^2) # Associated variance of presence after fitting model
preabundance=predict.gam(abundance.sig, predictiondata, se.fit=TRUE) # Fit second stage GAM with potential sampling station data
p.abundance=as.matrix(preabundance$fit) # Abundance values of lobsters at potential sampling stations
p.abundancevar=as.matrix(preabundance$se.fit^2) # Associated variance of abundance after fitting model

#### 1.4 Combine two-stage GAM ####
simabundance = p.presence * p.abundance # Predicted lobster abundance at potential sampling stations by combining results from two stages
simvar = p.presence * p.abundancevar + (p.abundance)^2 * p.presence *(1-p.presence) # Associated variance of simulated abundance at potential sampling stations

#### 1.5 Plot simulated abundance ####
colors <- brewer.pal(9, "YlOrRd")
colbrks<-classIntervals(simabundance, n=9, style="quantile")
brks<- colbrks$brks
par(mfrow=c(1,1)); par(mar=c(4,4,1,1))
plot(predictiondata$Longitude, predictiondata$Latitude, col=colors[findInterval(simabundance, brks ,all.inside=TRUE)], pch = 20, xlab="Longitude", ylab="Latitude")


text:
<file_sep>shinyUI(pageWithSidebar(
    headerPanel("Time Series Plotting"),
    sidebarPanel(
        h2('This application plots a time-series with sporadic missing entries'),
        dateInput("startDate", "Start Date:",
                  value = '1/1/2010', min ='12/31/2006'),
        dateInput("endDate", "End Date:",
                  value = '8/13/2015', max ='8/13/2015'),
        selectInput('type', "Type of Plot",
                    choices=c("points", "line", "both"),
                    selected = "line", multiple = FALSE,
                    selectize = TRUE),
        radioButtons("fitting", "Type of Fitting",
                     choices = list("linear" = "linear", "loess" = "loess"),
                     selected = "linear"),
        h2('Documentation'),
        h4('This page analyzes daily data from range specified by user.'),
        h4('There is a lot of data, so it may take a while to run.'),
        h4('Select date range by either clicking and selecting from calendar,
           or entering in the box directly. There is some basic error checking.'),
        h4('Select the type of plot - points, line or both through the drop down
           menu.'),
        h4('Select the type of lines to be fit through radio buttons.')
    ),
    mainPanel(
        plotOutput('tsPlot')

    )
))
<file_sep>#setwd("C:/Users/AA/Desktop/Our Files/Avinash/2015/Coursera/Data Science")
#setwd("09 - Developing Data Products/Code/Project")

require(xlsx)
require(forecast)
require (xts)
rm(list=ls())   # Clean the envrionment completely
# Read in data. Invalid entries have values 0 or 2
tsDF = read.xlsx("TimeSeries.xlsx",
                    sheetName = "Sheet1", header = T)
# Clean the data, by removing obvious errors (data <= 2)
for (i in 1:nrow(tsDF)) {
    if (tsDF[i,2] == 0 || tsDF[i,2]==2)
        (tsDF[i,2] = 'NA')
}
# Replace all 'NA' entries with a numeric, based on na.approx
tsDF2 = tsDF
tsDF2[,2]= na.approx(tsDF[,2])

shinyServer(
    function(input, output){
        output$tsPlot = renderPlot ({
            output$sDate = renderPrint({input$startDate})
            myTsDF = tsDF2[tsDF2$Date > input$startDate,]
            myTsDF = myTsDF[myTsDF$Date < input$endDate,]
            plot(myTsDF$Date, myTsDF$Data, type=input$type,
                 xlab = "Date", ylab = 'Data',
                 main = 'Plot of Selected Data')
            output$typeOfPlot = renderPrint({input$type})
            if (input$fitting == 'linear') {
                # Ceate a linear regression line, and print in red
                reg = lm( Data ~ Date, data = myTsDF)
                abline(reg, col='red')
            }
            else {
                # Create a loess model line and print in blue
                ind = 1:nrow(myTsDF)
                data.lo = data.frame(ind, myTsDF$Data)
                lo = loess(myTsDF$Data ~ ind)
                predict.lo = predict(lo)
                preDF = data.frame(myTsDF$Date, predict.lo)
                lines(preDF, col = "blue")
            }
        })
        output$oid1 <- renderPrint({input$id1})
        output$oid2 <- renderPrint({input$id2})
        output$odate <- renderPrint({input$date})
    }
)

branch_name: refs/heads/master

repo_language: R

repo_name: avinashgithub/DDP


text:
<repo_name>kalcok/jc<file_sep>/README.md
# jc
jc (pronounced as 'juice') is Object Document Manager (ODM) in golang built on top of `mgo` MongoDB driver. Aim is to provide abstraction layer on top of `mgo` without too much of a performance impact.

## Usage
Things you can do with `jc` (so far) :
 * Defining models
 * Saving / Updating Documents
 * Creating Queries to fetch documents from DB

### Document definition
You can turn any golang structure into model just by including anonymous field `jc.Collection`.

**Simple model**
```golang
type Person struct {
	jc.Collection 		`bson:"-"json:"-"`
	FirstName string
	LastName  string
}

```
Notice the *json* and *bson* structure tags for the `jc.Collection`. These are very usefull for controll over Un/Marshalling of structures. In this case they say that `jc.Collection` field should be ignored when Saving/Reading from DB (and also when serializing into *json*)

You can create models with either implicit or explicit ID field. In case of implicit ID (as in example above), every document gets automatically generated `mgo.ObjectID`. In case of document with explicit ID, it needs to be supplied before trying to save document into DB.

**Model with explicit ID**
```golang
type Employee struct {
	jc.Collection 		`bson:"-"json:"-"`
	BadgeID   int 		`bson:"_id"`
	FirstName string
	LastName  string
}
```
In this example we used *bson* structure tag to indicate that we want to store value from `BadgeID` field into `_id` field in DB, which is what MongoDB uses to store document indices.

*bson* tags can be used also for other "non-special" fields,for example, to translate native golang *CamleCase* names into more "standard" (at least for MongoDB) *snake_case* names.

**Custom field names**
```golang
type Person struct {
	jc.Collection 		`bson:"-"json:"-"`
	FirstName string	`bson:"first_name"`
	LastName  string	`bson:"last_name"`
}
```

You can also use struct tags to explicitly define collection name.

**Custom Collection name**
```golang

type NewYorkBranchEmployee struct {
	jc.Collection		`bson:"-"json:"-"jc:"ny_employees"`
	FirstName string
	LastName  string
}
```
### Session Management
Before using any `jc` features, there needs to be initialized session with MongoDB server. Master session is initialized by calling `jc.tools.InitSession()` which takes one argument in form of `jc.toosl.SessionConf` struct. `Sessionconf` is just convenient alias to `mgo.DialInfo`. After you don't need master session anymore, it can be closed with call to `jc.tools.CloseSession()`.

**Eample**
```golang
func main(){
	conf := tools.SessionConf{Addrs: []string{"localhost"}, Database: "jc_test"}
	tools.InitSession(&conf)
	defer tools.CloseSession()
	// program logic
	// ...
}
```
You usually don't need to be concerned with session for the rest of your program after initialization. However if you need direct access to `mgo.Session` object, you can get either clone or copy of master session by calling `jc.tools.GetSessionClone()` or `jc.tools.GetSessionCopy()` respectivelly. Don't forget that these session need to be closed separately by calling `mySession.Close()`
_______________________________________________________________
Note: `tools` package is ripe for renaming
### Document Initiation
Due to some Golang restrictions (mainly, missing constructors) new document instance must be initiated with call to `NewDocument()` which takes pointer to a struct that contains `jc.Collection` as an argument

**Example**
```golang
newPerson := Person{FirstName:"John", LastName:"Foo"}
err := jc.NewDocument(&newPerson)

if err != nil {
	panic(err)
}
```
### Inserting Document (Save)
Once document is properly initialized, it can be inserted into DB by calling Save() method.

**Example**
```golang
changeInfo, err := newPerson.Save(true)

if err != nil {
	panic(err)
}
```
`Save()` takes one boolean argument which decide whether the action will [Clone](https://godoc.org/gopkg.in/mgo.v2#Session.Clone) (`true`) or [Copy](https://godoc.org/gopkg.in/mgo.v2#Session.Copy) (`false`) master session.
Calling `Save()` multiple times on the same document works like Update, to insert new document instance into DB, documents ID must change. On documents that do not have explicit ID field, you can call `NewImplicitID` to generate new ID for the document

### Query
`Query` object is used to pull documents from DB. Query must be instantiated by calling `NewQuery()` and it takes single argument in form of pointer to either single document or slice of documents where eventual result will be saved. Query autmatically recognizes whether its target is single document or slice and adjusts final action to perform either `One()` or `All()` query. (**Note:** Documents passed into `NewQuery()` will be automatically initialized so there is no need to call `NewDocument()` manually)

**Example**
```golang
var person Person
var allPersons []Person

// When executed, this query will pull single record from DB into person object
oneQuery, err := jc.NewQuery(&person)
if err != nil {
	panic(err)
}

// When executed, this query will pull all records that conform given filter and save it into allPersons slice
multiQuery, err := jc.NewQuery(&allPersons)
if err != nil {
	panic(err)
}
```

#### Query restrictions
`jc` supports following restrictions that shape its finall result:
 * `Limit(int)` - Sets maximum number of records pulled from DB
 * `Skip(int)` - Ignore arbitrary number of records form the beggining of result set
 * `Filter(interface{})` - Takes map or struct and applies it as filter on the final result
All the restriction methods also return pointer to the query on which it was called so that they can be chained together.

**Example**
```golang
var tenJohns []Person
multiQuery, err := jc.NewQuery(&tenJohns)
if err != nil {
	panic(err)
}

multiQuery.Skip(5).Limit(10).Filter(Person{FirstName: "John"})
```

#### Query execution
To execute query and actually fill target document(s) with data from DB, you must call `Execute()` method. `Execute()` takes one boolean argument which decide whether the action will [Clone](https://godoc.org/gopkg.in/mgo.v2#Session.Clone) (`true`) or [Copy](https://godoc.org/gopkg.in/mgo.v2#Session.Copy) (`false`) master session.
\# TODO
<file_sep>/query.go
package jc

import (
	"fmt"
	"reflect"
	"errors"
	"gopkg.in/mgo.v2"
	"github.com/kalcok/jc/tools"
)

type Query struct {
	Database    string
	collection  string
	filter      interface{}
	limit       int
	skip        int
	singleValue bool
	result      interface{}
	resultType  reflect.Type
}

func NewQuery(result interface{}) (newQuery Query, err error) {
	newQuery.result = result

	internalType := reflect.TypeOf(result).Elem()

	if internalType.Kind() == reflect.Slice {
		internalType = internalType.Elem()
		newQuery.singleValue = false
	} else {
		newQuery.singleValue = true
	}

	prototype := reflect.New(internalType)
	targetInterface := reflect.TypeOf((*document)(nil)).Elem()
	if !prototype.Type().Implements(targetInterface) {
		err = errors.New(
			fmt.Sprintf("Supplied 'result' type does not implement '%s'.", targetInterface.String()))
		return
	}

	newQuery.resultType = internalType
	proto_val := initPrototype(prototype, internalType)

	newQuery.collection = proto_val.FieldByName("_collectionName").String()
	newQuery.Database = proto_val.FieldByName("_collectionDB").String()

	return
}

func (q *Query) Execute(reuseSocket bool) (err error) {
	var session *mgo.Session

	if reuseSocket {
		session, err = tools.GetSessionClone()
	} else {
		session, err = tools.GetSessionCopy()
	}
	if err != nil {
		return
	}
	defer session.Close()

	query := session.DB(q.Database).C(q.collection).Find(q.filter)

	if q.skip > 0 {
		query = query.Skip(q.skip)
	}

	if q.limit > 0 {
		query = query.Limit(q.limit)
	}

	if q.singleValue {
		err = query.One(q.result)
	} else {
		err = query.All(q.result)
	}

	if err != nil {
		return
	}
	q.initResult()
	return
}

func (q *Query) Collection() string {
	return q.collection
}

func (q *Query) Filter(filter interface{}) *Query {
	q.filter = filter
	return q
}

func (q *Query) GetFilter() interface{} {
	return q.filter
}

func (q *Query) Limit(limit int) *Query {
	q.limit = limit
	return q
}

func (q *Query) GetLimit() int {
	return q.limit
}

func (q *Query) Skip(skip int) *Query {
	q.skip = skip
	return q
}

func (q *Query) GetSkip() int {
	return q.skip
}

func (q *Query) initResult() error {
	var err error
	resultv := reflect.ValueOf(q.result)

	if q.singleValue {
		initPrototype(resultv, q.resultType)
	} else {
		slicev := resultv.Elem()
		for i := 0; i < slicev.Len(); i++ {
			initPrototype(slicev.Index(i).Addr(), q.resultType)
		}
	}

	return err
}
<file_sep>/tests/query_test.go
package tests

import (
	"testing"
	"github.com/kalcok/jc"
	"fmt"
	"gopkg.in/mgo.v2/bson"
)

// Creates <num> of records of ImplicitID type
func prepareSimpleRecords(num int, customData string) (ids []interface{}) {
	var doc ImplicitID

	if customData == "" {
		customData = "prepareSimpleRecords"
	}

	for i := 0; i < num; i++ {
		doc = ImplicitID{Data: customData}
		jc.NewDocument(&doc)
		doc.Save(true)
		ids = append(ids, doc.ID())
	}
	return ids
}

func TestQueryInit(t *testing.T) {
	var docs []ImplicitID
	_, err := jc.NewQuery(&docs)

	if err != nil {
		t.Error(fmt.Sprintf("Failed to initialize new query: %s", err))
	}
}

func TestQueryImplicitCollection(t *testing.T) {
	var docs []ImplicitCollection
	expectedCollection := "implicit_collection"

	q, _ := jc.NewQuery(&docs)

	if q.Collection() != expectedCollection {
		t.Error(fmt.Sprintf("Failed to set implicit collection to query. Expected '%s', got '%s'",
			q.Collection(),
			expectedCollection))
	}
}

func TestQueryExplicitCollection(t *testing.T) {
	var docs []ExplicitCollection
	expectedCollection := "my_collection"

	q, _ := jc.NewQuery(&docs)

	if q.Collection() != expectedCollection {
		t.Error(fmt.Sprintf("Failed to set explicit collection to query. Expected '%s', got '%s'",
			q.Collection(),
			expectedCollection))
	}
}

func TestQuerySelectSliceResult(t *testing.T) {
	prepareSimpleRecords(2, "")
	var docs []ImplicitID

	q, _ := jc.NewQuery(&docs)

	err := q.Execute(true)

	if err != nil {
		t.Error(fmt.Sprintf("Failed to select multiple documents from DB. Error: %s", err))
	}
}

func TestQuerySelectNonSliceResult(t *testing.T) {
	data := "single record"
	prepareSimpleRecords(2, data)
	var doc ImplicitID

	q, _ := jc.NewQuery(&doc)

	err := q.Execute(true)

	if err != nil {
		t.Error(fmt.Sprintf("Failed to select single document from DB. Error: %s", err))
	}
}

func TestQuerySelectAll(t *testing.T) {
	dropTestDB()
	recordNumber := 14
	prepareSimpleRecords(recordNumber, "")
	var docs []ImplicitID

	q, _ := jc.NewQuery(&docs)

	q.Execute(true)

	if len(docs) != recordNumber {
		t.Error(fmt.Sprintf("Failed to select All records from DB. Expected %d, got %d",
			recordNumber, len(docs)))
	}
}

func TestQuerySliceIntegrity(t *testing.T) {
	const recordsNumber = 10

	var data [recordsNumber]string
	var results []ExplicitID

	dropTestDB()

	for i := 0; i < recordsNumber; i++ {
		data[i] = fmt.Sprintf("Integrity test %d", i)
		doc := ExplicitID{MyID: i, Data: data[i]}

		jc.NewDocument(&doc)
		doc.Save(true)
	}

	q, _ := jc.NewQuery(&results)

	q.Execute(true)

	for _, record := range results {
		if record.Data != data[record.MyID] {
			t.Error(
				fmt.Sprintf("Integrity failed for record ID '%d'. Expected data '%s', got '%s'",
					record.MyID, data[record.MyID], record.Data))
		}
	}
}

func TestQueryNonSliceIntegrity(t *testing.T) {
	dropTestDB()
	data := "NonSliceIntegrity"
	prepareSimpleRecords(3, data)

	var doc ImplicitID

	q, _ := jc.NewQuery(&doc)
	q.Execute(true)

	if doc.Data != data {
		t.Error(fmt.Sprintf("Integrity failed for single record query. Expected '%s', got '%s'", data, doc.Data))
	}

}

func TestQuerySliceResetOnMultipleSelects(t *testing.T) {
	dropTestDB()
	firstData := "First batch"
	firstBatch := 10
	secondData := "Second batch"
	secondBatch := 5
	prepareSimpleRecords(firstBatch, firstData)

	docs := []ImplicitID{}

	q, _ := jc.NewQuery(&docs)
	q.Execute(true)

	dropTestDB()
	prepareSimpleRecords(secondBatch, secondData)
	q.Execute(true)

	if len(docs) != secondBatch {
		t.Error("Failed to clean up target slice between multiple selects")
	}
}

func TestQueryLimit(t *testing.T) {
	limit := 5
	prepareSimpleRecords(10, "TestQueryLimit")
	docs := []ImplicitID{}

	q, _ := jc.NewQuery(&docs)
	q.Limit(limit).Execute(true)

	if len(docs) != limit {
		t.Error("Failed to impose query limit")
	}
}

func TestQuerySkip(t *testing.T) {
	dropTestDB()
	skip := 1

	sample := ExplicitID{MyID: 1, Data: "TestQuery"}
	jc.NewDocument(&sample)
	sample.Save(true)
	sample.MyID = 2
	sample.Save(true)
	sample.MyID = 3
	sample.Save(true)

	result := []ExplicitID{}
	q, _ := jc.NewQuery(&result)
	q.Skip(skip).Execute(true)

	for _, record := range result {
		if record.MyID == 1 {
			t.Error("Failed to skip over entries in DB")
		}
	}
}

func TestQueryFilter(t *testing.T) {
	expectedData := "This is what we want"
	prepareSimpleRecords(3, "Not what We want")

	expected := ImplicitID{Data: expectedData}
	jc.NewDocument(&expected)

	expected.Save(true)

	result := ImplicitID{}
	q, _ := jc.NewQuery(&result)

	q.Filter(bson.M{"data": expectedData}).Execute(true)

	if result.Data != expectedData {
		t.Error("Failed to select records based on bson filter")
	}
}

func TestQueryFilterStruct(t *testing.T) {
	dropTestDB()
	expectedData := "This is what we want"
	prepareSimpleRecords(3, "Not what we want")
	expected := ImplicitID{Data: expectedData}
	jc.NewDocument(&expected)

	expected.Save(true)

	result := ImplicitID{}

	q, _ := jc.NewQuery(&result)

	q.Filter(ImplicitID{Data: expectedData}).Execute(true)

	if result.Data != expectedData {
		t.Error("Failed to select records based on struct filter")
	}
}<file_sep>/tests/fixtures.go
package tests

import (
	"github.com/kalcok/jc"
)

// Test Fixtures
type (
	ImplicitID struct {
		jc.Collection `bson:"-"json:"-"`
		Data string   `bson:"data"json:"data"`
	}

	ExplicitID struct {
		jc.Collection `bson:"-"json:"-"`
		MyID int      `bson:"_id"json:"myID"`
		Data string   `bson:"data"json:"data"`
	}
	ExplicitCollection struct {
		jc.Collection `bson:"-"json:"-"jc:"my_collection"`
		Data string   `bson:"data"json:"data"`
	}
	ImplicitCollection struct {
		jc.Collection `bson:"-"json:"-"`
		Data string   `bson:"data"json:"data"`
	}
)

// Benchmark Fixtures
type (
	simpleUserMGO struct {
		name    string
		surname string
		address string
		age     int
	}

	simpleUserJC struct {
		jc.Collection
		name    string
		surname string
		address string
		age     int
	}
)
<file_sep>/wrappers/uuid.go
package wrappers

import (
	"github.com/google/uuid"
	"gopkg.in/mgo.v2/bson"
)

type UuidField struct {
	Data uuid.UUID
}

func (u UuidField) GetBSON() (interface{}, error) {
	bytes, err := u.Data.MarshalBinary()
	return bson.Binary{Kind: 3, Data: bytes}, err
}

func (u *UuidField) SetBSON(raw bson.Raw) error {
	var bytes bson.Binary
	err := raw.Unmarshal(&bytes)
	if err == nil {
		err = u.Data.UnmarshalBinary(bytes.Data)
	}
	return err
}

func NewUuid() UuidField {
	return UuidField{Data: uuid.New()}
}
<file_sep>/tools/session.go
package tools

import (
	"gopkg.in/mgo.v2"
	"fmt"
	"errors"
)

type SessionConf mgo.DialInfo

var (
	session *mgo.Session
)

func InitSession(conf *SessionConf) {
	var err error
	session, err = mgo.DialWithInfo((*mgo.DialInfo)(conf))

	if err != nil {
		panic(fmt.Sprintf("Failed to connect to DB server. %s", err))
	}
}

func CloseSession() {
	if session != nil {
		session.Close()
	}
}

func GetSessionCopy() (*mgo.Session, error) {
	var err error
	if session == nil {
		err = errors.New("Session is not initialized")
	}
	return session.Copy(), err
}

func GetSessionClone() (*mgo.Session, error) {
	var err error
	if session == nil {
		err = errors.New("Session is not initialized")
	}
	return session.Clone(), err
}<file_sep>/tests/collection_test.go
package tests

import (
	"testing"
	"github.com/kalcok/jc/tools"
	"os"
	"github.com/kalcok/jc"
	"gopkg.in/mgo.v2/bson"
	"reflect"
	"gopkg.in/mgo.v2"
)

var (
	sessionDB  string
	mgoSession *mgo.Session
)

func initTestSession() {
	address, found := os.LookupEnv("JC_TEST_ADDRESS")
	if !found {
		address = "localhost"
	}

	db, found := os.LookupEnv("JC_TEST_DB")
	if !found {
		db = "jc_test"
	}
	sessionDB = db

	pass, _ := os.LookupEnv("JC_TEST_PASS")
	user, _ := os.LookupEnv("JC_TEST_USER")

	conf := tools.SessionConf{Database: db, Addrs: []string{address}, Password: <PASSWORD>, Username: user}
	tools.InitSession(&conf)
}

func dropTestDB() {
	session, err := tools.GetSessionClone()
	if err != nil {
		panic(err)
	}
	session.DB("").DropDatabase()
	session.Close()
}

func TestMain(m *testing.M) {
	var err error
	mgoSession, err = mgo.Dial("linux.dev")
	if err != nil {
		panic(err)
	}
	defer mgoSession.Clone()
	initTestSession()
	dropTestDB()
	m.Run()
	dropTestDB()
}

func TestSingleDocumentInit(t *testing.T) {
	doc := ExplicitID{Data: "jc_test", MyID: 1001}
	err := jc.NewDocument(&doc)
	if err != nil {
		t.Error(err)
	}
}

func TestImplicitCollectionName(t *testing.T) {
	err_message := "Failed to implicitly create collection name. Expected '%s', got '%s'"

	type camelCased ImplicitID
	cc_string := "camel_cased"

	type TrueCamelCased ImplicitID
	tcc_string := "true_camel_cased"

	type snake_cased ImplicitID
	sc_string := "snake_cased"

	type plain ImplicitID
	plain_string := "plain"

	cc := camelCased{Data: "camelCased"}
	jc.NewDocument(&cc)
	if cc.CollectionName() != cc_string {
		t.Error(err_message, cc_string, cc.CollectionName())
	}

	tcc := TrueCamelCased{Data: "TrueCamelCased"}
	jc.NewDocument(&tcc)
	if tcc.CollectionName() != tcc_string {
		t.Error(err_message, tcc_string, tcc.CollectionName())
	}

	sc := snake_cased{Data: "snake_cased"}
	jc.NewDocument(&sc)
	if sc.CollectionName() != sc_string {
		t.Error(err_message, sc_string, sc.CollectionName())
	}

	p := plain{Data: "plain"}
	jc.NewDocument(&p)
	if p.CollectionName() != plain_string {
		t.Error(err_message, plain_string, p.CollectionName())
	}
}

func TestDefaultDB(t *testing.T) {
	doc := ImplicitID{Data: "TestDefaultDB"}
	jc.NewDocument(&doc)

	if doc.Database() != sessionDB {
		t.Error("failed to init document default DB")
	}
}

func TestDBChange(t *testing.T) {
	doc := ImplicitID{Data: "TestDBChange"}
	jc.NewDocument(&doc)
	new_db := sessionDB + "_fancy"
	doc.SetDatabase(new_db)

	if doc.Database() != new_db {
		t.Error("failed to change documents DB")
	}
}

func TestGetField(t *testing.T) {
	data := "TestGetField"
	doc := ImplicitID{Data: data}
	jc.NewDocument(&doc)
	field, err := doc.GetField("Data")
	if err != nil {
		t.Error("Failed to get field value by name")
	}
	if field != data {
		t.Error("Unexpected value.")
	}
}

func TestGetFieldProtectUnexported(t *testing.T) {
	type unexportedFieldDoc struct {
		jc.Collection
		Data    string `bson:"data"json:"data"`
		private string
	}

	doc := unexportedFieldDoc{Data: "TestGetFieldProtectUnexported", private: "Unexported"}
	jc.NewDocument(&doc)
	_, err := doc.GetField("private")
	if err == nil {
		t.Error("GetField accessed unexported field")
	}
}

func TestGetFieldMissing(t *testing.T) {
	type testDocument struct {
		jc.Collection
		Data string `bson:"data"json:"data"`
	}
	doc := testDocument{Data: "TestGetFieldMissing"}
	jc.NewDocument(&doc)
	_, err := doc.GetField("DefinitelyNotPresent")
	if err == nil {
		t.Error("GetField didn't fail on missing field access")
	}
}

func TestExplicitCollectionName(t *testing.T) {
	err_msg := "Failed to set explicit Collection name. Expected '%s', got '%s'"
	type boringStruct struct {
		jc.Collection `bson:"-"json:"-"jc:"my_awesome_collection"`
		Data string   `bson:"data",json:"data"`
	}

	doc := boringStruct{Data: "Boring data"}
	jc.NewDocument(&doc)

	if doc.CollectionName() != "my_awesome_collection" {
		t.Error(err_msg, "my_awesome_collection", doc.CollectionName())
	}

}

func TestInsertExplicitIDCollection(t *testing.T) {
	id := 666
	doc := ExplicitID{Data: "TestInsertExplicitIDCollection", MyID: 666}
	err := jc.NewDocument(&doc)
	if err != nil {
		return
	}

	// Test Save() call
	_, err = doc.Save(true)
	if err != nil {
		t.Error(err)
	}

	// Test if document is in DB
	result := bson.M{}
	empty_result := bson.M{}
	session, _ := tools.GetSessionClone()
	defer session.Close()
	session.DB(sessionDB).C("explicit_i_d").FindId(id).One(&result)
	if reflect.DeepEqual(result, empty_result) {
		t.Error("Failed to insert document with explicit ID into DB")
	}
}

func TestInsertImplicitIDCollection(t *testing.T) {
	doc := ImplicitID{Data: "TestInsertImplicitIDCollection"}
	err := jc.NewDocument(&doc)

	if err != nil {
		return
	}

	// Test Save() call
	_, err = doc.Save(true)
	if err != nil {
		t.Error(err)
	}
	id := doc.ID()

	// Test if document is in DB
	result := bson.M{}
	empty_result := bson.M{}
	session, _ := tools.GetSessionClone()
	defer session.Close()
	session.DB(sessionDB).C("implicit_i_d").FindId(id).One(&result)
	if reflect.DeepEqual(result, empty_result) {
		t.Error("Failed to insert document with implicit ID into DB")
	}
}

func TestUpsert(t *testing.T) {
	original := "TestUpsert"
	update := "TestUpsertUpdated"
	doc := ImplicitID{Data: original}
	jc.NewDocument(&doc)

	doc.Save(true)
	id := doc.ID()

	doc.Data = update
	doc.Save(true)

	result := bson.M{}
	session, _ := tools.GetSessionClone()
	defer session.Close()
	session.DB(sessionDB).C("implicit_i_d").FindId(id).One(&result)

	if result["data"] != update {
		t.Error("Failed to Upsert document with implicit ID")
	}

}

func TestUpsertWithExplicitID(t *testing.T) {
	original := "TestUpsertWithExplicitID"
	updated := "TestUpsertWithExplicitIDUpdated"
	id := 123
	doc := ExplicitID{Data: original, MyID: id}
	jc.NewDocument(&doc)

	doc.Save(true)

	doc.Data = updated
	doc.Save(true)

	result := bson.M{}
	session, _ := tools.GetSessionClone()
	defer session.Close()
	session.DB(sessionDB).C(doc.CollectionName()).FindId(id).One(&result)

	if result["data"] != updated {
		t.Error("Failed to Upsert document with explicit ID")
	}
}

func TestNewImplicitID(t *testing.T) {
	doc := ImplicitID{Data: "TestNewID"}
	jc.NewDocument(&doc)

	originalID := doc.ID()
	err := doc.NewImplicitID()
	newID := doc.ID()

	if err != nil {
		t.Error("Failed to reinitialize new implicit ID: %s", err)
	}
	if originalID == newID {
		t.Error("Failed to reinitialize new implicit ID: New ID matches old ID")
	}
}

func TestNewImplicitIDError(t *testing.T) {
	doc := ExplicitID{Data: "TestNewImplicitIDError"}
	jc.NewDocument(&doc)

	err := doc.NewImplicitID()

	if err == nil {
		t.Error("NewImplicitID() failed to return error when used on document with explicit ID")
	}
}

func TestDocumentStartsUninitialized(t *testing.T) {
	doc := ImplicitID{Data: "TestDocumentStartsUninitialized"}

	if doc.IsInitialized() {
		t.Error("IsInitialized returns true for uninitialized document")
	}
}

func TestIsInitializedFlag(t *testing.T) {
	doc := ImplicitID{Data: "TestIsInitializedFlag"}

	jc.NewDocument(&doc)

	if !doc.IsInitialized() {
		t.Error("Initialization did not set _initialized flag")
	}
}<file_sep>/collection.go
package jc

import (
	"reflect"
	"unicode"
	"fmt"
	"errors"
	"strings"
	"gopkg.in/mgo.v2/bson"
	"gopkg.in/mgo.v2"
	"github.com/kalcok/jc/tools"
)

type document interface {
	setCollection(string)
	CollectionName() string
	SetDatabase(string)
	Database() string
	GetField(string) (interface{}, error)
	ID() interface{}
	Init(reflect.Value, reflect.Type)
	InitDB() error
	Info()
	NewImplicitID() error
	Save(bool) (*mgo.ChangeInfo, error)
	IsInitialized() bool
}

type Collection struct {
	_collectionName  string                `bson:"-"json:"-"`
	_collectionDB    string                `bson:"-"json:"-"`
	_parent          reflect.Value         `bson:"-"json:"-"`
	_parentType      reflect.Type          `bson:"-"json:"-"`
	_hasExplicitID   bool                  `bson:"-"json:"-"`
	_explicitIDField string                `bson:"-"json:"-"`
	_implicitIDValue bson.ObjectId         `bson:"-"json:"-"`
	_skeleton        []reflect.StructField `bson:"-"json:"-"`
	_initialized     bool                  `bson:"-",json:"-"`
}

func (c *Collection) setCollection(name string) {
	c._collectionName = name
}

func (c *Collection) CollectionName() string {
	return c._collectionName
}

func (c *Collection) SetDatabase(name string) {
	c._collectionDB = name

}

func (c *Collection) Database() string {
	return c._collectionDB
}

func (c *Collection) IsInitialized() bool {
	return c._initialized
}

func (c *Collection) Info() {
	fmt.Printf("Database %s\n", c._collectionDB)
	fmt.Printf("Collection %s\n", c._collectionName)
	fmt.Printf("Parent__ %s\n", c._parent)
}

func (c *Collection) Save(reuseSocket bool) (info *mgo.ChangeInfo, err error) {
	var session *mgo.Session
	var documentID interface{}
	idField := "_id"

	if reuseSocket {
		session, err = tools.GetSessionClone()
	} else {
		session, err = tools.GetSessionCopy()
	}
	if err != nil {
		return info, err
	}
	defer session.Close()

	if c._hasExplicitID {
		documentID = c._parent.Elem().FieldByName(c._explicitIDField).Interface()
	} else if len(c._implicitIDValue) > 0 {
		documentID = c._implicitIDValue
	} else {
		c._implicitIDValue = bson.NewObjectId()
		documentID = c._implicitIDValue
	}

	collection := session.DB(c._collectionDB).C(c._collectionName)
	info, err = collection.Upsert(bson.M{idField: documentID}, c._parent.Interface())

	return info, err
}

func (c *Collection) ID() (id interface{}) {
	if c._hasExplicitID {
		id = c._parent.Elem().FieldByName(c._explicitIDField).Interface()
	} else {
		id = c._implicitIDValue
	}
	return id
}

func (c *Collection) GetField(name string) (result interface{}, err error) {
	if unicode.IsLower(rune(name[0])) {
		err = errors.New(fmt.Sprintf("can't access unexported field '%s'", name))
		return
	}
	missingValue := reflect.Value{}
	resultValue := c._parent.Elem().FieldByName(name)
	if resultValue == missingValue {
		err = errors.New(fmt.Sprintf("field '%s' not found", name))
		return
	}
	result = resultValue.Interface()
	return
}

func (c *Collection) Init(parent reflect.Value, parentType reflect.Type) {

	c._parent = parent
	c._parentType = parentType
	c._hasExplicitID = false
	for i := 0; i < reflect.Indirect(c._parent).NumField(); i++ {
		field := c._parentType.Field(i)

		// Find explicit Collection name
		if field.Type == reflect.TypeOf(Collection{}) {
			explicitName := false
			jc_tag, tag_present := field.Tag.Lookup("jc")
			if tag_present {
				jc_fields := strings.Split(jc_tag, ",")
				if len(jc_fields) > 0 && jc_fields[0] != "" {
					c.setCollection(jc_fields[0])
					explicitName = true
				}
			}
			if !explicitName {
				c.setCollection(camelToSnake(parentType.Name()))
			}
		}

		// Find explicit index field
		bson_tag, tag_present := field.Tag.Lookup("bson")
		if tag_present {
			field_id := strings.Split(bson_tag, ",")
			switch field_id[0] {
			case "_id":
				c._explicitIDField = field.Name
				c._hasExplicitID = true
				break
			case "-":
				continue
			default:
				break
			}
		}
		c._skeleton = append(c._skeleton, field)
	}
	c._initialized = true

}

func (c *Collection) InitDB() error {
	session, err := tools.GetSessionClone()
	if err == nil {
		c.SetDatabase(session.DB("").Name)
		defer session.Close()
	} else {
		err = errors.New("database not initialized")
	}
	return err
}

func (c *Collection) NewImplicitID() (err error) {

	if c._hasExplicitID {
		return errors.New("can't assign new ID to document with Explicit ID")
	}
	c._implicitIDValue = bson.NewObjectId()
	return err
}

func NewDocument(c document) error {
	var err error
	objectType := reflect.TypeOf(c).Elem()
	objectValue := reflect.ValueOf(c)
	c.Init(objectValue, objectType)
	err = c.InitDB()

	return err
}

func camelToSnake(camel string) string {
	var (
		snake_name []rune
		next       rune
	)
	for i, c := range camel {
		if unicode.IsUpper(c) && i != 0 {
			snake_name = append(snake_name, '_')
		}
		next = unicode.ToLower(c)
		snake_name = append(snake_name, next)
	}
	return string(snake_name)
}

func initPrototype(prototype reflect.Value, internalType reflect.Type) reflect.Value {
	var inputs []reflect.Value
	inputs = append(inputs, reflect.ValueOf(prototype))
	inputs = append(inputs, reflect.ValueOf(internalType))
	prototype.MethodByName("Init").Call(inputs)
	prototype.MethodByName("InitDB").Call(nil)

	return reflect.Indirect(prototype)

}
<file_sep>/tests/bench_test.go
package tests

import (
	"testing"
	"github.com/kalcok/jc"
)

// Insert simple document with plain mgo
func BenchmarkMgoSave(b *testing.B) {
	session := mgoSession.Clone()
	collection := "BenchmarkMgoSave"
	data := simpleUserMGO{name: "Foo", surname: "Bar", address: "Foobar av"}

	for i := 0; i < b.N; i++ {
		data.age = i
		session.DB(sessionDB).C(collection).Insert(&data)
	}
}

// Insert documents with jc
// Creates new records with NewImplicitID call for better performance
func BenchmarkJcSave(b *testing.B) {
	data := simpleUserJC{name: "Foo", surname: "Bar", address: "Foobar av"}
	err := jc.NewDocument(&data)
	if err != nil {
		panic(err)
	}

	for i := 0; i < b.N; i++ {
		data.NewImplicitID()
		data.age = i
		data.Save(true)
	}
}

// Insert documents with jc
// For each insert, new jc document is initialized with NewDocument call
// Probably worst case scenario (performance wise)
func BenchmarkJcSaveReinit(b *testing.B) {
	for i := 0; i < b.N; i++ {
		data := simpleUserJC{name: "Foo", surname: "Bar", address: "Foobar av", age: i}
		err := jc.NewDocument(&data)
		if err != nil {
			panic(err)
		}
		data.NewImplicitID()
		data.Save(true)
	}
}

languages: Markdown Go

repo_language: Markdown

repo_name: kalcok/jc


text:
<repo_name>ChavdaAnant/Creative<file_sep>/includes/common.h
#ifndef __HEADER_COMMON_H__
#define __HEADER_COMMON_H__

#include <stdio.h>

#endif
<file_sep>/README.md
Creative
========

Be Original Be Creative
<file_sep>/src/sample/hello.c
/**\fn main
   \brief This is the entry of programme.
   \param int argc, char **argv
   \return int
**/

#include "common.h" //includes all common headers

enum { SUCCESS, FAILURE };

int main( int argc, char **argv )
{

	printf(" Hello World @@@ :)");

	return SUCCESS;

}

<file_sep>/Makefile

SOURCES = $(PWD)/src/sample/*.c
HEADERS = $(PWD)/includes/
creative:$(SOURCES)
	@echo " Entered into Make..Creating Creative Executable..:)"
	@cc -o creative $(SOURCES) -I $(HEADERS)
.PHONY = clean
clean:
	@echo "Removing Creative executable.....:("
	@rm -rf creative

languages: Makefile Markdown C

repo_language: Makefile

repo_name: ChavdaAnant/Creative


text:
<repo_name>FernandaPerezV/10Tarea<file_sep>/p2.py
'''
Este script ajusta los datos del archivo espectro.dat
(frecuencias [[erg s-1 Hz-1 cm-2]] vs longitudes de onda en [Angstrom])
de dos formas (modelos):
1) recta - gaussiana
2) recta - lorentz
Utilizando curve_fit. Realiza test KS con scipy (kstest y kstwobign
[aproximacion] para N grande) para cada modelo entregando
Dn y Nivel de confianza en cada caso.
'''
from __future__ import division
import numpy as np
import scipy.stats
from scipy.optimize import curve_fit
from scipy.stats import kstest
import matplotlib.pyplot as plt
from scipy.stats import kstest
from scipy.stats import kstwobign
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset

def recta_gaussiana(x, a, b, A, mu, sigma):
    return a * x + b - A * scipy.stats.norm(loc=mu, scale=sigma).pdf(x)

def recta_lorentz(x, a, b, A, mu, sigma):
    return a * x + b - A * scipy.stats.cauchy(loc=mu, scale=sigma).pdf(x)

def cdf(data, model):
    return np.array([np.sum(model <= yy) for yy in data]) / len(model)

wavelength = np.loadtxt('espectro.dat', usecols=(0,))
fnu = np.loadtxt('espectro.dat', usecols=(1,))

n = len(wavelength)
mean = sum(wavelength)/n
sigma = np.sqrt(sum((wavelength-mean)**2)/n)

popt1, pcov1 = curve_fit(recta_gaussiana, wavelength, fnu,
                         [1e-16, 1e-20, 1e-17, mean, sigma])
popt2, pcov2 = curve_fit(recta_lorentz, wavelength, fnu,
                         [1e-16, 1e-20, 1e-17, mean, sigma])

xmin = np.min(wavelength)
xmax = np.max(wavelength)
y_data_sorted = np.sort(fnu)
N = len(y_data_sorted)

y1_func_sorted = np.sort(recta_gaussiana(np.linspace(xmin, xmax, 1000),
                         *popt1))
y2_func_sorted = np.sort(recta_lorentz(np.linspace(xmin, xmax, 1000), *popt2))

# Test KS con scipy kstest
Dn_scipy_1, prob_scipy_1 = kstest(y_data_sorted, cdf, args=(y1_func_sorted,))
Dn_scipy_2, prob_scipy_2 = kstest(y_data_sorted, cdf, args=(y2_func_sorted,))

print "Dn_scipy 1   : ", Dn_scipy_1
print "Nivel de confianza 1 : ", prob_scipy_1
print "Dn_scipy 2   : ", Dn_scipy_2
print "Nivel de confianza 2 : ", prob_scipy_2

CDF_gauss = np.array([np.sum(y1_func_sorted <= yy)
                     for yy in y_data_sorted]) / len(y1_func_sorted)
CDF_lorentz = np.array([np.sum(y2_func_sorted <= yy)
                       for yy in y_data_sorted]) / len(y2_func_sorted)

# plot
fig = plt.figure(1)
plt.clf()
ax = fig.add_subplot(111)
ax.plot(y_data_sorted, np.arange(N) / N, '-^', drawstyle='steps-post',
        color='b', label='$F_{n2}$')
ax.plot(y_data_sorted, np.arange(1, N+1) / N, '-.', drawstyle='steps-post',
        color='b', label='$F_{n1}$')
ax.plot(y_data_sorted, CDF_gauss, '-x', drawstyle='steps-post',
        label='Ajuste recta-gaussiana', color='g')
ax.plot(y_data_sorted, CDF_lorentz, '-x', drawstyle='steps-post',
        label='Ajuste recta-lorentz', color='r')
plt.legend(loc=2)
plt.title('Funciones de probabilidad acumulada')
ax.set_xlabel('Frecuencia [$erg$ $s^{-1} Hz^{-1} cm^{-2}$]')

# zoom en el plot
axins = zoomed_inset_axes(ax, 6, loc=6)
axins.plot(y_data_sorted, np.arange(N) / N, '-^', drawstyle='steps-post',
           color='b')
axins.plot(y_data_sorted, np.arange(1, N+1) / N, '-.', drawstyle='steps-post',
           color='b')
axins.plot(y_data_sorted, CDF_gauss, '-x', drawstyle='steps-post',
           label='Ajuste recta-gaussiana', color='g')
axins.plot(y_data_sorted, CDF_lorentz, '-x', drawstyle='steps-post',
           label='Ajuste recta-lorentz', color='r')

plt.yticks(visible=False)
plt.xticks(visible=False)
x1, x2, y1, y2 = 1.375 * 10**-16, 1.39 * 10**-16, 0.05, 0.09  # limites
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
mark_inset(ax, axins, loc1=1, loc2=4, fc="none", ec="0.5")

plt.draw()
plt.show()
plt.savefig('figura2.png')

# Dn critico
ks_dist = kstwobign()  # aproximacion para N grande
alpha = 0.05
Dn_critico = ks_dist.ppf(1 - alpha) / np.sqrt(N)
print "Dn_critico = ", Dn_critico
<file_sep>/p1.py
'''
Este script ajusta los datos del archivo espectro.dat
(frecuencias [[erg s-1 Hz-1 cm-2]] vs longitudes de onda en [Angstrom])
de dos formas (modelos):
1) recta - gaussiana
2) recta - lorentz
Utilizando curve_fit.
Devuelve un grafico con el ploteo de los datos y de ambos ajustes, mas los
arreglos de los mejores parametros para cada caso.
'''
from __future__ import division
import numpy as np
import scipy.stats
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

def recta_gaussiana(x, a, b, A, mu, sigma):
    return a * x + b - A * scipy.stats.norm(loc=mu, scale=sigma).pdf(x)

def recta_lorentz(x, a, b, A, mu, sigma):
    return a * x + b - A * scipy.stats.cauchy(loc=mu, scale=sigma).pdf(x)

def chi_gauss(x, y, a, b, A, mu, sigma):
    return sum((y - recta_gaussiana(x, a, b, A, mu, sigma))**2)

def chi_lorentz(x, y, a, b, A, mu, sigma):
    return sum((y - recta_lorentz(x, a, b, A, mu, sigma))**2)

wavelength = np.loadtxt('espectro.dat', usecols=(0,))
fnu = np.loadtxt('espectro.dat', usecols=(1,))

n = len(wavelength)
mean = sum(wavelength)/n
sigma = np.sqrt(sum((wavelength-mean)**2)/n)
m, n = np.polyfit(wavelength, fnu, 1)

popt1, pcov1 = curve_fit(recta_gaussiana, wavelength, fnu,
                         [m, n, 1e-17, mean, sigma])
popt2, pcov2 = curve_fit(recta_lorentz, wavelength, fnu,
                         [m, n, 1e-17, mean, sigma])
fig = plt.figure(1)
plt.clf()
plt.plot(wavelength, fnu, 'o-', label='Datos', alpha=0.4, color='g')
plt.plot(wavelength, recta_gaussiana(wavelength, *popt1), color='b',
         label='Ajuste recta-gaussiana', linewidth=1.5,)
plt.plot(wavelength, recta_lorentz(wavelength, *popt2), color='r',
         label='Ajuste recta-lorentz', linewidth=1.5,)
plt.xlabel('Longitud de onda [$\AA$]')
plt.ylabel('Frecuencia [$erg$ $s^{-1} Hz^{-1} cm^{-2}$]')
plt.legend(loc=4)
plt.draw()
plt.show()
plt.savefig('figura1.png')

print 'mejores parametros: a, b, A, mu, sigma'
print 'mejores parametros caso1'
print popt1
print 'mejores parametros caso 2'
print popt2
print 'valor chi cuadrado gaussiana:'
print chi_gauss(wavelength, fnu, *popt1)
print 'valor chi cuadrado lorentz:'
print chi_lorentz(wavelength, fnu, *popt2)

repo_language: Python


text:
<repo_name>norrisollie/train-disruption-bingo<file_sep>/app.js
"use strict"

const Twitter = require('twitter');
const config = require('./config.js');
// const dateSuffix = require('./datesuffix.js');
const T = new Twitter(config);

// use arguments to choose what twitter account to look at
let toc_twitter = process.argv[2]; //value will be "banana"

// Set up your search parameters
const params = {
    screen_name: toc_twitter,
    count: 200,
    exclude_replies: true,
    include_rts: false,
    tweet_mode: 'extended'
}

let problemArray = [];

T.get('statuses/user_timeline', params, (error, tweets, response) => {

    if (!error) {

        // search array contains the keywords to look for in the tweets
        const problemArray = ["train fault", "points failure", "signal failure", "cancelled", "delayed", "will no longer call at", "problem currently under investigation"];

        // object with properties for the count for each words
        let countObject = {
            totalTweets: 0,
            trainFault: 0,
            pointsFailure: 0,
            signalFailure: 0,
            cancelled: 0,
            delayed: 0,
            willNoLongerCallAt: 0,
            problemCurrentlyUnderInvestigation: 0,
            noProblem: 0
        }

        // loop through all of the tweets from the user's timeline to search for tweets containing the keywords
        for (let tweet of tweets) {

            // increment totalTweets counter
            countObject.totalTweets++

            // get the contents of the tweet
            let theTweets = tweet.full_text;
            // get the date of the tweet and split by date
            let theTweetDate = tweet.created_at.split(" ");

            // iterate through the problems array
            for (let problem of problemArray) {

                // only tweets that contain one or more of the problem strings can increase the count
                if (theTweets.includes(problem)) {

                    // switch statement to increment counter
                    switch (problem) {

                        // if train fault
                        case "train fault":
                            countObject.trainFault++
                            break;
                        // if points failure
                        case "points failure":
                            countObject.pointsFailure++
                            break;
                        // if signal failure
                        case "signal failure":
                            countObject.signalFailure++
                            break;
                        // if cancelled
                        case "cancelled":
                            countObject.cancelled++
                            break;
                        // if delayed
                        case "delayed":
                            countObject.delayed++
                            break;
                        // if will no longer call at
                        case "will no longer call at":
                            countObject.willNoLongerCallAt++
                            break;
                        // if problem currently under investigation
                        case "problem currently under investigation":
                            countObject.willNoLongerCallAt++
                            break;
                    }

                } else {

                    // increment if the tweet contains none of the entries
                    countObject.noProblem++
                }
            }
        }

        // if all problem counts have a value of 1 or more, BINGO!
        if(countObject.trainFault >=1 && countObject.pointsFailure >=1 && countObject.signalFailure >=1 && countObject.cancelled >=1 && countObject.delayed >=1 && countObject.willNoLongerCallAt >=1 && countObject.willNoLongerCallAt) {
            console.log("\noOoOOoOOo       o.oOOOo.  ooOoOOo o.     O  .oOOOo.   .oOOOo.\n     o     o     O    Oo     o .O     o  .O     o\n     O     O     o    O O    O o         O       \n     oOooOO.     O    O  o   o O         o       \n     o     `O    o    O   o  O O   .oOOo O       \n     O      o    O    o    O O o.      O o       \n     o     .O    O    o     Oo  O.    oO `o     O\n     `OooOO'  ooOOoOo O     `o   `OooO'   `OoooO' \n")
        } else {
            console.log("\no.     O  .oOOOo.  oOoOOoOOo       o.oOOOo.  ooOoOOo o.     O  .oOOOo.   .oOOOo. \nOo     o .O     o.     o            o     o     O    Oo     o .O     o  .O     o.\nO O    O O       o     o            O     O     o    O O    O o         O       o\nO  o   o o       O     O            oOooOO.     O    O  o   o O         o       O\nO   o  O O       o     o            o     `O    o    O   o  O O   .oOOo O       o\no    O O o       O     O            O      o    O    o    O O o.      O o       O\no     Oo `o     O'     O            o     .O    O    o     Oo  O.    oO `o     O'\nO     `o  `OoooO'      o'           `OooOO'  ooOOoOo O     `o   `OooO'   `OoooO' \n");
        }

            console.log("'Train Fault' mentioned " + countObject.trainFault + " times.")
            console.log("'Points Failure' mentioned " + countObject.pointsFailure + " times.")
            console.log("'Signal Failure' mentioned " + countObject.signalFailure + " times.")
            console.log("'Cancelled' mentioned " + countObject.cancelled + " times.")
            console.log("'Delayed' mentioned " + countObject.delayed + " times.")
            console.log("'Will No Longer Call At' mentioned " + countObject.willNoLongerCallAt + " times.")
            console.log("'Problem Currently Under Investigation' mentioned " + countObject.problemCurrentlyUnderInvestigation + " times.")

    } else {
        console.log("Something's wrong...")
    }
});<file_sep>/config.js
module.exports = {
  consumer_key: 'F2tiM2BS1m4qi4IwhsQSlhspt',
  consumer_secret: '<KEY>',
  access_token_key: '<KEY>',
  access_token_secret: '<KEY>'
}<file_sep>/datesuffix.js
module.exports {

    const daysObject = {

        "Mon": "Monday",
        "Tue": "Tuesday",
        "Wed": "Wednesday",
        "Thu": "Thursday",
        "Fri": "Friday",
        "Sat": "Saturday",
        "Sun": "Sunday"
    };

    const monthObject = {

        "Jan": "January",
        "Feb": "February",
        "Mar": "March",
        "Apr": "April",
        "May": "May",
        "Jun": "June",
        "Jul": "July",
        "Aug": "August",
        "Sep": "September",
        "Oct": "October",
        "Nov": "November",
        "Dec": "December"
    };

    const dateObject = {

        "0": "th",
        "1": "st",
        "2": "nd",
        "3": "rd",
        "4": "th",
        "5": "th",
        "6": "th",
        "7": "th",
        "8": "th",
        "9": "th"
    };
};

repo_language: JavaScript

repo_name: norrisollie/train-disruption-bingo


text:
<file_sep>package flowershop;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
 * Test class.
 *
 * @author nmenego 2016/11/28.
 */
public class ShopRunnerTest {

    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

    @Before
    public void setUpStreams() {
        System.setOut(new PrintStream(outContent));
        System.setErr(new PrintStream(errContent));
    }

    @After
    public void cleanUpStreams() {
        System.setOut(null);
        System.setErr(null);
    }

    @Test
    public void normal() throws Exception {

        ShopRunner.main(new String[]{"files/flowershop.data"});
        Assert.assertEquals("10 R12 $12.99\n" +
                "\t10 x $12.99\n" +
                "15 L09 $41.90\n" +
                "\t6 x $16.95\n" +
                "\t9 x $24.95\n" +
                "13 T58 $25.85\n" +
                "\t3 x $5.95\n" +
                "\t5 x $19.90\n", outContent.toString());
    }

    @Test(expected = IOException.class)
    public void wrongDataFormat() throws Exception {
        ShopRunner.main(new String[]{"files/flowershop2.data"});
    }

    @Test
    public void noSuitableBundle() throws Exception {
        ShopRunner.main(new String[]{"files/flowershop3.data"});
        Assert.assertEquals("1 R12 : unable to find suitable bundle combination.\n" +
                "1 L09 : unable to find suitable bundle combination.\n" +
                "1 T58 : unable to find suitable bundle combination.\n", outContent.toString());
    }
}<file_sep># FlowerShop

A programming exercise using dynamic programming.

## Context:

A flower shop used to base the price of their flowers on an item by item cost. So if a
customer ordered 10 roses then they would be charged 10x the cost of single rose. The
flower shop has decided to start selling their flowers in bundles and charging the customer
on a per bundle basis. So if the shop sold roses in bundles of 5 and 10 and a customer
ordered 15 they would get a bundle of 10 and a bundle of 5.

| Name | Code | Bundle |
|---|---|---|
| Roses | R12 | 5 @ $6.99 |
| | | 10 @ $12.99 |
| Lilies | L09 | 3 @ $9.95 |
| | | 6 @ $16.95 |
| | | 9 @ $24.95 |
| Tulips | T58 | 3 @ 5.95 |
| | | 5 @ $9.95 |
| | | 9 @ $16.99 |

## Task:

Given a customer order you are required to determine the cost and bundle breakdown for each product.
To save on shipping space each order should contain the minimal number of bundles.

## Input:

Each order has a series of lines with each line containing the number of items followed by the product code

## An example input:

```
10 R12
15 L09
13 T58
```

## Output:

A successfully passing test(s) that demonstrates the following output: (The format of the output is not important)

```
10 R12 $12.99
    1 x 10 $12.99
15 L09 $41.90
    1 x 9 $24.95
    1 x 6 $16.95
13 T58 $25.85
    2 x 5 $9.95
    1 x 3 $5.95
```

languages: Java Markdown

repo_language: Java

repo_name: nmenego/FlowerShop


text:
<repo_name>furkaandogan/exlinetr.infrastructure.logging<file_sep>/README.md
# Exlinetr Logging


text:
<repo_name>alexxuhub/numberFormat<file_sep>/src/numberApplication.php
<?php
/**
 *
 */

namespace numberApplication;

class numberApplication
{
    /**@var string*/
    private $point;

    public function __construct($point = 4)
    {
        $this->point = $point;
    }

    /**
     * add
     *@param $leftOperate
     *@param $rightOperate
     *@return string
     */
    public function bcAdd(string $leftOperate,string $rightOperate):string {
        return bcadd($leftOperate,$rightOperate,$this->point);
    }

    /**
     * sub
     * @param $leftOperate
     * @param $rightOperate
     * @return string
     */
    public function bcSub(string $leftOperate,string $rightOperate):string {
        return bcsub($leftOperate,$rightOperate,$this->point);
    }

    /**
     * mul
     * @param $leftOperate
     * @param $rightOperate
     * @return string
     * @throws \Exception
     */
    public function bcMul(string $leftOperate,string $rightOperate):string {
        return bcmul($leftOperate,$rightOperate,$this->point);
    }
    /**
     * div
     * @param $leftOperate
     * @param $rightOperate
     * @return string
     * @throws \Exception
     */
    public function bcDiv(string $leftOperate,string $rightOperate):string {
        try{
            return bcdiv($leftOperate,$rightOperate,$this->point);
        }catch (\Throwable $exception){
            throw new \Exception($exception->getMessage());
        }
    }

}<file_sep>/test/NumberTest.php
<?php
/**
 * Created by PhpStorm.
 * User: pangchenxu
 * Date: 2020-07-12
 * Time: 14:44
 */

require_once "../src/numberApplication.php";
$leftOperate = "3.141";
$rightOperate = "1.7";

$oob = new \numberApplication\numberApplication();

echo $oob->bcDiv($leftOperate,$rightOperate).PHP_EOL;

repo_language: PHP

repo_name: alexxuhub/numberFormat


text:
<repo_name>seb-sal/ticket_app<file_sep>/README.md
##Ticket App

Everyone comes to Seb


text:
<repo_name>josephptflanagan/20200920-Social-Media-API<file_sep>/README.md
# 20200920 Social Media API

## GitHub Repository
[Github](https://github.com/josephptflanagan/20200920-Social-Media-API)

## Video Walkthrough
[Walkthrough](https://drive.google.com/file/d/16Shvu3iP3TWzYsyZxQ8kfb9xO-7UbZjm/view)

### Description
This program serves as an educational exercise to build the back end of a simple social media platform using MongoDB as the databasing software. This excursion into NoSQL comes after weeks of work with exclusively SQL databases.

This specific "social media platform" will allow users to post their thoughts, and to react to the thoughts of other users.

### Installation Instructions
1. Download all files to your system
2. Use the following command in your terminal:

    * npm i express nodemon mongoose moment
3. Use npm start to run the program which can then be accessed at http://localhost:3001


text:
<file_sep># 0x02. Shell, I/O Redirections and filters

---
File|Description
---|---
0-hello_world | Print "Hello, World", followed by a new line to the standard output
1-confused_smiley | Display a confused smiley \"(Ôo)'
2-hellofile | Display the content of the /etc/passwd file
3-twofiles | Display the content of /etc/passwd and /etc/hosts
4-lastlines | Display the last 10 lines of /etc/passwd
5-firstlines | Display the first 10 lines of /etc/passwd
6-third_line | Display the third line of the file iacta. You are not allowed to use sed.
7-file | Create a file with a specific name containing the text Holberton School ending by a new line
8-cwd_state | Write into the file ls_cwd_content the result of the command ls -la
9-duplicate_last_line | Duplicate the last line of the file iacta
10-no_more_js | Delete all the regular files (not the directories) with a .js extension that are present in the current directory and all its subfolders
11-directories | Count the number of directories and sub-directories in the current directory
12-newest_files | Display the 10 newest files in the current directory sorted from newest to oldest one line at a time
13-unique | Take a list of words as input and prints in sorted order only words that appear exactly once
14-findthatword | Display lines containing the pattern root from the file /etc/passwd
15-countthatword | Display the number of lines that contain the pattern bin in the file /etc/passwd
16-whatsnext | Display lines containing the pattern root and 3 lines after them in the file /etc/passwd
17-hidethisword | Display all the lines in the file /etc/passwd that do not contain the pattern bin
18-letteronly | Display all lines of the file /etc/ssh/sshd_config starting with a letter
19-AZ | Replace all characters A and c from input to Z and e respectively
20-hiago | Remove all letters c and C from input
21-reverse | Reverse its input
22-users_and_homes | Display all users and their home directories, sorted by users
100-empty_casks | Find all empty files and directories in the current directory and all sub-directories. You are not allowed to use basename, grep, egrep, fgrep, or rgrep.
101-gifs | List all files with a .gif extension in the current directory and all its sub-directories. You are not allowed to use basename, grep, egrep, fgrep, or regrep.
102-acrostic | Decode acrostics that use the first letter of each line, without use of grep, egrep, fgrep, or rgrep.
103-the_biggest_fan | Parse web servers logs in TSV format as input and displays the 11 hosts or IP addresses which did the most requests. Order by number of requests, most active host or IP at the top. You are not allowed to use grep, egrep, fgrep, or regrep.
<file_sep># 0x03. Shell, init files, variables and expansions

---
File|Description
---|---
0-alias | Create an alias
1-hello_you | Print hello user, where user is the current Linux user
2-path | Add /action to the PATH. /action should be the last directory the shell looks into when looking for a program
3-paths | Count the number of directories in the PATH
4-global_variables | List environment variables
5-local_variables | List all local variables and environment variables, and functions
6-create_local_variable | Create a new local variable
7-create_global_variable | Create a new global variable
8-true_knowledge | Print the result of the addition of 128 with the value stored in the environment variable TRUEKNOWLEDGE, followed by a new line
9-divide_and_rule | Print the result of POWER divided by DIVIDE, followed by a new line
10-love_exponent_breath | Display the result of BREATH to the power LOVE
11-binary_to_decimal | Convert a number from base 2 to base 10
12-combinations | Print all possible combinations of two letters, except oo
13-print_float | Print a number with two decimal places
14-decimal_to_hexadecimal | Convert a number from base 10 to base 16
<file_sep># 0x01. Shell, permissions

---
File|Description
---|---
0-current_working_directory | Prints the absolute path name of the current working directory
1-listit | Display the contents list of your current directory
2-bring_me_home | Changes the working directory to the user's home directory
3-listfiles | Display current directory contents in a long format
4-listmorefiles | Display current directory contents, including hidden files (starting with .) in long format
5-listfilesdigitonly | Display current directory contents with user and group IDs displayed numberically in long format
6-firstdirectory | Create a directory named holberton in the /tmp/ directory
7-movethatfile | Move the file betty from /tmp/ to /tmp/holberton
8-firstdelete | Delete the file betty in /tmp/holberton
9-firstdirdeletion | Delete the directory holberton that is in the /tmp directory
10-back | Change the working directory to the previous one
11-lists | List all files in the current directory and the parent of the working directory and the /boot directory in long format
12-file_type | Print the type of the file named iamafile
13-symbolic_link | Create a symbolic link to /bin/ls, named __ls__. The symbolic link is created in the current working directory
14-copy_html | Copy all the HTML files from the current working directory to the parent of the working directory
15-lets_move | Move all files beginning with an uppercase letter to the directory /tmp/u
16-clean_emacs | Delete all files in the current working directory that end with the character ~
17-tree | Create the directories welcome/, welcome/to/ and welcome/to/holberton in the current directory
18-commas | List all files and directories of the current directory, separated by commas (,)
holberton.mgc | A magic file that can be used with the command 'file' to detect Holberton data files. Holberton data files always contain the string HOLBERTON at offset 0.


text:
<repo_name>asazizmn/shoppinglist-2.3.14<file_sep>/src/ShoppingList.js
import React, { Component } from 'react';

import AddItem from './AddItem';
import DeleteItem from './DeleteItem';
import ShoppingItems from './ShoppingItems';

class ShoppingList extends Component {

    state = {
        items: []
    };

    addItem = item => {
        this.setState(oldState => ({

            // utilising the spread operator to ensure old items are preserved
            // ... and not overwritten
            items: [...oldState.items, item]
        }));
    };

    deleteLastItem = event => {

        // please note that 'negative' values count from the end of the array
        // so in this case 'slice(0, -1)' means that select everything until, but not including, the last item
        this.setState(prevState => ({ items: this.state.items.slice(0, -1) }));
    };

    noItemsFound = () => {
        return this.state.items.length === 0;
    };

    render() {
        return (
            <React.Fragment>

Shopping List

                <AddItem
                    addItem={this.addItem}
                />

                <DeleteItem
                    deleteLastItem={this.deleteLastItem}
                    noItemsFound={this.noItemsFound}
                />

                <ShoppingItems
                    items={this.state.items}
                />
            </React.Fragment>
        )
    }
}

export default ShoppingList;<file_sep>/src/AddItem.js
import React from 'react';
import PropTypes from 'prop-types';

class AddItemForm extends React.Component {

    state = {
        value: ''
    };

    handleChange = event => {
        this.setState({ value: event.target.value });
    };

    inputIsEmpty = () => {
        return this.state.value === '';
    };

    clearInput = () => {
        this.setState({ value: '' });
    }

    handleAddItem = event => {

        // destructure
        const { addItem } = this.props;

        // to prevent browser reload/refresh when submitting
        event.preventDefault();

        // pass the item back to the parent component
        // so that it can actually be added to the items
        addItem(this.state.value);

        // now clear field for next input
        this.clearInput();
    };

    render() {

        return (
            <form onSubmit={this.handleAddItem}>
                <input
                    type="text"
                    placeholder="Enter New Item"
                    value={this.state.value}
                    onChange={this.handleChange}
                />
                <button disabled={this.inputIsEmpty()}>Add</button>
            </form>
        )
    }
}

// please note that a static value is accessible directly through the class
// `propTypes` in this case is being defined as a static field
AddItemForm.propTypes = {
    addItem: PropTypes.func.isRequired
};

export default AddItemForm;<file_sep>/src/App.js
import React from 'react';
import logo from './logo.svg';
import './App.css';

import TopHeader from './TopHeader';
import ShoppingList from './ShoppingList';

class App extends React.Component {
  render() {
    return (

        <TopHeader
          logo={logo}
          title={'ReactND - Coding Practice'}
        />

        <ShoppingList />

    );
  }
}

export default App;
<file_sep>/src/ShoppingItem.js
import React from 'react';
import PropTypes from 'prop-types';

const ShoppingItem = props => (

    // pls note that this is NOT where `key` should be specified
    // ... when dealing with component based lists

{props.item}

);

ShoppingItem.propTypes = {
    item: PropTypes.string.isRequired
};

export default ShoppingItem;

branch_name: refs/heads/main