Skip to content

Grid reader

Sub-commands to support file reading. These are in technically components in that they render a full command line however they are not intended to be used directly but rather as sub-components of CGRID and INPGRID commands.

GRIDREGULAR

Bases: BaseSubComponent

SWAN Regular Grid subcomponent.

.. code-block:: text

xp yp alp xlen ylen mx my

Note

The direction of the x-axis alp must be 0 in case of spherical coordinates

Note

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

Examples

.. ipython:: python :okwarning:

from rompy_swan.subcomponents.readgrid import GRIDREGULAR
kwargs = dict(xp=173, yp=-40, alp=0, xlen=2, ylen=2, mx=199, my=199)
grid = GRIDREGULAR(suffix="c", **kwargs)
print(grid.render())
grid = GRIDREGULAR(suffix="inp", **kwargs)
print(grid.render())
Source code in rompy_swan/subcomponents/readgrid.py
class GRIDREGULAR(BaseSubComponent):
    """SWAN Regular Grid subcomponent.

    .. code-block:: text

        xp yp alp xlen ylen mx my

    Note
    ----
    The direction of the x-axis `alp` must be 0 in case of spherical coordinates

    Note
    ----
    All coordinates and distances should be given in m when Cartesian coordinates are
    used or degrees when Spherical coordinates are used (see command COORD).

    Examples
    --------

    .. ipython:: python
        :okwarning:

        from rompy_swan.subcomponents.readgrid import GRIDREGULAR
        kwargs = dict(xp=173, yp=-40, alp=0, xlen=2, ylen=2, mx=199, my=199)
        grid = GRIDREGULAR(suffix="c", **kwargs)
        print(grid.render())
        grid = GRIDREGULAR(suffix="inp", **kwargs)
        print(grid.render())

    """

    model_type: Literal["gridregular", "GRIDREGULAR"] = Field(
        default="gridregular", description="Model type discriminator"
    )
    xp: float = Field(
        description="The x-coordinate of the origin in problem coordinates",
    )
    yp: float = Field(
        description="The y-coordinate of the origin in problem coordinates",
    )
    alp: Optional[float] = Field(
        default=0.0,
        description="Direction of the xaxis in degrees",
    )
    xlen: float = Field(
        description="Length of the computational grid in the x-direction"
    )
    ylen: float = Field(
        description="Length of the computational grid in the y-direction"
    )
    mx: int = Field(
        description=(
            "Number of meshes in computational grid in x-direction (this number is "
            "one less than the number of grid points in this domain)"
        ),
    )
    my: int = Field(
        description=(
            "Number of meshes in computational grid in y-direction (this number is "
            "one less than the number of grid points in this domain)"
        ),
    )
    suffix: Optional[str] = Field(
        default="", description="Suffix for rendering with each output grid parameter."
    )

    @property
    def dx(self):
        """Grid spacing in x-direction."""
        return self.xlen / self.mx

    @property
    def dy(self):
        """Grid spacing in y-direction."""
        return self.ylen / self.my

    def cmd(self) -> str:
        """Command file string for this subcomponent."""
        repr = f"xp{self.suffix}={self.xp}"
        repr += f" yp{self.suffix}={self.yp}"
        repr += f" alp{self.suffix}={self.alp}"
        repr += f" xlen{self.suffix}={self.xlen}"
        repr += f" ylen{self.suffix}={self.ylen}"
        repr += f" mx{self.suffix}={self.mx}"
        repr += f" my{self.suffix}={self.my}"
        return repr

Attributes

model_type class-attribute instance-attribute

model_type: Literal['gridregular', 'GRIDREGULAR'] = Field(default='gridregular', description='Model type discriminator')

xp class-attribute instance-attribute

xp: float = Field(description='The x-coordinate of the origin in problem coordinates')

yp class-attribute instance-attribute

yp: float = Field(description='The y-coordinate of the origin in problem coordinates')

alp class-attribute instance-attribute

alp: Optional[float] = Field(default=0.0, description='Direction of the xaxis in degrees')

xlen class-attribute instance-attribute

xlen: float = Field(description='Length of the computational grid in the x-direction')

ylen class-attribute instance-attribute

ylen: float = Field(description='Length of the computational grid in the y-direction')

