pkgname <- "CopernicusMarine"
source(file.path(R.home("share"), "R", "examples-header.R"))
options(warn = 1)
library('CopernicusMarine')

base::assign(".oldSearch", base::search(), pos = 'CheckExEnv')
base::assign(".old_wd", base::getwd(), pos = 'CheckExEnv')
cleanEx()
nameEx("account")
### * account

flush(stderr()); flush(stdout())

### Name: cms_get_username
### Title: Set or get Copernicus account details
### Aliases: cms_get_username cms_get_password cms_set_username
###   cms_set_password

### ** Examples

if (interactive()) {
  ## Returns your account details only if they have been set for your session
  cms_get_username()
  cms_get_password()
}



cleanEx()
nameEx("cms_cite_product")
### * cms_cite_product

flush(stderr()); flush(stdout())

### Name: cms_cite_product
### Title: How to cite a Copernicus marine product
### Aliases: cms_cite_product

### ** Examples

cms_cite_product("SST_MED_PHY_SUBSKIN_L4_NRT_010_036")



cleanEx()
nameEx("cms_download_native")
### * cms_download_native

flush(stderr()); flush(stdout())

### Name: cms_download_native
### Title: Download raw files as provided to Copernicus Marine
### Aliases: cms_download_native cms_list_native_files

### ** Examples

if (interactive()) {
  cms_list_native_files(
    product       = "GLOBAL_ANALYSISFORECAST_PHY_001_024",
    layer         = "cmems_mod_glo_phy_anfc_0.083deg_PT1H-m",
    prefix        = "2022/06/"
  )

## If you omit the prefix, you may want to limit the
## number of results by setting `max`
  cms_list_native_files(
    product       = "GLOBAL_ANALYSISFORECAST_PHY_001_024",
    layer         = "cmems_mod_glo_phy_anfc_0.083deg_PT1H-m",
    max           = 10
  )
  
## Prefix can be omitted when not relevant:
  cms_list_native_files(product = "SEALEVEL_GLO_PHY_MDT_008_063")
  
## Use 'pattern' to download a file for a specific day:
  cms_download_native(
    destination   = tempdir(),
    product       = "GLOBAL_ANALYSISFORECAST_PHY_001_024",
    layer         = "cmems_mod_glo_phy_anfc_0.083deg_PT1H-m",
    prefix        = "2022/06/",
    pattern       = "m_20220630"
  )
}



cleanEx()
nameEx("cms_download_subset")
### * cms_download_subset

flush(stderr()); flush(stdout())

### Name: cms_download_subset
### Title: Subset and download a specific marine product from Copernicus
### Aliases: cms_download_subset

### ** Examples

if (interactive()) {

  mydata <- cms_download_subset(
    product       = "GLOBAL_ANALYSISFORECAST_PHY_001_024",
    layer         = "cmems_mod_glo_phy-cur_anfc_0.083deg_P1D-m",
    variable      = c("uo", "vo"),
    region        = c(-1, 50, 10, 55),
    timerange     = c("2025-01-01 UTC", "2025-01-02 UTC"),
    verticalrange = c(0, -2)
  )

  plot(mydata["vo"])
} else {
  message("Make sure to run this in an interactive environment")
}



cleanEx()
nameEx("cms_get_client_info")
### * cms_get_client_info

flush(stderr()); flush(stdout())

### Name: cms_get_client_info
### Title: Get information about Copernicus Marine clients
### Aliases: cms_get_client_info

### ** Examples

if (interactive()) {
  cms_get_client_info()
}



cleanEx()
nameEx("cms_login")
### * cms_login

flush(stderr()); flush(stdout())

### Name: cms_login
### Title: Contact Copernicus Marine login page
### Aliases: cms_login

### ** Examples

if (interactive()) {
  cms_login()
}



cleanEx()
nameEx("cms_native_proxy")
### * cms_native_proxy

flush(stderr()); flush(stdout())

### Name: cms_native_proxy
### Title: Get a proxy stars object from a native service
### Aliases: cms_native_proxy

### ** Examples

if (interactive()) {
  native_proxy <-
    cms_native_proxy(
      product       = "GLOBAL_ANALYSISFORECAST_PHY_001_024",
      layer         = "cmems_mod_glo_phy_anfc_0.083deg_PT1H-m",
      prefix        = "2022/06/",
      pattern       = "20220621"
    )
  plot(native_proxy["zos", 1:1000, 1:500, 1, 1], axes = TRUE)
}



cleanEx()
nameEx("cms_product_details")
### * cms_product_details

flush(stderr()); flush(stdout())

### Name: cms_product_details
### Title: Obtain details for a specific Copernicus marine product
### Aliases: cms_product_details

### ** Examples

if (interactive()) {
  cms_product_details("GLOBAL_ANALYSISFORECAST_PHY_001_024")
}



