示例#1
0
 def __init__(self, t, mjd=None, date=None):
     if t is None:
         if date is not None:
             mjd = datetomjd(date)
         if mjd is not None:
             t = mjd * 24. * 3600.
     super(TAITime, self).__init__(t)
示例#2
0
 def __init__(self, t, mjd=None, date=None):
     if t is None:
         if date is not None:
             mjd = datetomjd(date)
         if mjd is not None:
             t = mjd * 24. * 3600.
     super(TAITime, self).__init__(t)
示例#3
0
 def timed_out(self, dt):
     now = datenow()
     dt  = (now - self.lastNewFile).total_seconds()
     print('No new images seen for', dt, 'seconds.')
     markmjds = []
     if dt > self.longtime:
         edate = (self.lastNewFile +
                  datetime.timedelta(0, self.longtime))
         markmjds.append((datetomjd(edate),'r'))
     self.plot_recent(markmjds=markmjds)
示例#4
0
class TAITime(ScalarParam, ArithmeticParams):
    '''
    This is TAI as used in the SDSS 'frame' headers; eg

    TAI     =        4507681767.55 / 1st row - 
    Number of seconds since Nov 17 1858

    And http://mirror.sdss3.org/datamodel/glossary.html#tai
    says:

    MJD = TAI/(24*3600)
    '''
    equinox = 53084.28  # mjd of the spring equinox in 2004
    daysperyear = 365.25  # Julian years, by definition

    mjd2k = datetomjd(J2000)

    def __init__(self, t, mjd=None, date=None):
        if t is None:
            if date is not None:
                mjd = datetomjd(date)
            if mjd is not None:
                t = mjd * 24. * 3600.
        super(TAITime, self).__init__(t)

    def toMjd(self):
        return self.getValue() / (24. * 3600.)

    def getSunTheta(self):
        mjd = self.toMjd()
        th = 2. * np.pi * (mjd - TAITime.equinox) / TAITime.daysperyear
        th = np.fmod(th, 2. * np.pi)
        return th

    def toYears(self):
        ''' years since Nov 17, 1858 ?'''
        return float(self.getValue() / (24. * 3600. * TAITime.daysperyear))

    def toYear(self):
        ''' to proper year '''
        return self.toYears() - TAITime(None,
                                        mjd=TAITime.mjd2k).toYears() + 2000.0
