示例#1
0
 def add_parameter(self, name, magnitude, units='', **kwargs):
     '''
     Add a parameter to the parameters database for a solute
     
     See pyEQL.parameters documentation for a description of the arguments
     
     '''
     import pyEQL.parameter as pm
     newparam = pm.Parameter(name, magnitude, units, **kwargs)
     db.add_parameter(self.get_name(), newparam)
示例#2
0
    def search_parameters(self, formula):
        '''Each time a new solute species is created in a solution, this function:
        
        1) searches to see whether a list of parameters for the species has already been
        compiled from the database
        2) searches all files in the specified database directory(ies) for the species
        3) creates a Parameter object for each value found 
        4) compiles these objects into a set
        5) adds the set to a dictionary indexed by species name (formula)
        6) points the new solute object to the dictionary
        
        formula : str
                String representing the chemical formula of the species.
        '''
        # get the hill_order() and is_valid_formula() methods from the chemistry module
        import pyEQL.chemical_formula as chem

        # if the formula is already in the database, then we've already searched
        # and compiled parameters, so there is no need to do it again.
        if formula in self.parameters_database:
            pass
        else:
            # add an entry to the parameters database
            self.parameters_database[formula] = set()

            # search all the files in each database directory
            for directory in self.database_dir:
                for file in os.listdir(directory):
                    # reset the line number count
                    line_num = 0

                    # ignore the template file
                    if file == 'template.tsv':
                        continue

                    # look at only .csv files
                    if ".tsv" in file:

                        # open each file
                        current_file = open(directory + '/' + file, 'r')

                        # read each line of the file, looking for the formula
                        try:
                            for line in current_file:

                                line_num += 1

                                try:
                                    # look for keywords in the first column of each file. If found,
                                    # store the entry from the 2nd column
                                    if 'Name' in line:
                                        param_name = _parse_line(line)[1]

                                    elif 'Description' in line:
                                        param_desc = _parse_line(line)[1]

                                    elif 'Unit' in line:
                                        param_unit = _parse_line(line)[1]

                                    elif 'Reference' in line:
                                        param_ref = _parse_line(line)[1]

                                    elif 'Temperature' in line:
                                        param_temp = _parse_line(line)[1]

                                    elif 'Pressure' in line:
                                        param_press = _parse_line(line)[1]

                                    elif 'Ionic Strength' in line:
                                        param_ionic = _parse_line(line)[1]

                                    elif 'Comment' in line:
                                        param_comment = _parse_line(line)[1]

                                    # use the hill_order() function to standardize the
                                    # supplied formula. Then standardize teh formulas in the
                                    # database and see if they match.
                                    # this allows a database entry for 'MgCl2' to be matched
                                    # even if the user enters 'Mg(Cl)2', for example
                                    elif chem.is_valid_formula(
                                            _parse_line(line)[0]):
                                        if chem.hill_order(
                                                formula) == chem.hill_order(
                                                    _parse_line(line)[0]):
                                            # if there are multiple columns, pass the values as a list.
                                            # If a single column, then just pass the value
                                            if len(_parse_line(line)) > 2:
                                                param_value = _parse_line(
                                                    line)[1:]
                                            else:
                                                param_value = _parse_line(
                                                    line)[1]

                                            # Create a new parameter object
                                            parameter = pm.Parameter(param_name,param_value,param_unit, \
                                            reference=param_ref,pressure=param_press,temperature=param_temp,ionic_strength=param_ionic,description=param_desc,comment=param_comment)

                                            # Add the parameter to the set for this species
                                            self.parameters_database[
                                                formula].add(parameter)

                                except ValueError:
                                    logger.warning(
                                        'Error encountered when reading line %s in %s'
                                        % (line_num, file))
                                    continue

                        # log a warning if an invalid character prevents reading a line
                        except UnicodeDecodeError:
                            logger.warning(
                                'Invalid character found when reading %s. File skipped.'
                                % file)

                        current_file.close()