Exemple #1
0
    def from_levelb(cls, levelb, parent=''):
        packets, idb_versions, control = ScienceProduct.from_levelb(
            levelb, parent=parent)

        control.add_data('compression_scheme_counts_skm',
                         _get_compression_scheme(packets, 'NIX00260'))

        control.add_data('compression_scheme_triggers_skm',
                         _get_compression_scheme(packets, 'NIX00242'))

        data = Data()
        try:
            data['delta_time'] = np.uint32(
                packets.get_value('NIX00441').to(u.ds))
            data.add_meta(name='delta_time', nix='NIX00441', packets=packets)
        except AttributeError:
            data['delta_time'] = np.uint32(
                packets.get_value('NIX00404').to(u.ds))
            data.add_meta(name='delta_time', nix='NIX00404', packets=packets)
        unique_times = np.unique(data['delta_time'])

        data.add_basic(name='rcr',
                       nix='NIX00401',
                       attr='value',
                       packets=packets,
                       dtype=np.ubyte)
        data['num_pixel_sets'] = np.atleast_1d(
            _get_unique(packets, 'NIX00442', np.ubyte))
        data.add_meta(name='num_pixel_sets', nix='NIX00442', packets=packets)
        pixel_masks, pm_meta = _get_pixel_mask(packets, 'NIXD0407')
        pixel_masks = pixel_masks.reshape(-1, data['num_pixel_sets'][0], 12)
        if packets.ssid == 21 and data['num_pixel_sets'][0] != 12:
            pixel_masks = np.pad(pixel_masks,
                                 ((0, 0), (0, 12 - data['num_pixel_sets'][0]),
                                  (0, 0)))
        data.add_data('pixel_masks', (pixel_masks, pm_meta))
        data.add_data('detector_masks', _get_detector_mask(packets))
        # NIX00405 in BSD is 1 indexed
        data['integration_time'] = SCETimeDelta(packets.get_value('NIX00405'))
        data.add_meta(name='integration_time', nix='NIX00405', packets=packets)

        triggers = np.array(
            [packets.get_value(f'NIX00{i}') for i in range(242, 258)])
        triggers_var = np.array([
            packets.get_value(f'NIX00{i}', attr='error')
            for i in range(242, 258)
        ])

        data['triggers'] = triggers.T.astype(get_min_uint(triggers))
        data['triggers'].meta = {
            'NIXS': [f'NIX00{i}' for i in range(242, 258)]
        }
        data['triggers_err'] = np.float32(np.sqrt(triggers_var).T)
        data.add_basic(name='num_energy_groups',
                       nix='NIX00258',
                       packets=packets,
                       dtype=np.ubyte)

        tmp = dict()
        tmp['e_low'] = np.array(packets.get_value('NIXD0016'), np.ubyte)
        tmp['e_high'] = np.array(packets.get_value('NIXD0017'), np.ubyte)
        tmp['num_data_elements'] = np.array(packets.get_value('NIX00259'))
        unique_energies_low = np.unique(tmp['e_low'])
        unique_energies_high = np.unique(tmp['e_high'])

        counts = np.array(packets.get_value('NIX00260'))
        counts_var = np.array(packets.get_value('NIX00260', attr='error'))

        counts = counts.reshape(unique_times.size, unique_energies_low.size,
                                data['detector_masks'][0].sum(),
                                data['num_pixel_sets'][0].sum())

        counts_var = counts_var.reshape(unique_times.size,
                                        unique_energies_low.size,
                                        data['detector_masks'][0].sum(),
                                        data['num_pixel_sets'][0].sum())
        # t x e x d x p -> t x d x p x e
        counts = counts.transpose((0, 2, 3, 1))

        out_counts = None
        out_var = None

        counts_var = np.sqrt(counts_var.transpose((0, 2, 3, 1)))
        if packets.ssid == 21:
            out_counts = np.zeros((unique_times.size, 32, 12, 32))
            out_var = np.zeros((unique_times.size, 32, 12, 32))
        elif packets.ssid == 22:
            out_counts = np.zeros((unique_times.size, 32, 12, 32))
            out_var = np.zeros((unique_times.size, 32, 12, 32))

        dl_energies = np.array([
            [ENERGY_CHANNELS[lch].e_lower, ENERGY_CHANNELS[hch].e_upper]
            for lch, hch in zip(unique_energies_low, unique_energies_high)
        ]).reshape(-1)
        dl_energies = np.unique(dl_energies)
        sci_energies = np.hstack(
            [[ENERGY_CHANNELS[ch].e_lower for ch in range(32)],
             ENERGY_CHANNELS[31].e_upper])

        # If there is any onboard summing of energy channels rebin back to standard sci channels
        if (unique_energies_high - unique_energies_low).sum() > 0:
            rebinned_counts = np.zeros((*counts.shape[:-1], 32))
            rebinned_counts_var = np.zeros((*counts_var.shape[:-1], 32))
            e_ch_start = 0
            e_ch_end = counts.shape[-1]
            if dl_energies[0] == 0.0:
                rebinned_counts[..., 0] = counts[..., 0]
                rebinned_counts_var[..., 0] = counts_var[..., 0]
                e_ch_start += 1
            elif dl_energies[-1] == np.inf:
                rebinned_counts[..., -1] = counts[..., -1]
                rebinned_counts_var[..., -1] = counts_var[..., -1]
                e_ch_end -= 1

            torebin = np.where((dl_energies >= 4.0) & (dl_energies <= 150.0))
            rebinned_counts[..., 1:-1] = np.apply_along_axis(
                rebin_proportional, -1,
                counts[...,
                       e_ch_start:e_ch_end].reshape(-1, e_ch_end - e_ch_start),
                dl_energies[torebin], sci_energies[1:-1]).reshape(
                    (*counts.shape[:-1], 30))

            rebinned_counts_var[..., 1:-1] = np.apply_along_axis(
                rebin_proportional, -1,
                counts_var[..., e_ch_start:e_ch_end].reshape(
                    -1, e_ch_end - e_ch_start), dl_energies[torebin],
                sci_energies[1:-1]).reshape((*counts_var.shape[:-1], 30))

            energy_indices = np.full(32, True)
            energy_indices[[0, -1]] = False

            ix = np.ix_(np.full(unique_times.size, True),
                        data['detector_masks'][0].astype(bool),
                        np.ones(data['num_pixel_sets'][0], dtype=bool),
                        np.full(32, True))

            out_counts[ix] = rebinned_counts
            out_var[ix] = rebinned_counts_var
        else:
            energy_indices = np.full(32, False)
            energy_indices[unique_energies_low.min(
            ):unique_energies_high.max() + 1] = True

            ix = np.ix_(np.full(unique_times.size,
                                True), data['detector_masks'][0].astype(bool),
                        np.ones(data['num_pixel_sets'][0], dtype=bool),
                        energy_indices)

            out_counts[ix] = counts
            out_var[ix] = counts_var

        if counts.sum() != out_counts.sum():
            raise ValueError(
                'Original and reformatted count totals do not match')

        control['energy_bin_mask'] = np.full((1, 32), False, np.ubyte)
        all_energies = set(np.hstack([tmp['e_low'], tmp['e_high']]))
        control['energy_bin_mask'][:, list(all_energies)] = True

        # only fix here as data is send so needed for extraction but will be all zeros
        data['detector_masks'] = fix_detector_mask(control,
                                                   data['detector_masks'])

        sub_index = np.searchsorted(data['delta_time'], unique_times)
        data = data[sub_index]

        data['time'] = (control['time_stamp'][0] + data['delta_time'] +
                        data['integration_time'] / 2)
        data['timedel'] = data['integration_time']
        data['counts'] = \
            (out_counts * u.ct).astype(get_min_uint(out_counts))[..., :tmp['e_high'].max()+1]
        data.add_meta(name='counts', nix='NIX00260', packets=packets)
        data['counts_err'] = np.float32(out_var *
                                        u.ct)[..., :tmp['e_high'].max() + 1]
        data['control_index'] = control['index'][0]

        data = data['time', 'timedel', 'rcr', 'pixel_masks', 'detector_masks',
                    'num_pixel_sets', 'num_energy_groups', 'triggers',
                    'triggers_err', 'counts', 'counts_err']
        data['control_index'] = np.ubyte(0)

        return cls(service_type=packets.service_type,
                   service_subtype=packets.service_subtype,
                   ssid=packets.ssid,
                   control=control,
                   data=data,
                   idb_versions=idb_versions)
