Data sources¶
A source describes where data lives and how to open it; its open() method returns an xarray dataset. The bathymetry and every forcing class take a source in their source field. rompy defines the base sources, and rompy-xbeach extends them to carry a coordinate reference system (CRS), which is needed to reproject data onto a projected XBeach grid. This page lists the sources, shows which forcing classes accept which, and covers the options that control which part of the data is used.
The sources¶
| Source | From | model_type |
Data |
|---|---|---|---|
SourceGeotiff |
rompy-xbeach | geotiff |
GeoTIFF rasters; CRS read from the file |
SourceCRSFile |
rompy-xbeach | file |
NetCDF, Zarr or anything xarray.open_dataset opens |
SourceXYZ |
rompy-xbeach | xyz |
x, y, z point clouds, gridded when opened |
SourceCRSIntake |
rompy-xbeach | intake |
A dataset in an intake catalogue |
SourceCRSDataset |
rompy-xbeach | dataset |
An xarray dataset in memory |
SourceCRSWavespectra |
rompy-xbeach | wavespectra |
Wave spectra read with a wavespectra reader |
SourceCRSOceantide |
rompy-xbeach | oceantide |
Gridded tidal constituents read with an oceantide reader |
SourceTideConsPointCSV |
rompy-xbeach | tide_cons_point_csv |
Tidal constituents at one site, from CSV |
SourceTimeseriesCSV |
rompy | csv |
A timeseries in a CSV file |
The CRS-aware sources extend rompy's SourceFile, SourceIntake and SourceWavespectra with three fields: crs, and x_dim and y_dim, the names of the spatial dimensions (default x and y). SourceCRSWavespectra and SourceCRSOceantide default to crs=4326 with lon and lat, the usual layout of those datasets.
SourceCRSDataset and the timeseries source SourceTimeseriesDataFrame come from the rompy-binary-datasources package, installed with pip install rompy-xbeach[extra]. They hold data in memory, so they cannot be written to YAML.
Which classes accept which sources¶
| Data interface | Sources |
|---|---|
XBeachBathy |
geotiff, file, xyz, intake, dataset |
Wave spectra: BoundaryStationSpectraJons, ...Jonstable, ...Swan |
wavespectra, file, intake, dataset |
Wave parameters: BoundaryStationParam..., BoundaryGridParam... |
file, intake, dataset |
Wave parameters at a point: BoundaryPointParam... |
csv, and rompy's file, intake and dataset without a CRS |
WindGrid, WindStation, WaterLevelGrid, WaterLevelStation |
file, geotiff, xyz, intake, dataset |
WindPoint, WaterLevelPoint |
csv (and dataframe with the extra package) |
TideConsGrid |
oceantide |
TideConsPoint |
tide_cons_point_csv |
The forcing classes are described in Waves, Wind and Tide and water levels.
Gridded data¶
GeoTIFF¶
The CRS is read from the file, and band picks the raster band (default 1). The data variable is always called data, which is the default variable of XBeachBathy.
from rompy_xbeach.source import SourceGeotiff
source = SourceGeotiff(filename="tests/data/bathy.tif")
ds = source.open()
print(ds.rio.crs, dict(ds.sizes), list(ds.data_vars))
NetCDF and other xarray formats¶
uri is opened with xarray.open_dataset, and kwargs are passed to it. NetCDF files rarely declare a CRS, so you state it, with the names of the x and y dimensions:
from rompy_xbeach.source import SourceCRSFile
source = SourceCRSFile(uri="tests/data/bathy.nc", crs=4326, x_dim="x", y_dim="y")
ds = source.open()
print(ds.rio.crs, dict(ds.sizes))
XYZ point clouds¶
Survey or LiDAR points are interpolated onto a regular grid of resolution res (in CRS units) with scipy.interpolate.griddata when opened. xcol, ycol and zcol name the columns, read_csv_kwargs is passed to pandas.read_csv and griddata_kwargs to griddata (default {"method": "linear"}). The gridded variable is called data.
from rompy_xbeach.source import SourceXYZ
source = SourceXYZ(
filename="tests/data/bathy_xyz.zip",
crs=4326,
res=0.0005,
xcol="easting",
ycol="northing",
zcol="elevation",
read_csv_kwargs={"sep": "\t"},
)
print(dict(source.open().sizes))
Intake catalogues¶
An intake catalogue lists datasets by name, so configurations can refer to data without hard-coding file paths. Give either catalog_uri or catalog_yaml (the catalogue as a string), and dataset_id:
from rompy_xbeach.source import SourceCRSIntake
source = SourceCRSIntake(
catalog_uri="tests/data/catalog.yaml", dataset_id="bathy_netcdf", crs=4326
)
print(dict(source.open().sizes))
An xarray dataset in memory¶
Useful when data has been downloaded or processed in the same script. Needs rompy-xbeach[extra]:
import xarray as xr
from rompy_xbeach.source import SourceCRSDataset
source = SourceCRSDataset(obj=xr.open_dataset("tests/data/bathy.nc"), crs=4326)
Specialised sources¶
Wave spectra¶
SourceCRSWavespectra opens spectra with a wavespectra reader such as read_ww3, read_swan or read_era5; kwargs are passed to the reader.
from rompy_xbeach.source import SourceCRSWavespectra
source = SourceCRSWavespectra(
uri="tests/data/ww3-spectra-20230101-short.nc", reader="read_ww3"
)
print(dict(source.open().sizes))
Tidal constituents¶
SourceCRSOceantide reads gridded constituents with an oceantide reader, here an OTIS binary model. SourceTideConsPointCSV reads amplitude and phase per constituent at one site; ccol, acol and pcol name its columns.
from rompy_xbeach.source import SourceCRSOceantide, SourceTideConsPointCSV
cons_dir = "tests/data/swaus_tide_cons"
source = SourceCRSOceantide(
reader="read_otis_binary",
kwargs={
"gfile": f"{cons_dir}/grid_m2s2n2k2k1o1p1q1mmmf",
"hfile": f"{cons_dir}/h_m2s2n2k2k1o1p1q1mmmf",
"ufile": f"{cons_dir}/u_m2s2n2k2k1o1p1q1mmmf",
},
)
print("Gridded:", source.open().con.values)
source = SourceTideConsPointCSV(filename="tests/data/tide_cons_station.csv")
print("Point: ", source.open().con.values)
Gridded: ['M2' 'S2' 'N2' 'K2' 'K1' 'O1' 'P1' 'Q1' 'MM' 'MF']
Point: ['M2' 'S2' 'N2' 'K2' 'K1' 'O1' 'P1' 'Q1' 'MM' 'MF']
Timeseries¶
The ...Point classes take a single timeseries, such as a buoy or tide gauge record, which needs no CRS. rompy's SourceTimeseriesCSV reads one from CSV; tcol names the time column and the other columns become variables.
from rompy.core.source import SourceTimeseriesCSV
source = SourceTimeseriesCSV(filename="tests/data/wind.csv", tcol="time")
print(source.open())
<xarray.Dataset> Size: 1kB
Dimensions: (time: 25)
Coordinates:
* time (time) datetime64[us] 200B 2023-01-01 ... 2023-01-02
Data variables:
u10 (time) float64 200B -3.634 -3.476 -3.083 ... -5.894 -6.366 -6.693
v10 (time) float64 200B 6.261 6.246 6.411 6.792 ... 6.306 6.253 6.247
wspd (time) float64 200B 7.239 7.148 7.113 7.274 ... 8.632 8.923 9.155
wdir (time) float64 200B 149.9 150.9 154.3 159.0 ... 136.9 134.5 133.0
Choosing which data is used¶
The forcing classes build on rompy's DataGrid and inherit its fields. rompy-xbeach adds the fields that pick a location on the XBeach grid.
| Field | From | Purpose |
|---|---|---|
source |
rompy | Where the data comes from |
coords |
rompy | Names of the t, x, y and site (s) coordinates |
variables |
rompy | Variables to read |
filter |
rompy | Sort, subset, crop or rename when the data is opened |
crop_data, time_buffer |
rompy | Crop the data to the run period, plus extra time steps |
buffer |
rompy | Spatial margin for cropping; no effect in rompy-xbeach, which samples points |
location |
rompy-xbeach | Where the data is taken: centre of the grid, or offshore boundary midpoint |
sel_method, sel_method_kwargs |
rompy-xbeach | How the data is interpolated to that location |
Wind and water levels are taken at the grid centre by default; wave boundaries always at the offshore midpoint.
The ds property returns the dataset exactly as the object sees it, with the source, variables and filters applied, which is the quickest way to check a configuration before writing files. coords must match the dataset: the default names are longitude, latitude and time, so data with lon and lat must say so.
from rompy_xbeach.data.waterlevel import WaterLevelGrid
ssh_source = SourceCRSFile(
uri="tests/data/ssh_gridded.nc", crs=4326, x_dim="lon", y_dim="lat"
)
waterlevel = WaterLevelGrid(
source=ssh_source, coords={"x": "lon", "y": "lat"}, variables=["ssh"]
)
print(dict(waterlevel.ds.sizes))
Filters¶
filter.crop selects a range along any coordinate when the data is opened, which keeps memory use low with large regional or global datasets. Filter also provides sort, subset and rename. Filters are applied after variables are selected, so they cannot create the variables a class asks for.
from rompy.core.filters import Filter
cropped = WaterLevelGrid(
source=ssh_source,
coords={"x": "lon", "y": "lat"},
variables=["ssh"],
filter=Filter(crop={"lon": slice(115.4, 115.8), "lat": slice(-32.8, -32.5)}),
)
print(dict(cropped.ds.sizes))
In YAML, a crop is written as filter: {crop: {lon: {start: 115.4, stop: 115.8}}}.
Cropping to the run period¶
With crop_data=True (the default), get() limits the data to the run period plus time_buffer source time steps on each side (default one before and one after). The margin lets start and end times that fall between source time steps be interpolated. After get() the time crop appears in the filter:
from rompy.core.time import TimeRange
from rompy_xbeach.grid import RegularGrid
grid = RegularGrid(
ori={"x": 115.594239, "y": -32.641104, "crs": 4326},
alfa=347.0, dx=10.0, dy=15.0, nx=230, ny=220, crs=28350,
)
period = TimeRange(start="2023-01-01T06", end="2023-01-01T12", interval="1h")
waterlevel.get(destdir=OUT_DIR, grid=grid, time=period)
print(waterlevel.filter.crop)
{'time': Slice(start=datetime.datetime(2023, 1, 1, 5, 0), stop=datetime.datetime(2023, 1, 1, 13, 0))}
The run period must lie within the source's time range, otherwise get() stops with an error naming both ranges.
Interpolating to the location: sel_method¶
Station data (a site dimension, with x and y as variables) and gridded data are sampled differently:
| Data | sel_method |
Default | Options |
|---|---|---|---|
| Stations | idw, nearest |
idw |
wavespectra's sel_idw / sel_nearest; sel_method_kwargs such as tolerance or max_sites |
| Gridded | sel, interp |
sel with {"method": "nearest"} |
xarray's sel or interp; sel_method_kwargs are passed to them |
Both write the same file, with slightly different values. Inverse distance weighting needs neighbouring sites within its tolerance. With a single site, or none close enough, it returns only missing values and get() raises an error suggesting sel_method="nearest".
from pathlib import Path
from rompy_xbeach.data.waterlevel import WaterLevelStation
for method in ("idw", "nearest"):
stations = WaterLevelStation(
source=SourceCRSFile(
uri="tests/data/ssh_stations.nc", crs=4326, x_dim="lon", y_dim="lat"
),
coords={"s": "site", "x": "lon", "y": "lat"},
variables=["ssh"],
sel_method=method,
)
params = stations.get(destdir=OUT_DIR, grid=grid, time=period)
first_line = (Path(OUT_DIR) / params["zs0file"]).read_text().splitlines()[0]
print(f"{method:8s} {params['zs0file']}: {first_line}")
idw tide-20230101T060000-20230101T120000.txt: 0.00 0.44
nearest tide-20230101T060000-20230101T120000.txt: 0.00 0.46
See it in the notebooks¶
See it in the notebooks
- 3. Bathymetry from your data (tutorial)
- 4. Adding forcing: waves, wind and water levels (tutorial)
- Bathymetry options (example)
- Data sources (example)
- Water level forcing (example)
- Wave boundaries from spectra (example)
- Wave boundaries from wave parameters (example)
- Wind forcing (example)
- Data selection options (example)