Source code for pycif.plugins.models.wrfchem.perturb_model
import copy
from logging import warn
[docs]
def perturb_model(self, nsamples, transf_mapper):
"""Extend the WRF-Chem chemistry scheme to accommodate ensemble members.
Creates ``nsamples`` copies of each active species using the
``__sample#NNN`` naming convention, then removes the original un-suffixed
species.
.. note::
This function currently emits debug warnings (``warn``) for
diagnostic purposes.
Args:
self: WRF-Chem model plugin instance.
nsamples (int): number of ensemble members.
transf_mapper (dict): unused; kept for API consistency.
"""
warn(nsamples, transf_mapper)
# Perturb active species in the chemical scheme
list_acspecies = copy.deepcopy(self.chemistry.acspecies.attributes[:])
warn(f"list_acspecies = {list_acspecies}")
for spec in list_acspecies:
for i in range(nsamples):
spec_sample = f"{spec}__sample#{i:03d}"
spec_plg = getattr(self.chemistry.acspecies, spec)
setattr(self.chemistry.acspecies, spec_sample, spec_plg)
self.chemistry.acspecies.attributes.append(spec_sample)
delattr(self.chemistry.acspecies, spec)
self.chemistry.acspecies.attributes.remove(spec)
# Perturb emis species in the chemical scheme
list_emispecies = copy.deepcopy(self.chemistry.emis_species.attributes[:])
warn(f"list_emispecies = {list_emispecies}")
for spec in list_emispecies:
for i in range(nsamples):
spec_sample = f"{spec}__sample#{i:03d}"
spec_plg = getattr(self.chemistry.emis_species, spec)
setattr(self.chemistry.emis_species, spec_sample, spec_plg)
self.chemistry.emis_species.attributes.append(spec_sample)
delattr(self.chemistry.emis_species, spec)
self.chemistry.emis_species.attributes.remove(spec)
# Dump updated chemical scheme
# self.chemistry.create_chemicalscheme()