Skip to content

Base components

Base classes of the components and subcomponents.

rompy_swan.components.base

SWAN Components Base Module

This module provides the base classes for SWAN components in the ROMPY framework.

How to subclass: 1. Define a new model_type Literal for the subclass 2. Overwrite the cmd method to return the SWAN input file string

logger module-attribute

logger = get_logger(__name__)

MAX_LENGTH module-attribute

MAX_LENGTH = 180

SPACES module-attribute

SPACES = 4

BaseComponent pydantic-model

BaseComponent(**data: Any)

Bases: RompyBaseModel

Base class for SWAN components.

This class is not intended to be used directly, but to be subclassed by other SWAN components to implement the following common behaviour:

  • Define a render() method to render a CMD string from the component
  • Split the rendered CMD if longer than 132 characters
  • Forbid extra arguments so only implemented fields must be specified

Fields:

model_type pydantic-field

model_type: Literal['component']

Model type discriminator

cmd abstractmethod

cmd() -> str | list

Return the string or list of strings to render the component to the CMD.

render

render(cmd: Optional[str | list] = None) -> str

Render the component to a string.

Parameters:

Name Type Description Default
cmd Optional[str | list]

Command string or list of command strings to render, by default self.cmd().

None

Returns:

Name Type Description
cdmstr str

The rendered command file component.

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.

MultiComponents pydantic-model

MultiComponents(**data: Any)

Bases: BaseComponent

Mixin for components that can have multiple components.

Fields:

components pydantic-field

components: list

The components to render

model_type pydantic-field

model_type: Literal['component']

Model type discriminator

cmd

cmd() -> list[str]

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.

render

render(cmd: Optional[str | list] = None) -> str

Render the component to a string.

Parameters:

Name Type Description Default
cmd Optional[str | list]

Command string or list of command strings to render, by default self.cmd().

None

Returns:

Name Type Description
cdmstr str

The rendered command file component.

split_string

split_string(
    cmd: str, max_length: int = MAX_LENGTH, spaces: int = 4
) -> list

Split command cmd if longer than max_length.

Parameters:

Name Type Description Default
cmd str

SWAN CMD string to split.

required
max_length int

Maximum length of each split string.

MAX_LENGTH
spaces int

Number of spaces in each identation before each new line.

4

Returns:

Name Type Description
split_cmd list

List of split CMD strings.

rompy_swan.subcomponents.base

SWAN Subcomponents Base Module

This module provides the base classes for SWAN subcomponents in the ROMPY framework.

logger module-attribute

logger = get_logger(__name__)

BaseSubComponent pydantic-model

BaseSubComponent(**data: Any)

Bases: RompyBaseModel, ABC

Base class for SWAN sub-components.

This class is not intended to be used directly, but to be subclassed by other SWAN sub-components to implement the following common behaviour:

  • Define a render() method to render a CMD string from the subcomponent
  • Forbid extra arguments so only implemented fields must be specified

Fields:

model_type pydantic-field

model_type: Literal['subcomponent']

Model type discriminator

cmd

cmd() -> str

render

render() -> str

Render the sub-component to a string.

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.

XY pydantic-model

XY(**data: Any)

Bases: BaseSubComponent

Points in problem coordinates.

< [x] [y] >
Note

Coordinates should be given in m when Cartesian coordinates are used or degrees when Spherical coordinates are used (see command COORD).

Examples:

from rompy_swan.subcomponents.base import XY
points = XY(
    x=[172, 172, 172, 172.5, 173],
    y=[-41, -40.5, -40, -40, -40],
    fmt="0.2f",
)
print(points.render())
172.00 -41.00
172.00 -40.50
172.00 -40.00
172.50 -40.00
173.00 -40.00

Fields:

Validators:

model_type pydantic-field

model_type: Literal['xy', 'XY'] = 'xy'

Model type discriminator

x pydantic-field

x: list[float]

Problem x-coordinate values

y pydantic-field

y: list[float]

Problem y-coordinate values

fmt pydantic-field

fmt: str = '0.8f'

The format to render floats values

size property

size

validate_size pydantic-validator

validate_size() -> XY

cmd

cmd() -> str

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.

render

render() -> str

Render the sub-component to a string.

IJ pydantic-model

IJ(**data: Any)

Bases: BaseSubComponent

Points in grid indices coordinates.

< [i] [j] >

Grid indices are 0-based and must be in the range 0 ≤ i ≤ mx and 0 ≤ j ≤ my where mx and my are the number of cells in each direction as defined by the CGRID command.

Note

A value of -1 may be used as a sentinel for the last grid index: -1 in the i list is replaced by mx, and -1 in the j list is replaced by my. The substitution is performed automatically by :meth:SwanConfig.resolve_segment_ij_sentinels when the IJ object is used as the points of a BOUNDSPEC SEGMENT location.

Warning

SWAN terminates its SEGMENT IJ reading loop when it encounters a negative i value, so -1 cannot be passed literally to SWAN — it must be resolved to the actual grid index before the INPUT file is written.

Examples:

from rompy_swan.subcomponents.base import IJ
points = IJ(i=[0, 0, 5], j=[0, 19, 19])
print(points.render())
i=0 j=0
i=0 j=19
i=5 j=19

Fields:

Validators:

model_type pydantic-field

model_type: Literal['ij', 'IJ'] = 'ij'

Model type discriminator

i pydantic-field

i: list[int]

i-index values

j pydantic-field

j: list[int]

j-index values

size property

size

validate_size pydantic-validator

validate_size() -> IJ

cmd

cmd() -> str

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.

render

render() -> str

Render the sub-component to a string.