예제 #1
0
def find_pick_list(person):

    #This section pulls out the JSON
    with open(person) as f1:
        soup = BeautifulSoup(f1, 'html.parser')

    a = re.findall('({.+})', soup.text)

    for i in np.arange(len(a)):

        if 'DUKE' in a[i]:

            tab = pd.read_json(a[i])

    #Now we can dig into the table and extract the information
    #we want.
    person_name = tab.loc['pick_data']['game_and_pick_list']['team_name']
    pick_list = []
    game_date = []

    for i in range(4):

        for j in range(games):
            pick_list.append(tab.loc['regions'][0][i]['rounds'][round_id]
                             ['games'][j]['user_pick']['pick'])
            game_date.append(tab.loc['regions'][0][i]['rounds'][round_id]
                             ['games'][j]['game_date'][:-4])
    game_dates = Time(game_date, format='iso')
    pick_list = np.array(pick_list)

    return person_name, pick_list[game_dates.argsort()], tab
예제 #2
0
    def get_hartmann(self):
        data = self.telemetry.get('25m:hartmann:sp1Residuals:deg',
                                  (self.tstart - 0.3).datetime,
                                  (self.tend - 0.3).datetime,
                                  interpolation='raw',
                                  scan_archives=False)
        hart_times = data.times
        hart_times = Time(hart_times)
        hart_sorter = hart_times.argsort()
        hart_times = hart_times[hart_sorter]
        filt = self.tstart - 0.3 < hart_times
        hart_times = hart_times[filt]

        if self.args.verbose:
            print('hart_times: ', hart_times)
        hartmann_keys = ['25m:guider:cartridgeLoaded:cartridgeID',
                         '25m:guider:cartridgeLoaded:plateID',
                         '25m:guider:survey:plateType',
                         '25m:hartmann:r1PistonMove', '25m:hartmann:b1RingMove',
                         '25m:hartmann:sp1AverageMove',
                         '25m:hartmann:sp1Residuals:deg',
                         '25m:boss:sp1Temp:median']
        # '25m:hartmann:r2PistonMove', '25m:hartmann:b2RingMove
        # '25m:hartmann:sp2AverageMove',
        # '25m:hartmann:sp2Residuals:deg',
        # '25m:boss:sp2Temp:median']
        hart_data = {}
        for key in hartmann_keys:
            hart_data[key] = []
        for time in hart_times:
            self.query(hartmann_keys, time.datetime,
                       time.datetime,
                       hart_data)

        self.hartmann += '=' * 80 + '\n'
        self.hartmann += '{:^80}\n'.format('Hartmann Log')
        self.hartmann += '=' * 80 + '\n\n'
        self.hartmann += ('{:<5} {:<9} {:<5} {:<5} {:<5} {:<7} {:<4}'
                          '\n'.format('Time', 'Cart', 'R1', 'B1', 'Move1',
                                      'B1Resid', 'TSP1'))
        self.hartmann += '-' * 80 + '\n'
        for i, time in enumerate(hart_times):
            if hart_data[hartmann_keys[2]][i] == '':
                continue
            self.hartmann += ('{:>5} {:>2}-{:0>5}{:<1} {:>5.0f} {:>5.1f}'
                              ' {:>5.0f} {:>7.1f} {:>4.1f}'
                              '\n'.format(time.isot[11:16],
                                          hart_data[hartmann_keys[0]][i],
                                          hart_data[hartmann_keys[1]][i],
                                          hart_data[hartmann_keys[2]][i][0],
                                          hart_data[hartmann_keys[3]][i],
                                          hart_data[hartmann_keys[4]][i],
                                          hart_data[hartmann_keys[5]][i],
                                          hart_data[hartmann_keys[6]][i],
                                          hart_data[hartmann_keys[7]][i], ))
예제 #3
0
def test_time_argminmaxsort(scale, jds, delta):
    jd1, jd2 = jds
    t = Time(jd1, jd2+np.array([0, delta]), scale=scale, format="jd")
    imin = t.argmin()
    imax = t.argmax()
    isort = t.argsort()
    diff = (t.jd1[1]-t.jd1[0]) + (t.jd2[1]-t.jd2[0])
    if diff < 0:  # item 1 smaller
        assert delta < 0
        assert imin == 1 and imax == 0 and np.all(isort == [1, 0])
    elif diff == 0:  # identical within precision
        assert abs(delta) <= tiny
        assert imin == 0 and imax == 0 and np.all(isort == [0, 1])
    else:
        assert delta > 0
        assert imin == 0 and imax == 1 and np.all(isort == [0, 1])
예제 #4
0
    def _check_and_sort_time_intervals(self, time_intervals):
        """Sort the time_intervals by increasing time if not already ordered correctly.

        Parameters
        ----------
        time_intervals : list of `astropy.time.Time`
            Start and stop time for each interval to compute the LC
        """
        time_start = Time([interval[0] for interval in time_intervals])
        time_stop = Time([interval[1] for interval in time_intervals])
        sorted_indices = time_start.argsort()
        time_start_sorted = time_start[sorted_indices]
        time_stop_sorted = time_stop[sorted_indices]
        diff_time_stop = time_stop_sorted[1:] - time_stop_sorted[:-1]
        diff_time_interval_edges = time_start_sorted[1:] - time_stop_sorted[:-1]
        if np.any(diff_time_stop < 0) or np.any(diff_time_interval_edges < 0):
            raise ValueError(
                "LightCurveEstimator requires non-overlapping time bins.")
        else:
            return [
                Time([tstart, tstop])
                for tstart, tstop in zip(time_start_sorted, time_stop_sorted)
            ]
