Exemplo n.º 1
0
    def recoil_params(self, plot=False):
        '''
        Calculate recoil parameters, SFD, iSFD, eSFD,... add them to instance
        as attributes
        '''
        result = copy.copy(self)
        # Split recoil into return loops as used by hystools.recoil
        Hsplit, Msplit = [], []
        for h, m in zip(self.H, self.M):
            try:
                [h1, h2], [m1, m2] = hys.split(h, m)
                if len(h2) > 0.8*len(h1):
                    # don't append loops that haven't returned at least 80%
                    # Cut out first 10 points, where drift is happening
                    Hsplit.append(h2[10:])
                    Msplit.append(m2[10:])
            except:
                # split failed, probably incomplete loop.
                pass

        rec_params = hys.recoil(Hsplit, Msplit, startdef=1)

        paramstosave = ['SFD', 'iSFD', 'eSFD', 'H_zminor', 'M_zminor']
        # map to these attribute names
        maptoattrib  = ['SFD', 'iSFD', 'eSFD', 'H_zminor', 'M_zminor']

        for p, m in zip(paramstosave, maptoattrib):
            setattr(result, m, rec_params[p])

        if self.verbose:
            paramstoprint = ['SFD', 'iSFD', 'eSFD']
            unitstoprint = ['Oe', 'Oe', 'Oe']
            for param, unit in zip(paramstoprint, unitstoprint):
                print(param.ljust(4) + ': {:.2f} {}'.format(rec_params[param], unit))

        if plot:
            # make summary plot
            #plt.figure()

            ishold = plt.ishold()
            plt.plot(self.H[0], self.M[0])
            plt.hold(True)
            for h, m in zip(Hsplit[1:], Msplit[1:]):
                plt.plot(h, m)
            plt.plot(rec_params['H_zminor'], rec_params['M_zminor'], color='black', linewidth=3)
            plt.scatter(*zip(*rec_params['plotpts']), c='g', s=30, zorder=3)
            plt.grid(True)
            plt.xlabel('Field (Oe)')
            plt.ylabel('M (uemu)')
            plt.title(os.path.split(self.filepath)[1])
            plt.hold(ishold)

        return result
Exemplo n.º 2
0
    def importdata(self, filepath, split=True, **kwargs):
        # Read header and locate start of data
        global bgfilepath
        header = []
        with open(filepath, 'r') as f:
            # Name the enumerator, so it can be used after breaking from loop
            fenum = enumerate(f)
            for num, line in fenum:
                if line == '[Data]\n':
                    skip = num + 2
                    break
                else:
                    header.append(line)
            #colnames = fenum.next()[1].split(',')

        # Just import Temp, H and M from col 2, 3, and 4
        try:
            T, H, M = np.genfromtxt(filepath, skip_header=skip, delimiter=',',
                                 unpack=True, usecols=(2,3,4))
            # drop nan
            nan = np.isnan(M)
            M = M[~nan]
            H = H[~nan]
            T = T[~nan]
        except:
            # Data is invalid, or empty
            self.H = []
            self.M = []
            self.T = []
            self.filepath = filepath
            print('Failed to load data from ' + filepath)
            self.log = 'Failed to load data.\n'
            return

        # Get sample area from header
        try:
            area = float(header[10].split(',')[1][:5])
        except:
            area = None
            if not filepath == bgfilepath and self.verbose:
                print('Area could not be found in file header.')

        # convert to uemu
        M = M * 1000000

        if split:
            # split h and m into segments
            Hsplit, Msplit = hys.split(H, M, shortest=20)
            # split T too, but hys.split sucks
            Hsplit, Tsplit = hys.split(H, T, shortest=20)

            # Remove first and last few points
            for i in range(len(Hsplit)):
                Hsplit[i] = Hsplit[i][2:-2]
                Msplit[i] = Msplit[i][2:-2]
                Tsplit[i] = Tsplit[i][2:-2]

            # put the segments back together in groups of two to form loops
            Hloops = []
            Mloops = []
            Tloops = []
            for j in range(0, len(Hsplit), 2):
                try:
                    Hloops.append(np.append(Hsplit[j], Hsplit[j+1]))
                    Mloops.append(np.append(Msplit[j], Msplit[j+1]))
                    Tloops.append(np.append(Tsplit[j], Tsplit[j+1]))
                except:
                    Hloops.append(Hsplit[j])
                    Mloops.append(Msplit[j])
                    Tloops.append(Tsplit[j])

            self.M = Mloops
            self.H = Hloops
            self.T = Tloops
        else:
            # Leave M, H, T unsplit, list with len 1
            self.M = [M]
            self.H = [H]
            self.T = [T]

        self.filepath = filepath
        self.area = area
        self._thickness = None
        self.header = header
        self.log = '{}: PyPPMS version {}\n'.format(_importtime, _version)
        self.log += '{}: Data imported from {}\n'.format(_now(), filepath)

        if not filepath == bgfilepath:
            print('Imported {} loops from {}'.format(len(self.H), os.path.split(filepath)[1]))