Writers
Premade discharge writers for Router.set_discharge_writer. zarr_writer is the default. discharge_dir names
outputs discharge_<name>.zarr for it, so give any other writer its output paths with discharge_files.
import river_route as rr
router = rr.Router(rr.Configs.from_json('config.json')).set_discharge_writer(rr.router.writers.netcdf_writer)
river_route.router.writers
bitround(values, keepbits)
A copy of a float32 array with all but the leading keepbits mantissa bits zeroed, rounded to nearest even.
The relative error is bounded by 2 ** -(keepbits + 1) for every normal float. With 23 or more keepbits, every
float32 mantissa bit, or an array that is not float32, the array itself is returned, not a copy.
null_writer(*args, **kwargs)
Return without writing anything. Useful for benchmarking and auditing other processes. If you actually need to write to the null device, your custom writer may need to do something like this:
with open(os.devnull, 'wb') as sink: discharge_array.tofile(sink)
netcdf_writer(router, dates, discharge_array, discharge_file, forcing_file='', *, thread_pool=None, threads=1)
Write routed discharge, Q, to an uncompressed netCDF file with dimensions (riverId, time).
The variable is dimensioned as the routed array is laid out, so the whole array is written in one call with nothing transposed. Writing (time, riverId) instead costs about twice the kernel time on a large network, because each river's series then has to be transposed out in blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
router
|
Router
|
the Router that routed the discharge |
required |
dates
|
DatetimeArray
|
datetime array corresponding to the discharge columns |
required |
discharge_array
|
FloatArray
|
routed discharge values, C-order with shape (river, time) |
required |
discharge_file
|
PathInput
|
path to write the discharge data to |
required |
forcing_file
|
PathInput
|
path to the forcing file the discharge was routed from, or '' for channel routing |
''
|
thread_pool
|
Executor | None
|
not used, since the whole array is written in one call |
None
|
threads
|
int
|
not used, since the whole array is written in one call |
1
|
zarr_writer(router, dates, discharge_array, discharge_file, forcing_file='', *, thread_pool=None, threads=1)
Write routed discharge, Q, to a zarr store with dimensions (riverId, time), replacing anything already at the path. This is the default writer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
router
|
Router
|
the Router that routed the discharge |
required |
dates
|
DatetimeArray
|
datetime array corresponding to the discharge columns |
required |
discharge_array
|
FloatArray
|
routed discharge values, C-order with shape (river, time) |
required |
discharge_file
|
PathInput
|
path of the zarr store to write |
required |
forcing_file
|
PathInput
|
path to the forcing file the discharge was routed from, or '' for channel routing |
''
|
thread_pool
|
Executor | None
|
optional pool the chunks are rounded and written on, used as given and never shut down here |
None
|
threads
|
int
|
the concurrency of zarr's writes, and of the chunk writes when |
1
|