cleanEx()
nameEx("cms_product_metadata")
### * cms_product_metadata

flush(stderr()); flush(stdout())

### Name: cms_product_metadata
### Title: Obtain product meta data
### Aliases: cms_product_metadata

### ** Examples

if (interactive()) {
  cms_product_metadata("GLOBAL_ANALYSISFORECAST_PHY_001_024")
}



cleanEx()
nameEx("cms_product_services")
### * cms_product_services

flush(stderr()); flush(stdout())

### Name: cms_product_services
### Title: Obtain available services for a specific Copernicus marine
###   product
### Aliases: cms_product_services

### ** Examples

if (interactive()) {
  cms_product_services("GLOBAL_ANALYSISFORECAST_PHY_001_024")
}



cleanEx()
nameEx("cms_products_list")
### * cms_products_list

flush(stderr()); flush(stdout())

### Name: cms_products_list
### Title: List products available from data.marine.copernicus.eu
### Aliases: cms_products_list cms_products_list2

### ** Examples

cms_products_list()

## Query a specific product:
cms_products_list(freeText = "GLOBAL_ANALYSISFORECAST_PHY_001_024")



cleanEx()
nameEx("cms_translate")
### * cms_translate

flush(stderr()); flush(stdout())

### Name: cms_translate
### Title: Translate Python code or command line request to R list
### Aliases: cms_translate

### ** Examples

python_code <-
"import copernicusmarine

copernicusmarine.subset(
  dataset_id=\"cmems_mod_glo_phy_anfc_0.083deg_PT1H-m\",
  variables=[\"uo\",\"vo\"],
  minimum_longitude=-2,
  maximum_longitude=8,
  minimum_latitude=52,
  maximum_latitude=59,
  start_datetime=\"2025-01-01T00:00:00\",
  end_datetime=\"2025-01-01T23:00:00\",
  minimum_depth=0.49402499198913574,
  maximum_depth=0.49402499198913574,
)"

cli_code <-
"copernicusmarine subset
  --dataset-id cmems_mod_glo_phy_anfc_0.083deg_PT1H-m
  --variable uo
  --variable vo
  --start-datetime 2025-01-01T00:00:00
  --end-datetime 2025-01-01T23:00:00
  --minimum-longitude -2
  --maximum-longitude 8
  --minimum-latitude 52
  --maximum-latitude 59
  --minimum-depth 0.49402499198913574
  --maximum-depth 0.49402499198913574"

if (interactive()) {
  cms_translate(python_code)
  cms_translate(cli_code)
  translated <- cms_translate(cli_code)
  do.call(cms_download_subset, translated)
}



cleanEx()
nameEx("cms_wmts")
### * cms_wmts

flush(stderr()); flush(stdout())

### Name: cms_wmts_details
### Title: Obtain a WMTS entry for specific Copernicus marine products and
###   add to a leaflet map
### Aliases: cms_wmts_details addCmsWMTSTiles cms_wmts_get_capabilities

### ** Examples

if (interactive()) {
  wmts_details <-
    cms_wmts_details(
      product  = "GLOBAL_ANALYSISFORECAST_PHY_001_024",
      layer    = "cmems_mod_glo_phy-thetao_anfc_0.083deg_P1D-m",
      variable = "thetao"
    )

  capabilities <-
    cms_wmts_get_capabilities("GLOBAL_ANALYSISFORECAST_PHY_001_024")

  if (nrow(wmts_details) > 0) {
    leaflet::leaflet() |>
      leaflet::setView(lng = 3, lat = 54, zoom = 4) |>
      leaflet::addProviderTiles("Esri.WorldImagery") |>
      addCmsWMTSTiles(
        product  = "GLOBAL_ANALYSISFORECAST_PHY_001_024",
        layer    = "cmems_mod_glo_phy-thetao_anfc_0.083deg_P1D-m",
        variable = "thetao")
  }
}



cleanEx()
nameEx("cms_zarr_proxy")
### * cms_zarr_proxy

flush(stderr()); flush(stdout())

### Name: cms_zarr_proxy
### Title: Get a proxy stars object from a Zarr service
### Aliases: cms_zarr_proxy

### ** Examples

if (interactive()) {
  myproxy <- cms_zarr_proxy(
    product       = "GLOBAL_ANALYSISFORECAST_PHY_001_024",
    layer         = "cmems_mod_glo_phy-cur_anfc_0.083deg_P1D-m",
    variable      = c("uo", "vo"),
    asset         = "timeChunked")
  plot(myproxy["uo",1:200,1:100,50,1], axes = TRUE)
}



### * <FOOTER>
###
cleanEx()
options(digits = 7L)
base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n")
grDevices::dev.off()
###
### Local variables: ***
### mode: outline-minor ***
### outline-regexp: "\\(> \\)?### [*]+" ***
### End: ***
quit('no')
