db = Ska.DBI.DBI(dbi='sybase', server='sybase', user='******')
if 'obs' not in globals():
    obs = db.fetchall("""select obsid, kalman_datestart, kalman_datestop
    from observations where obsid = %s""" % obsid)[0]

if 'telem' not in globals():
    telem = fetch.MSIDset(msids,
                          obs['kalman_datestart'],
                          obs['kalman_datestop'],
                          filter_bad=True,
                          )

vals = [telem['aoattqt%d' % i].vals for i in range(1, 5)] + [telem['aoacyan0'].vals / 3600.,
                                                             telem['aoaczan0'].vals / 3600.]
if 'coords' not in globals():
    radecs = [yagzag2radec(yag, zag, Quat([q1, q2, q3, q4]))
              for q1, q2, q3, q4, yag, zag in zip(*vals)]
    coords = np.rec.fromrecords(radecs, names=('ra', 'dec'))

    # aborted code to transform back to dyag, dzag
    if 0:
        radecrolls = [Quat([q1, q2, q3, q4]).equatorial
                      for q1, q2, q3, q4, yag, zag in zip(*vals)]
        radecrolls = np.rec.fromrecords(radecrolls, names=('ra', 'dec', 'roll'))
        ra_nom = np.mean(radecrolls['ra'])
        dec_nom = np.mean(radecrolls['dec'])
        roll_nom = np.mean(radecrolls['roll'])
        q_nom = Quat([ra_nom, dec_nom, roll_nom])

ok = telem['aoacfct0'].vals == 'TRAK'
slot = 0
Exemple #2
0
def plot_centroid_resids_by_flag(start, stop, slot, save=False):
    """
    Plot centroid residuals for a start/stop interval with color
    coding to indicate readouts corresponding to a particular
    combination of DP, IR, MS, and SP.  The specified interval
    must be a stable Kalman dwell at one attitude.

    :param start: start time (any Chandra DateTime format)
    :param stop: stop time
    :param slot: ACA image slot
    :param save: save images as png files
    """

    slot_msids = [msid + "%s" % slot for msid in pcad_msids]

    msids = ['aopcadmd',
             'aoattqt1',
             'aoattqt2',
             'aoattqt3',
             'aoattqt4',
             ]

    msids.extend(slot_msids)

    print('Fetching telemetry from {} to {}'.format(start, stop))
    telem = fetch.MSIDset(msids,
                          start,
                          stop,
                          filter_bad=True,
                          )
    telem.interpolate(dt=2.05)

    vals = ([telem['aoattqt%d' % i].vals for i in range(1, 5)]
            + [telem['aoacyan{}'.format(slot)].vals / 3600.,
               telem['aoaczan{}'.format(slot)].vals / 3600.])

    print('Interpolating quaternions')
    radecs = [yagzag2radec(yag, zag, Quat([q1, q2, q3, q4]))
              for q1, q2, q3, q4, yag, zag in zip(*vals)]
    coords = np.rec.fromrecords(radecs, names=('ra', 'dec'))

    ok = telem['aoacfct{}'.format(slot)].vals == 'TRAK'

    flags = {'dp': telem['aoacidp%s' % slot].vals != 'OK ',
             'ir': telem['aoaciir%s' % slot].vals != 'OK ',
             'ms': telem['aoacims%s' % slot].vals != 'OK ',
             'sp': telem['aoacisp%s' % slot].vals != 'OK ',
             }

    times = telem['aoacyan%s' % slot].times
    dra = (coords['ra'] - np.mean(coords['ra'][ok])) * 3600 * np.cos(np.radians(coords['dec']))
    ddec = (coords['dec'] - np.mean(coords['dec'][ok])) * 3600
    dr = np.sqrt(dra ** 2 + ddec ** 2)

    fileroot = 'flags_{}'.format(DateTime(start).date[:14]) if save else None

    print('Making plots with output fileroot={}'.format(fileroot))
    for dp in (False, True):
        for ir in (False, True):
            for ms in (False, True):
                for sp in (False, True):
                    print('Making plot for dp={} ir={} ms={} sp={}'.format(dp, ir, ms, sp))
                    plot_axis('dR', times, dr, ok, dp, ir, ms, sp, flags, filename=fileroot)
