Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
CLIMATE-938 Unify format of the time information from a netCDF file.
  • Loading branch information
MichaelArthurAnderson committed Mar 4, 2018
1 parent 4807c4b commit 38fedad3c79bed9cf9feea7bdaee158008c07701
Showing 1 changed file with 11 additions and 6 deletions.
@@ -504,20 +504,25 @@ def temporal_slice(target_dataset, start_time, end_time):
:raises: ValueError
'''

# https://issues.apache.org/jira/browse/CLIMATE-938
# netCDF datetimes allow for a variety of calendars while Python has
# only one. This would throw an error about a calendar mismatch when
# comparing a Python datetime object to a netcdf datetime object.
# Cast the date as best we can so the comparison will compare like
# data types This will still throw an excdeption if the start / end date are
# not valid in given calendar. February 29th in a DatetimeNoLeap calendar for example.
slice_start_time =\
type(target_dataset.times.item(0))(start_time.year, start_time.month, start_time.day,
start_time.hour, start_time.minute, start_time.second)
slice_start_time = start_time
slice_end_time = end_time

if isinstance(target_dataset.times.item(0), netCDF4.netcdftime._netcdftime.datetime):
slice_start_time =\
type(target_dataset.times.item(0))(start_time.year, start_time.month, start_time.day,
start_time.hour, start_time.minute, start_time.second)

slice_end_time =\
type(target_dataset.times.item(0))(end_time.year, end_time.month, end_time.day,
end_time.hour, end_time.minute, end_time.second)
slice_end_time =\
type(target_dataset.times.item(0))(end_time.year, end_time.month, end_time.day,
end_time.hour, end_time.minute, end_time.second)

start_time_index = np.where(
target_dataset.times >= slice_start_time)[0][0]

0 comments on commit 38fedad

Please sign in to comment.