NetCDFWriter#
- class fridom.framework.modules.netcdf_writer.NetCDFWriter(write_interval: timedelta64 | float, filename: str = 'snap', start_time: datetime64 | float = 0, end_time: datetime64 | float | None = None, restart_interval: timedelta64 | float | None = None, snap_slice: tuple | None = None, directory: str | None = None, get_variables: callable | None = None)[source]#
Bases:
ModuleWriting model output to NetCDF files.
Parameters#
- write_intervalnp.timedelta64 | float
The interval at which the data should be written to the file.
- filenamestr, optional
The name of the file to write to. Default is “snap” (no directory).
- directorystr, optional
The directory where the files should be stored. Default is “snapshots”.
- start_timenp.datetime64, optional
The time at which the first file should be written. Default is
- end_timenp.datetime64, optional
The time at which the last file should be written. Default is None.
- restart_intervalnp.timedelta64, optional
The interval at which a new file should be created. Default is None.
- snap_slicetuple, optional
The slice of the grid that should be written to the file. Default is None.
- get_variablescallable, (default: None)
A function that returns a list of field variables that should be written to the file. If None, all fields of the State object will be written. The function signature of get_variables is: get_variables(mz: ‘ModelState’) -> list[FieldVariable]
Examples#
The following example shows how to create a netCDF output from a nonhydrostatic model using the SingleWave initial condition.
import fridom.nonhydro as nh import numpy as np import matplotlib.pyplot as plt # create a netCDF writer that outputs u, v, w, b, and p nc_writer = nh.modules.NetCDFWriter( get_variables = lambda mz: [mz.u, mz.v, mz.w, mz.b, mz.z_diag.p], write_interval = np.timedelta64(1, 's')) # create the model grid = nh.grid.cartesian.Grid( N=[128]*3, L=[1]*3, periodic_bounds=(True, True, True)) mset = nh.ModelSettings(grid=grid, dsqr=0.02, Ro=0.0) mset.time_stepper.dt = np.timedelta64(10, 'ms') # add the netCDF writer to the diagnostics mset.diagnostics.add_module(nc_writer) mset.setup() z = nh.initial_conditions.SingleWave(mset, kx=2, ky=0, kz=1) model = nh.Model(mset) model.z = z model.run(runlen=np.timedelta64(10, 's'))
- __init__(write_interval: timedelta64 | float, filename: str = 'snap', start_time: datetime64 | float = 0, end_time: datetime64 | float | None = None, restart_interval: timedelta64 | float | None = None, snap_slice: tuple | None = None, directory: str | None = None, get_variables: callable | None = None)[source]#
Methods
__init__(write_interval[, filename, ...])disable()Enabling the module means that it will be executed at each time step.
enable()Enabling the module means that it will be executed at each time step.
is_enabled()Return whether the module is enabled or not.
reset()Stop and start the module.
setup(mset)Start the module
start()Start the module
stop()Stop the module
update(mz)Update the module
Attributes
diff_moduleThe differentiation module to be used by this module.
gridThe grid of the model settings
infoReturn a dictionary with information about the time stepper.
interp_moduleThe interpolation module to be used by this module.
msetThe model settings
required_haloExamples using
fridom.framework.modules.NetCDFWriter#- name = 'NetCDFWriter'#
- setup(mset: ModelSettingsBase) None[source]#
Start the module
Description#
This method is called by the ModelSettings.setup() and sets the ModelSettings as well as the differentiation and interpolation modules.
- start()[source]#
Start the module
Description#
This method is called at the beginning of the model run. Child classes that require a start method (for example to start an output writer) should overwrite this method. Make sure to decorate the method with the @module_method decorator.
- stop()[source]#
Stop the module
Description#
This method is called by the model at the end of the model run or when the model is reset. Child classes that require a stop method (for example to close an output file) should overwrite this method. Make sure to decorate the method with the @module_method decorator.
- update(mz: ModelState) ModelState[source]#
Update the module
Description#
This method is called by the model at each time step. Child classes should overwrite this method to update the module. Make sure to decorate the method with the @module_method decorator.
Parameters#
- mzfr.ModelState
The model state at the current time step.
Returns#
- fr.ModelState
The updated model state.
- log_level: fr.config.LogLevel | None#
- timer: fr.timing_module.TimingModule | None#