def plot_centroid_resids_by_flag(start, stop, slot, plot=False):
    """
    Plot centroid residuals for a start/stop interval with color
    coding to indicate readouts corresponding to a particular
    combination of DP, IR, MS, and SP.  The specified interval
    must be a stable Kalman dwell at one attitude.

    :param start: start time (any Chandra DateTime format)
    :param stop: stop time
    :param slot: ACA image slot
    :param save: save images as png files
    """

    pcad_msids = ['aoacfct',
                  'aoacisp',
                  'aoacidp',
                  'aoaciir',
                  'aoacims',
                  'aoacyan',
                  'aoaczan']
    slot_msids = [msid + "%s" % slot for msid in pcad_msids]

    msids = ['aoattqt1',
             'aoattqt2',
             'aoattqt3',
             'aoattqt4',
             ]

    msids.extend(slot_msids)

    print('Fetching telemetry from {} to {}'.format(start, stop))
    telems = fetch.MSIDset(msids, start, stop)
    telems.interpolate(dt=2.05, filter_bad=False)

    bads = np.zeros(len(telems.times), dtype=bool)
    for msid in telems:
        bads |= telems[msid].bads
    bads |= telems['aoacfct{}'.format(slot)].vals != 'TRAK'

    telems.bads = bads
    for msid in telems:
        telems[msid].bads = bads
    telems.filter_bad()

    vals = ([telems['aoattqt%d' % i].vals for i in range(1, 5)]
            + [telems['aoacyan{}'.format(slot)].vals / 3600.,
               telems['aoaczan{}'.format(slot)].vals / 3600.])

    print('Interpolating quaternions')
    radecs = [yagzag2radec(yag, zag, Quat(normalize([q1, q2, q3, q4])))
              for q1, q2, q3, q4, yag, zag in zip(*vals)]
    coords = np.rec.fromrecords(radecs, names=('ra', 'dec'))

    # ok = telems['aoacfct{}'.format(slot)].vals == 'TRAK'

    flags = {'dp': telems['aoacidp%s' % slot].vals != 'OK ',
             'ir': telems['aoaciir%s' % slot].vals != 'OK ',
             'ms': telems['aoacims%s' % slot].vals != 'OK ',
             'sp': telems['aoacisp%s' % slot].vals != 'OK ',
             }

    times = telems['aoacyan%s' % slot].times
    dra = (coords['ra'] - np.mean(coords['ra'])) * 3600 * np.cos(np.radians(coords['dec']))
    ddec = (coords['dec'] - np.mean(coords['dec'])) * 3600
    dr = np.sqrt(dra ** 2 + ddec ** 2)

    # fileroot = 'flags_{}'.format(DateTime(start).date[:14]) if save else None

    # print('Making plots with output fileroot={}'.format(fileroot))

    filt = ((flags['dp'] == False) & (flags['ir'] == False)
            & (flags['ms'] == False) & (flags['sp'] == False))
    if plot:
        plot_axis('dR', times, dr, filt, title='No status flags')
    if np.sum(filt) > 20:
        clean_perc = np.percentile(dr[filt], [50, 84, 90, 95])
        if clean_perc[2] > 1.0:
            print '{} {} : {}'.format(start, stop, str(clean_perc))
    else:
        clean_perc = [-1, -1, -1, -1]
    clean_perc.append(np.sum(filt))

    filt = flags['dp'] == True
    if plot:
        plot_axis('dR', times, dr, filt, title='DP == True')
    if np.sum(filt) > 20:
        dp_perc = np.percentile(dr[filt], [50, 84, 90, 95])
    else:
        dp_perc = [-1, -1, -1, -1]
    dp_perc.append(np.sum(filt))

    return clean_perc, dp_perc