sedtrails.data_manager.netcdf_writer

Module Contents

Classes

NetCDFWriter

A class for writing NetCDF files for the SedTrails Particle Tracer System using xarray.

API

class sedtrails.data_manager.netcdf_writer.NetCDFWriter(output_dir)

A class for writing NetCDF files for the SedTrails Particle Tracer System using xarray.

This class provides methods to: - Create xarray datasets with SedTrails structure - Add metadata to datasets (populations, flow fields, simulation info) - Write xarray datasets to NetCDF files with optional timestep trimming

# Method 1: Step-by-step approach output_dir = “some/path” writer = NetCDFWriter(output_dir)

# Create the xarray dataset dataset = writer.create_dataset( N_particles=total_particles, N_populations=n_populations, N_timesteps=n_timesteps, N_flowfields=n_flowfields )

# Populate metadata to the dataset writer.add_metadata(dataset, populations, flow_field_names)

# Write the dataset to a netcdf file writer.write(dataset, filename)

# Optional: Write with timestep trimming writer.write(dataset, filename, trim_to_actual_timesteps=True, actual_timesteps=100)

# Method 2: All-in-one approach writer.create_and_write_simulation_results( populations, flow_field_names, n_timesteps, filename )

output_dirpathlib.Path

The directory where output files (NetCDF, images, etc.) are stored.

Initialization

_validate_filename(filename)

Validates the file name to ensure it is a NetCDF file.

write(xr_dataset, filename, trim_to_actual_timesteps=False, actual_timesteps=None)

Write an xr.Dataset to a NetCDF file in the output directory.

xr_datasetxr.Dataset

The xarray dataset to write.

filenamestr

The name of the NetCDF file to write (should end with .nc).

trim_to_actual_timestepsbool, optional

Whether to trim the dataset to actual timesteps used (default: False)

actual_timestepsint, optional

Number of actual timesteps to keep (if None, tries to determine automatically)

pathlib.Path

Path to the written file

create_dataset(N_particles, N_populations, N_timesteps, N_flowfields, name_strlen=24)

Create an xarray dataset with the SedTrails structure.

N_particlesint

Number of particles

N_populationsint

Number of populations

N_timestepsint

Number of timesteps

N_flowfieldsint

Number of flow fields

name_strlenint, optional

Maximum length for string variables (default: 24)

xr.Dataset

The created xarray dataset

add_metadata(dataset, populations, flow_field_names, simulation_metadata=None)

Add metadata to the xarray dataset.

datasetxr.Dataset

The dataset to add metadata to

populationslist

List of population objects from the simulation

flow_field_nameslist

List of flow field names used in the simulation

simulation_metadatadict, optional

Additional simulation metadata to include as global attributes

xr.Dataset

The dataset with metadata added (modifies in place and returns)

create_and_write_simulation_results(populations, flow_field_names, N_timesteps, filename='simulation_results.nc', simulation_metadata=None, name_strlen=24)

Convenience method that creates dataset, adds metadata, and writes to file in one call.

populationslist

List of population objects from the simulation

flow_field_nameslist

List of flow field names used in the simulation

N_timestepsint

Number of timesteps in the simulation

filenamestr, optional

The name of the NetCDF file to write (default: ‘simulation_results.nc’)

simulation_metadatadict, optional

Additional simulation metadata to include as global attributes

name_strlenint, optional

Maximum length for string variables (default: 24)

pathlib.Path

Path to the written file