mx class-attribute instance-attribute

mx: int = Field(description='Number of meshes in computational grid in x-direction (this number is one less than the number of grid points in this domain)')

my class-attribute instance-attribute

my: int = Field(description='Number of meshes in computational grid in y-direction (this number is one less than the number of grid points in this domain)')

suffix class-attribute instance-attribute

suffix: Optional[str] = Field(default='', description='Suffix for rendering with each output grid parameter.')

dx property

dx

Grid spacing in x-direction.

dy property

dy

Grid spacing in y-direction.

Functions

cmd

cmd() -> str

Command file string for this subcomponent.

Source code in rompy_swan/subcomponents/readgrid.py
def cmd(self) -> str:
    """Command file string for this subcomponent."""
    repr = f"xp{self.suffix}={self.xp}"
    repr += f" yp{self.suffix}={self.yp}"
    repr += f" alp{self.suffix}={self.alp}"
    repr += f" xlen{self.suffix}={self.xlen}"
    repr += f" ylen{self.suffix}={self.ylen}"
    repr += f" mx{self.suffix}={self.mx}"
    repr += f" my{self.suffix}={self.my}"
    return repr

READCOORD

Bases: READGRID

SWAN coordinates reader.

.. code-block:: text

READGRID COORDINATES [fac] 'fname' [idla] [nhedf] [nhedvec] &
    FREE|FORMAT ('form'|idfm)

Examples

.. ipython:: python :okwarning:

from rompy_swan.subcomponents.readgrid import READCOORD
readcoord = READCOORD(
    fac=1.0,
    fname="coords.txt",
    idla=3,
    format="free",
)
print(readcoord.render())
Source code in rompy_swan/subcomponents/readgrid.py
class READCOORD(READGRID):
    """SWAN coordinates reader.

    .. code-block:: text

        READGRID COORDINATES [fac] 'fname' [idla] [nhedf] [nhedvec] &
            FREE|FORMAT ('form'|idfm)

    Examples
    --------

    .. ipython:: python
        :okwarning:

        from rompy_swan.subcomponents.readgrid import READCOORD
        readcoord = READCOORD(
            fac=1.0,
            fname="coords.txt",
            idla=3,
            format="free",
        )
        print(readcoord.render())

    """

    model_type: Literal["readcoord", "READCOORD"] = Field(
        default="readcoord", description="Model type discriminator"
    )
    grid_type: Literal["coordinates"] = Field(
        default="coordinates", description="Type of the SWAN grid file"
    )
    fname: str = Field(description="Name of the SWAN coordinates file")

    def cmd(self) -> str:
        repr = (
            f"READGRID COORDINATES fac={self.fac} fname='{self.fname}' "
            f"idla={self.idla} nhedf={self.nhedf} nhedvec={self.nhedvec} "
            f"{self.format_repr}"
        )
        return repr

Attributes

model_type class-attribute instance-attribute

model_type: Literal['readcoord', 'READCOORD'] = Field(default='readcoord', description='Model type discriminator')

grid_type class-attribute instance-attribute

grid_type: Literal['coordinates'] = Field(default='coordinates', description='Type of the SWAN grid file')

fname class-attribute instance-attribute

fname: str = Field(description='Name of the SWAN coordinates file')

Functions

cmd

cmd() -> str
Source code in rompy_swan/subcomponents/readgrid.py
def cmd(self) -> str:
    repr = (
        f"READGRID COORDINATES fac={self.fac} fname='{self.fname}' "
        f"idla={self.idla} nhedf={self.nhedf} nhedvec={self.nhedvec} "
        f"{self.format_repr}"
    )
    return repr

READINP

Bases: READGRID

SWAN input grid reader.

.. code-block:: text

