Example #1
0
    def fetch(self):
        """Fetch each  channel and combine into  a multichannel instance
        of :py:class:`~pyfusion.data.timeseries.TimeseriesData`.

        :rtype: :py:class:`~pyfusion.data.timeseries.TimeseriesData`
        """
 
        ## initially, assume only single channel signals
        # this base debug breakpoint will apply to all flavours of acquisition
        debug_(pyfusion.DEBUG, level=3, key='entry_base_multi_fetch')
        ordered_channel_names = self.ordered_channel_names()
        data_list = []
        channels = ChannelList()  # empty I guess
        common_tb = None  # common_timebase
        meta_dict={}

        group_utc = None  # assume no utc, will be replaced by channels
        # t_min, t_max seem obsolete, and should be done in single chan fetch
        if hasattr(self, 't_min') and hasattr(self, 't_max'):
            t_range = [float(self.t_min), float(self.t_max)]
        else:
            t_range = []
        for chan in ordered_channel_names:
            sgn = 1
            if chan[0]=='-': sgn = -sgn
            bare_chan = (chan.split('-'))[-1]
            ch_data = self.acq.getdata(self.shot, bare_chan)
            if len(t_range) == 2:
                ch_data = ch_data.reduce_time(t_range)

            channels.append(ch_data.channels)
            # two tricky things here - tmp.data.channels only gets one channel hhere
            # Config_name for a channel is attached to the multi part -
            # We need to move it to the particular channel 
            # Was  channels[-1].config_name = chan
            # 2013 - default to something if config_name not defined
            if pyfusion.VERBOSE>0:
                print("base:multi ch_data.config_name", ch_data.config_name)
            if hasattr(ch_data,'config_name'):
                channels[-1].config_name = ch_data.config_name                
            else:
                channels[-1].config_name = 'fix_me'
            meta_dict.update(ch_data.meta)
            # tack on the data utc
            ch_data.signal.utc = ch_data.utc

            # Make a common timebase and do some basic checks.
            if common_tb is None:
                common_tb = ch_data.timebase
                if hasattr(ch_data,'utc'):
                    group_utc = ch_data.utc
                    tb_chan = ch_data.channels

                # for the first, append the whole signal
                data_list.append(sgn * ch_data.signal)
            else:
                if hasattr(self, 'skip_timebase_check') and self.skip_timebase_check == 'True':
                    # append regardless, but will have to go back over
                    # later to check length cf common_tb
                    if pyfusion.VERBOSE > 0: print('utcs: ******',ch_data.utc[0],group_utc[0])
                    if ch_data.utc[0] != group_utc[0]:
                        dts = (ch_data.utc[0] - group_utc[0])/1e9
                        print('*** different start times *****\n********trace {chcnf} starts after {tbch} by {dts:.2g} s'
                              .format(tbch = tb_chan.config_name, chcnf = ch_data.config_name, dts=dts))
                        # should pad this channel out with nans - for now report an error.
                        # this won't work if combining signals on a common_tb
                        # ch_data.timebase += -dts  # not sure if + or -?
                        # print(ch_data.timebase[0])
                        """ kind of works for L53_LP0701 309 13, but time error
                        dtclock = 2e-6
                        nsampsdiff = int(round(dts/dtclock,0))
                        newlen = len(ch_data.timebase) + nsampsdiff
                        newsig = np.array(newlen * [np.nan])
                        newtb = np.array(newlen * [np.nan])
                        newsig[nsampsdiff:] = ch_data.signal
                        newtb[nsampsdiff:] = ch_data.timebase
                        ch_data.signal = newsig
                        ch_data.timebase = newtb
                        ch_data.timebase += -dts  # not sure if + or -?
                        """

                    if len(ch_data.signal)<len(common_tb):
                        common_tb = ch_data.timebase
                        tb_chan = ch_data.channels
                    data_list.append(ch_data.signal[0:len(common_tb)])
                else:
                    try:
                        assert_array_almost_equal(common_tb, ch_data.timebase)
                        data_list.append(ch_data.signal)
                    except:
                        print('####  matching error in {c} - perhaps timebase not the same as the previous channel'.format(c=ch_data.config_name))
                        raise 

        if hasattr(self, 'skip_timebase_check') and self.skip_timebase_check == 'True':
            #  Messy - if we ignored timebase checks, then we have to check
            #  length and cut to size, otherwise it will be stored as a signal (and 
            #  we will end up with a signal of signals)
            #  This code may look unpythonic, but it avoids a copy()
            #  and may be faster than for sig in data_list....
            ltb = len(common_tb)
            for i in range(len(data_list)):
                if len(data_list[i]) > ltb:
                    # this is a replacement.
                    data_list.insert(i,data_list.pop(i)[0:ltb])

        signal = Signal(data_list)
        print(shape(signal))

        output_data = TimeseriesData(signal=signal, timebase=common_tb,
                                     channels=channels)
        #output_data.meta.update({'shot':self.shot})
        output_data.meta.update(meta_dict)
        output_data.comment = self.comment if hasattr(self, 'comment') else ''
        print(output_data.comment)
        #if not hasattr(output_data,'utc'):
        #    output_data.utc = None  # probably should try to get it
        #                           # from a channel - but how?
        output_data.utc = group_utc   # should check that all channels have same utc
        debug_(pyfusion.DEBUG, level=2, key='return_base_multi_fetch')        
        return output_data