interpolate_spherical: Interpolate in spherical space

View source: R/interpolate_spherical.R

interpolate_sphericalR Documentation

Interpolate in spherical space

Description

Interpolate values from canopy photographs using inverse distance weighting (IDW) with k-nearest neighbors, computing distances in spherical coordinates that map the sky vault. Optionally blend with a model surface to fill voids.

Usage

interpolate_spherical(
  sky_points,
  z,
  a,
  filling_source = NULL,
  w = 1,
  k = 3,
  p = 2,
  angular_radius = 20,
  rule = "any",
  size = 50
)

Arguments

sky_points

data.frame returned by extract_rr(). If generated by other means, it must contain columns row, col, z, a, and rr, where the first four define geometry (degrees) and rr is the value to interpolate.

z

terra::SpatRaster generated with zenith_image().

a

terra::SpatRaster generated with azimuth_image().

filling_source

optional numeric terra::SpatRaster with one layer. Surface used to complement rr when neighbors are insufficient (e.g., output of fit_cie_model()). If NULL (default), no filling is applied.

w

numeric vector of length one. Weight assigned to filling_source in the blend with local estimates (see Eq. 6 in \insertCiteLang2010;textualrcaiman).

k

numeric vector of length one. Number of neighbors.

p

numeric vector of length one. Inverse distance weighting exponent.

angular_radius

numeric vector of length one. The maximum radius for searching k-nearest neighbors (KNN) in degrees.

rule

character vector of length one. Either "any" or "all". With "any", pixels within angular_radius of at least one sample are interpolated. With "all", pixels are interpolated only if the k nearest neighbors lie within angular_radius. If k = 1, both are equivalent.

size

numeric vector of length one. Number of rows and columns of the low-resolution grid used before resampling to full resolution.

Details

Distances are great-circle distances on the sky vault. When filling_source is provided, local IDW estimates are blended with that surface following Eq. 6 in \insertCiteLang2010;textualrcaiman. For efficiency, interpolation runs on a temporary low-resolution grid of size size.

Value

Numeric terra::SpatRaster with one layer of interpolated values and the geometry of z.

Note

This function assumes that sky_points and the terra::SpatRaster inputs are spatially aligned and share the same geometry. No checks are performed to enforce this.

References

\insertAllCited

Examples

## Not run: 
caim <- read_caim()
z <- zenith_image(ncol(caim), lens())
a <- azimuth_image(z)

# Manual method following Lang et al. (2010)
path <- system.file("external/sky_points.csv",
                    package = "rcaiman")
sky_points <- read.csv(path)
sky_points <- sky_points[c("Y", "X")]
colnames(sky_points) <- c("row", "col")
head(sky_points)
plot(caim$Blue)
points(sky_points$col, nrow(caim) - sky_points$row, col = 2, pch = 10)

# x11()
# plot(caim$Blue)
# sun_angles <- click(c(z, a), 1) %>% as.numeric()
sun_angles <- c(z = 49.5, a = 27.4) #taken with above lines then hardcoded

sun_row_col <- row_col_from_zenith_azimuth(z, a,
                                           sun_angles["z"],
                                           sun_angles["a"])
points(sun_row_col[2], nrow(caim) - sun_row_col[1],
       col = "yellow", pch = 8, cex = 3)

rr <- extract_rr(caim$Blue, z, a, sky_points)

set.seed(7)
model <- fit_cie_model(rr, sun_angles,
                       general_sky_type = "Clear")

sky_cie <- cie_image(z, a, model$sun_angles, model$coef)

sky_rr <- interpolate_spherical(rr$sky_points, z, a,
                                filling_source = sky_cie,
                                w = 1,
                                k = 10,
                                p = 2,
                                angular_radius = 20,
                                rule = "any",
                                size = 50)
plot(r/sky_rr/rr$zenith_dn)

## End(Not run)

rcaiman documentation built on Sept. 9, 2025, 5:42 p.m.