Learn R Programming

CopernicusMarine (version 0.4.9)

cms_write_ncdf: Store Typical Copernicus Marine Rasters Objects as NCDF

Description

[Experimental] The cms_download_subset() returns a stars class object. This is fine if you want to use it directly in R. But if you want to open it in external software, you need a more acceptable exchange format. You can use this function to store the stars object as a NetCDF file.

Usage

cms_write_ncdf(x, file, missval = -999, prec = "double", ...)

Value

Returns NULL invisibly.

Arguments

x

A stars class object created with cms_download_subset(). Note that this is not a highly generic NetCDF writer, so it makes certain assumptions about the stars object, that may not be true for all.

file

File path where to store the object.

missval

Value used to represent missing data (NA) in your object.

prec

Precision used for storing the data ("double" by default).

...

Ignored.

Details

Note that dimensions returned by cms_download_subset() have interval values, describing between what range the raster cell lies. When reading a file stored with cms_write_ncdf(), you must specify where from the file the bounds of the dimension should be read. Otherwise the GDAL driver will (often incorrectly) deduce dimension values. The example shows how to read the written file using stars::read_mdim().

The cms_write_ncdf() function is tailored to stars objects returned by cms_download_subset(). It might work on other stars objects, but it is not guaranteed.

Writing multidimensional data to a standardised format is the source of many headaches. The current implementation is not ideal, which is why it is currently experimental. Perhaps future GDAL release will have better support for writing higher dimension raster data. To be safe, you can also save your data as .rdata, or reduce the dimensions by storing specific slices.

See Also

Other supporting: cms_get_client_info(), cms_glossary(), cms_translate()

Examples

Run this code
if (interactive()) {
  ## Download some data to store
  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)
  )
  fn <- tempfile(fileext = ".nc")
  cms_write_ncdf(mydata, fn)
  mydata2 <- stars::read_mdim(
    fn,
    ## You explicitly need to specify dimension bounds when reading back the data
    bounds = c(
      longitude = "longitude_bnds",
      latitude  = "latitude_bnds",
      elevation = "elevation_bnds"))

  ## clean up after our selves
  unlink(fn, TRUE, TRUE)
}

Run the code above in your browser using DataLab