예제 #5
0
def game_list(person):

    #This section pulls out the JSON
    with open(person) as f1:
        soup = BeautifulSoup(f1, 'html.parser')

    a = re.findall('({.+})', soup.text)

    for i in np.arange(len(a)):

        if 'DUKE' in a[i]:

            tab = pd.read_json(a[i])

    #Now we can dig into the table and extract the information
    #we want.
    person_name = tab.loc['pick_data']['game_and_pick_list']['team_name']
    gameteam = []
    gamedate = []

    for i in range(4):

        for j in range(games):
            home = tab.loc['regions'][0][i]['rounds'][round_id]['games'][j][
                'home_abbr']
            away = tab.loc['regions'][0][i]['rounds'][round_id]['games'][j][
                'away_abbr']
            gamedate.append(tab.loc['regions'][0][i]['rounds'][round_id]
                            ['games'][j]['game_date'][:-4])
            gameteam.append('{} vs {}'.format(home, away))

        gamedates = Time(list(gamedate), format='iso')
        idx = gamedates.argsort()
        gameteams = np.array(gameteam)

    return gameteams[idx], gamedates[idx]
예제 #6
0
class LogSupport:
    def __init__(self, tstart, tend, args):
        self.tstart = Time(tstart)
        self.tend = Time(tend)
        self.args = args
        self.telemetry = epics_fetch.telemetry

        self.call_times = []
        self.offsets = ''
        self.focus = ''
        self.weather = ''
        self.hartmann = ''

    def query(self, key, tstart, tend, tel_dict):
        data = self.telemetry.get(key, tstart, tend,
                                  interpolation='raw', scan_archives=False)
        if isinstance(key, list):
            for k, d in zip(key, data):
                tel_dict[k].append(d.values[0])
        else:
            value = data.values[0]
            tel_dict[key].append(value)
        return data

    def set_callbacks(self):
        data = self.telemetry.get([
            # '25m:sop:doApogeeBossScience_nDither'
            # ':nDitherDone',
            # '25m:sop:doBossScience_nExp'
            # ':nExpDone',
            # '25m:sop:doMangaSequence_ditherSeq:index',
            # '25m:sop:doApogeeMangaSequence_ditherSeq:'
            # 'index',
            # '25m:sop:doApogeeScience_index:index'],
            '25m:apogee:exposureWroteSummary'],
            (self.tstart - 0.3).datetime,
            (self.tend - 0.3).datetime,
            interpolation='raw',
            scan_archives=False)
        self.call_times = []
        for dat in data:
            self.call_times += dat.times
        self.call_times = Time(self.call_times)
        callback_sorter = self.call_times.argsort()
        self.call_times = self.call_times[callback_sorter]

        if self.args.verbose:
            print('Callback start: {}'.format(self.tstart.isot))
            print('Callback end: {}'.format(self.tend.isot))
            print(self.call_times)

        filt = self.tstart - 0.3 < self.call_times
        self.call_times = self.call_times[filt]

    def get_offsets(self):
        offsets_keys = ['25m:guider:cartridgeLoaded:cartridgeID',
                        '25m:guider:cartridgeLoaded:plateID',
                        '25m:guider:survey:plateType',
                        '25m:tcc:axePos:az', '25m:tcc:axePos:alt',
                        '25m:tcc:axePos:rot', '25m:tcc:objArcOff:az',
                        '25m:tcc:objArcOff:alt', '25m:tcc:guideOff:rot',
                        '25m:tcc:calibOff:az', '25m:tcc:calibOff:alt',
                        '25m:tcc:calibOff:rot', '25m:guider:guideRMS:RMSerror']
        off_data = {}
        for key in offsets_keys:
            off_data[key] = []
        for time in self.call_times:
            self.query(offsets_keys, time.datetime, time.datetime,
                       off_data)

        self.offsets += '=' * 80 + '\n'
        self.offsets += '{:^80}\n'.format(
            'Telescope Offsets and Scale (arcsec)')
        self.offsets += '=' * 80 + '\n\n'
        self.offsets += ('{:<5} {:<9} {:<6} {:<4} {:<6} {:<13} {:<8} {:<10}'
                         ' {:<8}\n'.format('Time', 'Cart', 'Az', 'Alt', 'Rot',
                                           '(\u03B4RA, \u03B4Dec)', 'guideRot',
                                           'calibOff', 'guideRMS'))
        self.offsets += '-' * 80 + '\n'
        for i, time in enumerate(self.call_times):
            if off_data[offsets_keys[2]][i] == '':
                continue
            self.offsets += ('{:>5} {:>2}-{:0>5} {:>1} {:>6.1f} {:>4.1f}'
                             ' {:>6.1f} ({:>5.1f},{:>5.1f}) {:>8.1f}'
                             ' ({:2.0f},{:2.0f},{:2.0f}) {:>8.3f}'
                             '\n'.format(time.isot[11:16],
                                         off_data[offsets_keys[0]][i],
                                         off_data[offsets_keys[1]][i],
                                         off_data[offsets_keys[2]][i][0],
                                         off_data[offsets_keys[3]][i],
                                         off_data[offsets_keys[4]][i],
                                         off_data[offsets_keys[5]][i],
                                         off_data[offsets_keys[6]][i][0] * 3600,
                                         off_data[offsets_keys[7]][i][0] * 3600,
                                         off_data[offsets_keys[8]][i][0] * 3600,
                                         off_data[offsets_keys[9]][i][0] * 3600,
                                         off_data[offsets_keys[10]][
                                             i][0] * 3600,
                                         off_data[offsets_keys[11]][
                                             i][0] * 3600,
                                         off_data[offsets_keys[12]][i])
                             )

    def get_focus(self):
        focus_keys = ['25m:guider:cartridgeLoaded:cartridgeID',
                      '25m:guider:cartridgeLoaded:plateID',
                      '25m:guider:survey:plateType',
                      '25m:tcc:scaleFac', '25m:tcc:primOrient:pos',
                      '25m:tcc:secOrient:piston', '25m:tcc:secFocus',
                      '25m:tcc:axePos:az', '25m:tcc:axePos:alt',
                      '25m:apo:airTempPT', '25m:apo:winds',
                      '25m:apo:windd', '25m:guider:seeing']
        foc_data = {}
        for key in focus_keys:
            foc_data[key] = []
        for time in self.call_times:
            self.query(focus_keys, time.datetime, time.datetime,
                       foc_data)

        self.focus += '=' * 80 + '\n'
        self.focus += '{:^80}\n'.format('Telescope Focus')
        self.focus += '=' * 80 + '\n\n'
        self.focus += ('{:<5} {:<9} {:<6} {:<5} {:<5} {:<5} {:<6} {:<5}'
                       ' {:<5} {:<4} {:<3}'
                       ' {:<4}\n'.format('Time', 'Cart', 'Scale', 'M1', 'M2',
                                         'Focus', 'Az', 'Alt', 'Temp',
                                         'Wind', 'Dir', 'FWHM'))
        self.focus += '-' * 80 + '\n'
        for i, time in enumerate(self.call_times):
            if foc_data[focus_keys[2]][i] == '':
                continue
            self.focus += ('{:>5} {:>2}-{:0>5}{:>1} {:>6.1f} {:>5.0f}'
                           ' {:>5.0f}'
                           ' {:>5.0f} {:>6.1f} {:>5.1f} {:>5.1f} {:>4.0f}'
                           ' {:>3.0f}'
                           ' {:>4.1f}\n'.format(time.isot[11:16],
                                                foc_data[focus_keys[0]][i],
                                                foc_data[focus_keys[1]][i],
                                                foc_data[focus_keys[2]][i][0],
                                                (foc_data[focus_keys[3]][i]
                                                 - 1) * 1e6,
                                                foc_data[focus_keys[4]][i],
                                                foc_data[focus_keys[5]][i],
                                                foc_data[focus_keys[6]][i],
                                                foc_data[focus_keys[7]][i],
                                                foc_data[focus_keys[8]][i],
                                                foc_data[focus_keys[9]][i],
                                                foc_data[focus_keys[10]][i],
                                                foc_data[focus_keys[11]][i],
                                                foc_data[focus_keys[12]][i]))

    def get_weather(self):
        weather_keys = ['25m:guider:cartridgeLoaded:cartridgeID',
                        '25m:guider:cartridgeLoaded:plateID',
                        '25m:guider:survey:plateType',
                        '25m:apo:airTempPT', '25m:apo:dpTempPT',
                        '25m:apo:humidPT', '25m:apo:winds',
                        '25m:apo:windd', '25m:apo:dustb',
                        '25m:apo:irscsd', '25m:apo:irscmean']
        weather_data = {}
        for key in weather_keys:
            weather_data[key] = []
        for time in self.call_times:
            self.query(weather_keys, time.datetime, time.datetime,
                       weather_data)

        self.weather = '=' * 80 + '\n'
        self.weather += '{:^80}\n'.format('Weather Log')
        self.weather += '=' * 80 + '\n\n'
        self.weather += ('{:<5} {:<9} {:<5} {:<5} {:<4} {:<5} {:<4} {:<3}'
                         ' {:<6} {:<7} {:<5}'
                         '\n'.format('Time', 'Cart', 'Temp', 'DP', 'Diff',
                                     'Humid', 'Wind', 'Dir', '1\u03BCmDst',
                                     'IRSC\u03C3', 'IRSC\u03BC'))
        self.weather += '-' * 80 + '\n'
        for i, time in enumerate(self.call_times):
            if weather_data[weather_keys[2]][i] == '':
                continue
            self.weather += ('{:>5} {:>2}-{:0>5}{:>1} {:>5.1f} {:>5.1f}'
                             ' {:>4.1f} {:>5}'
                             ' {:>4.0f} {:>3.0f} {:>6.0f} {:>7.1f} {:>5.1f}'
                             '\n'.format(time.isot[11:16],
                                         weather_data[weather_keys[0]][i],
                                         weather_data[weather_keys[1]][i],
                                         weather_data[weather_keys[2]][i][0],
                                         weather_data[weather_keys[3]][i],
                                         weather_data[weather_keys[4]][i],
                                         weather_data[weather_keys[3]][i]
                                         - weather_data[weather_keys[4]][i],
                                         '{:>.0f}%'.format(
                                             weather_data[weather_keys[5]][i]),
                                         weather_data[weather_keys[6]][i],
                                         weather_data[weather_keys[7]][i],
                                         weather_data[weather_keys[8]][i],
                                         weather_data[weather_keys[9]][i],
                                         weather_data[weather_keys[10]][i]))

    def get_hartmann(self):
        data = self.telemetry.get('25m:hartmann:sp1Residuals:deg',
                                  (self.tstart - 0.3).datetime,
                                  (self.tend - 0.3).datetime,
                                  interpolation='raw',
                                  scan_archives=False)
        hart_times = data.times
        hart_times = Time(hart_times)
        hart_sorter = hart_times.argsort()
        hart_times = hart_times[hart_sorter]
        filt = self.tstart - 0.3 < hart_times
        hart_times = hart_times[filt]

        if self.args.verbose:
            print('hart_times: ', hart_times)
        hartmann_keys = ['25m:guider:cartridgeLoaded:cartridgeID',
                         '25m:guider:cartridgeLoaded:plateID',
                         '25m:guider:survey:plateType',
                         '25m:hartmann:r1PistonMove', '25m:hartmann:b1RingMove',
                         '25m:hartmann:sp1AverageMove',
                         '25m:hartmann:sp1Residuals:deg',
                         '25m:boss:sp1Temp:median']
        # '25m:hartmann:r2PistonMove', '25m:hartmann:b2RingMove
        # '25m:hartmann:sp2AverageMove',
        # '25m:hartmann:sp2Residuals:deg',
        # '25m:boss:sp2Temp:median']
        hart_data = {}
        for key in hartmann_keys:
            hart_data[key] = []
        for time in hart_times:
            self.query(hartmann_keys, time.datetime,
                       time.datetime,
                       hart_data)

        self.hartmann += '=' * 80 + '\n'
        self.hartmann += '{:^80}\n'.format('Hartmann Log')
        self.hartmann += '=' * 80 + '\n\n'
        self.hartmann += ('{:<5} {:<9} {:<5} {:<5} {:<5} {:<7} {:<4}'
                          '\n'.format('Time', 'Cart', 'R1', 'B1', 'Move1',
                                      'B1Resid', 'TSP1'))
        self.hartmann += '-' * 80 + '\n'
        for i, time in enumerate(hart_times):
            if hart_data[hartmann_keys[2]][i] == '':
                continue
            self.hartmann += ('{:>5} {:>2}-{:0>5}{:<1} {:>5.0f} {:>5.1f}'
                              ' {:>5.0f} {:>7.1f} {:>4.1f}'
                              '\n'.format(time.isot[11:16],
                                          hart_data[hartmann_keys[0]][i],
                                          hart_data[hartmann_keys[1]][i],
                                          hart_data[hartmann_keys[2]][i][0],
                                          hart_data[hartmann_keys[3]][i],
                                          hart_data[hartmann_keys[4]][i],
                                          hart_data[hartmann_keys[5]][i],
                                          hart_data[hartmann_keys[6]][i],
                                          hart_data[hartmann_keys[7]][i], ))
