---
title: "Download original assets"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Download original assets}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4)
helper <- if (file.exists("vignettes/real-example-helpers.R")) {
  "vignettes/real-example-helpers.R"
} else {
  "real-example-helpers.R"
}
source(helper)
real_mode <- bt_real_examples_enabled()
```

`r if (real_mode) bt_real_example_label else bt_offline_note`

This example uses BlueTopo tiles covering New York Harbor. The workflow
demonstrates tile discovery, checksum-verified asset retrieval, and file-backed
raster access with `terra`. Raster Attribute Table (RAT) sidecars are retained
with the source GeoTIFF assets.

## Example area

```{r setup, include=FALSE, eval=bt_real_examples_enabled()}
real <- bt_real_example_setup()
real_aoi <- real$aoi
```

## First download

```{r download, eval=bt_real_examples_enabled()}
manifest <- bluertopo_download(
  real_aoi,
  path = real$download_dir,
  resolution = "native",
  coverage = "fill",
  rat = TRUE,
  verify = "sha256",
  on_exists = "verify",
  progress = FALSE,
  quiet = TRUE
)
```

```{r manifest-table, echo=FALSE, eval=bt_real_examples_enabled()}
bt_display_table(bt_manifest_table(manifest))
```

## Reuse existing verified files

```{r reuse, echo=FALSE, eval=bt_real_examples_enabled()}
reused <- bluertopo_download(
  real_aoi,
  path = real$download_dir,
  resolution = "native",
  coverage = "fill",
  rat = TRUE,
  verify = "sha256",
  on_exists = "verify",
  progress = FALSE,
  quiet = TRUE
)

reuse_df <- as.data.frame(reused)
reuse_summary <- aggregate(
  verified ~ status,
  data = reuse_df,
  FUN = function(x) sum(x %in% TRUE)
)
names(reuse_summary) <- c("status", "verified_count")
reuse_summary$count <- as.integer(table(reuse_df$status)[reuse_summary$status])
reuse_summary <- reuse_summary[c("status", "count", "verified_count")]

bt_display_table(reuse_summary)
```

## Manifest files

```{r manifest-files, echo=FALSE, eval=bt_real_examples_enabled()}
bt_display_table(bt_manifest_files_table(real$download_dir))
```

## Source domain summary

```{r source-domain-summary, echo=FALSE, eval=bt_real_examples_enabled()}
bt_display_table(bt_source_domain_summary(manifest))
```

## Download status counts

```{r status-bars, echo=FALSE, eval=bt_real_examples_enabled(), fig.cap="SHA-256 verified download status counts for the selected BlueTopo assets.", fig.alt="Bar plot of downloaded and reused BlueTopo asset statuses."}
status_counts <- bt_status_counts(manifest)
barplot(
  stats::setNames(status_counts$count, status_counts$status),
  col = "#005f73",
  ylab = "asset count",
  main = "First-call download statuses"
)
```

The second call reports `reused_verified` for files that already exist and still
match NOAA SHA-256 checksums. RAT sidecars are downloaded and verified with the
GeoTIFF assets.
