dep_tools.writers module

This module contains the definition and various implementations of Writer objects, which are used to write files locally or to the cloud.

class dep_tools.writers.Writer[source]

Bases: ABC

The base abstract class for writing.

abstract write(*args, **kwargs)[source]
Return type:

str | list[str]

class dep_tools.writers.DsCogWriter(itempath, write_multithreaded=False, load_before_write=False, write_function=<function write_to_s3>, **kwargs)[source]

Bases: Writer

A Writer object which writes an xarray.Dataset to a cloud-optimized GeoTIFF (COG) file or files. A COG is written for each Dataset variable.

Parameters:
  • itempath (ItemPath) – The ItemPath used to define the output path.

  • write_multithreaded (bool) – Whether to use multiple threads (one for each Dataset variable) when writing.

  • load_before_write (bool) – Whether to load the Dataset before writing. This can be useful to prevent re-reading source data from when multiple variables depend on the same source. data.

  • write_function (Callable) – The function actually used to write the data. It should take an xarray.DataArray as the first parameter and the output path (as a string or Path) as a named parameter.

  • **kwargs – Additional arguments to write_function().

write(xr, item_id)[source]

Write a xarray.Dataset.

Parameters:
  • xr (Dataset) – The Dataset.

  • item_id (str) – An item id. It is passed to the ItemPath provided on initialization to determine the output file path.

Return type:

Union[str, List]

Returns: The output path(s) as a string or list of strings.

class dep_tools.writers.AwsDsCogWriter(itempath, write_multithreaded=False, load_before_write=False, write_function=<function write_to_s3>, **kwargs)[source]

Bases: DsCogWriter

A DsCogWriter which writes to S3 storage. The only difference is that the ItemPath is an S3ItemPath; itempath.bucket is passed as the bucket keyword argument to write_function.

class dep_tools.writers.LocalDsCogWriter(**kwargs)[source]

Bases: DsCogWriter

A DsCogWriter which writes to local storage using dep_tools.utils.write_to_local_storage().

A Writer object which writes an xarray.Dataset to a cloud-optimized GeoTIFF (COG) file or files. A COG is written for each Dataset variable.

Parameters:
  • itempath – The ItemPath used to define the output path.

  • write_multithreaded – Whether to use multiple threads (one for each Dataset variable) when writing.

  • load_before_write – Whether to load the Dataset before writing. This can be useful to prevent re-reading source data from when multiple variables depend on the same source. data.

  • write_function – The function actually used to write the data. It should take an xarray.DataArray as the first parameter and the output path (as a string or Path) as a named parameter.

  • **kwargs – Additional arguments to write_function().

class dep_tools.writers.StacWriter(itempath, write_stac_function=<function write_stac_s3>, **kwargs)[source]

Bases: Writer

A Write which writes spatio-temporal asset catalog (STAC) Items.

Parameters:
  • itempath (GenericItemPath) – The ItemPath used to define the output path.

  • write_stac_function (Callable) – The function used to write a STAC item. It should take the Item as its first argument and the output path as its second.

  • **kwargs – Additional arguments to write_stac_function().

write(item, item_id)[source]

Write a STAC Item to an output file.

Parameters:
  • item (Item) – The STAC Item.

  • item_id (str) – The item id, passed to ItemPath.stac_path() to define the output path.

Return type:

str

Returns: The self reference of the item (item.self_href).

class dep_tools.writers.AwsStacWriter(itempath, **kwargs)[source]

Bases: StacWriter

A StacWriter to write to Amazon S3 Storage.

Parameters:
  • itempath (S3ItemPath) – The itempath used to define the output path. itempath.bucket is passed as the bucket keyword argument to dep_tools.aws.write_to_s3().

  • **kwargs – Additional arguments to StacWriter.__init__().

class dep_tools.writers.LocalStacWriter(itempath, **kwargs)[source]

Bases: StacWriter

A StacWriter to write to a local file.

This class uses dep_tools.utils.write_to_local_storage() to write the data. It is typically used for testing only.

Parameters:
  • itempath (GenericItemPath) – The itempath used to define the output path.

  • **kwargs – Additional arguments to StacWriter.__init__().