2121# ' @seealso{\code{\link{tempTrend}}, \code{\link{gVoCC}}}
2222# '
2323# ' @importFrom rlang .data
24+ # ' @importFrom stats na.omit
2425# '
2526# ' @export
2627# ' @author Jorge Garcia Molinos, David S. Schoeman, and Michael T. Burrows
2728# ' @examples
28- # '
29+ # ' \dontrun{
2930# ' HSST <- VoCC_get_data("HSST.tif")
3031# '
3132# ' yrSST <- sumSeries(HSST,
3839# ' sg <- spatGrad(yrSST, th = 0.0001, projected = FALSE)
3940# '
4041# ' terra::plot(sg)
42+ # ' }
4143# '
4244spatGrad <- function (r , th = - Inf , projected = FALSE ) {
45+
4346 # Fix devtools check warnings
47+ " ." <- NULL
4448 gradNS1 <- gradNS2 <- gradNS3 <- gradNS4 <- gradNS5 <- gradNS6 <- gradWE1 <- gradWE2 <- gradWE3 <- gradWE4 <- gradWE5 <- gradWE6 <- NULL
4549 sy <- sx <- NSgrad <- WEgrad <- NULL
4650 clim <- climE <- climN <- climNE <- climNW <- climS <- climSE <- climSW <- climW <- climFocal <- NULL
47- to <- code <- i.to <- LAT <- angle <- Grad <- NULL
51+ to <- code <- i.to <- LAT <- angle <- Grad <- .SD <- NULL
4852
4953 if (terra :: nlyr(r ) > 1 ) {
5054 r <- terra :: mean(r , na.rm = TRUE )
@@ -55,26 +59,18 @@ spatGrad <- function(r, th = -Inf, projected = FALSE) {
5559
5660 # Create a columns for focal and each of its 8 adjacent cells
5761 y <- data.table :: data.table(terra :: adjacent(r , 1 : terra :: ncell(r ), directions = 8 , pairs = TRUE ))
58- y <- stats :: na.omit(y [, climFocal : = terra :: values(r )[from ]][order(from , to )]) # Get value for focal cell, order the table by raster sequence and omit NAs (land cells)
59-
60- # TODO JDE added in na.rm = TRUE as I was getting NaN. I can't test if this behaviour has changed from raster....
61- # On second thought I am not sure if NAs are valid here. It gives errors below when calculating weighted means
62- y [, clim : = terra :: values(r , na.rm = TRUE )[to ]] # Insert values for adjacent cells
62+ y <- na.omit(y [, climFocal : = terra :: values(r )[from ]][order(from , to )]) # Get value for focal cell, order the table by raster sequence and omit NAs (land cells)
63+ y [, clim : = terra :: values(r )[to ]] # Insert values for adjacent cells
6364 y [, sy : = terra :: rowFromCell(r , from ) - terra :: rowFromCell(r , to )] # Column to identify rows in the raster (N = 1, mid = 0, S = -1)
6465 y [, sx : = terra :: colFromCell(r , to ) - terra :: colFromCell(r , from )] # Same for columns (E = 1, mid = 0, W = -1)
6566 y [sx > 1 , sx : = - 1 ] # Sort out the W-E wrap at the dateline, part I
6667 y [sx < - 1 , sx : = 1 ] # Sort out the W-E wrap at the dateline, part II
6768 y [, code : = paste0(sx , sy )] # Make a unique code for each of the eight neighbouring cells
6869
6970 # Code cells with positions
70- y [
71- list (
72- code = c(" 10" , " -10" , " -11" , " -1-1" , " 11" , " 1-1" , " 01" , " 0-1" ),
73- to = c(" climE" , " climW" , " climNW" , " climSW" , " climNE" , " climSE" , " climN" , " climS" )
74- ),
75- on = " code" ,
76- code : = i.to
77- ]
71+ y [.(code = c(" 10" , " -10" , " -11" , " -1-1" , " 11" , " 1-1" , " 01" , " 0-1" ),
72+ to = c(" climE" , " climW" , " climNW" , " climSW" , " climNE" , " climSE" , " climN" , " climS" )),
73+ on = " code" , code : = i.to ]
7874 y <- data.table :: dcast(y [, c(" from" , " code" , " clim" )], from ~ code , value.var = " clim" )
7975 y [, climFocal : = terra :: values(r )[from ]] # Put climFocal back in
8076 y [, LAT : = terra :: yFromCell(r , from )] # Add focal cell latitude
@@ -93,7 +89,8 @@ spatGrad <- function(r, th = -Inf, projected = FALSE) {
9389 y [, gradWE5 : = (climE - climFocal ) / (cos(co * CircStats :: rad(LAT )) * (d * re [1 ]))]
9490 y [, gradWE6 : = (climSE - climS ) / (cos(co * CircStats :: rad(LAT - re [2 ])) * (d * re [1 ]))]
9591
96- # NS gradients difference in temperatures for each northern and southern pairs divided by the distance between them (111.325 km per degC *re[2] degC)
92+ # NS gradients difference in temperatures for each northern and southern pairs divided by
93+ # the distance between them (111.325 km per degC *re[2] degC)
9794 # Positive values indicate an increase in sst from S to N (i.e., in line with the Cartesian y axis)
9895 y [, gradNS1 : = (climNW - climW ) / (d * re [2 ])]
9996 y [, gradNS2 : = (climN - climFocal ) / (d * re [2 ])]
@@ -102,29 +99,16 @@ spatGrad <- function(r, th = -Inf, projected = FALSE) {
10299 y [, gradNS5 : = (climFocal - climS ) / (d * re [2 ])]
103100 y [, gradNS6 : = (climE - climSE ) / (d * re [2 ])]
104101
105-
106- for (nn in 1 : 365 ){
107-
108- print(nn )
109-
110- print(stats :: weighted.mean(y [nn ,12 : 17 ], w = c(1 , 2 , 1 , 1 , 2 , 1 ), na.rm = TRUE ))
111-
112-
113- }
114-
115-
116- browser()
117- # Calulate NS and WE gradients. NOTE: for angles to work (at least using simple positive and negative values on Cartesian axes),
118- # S-N & W-E gradients need to be positive.
119- # JDE Notes: 1 in apply = operate over rows
120- # Lots of NAs in clim. Can these be removed? Should they be? Chat to Dave S
121- y [, WEgrad : = apply(data.table :: .SD , 1 , function (x ) stats :: weighted.mean(x , w = c(1 , 2 , 1 , 1 , 2 , 1 ), na.rm = TRUE )), .SDcols = gradWE1 : gradWE6 ]
122- y [, NSgrad : = apply(data.table :: .SD , 1 , function (x ) stats :: weighted.mean(x , c(1 , 2 , 1 , 1 , 2 , 1 ), na.rm = T )), .SDcols = 18 : 23 ]
102+ # Calulate NS and WE gradients.
103+ # NOTE: for angles to work (at least using simple positive and negative values on Cartesian axes),
104+ # S-N & W-E gradients need to be positive)
105+ y [, WEgrad : = apply(.SD , 1 , function (x ) stats :: weighted.mean(x , c(1 , 2 , 1 , 1 , 2 , 1 ), na.rm = TRUE )), .SDcols = 12 : 17 ]
106+ y [, NSgrad : = apply(.SD , 1 , function (x ) stats :: weighted.mean(x , c(1 , 2 , 1 , 1 , 2 , 1 ), na.rm = TRUE )), .SDcols = 18 : 23 ]
123107 y [is.na(WEgrad ) & ! is.na(NSgrad ), WEgrad : = 0L ] # Where NSgrad does not exist, but WEgrad does, make NSgrad 0
124108 y [! is.na(WEgrad ) & is.na(NSgrad ), NSgrad : = 0L ] # same the other way around
125109
126110 # Calculate angles of gradients (degrees) - adjusted for quadrant (0 deg is North)
127- y [, angle : = angulo(data.table :: . SD$ WEgrad , data.table :: .SD $ NSgrad ), .SDcols = c(" WEgrad" , " NSgrad" )]
111+ y [, angle : = angulo(. SD$ WEgrad , .SD $ NSgrad ), .SDcols = c(" WEgrad" , " NSgrad" )]
128112
129113 # Calculate the vector sum of gradients (C/km)
130114 y [, Grad : = sqrt(apply(cbind((y $ WEgrad ^ 2 ), (y $ NSgrad ^ 2 )), 1 , sum , na.rm = TRUE ))]
@@ -139,5 +123,6 @@ spatGrad <- function(r, th = -Inf, projected = FALSE) {
139123 rGrad [rGrad [] < th ] <- th
140124 output <- c(rGrad , rAng )
141125 names(output ) <- c(" Grad" , " Ang" )
126+
142127 return (output )
143128}
0 commit comments