## ----include=FALSE------------------------------------------------------------
shark_available <- tryCatch({
  httr::status_code(httr::GET("https://shark.smhi.se/",
                               httr::timeout(5))) < 400
}, error = function(e) FALSE)

obis_available <- tryCatch({
  httr::status_code(httr::GET("https://api.obis.org/",
                               httr::timeout(5))) < 400
}, error = function(e) FALSE)

## ----eval=FALSE---------------------------------------------------------------
# install.packages("SHARK4R")

## -----------------------------------------------------------------------------
library(SHARK4R)

## ----include=FALSE, eval=shark_available--------------------------------------
shark_options <- try(get_shark_options(), silent = TRUE)
if (inherits(shark_options, "try-error")) shark_available <- FALSE

## ----eval=FALSE---------------------------------------------------------------
# shark_options <- get_shark_options()

## ----include=FALSE, eval=shark_available--------------------------------------
chlorophyll_datasets <- shark_options$datasets[grepl("Chlorophyll",
                                                     shark_options$datasets)]
selected_dataset <- chlorophyll_datasets[1]

## ----eval=FALSE---------------------------------------------------------------
# # Filter names using grepl
# chlorophyll_datasets <- shark_options$datasets[grepl("Chlorophyll",
#                                                      shark_options$datasets)]
# 
# # Select the first dataset for demonstration
# selected_dataset <- chlorophyll_datasets[1]
# 
# # Print the name of the selected dataset
# print(selected_dataset)

## ----echo=FALSE, eval=shark_available-----------------------------------------
print(selected_dataset)

## ----include=FALSE, eval=shark_available--------------------------------------
chlorophyll_data <- try(
  get_shark_datasets(selected_dataset, save_dir = tempdir(),
                     return_df = TRUE, verbose = FALSE),
  silent = TRUE
)
if (inherits(chlorophyll_data, "try-error")) shark_available <- FALSE

## ----eval=FALSE---------------------------------------------------------------
# chlorophyll_data <- get_shark_datasets(selected_dataset,
#                                        save_dir = tempdir(),
#                                        return_df = TRUE,
#                                        verbose = FALSE)
# 
# print(chlorophyll_data)

## ----echo=FALSE, eval=shark_available-----------------------------------------
print(chlorophyll_data)

## ----include=FALSE, eval=shark_available--------------------------------------
check_fields_result <- try(
  check_fields(data = chlorophyll_data, datatype = "Chlorophyll"),
  silent = TRUE
)

## ----eval=FALSE---------------------------------------------------------------
# check_fields(data = chlorophyll_data, datatype = "Chlorophyll")

## ----echo=FALSE, eval=shark_available && !inherits(check_fields_result, "try-error")----
check_fields_result

## ----include=FALSE, eval=shark_available--------------------------------------
check_codes_result1 <- try(check_codes(chlorophyll_data), silent = TRUE)
check_codes_result2 <- try(
  check_codes(data = chlorophyll_data, field = "platform_code",
              code_type = "SHIPC", match_column = "Code"),
  silent = TRUE
)

## ----eval=FALSE---------------------------------------------------------------
# # Validate project codes
# check_codes(chlorophyll_data)
# 
# # Validate ship/platform codes
# check_codes(data = chlorophyll_data,
#             field = "platform_code",
#             code_type = "SHIPC",
#             match_column = "Code")

## ----echo=FALSE, eval=shark_available && !inherits(check_codes_result1, "try-error")----
check_codes_result1

## ----echo=FALSE, eval=shark_available && !inherits(check_codes_result2, "try-error")----
check_codes_result2

## ----eval=shark_available && Sys.getenv("NOT_CRAN", unset = "FALSE") == "TRUE"----
# plot_map_leaflet(chlorophyll_data)

## ----echo=FALSE, eval=!Sys.getenv("NOT_CRAN", unset = "FALSE") == "TRUE"------
message("The interactive map is omitted here but appears in the online tutorial.")

## ----include=FALSE, eval=shark_available && obis_available--------------------
n_rows_on_land <- try(check_onland(chlorophyll_data), silent = TRUE)
onland_ok <- !inherits(n_rows_on_land, "try-error")

