## ----setup, include = FALSE---------------------------------------------------
NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN", "false")), "true")
# network chunks need NOT_CRAN and a reachable CPCB feed
CPCB_OK <- NOT_CRAN && tryCatch(
  {
    httr::status_code(httr::HEAD(
      "https://airquality.cpcb.gov.in/caaqms/iit_rss_feed_with_coordinates",
      httr::timeout(5)
    )) < 400
  },
  error = function(e) FALSE
)
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5,
  eval = CPCB_OK
)

## ----install, eval=FALSE------------------------------------------------------
# # Install from GitHub
# devtools::install_github("saketlab/vayuayan", subdir = "R")

## ----quickstart, eval = TRUE--------------------------------------------------
library(vayuayan)
library(dplyr)

vayuayan_welcome()

## ----historical_explore, eval = TRUE------------------------------------------
states <- cpcb_get_state_list()
head(states)

cities <- cpcb_get_city_list("Maharashtra")
head(cities)

stations <- cpcb_get_station_list("Mumbai")
head(stations)

## ----add_coords, eval = TRUE--------------------------------------------------
delhi_stations <- cpcb_add_coords(cpcb_get_station_list("Delhi"))
head(delhi_stations)

## ----city_daily---------------------------------------------------------------
# daily <- cpcb_city_daily(c("Delhi", "Mumbai"), 2024, cache_dir = tempdir())
# 
# daily %>%
#   group_by(city, month) %>%
#   summarise(pm25 = mean(value), .groups = "drop")

## ----station_daily------------------------------------------------------------
# station_daily <- cpcb_station_daily(
#   head(delhi_stations$value, 3), 2025,
#   cache_dir = tempdir()
# )
# count(station_daily, station_id)

## ----live_aqi-----------------------------------------------------------------
# nearest <- cpcb_get_nearest_station(coords = c(19.0760, 72.8777))
# nearest
# 
# aqi_data <- cpcb_get_live_aqi_data(station_id = nearest$station_id)
# aqi_data$metrics

## ----live_table---------------------------------------------------------------
# live <- stations_to_dataframe(cpcb_get_all_india())
# 
# live %>%
#   group_by(state_id) %>%
#   summarise(
#     stations = n(),
#     live = sum(live),
#     avg_aqi = mean(avg_aqi, na.rm = TRUE),
#     .groups = "drop"
#   ) %>%
#   arrange(desc(stations)) %>%
#   head(10)
# 
# count(live, aqi_category, sort = TRUE)

## ----live_map-----------------------------------------------------------------
# library(ggplot2)
# library(sf)
# 
# aqi_colors <- c(
#   "Good"         = "green",
#   "Satisfactory" = "lightgreen",
#   "Moderate"     = "yellow",
#   "Poor"         = "orange",
#   "Very Poor"    = "red",
#   "Severe"       = "darkred",
#   "No Data"      = "grey70"
# )
# 
# live %>%
#   filter(live) %>%
#   st_as_sf(coords = c("longitude", "latitude"), crs = 4326) %>%
#   ggplot() +
#   geom_sf(aes(color = aqi_category), size = 2, alpha = 0.6) +
#   scale_color_manual(values = aqi_colors) +
#   labs(title = "Live CPCB monitoring stations", color = "AQI category") +
#   theme_minimal()

## ----pm25_setup, eval = FALSE-------------------------------------------------
# pm25_nc <- pm25_download_netcdf(2023, 11, region = "AS")
# delhi <- india_boundary(state = "Delhi")

## ----pm25_stats, eval = FALSE-------------------------------------------------
# pm25_get_stats(
#   geojson_file = "https://geo.bharatviz.org/geojsons/cities/delhi.geojson",
#   nc_file      = pm25_nc
# )
# 
# pm25_get_stats(
#   geojson_file = "https://geo.bharatviz.org/geojsons/districts/India_LGD_districts.geojson",
#   nc_file      = pm25_nc,
#   group_by     = "state_name"
# )

## ----pm25_map, eval = FALSE---------------------------------------------------
# r <- pm25_load_netcdf_bbox(pm25_nc, delhi, var = "PM25", mask = TRUE)
# pm25_map(r, delhi, title = "Delhi, November 2023")

## ----pm25_cache, eval = FALSE-------------------------------------------------
# pm25_list_cached_files()
# pm25_clear_cache()

## ----utilities, eval = TRUE---------------------------------------------------
clean_station_name("Dr. Karni Singh Shooting Range, Delhi - DPCC")

get_aqi_categories(c(42, 180, 320))

# Mumbai to Delhi, in km
haversine_distance(19.0760, 72.8777, 28.7041, 77.1025)

