Skip to content

Conversation

@tdh2005
Copy link

@tdh2005 tdh2005 commented Sep 18, 2025

Fixes #1701

This update modifies 3 files; constraints.py, test_constraints.py and test.read.py.

Modifications:

  1. Constraints.py: Commits modifying this file have added the function 'my_pdt_fromstring()' which parses out am ISO date string into temporal coordinates (year, month, day, hour, minute and second) to return a PartialDateTime object to replicate the 'fromisoformat()' method. The function for 'generate_time_constraint()' is also modified such to use 'my_pdt_fromstring()' rather than 'fromisoformat()'. This has therefore allowed for the ability to process files with more idealised calendars included in CF Metadata convention (eg. 360 day).

  2. test_constraint.py: A series of test functions have been written to test the 'my_pdt_fromstring()' function added to constraints.py. These verify the function is able to correctly parse a given date string and return a PartialDateTtime.

  3. test_read.py: A test function named 'test_read_cubes_generate_time_constraint()' verifies that a cube can be read and correctly constrained along the time coordinate.

NOTE: the test function added to test_read.py uses a NetCDF file that has not been included in the pull request. The file in question has a size of 24 KB, similar to other data files included within the test_data folder and uses a 360 day calendar. We would like to further ask for further approval to add this.

@tdh2005 tdh2005 requested a review from jfrost-mo September 18, 2025 15:39
Copy link
Member

@jfrost-mo jfrost-mo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a sensible approach. I've suggested some additional date strings to add as tests, which I think will require some changes to your date parser.

Copy link
Member

@jfrost-mo jfrost-mo Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to support the full range of ISO 8601 dates. Additional test cases to add:

  • Basic format: 20250924
  • Basic format with time: 20250924T0921Z
  • Month precision: 2025-09
  • Microseconds: 2025-09-24T09:21:42.123456
  • Alternate UTC representation: 2025-09-24T09:21+00:00
  • Indian Standard Time: 2025-09-24T09:21+05:30

We probably don't need to support week dates (2025-W01) or ordinal dates (2025-365).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are looking good.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be any issue adding a 24 KiB test file. Stick it somewhere under tests/test_data/.

tdh2005 added a commit that referenced this pull request Oct 10, 2025
file name changed in test_read.py file
… test.read.py.

Modifications:

Constraints.py: Commits modifying this file have added the function 'my_pdt_fromstring()' which parses out am ISO date string into temporal coordinates (year, month, day, hour, minute and second) to return a PartialDateTime object to replicate the 'fromisoformat()' method. The function for 'generate_time_constraint()' is also modified such to use 'my_pdt_fromstring()' rather than 'fromisoformat()'. This has therefore allowed for the ability to process files with more idealised calendars included in CF Metadata convention (eg. 360 day).

test_constraint.py: A series of test functions have been written to test the 'my_pdt_fromstring()' function added to constraints.py. These verify the function is able to correctly parse a given date string and return a PartialDateTtime.

test_read.py: A test function named 'test_read_cubes_generate_time_constraint()' verifies that a cube can be read and correctly constrained along the time coordinate.

NOTE: the test function added to test_read.py uses a NetCDF file that has not been included in the pull request. The file in question has a size of 24 KB, similar to other data files included within the test_data folder and uses a 360 day calendar. We would like to further ask for further approval to add this.
Fixes #1701
@tdh2005 tdh2005 force-pushed the operator-constraint-update branch from 052b90a to 41e26e1 Compare October 10, 2025 15:24
@github-actions
Copy link
Contributor

github-actions bot commented Oct 10, 2025

Coverage

Constraints.py: This commit modifies the file to add wider support for offset formats (e.g. hh, hhmm, hh:mm) with the my_pdt_fromstring() function.
constraints.py: Format of pdt output changed for special case YYYY-MM for the my_pdt_fromstring() function.

test_constraints.py: asserts modified for the special case YYYY-MM test to reflect changes to my_pdt_fromstring() function.

test_read.py: More specific asserts added to the test_read_cubes_generate_time_constraint() test to reflect file properties.
@tdh2005 tdh2005 requested a review from jfrost-mo October 14, 2025 13:23
@jfrost-mo jfrost-mo added this to the CSET next milestone Oct 28, 2025
Copy link
Member

@jfrost-mo jfrost-mo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being slow to review this again. Its looking really good, and the test cases are nice and comprehensive.

Because the functionality of creating PartialDateTime objects looks useful, I think it should be moved into _utils.py to make it available to all operators, and renamed to be clearer. The tests will also need moving to test_utils.py

Otherwise it looks ready to go.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are looking good.

Arguments
---------
datestring: str
ISO date
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ISO date
ISO 8601 date.

time = ""
elif len(datetime_split) == 2:
time = datetime_split[1]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

from CSET._common import iter_maybe


def my_pdt_fromstring(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this function being useful for multiple operators. Could it be moved into _utils.py? This also stops it being exposed as a public function via constraints.my_pdt_fromstring.

Furthermore, I think it would be good to rename this function to pdt_fromisoformat, to better describe what it does, and match datetime.fromisoformat.

… test.read.py.

Modifications:

Constraints.py: Commits modifying this file have added the function 'my_pdt_fromstring()' which parses out am ISO date string into temporal coordinates (year, month, day, hour, minute and second) to return a PartialDateTime object to replicate the 'fromisoformat()' method. The function for 'generate_time_constraint()' is also modified such to use 'my_pdt_fromstring()' rather than 'fromisoformat()'. This has therefore allowed for the ability to process files with more idealised calendars included in CF Metadata convention (eg. 360 day).

test_constraint.py: A series of test functions have been written to test the 'my_pdt_fromstring()' function added to constraints.py. These verify the function is able to correctly parse a given date string and return a PartialDateTtime.

test_read.py: A test function named 'test_read_cubes_generate_time_constraint()' verifies that a cube can be read and correctly constrained along the time coordinate.

NOTE: the test function added to test_read.py uses a NetCDF file that has not been included in the pull request. The file in question has a size of 24 KB, similar to other data files included within the test_data folder and uses a 360 day calendar. We would like to further ask for further approval to add this.
Fixes #1701
Constraints.py: This commit modifies the file to add wider support for offset formats (e.g. hh, hhmm, hh:mm) with the my_pdt_fromstring() function.
constraints.py: Format of pdt output changed for special case YYYY-MM for the my_pdt_fromstring() function.

test_constraints.py: asserts modified for the special case YYYY-MM test to reflect changes to my_pdt_fromstring() function.

test_read.py: More specific asserts added to the test_read_cubes_generate_time_constraint() test to reflect file properties.
@tdh2005 tdh2005 force-pushed the operator-constraint-update branch from 3337295 to 5f6c414 Compare October 30, 2025 11:32
…fice/CSET into operator-constraint-update

Updating branch with changes to main branch.
- my_pdt_fromstring() : This function has been renamed to pdt_fromisoformat() such to allign to what it replaces (.fromisoformat()).

- _utils.py : The newly named pdt_fromisoformat() function has been moved from constraints.py into utils.py, such that it can be used in conjunction to multiple different operators.

- test_utils.py : The tests made for the pdt_fromisoformat() function have been moved to test_utils.py, to reflect the relocation of the function from constraints.py to _utils.py.

- test_read.py : This file has been modified to handle the changes to name and location of the pdt_fromisoformat() function.
@tdh2005 tdh2005 requested a review from jfrost-mo October 30, 2025 15:17
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.

Extending constrants.generate_time_constraint to support idealised calendars (eg. 360-day)

3 participants