Exemple #2
0
    def from_levelb(cls, levelb, parent=''):
        packets, idb_versions, control = ScienceProduct.from_levelb(
            levelb, parent=parent)

        control.add_data('compression_scheme_counts_skm',
                         _get_compression_scheme(packets, 'NIX00263'))

        control.add_data('compression_scheme_triggers_skm',
                         _get_compression_scheme(packets, 'NIX00242'))

        data = Data()
        try:
            data['delta_time'] = packets.get_value('NIX00441')
            data.add_meta(name='delta_time', nix='NIX00441', packets=packets)
        except AttributeError:
            data['delta_time'] = packets.get_value('NIX00404')
            data.add_meta(name='delta_time', nix='NIX00404', packets=packets)
        data['control_index'] = np.full(len(data['delta_time']), 0)
        unique_times = np.unique(data['delta_time'])

        # time = np.array([])
        # for dt in set(self.delta_time):
        #     i, = np.where(self.delta_time == dt)
        #     nt = sum(np.array(packets['NIX00258'])[i])
        #     time = np.append(time, np.repeat(dt, nt))
        # self.time = time

        data.add_basic(name='rcr',
                       nix='NIX00401',
                       attr='value',
                       packets=packets)

        data.add_data('pixel_mask1', _get_pixel_mask(packets, 'NIXD0407'))
        data.add_data('pixel_mask2', _get_pixel_mask(packets, 'NIXD0444'))
        data.add_data('pixel_mask3', _get_pixel_mask(packets, 'NIXD0445'))
        data.add_data('pixel_mask4', _get_pixel_mask(packets, 'NIXD0446'))
        data.add_data('pixel_mask5', _get_pixel_mask(packets, 'NIXD0447'))
        data.add_data('detector_masks', _get_detector_mask(packets))
        data['detector_masks'] = fix_detector_mask(control,
                                                   data['detector_masks'])
        # NIX00405 in BSD is 1 indexed
        data['integration_time'] = packets.get_value('NIX00405')
        data.add_meta(name='integration_time', nix='NIX00405', packets=packets)

        triggers = []
        triggers_var = []
        for i in range(242, 258):
            triggers.extend(packets.get_value(f'NIX00{i}'))
            triggers_var.extend(packets.get_value(f'NIX00{i}', attr='error'))

        data['triggers'] = np.array(triggers).reshape(-1, 16)
        data['triggers'].meta = {
            'NIXS': [f'NIX00{i}' for i in range(242, 258)]
        }  # ,
        #                         'PCF_CURTX': [packets.get(f'NIX00{i}')[0].idb_info.PCF_CURTX
        #                                       for i in range(242, 258)]}
        data['triggers_err'] = np.sqrt(triggers_var).reshape(-1, 16)

        tids = np.searchsorted(data['delta_time'], unique_times)
        data = data[tids]

        # sum(packets.get_value('NIX00258'))

        # Data
        e_low = np.array(packets.get_value('NIXD0016'))
        e_high = np.array(packets.get_value('NIXD0017'))

        # TODO create energy bin mask
        control['energy_bin_mask'] = np.full((1, 32), False, np.ubyte)
        all_energies = set(np.hstack([e_low, e_high]))
        control['energy_bin_mask'][:, list(all_energies)] = True

        data['flux'] = np.array(packets.get_value('NIX00261')).reshape(
            unique_times.size, -1)
        data.add_meta(name='flux', nix='NIX00261', packets=packets)
        num_detectors = packets.get_value('NIX00262')[0]
        data['detector_id'] = np.array(packets.get_value('NIX00100')).reshape(
            unique_times.size, -1, num_detectors)

        data['real'] = packets.get_value('NIX00263').reshape(
            unique_times.size, num_detectors, -1)
        data.add_meta(name='real', nix='NIX00263', packets=packets)
        data['real_err'] = np.sqrt(
            packets.get_value('NIX00263',
                              attr='error').reshape(unique_times.size,
                                                    num_detectors, -1))
        data.add_meta(name='real_err', nix='NIX00263', packets=packets)
        data['imaginary'] = packets.get_value('NIX00264').reshape(
            unique_times.size, num_detectors, -1)
        data.add_meta(name='imaginary', nix='NIX00264', packets=packets)
        data['imaginary_err'] = np.sqrt(
            packets.get_value('NIX00264',
                              attr='error').reshape(unique_times.size,
                                                    num_detectors, -1))
        data.add_meta(name='imaginary', nix='NIX00264', packets=packets)

        data['time'] = (control["time_stamp"][0] + data['delta_time'] +
                        data['integration_time'] / 2)
        data['timedel'] = SCETimeDelta(data['integration_time'])

        return cls(service_type=packets.service_type,
                   service_subtype=packets.service_subtype,
                   ssid=packets.ssid,
                   control=control,
                   data=data,
                   idb_versions=idb_versions)
