R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library (dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library (tidyr)
library (ggplot2)
library (leaflet)
library (gsw)
library (matrixStats)
## 
## Attaching package: 'matrixStats'
## The following object is masked from 'package:dplyr':
## 
##     count

install.packages(“dplyr”) install.packages(“tidyr”) install.packages(“leaflet”) install.packages(“knitr”) install.packages(“rmarkdown”) install.packages(“gsw”) install.packages(“matrixStats”)

##shell("cd C:/Users/ingri/MAST467/Data/output.dat")
shell("C:/Users/ingri/MAST467/Data/NASA.cmd", translate=TRUE)
Omg<-read.csv("C:/Users/ingri/MAST467/Data/output.dat",sep=" ")

###Plotting Temp and Sal ###From Dr.Muenchow's code
ggplot(Omg,mapping=aes(x=sal,y=tem,group=cast,color=dep)) +
  geom_point() +
  xlim(c(25,35)) +
  ylim(c(-2,8)) +
  xlab("Salinity, psu") +
  ylab("Temperature, C") +
  scale_color_viridis_c(guide=guide_colorbar(barheight=15)) +
  facet_wrap(~cast)

###Depth and Temp ###From Dr.Muenchow's code
ggplot(Omg,aes(x=dep,y=tem,group=cast)) +
  geom_path() +
  ylim(c(-1.5,6.5)) +
  xlim(c(500,0)) +
  ylab("Temperature, C") +
  xlab("Depth, m") +
  coord_flip(expand=FALSE) + 
  facet_wrap(~cast)
## Warning: Removed 3735 row(s) containing missing values (geom_path).

###Depth and Sal ###From Dr.Muenchow's code
ggplot(Omg,mapping=aes(x=dep,y=sal,group=cast)) +
  geom_line() +
  ylim(c(30,35)) +
  xlim(c(500,0)) +
  ylab("Salinity, psu") +
  coord_flip(expand=FALSE) +
  facet_wrap(~cast)
## Warning: Removed 3097 row(s) containing missing values (geom_path).

###Making data fram for 1 cast
Omg.df = data.frame(Omg$lon, Omg$lat, Omg$dep, Omg$tem, Omg$sal, Omg$cast)
###rename columns
colnames(Omg.df) = c("lon", "lat", "dep", "tem", "sal", "cast")
###Specifying cast number
C106 = Omg.df[Omg.df$cast==106,]

###Using one cast (106) to convert in-situ temperature, salinity, and depth to pressure, absolute salinity, and potential temperature
#Pressure
p = gsw_p_from_z(-C106$dep, C106$lat)
#Absolute Salinity
SA = gsw_SA_from_SP(C106$sal, p, C106$lat, C106$lon)
#Potential Temperature
pt = gsw_pt_from_t(SA, C106$tem, p, 0)


####Using cast 106 to estimate potential denisty at both zero and 1000-dbar pressure as well as the freezing point
#Conservative Temperature
CT= gsw_CT_from_t(SA, C106$tem, p)
#Potential Density to Zero Pressure
r0 = gsw_sigma0(SA,CT)
#Potential Density to 1000-m Pressure
r1 = gsw_sigma1(SA,CT)
#Freezing Point
tf = gsw_CT_freezing(SA, p, saturation_fraction = 1)

dat= data.frame(p, SA, pt, CT, r0, r1, tf, C106$tem)

###Plotting profile to compare in-situ with potential temperature
ggplot(dat, aes(x=p)) + 
  geom_line(aes(y = C106$tem), color = "darkred") + 
  geom_line(aes(y = pt), color="steelblue", linetype="twodash")+
  coord_flip(expand=FALSE)+
  ylim(c(3,6.5)) +
  xlim(c(350,0)) +
  ylab("Temperature, C") +
  xlab("Pressure, p")

###Plotting profile to compare absolute salinity with pressure
ggplot(dat, aes(x=p)) + 
  geom_line(aes(y = SA), color = "darkred") + 
  #geom_line(aes(y = r1), color="steelblue", linetype="twodash")+
  coord_flip(expand=FALSE)+
  ylim(c(31.5,35.5)) +
  xlim(c(325,0)) +
  ylab("Absolute Salinity, SA") +
  xlab("Pressure, p")

###Plotting profile to compare potential density with pressure
ggplot(dat, aes(x=p)) + 
  #geom_line(aes(y = SA), color = "darkred") + 
  geom_line(aes(y = r1), color="steelblue", linetype="twodash")+
  coord_flip(expand=FALSE)+
  ylim(c(29,32.5)) +
  xlim(c(325,0)) +
  ylab("Potential density to 1000-m pressure") +
  xlab("Pressure, p")

###Plotting profile to compare potential density with absolute salinity
ggplot(dat, aes(x=r1)) + 
  geom_line(aes(y = SA), color = "darkred") + 
  #geom_line(aes(y = r1), color="steelblue", linetype="twodash")+
  coord_flip(expand=FALSE)+
  ylim(c(31.5,35.5)) +
  xlim(c(29,32.5)) +
  ylab("Absolute Salinity, SA") +
  xlab("Potential density to 1000-m pressure")

###creating for loop
Dmax = 0
for (i in 106:148)
{data<-dplyr::filter(Omg, cast==i)
  print(i)
  last=length(data[,1])
  print(last)
  
  Depth<-as.numeric(data[,3])
  Temp<-as.numeric(data[,4])
  Salt<-as.numeric(data[,5])
  
  maxDepth<-as.integer(tail(Depth,1))
  print(maxDepth)
  #if(maxDepth>=Dmax)
  
  Dmax = maxDepth
  
  if (last>=1){
  binDepth<-seq(2,maxDepth,1)
  
  binSalt<-binMeans(Salt, x=Depth, bx=binDepth, na.rm=TRUE)
  binTemp<-binMeans(Temp, x=Depth, bx=binDepth, na.rm=TRUE)
  
  }}###end loop
## [1] 106
## [1] 2798
## [1] 310
## [1] 107
## [1] 4617
## [1] 527
## [1] 108
## [1] 1956
## [1] 217
## [1] 109
## [1] 5470
## [1] 609
## [1] 110
## [1] 3687
## [1] 403
## [1] 111
## [1] 0
## integer(0)
## [1] 112
## [1] 0
## integer(0)
## [1] 113
## [1] 0
## integer(0)
## [1] 114
## [1] 0
## integer(0)
## [1] 115
## [1] 0
## integer(0)
## [1] 116
## [1] 0
## integer(0)
## [1] 117
## [1] 0
## integer(0)
## [1] 118
## [1] 0
## integer(0)
## [1] 119
## [1] 0
## integer(0)
## [1] 120
## [1] 0
## integer(0)
## [1] 121
## [1] 0
## integer(0)
## [1] 122
## [1] 0
## integer(0)
## [1] 123
## [1] 0
## integer(0)
## [1] 124
## [1] 0
## integer(0)
## [1] 125
## [1] 0
## integer(0)
## [1] 126
## [1] 0
## integer(0)
## [1] 127
## [1] 0
## integer(0)
## [1] 128
## [1] 0
## integer(0)
## [1] 129
## [1] 0
## integer(0)
## [1] 130
## [1] 0
## integer(0)
## [1] 131
## [1] 0
## integer(0)
## [1] 132
## [1] 0
## integer(0)
## [1] 133
## [1] 0
## integer(0)
## [1] 134
## [1] 0
## integer(0)
## [1] 135
## [1] 0
## integer(0)
## [1] 136
## [1] 0
## integer(0)
## [1] 137
## [1] 0
## integer(0)
## [1] 138
## [1] 0
## integer(0)
## [1] 139
## [1] 0
## integer(0)
## [1] 140
## [1] 0
## integer(0)
## [1] 141
## [1] 0
## integer(0)
## [1] 142
## [1] 0
## integer(0)
## [1] 143
## [1] 0
## integer(0)
## [1] 144
## [1] 0
## integer(0)
## [1] 145
## [1] 0
## integer(0)
## [1] 146
## [1] 3122
## [1] 354
## [1] 147
## [1] 6382
## [1] 692
## [1] 148
## [1] 434
## [1] 49
n=maxDepth-2
dat2 = data.frame (binDepth[1:n], binSalt[1:n], binTemp[1:n])

ggplot
## function (data = NULL, mapping = aes(), ..., environment = parent.frame()) 
## {
##     UseMethod("ggplot")
## }
## <bytecode: 0x0000000013303b80>
## <environment: namespace:ggplot2>