esm-tools / esm_tools

Simple Infrastructure for Earth System Simulations
https://esm-tools.github.io/
GNU General Public License v2.0
25 stars 12 forks source link

feat/jinja templates #1187

Open mandresm opened 1 month ago

mandresm commented 1 month ago

After this PR, files that are handled by ESM-Tools can be jinja2 templates and will be rendered at the time of the copying.

This was a request by @JanStreffing to be able to handle variable substitution in xml files. Jan, could you test it? You can do your xml and yaml modifications in a new branch departing from this one. We can merge in release that branch right after this one gets merged, and there should be no conflicts.

How does it work

ESM-Tools automatically tries to resolve jinja templates during movements. It searches for sources finishing with .j2 and then it knows that instead of copying/linking/moving it needs to render the template and produce the new target file. We pass config to jinja, that means you just need to specify the variable you want to subsitute in the right format with the full path separated by points:

xios.yaml

config_sources:
-        domain_def: ${xml_dir}/domain_def.xml
+        domain_def: ${xml_dir}/domain_def.j2
        field_def: ${xml_dir}/field_def.xml
        file_def: ${xml_dir}/file_def.xml

domain_def.xml

<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>

or with python dict syntax

<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>

TODO

mandresm commented 3 weeks ago

Document that tarjet changes extension

JanStreffing commented 3 weeks ago

Looks good from the Python side, still needs user-testing (@JanStreffing) and documentation in the handbook. Nice work! 👍🏻

I hope to give this a try tomorrow. Maybe I just drop by one of your offices?

pgierz commented 3 weeks ago

Sure, I'll be around tomorrow