예제 #7
0
class TestArithmetic:
    """Arithmetic on Time objects, using both doubles."""
    kwargs = ({}, {'axis': None}, {'axis': 0}, {'axis': 1}, {'axis': 2})
    functions = ('min', 'max', 'sort')

    def setup(self):
        mjd = np.arange(50000, 50100, 10).reshape(2, 5, 1)
        frac = np.array([0.1, 0.1 + 1.e-15, 0.1 - 1.e-15, 0.9 + 2.e-16, 0.9])
        if use_masked_data:
            frac = np.ma.array(frac)
            frac[1] = np.ma.masked
        self.t0 = Time(mjd, frac, format='mjd', scale='utc')

        # Define arrays with same ordinal properties
        frac = np.array([1, 2, 0, 4, 3])
        if use_masked_data:
            frac = np.ma.array(frac)
            frac[1] = np.ma.masked
        self.t1 = Time(mjd + frac, format='mjd', scale='utc')
        self.jd = mjd + frac

    @pytest.mark.parametrize('kw, func', itertools.product(kwargs, functions))
    def test_argfuncs(self, kw, func, masked):
        """
        Test that ``np.argfunc(jd, **kw)`` is the same as ``t0.argfunc(**kw)``
        where ``jd`` is a similarly shaped array with the same ordinal properties
        but all integer values.  Also test the same for t1 which has the same
        integral values as jd.
        """
        t0v = getattr(self.t0, 'arg' + func)(**kw)
        t1v = getattr(self.t1, 'arg' + func)(**kw)
        jdv = getattr(np, 'arg' + func)(self.jd, **kw)

        if self.t0.masked and kw == {'axis': None} and func == 'sort':
            t0v = np.ma.array(t0v, mask=self.t0.mask.reshape(t0v.shape)[t0v])
            t1v = np.ma.array(t1v, mask=self.t1.mask.reshape(t1v.shape)[t1v])
            jdv = np.ma.array(jdv, mask=self.jd.mask.reshape(jdv.shape)[jdv])

        assert np.all(t0v == jdv)
        assert np.all(t1v == jdv)
        assert t0v.shape == jdv.shape
        assert t1v.shape == jdv.shape

    @pytest.mark.parametrize('kw, func', itertools.product(kwargs, functions))
    def test_funcs(self, kw, func, masked):
        """
        Test that ``np.func(jd, **kw)`` is the same as ``t1.func(**kw)`` where
        ``jd`` is a similarly shaped array and the same integral values.
        """
        t1v = getattr(self.t1, func)(**kw)
        jdv = getattr(np, func)(self.jd, **kw)
        assert np.all(t1v.value == jdv)
        assert t1v.shape == jdv.shape

    def test_argmin(self, masked):
        assert self.t0.argmin() == 2
        assert np.all(self.t0.argmin(axis=0) == 0)
        assert np.all(self.t0.argmin(axis=1) == 0)
        assert np.all(self.t0.argmin(axis=2) == 2)

    def test_argmax(self, masked):
        assert self.t0.argmax() == self.t0.size - 2
        if masked:
            # The 0 is where all entries are masked in that axis
            assert np.all(self.t0.argmax(axis=0) == [1, 0, 1, 1, 1])
            assert np.all(self.t0.argmax(axis=1) == [4, 0, 4, 4, 4])
        else:
            assert np.all(self.t0.argmax(axis=0) == 1)
            assert np.all(self.t0.argmax(axis=1) == 4)
        assert np.all(self.t0.argmax(axis=2) == 3)

    def test_argsort(self, masked):
        order = [2, 0, 4, 3, 1] if masked else [2, 0, 1, 4, 3]
        assert np.all(self.t0.argsort() == np.array(order))
        assert np.all(self.t0.argsort(axis=0) == np.arange(2).reshape(2, 1, 1))
        assert np.all(self.t0.argsort(axis=1) == np.arange(5).reshape(5, 1))
        assert np.all(self.t0.argsort(axis=2) == np.array(order))
        ravel = np.arange(50).reshape(-1, 5)[:, order].ravel()
        if masked:
            t0v = self.t0.argsort(axis=None)
            # Manually remove elements in ravel that correspond to masked
            # entries in self.t0.  This removes the 10 entries that are masked
            # which show up at the end of the list.
            mask = self.t0.mask.ravel()[ravel]
            ravel = ravel[~mask]
            assert np.all(t0v[:-10] == ravel)
        else:
            assert np.all(self.t0.argsort(axis=None) == ravel)

    @pytest.mark.parametrize('scale', Time.SCALES)
    def test_argsort_warning(self, masked, scale):
        if scale == 'utc':
            pytest.xfail()
        with warnings.catch_warnings(record=True) as wlist:
            Time([1, 2, 3], format='jd', scale=scale).argsort()
        assert len(wlist) == 0

    def test_min(self, masked):
        assert self.t0.min() == self.t0[0, 0, 2]
        assert np.all(self.t0.min(0) == self.t0[0])
        assert np.all(self.t0.min(1) == self.t0[:, 0])
        assert np.all(self.t0.min(2) == self.t0[:, :, 2])
        assert self.t0.min(0).shape == (5, 5)
        assert self.t0.min(0, keepdims=True).shape == (1, 5, 5)
        assert self.t0.min(1).shape == (2, 5)
        assert self.t0.min(1, keepdims=True).shape == (2, 1, 5)
        assert self.t0.min(2).shape == (2, 5)
        assert self.t0.min(2, keepdims=True).shape == (2, 5, 1)

    def test_max(self, masked):
        assert self.t0.max() == self.t0[-1, -1, -2]
        assert np.all(self.t0.max(0) == self.t0[1])
        assert np.all(self.t0.max(1) == self.t0[:, 4])
        assert np.all(self.t0.max(2) == self.t0[:, :, 3])
        assert self.t0.max(0).shape == (5, 5)
        assert self.t0.max(0, keepdims=True).shape == (1, 5, 5)

    def test_ptp(self, masked):
        assert self.t0.ptp() == self.t0.max() - self.t0.min()
        assert np.all(self.t0.ptp(0) == self.t0.max(0) - self.t0.min(0))
        assert self.t0.ptp(0).shape == (5, 5)
        assert self.t0.ptp(0, keepdims=True).shape == (1, 5, 5)

    def test_sort(self, masked):
        order = [2, 0, 4, 3, 1] if masked else [2, 0, 1, 4, 3]
        assert np.all(self.t0.sort() == self.t0[:, :, order])
        assert np.all(self.t0.sort(0) == self.t0)
        assert np.all(self.t0.sort(1) == self.t0)
        assert np.all(self.t0.sort(2) == self.t0[:, :, order])
        if not masked:
            assert np.all(self.t0.sort(None) == self.t0[:, :, order].ravel())
            # Bit superfluous, but good to check.
            assert np.all(self.t0.sort(-1)[:, :, 0] == self.t0.min(-1))
            assert np.all(self.t0.sort(-1)[:, :, -1] == self.t0.max(-1))
