Ejemplo n.º 1
0
    def fetch_data(self, mnemonic, starttime, endtime):
        """
        Get data for a mnemonic.

        Parameters
        mnemonic: str
            The engineering mnemonic to retrieve

        starttime: str or astropy.time.Time
            The, inclusive, start time to retireve from.

        endttime: str or astropy.time.Time
            The, inclusive, end time to retireve from.

        Returns
        -------
        mnemonic_data: dict
            The returned structure
        """
        with open(os.path.join(self.db_path, mnemonic_data_fname(mnemonic)),
                  'r') as fp:
            db_data = json.load(fp)

        # Find the range of data
        stime = Time(starttime, format='iso')
        etime = Time(endtime, format='iso')
        stime_mil = int(stime.unix * 1000)
        etime_mil = int(etime.unix * 1000)

        data = db_data['Data']
        start_idx = 0
        try:
            while engdb_tools.extract_db_time(
                    data[start_idx]['ObsTime']) < stime_mil:
                start_idx += 1
        except IndexError:
            pass
        end_idx = start_idx
        try:
            while engdb_tools.extract_db_time(
                    data[end_idx]['ObsTime']) <= etime_mil:
                end_idx += 1
        except IndexError:
            end_idx -= 1

        # Real database always returns a bracketed result.
        start_idx = max(0, start_idx - 1)
        end_idx = min(len(data), end_idx + 1)

        # Construct the resultant data
        data_return = [data[idx] for idx in range(start_idx, end_idx)]

        # Construct the return structure
        db_data['ReqSTime'] = '/Date({:013d}+0000)/'.format(stime_mil)
        db_data['ReqETime'] = '/Date({:013d}+0000)/'.format(etime_mil)
        db_data['Count'] = len(data_return)
        db_data['Data'] = data_return

        return db_data
Ejemplo n.º 2
0
def test_cache_partial_data(db_cache, engdb):
    """
    Test read of some data.
    """
    data = db_cache.fetch_data(GOOD_MNEMONIC, GOOD_STARTTIME, GOOD_ENDTIME)
    assert data['Count'] > 4  # Just to make sure we have some data
    endtime = Time(engdb_tools.extract_db_time(data['Data'][1]['ObsTime']) /
                   1000.,
                   format='unix')

    data_short = db_cache.fetch_data(GOOD_MNEMONIC, GOOD_STARTTIME,
                                     endtime.iso)
    live_data_short = engdb.get_records(GOOD_MNEMONIC, GOOD_STARTTIME,
                                        endtime.iso)
    assert data_short == live_data_short
Ejemplo n.º 3
0
    def fetch_data(self, mnemonic, starttime, endtime):
        """
        Get data for a mnemonic.

        Parameters
        mnemonic: str
            The engineering mnemonic to retrieve

        starttime: str or astropy.time.Time
            The, inclusive, start time to retireve from.

        endttime: str or astropy.time.Time
            The, inclusive, end time to retireve from.

        Returns
        -------
        mnemonic_data: dict
            The returned structure
        """
        with open(
                os.path.join(
                    self.db_path,
                    mnemonic_data_fname(mnemonic)),
                'r') as fp:
            db_data = json.load(fp)

        # Find the range of data
        stime = Time(starttime, format='iso')
        etime = Time(endtime, format='iso')
        stime_mil = int(stime.unix * 1000)
        etime_mil = int(etime.unix * 1000)

        data = db_data['Data']
        start_idx = 0
        try:
            while engdb_tools.extract_db_time(data[start_idx]['ObsTime']) < stime_mil:
                start_idx += 1
        except IndexError:
            pass
        end_idx = start_idx
        try:
            while engdb_tools.extract_db_time(data[end_idx]['ObsTime']) <= etime_mil:
                end_idx += 1
        except IndexError:
            end_idx -= 1

        # Real database always returns a bracketed result.
        start_idx = max(0, start_idx - 1)
        end_idx = min(len(data), end_idx + 1)

        # Construct the resultant data
        data_return = [
            data[idx]
            for idx in range(start_idx, end_idx)
        ]

        # Construct the return structure
        db_data['ReqSTime'] = '/Date({:013d}+0000)/'.format(stime_mil)
        db_data['ReqETime'] = '/Date({:013d}+0000)/'.format(etime_mil)
        db_data['Count'] = len(data_return)
        db_data['Data'] = data_return

        return db_data
Ejemplo n.º 4
0
def test_db_time():
    time = 1234567890123
    stime = ''.join(['/Date(', str(time), '+1234', ')/'])
    result = engdb_tools.extract_db_time(stime)
    assert result == time