Source code for pycif.plugins.models.wrfchem.io.inputs.make_fluxes
import os
import pandas as pd
from ......utils import path
[docs]
def make_fluxes(self, datastore, runsubdir, datei, datef, mode):
"""Write fluxes to WRF flux input files
Args:
self (pycif.utils.classes.fluxes.Flux): Flux plugin with all
attributes
datastore (dict): information on flux species
runsubdir (str): directory of the current run
datei (datetime.datetime)
datef (datetime.datetime)
mode (str): running mode: 'fwd', 'tl' or 'adj'
"""
# Based on models/chimere/io/inputs/fluxes.py from 2021-09-21
datastore = {
trid: datastore[trid]
for trid in datastore
if trid[0] in ["flux"]
}
list_trid = [("flux", spec)
for spec in self.chemistry.emis_species.attributes]
for trid in list_trid:
spec = trid[1]
emis_type = trid[0]
flx_plg = self.flux
# freum 2021-11-02: According to Antoine, can only use flx_plg
# to access methods, not parameters in the yaml. Full plugin
# with access to parameters in yaml would be: tracer["tracer"].
tracer = datastore[trid]
tracer_data = tracer["data"][datei]
# freum: I don't know what this catches. Comment and wait for errors
# if trid in datastore:
# pass
#
# # If spec not explicitly defined in datastore,
# # fetch general component information if available
# elif trid not in datastore and (emis_type, "") in datastore:
# trid = (emis_type, "")
# else:
# continue
# If no data is provided, just copy from original file
# freum: I think this is the case where it's not being optimized,
# because the files are linked
if "spec" not in tracer_data:
print("spec not in tracer_data")
# WARNING: this point to external files.
# TODO: in init_component, we should add a loop to change the
# root directory of fetched files to the local directory
for file_in in tracer["input_files"][datei]:
file_out = f"{runsubdir}/{os.path.basename(file_in)}"
path.link(file_in, file_out)
else:
flx = tracer_data["spec"]
# Variable name: this relies on naming convention in model_sources/wrfchem/add_tracers.bash
varname = "E_" + getattr(getattr(self.chemistry.acspecies, spec), "varname")
for time in flx.time.values:
date = pd.to_datetime(time)
fn_out = date.strftime("wrfchemi_d01_%Y-%m-%d_%H:%M:%S")
fp_out = f"{runsubdir}/{fn_out}"
rw_mode = "a" if os.path.exists(fp_out) else "w"
# Write to file
flx_plg.write(varname,
fp_out,
date,
flx.loc[time].values,
self.domain,
rw_mode)