Beispiel #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
Beispiel #2
0
def load_isotopedb_defaults(db):
    with db.session_ctx() as sess:
        for name, mass in MOLECULAR_WEIGHTS.iteritems():
            db.add_molecular_weight(name, mass)

        for at in ['blank_air',
                   'blank_cocktail',
                   'blank_unknown',
                   'background', 'air', 'cocktail', 'unknown']:
        #                           blank', 'air', 'cocktail', 'background', 'unknown']:
            db.add_analysis_type(at)

        for mi in ['obama', 'jan', 'nmgrl map']:
            db.add_mass_spectrometer(mi)

        project = db.add_project('references')
        #print project
        for i, di in enumerate(['blank_air',
                                'blank_cocktail',
                                'blank_unknown',
                                'background', 'air', 'cocktail']):
            samp = db.add_sample(di, project=project)
            #print samp.id, samp, project.id
            #            samp.project = project
            #samp.project_id=project.id
            #print samp.project_id
            db.add_labnumber(i + 1, sample=samp)
        sess.commit()

        for hi, kind, make in [('Fusions CO2', '10.6um co2', 'photon machines'),
                               ('Fusions Diode', '810nm diode', 'photon machines'),
                               ('Fusions UV', '193nm eximer', 'photon machines')
        ]:
            db.add_extraction_device(name=hi,
                                     kind=kind,
                                     make=make,
            )

        mdir = os.path.join(paths.setup_dir, 'irradiation_tray_maps')
        mdir=paths.irradiation_tray_maps_dir
        # if not os.path.isdir(mdir):
        #     if information(None, 'No irradiation_tray_maps directory. add to .../setupfiles'):
        #         try:
        #             os.mkdir(mdir)
        #         except OSError,e:
        #             warning(None, 'Failed making {}: error={}'.format(mdir, e))
        #
        # else:
        for p, name in iterdir(mdir, exclude=('.zip',)):
            load_irradiation_map(db, p, name)

        mdir = paths.map_dir
        for p, name in iterdir(mdir):
            _load_tray_map(db, p, name)

        for t in ('ok', 'invalid'):
            db.add_tag(t, user='******')
Beispiel #3
0
    def map_dac_to_isotope(self, dac=None, det=None, current=True):
        if dac is None:
            dac = self._dac
        if det is None:
            det = self.detector

        if det:
            dac = self.spectrometer.uncorrect_dac(det, dac, current=current)

        m = self.map_dac_to_mass(dac, det.name)
        molweights = self.spectrometer.molecular_weights
        return next((k for k, v in molweights.iteritems() if abs(v - m) < 0.001), None)
    def load(self, db_mol_weights=True):
        self.debug('******************************* LOAD Spec')
        if db_mol_weights:
            # get the molecular weights from the database
            dbm = IsotopeDatabaseManager(application=self.application,
                                         warn=False)
            self.db = dbm
            if dbm.isConnected():
                self.info('loading molecular_weights from database')
                mws = dbm.db.get_molecular_weights()
                # convert to a dictionary
                m = dict([(mi.name, mi.mass) for mi in mws])
                self.spectrometer.molecular_weights = m

        if not self.spectrometer.molecular_weights:
            import csv
            # load the molecular weights dictionary
            p = os.path.join(paths.spectrometer_dir, 'molecular_weights.csv')
            if os.path.isfile(p):
                self.info('loading "molecular_weights.csv" file')
                with open(p, 'U') as f:
                    reader = csv.reader(f, delimiter='\t')
                    args = [[l[0], float(l[1])] for l in reader]
                    self.spectrometer.molecular_weights = dict(args)
            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 mw

                with open(p, 'U') as f:
                    writer = csv.writer(f, delimiter='\t')
                    data = [a for a in mw.itervalues()]
                    data = sorted(data, key=lambda x: x[1])
                    for row in data:
                        writer.writerow(row)
                self.spectrometer.molecular_weights = mw

        #config = self.get_configuration(path=os.path.join(paths.spectrometer_dir, 'detectors.cfg'))
        #for name in config.sections():
        #    relative_position = self.config_get(config, name, 'relative_position', cast='float')
        #    color = self.config_get(config, name, 'color', default='black')
        #    default_state = self.config_get(config, name, 'default_state', default=True, cast='boolean')
        #    isotope = self.config_get(config, name, 'isotope')
        #    kind = self.config_get(config, name, 'kind', default='Faraday', optional=True)
        #    self.spectrometer.add_detector(name=name,
        #                                   relative_position=relative_position,
        #                                   color=color,
        #                                   active=default_state,
        #                                   isotope=isotope,
        #                                   kind=kind)
        self.spectrometer.load()

        return True
    def load(self, db_mol_weights=True):
        self.debug('******************************* LOAD Spec')
        if db_mol_weights:
            # get the molecular weights from the database
            dbm = IsotopeDatabaseManager(application=self.application,
                                         warn=False)
            self.db = dbm
            if dbm.isConnected():
                self.info('loading molecular_weights from database')
                mws = dbm.db.get_molecular_weights()
                # convert to a dictionary
                m = dict([(mi.name, mi.mass) for mi in mws])
                self.spectrometer.molecular_weights = m

        if not self.spectrometer.molecular_weights:
            import csv
            # load the molecular weights dictionary
            p = os.path.join(paths.spectrometer_dir, 'molecular_weights.csv')
            if os.path.isfile(p):
                self.info('loading "molecular_weights.csv" file')
                with open(p, 'U') as f:
                    reader = csv.reader(f, delimiter='\t')
                    args = [[l[0], float(l[1])] for l in reader]
                    self.spectrometer.molecular_weights = dict(args)
            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 mw

                with open(p, 'U') as f:
                    writer = csv.writer(f, delimiter='\t')
                    data = [a for a in mw.itervalues()]
                    data = sorted(data, key=lambda x: x[1])
                    for row in data:
                        writer.writerow(row)
                self.spectrometer.molecular_weights = mw

        #config = self.get_configuration(path=os.path.join(paths.spectrometer_dir, 'detectors.cfg'))
        #for name in config.sections():
        #    relative_position = self.config_get(config, name, 'relative_position', cast='float')
        #    color = self.config_get(config, name, 'color', default='black')
        #    default_state = self.config_get(config, name, 'default_state', default=True, cast='boolean')
        #    isotope = self.config_get(config, name, 'isotope')
        #    kind = self.config_get(config, name, 'kind', default='Faraday', optional=True)
        #    self.spectrometer.add_detector(name=name,
        #                                   relative_position=relative_position,
        #                                   color=color,
        #                                   active=default_state,
        #                                   isotope=isotope,
        #                                   kind=kind)
        self.spectrometer.load()

        return True
