UT Conversion#
This is a description of the sktimeutils.mjd module. It provides several utility functions to convert to and from modified julian date. The example below demonstrates how to convert standard python representations of UTC into mjd:
import numpy as np
from datetime import datetime
from sktimeutils.mjd import ut_to_mjd
tstr = '2019-01-11 15:32:45.123456'
tdatetime = datetime.fromisoformat( tstr)
tdatetime64 = np.datetime64(tstr,dtype='datetime64[ms]')
mjda = ut_to_mjd( tstr ) # Test scalar UTC string to julian date
mjdb = ut_to_mjd( tdatetime ) # Test scalar datetime to julian date
mjdc = ut_to_mjd( tdatetime64 ) # Test scalar numpy.datetime64 to julian date
print( 'mjd from str = ', jda)
print( 'mjd from datetime = ', jdb)
print( 'mjd from datetime64 = ', jdc)
The ut_to_mjd function also supports arrays or lists of time objects/strings and generates arrays of jd1 and jd2. For example, using strings:
import numpy as np
from datetime import datetime
from sktimeutils.juliandate import ut_to_mjd
tstr = ['2019-01-11 15:32:45.123456', 2019-01-12 16:32:45.123456', 2019-01-13 17:32:45.123456']
mjd = ut_to_mjd( tstr ) # Test array UTC string to modified julian date
print( 'First mjd = ', mjd[0])
print( 'Second mjd = ', mjd[1])
print( 'Third mjd = ', mjd[2])
ut_to_mjd#
- ut_to_mjd(utc)[source]#
A convenience function that converts various arrays or scalar representations of UT to modified julian date. Scalar input values will return as a scalar float modified julian date while array, list or tuple input values are all returned as numpy arrays of mjd in float64.
- Parameters:
- utcscalar, list, tuple or array
The input time which represents a coordinated universal time. It can be represented by (i) a string in a supported python datetime.isoformat (ii) a number. The number is assumed to represent a modified julian date. (iii) a datetime.datetime object. (iv) a numpy.datetime64 object. The utc object can be a scalar, list, tuple or numpy array. The same representation format must be used for all objects in the arrays and sequences.
- Returns:
- scalar or numpy.ndarray of float
The mjd is returned as a scalar if the input was a scalar or as a numpy array for input sequences and arrays
mjd_to_ut#
- mjd_to_ut(mjd)[source]#
A convenience function that converts scalars or arrays of floating point modified julian dates to universal time representations. The universal time is always returned as a scalar or array of numpy.datetime64 object(s).
- Parameters:
- mjdscalar, array, or sequence of float
The input can be a scalar, numpy array or regular python list/tuple of floats storing MJD values.
- Returns:
- numpy.datetime64 or numpy.ndarray< numpy.datetime64 >
Returns either a scalar or array of numpy.datetime64 that matches the input object
ut_to_datetime#
- ut_to_datetime(utc: ndarray | Sequence | float | str | datetime64 | datetime) ndarray | datetime | None[source]#
A convenience function that converts various arrays or scalar representations of UT to datetime Scalar input values will return as a scalar datetime while array, list or tuple input values are all returned as numpy arrays of datetime objects
- Parameters:
- utcscalar, list, tuple or array
The input time which represents a coordinated universal time. It can be represented by (i) a string in a supported python datetime.isoformat (ii) a number. The number is assumed to represent a modified julian date. (iii) a datetime.datetime object. (iv) a numpy.datetime64 object. The utc object can be a scalar, list, tuple or numpy array. The same representation format must be used for all objects in the arrays and sequences.
- Returns:
- scalar or numpy.ndarray of datetime
The time is returned as a scalar if the input was a scalar or as a numpy array for input sequences and arrays
ut_to_datetime64#
- ut_to_datetime64(utc)[source]#
A convenience function that converts various arrays or scalar representations of UT to datetime64 Scalar input values will return as a scalar datyetime64 while array, list or tuple input values are all returned as numpy arrays of utc in datetime64.
- Parameters:
- utcscalar, list, tuple or array
The input time which represents a coordinated universal time. It can be represented by (i) a string in a supported python datetime.isoformat (ii) a number. The number is assumed to represent a modified julian date. (iii) a datetime.datetime object. (iv) a numpy.datetime64 object. The utc object can be a scalar, list, tuple or numpy array. The same representation format must be used for all objects in the arrays and sequences.
- Returns:
- scalar or numpy.ndarray of np.datetim64
The time is returned as a scalar if the input was a scalar or as a numpy array for input sequences and arrays
datetime64_to_timestamp#
- datetime64_to_timestamp(utc)[source]#
Converts a scalar or array of numpy.datetime64 to floating point timestamp (seconds since 1970). The conversion includes the fraction of seconds.
- Parameters:
- utcnumpy.datetime64 or Array[numpy.datetime64]
A time specified with a numpy datetime64 object. Explicit time zones are not currently supported. Only single scalar values are supported in this function.
- Returns:
- float
The time expressed as a timestamp value. Number of seconds since Jan 1, 1970.
ut_to_jd#
- ut_to_jd(ut: ndarray | List[Any] | Any)[source]#
A convenience function that converts various arrays or scalar representations of UT to julian date. Each Julian date is returned as two floating point components, jd1 and jd2. If the input is an array of value sthen jd1 and jd2 will be returned as floating point arrays with the same shape and size as the input array. If the inputs are scalar values then jd1 and jd2 will be returned as scalar floating point values.
- Parameters:
- utcscalar, list, tuple or array
The input time which represents a universal time (strictly UT1). It can be represented by (i) a string in a supported python datetime.isoformat (ii) a number which is assumed to represent a julian date if it is greater than 500,000 or a modified julian date if it less than 500,000, (iii) a datetime.datetime object representing UT (iv) a numpy.datetime64 object representing UT
The ut object can be a scalar, list, tuple or numpy array. The same representation format must be used for all objects in the arrays and sequences.
- Returns:
- scalar or numpy.ndarray of float
The mjd is returned as a scalar if the input was a scalar or as a numpy array for input sequences and arrays
utc_to_astropy#
- utc_to_astropy(utc)[source]#
A convenience function that converts list, arrays or scalar representations of UTC to an astropy Time object. The function is a shallow wrapper for strings, datetime and datetime64 arrays as numpy arrays of mjd in float64.
- Parameters:
- utcscalar, list, tuple or array
The input time which represents a coordinated universal time. It can be represented by (i) a string in a supported python datetime.isoformat (ii) a number. The number is assumed to represent a modified julian date. (iii) a datetime.datetime object. (iv) a numpy.datetime64 object. The utc object can be a scalar, list, tuple or numpy array. The same representation format must be used for all objects in the arrays and sequences.
- Returns:
- scalar or numpy.ndarray of float
The mjd is returned as a scalar if the input was a scalar or as a numpy array for input sequences and arrays
mjd_to_datetime#
Scalar conversion only.
- mjd_to_datetime(mjd: float) datetime[source]#
Converts a modified julian date to a datetime.datetime object. This function only works on scalar values of mjd. Time zones are not set.
- Parameters:
- mjdfloat
The modified julian date.
- Returns:
- datetime.datetime
The datetime.datetime corresponding to the modified julian date. No time zone is set
mjd_to_datetime64#
Scalar conversion only.
datetime64_to_datetime#
Scalar conversion only.
- datetime64_to_datetime(usertime: datetime64) datetime[source]#
Converts a single numpy.datetime64 to a datetime.datetime
- Parameters:
- usertimenumpy.datetime64
A time specified with a numpy datetime64 object. Explicit time zones are not currently supported. Only single scalar values are supported in this function.
- Returns:
- datetime.datetime
The time expressed as a regular python datetime.datetime.