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

Parsing delta_t to DatePeriod? #8

Closed
rafaqz opened this issue Aug 14, 2020 · 4 comments
Closed

Parsing delta_t to DatePeriod? #8

rafaqz opened this issue Aug 14, 2020 · 4 comments

Comments

@rafaqz
Copy link
Member

rafaqz commented Aug 14, 2020

I would like to get time periods in julia DatePeriod object like Month(1) from netcdf time dimension delta_t fields like "0000-01-00 00:00:00". This would give correct time ranges in GeoData.jl.

Are there tools here that would already do that, or get some of the way?

@rafaqz rafaqz closed this as completed Aug 28, 2020
@rafaqz
Copy link
Member Author

rafaqz commented Sep 9, 2020

BTW I implemented this with something like:

function get_period(index, attrs)
    if haskey(attrs, "delta_t")
        period = parse_period(attrs["delta_t"])
    elseif haskey(attrs, "avg_period")
        period = parse_period(attrs["avg_period"])
    else
        nothing
    end
end

function parse_period(period_str::String)
    regex = r"(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)"
    mtch = match(regex, period_str)
    if mtch isa Nothing
        nothing
    else
        vals = Tuple(parse.(Int, mtch.captures))
        periods = (Year, Month, Day, Hour, Minute, Second)
        if length(vals) == length(periods)
            sum(map((p, v) -> p(v), periods, vals))
        else
            nothing
        end
    end
end

Witch returns a DatePeriod or Nothing.

It could go here if you want.

@rafaqz rafaqz reopened this Sep 9, 2020
@Alexander-Barth
Copy link
Member

Is this part of the CF conventions? If yes, can you share a link that I can learn about it?
Thanks!

@rafaqz
Copy link
Member Author

rafaqz commented Sep 11, 2020

No sorry I reallised it's not actually CF, its CDC. It's just fairly common in netcdf files I use.

https://psl.noaa.gov/data/gridded/conventions/cdc_netcdf_standard.shtml

CF conventions don't seem to specify how to get/set the interval between time points, or at least I haven't been able to find anything clear. So to get the timespan in months you would have to use some heuristics to work out the closest interval to the points in the series, which is pretty awful, but guess I will have to write it at some stage. The difficult part is the spacing doesn't have to be equal so you need to check every date in the index to make sure it roughly e.g. month spaced for months in whatever the calendar is.

But when you know the step size in julia DatePeriod the index is a lot easier to work with - especially when the values are meant to be averages over the interval, and the point is just the middle of the month or 3hour period, etc.

I think with CF the idea is that there is another vector that holds the time bounds, so that each interval is self contained. But I've never seen an actual file that had that in it.

@Alexander-Barth
Copy link
Member

Thanks, but the aim for this package to just implement the CF conventions.

This issue was closed.
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

No branches or pull requests

2 participants