## ----include = FALSE----------------------------------------------------------
# Live downloads write to the user cache, so skip them on CRAN.
live_ok <- identical(Sys.getenv("NOT_CRAN"), "true") &&
  !is.null(tryCatch(lightson::bhuvan_stats(years = 2024), error = function(e) NULL))

knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  dev.args = list(bg = "transparent"),
  eval     = live_ok
)

## ----offline-notice, echo = FALSE, results = "asis", eval = !live_ok----------
# cat("_Live data were not downloaded when this vignette was built, so its examples were not run. Run this vignette locally to see the output._")

## -----------------------------------------------------------------------------
library(lightson)

df <- bhuvan_stats()
dim(df)
head(df)

## -----------------------------------------------------------------------------
bihar <- bhuvan_stats(state = "BIHAR")
bihar

## ----plot-bihar, fig.width = 7, fig.height = 4--------------------------------
library(ggplot2)
plot_ntl_trend(bihar,
  region = "Bihar",
  caption = "Source: ISRO Bhuvan NTL portal"
)

## ----fig.width = 7, fig.height = 4--------------------------------------------
states <- c("Bihar", "Maharashtra", "Tamil Nadu", "Uttar Pradesh", "Kerala")
plot_ntl_trend(
  df[df$state %in% states, ],
  region   = states,
  subtitle = "Bhuvan state mean radiance (nW/cm^2/sr)",
  caption  = "Source: ISRO Bhuvan NTL portal"
)

## -----------------------------------------------------------------------------
states_sf <- get_india_admin("state")

years_sel <- c(2012, 2015, 2018, 2021, 2024)
rasters <- tryCatch(
  bhuvan_raster("IND", years = years_sel, bbox_pad = c(0, 0, 0, 3.5)),
  error = function(e) list()
)

## ----fig.width = 11, fig.height = 8, eval = live_ok && length(rasters) > 0----
plot_ntl_panel(
  rasters,
  polygons = states_sf,
  title    = "India at night, 2012 to 2024",
  subtitle = "Bhuvan NTL radiance classes. Boundaries: LGD/SoI.",
  caption  = "Source: ISRO Bhuvan NTL portal",
  ncol     = 3L
)

## -----------------------------------------------------------------------------
post_2015 <- bhuvan_stats(years = 2015:2024)
nrow(post_2015)

