def _normalize_datetime( val: Union[pd.Timestamp, str, np.datetime64, datetime.datetime, datetime.date] ) -> Tuple[np.datetime64, np.timedelta64]: ''' Break up a datetime into numpy date and numpy timedelta. Args: val: The datetime to normalize. Can be an array or a single datetime as a string, pandas timestamp, numpy datetime or python date or datetime >>> print(_normalize_datetime(pd.Timestamp('2016-05-01 3:55:00'))) (numpy.datetime64('2016-05-01'), numpy.timedelta64(14100000000000,'ns')) >>> print(_normalize_datetime('2016-05-01')) (numpy.datetime64('2016-05-01'), numpy.timedelta64(0,'D')) >>> x = pd.DataFrame({'x' : [np.datetime64('2015-01-01 05:00:00'), np.datetime64('2015-02-01 06:00:00')]}) >>> print(_normalize_datetime(x.x)) (array(['2015-01-01', '2015-02-01'], dtype='datetime64[D]'), array([18000000000000, 21600000000000], dtype='timedelta64[ns]')) >>> x = pd.DataFrame({'x' : [1, 2]}, index = [np.datetime64('2015-01-01 05:00:00'), np.datetime64('2015-02-01 06:00:00')]) >>> print(_normalize_datetime(x.index)) (array(['2015-01-01', '2015-02-01'], dtype='datetime64[D]'), array([18000000000000, 21600000000000], dtype='timedelta64[ns]')) ''' if isinstance(val, pd.Timestamp): datetime = val.to_datetime64() elif isinstance(val, pd.Series) or isinstance(val, pd.DatetimeIndex): datetime = val.values elif isinstance(val, np.ndarray) and np.issubdtype(val.dtype, np.datetime64): datetime = val else: datetime = np.datetime64(val) date = datetime.astype('M8[D]') time_delta = datetime - date return date, time_delta
def merra2_stare_time(ds, iTime=None, tType=2, centered=True): if centered: start_time_mn = ds['time'].begin_time / 100 start_time_sec = ds['time'].begin_time % 100 resolution = stare_temporal_resolutions[tType]['1/2hr'] else: start_time_mn = 0 start_time_sec = 0 resolution = stare_temporal_resolutions[tType]['1hr'] yr, mo, dy = merra2_parse_begin_date(ds['time'].begin_date) tm = [] if iTime is None: i0 = 0 i1 = 24 else: i0 = iTime i1 = iTime + 1 for i in range(i0, i1): # hr,mn = merra2_make_time(ds['time'].begin_date,ds['time'][i]) hr, mn = merra2_make_time(start_time_mn, ds['time'][i]) sc = start_time_sec tm.append(format_time(yr, mo, dy, hr, mn, sc)) dt = np.array(tm, dtype='datetime64[ms]') idx = ps.from_utc(dt.astype(np.int64), resolution) return idx
def normalize_datetime(val): ''' Break up a datetime into date and time. Args: val: The datetime to normalize. Can be an array or a single datetime as a string, pandas timestamp, numpy datetime or python date or datetime Return: tuple of numpy datetime64('D') and np.timedelta64 File "__main__", line 55, in __main__._normalize_date_time >>> print(normalize_datetime(pd.Timestamp('2016-05-01 3:55:00'))) (numpy.datetime64('2016-05-01'), numpy.timedelta64(14100000000000,'ns')) >>> print(normalize_datetime('2016-05-01')) (numpy.datetime64('2016-05-01'), numpy.timedelta64(0,'D')) >>> x = pd.DataFrame({'x' : [np.datetime64('2015-01-01 05:00:00'), np.datetime64('2015-02-01 06:00:00')]}) >>> print(normalize_datetime(x.x)) (array(['2015-01-01', '2015-02-01'], dtype='datetime64[D]'), array([18000000000000, 21600000000000], dtype='timedelta64[ns]')) >>> x = pd.DataFrame({'x' : [1, 2]}, index = [np.datetime64('2015-01-01 05:00:00'), np.datetime64('2015-02-01 06:00:00')]) >>> print(normalize_datetime(x.index)) (array(['2015-01-01', '2015-02-01'], dtype='datetime64[D]'), array([18000000000000, 21600000000000], dtype='timedelta64[ns]')) ''' if isinstance(val, pd.Timestamp): datetime = val.to_datetime64() elif isinstance(val, pd.Series) or isinstance(val, pd.DatetimeIndex): datetime = val.values else: datetime = np.datetime64(val) date = datetime.astype('M8[D]') time_delta = datetime - date return date, time_delta
def read(self, dataset, filename, timeshift=0, **kargs): fh = codecs.open(filename, encoding='utf-8-sig', errors='ignore') for line in fh.readlines(): a = line.encode('utf-8') if a == b'\x00'*len(a): fh.close() msg = "File {} contains line of binary 0's" raise MiniDoasException(msg.format(filename)) fh.seek(0) dt = np.dtype([('station', 'S2'), ('date', 'S10'), ('time', np.float), ('stept', np.int), ('angle', np.float), ('intt', np.int), ('nspec', np.int), ('specin', np.float), ('counts', np.int, (482,))]) def date_converter(x): y = x.decode('ascii') return '%s-%s-%s' % (y[0:4], y[4:6], y[6:8]) data = np.loadtxt(fh, converters={1: date_converter}, dtype=dt, delimiter=',', ndmin=1) fh.close() # Construct datetimes date = data['date'].astype('datetime64') hours = (data['time']/3600.).astype(int) minutes = ((data['time'] - hours*3600.)/60.).astype(int) fseconds = data['time'] - hours*3600. - minutes*60. iseconds = (fseconds).astype(int) mseconds = np.round((fseconds - iseconds), 3)*1e3 datetime = date + hours.astype('timedelta64[h]') \ + minutes.astype('timedelta64[m]') \ + iseconds.astype('timedelta64[s]') \ + mseconds.astype('timedelta64[ms]') datetime -= np.timedelta64(int(timeshift), 'h') # Convert radians to decimal degrees angles = data['angle']*360./(2.*np.pi) rdtb = RawDataTypeBuffer(d_var_unit='ppm-m', ind_var_unit='nm', name='measurement', acquisition='stationary') wavelengths = np.arange(30, 512) rb = RawDataBuffer(inc_angle=angles, datetime=datetime.astype(str), ind_var=wavelengths, d_var=data['counts'], integration_time=data['intt']) return {str(rb): rb, str(rdtb): rdtb}
def goes10_img_stare_time(ds,tType=2,centered=True): resolution = stare_temporal_resolutions[2]['1/4hr'] dt = np.array(ds['time'][0]*1000,dtype='datetime64[ms]').reshape([1]) return ps.from_utc(dt.astype(np.int64),resolution)