agrifoodpy.utils.nodes

Functions

add_items(dataset, items[, values, copy_from, datablock])

Adds a list of items to a selected dataset in the datablock and

add_years(dataset, years[, projection, datablock])

Extends the Year coordinates of a dataset.

copy_datablock(datablock, key, out_key)

Copy a datablock element into a new key in the datablock

print_datablock(datablock, key[, attr, method, args, ...])

Prints a datablock element or its attributes/methods at any point in the

write_to_datablock(datablock, key, value[, overwrite])

Writes a value to a specified key in the datablock.

load_dataset(datablock_path[, path, module, ...])

Loads a dataset to the specified datablock dictionary.

Module Contents

agrifoodpy.utils.nodes.add_items(dataset, items, values=None, copy_from=None, datablock=None)

Adds a list of items to a selected dataset in the datablock and initializes their values.

Parameters:
  • datablock (Datablock) – Datablock object.

  • dataset (dict) – Datablock path to the datasets to modify.

  • items (list, dict) – List of items or dictionary of items attributes to add. If a dictionary, keys are the item names, and non-dimension coordinates.

  • values (list, optional) – List of values to initialize the items. If not set, values are set to 0, unless the copy from parameter is set.

  • copy_from (list, optional) – Items to copy the values from.

Returns:

  • If no datablock is provided, returns a xarray.Dataset with the new items.

  • If a datablock is provided, returns the datablock with the modified datasets on the corresponding keys.

Return type:

dict or xarray.Dataset

agrifoodpy.utils.nodes.add_years(dataset, years, projection='empty', datablock=None)

Extends the Year coordinates of a dataset.

Parameters:
  • datablock (dict) – The datablock dictionary where the dataset is stored.

  • dataset (str) – Datablock key of the dataset to extend.

  • years (list) – List of years to extend the dataset to.

  • projection (str) – Projection mode. If “constant”, the last year of the input array is copied to every new year. If “empty”, values are initialized and set to zero. If a float array is given, these are used to populate the new year using a scaling of the last year of the array

agrifoodpy.utils.nodes.copy_datablock(datablock, key, out_key)

Copy a datablock element into a new key in the datablock

Parameters:
  • datablock (xarray.Dataset) – The datablock to print

  • key (str) – The key of the datablock to print

  • out_key (str) – The key of the datablock to copy to

Returns:

datablock – Datablock to with added key

Return type:

dict

agrifoodpy.utils.nodes.print_datablock(datablock, key, attr=None, method=None, args=None, kwargs=None, preffix='', suffix='')

Prints a datablock element or its attributes/methods at any point in the pipeline execution.

Parameters:
  • datablock (dict) – The datablock to print from.

  • key (str) – The key of the datablock to print.

  • attr (str, optional) – Name of an attribute of the object to print.

  • method (str, optional) – Name of a method of the object to call and print.

  • args (list, optional) – Positional arguments for the method call.

  • kwargs (dict, optional) – Keyword arguments for the method call.

Returns:

datablock – Unmodified datablock to continue execution.

Return type:

dict

agrifoodpy.utils.nodes.write_to_datablock(datablock, key, value, overwrite=True)

Writes a value to a specified key in the datablock.

Parameters:
  • datablock (dict) – The datablock to write to.

  • key (str) – The key in the datablock where the value will be written.

  • value (any) – The value to write to the datablock.

  • overwrite (bool, optional) – If True, overwrite the existing value at the key. If False, do not overwrite.

Returns:

datablock – The updated datablock with the new key-value pair.

Return type:

dict

agrifoodpy.utils.nodes.load_dataset(datablock_path, path=None, module=None, data_attr=None, da=None, coords=None, scale=1.0, datablock=None)

Loads a dataset to the specified datablock dictionary.

Parameters:
  • datablock (dict) – The datablock path where the dataset is stored

  • path (str) – The path to the dataset stored in a netCDF file.

  • module (str) – The module name where the dataset will be imported from.

  • data_attr (str) – The attribute name of the dataset in the module.

  • da (str) – The dataarray to be loaded.

  • coords (dict) – Dictionary containing the coordinates of the dataset to be loaded.

  • scale (float) – Optional multiplicative factor to be applied to the dataset on load.

  • datablock_path (str) – The path to the datablock where the dataset is stored.