def convert_to_std_time(self, time_stamp_info=None): """ Convert this coordinate to standard time. It will use either: the units of the coordinate if it is a cf_units Unit, or the first word of the units, combined with the time stamp (if the timestamp is not given an error is thrown). :param time_stamp_info: the time stamp info from the file, None if it does not exist """ from cis.time_util import convert_time_since_to_std_time, cis_standard_time_unit, \ convert_time_using_time_stamp_info_to_std_time, convert_datetime_to_std_time from cf_units import Unit if isinstance(self.units, Unit): self._data = convert_time_since_to_std_time(self.data, self.units) elif str(self.units).lower().startswith('datetime'): self._data = convert_datetime_to_std_time(self.data) else: if time_stamp_info is None: raise ValueError( "File must have time stamp info if converting without 'since' in units definition" ) self._data = convert_time_using_time_stamp_info_to_std_time( self.data, self.units, time_stamp_info) self.units = cis_standard_time_unit
def check_temporal_subsetting(self, t_min, t_max): import datetime self.ds = Dataset(self.OUTPUT_FILENAME) cis_standard = datetime.datetime(1600, 1, 1, 0, 0, 0) time = self.ds.variables['time'] datetime_min = datetime.datetime.strptime(t_min, "%Y-%m-%dT%H:%M:%S") datetime_max = datetime.datetime.strptime(t_max, "%Y-%m-%dT%H:%M:%S") # Expand the search by a second either way to avoid rounding problems datetime_min -= datetime.timedelta(seconds=1.5) datetime_max += datetime.timedelta(seconds=1.5) time_vals = convert_time_since_to_std_time(time[:], time.units) for time_val in time_vals: delta = datetime.timedelta(days=time_val) datetime_value = cis_standard + delta assert_that(datetime_value, greater_than_or_equal_to(datetime_min)) assert_that(datetime_value, less_than_or_equal_to(datetime_max))
def convert_to_std_time(self, time_stamp_info=None): """ Convert this coordinate to standard time. It will use either: the units of the coordinate if it is in the standard 'x since y' format; or the first word of the units, combined with the time stamp (if the timestamp is not given an error is thrown). :param time_stamp_info: the time stamp info from the file, None if it does not exist """ from cis.time_util import convert_time_since_to_std_time, cis_standard_time_unit, \ convert_time_using_time_stamp_info_to_std_time if "since" in self.units: self._data = convert_time_since_to_std_time(self.data, self.units) else: if time_stamp_info is None: raise ValueError("File must have time stamp info if converting without 'since' in units definition") self._data = convert_time_using_time_stamp_info_to_std_time(self.data, self.units, time_stamp_info) self.units = str(cis_standard_time_unit) self.metadata.calendar = cis_standard_time_unit.calendar
def convert_to_std_time(self, time_stamp_info=None): """ Convert this coordinate to standard time. It will use either: the units of the coordinate if it is a cf_units Unit, or the first word of the units, combined with the time stamp (if the timestamp is not given an error is thrown). :param time_stamp_info: the time stamp info from the file, None if it does not exist """ from cis.time_util import convert_time_since_to_std_time, cis_standard_time_unit, \ convert_time_using_time_stamp_info_to_std_time, convert_datetime_to_std_time from cf_units import Unit if isinstance(self.units, Unit): self._data = convert_time_since_to_std_time(self.data, self.units) elif str(self.units).lower().startswith('datetime'): self._data = convert_datetime_to_std_time(self.data) else: if time_stamp_info is None: raise ValueError("File must have time stamp info if converting without 'since' in units definition") self._data = convert_time_using_time_stamp_info_to_std_time(self.data, self.units, time_stamp_info) self.units = cis_standard_time_unit