def _test_erfa_conversion(leap, i_f): i_i, f_i = i_f assume(0 <= f_i < 1) if leap: assume(i_i in leap_sec_days) else: assume(i_i not in leap_sec_days) jd1_in, jd2_in = day_frac(erfa.DJM0 + i_i, f_i) y, mo, d, f = erfa.jd2cal(jd1_in, jd2_in) assert 0 < y < 3000 assert 0 < mo <= 12 assert 0 <= d < 32 assert 0 <= f < 1 jd1_temp, jd2_temp = erfa.cal2jd(y, mo, d) jd1_temp, jd2_temp = day_frac(jd1_temp, jd2_temp) # improve numerics jd1_temp, jd2_temp = day_frac(jd1_temp, jd2_temp + f) jd_change = abs((jd1_temp - jd1_in) + (jd2_temp - jd2_in)) * u.day assert jd_change.to(u.ns) < 1 * u.ns ft = 24 * f h = safe_kind_conversion(np.floor(ft), dtype=int) ft -= h ft *= 60 m = safe_kind_conversion(np.floor(ft), dtype=int) ft -= m ft *= 60 s = ft assert 0 <= h < 24 assert 0 <= m < 60 assert 0 <= s < 60 jd1, jd2 = erfa.dtf2d("UTC", y, mo, d, h, m, s) y2, mo2, d2, f2 = erfa.jd2cal(jd1, jd2) # assert (y, mo, d) == (y2, mo2, d2) # assert (abs(f2-f)*u.day).to(u.s) < 1*u.ns assert jd1 == np.floor(jd1) + 0.5 assert 0 <= jd2 < 1 jd1, jd2 = day_frac(jd1, jd2) jd_change = abs((jd1 - jd1_in) + (jd2 - jd2_in)) * u.day if leap: assert jd_change.to(u.s) < 1 * u.s else: assert jd_change.to(u.ns) < 2 * u.ns # assert jd_change.to(u.ns) < 1 * u.ns return i_o, f_o = day_frac(jd1 - erfa.DJM0, jd2) mjd_change = abs((i_o - i_i) + (f_o - f_i)) * u.day if leap: assert mjd_change.to(u.s) < 1 * u.s else: assert mjd_change.to(u.ns) < 1 * u.ns
def mjds_to_jds_pulsar(mjd1, mjd2): # To get around leap second issues, first convert to YMD, # then back to astropy/ERFA-convention jd1,jd2 using the # ERFA dtf2d() routine which handles leap seconds. v1, v2 = day_frac(mjd1, mjd2) (y, mo, d, f) = erfa.jd2cal(erfa.DJM0 + v1, v2) # Fractional day to HMS. Uses 86400-second day always. # Seems like there should be a ERFA routine for this.. # There is: erfa.d2tf. Unfortunately it takes a "number of # digits" argument and returns some kind of bogus # fractional-part-as-an-integer thing. # Worse, it fails to provide nanosecond accuracy. # Good idea, though, because using np.remainder is # numerically unstable and gives bogus values now # and then. This is more stable. f *= 24 h = safe_kind_conversion(np.floor(f), dtype=int) f -= h f *= 60 m = safe_kind_conversion(np.floor(f), dtype=int) f -= m f *= 60 s = f return erfa.dtf2d("UTC", y, mo, d, h, m, s)
print(''' Date and time.''') iy = 2008 im = 2 id = 29 ihour = 23 imin = 59 sec = 59.9 print("%4d/%2.2d/%2.2d%3d:%2.2d:%4.1f\n" % (iy, im, id, ihour, imin, sec)) print('Express as 2-part JD.') d1, d2 = erfa.cal2jd(iy, im, id) d = erfa.tf2d(ihour, imin, sec) d2 += d print("%9.1f +%13.6f =%15.6f\n" % (d1, d2, d1 + d)) print('Express as calendar date and fraction of a day.') iy, im, id, fd = erfa.jd2cal(d1, d2) d = id + fd print("%4d/%2.2d/%9.6f\n" % (iy, im, d)) print('Round to 0.001 day.') iymdf = erfa.jdcalf(3, d1, d2) print("%4d/%2.2d/%2.2d.%3.3d\n" % iymdf) print('=====') print(''' Besselian and Julian epochs''') print('Julian Date.') d = 2457073.05631 print("%13.5f" % d) print('Transform into Besselian epoch:') e = erfa.epb(0.0, d) print("B%15.10f" % e) print('Transform back.')
print ( "%s%2d:%2.2d:%2.2d.%3.3d\n"%ihmsf) print('=====') print(''' Date and time.''') iy = 2008; im = 2; id = 29; ihour = 23; imin = 59; sec = 59.9; print("%4d/%2.2d/%2.2d%3d:%2.2d:%4.1f\n"% (iy, im, id, ihour, imin, sec)) print('Express as 2-part JD.') d1, d2 = erfa.cal2jd ( iy, im, id) d = erfa.tf2d (ihour, imin, sec) d2 += d print("%9.1f +%13.6f =%15.6f\n"%( d1, d2, d1 + d)) print('Express as calendar date and fraction of a day.') iy, im, id, fd = erfa.jd2cal(d1, d2) d = id + fd print( "%4d/%2.2d/%9.6f\n"%(iy, im, d)) print('Round to 0.001 day.') iymdf = erfa.jdcalf ( 3, d1, d2) print( "%4d/%2.2d/%2.2d.%3.3d\n"%iymdf) print('=====') print(''' Besselian and Julian epochs''') print('Julian Date.') d = 2457073.05631 print ( "%13.5f"%d ) print('Transform into Besselian epoch:') e = erfa.epb ( 0.0, d) print( "B%15.10f"%e ) print('Transform back.')