Skip to content

Output

SWAN writes output at named sets of locations: the computational grid, a frame, a sub-grid, points, curves or depth contours. Write commands then say what to write there and where: maps (BLOCK), tables (TABLE), spectra (SPECOUT) or boundary spectra for a nested model (NESTOUT). In rompy-swan each command is a component named after it, and the OUTPUT group, the output field of SwanConfig, writes them in order and checks that every write command refers to locations that exist. This page covers the locations, the write commands, the output quantities and formats, the output times and how to read the files.

The OUTPUT group

OUTPUT takes one component per field and writes them in this order, locations first:

Field Components SWAN commands
frame FRAME FRAME
group GROUP GROUP
curve CURVES one CURVE per curve
ray RAY RAY
isoline ISOLINE ISOLINE
points POINTS, POINTS_FILE POINTS
nests list of NEST NGRID and NESTOUT per nest
quantity QUANTITIES one QUANTITY per entry
output_options OUTPUT_OPTIONS OUTPUT OPTIONS
block BLOCK, BLOCKS one BLOCK per map file
table TABLE TABLE
specout SPECOUT SPECOUT
test TEST TEST

The ngrid and nestout fields are deprecated: use nests, described in Nesting.

from rompy_swan.components.group import OUTPUT
from rompy_swan.components.output import BLOCK, POINTS, SPECOUT, TABLE

output = OUTPUT(
    points=POINTS(sname="sites", xp=[113.0, 114.5], yp=[-32.0, -34.0]),
    block=BLOCK(sname="COMPGRID", fname="swangrid.nc", output=["depth", "hsign", "tps", "dir"]),
    table=TABLE(sname="sites", fname="sites.txt", output=["hsign", "tps", "dir"]),
    specout=SPECOUT(sname="sites", fname="spectra.nc"),
)
print(output.render())
POINTS sname='sites' &
    xp=113.0 yp=-32.0 &
    xp=114.5 yp=-34.0

BLOCK sname='COMPGRID' fname='swangrid.nc' &
    DEPTH &
    HSIGN &
    TPS &
    DIR

TABLE sname='sites' fname='sites.txt' &
    HSIGN &
    TPS &
    DIR

SPECOUT sname='sites' fname='spectra.nc'

The group checks the rules of the SWAN manual:

  • each set of locations has a unique sname of at most 8 characters, which is not one of the special names;
  • the sname of each write command is defined by a location component, or is a special name;
  • BLOCK writes on COMPGRID, BOTTGRID, a FRAME or a GROUP only;
  • an ISOLINE needs the RAY it names.
from pydantic import ValidationError

try:
    OUTPUT(table=TABLE(sname="buoys", fname="buoys.txt", output=["hsign"]))
except ValidationError as err:
    print(err.errors()[0]["msg"])
Value error, Write component 'table' specified with sname='buoys' but no location component with sname='buoys' has been defined

Output locations

Locations Component Use
COMPGRID, BOTTGRID none: special names the computational grid, the bottom grid; for BLOCK only
a regular grid of its own FRAME maps of part of the domain at another resolution or orientation
part of the computational grid GROUP maps on a sub-grid by indices, without interpolation (not on unstructured grids)
points POINTS, POINTS_FILE buoys, sites; POINTS_FILE reads the coordinates from a file
points along lines CURVES of CURVE transects
points along a depth contour RAY and ISOLINE e.g. conditions along the 10 m contour
the boundary of a nested grid NGRID in a NEST boundary spectra for a nested model

Coordinates are in the units of the computational grid: degrees with spherical coordinates, metres with Cartesian ones. Outside the computational grid, or on dry points, SWAN writes the exception value.

from rompy_swan.components.output import CURVE, CURVES, FRAME, GROUP, ISOLINE, POINTS_FILE, RAY

frame = FRAME(sname="coast", grid=dict(xp=114.0, yp=-35.0, alp=0.0, xlen=2.0, ylen=3.0, mx=40, my=60))
group = GROUP(sname="west", ix1=0, iy1=0, ix2=4, iy2=14)
curves = CURVES(
    curves=[CURVE(sname="transect", xp1=111.0, yp1=-33.0, npts=[20], xp=[115.0], yp=[-33.0])]
)
ray = RAY(rname="rays", xp1=111.0, yp1=-35.0, xq1=116.0, yq1=-35.0, npts=[10], xp=[111.0], yp=[-30.0], xq=[116.0], yq=[-30.0])
isoline = ISOLINE(sname="dep50", rname="rays", dep=50.0)
for location in [frame, group, curves, ray, isoline, POINTS_FILE(sname="buoys", fname="buoys.txt")]:
    print(location.render())
FRAME sname='coast' xpfr=114.0 ypfr=-35.0 alpfr=0.0 xlenfr=2.0 ylenfr=3.0 mxfr=40 myfr=60
GROUP sname='west' SUBGRID ix1=0 iy1=0 ix2=4 iy2=14
CURVE sname='transect' xp1=111.0 yp1=-33.0 &
    int=20 xp=115.0 yp=-33.0
