Source code for pycif.plugins.datastreams.fluxes.GCP_1x1.write
import datetime
import pandas as pd
[docs]
def write(self, name, flx_file, flx, mode="a", metadata=None, **kwargs):
"""Write flux to GCP template files.
Args:
self (Fluxes): the Fluxes plugin
flx_file (str): the file where to write fluxes
flx (xarray.Dataset): fluxes data to write
"""
ds = flx.squeeze("lev").to_dataset(name='fch4')
# Add longitudes and latitudes
domain = metadata["domain"]
ds = ds.assign({
"longitude": (["longitude"], domain.zlon[0]),
"latitude": (["latitude"], domain.zlat[:, 0])
})
# -- Write variable attributes
ds['fch4'].attrs['units'] = 'kgCH4/m2/s'
# -- Write dataset attributes
ds.attrs['Time period'] = '{} to {}'.format(
pd.to_datetime([flx.time.min().values]).strftime("%Y-%m-%d")[0],
pd.to_datetime([flx.time.max().values]).strftime("%Y-%m-%d")[0]
)
if hasattr(self, "contact_person"):
ds.attrs['Contact'] = "{} ({})".format(
self.contact_person, self.contact_mail)
ds.attrs['created'] = datetime.datetime.now().strftime(
'by {} on %B %d, %Y'.format(self.contact_person))
if hasattr(self, "References"):
ds.attrs['References'] = self.References
if hasattr(self, "dump_longname"):
ds.attrs['Title'] = self.dump_longname
ds['fch4'].attrs['title'] = \
'Total CH4 flux from {}'.format(self.dump_longname)
ds.to_netcdf(flx_file)