1515# ' @seealso{\code{\link{dVoCC}}, \code{\link{climPCA}}}
1616# '
1717# ' @export
18- # ' @author Jorge Garcia Molinos and Naoki H. Kumagai
1918# ' @examples
20- # ' \dontrun{
21- # ' JapTC <- VoCC_get_data(" JapTC.tif")
19+ # '
20+ # ' JapTC <- terra::rast(system.file("extdata", " JapTC.tif", package = "VoCC") )
2221# '
2322# ' # Plot climate space for the two first variables(annual precipitation and maximum temperature)
2423# ' xy <- stats::na.omit(data.frame(
3736# ' plot = out, filename = file.path(getwd(), "example_plot.pdf"),
3837# ' width = 17, height = 17, unit = "cm"
3938# ' )
40- # ' }
39+ # '
4140climPlot <- function (xy , x.binSize , y.binSize , x.name = " V1" , y.name = " V2" ) {
4241 xp <- xy [, 1 ]
4342 yp <- xy [, 3 ]
4443 xf <- xy [, 2 ]
4544 yf <- xy [, 4 ]
4645
46+ # OPTIMIZATION: Pre-calculate ranges to avoid repeated min/max calls
47+ x_combined <- c(xp , xf )
48+ y_combined <- c(yp , yf )
49+ x_range <- range(x_combined )
50+ y_range <- range(y_combined )
51+
4752 # bins per axis
48- x.nbins <- floor(( abs(range( xp , xf ) [2 ] - range( xp , xf ) [1 ]) ) / x.binSize )
49- y.nbins <- floor(( abs(range( yp , yf ) [2 ] - range( yp , yf ) [1 ]) ) / y.binSize )
50- x.bin <- seq(floor(min(cbind( xp , xf ))) , ceiling(max(cbind( xp , xf )) ), length = x.nbins )
51- y.bin <- seq(floor(min(cbind( yp , yf ))) , ceiling(max(cbind( yp , yf )) ), length = y.nbins )
53+ x.nbins <- floor(abs(x_range [2 ] - x_range [1 ]) / x.binSize )
54+ y.nbins <- floor(abs(y_range [2 ] - y_range [1 ]) / y.binSize )
55+ x.bin <- seq(floor(x_range [ 1 ]) , ceiling(x_range [ 2 ] ), length = x.nbins )
56+ y.bin <- seq(floor(y_range [ 1 ]) , ceiling(y_range [ 2 ] ), length = y.nbins )
5257
5358 # define palette
5459 rf <- grDevices :: colorRampPalette(rev(RColorBrewer :: brewer.pal(11 , " Spectral" )))
@@ -75,18 +80,20 @@ climPlot <- function(xy, x.binSize, y.binSize, x.name = "V1", y.name = "V2") {
7580 freq2Dp [freq2Dp > UL ] <- UL
7681 freq2Df [freq2Df > UL ] <- UL
7782
78- # novel (in future but not present, 2), remnant (in both, 1), and dissapearing (in present but not future, 3) climates
79- freq2D <- diag(nrow = x.nbins , ncol = y.nbins )
80- freq2D [] <- NA
81- for (i in 1 : x.nbins ) {
82- for (j in 1 : y.nbins ) {
83- freq2D [i , j ] <- ifelse(is.na(freq2Dp [i , j ]) & ! is.na(freq2Df [i , j ]), 1 ,
84- ifelse(! is.na(freq2Dp [i , j ]) & is.na(freq2Df [i , j ]), 2 ,
85- ifelse(is.na(freq2Dp [i , j ]) & is.na(freq2Df [i , j ]), NA , 0 )
86- )
87- )
88- }
89- }
83+ # OPTIMIZATION: Vectorized climate classification - eliminates nested loops
84+ freq2D <- matrix (NA , nrow = x.nbins , ncol = y.nbins )
85+
86+ # Vectorized logical operations - much faster than nested loops
87+ present_na <- is.na(freq2Dp )
88+ future_na <- is.na(freq2Df )
89+
90+ # Novel climates: in future but not present
91+ freq2D [present_na & ! future_na ] <- 1
92+ # Disappearing climates: in present but not future
93+ freq2D [! present_na & future_na ] <- 2
94+ # Remnant climates: in both present and future
95+ freq2D [! present_na & ! future_na ] <- 0
96+ # NA remains NA (neither present nor future)
9097
9198 # plot climate space
9299 Freq2Dpf <- rbind(
0 commit comments