pycsa.core.io¶
Input/Output routines
Functions
|
Automatically generates HDF5 output filename from |
Classes
|
Write per-cell CSA results to a chunked NetCDF4 output file. |
|
Helper class to read NetCDF4 topographic data |
|
Simple reader class to read HDF5 output written by |
|
HDF5 writer class |
- class pycsa.core.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_merit (bool, optional) – toggles between the MERIT DEM and USGS GMTED 2010 data files. By default False, i.e., read USGS GMTED 2010 data files.
padding (int, optional) – number of data points to pad the loaded topography file, by default 0
padding_tol (int, 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
objclass.- Parameters:
fn (str) – filename
obj (
pycsa.data.cell.gridorpycsa.data.cell.topoorpycsa.data.cell.topo_cell) – any data object inpycsa.data.cellaccepting topography attributes
- read_topo(topo, cell, lon_vert, lat_vert)¶
Reads USGS GMTED 2010 dataset
- Parameters:
topo (
pycsa.data.cell.topoorpycsa.data.cell.topo_cell) – instance of a topography class containing the full regional or global topography loaded viapycsa.core.io.ncdata.read_dat().cell (
pycsa.data.cell.topo_cell) – instance of a cell objectlon_vert (list) – extent of the longitudinal coordinates encompassing the region to be loaded
lat_vert (list) – extent of the latitudinal coordinates encompassing the region to be loaded
note:: (..) – Loading the global topography in the
topoargument may not be memory efficient. The notebooknc_compactifier.ipynbcontains a script to extract a region of interest from the global GMTED 2010 dataset.
- class read_merit_topo(cell, params, verbose=False, is_parallel=False)¶
Subclass to read MERIT topographic data
- __init__(cell, params, verbose=False, is_parallel=False)¶
Populates
cellobject instance with arguments fromparams- Parameters:
cell (
pycsa.data.cell.topoorpycsa.data.cell.topo_cell) – instance of an object with topograhy attributeparams (
pycsa.config.params.params) – user-defined run parametersverbose (bool, optional) – prints loading progression, by default False
is_parallel (bool, optional) – flag for parallel processing, by default False
- close_cached_files()¶
Close all cached NetCDF files in current thread.
- get_topo(cell)¶
Load MERIT topography spanning the configured lat-lon extent.
Determines which MERIT (or REMA, for the Antarctic band) tiles cover the requested region, handles the east-west split for extents wider than 180 degrees, and loads the assembled topography into
cell.- Parameters:
cell (
pycsa.data.cell.topoorpycsa.data.cell.topo_cell) – instance whoselat,lonandtopoattributes are populated in place with the loaded data
- close_all()¶
Close every NetCDF dataset opened during loading.
Iterates over the handles accumulated in
self.opened_dfsand closes each one to release the underlying file descriptors.
- class read_etopo_topo(cell, params, verbose=False, is_parallel=False)¶
Subclass to read ETOPO 2022 15 arc-second topographic data
- __init__(cell, params, verbose=False, is_parallel=False)¶
Populates
cellobject instance with arguments fromparams- Parameters:
cell (
pycsa.data.cell.topoorpycsa.data.cell.topo_cell) – instance of an object with topography attributeparams (
pycsa.config.params.params) – user-defined run parametersverbose (bool, optional) – prints loading progression, by default False
is_parallel (bool, optional) – flag for parallel processing, by default False
- close_cached_files()¶
Close all cached NetCDF files in current thread.
- get_topo(cell)¶
Main method to load ETOPO topography data
- close_all()¶
Close all opened NetCDF files
- class pycsa.core.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
fnand a group for each index inidxs
- io_create_file(paths)¶
Helper function to create file.
- Parameters:
paths (list) – 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.
- write_attr(idx, key, value)¶
Write HDF5 attributes for a group
- write_all_attrs(obj)¶
Write all attributes a given class instance to the HDF5 file
- Parameters:
obj (
pycsa.config.params.params) – write all user-defined parameters to the HDF5 file for reproducibility of the output
- class pycsa.core.io.nc_writer(params, sfx='')¶
Write per-cell CSA results to a chunked NetCDF4 output file.
Each cell is stored as its own NetCDF group keyed by cell id, holding the land mask, center coordinates and (for land cells) the picked spectral amplitudes and wavenumbers. The writer is safe to re-instantiate against an existing chunk file: completed cell groups are skipped on resume rather than overwritten.
- __init__(params, sfx='')¶
Build the output filename and initialize the NetCDF file.
The output path is derived from
params.fn_outputwith the optionalsfxsuffix appended and a.ncextension ensured, placed under adatasets/subdirectory ofparams.path_output(created if absent). If the target file already exists the method returns early without truncating it, so re-instantiating the writer for a later memory batch preserves cells written by earlier batches. Otherwise a freshNETCDF4file is created, every non-Noneattribute ofparamsis written as a global attribute (Booleans coerced toint), and thenspecdimension is added.- Parameters:
params (
pycsa.config.params.params) – user-defined run parameters supplying output paths, mode count and writer settingssfx (str or int, optional) – suffix appended to the base output filename to distinguish chunk files, by default
""
- output(id, clat, clon, is_land, analysis=None, topo_mean=None, topo_peak=None)¶
Write a single cell’s result to its NetCDF group.
Creates (or opens) the group named
idand stores the land mask and center coordinates, the mean elevation whentopo_meanis given, and the zero-padded spectral amplitudes and wavenumbers when ananalysisis given. If a complete group already exists the method returns without rewriting it.- Parameters:
clat (float) – cell-center latitude
clon (float) – cell-center longitude
analysis (
pycsa.data.results.analysis, optional) – spectral analysis result supplyingdk,dl,ampls,kksandlls; if None only mask and coordinates are written, by default Nonetopo_mean (float, optional) – mean cell elevation subtracted before analysis, by default None
- Raises:
RuntimeError – If the cell group already exists but fails the completeness check, indicating a partial write from a previous run.
- duplicate(id, struct)¶
Write one cell to its NetCDF group from a
grp_structrecord.Like
output(), but reads all fields from a pre-assembledstructinstead of individual arguments, and additionally writescell_areawhen the struct carries it. Completed groups are skipped on resume.- Parameters:
struct (
pycsa.core.io.nc_writer.grp_struct) – record holding the cell’s mask, coordinates, optionalcell_area/topo_meanand, for land cells, the spectral fieldsdk,dl,ampls,kksandlls
- Raises:
RuntimeError – If the cell group already exists but fails the completeness check, indicating a partial write from a previous run.
- duplicate_all(data)¶
Write a whole sequence of cell records to the output file.
Iterates over
data(with a progress bar) and writes each record to a NetCDF group keyed by its enumeration index, storing the mask, coordinates, optional mean elevation and, for land cells, the zero-padded spectral fields. Unlikeduplicate(), no completeness check is performed and groups are written unconditionally.- Parameters:
data (sequence of
pycsa.core.io.nc_writer.grp_struct) – ordered collection of cell records; each record’s position in the sequence becomes its NetCDF group name
- static read_dat(path, fn, id, struct)¶
Populate
structfrom one cell group in a NetCDF chunk file.Opens the file
path + fn, reads the land mask and center coordinates of groupidintostruct, and for land cells also reads the spectral fieldsdk,dl,H_spec,kksandlls.- Parameters:
path (str) – directory containing the chunk file
fn (str) – chunk filename appended to
pathstruct (
pycsa.core.io.nc_writer.grp_struct) – record populated in place with the cell’s data
- Returns:
Falseif the file could not be opened, otherwiseTrueoncestructhas been populated.- Return type:
- class grp_struct(c_idx, clat, clon, is_land, analysis=None, cell_area=None, topo_mean=None, topo_peak=None)¶
Lightweight container holding one cell’s writable result fields.
Bundles the per-cell metadata and spectral results so that a whole run can be assembled in memory and later written out via
nc_writer.duplicate()ornc_writer.duplicate_all().- __init__(c_idx, clat, clon, is_land, analysis=None, cell_area=None, topo_mean=None, topo_peak=None)¶
Store cell metadata and copy across any analysis results.
The spectral attributes (
dk,dl,ampls,kks,lls) default toNoneand are overwritten by copying every attribute ofanalysisonto this instance when ananalysisis supplied.- Parameters:
clat (float) – cell-center latitude
clon (float) – cell-center longitude
analysis (
pycsa.data.results.analysis, optional) – spectral analysis result whose attributes are copied onto this struct, by default Nonecell_area (float, optional) – area of the ICON grid cell, by default None
topo_mean (float, optional) – mean cell elevation subtracted before analysis, by default None
topo_peak (float, optional) – peak cell elevation above the cell mean (obstacle height for the MS-GWaM Long number), by default None
- class pycsa.core.io.reader(fn)¶
Simple reader class to read HDF5 output written by
pycsa.core.io.writer- get_params(params)¶
Get the user-defined parameters from the HDF5 file attributes
- Parameters:
params (
pycsa.config.params.params) – empty instance of the user-defined parameters class to be populated
- read_data(idx, name)¶
Read a particular dataset
namefrom a groupidx
- read_all(idx, cell)¶
Populate
cellwith the datasets listed inself.namesfrom a groupidx- Parameters:
cell (
pycsa.data.cell.topo_cell) – empty instance of a cell object to be populated
- pycsa.core.io.fn_gen(params)¶
Automatically generates HDF5 output filename from
pycsa.config.params.params.- Parameters:
params (
pycsa.config.params.params) – instance of the user parameter class- Returns:
automatically generated filename
- Return type: