Beispiel #1
0
    def from_levelb(cls, levelb, parent=''):
        packets, idb_versions = GenericProduct.getLeveL0Packets(levelb)

        control = Control()
        control['scet_coarse'] = packets.get('scet_coarse')
        control['scet_fine'] = packets.get('scet_fine')
        control['index'] = np.arange(len(control)).astype(get_min_uint(len(control)))

        # When the packets are parsed empty packets are dropped but in LB we don't parse so this
        # is not known need to compare control and levelb.control and only use matching rows
        if len(levelb.control) > len(control):
            matching_index = np.argwhere(
                np.in1d(levelb.control['scet_coarse'], np.array(packets.get('scet_coarse'))))
            control['raw_file'] = levelb.control['raw_file'][matching_index].reshape(-1)
            control['packet'] = levelb.control['packet'][matching_index].reshape(-1)
        else:
            control['raw_file'] = levelb.control['raw_file'].reshape(-1)
            control['packet'] = levelb.control['packet'].reshape(-1)

        control['parent'] = parent

        tmp = Data()
        tmp.add_basic(name='ubsd_counter', nix='NIX00285', packets=packets, dtype=np.uint32)
        tmp.add_basic(name='pald_counter', nix='NIX00286', packets=packets, dtype=np.uint32)
        tmp.add_basic(name='num_flares', nix='NIX00294', packets=packets, dtype=np.uint16)

        colnames = ['start_scet_coarse', 'end_scet_coarse', 'highest_flareflag', 'tm_byte_volume',
                    'average_z_loc', 'average_y_loc', 'processing_mask']

        flares = Data()
        if tmp['num_flares'].sum() > 0:
            flares.add_basic(name='start_scet_coarse', nix='NIX00287', packets=packets)
            flares.add_basic(name='end_scet_coarse', nix='NIX00288', packets=packets)
            flares.add_basic(name='highest_flareflag', nix='NIX00289', packets=packets,
                             dtype=np.byte)
            flares.add_basic(name='tm_byte_volume', nix='NIX00290', packets=packets, dtype=np.byte)
            flares.add_basic(name='average_z_loc', nix='NIX00291', packets=packets, dtype=np.byte)
            flares.add_basic(name='average_y_loc', nix='NIX00292', packets=packets, dtype=np.byte)
            flares.add_basic(name='processing_mask', nix='NIX00293', packets=packets, dtype=np.byte)

        tmp_data = defaultdict(list)
        start = 0
        for i, (ubsd, pald, n_flares) in enumerate(tmp):
            end = start + n_flares
            if n_flares == 0:
                tmp_data['control_index'].append(i)
                tmp_data['coarse'].append(control['scet_coarse'][i])
                tmp_data['fine'].append(control['scet_fine'][i])
                tmp_data['ubsd'].append(ubsd)
                tmp_data['pald'].append(pald)
                for name in colnames:
                    tmp_data[name].append(0)
            else:
                tmp_data['control_index'].append([i] * n_flares)
                tmp_data['coarse'].append([control['scet_coarse'][i]] * n_flares)
                tmp_data['fine'].append([control['scet_fine'][i]] * n_flares)
                ubsd['ubsd']([ubsd] * n_flares)
                pald['pald'].append([pald] * n_flares)
                for name in colnames:
                    tmp_data[name].extend(flares[name][start:end])

            start = end

        data = Data(tmp_data)
        data['time'] = SCETime(tmp_data['coarse'], tmp_data['fine'])
        data['timedel'] = SCETimeDelta(np.full(len(data), 0), np.full(len(data), 0))
        data.remove_columns(['coarse', 'fine'])

        return cls(service_type=packets.service_type,
                   service_subtype=packets.service_subtype,
                   ssid=packets.ssid,
                   control=control,
                   data=data,
                   idb_versions=idb_versions,
                   packets=packets)
Beispiel #2
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)