Templating files with Jinja
esm_runscripts supports jinja templating. This is desirable because some times
we might want that esm_runcripts “edits” some source files based on parameters
defined in the configuration yamls or in its current scope.
For example, in the following domain_def.xml file:
<domain_definition>
<!-- Definition of the native domain of the model -->
<domain id="reduced_gaussian" long_name="reduced Gaussian grid" type="gaussian" />
<!-- Definition of regular Gaussian domains -->
<domain_group id="regular_domains" type="rectilinear" >
<domain id="regular" long_name="regular grid" ni_glo="{{xios.ni_glo}}" nj_glo="{{xios.nj_glo}}" >
<generate_rectilinear_domain />
<interpolate_domain order="1" write_weight="true" />
</domain>
</domain_group>
</domain_definition>
we want that ni_glo and nj_glo get the values from the parameters
xios.ni_glo and xios.nj_glo defined in
esm_tools/configs/components/xios/xios.yaml.
This is achieved in esm_runscripts by:
Having a source file where it’s name finishes with
.j2, and that uses the syntax in the example above (including the ESM-Tools parameters to be substituted within double curly braces{{ }}), or using any otherjinjasyntax (read more aboutjinjasyntax in the jinja’s documentation).Including that file as a
sourcein any of the configuration yamls involved in the simulation. For example, to use thenamelists/oifs/43r3/xios/domain_def.xml.j2template to generate thedomain_def.xmlfile, we would include it in theesm_tools/configs/components/xios/xios.yamlas follows (the same way we include any file that must be copied/moved/linked to the working directory, i.e., File Dictionaries):config_sources: domain_def: ${xml_dir}/domain_def.xml.j2 config_in_work: domain_def: domain_def.xml
Warning
If a template’s name does not end with .j2, it will be copied as is, without any
substitution.
Note
As with any other file dictionary, you can ommit the target (in this case
config_in_work.domain_def) and the file will be copied to the target directory
with the same name as the source file, except that for jinja files, the .j2 will
be removed.