Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template function to offset datetime parameter #538

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

leifdenby
Copy link
Contributor

@leifdenby leifdenby commented Oct 9, 2020

I have a opendap server where the files are stored with a time-span in the filename which is always one hour apart, for example
LIDAR_obs_09291300_09291400.nc
i.e. the format is
LIDAR_obs_{{t_start.strftime("%m%d%H%M")}}_{{t_end.strftime("%m%d%H%M")}}.nc

It would be nice to have the user just provide t_start (which I can then check for validity) and then calculate t_end from it (t_end = t_start + datetime.timedelta(hours=1)). I appreciate that allowing for custom functions would open a whole can of worms, but maybe adding a specialised jinja template function for offsetting datetimes might be useful?

I've created one called offset_datetime, with that the above URL can be written as:
LIDAR_obs_{{t_start.strftime("%m%d%H%M")}}_{{offset_datetime(t_start, hours=1).strftime("%m%d%H%M")}}.nc

Would this be something you would consider adding to intake?

Adds `offset_datetime` template function with the same call signature as
`datetime.timedelta` enabling the offsetting of datetime parameters in
URL construction. For example with a datetime parameter `t_start` the
template URL could be
`foobar_{{t_start.strftime("%m%d")}}_{{offset_datetime(t_start, days=1).strftime("%m%d")}}`
to create URLs which contain a time-span.
@martindurant
Copy link
Member

I am personally uncertain. I can see the benefit, obviously - and indeed there could be many other utility functions that might be handy. I'm not sure how one would go about specifying the set to include - we would not generally want to allow arbitrary code execution during template filling.

Perhaps you could tag one or two of the maintainers on intake-xarray to see if someone else already has a solution for this kind of thing or has thoughts on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants