fit_trend_surface: Fit a trend surface to sky digital numbers

View source: R/fit_trend_surface.R

fit_trend_surfaceR Documentation

Fit a trend surface to sky digital numbers

Description

Fits a trend surface to sky digital numbers using spatial::surf.ls() as the computational workhorse.

Usage

fit_trend_surface(sky_points, r, np = 6, col_id = "dn", extrapolate = FALSE)

Arguments

sky_points

data.frame with columns row, col, and one additional numeric column with values to interpolate. Typically returned by extract_rr() or extract_dn().

r

numeric terra::SpatRaster with one layer. Image from which sky_points were derived, or another raster with the same number of rows and columns. Used only as geometric template; cell values are ignored.

np

degree of polynomial surface

col_id

numeric or character vector of length one. The name or position of the column in sky_points containing the values to interpolate.

extrapolate

logical vector of length one. If TRUE, predictions are extrapolated to the entire extent of r; otherwise, predictions are limited to the convex hull of the input sky points.

Details

This function models the variation in digital numbers across the sky dome by fitting a polynomial surface in Cartesian space. It is intended to capture smooth large-scale gradients and is more effective when called via apply_by_direction().

Value

List with named elements:

raster

terra::SpatRaster containing the fitted surface.

model

object of class trls returned by spatial::surf.ls().

r2

numeric value giving the coefficient of determination (R^2) of the fit.

References

\insertAllCited

Examples

## Not run: 
caim <- read_caim()
z <- zenith_image(ncol(caim), lens())
a <- azimuth_image(z)
m <- !is.na(z)
r <- caim$Blue

bin <- binarize_by_region(r, ring_segmentation(z, 15), "thr_isodata") &
  select_sky_region(z, 0, 88)

g <- sky_grid_segmentation(z, a, 10, first_ring_different = TRUE)
sky_points <- extract_sky_points(r, bin, g, dist_to_black = 3)
plot(bin)
points(sky_points$col, nrow(caim) - sky_points$row, col = 2, pch = 10)
sky_points <- extract_dn(r, sky_points, use_window = TRUE)

sky_s <- fit_trend_surface(sky_points, r, np = 4, col_id = 3,
                           extrapolate = TRUE)
plot(sky_s$raster)
binarize_with_thr(r/sky_s$raster, 0.5) %>% plot()

sky_s <- fit_trend_surface(sky_points, r, np = 6, col_id = 3,
                           extrapolate = FALSE)
plot(sky_s$raster)
binarize_with_thr(r/sky_s$raster, 0.5) %>% plot()

## End(Not run)

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