src.io#
Input/Output routines
Functions
|
Automatically generates HDF5 output filename from |
Classes
|
Helper class to read NetCDF4 topographic data |
|
Simple reader class to read HDF5 output written by |
|
HDF5 writer class |
- class src.io.ncdata(read_merit=False, padding=0, padding_tol=50)#
Helper class to read NetCDF4 topographic data
- __init__(read_merit=False, padding=0, padding_tol=50)#
Parameters#
- read_meritbool, optional
toggles between the MERIT DEM and USGS GMTED 2010 data files. By default False, i.e., read USGS GMTED 2010 data files.
- paddingint, optional
number of data points to pad the loaded topography file, by default 0
- padding_tolint, optional
padding tolerance is added no matter the user-defined
padding
, by default 50
- read_dat(fn, obj)#
Reads data by attributes defined in the
obj
class.Parameters#
- fnstr
filename
- obj
src.var.grid
orsrc.var.topo
orsrc.var.topo_cell
any data object in
src.var
accepting topography attributes
- read_topo(topo, cell, lon_vert, lat_vert)#
Reads USGS GMTED 2010 dataset
Parameters#
- topo
src.var.topo
orsrc.var.topo_cell
instance of a topography class containing the full regional or global topography loaded via
src.io.read_dat()
.- cell
src.var.topo_cell
instance of a cell object
- lon_vertlist
extent of the longitudinal coordinates encompassing the region to be loaded
- lat_vertlist
extent of the latitudinal coordinates encompassing the region to be loaded
Note
Loading the global topography in the
topo
argument may not be memory efficient. The notebooknc_compactifier.ipynb
contains a script to extract a region of interest from the global GMTED 2010 dataset.- topo
- class read_merit_topo(cell, params, verbose=False)#
Subclass to read MERIT topographic data
- __init__(cell, params, verbose=False)#
Populates
cell
object instance with arguments fromparams
Parameters#
- cell
src.var.topo
orsrc.var.topo_cell
instance of an object with topograhy attribute
- params
src.var.params
user-defined run parameters
- verbosebool, optional
prints loading progression, by default False
- cell
- get_topo(cell, fns, lon_cnt, lat_cnt, init=True, populate=True)#
This method assembles a contiguous array in
cell.topo
containing the regional topography to be loaded.However, this full regional array is assembled from an array of block arrays. Each block array is loaded from a separated MERIT data file and varies in shape that is not known beforehand.
- Therefore, the
get_topo
method is run recursively: The first run determines the shape of each constituting block array and subsequently the shape of the full regional array. An empty array in initialised.
The second run populates the empty array with the information of the block arrays obtained in the first run.
- Therefore, the
- class src.io.writer(fn, idxs, sfx='', debug=False)#
HDF5 writer class
Contains methods to create HDF5 file, create data sets and populate them with output variables.
Note
This class was taken from an I/O routine originally written for the numerical flow solver used in Chew et al. (2022) and Chew et al. (2023).
- __init__(fn, idxs, sfx='', debug=False)#
Creates an empty HDF5 file with filename
fn
and a group for each index inidxs
Parameters#
- fnstr
filename
- idxslist
list of cell indices
- sfxstr, optional
suffixes to the filename, by default ‘’
- debugbool, optional
debug flag, by default False
- io_create_file(paths)#
Helper function to create file.
Parameters#
- pathslist
List of strings containing the name of the groups.
Notes#
Currently, if the filename of the HDF5 file already exists, this function will append the existing filename with ‘_old’ and create an empty HDF5 file with the same filename in its place.
- write_all(idx, *args)#
Write all attributes and datasets of a given class instance to the group
idx
.Parameters#
- idxstr or int
group name to write the attributes or datasets
- write_attr(idx, key, value)#
Write HDF5 attributes for a group
Parameters#
- idxstr or int
group name to write the attributes
- keystr
attribute name
- valueany
attribute value that is accepted by HDF5
- write_all_attrs(obj)#
Write all attributes a given class instance to the HDF5 file
Parameters#
- obj
src.var.params
write all user-defined parameters to the HDF5 file for reproducibility of the output
- obj
- class src.io.reader(fn)#
Simple reader class to read HDF5 output written by
src.io.writer
- get_params(params)#
Get the user-defined parameters from the HDF5 file attributes
Parameters#
- params
src.var.params
empty instance of the user-defined parameters class to be populated
- params
- read_data(idx, name)#
Read a particular dataset
name
from a groupidx
Parameters#
- idxstr or int
the group name
- namestr
the dataset name
Returns#
- array-like
the dataset
- read_all(idx, cell)#
Populate
cell
with all datasets in a groupidx
Parameters#
- idxint or str
the group name
- cell
src.var.topo_cell
empty instance of a cell object to be populated
- src.io.fn_gen(params)#
Automatically generates HDF5 output filename from
src.var.params
.Parameters#
- params
src.var.params
instance of the user parameter class
Returns#
- str
automatically generated filename
- params