<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def return_dat(self, chan, begsam, endsam):
 """Read the data based on begsam and endsam. Parameters chan : list of int list of channel indeces begsam : int index of the first sample endsam : index of the last sample Returns ------- ndarray 2-d matrix with data (might contain NaN) Notes ----- The sample numbering is not based on the samples in the files (i.e. the first sample of the first file is NOT the first sample of the dataset) because it depends on the stamps in the STC file. Usually, the recording starts and after a few millisecond (maybe one second), the actual acquisition starts. STC takes the offset into account. This has the counterintuitive result that if you call read_data, the first few hundreds samples are nan. """

        dat = empty((len(chan), endsam - begsam))
        dat.fill(NaN)

        stc, all_stamp = _read_stc(self._filename.with_suffix('.stc'))

        all_erd = all_stamp['segment_name'].astype('U')  # convert to str
        all_beg = all_stamp['start_stamp']
        all_end = all_stamp['end_stamp']

        try:
            begrec = where((all_end >= begsam))[0][0]
            endrec = where((all_beg < endsam))[0][-1]
        except IndexError:
            return dat

        for rec in range(begrec, endrec + 1):

            begpos_rec = max(begsam, all_beg[rec])
            endpos_rec = min(endsam, all_end[rec] + 1)  # check + 1

            # this looks weird, but it takes into account whether the values
            # are outside of the limits of the file
            d1 = begpos_rec - begsam
            d2 = endpos_rec - begsam

            erd_file = (Path(self.filename) / all_erd[rec]).with_suffix('.erd')

            try:
                dat_rec = _read_erd(erd_file, begpos_rec, endpos_rec)
                dat[:, d1:d2] = dat_rec[chan, :]
            except (FileNotFoundError, PermissionError):
                lg.warning('{} does not exist'.format(erd_file))

        return dat