## ----eval=FALSE---------------------------------------------------------------
# n_rows_on_land <- check_onland(chlorophyll_data)
# nrow(n_rows_on_land)

## ----echo=FALSE, eval=shark_available && obis_available && onland_ok----------
nrow(n_rows_on_land)

## ----include=FALSE, eval=shark_available && obis_available--------------------
depth_result1 <- try(check_depth(data = chlorophyll_data), silent = TRUE)
depth_result2 <- try(
  check_depth(data = chlorophyll_data, "water_depth_m"),
  silent = TRUE
)
depth_ok <- !inherits(depth_result1, "try-error")

## ----eval=FALSE---------------------------------------------------------------
# check_depth(data = chlorophyll_data) # default columns: min/max depth
# check_depth(data = chlorophyll_data, "water_depth_m")

## ----echo=FALSE, eval=shark_available && obis_available && depth_ok-----------
depth_result1
depth_result2

## ----include=FALSE, eval=shark_available--------------------------------------
shark_statistics <- try(
  get_shark_statistics(datatype = "Chlorophyll", fromYear = 2020,
                       toYear = 2024, verbose = FALSE),
  silent = TRUE
)
stats_ok <- !inherits(shark_statistics, "try-error")

## ----eval=FALSE---------------------------------------------------------------
# shark_statistics <- get_shark_statistics(datatype = "Chlorophyll",
#                                          fromYear = 2020,
#                                          toYear = 2024,
#                                          verbose = FALSE)
# 
# print(shark_statistics)

## ----echo=FALSE, eval=shark_available && stats_ok-----------------------------
print(shark_statistics)

## ----eval=FALSE---------------------------------------------------------------
# check_outliers(data = chlorophyll_data,
#                parameter = "Chlorophyll-a",
#                datatype = "Chlorophyll",
#                threshold_col = "P99",
#                thresholds = shark_statistics)

## ----echo=FALSE, eval=shark_available && stats_ok-----------------------------
check_outliers(data = chlorophyll_data,
               parameter = "Chlorophyll-a",
               datatype = "Chlorophyll",
               threshold_col = "P99",
               thresholds = shark_statistics)

## ----eval=shark_available && stats_ok && Sys.getenv("NOT_CRAN", unset = "FALSE") == "TRUE"----
# # Scatterplot with horizontal line at 99th percentile
# scatterplot(chlorophyll_data,
#             hline = shark_statistics$P99)

## ----echo=FALSE, eval=!Sys.getenv("NOT_CRAN", unset = "FALSE") == "TRUE"------
message("The interactive plot is omitted here but appears in the online tutorial.")

## ----eval=FALSE---------------------------------------------------------------
# check_parameter_rules(data = chlorophyll_data)

## ----echo=FALSE, eval=shark_available-----------------------------------------
check_parameter_rules(data = chlorophyll_data)

## ----eval=FALSE---------------------------------------------------------------
# station_match <- match_station(chlorophyll_data$station_name)
# head(station_match)

## ----echo=FALSE, eval=shark_available-----------------------------------------
station_match <- match_station(chlorophyll_data$station_name)
head(station_match)

## ----eval=shark_available && Sys.getenv("NOT_CRAN", unset = "FALSE") == "TRUE"----
# check_station_distance(data = chlorophyll_data,
#                        plot_leaflet = TRUE)

## ----echo=FALSE, eval=!Sys.getenv("NOT_CRAN", unset = "FALSE") == "TRUE"------
message("The interactive map is omitted here but appears in the online tutorial.")

## ----eval=FALSE---------------------------------------------------------------
# check_nominal_station(data = chlorophyll_data)

## ----echo=FALSE, eval=shark_available-----------------------------------------
check_nominal_station(data = chlorophyll_data)

## ----eval=FALSE---------------------------------------------------------------
# # Run the app
# run_qc_app()
# 
# # Alternative, download support files and knit documents locally
# check_setup(path = tempdir()) # using a temp folder in this example

## ----echo=FALSE---------------------------------------------------------------
citation("SHARK4R")

## ----echo=FALSE---------------------------------------------------------------
clean_shark4r_cache(0, clear_perm_cache = TRUE, verbose = FALSE)