RAY rname='rays' xp1=111.0 yp1=-35.0 xq1=116.0 yq1=-35.0 &
    int=10 xp=111.0 yp=-30.0 xq=116.0 yq=-30.0
ISOLINE sname='dep50' rname='rays' dep=50.0
POINTS sname='buoys' FILE fname='buoys.txt'

A CURVE has corner points xp1, yp1, then xp, yp; npts gives the number of segments between successive corners, so npts=[20] gives 21 points. RAY defines master rays from (xp, yp) to (xq, yq), subdivided by npts; ISOLINE takes the points where the rays cross the depth contour dep.

What to write

Component Writes Locations
BLOCK maps of the output quantities COMPGRID, BOTTGRID, FRAME, GROUP
BLOCKS several BLOCK files, each with its own locations, quantities and times as BLOCK
TABLE one row per location and time, format "header" (SWAN default), "noheader" or "indexed" points, curves, frames, groups
SPECOUT spectra, 2D (SPEC2D, default) or 1D (SPEC1D), in absolute (ABS, default) or relative (REL) frequency as TABLE
NESTOUT 2D spectra along the boundary of a nested grid NGRID, through NEST
TEST integral parameters and source terms at up to 50 points, for debugging its own points

The output quantities are SWAN's keywords in lower case: hsign, hswell, dir, pdir, tps, tm01, tm02, dspr, depth, watlev, wind, vel, setup, qb, ubot, dissip, time, xp, yp and many others, listed in BlockOptions.

from rompy_swan.components.output import BLOCKS
from rompy_swan.subcomponents.output import REL, SPEC1D

maps = BLOCKS(
    components=[
        BLOCK(sname="COMPGRID", fname="swangrid.nc", output=["hsign", "hswell", "dir"]),
        BLOCK(sname="coast", fname="coast.nc", output=["hsign", "tps", "setup"]),
    ]
)
print(maps.render())
print(TABLE(sname="sites", format="noheader", fname="sites.txt", output=["time", "hsign", "tps"]).render())
print(SPECOUT(sname="sites", dim=SPEC1D(), freq=REL(), fname="spec1d.nc").render())
BLOCK sname='COMPGRID' fname='swangrid.nc' &
    HSIGN &
    HSWELL &
    DIR
BLOCK sname='coast' fname='coast.nc' &
    HSIGN &
    TPS &
    SETUP
TABLE sname='sites' NOHEADER fname='sites.txt' &
    TIME &
    HSIGN &
    TPS
SPECOUT sname='sites' SPEC1D REL fname='spec1d.nc'

File formats

The extension of fname sets the format SWAN writes:

Extension Format
.nc NetCDF, one variable per quantity
.mat MATLAB binary, single precision
anything else text

NetCDF output needs a SWAN built with NetCDF. The ghcr.io/rom-py/swan Docker image has it; the official delftwaves/swan image does not, and writes files named .nc as text. For text maps, BLOCK also takes header=False, the layout idla (1, 3 or 4) and the scaling unit; OUTPUT_OPTIONS sets the comment character, the width of table fields and the decimals of block and spectral output:

from rompy_swan.components.output import OUTPUT_OPTIONS

print(BLOCK(sname="COMPGRID", fname="hsign.txt", header=False, idla=3, output=["hsign"]).render())
print(OUTPUT_OPTIONS(comment="%", field=10, ndec_block=5, len=8, ndec_spec=6).render())
BLOCK sname='COMPGRID' NOHEADER fname='hsign.txt' LAYOUT idla=3 HSIGN
OUTPUT OPTIONS comment='%' TABLE field=10 BLOCK ndec=5 len=8 SPEC ndec=6

Quantity options: QUANTITY

QUANTITY changes how quantities are computed or written; the quantity field takes several in a QUANTITIES list. The most used options:

Option Effect SWAN default
fswell upper frequency of the swell, for hswell 0.1 Hz
fmin, fmax frequency range of the integral parameters 0 to 1000 Hz
excv exception value, written at dry points or outside the grid
hexp highest expected value, which sets the decimals in tables with header
power power of the mean period per and wave length wlen 1
ref reference time of tsec start of the computation
short, long names in table and block headers SWAN's names
coord directions and vectors relative to the problem coordinates or to the FRAME problemcoord
from rompy_swan.components.output import QUANTITIES, QUANTITY

quantity = QUANTITIES(
    quantities=[
        QUANTITY(output=["hswell"], fswell=0.08),
        QUANTITY(output=["hsign", "tps", "dir"], excv=-9.0),
        QUANTITY(output=["tm01"], fmin=0.04, fmax=0.5),
    ]
)
print(quantity.render())
QUANTITY HSWELL fswell=0.08
QUANTITY HSIGN TPS DIR excv=-9.0
QUANTITY TM01 fmin=0.04 fmax=0.5