Exemple #3
0
    def from_levelb(cls, levelb, parent=''):
        packets, idb_versions, control = ScienceProduct.from_levelb(
            levelb, parent=parent)

        data = Data()
        data['start_time'] = packets.get_value('NIX00404').astype(np.uint32)
        data.add_meta(name='start_time', nix='NIX00404', packets=packets)
        data.add_basic(name='rcr',
                       nix='NIX00401',
                       attr='value',
                       packets=packets,
                       dtype=np.ubyte)
        # NIX00405 in BSD is 1 indexed
        data['integration_time'] = packets.get_value('NIX00405').astype(
            np.uint16)
        data.add_meta(name='integration_time', nix='NIX00405', packets=packets)
        data.add_data('pixel_masks', _get_pixel_mask(packets, 'NIXD0407'))
        data.add_data('detector_masks', _get_detector_mask(packets))
        data['triggers'] = np.array(
            [packets.get_value(f'NIX00{i}') for i in range(408, 424)]).T
        data['triggers'].dtype = get_min_uint(data['triggers'])
        data['triggers'].meta = {
            'NIXS': [f'NIX00{i}' for i in range(408, 424)]
        }
        data.add_basic(name='num_samples',
                       nix='NIX00406',
                       packets=packets,
                       dtype=np.uint16)

        num_detectors = 32
        num_energies = 32
        num_pixels = 12

        # Data
        tmp = dict()
        tmp['pixel_id'] = np.array(packets.get_value('NIXD0158'), np.ubyte)
        tmp['detector_id'] = np.array(packets.get_value('NIXD0153'), np.ubyte)
        tmp['channel'] = np.array(packets.get_value('NIXD0154'), np.ubyte)
        tmp['continuation_bits'] = np.array(packets.get_value('NIXD0159'),
                                            np.ubyte)

        control['energy_bin_mask'] = np.full((1, 32), False, np.ubyte)
        all_energies = set(tmp['channel'])
        control['energy_bin_mask'][:, list(all_energies)] = True

        # Find contiguous time indices
        unique_times = np.unique(data['start_time'])
        time_indices = np.searchsorted(unique_times, data['start_time'])

        counts_1d = packets.get_value('NIX00065')

        end_inds = np.cumsum(data['num_samples'])
        start_inds = np.hstack([0, end_inds[:-1]])
        dd = [(tmp['pixel_id'][s:e], tmp['detector_id'][s:e],
               tmp['channel'][s:e], counts_1d[s:e])
              for s, e in zip(start_inds.astype(int), end_inds)]

        counts = np.zeros(
            (len(unique_times), num_detectors, num_pixels, num_energies),
            np.uint32)
        for i, (pid, did, cid, cc) in enumerate(dd):
            counts[time_indices[i], did, pid, cid] = cc

        data['detector_masks'] = fix_detector_mask(control,
                                                   data['detector_masks'])

        sub_index = np.searchsorted(data['start_time'], unique_times)
        data = data[sub_index]
        data['time'] = control["time_stamp"][0] \
            + data['start_time'] + data['integration_time'] / 2
        data['timedel'] = SCETimeDelta(data['integration_time'])
        data['counts'] = (counts * u.ct).astype(get_min_uint(counts))
        # data.add_meta(name='counts', nix='NIX00065', packets=packets)
        data['control_index'] = control['index'][0]

        data.remove_columns(['start_time', 'integration_time', 'num_samples'])

        return cls(service_type=packets.service_type,
                   service_subtype=packets.service_subtype,
                   ssid=packets.ssid,
                   control=control,
                   data=data,
                   idb_versions=idb_versions)