Beispiel #6
0
def load_isotopedb_defaults(db):
    with db.session_ctx() as sess:
        for name, mass in MOLECULAR_WEIGHTS.iteritems():
            db.add_molecular_weight(name, mass)

        populate_isotopes(db)

        for at in ['blank_air',
                   'blank_cocktail',
                   'blank_unknown',
                   'background', 'air', 'cocktail', 'unknown']:
            #                           blank', 'air', 'cocktail', 'background', 'unknown']:
            db.add_analysis_type(at)

        # for mi in ['obama', 'jan', 'nmgrl map']:
        #     db.add_mass_spectrometer(mi)

        project = db.add_project('REFERENCES')
        # print project
        for i, di in enumerate(['blank_air',
                                'blank_cocktail',
                                'blank_unknown',
                                'background', 'air', 'cocktail']):
            samp = db.add_sample(di, project=project)
            # print samp.id, samp, project.id
            #            samp.project = project
            # samp.project_id=project.id
            # print samp.project_id
            # db.add_labnumber(i + 1, sample=samp)
        sess.commit()

        # for hi, kind, make in [('Fusions CO2', '10.6um co2', 'photon machines'),
        #                        ('Fusions Diode', '810nm diode', 'photon machines'),
        #                        ('Fusions UV', '193nm eximer', 'photon machines')]:
        #     db.add_extraction_device(name=hi,
        #                              kind=kind,
        #                              make=make)

        mdir = paths.irradiation_tray_maps_dir
        for p, name in iterdir(mdir, exclude=('.zip',)):
            og = False
            load_irradiation_map(db, p, name, overwrite_geometry=og)

        mdir = paths.map_dir
        for p, name in iterdir(mdir):
            try:
                _load_tray_map(db, p, name)
            except BaseException:
                print 'failed loading tray map "{}", "{}"'.format(p, name)

        for t in ('ok', 'invalid'):
            db.add_tag(t, user='******')
