Using Jinja2 filter to convert to julian date

I have a simple python script which I call d2j.py to convert a date into a julian date. I put it in a directory called Jinja2Filters as in the documentation.
I have a simple test workflow which gives me an UndefinedError which I think is about d2j not being defined.

#!Jinja2

{% set CYCLE_START_DATE = '20240801' %}
{% set CYCLE_END_DATE = '20240802' %}
{% set JUL_START_DATE = d2j(CYCLE_START_DATE) %}
{% set JUL_END_DATE = d2j(CYCLE_END_DATE) %}

[scheduling]
    initial cycle point = {{CYCLE_START_DATE}}
    final cycle point = {{CYCLE_END_DATE}}

    [[graph]]
        P1D = """
            a
        """

[runtime]

   [[a]]
        script = """
        echo "start ${CYLC_WORKFLOW_INITIAL_CYCLE_POINT}"
        echo "stop ${CYLC_WORKFLOW_FINAL_CYCLE_POINT}"
        echo "start {{ CYCLE_START_DATE }}"
        echo "stop {{ CYCLE_STOP_DATE }}"
        echo "start {{ JUL_START_DATE }}"
        echo "stop {{ JUL_STOP_DATE }}"

I think that you might want something more like

{% set JUL_START_DATE = CYCLE_START_DATE | d2j %}
{% set JUL_END_DATE = CYCLE_END_DATE | d2j %}

Once it validates use cylc view <path> -j to inspect the processed config.

I suspect that you’re going to have a few more jinja2 issues to fix because you define CYCLE_END_DATE and JUL_END_DATE but try to use CYCLE_STOP_DATE and JUL_STOP_DATE.

Yep, besides the PEBKAB of “STOP” instead of “END”, I ended up putting the script in python/lib and importing it. It just does not seem to find the script in Jinja2Filters

I got it working, but my filter is in a file with the same name as the filter:

/var/tmp/cylc-src/fine.lime
|-- Jinja2Filters
|   `-- d2j.py
`-- flow.cylc
> cat /var/tmp/cylc-src/fine.lime/Jinja2Filters/d2j.py 
def d2j(x):
    return x + ' HELLO WORLD'

Yes, I got it to work as well. Good to have two options. Cheers!

1 Like