Arithmetic with $CYLC_TASK_CYCLE _POINT in standard date/time format

Hi Jonny,

Firstly regarding the first step, grabbing the year only from the full datetime. If you are using Cylc with Rose, you can do this in a more explicit way than the bash pipe you state (though there is otherwise no problem with that) which was designed for doing such datetime manipulation, the ‘rose date’ command (see also here), which can format datetimes. Use the 'print-format- option, like so:

rose date ${CYLC_TASK_CYCLE_POINT} --print-format='%Y'

To use this in a suites script, wrap it in a $( ... ) construct.

For performing the arithmetic, it should just be a case of applying bash arithmetic, where I’m using some arbitarry integers as an example here:

expr <1950 calcuated via the above> \* 70 \* 80 / 4

Note you need to escape the asterisk so it is not interpreted as a wildcard character by the shell. So, overall this should work (replace with your bash solution for 1950 if you don’t want to use Rose):

expr $(rose date ${CYLC_TASK_CYCLE_POINT} --print-format='%Y') \* 70 \* 80 / 4

You could also use Jinja2 to do the trimming of all but the year (using string indexing [:4] for a sub-string, exactly as for Python) & the arithmetic, but since you have put some bash it seems you want to use this directly in your task script, & with Jinja2 you would have to convert into the equivalent environment variable to use in the script, so it is a more contrived solution. However, if you want to extend your current logic significantly to do further arithmetic, Jinja2 might be the way to go as it is very powerful. [UPDATE: this is incorrect; Jinja2 is processed at start-up, whereas CYLC_TASK_CYCLE_POINT is a job environment variable set at run time - HO]

Hope that helps!

1 Like