-
Notifications
You must be signed in to change notification settings - Fork 17
Read time from filename #48
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
Open
jasonmlkang
wants to merge
3
commits into
apache:develop
Choose a base branch
from
jasonmlkang:SDAP-345/read-time-from-filename
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from enum import Enum | ||
| import re | ||
| from functools import reduce | ||
| from datetime import datetime, timedelta | ||
| from typing import Tuple | ||
|
|
||
|
|
||
| class ParseDatetimeUtils: | ||
| def __init__(self, expression: str, sub_str_start: int =None, sub_str_end: int =None, is_range: bool =False): | ||
| self.expression = expression | ||
|
|
||
| self.sub_str_start = sub_str_start | ||
| self.sub_str_end = sub_str_end | ||
| self.is_sub_str = self.sub_str_start is not None and self.sub_str_end is not None | ||
|
|
||
| self.is_range = is_range | ||
|
|
||
| def parse(self, input_str: str) -> Tuple[datetime, datetime]: | ||
| input_str = input_str[self.sub_str_start:self.sub_str_end] \ | ||
| if self.is_sub_str else input_str | ||
|
|
||
| start_dt, end_dt = (None, None) | ||
| if self.is_range: | ||
| input_str_len = len(input_str) | ||
| input_str_mid = int(input_str_len / 2) | ||
| start_dt = datetime.strptime(input_str[0:input_str_mid], self.expression) | ||
| end_dt = datetime.strptime(input_str[input_str_mid+(input_str_len%2):], self.expression) | ||
| else: | ||
| start_dt = datetime.strptime(input_str, self.expression) | ||
| end_dt = start_dt | ||
|
|
||
| return (start_dt, end_dt) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .ParseDatetimeUtils import ParseDatetimeUtils |
42 changes: 42 additions & 0 deletions
42
common/common/parse_datetime_utils_test/ParseDatetimeUtilsTests.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| from parse_datetime_utils import ParseDatetimeUtils | ||
| import unittest | ||
| from datetime import datetime | ||
|
|
||
|
|
||
| class ParseDatetimeUtilsTests(unittest.TestCase): | ||
| def test_parse(self): | ||
| filename = 'V4NA03_PM25_NA_20040115-RH35.nc' | ||
| expression = '%Y%m%d' | ||
| sub_str_start, sub_str_end = (15, 23) | ||
| parser = ParseDatetimeUtils(expression, sub_str_start=sub_str_start, | ||
| sub_str_end=sub_str_end) | ||
| start_datetime, end_datetime = parser.parse(filename) | ||
|
|
||
| self.assertEqual(start_datetime, end_datetime) | ||
| self.assertEqual(end_datetime, datetime(2004, 1, 15)) | ||
|
|
||
| def test_parse_range_with_separator(self): | ||
| filename = 'V5GL01.HybridPM25.NorthAmerica.200706-200908.nc' | ||
| expression = '%Y%m' | ||
| sub_str_start, sub_str_end = (31, 44) | ||
| parser = ParseDatetimeUtils(expression, sub_str_start=sub_str_start, | ||
| sub_str_end=sub_str_end, is_range=True) | ||
| start_datetime, end_datetime = parser.parse(filename) | ||
|
|
||
| self.assertEqual(start_datetime, datetime(2007, 6, 1)) | ||
| self.assertEqual(end_datetime, datetime(2009, 8, 1)) | ||
|
|
||
| def test_parse_range_without_separator(self): | ||
| filename = 'V5GL01.HybridPM25.NorthAmerica.200706200908.nc' | ||
| expression = '%Y%m' | ||
| sub_str_start, sub_str_end = (31, 43) | ||
| parser = ParseDatetimeUtils(expression, sub_str_start=sub_str_start, | ||
| sub_str_end=sub_str_end, is_range=True) | ||
| start_datetime, end_datetime = parser.parse(filename) | ||
|
|
||
| self.assertEqual(start_datetime, datetime(2007, 6, 1)) | ||
| self.assertEqual(end_datetime, datetime(2009, 8, 1)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .ParseDatetimeUtilsTests import ParseDatetimeUtilsTests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want to be careful about the UTC timezone.
For example with code
Or with the example in my comment above:
That would be more simple than the current version of the code and spare some lines of code.
I am not sure if the hardcoded -1 days is also something we would like to have for all the datasets. Maybe an additional optional configuration variable for the collection should give an offset for the min and offset for the max time.
In this case that would be -1 and -1 for both.
I guess the most difficult change in my request is to read from the collection configmap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasonmlkang did you have a chance to look at my comments. We're having similar need for AQACF project but of course the file naming convention is different so we would need to discuss how that could be configured.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Examples of file names from AQACF project:
V5GL01.HybridPM25.NorthAmerica.200807-200807.ncV4NA03_PM25_NA_200401_200412-RH35.nc