Source code for pycif.plugins.datastreams.backgrounds.tm5_background.write
from .....utils.classes.fluxes import Flux
import numpy as np
import os
import xarray as xr
from .....utils.hdf5 import _hdf5_lock
[docs]
def write(self, prescr_file, prescr, typefile, mode="a", **kwargs):
"""Write prescribed species background fields for LMDZ.
Args:
self (Fluxes): the Fluxes plugin.
prescr_file (str): the file where to write the prescribed field.
prescr (xarray.Dataset): prescribed species data to write. For
``typefile == "bin"``, must expose ``"fwd"`` and ``"tl"``
data variables.
typefile (str): output format, either ``"bin"`` for a raw binary
file (``fwd``/``tl`` arrays transposed and dumped with
``tofile``) or ``"nc"`` for a NETCDF3_CLASSIC file.
mode (str): unused; kept for interface consistency. NetCDF output
is always merged into any existing file, since xarray's
append mode is not supported here.
"""
if typefile == 'bin':
prescr_fwd = prescr["fwd"].values
prescr_tl = prescr["tl"].values
np.transpose([prescr_fwd, prescr_tl], axes=(0, 4, 3, 2, 1)).T.tofile(prescr_file)
if typefile == 'nc':
# option mode = 'a' does not work -> do it "manually"
if os.path.exists(prescr_file):
with _hdf5_lock:
ds = xr.open_dataset(prescr_file)
append_data = xr.merge([ds, prescr])
append_data.to_netcdf(prescr_file, format="NETCDF3_CLASSIC", mode = 'w')
else:
with _hdf5_lock:
prescr.to_netcdf(prescr_file, format="NETCDF3_CLASSIC", mode = 'w')