Output times

In stationary mode SWAN makes a single computation, and the write commands have no times: SWAN writes once, at the end. SwanConfig rejects write components with times in this mode (see Stationary and nonstationary runs).

In nonstationary mode each write command takes output times. rompy-swan sets them from the run period when the workspace is generated: output starts at the run start and is written at the run interval. The times field, a TimeRangeOpen, overrides them: delt sets another interval, tbeg a later start (for example to leave out the spin-up), and dfmt and tfmt the format of the interval and time. The same applies to NESTOUT in nests.

from datetime import datetime, timedelta
from pathlib import Path

from rompy.core.time import TimeRange
from rompy.model import ModelRun
from rompy_swan.components.cgrid import REGULAR
from rompy_swan.components.group import LOCKUP, STARTUP
from rompy_swan.components.lockup import COMPUTE_NONSTAT
from rompy_swan.components.startup import MODE
from rompy_swan.config import SwanConfig
from rompy_swan.grid import SwanGrid
from rompy_swan.subcomponents.spectrum import SPECTRUM
from rompy_swan.subcomponents.time import TimeRangeOpen

grid = SwanGrid(x0=110.0, y0=-36.0, dx=0.5, dy=0.5, nx=13, ny=15)
output = OUTPUT(
    points=POINTS(sname="sites", xp=[113.0, 114.5], yp=[-32.0, -34.0]),
    # Every run interval, from the run start
    block=BLOCK(sname="COMPGRID", fname="swangrid.nc", output=["hsign", "tps"]),
    # Every 3 hours
    table=TABLE(
        sname="sites",
        fname="sites.txt",
        output=["time", "hsign", "tps"],
        times=TimeRangeOpen(delt=timedelta(hours=3), dfmt="hr"),
    ),
    # Every hour, after a 6-hour spin-up
    specout=SPECOUT(
        sname="sites",
        fname="spectra.nc",
        times=TimeRangeOpen(tbeg=datetime(2023, 1, 1, 6), delt=timedelta(hours=1), dfmt="hr"),
    ),
)
config = SwanConfig(
    startup=STARTUP(mode=MODE(kind="nonstationary")),
    cgrid=REGULAR(grid=grid.component, spectrum=SPECTRUM(mdc=36, flow=0.04, fhigh=1.0)),
    output=output,
    lockup=LOCKUP(compute=COMPUTE_NONSTAT()),
)
period = TimeRange(start="2023-01-01T00:00", end="2023-01-02T00:00", interval="30m")
workspace = Path(ModelRun(run_id="output", period=period, output_dir=OUT_DIR, config=config)())
input_file = (workspace / "INPUT").read_text()
section = input_file[input_file.index("! Output") :].splitlines()
print("\n".join(line for line in section if line and line[0] != "!"))
POINTS sname='sites' &
    xp=113.0 yp=-32.0 &
    xp=114.5 yp=-34.0
BLOCK sname='COMPGRID' fname='swangrid.nc' &
    HSIGN &
    TPS &
    OUTPUT tbegblk=20230101.000000 deltblk=1800.0 SEC
TABLE sname='sites' fname='sites.txt' &
    TIME &
    HSIGN &
    TPS &
    OUTPUT tbegtbl=20230101.000000 delttbl=3.0 HR
SPECOUT sname='sites' fname='spectra.nc' OUTPUT tbegspc=20230101.060000 deltspc=1.0 HR
COMPUTE NONSTATIONARY tbegc=20230101.000000 deltc=1800.0 SEC tendc=20230102.000000
STOP

The OUTPUT keyword and the times follow the output quantities of each write command, and COMPUTE NONSTATIONARY takes the run period.

Reading the output

SWAN writes the output files into the workspace. These examples need SWAN output, so they are not run here; the output example and tutorial lesson 6 run them on real results.

NetCDF maps open with xarray. SWAN uses short variable names, for example hs for HSIGN, hswe for HSWELL and xwnd, ywnd for WIND:

import xarray as xr

maps = xr.open_dataset(workspace / "swangrid.nc")
maps.hs.sel(time="2023-01-01T12:00").plot()

A text table has one row per location and time, the locations repeating at each time; with format="header" the header lines start with %:

import pandas as pd

table = pd.read_csv(
    workspace / "sites.txt", comment="%", sep=r"\s+", names=["time", "hs", "tps"], dtype={"time": str}
)
table["time"] = pd.to_datetime(table["time"], format="%Y%m%d.%H%M%S")
table["site"] = ["site1", "site2"] * (len(table) // 2)

wavespectra reads SWAN spectra, NetCDF with read_ncswan and text with read_swan:

import wavespectra

spectra = wavespectra.read_ncswan(workspace / "spectra.nc")
print(spectra.spec.hs())
spectra.isel(site=0, time=-1).spec.plot()

Also read PRINT after each run: SWAN reports there the errors and warnings that can leave output files empty or incomplete. See Running SWAN.

See it in the notebooks