READINP GRID_TYPE [fac] ('fname1' | SERIES 'fname2') [idla] [nhedf] &
    ([nhedt]) [nhedvec] FREE|FORMAT ('form'|idfm)|UNFORMATTED`

Examples

.. ipython:: python :okwarning:

from rompy_swan.subcomponents.readgrid import READINP
readinp = READINP(
    grid_type="wind",
    fname1="wind.txt",
    fac=1.0,
    idla=3,
    format="free",
)
print(readinp.render())
Source code in rompy_swan/subcomponents/readgrid.py
class READINP(READGRID):
    """SWAN input grid reader.

    .. code-block:: text

        READINP GRID_TYPE [fac] ('fname1' | SERIES 'fname2') [idla] [nhedf] &
            ([nhedt]) [nhedvec] FREE|FORMAT ('form'|idfm)|UNFORMATTED`

    Examples
    --------

    .. ipython:: python
        :okwarning:

        from rompy_swan.subcomponents.readgrid import READINP
        readinp = READINP(
            grid_type="wind",
            fname1="wind.txt",
            fac=1.0,
            idla=3,
            format="free",
        )
        print(readinp.render())

    """

    model_type: Literal["readinp", "READINP"] = Field(
        default="readinp", description="Model type discriminator"
    )
    grid_type: Optional[GridOptions] = Field(
        default=None, description="Type of the SWAN grid file"
    )
    fname1: str = Field(
        description="Name of the file with the values of the variable.",
    )
    fname2: Optional[str] = Field(
        default=None,
        description=(
            "Name of file that contains the names of the files where the variables "
            "are given when the SERIES option is used. These names are to be given in "
            "proper time sequence. SWAN reads the next file when the previous file "
            "end has been encountered. In these files the input should be given in "
            "the same format as in the above file 'fname1' (that implies that a file "
            "should start with the start of an input time step)"
        ),
    )
    nhedt: int = Field(
        default=0,
        description=(
            "Only if variable is time dependent: number of header lines in the file "
            "at the start of each time level. A time step may start with more header "
            "lines than `nhedt` because the variable may be a vector variable which "
            "has its own header lines (see `nhedvec`)"
        ),
        ge=0,
    )

    @field_validator("grid_type")
    @classmethod
    def set_undefined(cls, v: str | None) -> str:
        """Allow for undefined value so it can be redefined in INPGRID components."""
        if v is None:
            return "undefined"
        return v

    def cmd(self) -> str:
        repr = f"READINP {self.grid_type.upper()} fac={self.fac} fname1='{self.fname1}'"
        if self.fname2:
            repr += f" SERIES fname2='{self.fname2}'"
        repr += f" idla={self.idla} nhedf={self.nhedf} nhedt={self.nhedt} nhedvec={self.nhedvec} {self.format_repr}"
        return repr

Attributes

model_type class-attribute instance-attribute

model_type: Literal['readinp', 'READINP'] = Field(default='readinp', description='Model type discriminator')

grid_type class-attribute instance-attribute

grid_type: Optional[GridOptions] = Field(default=None, description='Type of the SWAN grid file')

fname1 class-attribute instance-attribute

fname1: str = Field(description='Name of the file with the values of the variable.')

fname2 class-attribute instance-attribute

fname2: Optional[str] = Field(default=None, description="Name of file that contains the names of the files where the variables are given when the SERIES option is used. These names are to be given in proper time sequence. SWAN reads the next file when the previous file end has been encountered. In these files the input should be given in the same format as in the above file 'fname1' (that implies that a file should start with the start of an input time step)")

nhedt class-attribute instance-attribute

nhedt: int = Field(default=0, description='Only if variable is time dependent: number of header lines in the file at the start of each time level. A time step may start with more header lines than `nhedt` because the variable may be a vector variable which has its own header lines (see `nhedvec`)', ge=0)

Functions

set_undefined classmethod

set_undefined(v: str | None) -> str

Allow for undefined value so it can be redefined in INPGRID components.

Source code in rompy_swan/subcomponents/readgrid.py
@field_validator("grid_type")
@classmethod
def set_undefined(cls, v: str | None) -> str:
    """Allow for undefined value so it can be redefined in INPGRID components."""
    if v is None:
        return "undefined"
    return v

cmd

cmd() -> str
Source code in rompy_swan/subcomponents/readgrid.py
def cmd(self) -> str:
    repr = f"READINP {self.grid_type.upper()} fac={self.fac} fname1='{self.fname1}'"
    if self.fname2:
        repr += f" SERIES fname2='{self.fname2}'"
    repr += f" idla={self.idla} nhedf={self.nhedf} nhedt={self.nhedt} nhedvec={self.nhedvec} {self.format_repr}"
    return repr