예제 #8
0
def plt_qlook_image(imres, figdir=None, specdata=None, verbose=True, stokes='I,V', fov=None):
    from matplotlib import pyplot as plt
    from sunpy import map as smap
    from sunpy import sun
    import astropy.units as u
    if not figdir:
        figdir = './'

    observatory = 'EOVSA'
    polmap = {'RR': 0, 'LL': 1, 'I': 0, 'V': 1}
    pols = stokes.split(',')
    npols = len(pols)
    # SRL = set(['RR', 'LL'])
    # SXY = set(['XX', 'YY', 'XY', 'YX'])
    Spw = sorted(list(set(imres['Spw'])))
    nspw = len(Spw)
    # Freq = set(imres['Freq']) ## list is an unhashable type
    Freq = sorted(uniq(imres['Freq']))

    plttimes = list(set(imres['BeginTime']))
    ntime = len(plttimes)
    # sort the imres according to time
    images = np.array(imres['ImageName'])
    btimes = Time(imres['BeginTime'])
    etimes = Time(imres['EndTime'])
    spws = np.array(imres['Spw'])
    suc = np.array(imres['Succeeded'])
    inds = btimes.argsort()
    images_sort = images[inds].reshape(ntime, nspw)
    btimes_sort = btimes[inds].reshape(ntime, nspw)
    suc_sort = suc[inds].reshape(ntime, nspw)
    spws_sort = spws[inds].reshape(ntime, nspw)
    if verbose:
        print '{0:d} figures to plot'.format(ntime)
    plt.ioff()
    import matplotlib.gridspec as gridspec
    spec = specdata['spec']
    (npol, nbl, nfreq, ntim) = spec.shape
    tidx = range(ntim)
    fidx = range(nfreq)
    tim = specdata['tim']
    freq = specdata['freq']
    freqghz = freq / 1e9
    pol = ''.join(pols)
    spec_tim = Time(specdata['tim'] / 3600. / 24., format='mjd')
    timstrr = spec_tim.plot_date
    if npols == 1:
        if pol == 'RR':
            spec_plt = spec[0, 0, :, :]
        elif pol == 'LL':
            spec_plt = spec[1, 0, :, :]
        elif pol == 'I':
            spec_plt = (spec[0, 0, :, :] + spec[1, 0, :, :]) / 2.
        elif pol == 'V':
            spec_plt = (spec[0, 0, :, :] - spec[1, 0, :, :]) / 2.
        spec_plt = [spec_plt]
        print 'plot the dynamic spectrum in pol ' + pol  # ax1 = fig.add_subplot(211)

        hnspw = nspw / 2
        ncols = hnspw
        nrows = 2 + 2  # 1 image: 1x1, 1 dspec:2x4
        fig = plt.figure(figsize=(8, 8))
        gs = gridspec.GridSpec(nrows, ncols)
        axs = [plt.subplot(gs[0, 0])]
        for ll in range(1, nspw):
            axs.append(plt.subplot(gs[ll / hnspw, ll % hnspw], sharex=axs[0], sharey=axs[0]))
        for ll in range(nspw):
            axs.append(plt.subplot(gs[ll / hnspw + 2, ll % hnspw], sharex=axs[0], sharey=axs[0]))
        axs_dspec = [plt.subplot(gs[2:, :])]
        cmaps = ['jet']
    elif npols == 2:
        R_plot = np.absolute(spec[0, 0, :, :])
        L_plot = np.absolute(spec[1, 0, :, :])
        if pol == 'RRLL':
            spec_plt = [R_plot, L_plot]
            polstr = ['RR', 'LL']
            cmaps = ['jet'] * 2
        if pol == 'IV':
            I_plot = (R_plot + L_plot) / 2.
            V_plot = (R_plot - L_plot) / 2.
            spec_plt = [I_plot, V_plot]
            polstr = ['I', 'V']
            cmaps = ['jet', 'RdBu']
        print 'plot the dynamic spectrum in pol ' + pol

        hnspw = nspw / 2
        ncols = hnspw + 2  # 1 image: 1x1, 1 dspec:2x2
        nrows = 2 + 2
        fig = plt.figure(figsize=(12, 8))
        gs = gridspec.GridSpec(nrows, ncols)
        axs = [plt.subplot(gs[0, 0])]
        for ll in range(1, nspw):
            axs.append(plt.subplot(gs[ll / hnspw, ll % hnspw], sharex=axs[0], sharey=axs[0]))
        for ll in range(nspw):
            axs.append(plt.subplot(gs[ll / hnspw + 2, ll % hnspw], sharex=axs[0], sharey=axs[0]))
        axs_dspec = [plt.subplot(gs[:2, hnspw:])]
        axs_dspec.append(plt.subplot(gs[2:, hnspw:], sharex=axs_dspec[0], sharey=axs_dspec[0]))

    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
    timetext = fig.text(0.01, 0.98, '', color='w', fontweight='bold', fontsize=12, ha='left', va='top')
    for i in range(ntime):
        plt.ioff()
        # plt.clf()
        for ax in axs:
            ax.cla()
        plttime = btimes_sort[i, 0]
        # tofd = plttime.mjd - np.fix(plttime.mjd)
        suci = suc_sort[i]
        # if tofd < 16. / 24. or sum(
        #         suci) < nspw - 2:  # if time of the day is before 16 UT (and 24 UT), skip plotting (because the old antennas are not tracking)
        #     continue
        # fig=plt.figure(figsize=(9,6))
        # fig.suptitle('EOVSA @ '+plttime.iso[:19])
        timetext.set_text(plttime.iso[:19])
        if verbose:
            print 'Plotting image at: ', plttime.iso

        if i == 0:
            dspecvspans = []
            for pol in range(npols):
                ax = axs_dspec[pol]
                ax.pcolormesh(timstrr, freqghz, spec_plt[pol], cmap=cmaps[pol])
                ax.xaxis_date()
                ax.xaxis.set_major_formatter(DateFormatter("%H:%M:%S"))
                # plt.xticks(rotation=45)
                ax.set_xlim(timstrr[tidx[0]], timstrr[tidx[-1]])
                ax.set_ylim(freqghz[fidx[0]], freqghz[fidx[-1]])
                ax.set_xlabel('Time [UT]')
                ax.set_ylabel('Frequency [GHz]')
                for idx, freq in enumerate(Freq):
                    ax.axhspan(freq[0], freq[1], linestyle='dotted', edgecolor='w', alpha=0.7, facecolor='none')
                    xtext, ytext = ax.transAxes.inverted().transform(ax.transData.transform([timstrr[tidx[0]], np.mean(freq)]))
                    ax.text(xtext + 0.01, ytext, 'spw ' + Spw[idx], color='w', transform=ax.transAxes, fontweight='bold', ha='left', va='center',
                            fontsize=8, alpha=0.5)
                ax.text(0.01, 0.98, 'Stokes ' + pols[pol], color='w', transform=ax.transAxes, fontweight='bold', ha='left', va='top')
                dspecvspans.append(ax.axvspan(btimes[i].plot_date, etimes[i].plot_date, color='w', alpha=0.4))
                ax_pos = ax.get_position().extents
                x0, y0, x1, y1 = ax_pos
                h, v = x1 - x0, y1 - y0
                x0_new = x0 + 0.15 * h
                y0_new = y0 + 0.15 * v
                x1_new = x1 - 0.05 * h
                y1_new = y1 - 0.05 * v
                ax.set_position(mpl.transforms.Bbox([[x0_new, y0_new], [x1_new, y1_new]]))
        else:
            for pol in range(npols):
                xy = dspecvspans[pol].get_xy()
                xy[:, 0][np.array([0, 1, 4])] = btimes[i].plot_date
                xy[:, 0][np.array([2, 3])] = etimes[i].plot_date
                dspecvspans[pol].set_xy(xy)

        for n in range(nspw):
            image = images_sort[i, n]
            # fig.add_subplot(nspw/3, 3, n+1)
            # fig.add_subplot(2, nspw / 2, n + 1)
            for pol in range(npols):
                if suci[n]:
                    try:
                        eomap = smap.Map(image)
                    except:
                        continue
                    sz = eomap.data.shape
                    if len(sz) == 4:
                        eomap.data = eomap.data[min(polmap[pols[pol]], eomap.meta['naxis4'] - 1), 0, :, :].reshape((sz[2], sz[3]))
                    # resample the image for plotting
                    if fov is not None:
                        fov = [np.array(ll) for ll in fov]
                        pad = max(np.diff(fov[0])[0], np.diff(fov[1])[0])
                        eomap = eomap.submap((fov[0] + np.array([-1.0, 1.0]) * pad) * u.arcsec, (fov[1] + np.array([-1.0, 1.0]) * pad) * u.arcsec)
                    else:
                        dim = u.Quantity([256, 256], u.pixel)
                        eomap = eomap.resample(dim)
                    eomap.plot_settings['cmap'] = plt.get_cmap(cmaps[pol])
                    # import pdb
                    # pdb.set_trace()
                    eomap.plot(axes=axs[n + nspw * pol])
                    eomap.draw_limb()
                    eomap.draw_grid()
                    ax = plt.gca()
                    ax.set_autoscale_on(False)
                    if fov:
                        # pass
                        ax.set_xlim(fov[0])
                        ax.set_ylim(fov[1])
                    else:
                        ax.set_xlim([-1080, 1080])
                        ax.set_ylim([-1080, 1080])
                    spwran = spws_sort[i, n]
                    # freqran = [int(s) * 0.5 + 2.9 for s in spwran.split('~')]
                    # if len(freqran) == 1:
                    #     ax.text(0.98, 0.01, '{0:.1f} GHz'.format(freqran[0]), color='w',
                    #             transform=ax.transAxes, fontweight='bold', ha='right')
                    # else:
                    #     ax.text(0.98, 0.01, '{0:.1f} - {1:.1f} GHz'.format(freqran[0], freqran[1]), color='w',
                    #             transform=ax.transAxes, fontweight='bold', ha='right')
                    ax.text(0.98, 0.01, 'Stokes {1} @ {0:.3f} GHz'.format(eomap.meta['crval3'] / 1e9, pols[pol]), color='w', transform=ax.transAxes,
                            fontweight='bold', ha='right')
                    ax.set_title(' ')
                    # ax.set_title('spw '+spws_sort[i,n])
                    # ax.text(0.01,0.02, plttime.isot,transform=ax.transAxes,color='white')
                    ax.xaxis.set_visible(False)
                    ax.yaxis.set_visible(False)
                else:
                    # make an empty map
                    data = np.zeros((512, 512))
                    header = {"DATE-OBS": plttime.isot, "EXPTIME": 0., "CDELT1": 5., "NAXIS1": 512, "CRVAL1": 0., "CRPIX1": 257, "CUNIT1": "arcsec",
                              "CTYPE1": "HPLN-TAN", "CDELT2": 5., "NAXIS2": 512, "CRVAL2": 0., "CRPIX2": 257, "CUNIT2": "arcsec",
                              "CTYPE2": "HPLT-TAN", "HGLT_OBS": sun.heliographic_solar_center(plttime)[1].value, "HGLN_OBS": 0.,
                              "RSUN_OBS": sun.solar_semidiameter_angular_size(plttime).value, "RSUN_REF": sun.constants.radius.value,
                              "DSUN_OBS": sun.sunearth_distance(plttime).to(u.meter).value, }
                    eomap = smap.Map(data, header)
                    # resample the image for plotting
                    if fov:
                        fov = [np.array(ll) for ll in fov]
                        pad = max(np.diff(fov[0])[0], np.diff(fov[1])[0])
                        try:
                            eomap = eomap.submap((fov[0] + np.array([-1.0, 1.0]) * pad) * u.arcsec, (fov[1] + np.array([-1.0, 1.0]) * pad) * u.arcsec)
                        except:
                            x0, x1 = fov[0] + np.array([-1.0, 1.0]) * pad
                            y0, y1 = fov[1] + np.array([-1.0, 1.0]) * pad
                            bl = SkyCoord(x0 * u.arcsec, y0 * u.arcsec, frame=eomap.coordinate_frame)
                            tr = SkyCoord(x1 * u.arcsec, y1 * u.arcsec, frame=eomap.coordinate_frame)
                            eomap = eomap.submap(bl, tr)
                    else:
                        dim = u.Quantity([256, 256], u.pixel)
                        eomap = eomap.resample(dim)
                    eomap.plot_settings['cmap'] = plt.get_cmap(cmaps[pol])
                    eomap.plot(axes=axs[n + nspw * pol])
                    eomap.draw_limb()
                    eomap.draw_grid()
                    ax = plt.gca()
                    ax.set_autoscale_on(False)
                    if fov:
                        # pass
                        ax.set_xlim(fov[0])
                        ax.set_ylim(fov[1])
                    else:
                        ax.set_xlim([-1080, 1080])
                        ax.set_ylim([-1080, 1080])
                    # ax.set_title('spw '+spwran+'( )'))
                    spwran = spws_sort[i, n]
                    freqran = [int(s) * 0.5 + 2.9 for s in spwran.split('~')]
                    spwran = spws_sort[i, n]
                    # ax.set_title('{0:.1f} - {1:.1f} GHz'.format(freqran[0],freqran[1]))
                    # ax.text(0.98, 0.01, '{0:.1f} - {1:.1f} GHz'.format(freqran[0], freqran[1]), color='w',
                    #         transform=ax.transAxes, fontweight='bold', ha='right')
                    ax.text(0.98, 0.01, 'Stokes {1} @ {0:.3f} GHz'.format(0., pols[pol]), color='w', transform=ax.transAxes, fontweight='bold',
                            ha='right')
                    ax.set_title(' ')

                    # ax.text(0.01,0.02, plttime.isot,transform=ax.transAxes,color='white')
                    ax.xaxis.set_visible(False)
                    ax.yaxis.set_visible(False)
        figname = observatory + '_qlimg_' + plttime.isot.replace(':', '').replace('-', '')[:19] + '.png'
        fig_tdt = plttime.to_datetime()
        # fig_subdir = fig_tdt.strftime("%Y/%m/%d/")
        figdir_ = figdir  # + fig_subdir
        if not os.path.exists(figdir_):
            os.makedirs(figdir_)
        if verbose:
            print 'Saving plot to: ' + os.path.join(figdir_, figname)
        plt.savefig(os.path.join(figdir_, figname))
    plt.close(fig)
    DButil.img2html_movie(figdir_)
