Follow up to error message with code!

4 views
Skip to first unread message

Alexandra McInturf

unread,
Jun 16, 2017, 11:56:47 AM6/16/17
to davi...@googlegroups.com
Hello all -

Realized that my code had not attached for the last java error message. It is as follows. I’ve bolded where I run across the error. 


library(ncdf4)
library(OpenStreetMap)
library(maptools)
setwd("~/Desktop/Cow Sharks/7G Final Analysis/Shark844")

####################### PLOTTING CURRENT: ########################
#1. Need to incorporate netCDF files in correct format (files generated in Delft3d)
#u and v are the coordinates within the grid system, while x and y are coordinates IRL
#alpha is the angle between grid system and real life coordinates
#need to also format time as a POSIXct, in order to convert from GMT to Pacific
ncin <- nc_open("trim-ggate2.nc")
udat <- ncvar_get(ncin, varid="U1")
vdat <- ncvar_get(ncin, varid="V1")
xcoor <- ncvar_get(ncin, varid="XCOR")
ycoor <- ncvar_get(ncin, varid="YCOR")
initialTimeVec <- ncvar_get(ncin, varid="time")
alpha <- ncvar_get(ncin, varid="ALFAS")
originTime <- as.POSIXct(strsplit(ncin[[14]][[28]]$dim[[4]]$units, split="seconds since ")[[1]][2], tz="GMT")
attr(originTime, "tzone") <- "US/Pacific"
timevec <- originTime+ initialTimeVec

timevec[1:10]
plot(xcoor[xcoor!=0],ycoor[xcoor!=0], cex=.2)
dim(udat)


###########with map background:
#2. Need to convert current coordinates so can be plotted onto map
conv.coord <- function(dd, crs1, crs2)
{
temp <- SpatialPoints(dd, proj4string=CRS(crs1))
data.frame(spTransform(temp, CRS(crs2)))
}

udat[1:10,1:10,1:3]
udat[100:110,100:110,1:3]

#3. Have readjusted time stamp so it reflects the correct time in the correct order for
#the simulation
#this is particularly important because we are sampling on every round number for the current data, 
#but our shark data takes place every second (does not line up without conversion)
timestamp <- "2008-07-26 17:10:00"
index <- which(format(timevec)== timestamp)
arrowsize <- 500
subsampling <- 1

#4. Create a dataframe with all of the necessary coordinate info (both IRL and grid)
cdatall <- data.frame(x=xcoor[xcoor!=0], y=ycoor[xcoor!=0], u=udat[,,index][xcoor!=0], v=vdat[,,index][xcoor!=0], alpha=alpha[xcoor!=0])
cdatallsub <- cdatall[sort(sample(1:nrow(cdatall), nrow(cdatall)/subsampling)),]

#5. here we convert coordinates to correct time zone
#more importantly, we also convert our data from the grid to match that of our IRL data using alpha
cdat <- data.frame(conv.coord(cdatallsub[,1:2], crs1="+proj=utm +zone=10 +north +datum=WGS84", crs2="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"), cdatallsub[,3:5])
u2 <- apply(cdat, 1, function(v) {v[3]*cos(-v[5]*pi/180)+v[4]*sin(-v[5]*pi/180)})
v2 <- apply(cdat, 1, function(v) {-v[3]*sin(-v[5]*pi/180)+v[4]*cos(-v[5]*pi/180)})
cdat <- cbind(cdat[,1:2], u2, v2, cdat[,3:5])
head(cdat)




##6. here we create our map of the SF bay
sizex <- 0.1
sizey <- 0.07
center <- apply(cdatall[,1:2],2,mean)
centerdeg <- unlist(c(conv.coord(matrix(center, ncol=2), crs1="+proj=utm +zone=10 +north +datum=WGS84", crs2="+proj=longlat +datum=WGS84")))
map <-  openmap(c(lat= centerdeg[2]+sizey,   lon= centerdeg[1]-sizex),c(lat= centerdeg[2]-sizey,   lon= centerdeg[1]+sizex), minNumTiles=9,type="bing")
plot(map, main="title")
arrows(cdat[,1], cdat[,2], cdat[,1]+ cdat[,3]*arrowsize, cdat[,2]+ cdat[,4]*arrowsize, angle=30, length=.02, col=2, lwd=.5)###pretty!



