aggregate_geozarr: Aggregate multiple CF data variables into a single GeoZarr...

View source: R/aggregate.R

aggregate_geozarrR Documentation

Aggregate multiple CF data variables into a single GeoZarr array

Description

NetCDF files have file size limitations, leading to large data sets being split up into multiple files, usually over the temporal dimension. This is very typical in climate projection data sets: a typical CMIP6 run over the period 2015-2100 at daily resolution is broken into 3 to 16 separate files for each variable, making data extractions across the temporal boundaries cumbersome. Zarr does not have that same limitation: large data sets are broken up into "chunks", each being stored as a file when the store is on a local file system, but this is transparent to the user of the data. A full CMIP6 run of any variable can be stored and manipulated in a single GeoZarr array.

Usage

aggregate_geozarr(
  x,
  location,
  along = NULL,
  var = NULL,
  name = NULL,
  chunking = NULL,
  external_coordinates = NULL,
  verbose = interactive()
)

Arguments

x

Either a character vector with fully-qualified paths to netCDF files, or a list of CFVariable instances, to aggregate.

location

A zarr_group in which the array is created, or a zarr object whose root node is a zarr_group. The array must not already exist.

along

Optional. Character vector with the names of the axes to aggregate along. If omitted or NULL (the default), the time axis of the first data variable is used.

var

Optional. When x is a character vector, the name of the data variable to read from each file. If omitted or NULL (the default), each file must have a single data variable.

name

Optional. The name of the array to create. If omitted or NULL (the default), the name of the first data variable is used.

chunking

Optional. Named vector or list, with numeric values giving the relative weights of each axis in the chunking - higher values favour data extraction over that axis. The names correspond to the names of the axes of the data variables, any non-named axes will be assigned a value of 1. If omitted or NULL (default), all axes will have equal weight in the chunking.

external_coordinates

Optional. Character string with the path of a group, relative to location, where the arrays with external coordinates are stored: coordinate values and boundaries that are not regular and too long to be stored in the array metadata, label sets and the arrays of an auxiliary longitude-latitude grid. The group is created if it does not exist. If omitted or NULL (the default), these arrays are stored in location, next to the new array. The path may go up the hierarchy with .., e.g. "../shared" for a group next to location, but not beyond the root group of the Zarr store, which is an error.

verbose

Logical. If TRUE, progress messages are printed to the console: the files being opened, the array that is created, and the progress of copying the data of each data variable, per chunk written. Default is TRUE in an interactive session, FALSE otherwise.

Details

This function makes a best effort. If the data variables provided are sensible, so will be the result. Otherwise, an error is thrown.

The data variables must have the same axes, by name and in the same order. Along the axes named in along the coordinates of the data variables are combined, in the direction of the axis; any duplicate coordinates are included once. These axes may also be scalar axes of the data variables, such as a height or an ensemble member, which then become new dimensions of the array. All other axes have to be identical in all data variables, including their boundary values. The coordinates of the data variables have to be contiguous subsets of the aggregated axis.

The following conditions throw an error:

  • the data variables do not have the same axes, by name and in the same order;

  • an axis is not of the same type or, for time axes, does not have the same calendar, unit and origin in all data variables;

  • the coordinates of an axis in along are partly increasing and partly decreasing, or have gaps or interleave;

  • a discrete axis is named in along;

  • the data variables have different data types, or not all have the same CRS;

  • only some data variables have an auxiliary longitude-latitude grid, the grids differ, or, with such a grid, the X or Y axes are in along;

  • label sets of an axis that is not in along differ among the data variables, or labels for the same coordinate differ;

  • One or more of the data variables has parametric vertical coordinates.

Attributes of the axes are set on the coordinates of the axes, leaving out those that are structural in GeoZarr, such as axis, bounds and units. Global attributes, those of the group of the data variables, are set on the location group, overwriting attributes with the same name that are already there. As no single value describes the aggregate when it differs among the files, such as for a creation date or a tracking id, only the global attributes that have the same value in all data variables are set. The exception is the history attribute, which is set as an array of entries, most recent first: an entry for the aggregation, followed by the entries of each distinct history among the data variables.

Attributes of the data variables are set on the array. If they differ among the data variables, or are not in all of them, a warning is issued and the value from the last data variable is used. This also holds for the attributes of an axis. Missing data are translated to the GeoZarr conventions, see the "Exporting data" vignette for details.

Label sets of the axes are included. Any axis can be aggregated along, including the X and Y axes, except when the data variables have an auxiliary longitude-latitude grid. That grid is set as the geolocation of the array and it is defined over the X and Y axes, so these cannot be in along. The grid, and the axes it is defined over, must be identical in all data variables. If the axes of the grid have no orientation, as is common for curvilinear grids, the first is taken to be the X axis and the second the Y axis.

Limitations: parametric vertical coordinates (CF formula terms) are not supported.

Value

The new geozarr_array, invisibly.

Examples

## Not run: 
pr <- list.files(pattern = '^pr.*\\.nc$', full.names = TRUE)
z <- zarr::create_zarr()
gza <- aggregate_geozarr(x = pr, location = z)

## End(Not run)

ncdfCF documentation built on Nov. 24, 2026, 5:07 p.m.