示例#1
0
    def load_molecular_weights(self):
        import csv, yaml
        # load the molecular weights dictionary

        p = os.path.join(paths.spectrometer_dir, 'molecular_weights.csv')
        yp = os.path.join(paths.spectrometer_dir, 'molecular_weights.yaml')
        if os.path.isfile(p):
            self.info('loading "molecular_weights.csv" file. {}'.format(p))
            with open(p, 'r') as f:
                reader = csv.reader(f, delimiter='\t')
                mws = {l[0]: float(l[1]) for l in reader}
        elif os.path.isfile(yp):
            self.info('loading "molecular_weights.yaml" file. {}'.format(yp))
            with open(yp, 'r') as f:
                mws = yaml.load(f)
        else:
            self.info('writing a default "molecular_weights.csv" file')
            # make a default molecular_weights.csv file
            from pychron.spectrometer.molecular_weights import MOLECULAR_WEIGHTS as mws

            with open(p, 'U' if os.path.isfile(p) else 'w') as f:
                writer = csv.writer(f, delimiter='\t')
                # data = [a for a in six.itervalues(mws)]
                data = [a for a in mws.values()]
                data = sorted(data, key=lambda x: x[1])
                for row in data:
                    writer.writerow(row)

        self.debug('Mol weights {}'.format(mws))
        self.molecular_weights = mws