###7. Map the currents, by creating a function...again, have to convert time so that our times
#for plotting match up with the times for the current data
mapCurrent <- function(map, udat, vdat, alpha, xcoor, ycoor, timestamp, timevec, arrowsize, arrowcol, arrowlength, arrowwidth){
timestamp2 <- as.POSIXct(timestamp, "%Y-%m-%d %H:%M:%S", tz="US/Pacific")
minval <- floor(as.numeric(format(timestamp2+150, "%M"))/5)*5
timestamp3 <- as.POSIXct(paste0(format(timestamp2+150, "%Y-%m-%d %H:"), minval, ":00"),tz="US/Pacific")
index <- which(format(timevec, "%Y-%m-%d %H:%M:%S")== format(timestamp3, "%Y-%m-%d %H:%M:%S"))
cdatall <- data.frame(x=xcoor[xcoor!=0], y=ycoor[xcoor!=0], u=udat[,,index][xcoor!=0], v=vdat[,,index][xcoor!=0], alpha=alpha[xcoor!=0])
cdatallsub <- cdatall[sort(sample(1:nrow(cdatall), nrow(cdatall))),]
cdat <- data.frame(conv.coord(cdatallsub[,1:2], crs1="+proj=utm +zone=10 +north +datum=WGS84", crs2="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"), cdatallsub[,3:5])
u2 <- apply(cdat, 1, function(v) {v[3]*cos(-v[5]*pi/180)+v[4]*sin(-v[5]*pi/180)})
v2 <- apply(cdat, 1, function(v) {-v[3]*sin(-v[5]*pi/180)+v[4]*cos(-v[5]*pi/180)})
cdat <- cbind(cdat[,1:2], u2, v2, cdat[,3:5])
par(mar=c(0,0,0,0), oma=c(0,0,3,0))
plot(map)
mtext(timestamp, side=3, line=1, cex=1.5)
arrows(cdat[,1], cdat[,2], cdat[,1]+ cdat[,3]*arrowsize, cdat[,2]+ cdat[,4]*arrowsize, angle=30, length= arrowlength, col= arrowcol, lwd= arrowwidth)###pretty!
}

plot(map)

mapCurrent(map=map, udat=udat, vdat=vdat, alpha=alpha, xcoor=xcoor, ycoor=ycoor, timestamp="2008-07-30 12:02:35", timevec=timevec, arrowsize=500, arrowcol="red", arrowlength=0.02, arrowwidth=.5)

Alexandra McInturf
PhD Student, Animal Behavior
Biotelemetry Laboratory
University of California, Davis
amci...@ucdavis.edu  
agmci...@gmail.com



Alex Mandel

unread,
Jun 19, 2017, 12:39:58 PM6/19/17
to davi...@googlegroups.com, Alexandra McInturf
So to clarify a few things.
You are using the OpenStreetMap package, which uses a Java library.
https://cran.r-project.org/web/packages/OpenStreetMap/index.html

Though you are not actually using OSM data you are using Bing (be aware
of terms of service). You can try changing which basemap you use to see
if it's just an issue with Bing, the OpenStreetMap library, or the
request you are sending.

And I assume this is because you want it to work with ggplot2?

As a workaround, try the ggmap package instead.
https://cran.r-project.org/web/packages/ggmap/index.html

For not so static maps, I happen to like the leaflet library.

Enjoy,
Alex Mandel, PhD

Center for Spatial Sciences
http://spatial.ucdavis.edu
Geospatial and Farming Systems Research Consortium
http://gfc.ucdavis.edu
University of California, Davis
> amci...@ucdavis.edu <mailto:amci...@ucdavis.edu>
> agmci...@gmail.com <mailto:agmci...@gmail.com>
>
>
>
Reply all
Reply to author
Forward
0 new messages