Beispiel #7
0
    def load(self, db_mol_weights=True):
        spec = self.spectrometer
        mftable = spec.magnet.mftable

        self.debug('******************************* LOAD Spec')
        if db_mol_weights:
            # get the molecular weights from the database
            # dbm = IsotopeDatabaseManager(application=self.application,
            #                              warn=False)
            dbm = self.application.get_service(
                'pychron.database.isotope_database_manager.IsotopeDatabaseManager'
            )
            if dbm and dbm.is_connected():
                self.info('loading molecular_weights from database')
                mws = dbm.db.get_molecular_weights()
                # convert to a dictionary
                m = dict([(mi.name, mi.mass) for mi in mws])
                spec.molecular_weights = m
                mftable.db = dbm.db

        if not spec.molecular_weights:
            import csv
            # load the molecular weights dictionary
            p = os.path.join(paths.spectrometer_dir, 'molecular_weights.csv')
            if os.path.isfile(p):
                self.info('loading "molecular_weights.csv" file')
                with open(p, 'U') as f:
                    reader = csv.reader(f, delimiter='\t')
                    args = [[l[0], float(l[1])] for l in reader]
                    spec.molecular_weights = dict(args)
            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 mw

                with open(p, 'U') as f:
                    writer = csv.writer(f, delimiter='\t')
                    data = [a for a in mw.itervalues()]
                    data = sorted(data, key=lambda x: x[1])
                    for row in data:
                        writer.writerow(row)
                spec.molecular_weights = mw

        self.spectrometer.load()
        mftable.spectrometer_name = self.spectrometer.name

        return True
Beispiel #8
0
    def load(self, db_mol_weights=True):
        spec = self.spectrometer
        mftable = spec.magnet.mftable

        self.debug("******************************* LOAD Spec")
        if db_mol_weights:
            # get the molecular weights from the database
            # dbm = IsotopeDatabaseManager(application=self.application,
            #                              warn=False)
            dbm = self.application.get_service("pychron.database.isotope_database_manager.IsotopeDatabaseManager")
            if dbm and dbm.is_connected():
                self.info("loading molecular_weights from database")
                mws = dbm.db.get_molecular_weights()
                # convert to a dictionary
                m = dict([(mi.name, mi.mass) for mi in mws])
                spec.molecular_weights = m
                mftable.db = dbm.db

        if not spec.molecular_weights:
            import csv

            # load the molecular weights dictionary
            p = os.path.join(paths.spectrometer_dir, "molecular_weights.csv")
            if os.path.isfile(p):
                self.info('loading "molecular_weights.csv" file')
                with open(p, "U") as f:
                    reader = csv.reader(f, delimiter="\t")
                    args = [[l[0], float(l[1])] for l in reader]
                    spec.molecular_weights = dict(args)
            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 mw

                with open(p, "U") as f:
                    writer = csv.writer(f, delimiter="\t")
                    data = [a for a in mw.itervalues()]
                    data = sorted(data, key=lambda x: x[1])
                    for row in data:
                        writer.writerow(row)
                spec.molecular_weights = mw

        self.spectrometer.load()
        mftable.spectrometer_name = self.spectrometer.name

        return True
Beispiel #9
0
    def map_dac_to_isotope(self, dac=None, det=None, current=True):
        """
        convert a dac voltage to isotope name for a given detector


        :param dac: float, voltage
        :param det: str, detector name
        :param current: bool, get current hv
        :return: str, e.g Ar40
        """
        if dac is None:
            dac = self._dac
        if det is None:
            det = self.detector

        if det:
            dac = self.spectrometer.uncorrect_dac(det, dac, current=current)

        m = self.map_dac_to_mass(dac, det.name)
        if m is not None:
            molweights = self.spectrometer.molecular_weights
            return next((k for k, v in molweights.iteritems() if abs(v - m) < 0.001), None)
Beispiel #10
0
    def map_dac_to_isotope(self, dac=None, det=None, current=True):
        """
        convert a dac voltage to isotope name for a given detector


        :param dac: float, voltage
        :param det: str, detector name
        :param current: bool, get current hv
        :return: str, e.g Ar40
        """
        if dac is None:
            dac = self._dac
        if det is None:
            det = self.detector

        if det:
            dac = self.spectrometer.uncorrect_dac(det, dac, current=current)

        m = self.map_dac_to_mass(dac, det.name)
        if m is not None:
            molweights = self.spectrometer.molecular_weights
            return next((k for k, v in molweights.iteritems() if abs(v - m) < 0.001), None)
Beispiel #11
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')
        mws = None
        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')
                try:
                    mws = {l[0]: float(l[1]) for l in reader}
                except BaseException:
                    mws = None
        elif os.path.isfile(yp):
            self.info('loading "molecular_weights.yaml" file. {}'.format(yp))
            with open(yp, 'r') as f:
                try:
                    mws = yaml.load(f)
                except BaseException:
                    mws = None

        if mws is None:
            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, 'w') as f:
                writer = csv.writer(f, delimiter='\t')
                data = sorted(mws.items(), key=lambda x: x[1])
                for row in data:
                    writer.writerow(row)

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