示例#5
0
def plot_measurements(mm, plotfn, nom, mjds=[], mjdrange=None, allobs=None,
                      markmjds=[], show_plot=True):
    import pylab as plt
    T = db_to_fits(mm)
    T.band = np.core.defchararray.replace(T.band, 'zd', 'z')
    print(len(T), 'exposures')

    T.mjd_end = T.mjd_obs + T.exptime / 86400.

    #Tall = T
    Tnonobject = T[T.obstype != 'object']
    print(len(Tnonobject), 'exposures are not OBJECTs')
    T = T[T.obstype == 'object']
    print(len(T), 'OBJECT exposures')

    if len(T) == 0:
        return
    
    ccmap = dict(g='g', r='r', z='m')

    #bands = 'grz'
    bands = np.unique(T.band)
    #print('Unique bands:', bands)
    
    TT = []
    for band in bands:
        G = T[T.band == band]
        TT.append(G)

    plt.clf()
    plt.subplots_adjust(hspace=0.1, top=0.98, right=0.95, left=0.1,
                        bottom=0.07)

    def limitstyle(band):
        return dict(mec='k', mfc=ccmap[band], ms=8, mew=1)

    # Check for bad things that can happen
    bads = []

    # bad_pixcnt
    I = np.flatnonzero(T.bad_pixcnt)
    for i in I:
        bads.append((i, 'pixcnt'))

    # low nmatched
    I = np.flatnonzero((T.nmatched >= 0) * (T.nmatched < 10))
    for i in I:
        bads.append((i, 'nmatched'))

    if allobs is not None:

        # Duplicate md5sum
        for i in range(len(T)):
            if T.md5sum[i] == '':
                continue
            a = allobs.filter(md5sum=T.md5sum[i]).exclude(
                filename=T.filename[i], extension=T.extension[i])

            if a.count():
                # Only report this one as bad if one of the repeats was earlier
                for ai in a:
                    if ai.mjd_obs < T.mjd_obs[i]:
                        bads.append((i, 'md5sum'))
                        break
                
    # Group together bad things for the same image.
    bd = {}
    for i,reason in bads:
        if i in bd:
            bd[i] = bd[i] + ', ' + reason
        else:
            bd[i] = reason
    bads = bd.items()

    ilatest = np.argmax(T.mjd_obs)
    latest = T[ilatest]

    SP = 5
    mx = 2.
    plt.subplot(SP,1,1)
    for band,Tb in zip(bands, TT):
        plt.plot(Tb.mjd_obs, Tb.seeing, 'o', color=ccmap[band])

        I = np.flatnonzero(Tb.seeing > mx)
        if len(I):
            plt.plot(Tb.mjd_obs[I], [mx]*len(I), '^', **limitstyle(band))
    yl,yh = plt.ylim()
    plt.axhline(1.3, color='k', alpha=0.5)
    plt.axhline(1.2, color='k', alpha=0.1)
    plt.axhline(1.0, color='k', alpha=0.1)
    plt.axhline(0.8, color='k', alpha=0.1)

    plt.text(latest.mjd_obs, yl+0.01*(yh-yl),
             '%.2f' % latest.seeing, ha='center')

    y = yl + 0.01*(yh-yl)
    plt.plot(np.vstack((T.mjd_obs, T.mjd_end)),
             np.vstack((y, y)), '-', lw=3, alpha=0.5, color=ccmap[band],
             solid_joinstyle='bevel')

    plt.ylim(yl,min(yh,mx))
    plt.ylabel('Seeing (arcsec)')

    ax = plt.axis()
    for i,reason in bads:
        plt.axvline(T.mjd_obs[i], color='r', lw=3, alpha=0.3)
        plt.text(T.mjd_obs[i], ax[3], reason,
                 rotation=90, va='top')
    plt.axis(ax)

    plt.subplot(SP,1,2)
    mx = 20
    for band,Tb in zip(bands, TT):
        sky0 = nom.sky(band)
        plt.plot(Tb.mjd_obs, Tb.sky, 'o', color=ccmap[band])
        plt.axhline(sky0, color=ccmap[band], alpha=0.5)

        I = np.flatnonzero(Tb.sky > mx)
        if len(I):
            plt.plot(Tb.mjd_obs[I], [mx]*len(I), '^', **limitstyle(band))
    yl,yh = plt.ylim()
    yh = min(yh,mx)

    plt.text(latest.mjd_obs, yl+0.01*(yh-yl),
             '%.2f' % latest.sky, ha='center')

    for t in T:
        if t.passnumber > 0:
            plt.text(t.mjd_obs, min(mx, t.sky) - 0.03*(yh-yl),
                     #yl+0.1*(yh-yl),
                     '%i' % t.passnumber, ha='center', va='top')
    
    plt.ylim(yl,yh)
    plt.ylabel('Sky (mag)')

    plt.subplot(SP,1,3)
    mx = 1.2
    mn = 0.5
    for band,Tb in zip(bands, TT):
        plt.plot(Tb.mjd_obs, Tb.transparency, 'o', color=ccmap[band])
        I = np.flatnonzero(Tb.transparency > mx)
        if len(I):
            plt.plot(Tb.mjd_obs[I], [mx]*len(I), '^', **limitstyle(band))
        I = np.flatnonzero(Tb.transparency < mn)
        if len(I):
            plt.plot(Tb.mjd_obs[I], [mn]*len(I), 'v', **limitstyle(band))

    plt.axhline(1.0, color='k', alpha=0.5)
    plt.axhline(0.9, color='k', ls='--', alpha=0.5)
    plt.ylabel('Transparency')
    yl,yh = plt.ylim()
    yl,yh = min(0.89, max(mn, yl)), min(mx, max(yh, 1.01))

    plt.text(latest.mjd_obs, yl+0.01*(yh-yl),
             '%.2f' % latest.transparency, ha='center')

    plt.ylim(yl, yh)
    plt.subplot(SP,1,4)
    mx = 300
    for band,Tb in zip(bands, TT):
        fid = nom.fiducial_exptime(band)

        basetime = fid.exptime
        lo,hi = fid.exptime_min, fid.exptime_max
        exptime = basetime * Tb.expfactor
        clipped = np.clip(exptime, lo, hi)
        if band == 'z':
            t_sat = nom.saturation_time(band, Tb.sky)
            clipped = np.minimum(clipped, t_sat)
        Tb.clipped_exptime = clipped
        Tb.depth_factor = Tb.exptime / clipped
        I = np.flatnonzero(exptime > clipped)
        if len(I):
            plt.plot(Tb.mjd_obs[I], exptime[I], 'v', **limitstyle(band))

        I = np.flatnonzero(exptime > mx)
        if len(I):
            plt.plot(Tb.mjd_obs[I], [mx]*len(I), '^', **limitstyle(band))

        I = np.flatnonzero(exptime < clipped)
        if len(I):
            plt.plot(Tb.mjd_obs[I], exptime[I], '^', **limitstyle(band))
        plt.plot(Tb.mjd_obs, clipped, 'o', color=ccmap[band])
        plt.plot(Tb.mjd_obs, Tb.exptime, 'o', mec='k', mfc='none')
    yl,yh = plt.ylim()
    for band,Tb in zip(bands, TT):

        dt = dict(g=-0.5,r=+0.5,z=0)[band]

        fid = nom.fiducial_exptime(band)
        basetime = fid.exptime
        lo,hi = fid.exptime_min, fid.exptime_max

        plt.axhline(basetime+dt, color=ccmap[band], alpha=0.2)
        plt.axhline(lo+dt, color=ccmap[band], ls='--', alpha=0.5)
        plt.axhline(hi+dt, color=ccmap[band], ls='--', alpha=0.5)
        if band == 'z':
            plt.plot(Tb.mjd_obs, t_sat, color=ccmap[band], ls='--', alpha=0.5)

    plt.ylim(yl,min(mx, yh))
    plt.ylabel('Target exposure time (s)')

    plt.subplot(SP,1,5)
    mn,mx = 0.6, 1.4
    for band,Tb in zip(bands, TT):
        plt.plot(Tb.mjd_obs, Tb.depth_factor, 'o', color=ccmap[band])
        # lower and upper limits
        I = np.flatnonzero(Tb.depth_factor < mn)
        if len(I):
            plt.plot(Tb.mjd_obs[I], [mn]*len(I), 'v', **limitstyle(band))
        I = np.flatnonzero(Tb.depth_factor > mx)
        if len(I):
            plt.plot(Tb.mjd_obs[I], [mx]*len(I), '^', **limitstyle(band))
    plt.axhline(1.0, color='k', alpha=0.5)
    plt.axhline(0.9, color='k', ls='--', alpha=0.5)
    plt.axhline(1.1, color='k', ls='--', alpha=0.5)

    F = Tnonobject[Tnonobject.obstype == 'focus']
    print(len(F), 'focus frames')
    if len(F):
        plt.plot(F.mjd_obs, 0.9 + np.zeros(len(F)), 'ko')
        for f in F:
            plt.text(f.mjd_obs, 0.88, 'F', ha='center', va='top')
    
    if len(T) > 50:
        ii = [np.argmin(T.expnum + (T.expnum == 0)*1000000),
              np.argmax(T.expnum)]
    else:
        ii = range(len(T))
    for i in ii:
        if T.expnum[i] == 0:
            continue
        plt.text(T.mjd_obs[i], mx, '%i ' % T.expnum[i],
                 rotation=90, va='top', ha='center')
        if len(T) <= 50:
            # Mark focus frames too
            for i in range(len(F)):
                plt.text(F.mjd_obs[i], mx, '%i ' % F.expnum[i],
                         rotation=90, va='top', ha='center')

    plt.ylim(mn,mx)
    plt.ylabel('Depth factor')
    
    plt.xlabel('MJD')
    
    if mjdrange is not None:
        for sp in range(SP):
            plt.subplot(SP,1,sp+1)
            plt.xlim(mjdrange)

    plt.subplot(SP,1,SP)
    xl,xh = plt.xlim()
    if xh - xl < 2./24.:
        # range of less than 2 hours: label every ten minutes
        tx = []
        tt = []
        dl = mjdtodate(xl)
        d = datetime.datetime(dl.year, dl.month, dl.day, dl.hour)
        while True:
            mjd = datetomjd(d)
            if mjd > xh:
                break
            if mjd > xl:
                tx.append(mjd)
                tt.append(d.strftime('%H:%M'))
            d += datetime.timedelta(0, 600)
        plt.xticks(tx, tt)
        plt.xlabel(dl.strftime('Time UTC starting %Y-%m-%d'))

    elif xh - xl < 1:
        # range of less than a day: label the hours
        tx = []
        tt = []
        dl = mjdtodate(xl)
        d = datetime.datetime(dl.year, dl.month, dl.day, dl.hour)
        while True:
            mjd = datetomjd(d)
            if mjd > xh:
                break
            if mjd > xl:
                tx.append(mjd)
                tt.append(d.strftime('%H:%M'))
            d += datetime.timedelta(0, 3600)
        plt.xticks(tx, tt)
        plt.xlabel(dl.strftime('Time UTC starting %Y-%m-%d'))

    else:
        tx,tt = plt.xticks()

    # Set consistent tick marks but no labels on top plots
    tt = ['']*len(tx)
    for sp in range(1, SP):
        plt.subplot(SP,1,sp)
        plt.xticks(tx,tt)
        plt.xlim(xl,xh)

    plt.xlim(xl,xh)

    if len(markmjds):
        for sp in range(1, SP+1):
            plt.subplot(SP,1,sp)
            ax = plt.axis()
            for mjd,c in markmjds:
                plt.axvline(mjd, color=c, alpha=0.5, lw=2)
            plt.axis(ax)

    if show_plot:
        plt.draw()
        plt.show(block=False)
        plt.pause(0.001)
    plt.savefig(plotfn)
    print('Saved', plotfn)
    if show_plot:
        plt.draw()
        plt.show(block=False)
        plt.pause(0.001)
示例#6
0
def ephem_date_to_mjd(edate):
    from astrometry.util.starutil_numpy import datetomjd
    return datetomjd(edate.datetime())
示例#7
0
def mjdnow():
    from astrometry.util.starutil_numpy import datetomjd
    return datetomjd(datenow()) + mjdnow_offset