parsac.util module

class parsac.util.NcDict(file: PathLike[str] | str)

Bases: Mapping[str, ndarray]

A dictionary-like interface to a NetCDF file.

The file is opened when the object is created. It closed when the object is finalized (when used within a with statement) or when finalize() is called.

Parameters:

file – Path to the NetCDF file.

eval(expression: str | CodeType, no_trailing_singletons: bool = True) ndarray
finalize() None

Close the NetCDF file.

class parsac.util.TextFormat(*values)

Bases: Enum

Formats of text-based files with observations.

DEPTH_EXPLICIT = 0

A whitespace-separated text file with the following columns:

  • date + time, for instance 2023-10-01 12:00:00

  • depth (negative, below surface)

  • variable value

  • standard deviation (optional)

DEPTH_INDEPENDENT = 1

A whitespace-separated text file with the following columns:

  • date + time, for instance 2023-10-01 12:00:00

  • variable value

  • standard deviation (optional)

GOTM_PROFILES = 2

A text file in GOTM profile format.

class parsac.util.YAMLFile(file: PathLike[str] | str)

Bases: object

A dictionary-like interface to a YAML file. The file is parsed when the object is created. The data can subsequently be accessed using the [] operator, for instance value = data["key/subkey"] or data["key/subkey"] = new_value.

Parameters:

file – Path to the YAML file.

save() None

Save the current data to the YAML file.

parsac.util.backup_file(file: Path) Path

Create a backup of a file by copying it to a new file with a .bck suffix.

Parameters:

file – Path to the file to backup.

Returns:

Path to the backup file.

parsac.util.copy_directory(src_dir: Path, dst_dir: Path, *, exclude_files: Iterable[str] = (), exclude_dirs: Iterable[str] = (), symlink: bool = False, logger: Logger | None = None) None

Copy files from src_dir to dst_dir, excluding files and directories that match the patterns in exclude_files and exclude_dirs, respectively.

Parameters:
  • src_dir – source directory.

  • dst_dir – destination directory.

  • exclude_files – patterns for files to exclude.

  • exclude_dirs – patterns for directories to exclude.

  • symlink – whether to create symlinks instead of copying files.

  • logger – Logger to use for diagnostic messages.

parsac.util.filter_by_time(values: ndarray, time: ndarray, time_units: str, months: Iterable[int] = ()) ndarray
parsac.util.readVariableFromNcFile(file: Path, expression: str, *, depth_expression: str | None, logger: Logger, mindepth: float = -inf, maxdepth: float = inf, time_name: str = 'time') tuple[Sequence[datetime], ndarray | None, ndarray]

Read a variable from a NetCDF file.

Parameters:
  • file – Path to the NetCDF file.

  • expression – Expression to evaluate.

  • depth_expression – Expression to evaluate for depth.

  • logger – Logger to use for diagnostic messages.

  • mindepth – Minimum depth to include.

  • maxdepth – Maximum depth to include.

  • time_name – Name of the time variable.

Returns:

Tuple of time, depth, and values.

parsac.util.readVariableFromTextFile(file: Path, *, format: TextFormat, logger: Logger, mindepth: float = -inf, maxdepth: float = inf) tuple[Sequence[datetime], ndarray | None, ndarray, ndarray | None]

Read a variable from a text file.

Parameters:
  • file – Path to the text file.

  • format – Format of the text file.

  • logger – Logger to use for diagnostic messages.

  • mindepth – Minimum depth to include.

  • maxdepth – Maximum depth to include.

Returns:

Tuple of time, depth, values, and standard deviations

parsac.util.run_program(executable: PathLike[str] | str, rundir: Path, *, logger: Logger, use_shell: bool = False, show_output: bool = True, args: Iterable[str] = ())

Run a program in a subprocess.

Parameters:
  • executable – Path to the executable.

  • rundir – Directory in which to run the program.

  • logger – Logger to use for diagnostic messages.

  • use_shell – Whether to use a shell to run the program.

  • show_output – Whether to show the output of the program.

Returns:

The return code of the program.