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 "months since ..." dates #304

Closed
davidhassell opened this issue Jul 25, 2023 · 4 comments
Closed

Parsing "months since ..." dates #304

davidhassell opened this issue Jul 25, 2023 · 4 comments

Comments

@davidhassell
Copy link
Contributor

Hi,

Following on from #68, it turns out that you can't now parse dates of "months since ..." in the Gregorian calendar:

>>> import cftime
>>> cftime.__version__
'1.6.2'
>>> cftime._dateparse('months since 2000-1-1', 'gregorian')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[17], line 1
----> 1 cftime._dateparse('months since 2000-1-1', 'gregorian')

File src/cftime/_cftime.pyx:98, in cftime._cftime._dateparse()

ValueError: 'months since' units only allowed for '360_day' calendar

Was this intended?

This makes it hard to read datasets like:

	float32 T(T) ;
		T:standard_name = "time" ;
		T:calendar = "standard" ;
		T:units = "months since 1960-01-01" ;

       data:
             T = 0, 1, 2, 3

which exist, even if the creators probably didn't really mean what they encoded (although they might have!).

The lines https://github.com/Unidata/cftime/blob/master/src/cftime/_cftime.pyx#L97-L100 seem to restrictive to me. I'm fine with raising an exception when you try do something in these units (like date2num), but I think that it might be too restrictive to prevent these units from being stored in the first place.

Thanks,
David

@jswhit
Copy link
Collaborator

jswhit commented Jul 28, 2023

cftime._dateparse is not part of the public api, it's used internally in num2date and date2num. So, unless I'm missing something, you shouldn't ever see this error unless you try to do something with these units.

@jswhit
Copy link
Collaborator

jswhit commented Jul 28, 2023

Having said that, if you are using cftime._dateparse and don't want it to fail on invalid units I see no harm in removing that error - it will be triggered later in date2num and num2date anyway.

@davidhassell
Copy link
Contributor Author

Thanks - that would certainly help me! I appreciate that it's an underscore method, but it is very useful - converting CF-netCDF units attributes to datetime objects is something I do frequently, and I don't know of another method for doing it.

@davidhassell
Copy link
Contributor Author

Ah, but looking at my code, it may be that I don't have to rely on this method as much as I perhaps thought ... I shall investigate :)

@jswhit jswhit closed this as completed Jan 24, 2024
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