예제 #9
0
class TestArithmetic():
    """Arithmetic on Time objects, using both doubles."""
    kwargs = ({}, {'axis': None}, {'axis': 0}, {'axis': 1}, {'axis': 2})
    functions = ('min', 'max', 'sort')

    def setup(self):
        mjd = np.arange(50000, 50100, 10).reshape(2, 5, 1)
        frac = np.array([0.1, 0.1+1.e-15, 0.1-1.e-15, 0.9+2.e-16, 0.9])
        if use_masked_data:
            frac = np.ma.array(frac)
            frac[1] = np.ma.masked
        self.t0 = Time(mjd, frac, format='mjd', scale='utc')

        # Define arrays with same ordinal properties
        frac = np.array([1, 2, 0, 4, 3])
        if use_masked_data:
            frac = np.ma.array(frac)
            frac[1] = np.ma.masked
        self.t1 = Time(mjd + frac, format='mjd', scale='utc')
        self.jd = mjd + frac

    @pytest.mark.parametrize('kw, func', itertools.product(kwargs, functions))
    def test_argfuncs(self, kw, func, masked):
        """
        Test that np.argfunc(jd, **kw) is the same as t0.argfunc(**kw) where
        jd is a similarly shaped array with the same ordinal properties but
        all integer values.  Also test the same for t1 which has the same
        integral values as jd.
        """
        t0v = getattr(self.t0, 'arg' + func)(**kw)
        t1v = getattr(self.t1, 'arg' + func)(**kw)
        jdv = getattr(np, 'arg' + func)(self.jd, **kw)

        if self.t0.masked and kw == {'axis': None} and func == 'sort':
            t0v = np.ma.array(t0v, mask=self.t0.mask.reshape(t0v.shape)[t0v])
            t1v = np.ma.array(t1v, mask=self.t1.mask.reshape(t1v.shape)[t1v])
            jdv = np.ma.array(jdv, mask=self.jd.mask.reshape(jdv.shape)[jdv])

        assert np.all(t0v == jdv)
        assert np.all(t1v == jdv)
        assert t0v.shape == jdv.shape
        assert t1v.shape == jdv.shape

    @pytest.mark.parametrize('kw, func', itertools.product(kwargs, functions))
    def test_funcs(self, kw, func, masked):
        """
        Test that np.func(jd, **kw) is the same as t1.func(**kw) where
        jd is a similarly shaped array and the same integral values.
        """
        t1v = getattr(self.t1, func)(**kw)
        jdv = getattr(np, func)(self.jd, **kw)
        assert np.all(t1v.value == jdv)
        assert t1v.shape == jdv.shape

    def test_argmin(self, masked):
        assert self.t0.argmin() == 2
        assert np.all(self.t0.argmin(axis=0) == 0)
        assert np.all(self.t0.argmin(axis=1) == 0)
        assert np.all(self.t0.argmin(axis=2) == 2)

    def test_argmax(self, masked):
        assert self.t0.argmax() == self.t0.size - 2
        if masked:
            # The 0 is where all entries are masked in that axis
            assert np.all(self.t0.argmax(axis=0) == [1, 0, 1, 1, 1])
            assert np.all(self.t0.argmax(axis=1) == [4, 0, 4, 4, 4])
        else:
            assert np.all(self.t0.argmax(axis=0) == 1)
            assert np.all(self.t0.argmax(axis=1) == 4)
        assert np.all(self.t0.argmax(axis=2) == 3)

    def test_argsort(self, masked):
        order = [2, 0, 4, 3, 1] if masked else [2, 0, 1, 4, 3]
        assert np.all(self.t0.argsort() == np.array(order))
        assert np.all(self.t0.argsort(axis=0) == np.arange(2).reshape(2, 1, 1))
        assert np.all(self.t0.argsort(axis=1) == np.arange(5).reshape(5, 1))
        assert np.all(self.t0.argsort(axis=2) == np.array(order))
        ravel = np.arange(50).reshape(-1, 5)[:, order].ravel()
        if masked:
            t0v = self.t0.argsort(axis=None)
            # Manually remove elements in ravel that correspond to masked
            # entries in self.t0.  This removes the 10 entries that are masked
            # which show up at the end of the list.
            mask = self.t0.mask.ravel()[ravel]
            ravel = ravel[~mask]
            assert np.all(t0v[:-10] == ravel)
        else:
            assert np.all(self.t0.argsort(axis=None) == ravel)

    def test_min(self, masked):
        assert self.t0.min() == self.t0[0, 0, 2]
        assert np.all(self.t0.min(0) == self.t0[0])
        assert np.all(self.t0.min(1) == self.t0[:, 0])
        assert np.all(self.t0.min(2) == self.t0[:, :, 2])
        assert self.t0.min(0).shape == (5, 5)
        assert self.t0.min(0, keepdims=True).shape == (1, 5, 5)
        assert self.t0.min(1).shape == (2, 5)
        assert self.t0.min(1, keepdims=True).shape == (2, 1, 5)
        assert self.t0.min(2).shape == (2, 5)
        assert self.t0.min(2, keepdims=True).shape == (2, 5, 1)

    def test_max(self, masked):
        assert self.t0.max() == self.t0[-1, -1, -2]
        assert np.all(self.t0.max(0) == self.t0[1])
        assert np.all(self.t0.max(1) == self.t0[:, 4])
        assert np.all(self.t0.max(2) == self.t0[:, :, 3])
        assert self.t0.max(0).shape == (5, 5)
        assert self.t0.max(0, keepdims=True).shape == (1, 5, 5)

    def test_ptp(self, masked):
        assert self.t0.ptp() == self.t0.max() - self.t0.min()
        assert np.all(self.t0.ptp(0) == self.t0.max(0) - self.t0.min(0))
        assert self.t0.ptp(0).shape == (5, 5)
        assert self.t0.ptp(0, keepdims=True).shape == (1, 5, 5)

    def test_sort(self, masked):
        order = [2, 0, 4, 3, 1] if masked else [2, 0, 1, 4, 3]
        assert np.all(self.t0.sort() == self.t0[:, :, order])
        assert np.all(self.t0.sort(0) == self.t0)
        assert np.all(self.t0.sort(1) == self.t0)
        assert np.all(self.t0.sort(2) == self.t0[:, :, order])
        if not masked:
            assert np.all(self.t0.sort(None) ==
                          self.t0[:, :, order].ravel())
            # Bit superfluous, but good to check.
            assert np.all(self.t0.sort(-1)[:, :, 0] == self.t0.min(-1))
            assert np.all(self.t0.sort(-1)[:, :, -1] == self.t0.max(-1))