Пример #1
0
def open_cif(param, phase):
    """
	Open a cif file a build a structure for phase number "phase"
	filename: param['structure_phase_%i' %phase]
	
	returns 
	- a structure with cif information
	
	sets
	- param['sgno_phase_%i' %phase] : space group number
	- param['sgname_phase_%i' %phase] : space group name
	- param['cell_choice_phase_%i' %phase] : not 100% sure
	- param['unit_cell_phase_%i' %phase] : unit cell parameters as [a,b,c,alpha,beta,gamma']
	
	Adapted from polyxsim.structure
	Created: 12/2019, S. Merkel, Univ. Lille, France
	"""
    file = param['structure_phase_%i' % phase]
    cf = ReadCif(
        file
    )  # Generate an error if reading cif fails which is not always true below
    struct = structure.build_atomlist()
    struct.CIFread(ciffile=file)
    param['sgno_phase_%i' % phase] = sg.sg(sgname=struct.atomlist.sgname).no
    param['sgname_phase_%i' % phase] = struct.atomlist.sgname
    param['cell_choice_phase_%i' %
          phase] = sg.sg(sgname=struct.atomlist.sgname).cell_choice
    param['unit_cell_phase_%i' % phase] = struct.atomlist.cell
    return struct
Пример #2
0
    def CIFopen(self, ciffile=None, cifblkname=None):
        from CifFile import ReadCif  # part of the PycifRW module
        try:
            # the following is a trick to avoid that urllib.URLopen.open
            # used by ReadCif misinterprets the url when a drive:\ is
            # present (Win)
            ciffile = ciffile.replace(':', '|')
            cf = ReadCif(ciffile)
        except:
            logging.error('File %s could not be accessed' % ciffile)

        if cifblkname == None:
            #Try to guess blockname
            blocks = list(cf.keys())
            if len(blocks) > 1:
                if len(blocks) == 2 and 'global' in blocks:
                    cifblkname = blocks[abs(blocks.index('global') - 1)]
                else:
                    logging.error('More than one possible data set:')
                    logging.error(
                        'The following data block names are in the file:')
                    for block in blocks:
                        logging.error(block)
                    raise Exception
            else:
                # Only one available
                cifblkname = blocks[0]
        #Extract block
        try:
            self.cifblk = cf[cifblkname]
        except:
            logging.error('Block - %s - not found in %s' %
                          (blockname, ciffile))
            raise IOError
        return self.cifblk
Пример #3
0
    def test_calculating_xrd_pattern_from_cif_file(self):
        fcc_cif = ReadCif(get_cif_url('fcc.cif'))
        cif_phase = CifPhase(fcc_cif[fcc_cif.keys()[0]])
        cif_converter = CifConverter(0.31)
        jcpds_phase = cif_converter.convert_cif_phase_to_jcpds(cif_phase)

        self.assertAlmostEqual(jcpds_phase.reflections[0].intensity, 100, places=4)
        self.assertAlmostEqual(jcpds_phase.reflections[0].d0, 2.814, places=4)
        self.assertAlmostEqual(jcpds_phase.reflections[1].d0, 2.437, places=4)
Пример #4
0
def main(name):
    mof = ReadCif(name)
    mof = mof[mof.visible_keys[0]]

    elements = mof["_atom_site_type_symbol"]
    n_atoms = len(elements)

    prop_dict = {}
    for a1, a2 in combinations_with_replacement(set(elements), 2):
        prop_arr = [prop[a1] * prop[a2] for prop in prop_list]
        prop_dict[(a1, a2)] = prop_arr
        if a1 != a2:
            prop_dict[(a2, a1)] = prop_arr

    la = float(mof["_cell_length_a"])
    lb = float(mof["_cell_length_b"])
    lc = float(mof["_cell_length_c"])
    aa = np.deg2rad(float(mof["_cell_angle_alpha"]))
    ab = np.deg2rad(float(mof["_cell_angle_beta"]))
    ag = np.deg2rad(float(mof["_cell_angle_gamma"]))
    # If volume is missing from .cif, calculate it.
    try:
        cv = float(mof["_cell_volume"])
    except KeyError:
        cv = la * lb * lc * math.sqrt(1 - (math.cos(aa))**2 -
                                      (math.cos(ab))**2 - (math.cos(ag))**2 +
                                      (2 * math.cos(aa) * math.cos(ab) *
                                       math.cos(ag)))

    frac2cart = np.zeros([3, 3], dtype=float)
    frac2cart[0, 0] = la
    frac2cart[0, 1] = lb * np.cos(ag)
    frac2cart[0, 2] = lc * np.cos(ab)
    frac2cart[1, 1] = lb * np.sin(ag)
    frac2cart[1, 2] = lc * (np.cos(aa) - np.cos(ab) * np.cos(ag)) / np.sin(ag)
    frac2cart[2, 2] = cv / (la * lb * np.sin(ag))

    frac = np.array([
        mof["_atom_site_fract_x"],
        mof["_atom_site_fract_y"],
        mof["_atom_site_fract_z"],
    ],
                    dtype=float).T

    apw_rdf = np.zeros([n_props, n_bins], dtype=np.float64)
    for i, j in combinations(range(n_atoms), 2):
        cart_i = frac2cart @ frac[i]
        cart_j = (frac2cart @ (super_cell + frac[j]).T).T
        dist_ij = min(np.linalg.norm(cart_j - cart_i, axis=1))
        rdf = np.exp(smooth * (bins - dist_ij)**2)
        rdf = rdf.repeat(n_props).reshape(n_bins, n_props)
        apw_rdf += (rdf * prop_dict[(elements[i], elements[j])]).T
    apw_rdf = np.round(apw_rdf.flatten() * factor / n_atoms, decimals=12)

    return ("{}," * len(apw_rdf) + "{}\n").format(
        name.split('/')[-1], *apw_rdf.tolist())
Пример #5
0
def import_cif(file_path, occupancy_tolerance=100):
    """
    Import cif with pymatgen. This is the current default.
    
    Aruments
    --------
    occupancy_tolerance: int
        Occupancy tolerance for pymatgen.io.cif.CifParser. Files from the 
        CSD can contain multiple atoms which sit on the same site when 
        PBC are taken into account. Pymatgen correctly recognizes these as the
        same atom, however, it has a tolerance for the number of atoms which it
        will assume are the same. This is the occupancy tolerance. I don't 
        believe this value should be limited and thus it's set at 100.
    """
    try:
        pycif = CifParser(file_path, occupancy_tolerance=occupancy_tolerance)
        pstruct_list = pycif.get_structures(primitive=False)
        struct = Structure.from_pymatgen(pstruct_list[0])
    except:
        struct = import_cif_ase(file_path)

    file_name = os.path.basename(file_path)
    struct.struct_id = file_name.replace('.cif', '')

    #### Read in data that may be stored as a CIF property
    if read_cif_data:
        ### Wrap in try except in case of PyCIFRW errors
        try:
            cif = ReadCif(file_path)
        except:
            print("Error in reading CIF data for {}".format(file_path))
            return struct

        if len(cif) > 1:
            raise Exception("Must only have one structure per CIF file.")

        cif_struct = [x for x in cif.keys()]
        cif_data = cif[cif_struct[0]]
        for key, value in cif_data.items():
            ## Skip over geometry information
            if "_atom_" == key[0:len("_atom_")]:
                continue
            elif "_cell_" == key[0:len("_cell_")]:
                continue
            elif "_symmetry_equiv" == key[0:len("_symmetry_equiv")]:
                continue
            else:
                pass

            ## Otherwise add info to structure property information
            struct.properties[key] = value

    return struct
Пример #6
0
    def _loadFromCif(self):
        from CifFile import ReadCif

        cifFileUrl = self._getFileUrl()
        workspace = self.getProperty('Workspace').value

        # Try to parse cif file using PyCifRW
        parsedCifFile = ReadCif(cifFileUrl)

        self._setCrystalStructureFromCifFile(workspace, parsedCifFile)

        ubOption = self.getProperty('LoadUBMatrix').value
        if ubOption:
            self._setUBMatrixFromCifFile(workspace, parsedCifFile)
Пример #7
0
    def import_cif_file(self):
        file = self.ui.cif_file.text()

        if not os.path.isfile(file):
            return

        try:
            cf = ReadCif(file)

            search_params = {
                '_cell_length_a': InterplanarSpacing.PARAMETER_A,
                '_cell_length_b': InterplanarSpacing.PARAMETER_B,
                '_cell_length_c': InterplanarSpacing.PARAMETER_C,
                '_cell_angle_alpha': InterplanarSpacing.PARAMETER_ALPHA,
                '_cell_angle_beta': InterplanarSpacing.PARAMETER_BETA,
                '_cell_angle_gamma': InterplanarSpacing.PARAMETER_GAMMA,
            }

            ui_mapping = self.get_parameter_mapping()
            import re
            # find the lattice parameters in the cif file
            for key, entry in cf.items():
                for search_key, search_mapping in search_params.items():
                    if entry.has_key(search_key):
                        try:
                            number = float(entry[search_key])
                        except:
                            number = re.findall(r"[-+]?\d*\.\d+|\d+",
                                                entry[search_key])
                            if len(number) == 0:
                                continue
                            number = number[0]
                        ui_mapping[search_mapping].setText(str(number))

            self.system.set_parameter(self.get_lattice_parameters())

            system_class = self.system.detect_system()
            # Now set the input text field for the crystal system
            crystal_family = None
            for key, crystal_class in self.CLASS_MAPPING.items():
                if crystal_class == system_class:
                    crystal_family = key

            self.set_crystal_family(crystal_family)

            self.update_view()

        except BaseException as e:
            print(e)
Пример #8
0
def read(cif_file, grammar=None):
    """
    Takes a CIF format file, and returns a Structure object. Applies all
    symmetry operations in the CIF to the atoms supplied with the structure. If
    these are not correct, the structure will not be either. If the CIF
    contains more than one file, the return will be a list. If not, the return
    will be a single structure (not in a len-1 list).

    Examples::

        >>> s = io.cif.read(INSTALL_PATH+'io/files/fe3o4.cif')

    """
    if grammar:
        cf = ReadCif(cif_file, grammar=grammar)
    else:
        cf = ReadCif(cif_file)
    structures = []
    for key in cf.keys():
        structures.append(_read_cif_block(cf[key]))
    if len(structures) == 1:
        return structures[0]
    else:
        return structures
Пример #9
0
def read_reference(cif_file):
    """
    Read a cif file and return a :mod:`~qmpy.Reference`.
    """
    cf = ReadCif(cif_file, grammar='1.1')
    cif_block = cf[cf.keys()[0]]
    reference = rx.Reference()
    reference.authors = _get_authors(cif_block)
    reference.journal = _get_journal(cif_block)
    reference.volume = _get_volume(cif_block)
    reference.year = _get_year(cif_block)
    reference.first_page = _get_first_page(cif_block)
    reference.last_page = _get_last_page(cif_block)
    reference.title = _get_title(cif_block)
    return reference
Пример #10
0
    def convert_cif_to_jcpds(self, filename):
        """
        Reads a cif file and returns a jcpds with correct reflection and intensities
        :param filename:  cif filename
        :return: converted jcpds object
        :rtype: jcpds
        """
        file_url = 'file:' + pathname2url(filename)
        cif_file = ReadCif(file_url)
        cif_phase = CifPhase(cif_file[cif_file.keys()[0]])
        jcpds_phase = self.convert_cif_phase_to_jcpds(cif_phase)
        jcpds_phase.filename = filename
        jcpds_phase.name = os.path.splitext(os.path.basename(filename))[0]
        jcpds_phase.modified = False

        return jcpds_phase
Пример #11
0
    def test_reading_phase(self):
        fcc_cif = ReadCif(get_cif_url('fcc.cif'))

        cif_phase = CifPhase(fcc_cif[fcc_cif.keys()[0]])

        self.assertEqual(cif_phase.a, 4.874)
        self.assertEqual(cif_phase.b, 4.874)
        self.assertEqual(cif_phase.c, 4.874)

        self.assertEqual(cif_phase.alpha, 90)
        self.assertEqual(cif_phase.beta, 90)
        self.assertEqual(cif_phase.gamma, 90)

        self.assertEqual(cif_phase.volume, 115.79)
        self.assertEqual(cif_phase.space_group_number, 225)

        self.assertEqual(len(cif_phase.atoms), 8)
        self.assertEqual(cif_phase.comments, 'HoN, Fm-3m - NaCl structure type, ICSD 44776')
Пример #12
0
def read_structure(path_to_file):

    cwd = os.getcwd()
    try:
        os.chdir(os.path.dirname(path_to_file))
        file_name = os.path.split(path_to_file)[-1]
        cif_data = ReadCif(file_name)
        os.chdir(cwd)
    except:
        os.chdir(cwd)
        raise ValueError("The reading or parsing of the " + file_name +
                         " is failed!")
    try:
        name, data = list(cif_data.items())[0]
        structure_data = StructureData(name, data)
        structure = Structure().build_structure(structure_data)
        structure.reamove_connectivity()
        return structure
    except:
        print("Reading structure is failed!")
Пример #13
0
def read_structures(path_to_file):

    cwd = os.getcwd()
    try:
        os.chdir(os.path.dirname(path_to_file))
        file_name = os.path.split(path_to_file)[-1]
        cif_data = ReadCif(file_name)
        os.chdir(cwd)
    except:
        os.chdir(cwd)
        raise ValueError("The reading or parsing of the " + file_name +
                         " is failed!")
    for i, (name, data) in enumerate(cif_data.items()):
        try:
            structure_data = StructureData(name, data)
            structure = Structure().build_structure(structure_data)
            structure.reamove_connectivity()
            yield structure
        except:
            os.chdir(cwd)
            print("Reading " + str(i) + " structure is failed!")
Пример #14
0
    def test_sfcalc(self):  ## test method names begin 'test*'
        # Read the cif
        mylist =  structure.build_atomlist()
        mylist.CIFread('PPA.cif','oPPA')

        # Read the fcf
        fcf = ReadCif('oPPA.fcf')['oPPA']
        for i in range(500):# Consider only the first 500 reflections
            hkl =[eval(fcf['_refln_index_h'][i]),
                  eval(fcf['_refln_index_k'][i]),
                  eval(fcf['_refln_index_l'][i])]
            (Fr, Fi) = structure.StructureFactor(hkl, mylist.atomlist.cell,
                                                 mylist.atomlist.sgname,
                                                 mylist.atomlist.atom,
                                                 mylist.atomlist.dispersion)
            F2 = Fr**2 + Fi**2
            reldif = F2/eval(fcf['_refln_F_squared_calc'][i])-1
            print(i, reldif, F2)
            if F2 > 10: # Only compare those with an F^2 larger than ten 
                        # to avoid that the very weak reflections which
                        # have a relative difference that are slightly larger
                self.assertAlmostEqual(reldif,0,2)
Пример #15
0
def star_reader(filename):
    """
    star_reader: reads a star file and returns a dictionary
    """
    key='data_'
    new_key='data_metadata'
    new_starfile='.tmp.star'
    #
    with open(filename) as f1:
        with open(new_starfile, 'w') as f2:
            lines = f1.readlines()
            for line in lines:
                if(line.startswith(key)):
                    f2.write(new_key)
                else:
                    f2.write(line)
    data = ReadCif(new_starfile, grammar='STAR2')
    os.remove(new_starfile)
    print('Number of particles in this star file: {}'.format(len(data['metadata'][data['metadata'].keys()[0]])))
    print("The entries in the returned dictionary are:")
    print("data['metadata'].keys(): {}".format(data['metadata'].keys()))
    return data
Пример #16
0
    def _readCif(self, fcif=DFLT_NAME + '.cif'):
        """
        >> @AUTHOR:     Saransh Singh, Lawrence Livermore National Lab, [email protected]
        >> @DATE:       10/16/2019 SS 1.0 original
        >> @DETAILS:    hexrd3 will have real structure factors and will require the overhaul
                        of the crystallography. In this effort, we will have a cif reader and
                        also the HDF5 format reader in the material class. We will be using
                        pycifrw for i/o
        """

        # make sure file exists etc.
        if (fcif == Material.DFLT_NAME + '.cif'):
            try:
                cif = ReadCif(fcif)
            except (OSError):
                raise RuntimeError(
                    'OS Error: No file name supplied and default file name not found.'
                )
        else:
            try:
                cif = ReadCif(fcif)
            except (OSError):
                raise RuntimeError('OS Error: File not found')

        # read the file
        for k in cif.keys():
            if ('_cell_length_a' in cif[k]):
                m = k
                break
        cifdata = cif[m]
        # cifdata = cif[cif.keys()[0]]

        # make sure the space group is present in the cif file, either as
        # international table number, hermann-maguain or hall symbol
        sgkey = [
            '_space_group_IT_number', '_symmetry_space_group_name_h-m',
            '_symmetry_space_group_name_hall', '_symmetry_Int_Tables_number'
        ]

        sgdata = False
        for key in sgkey:
            sgdata = sgdata or (key in cifdata)
            if (sgdata):
                skey = key
                break

        if (not (sgdata)):
            raise RuntimeError(' No space group information in CIF file! ')

        sgnum = 0
        if skey is sgkey[0]:
            sgnum = int(cifdata[sgkey[0]])
        elif (skey is sgkey[1]):
            HM = cifdata[sgkey[1]]
            HM = HM.replace(" ", "")
            sgnum = HM_to_sgnum[HM]
        elif (skey is sgkey[2]):
            hall = cifdata[sgkey[2]]
            hall = hall.replace(" ", "")
            sgnum = Hall_to_sgnum[HM]
        elif (skey is sgkey[3]):
            sgnum = int(cifdata[sgkey[3]])

        # lattice parameters
        lparms = []
        lpkey = ['_cell_length_a', '_cell_length_b', \
                 '_cell_length_c', '_cell_angle_alpha', \
                 '_cell_angle_beta', '_cell_angle_gamma']

        for key in lpkey:
            n = cifdata[key].find('(')
            if (n != -1):
                lparms.append(float(cifdata[key][:n]))
            else:
                lparms.append(float(cifdata[key]))

        for i in range(6):
            if (i < 3):
                lparms[i] = _angstroms(lparms[i])
            else:
                lparms[i] = _degrees(lparms[i])

        self._lparms = lparms
        self.sgnum = sgnum

        # fractional atomic site, occ and vibration amplitude
        fracsitekey = ['_atom_site_fract_x', '_atom_site_fract_y',\
                        '_atom_site_fract_z',]

        occ_U       = ['_atom_site_occupancy',\
                        '_atom_site_u_iso_or_equiv','_atom_site_U_iso_or_equiv']

        sitedata = True
        for key in fracsitekey:
            sitedata = sitedata and (key in cifdata)

        if (not (sitedata)):
            raise RuntimeError(
                ' fractional site position is not present or incomplete in the CIF file! '
            )

        atompos = []
        for key in fracsitekey:
            slist = cifdata[key]
            pos = []

            for p in slist:
                n = p.find('(')

                if (n != -1):
                    pos.append(p[:n])
                else:
                    pos.append(p)
            '''
            sometimes cif files have negative values so need to
            bring them back to fractional coordinates between 0-1
            '''
            pos = numpy.asarray(pos).astype(numpy.float64)
            pos, _ = numpy.modf(pos + 100.0)
            atompos.append(pos)
        """note that the vibration amplitude, U is just the amplitude (in A)
            to convert to the typical B which occurs in the debye-waller factor,
            we will use the following formula
            B = 8 * pi ^2 * < U_av^2 >
            this will be done here so we dont have to worry about it later
        """

        pocc = (occ_U[0] in cifdata.keys())
        pU = (occ_U[1] in cifdata.keys()) or (occ_U[2] in cifdata.keys())

        if (not pocc):
            warn('occupation fraction not present. setting it to 1')
            occ = numpy.ones(atompos[0].shape)
            atompos.append(occ)
        else:
            slist = cifdata[occ_U[0]]
            occ = []
            for p in slist:
                n = p.find('(')

                if (n != -1):
                    occ.append(p[:n])
                else:
                    occ.append(p)

            atompos.append(numpy.asarray(occ).astype(numpy.float64))

        if (not pU):
            warn(
                'Debye-Waller factors not present. setting to same values for all atoms.'
            )
            U = 1.0 / numpy.pi / 2. / numpy.sqrt(2.) * numpy.ones(
                atompos[0].shape)
            self._U = U
        else:
            if (occ_U[1] in cifdata.keys()):
                k = occ_U[1]
            else:
                k = occ_U[2]

            slist = cifdata[k]
            U = []
            for p in slist:
                n = p.find('(')

                if (n != -1):
                    U.append(p[:n])
                else:
                    U.append(p)

            self._U = numpy.asarray(U).astype(numpy.float64)
        '''
        format everything in the right shape etc.
        '''
        self._atominfo = numpy.asarray(atompos).T
        '''
        get atome types here i.e. the atomic number of atoms at each site
        '''
        atype = '_atom_site_type_symbol'
        patype = (atype in cifdata)
        if (not patype):
            raise RuntimeError('atom types not defined in cif file.')

        satype = cifdata[atype]
        atomtype = []

        for s in satype:
            atomtype.append(ptable[s])

        self._atomtype = numpy.asarray(atomtype).astype(numpy.int32)
        self._sgsetting = 0
Пример #17
0
#! ./venv/bin/python

from CifFile import ReadCif
import sys

# I couldn't find a good way to read CIFs in ruby so we have to call this python script
# pip install PyCifRW

cf = ReadCif(sys.argv[1])
atoms = cf[cf.visible_keys[0]]["_atom_site_label"]
j = 0
while j < len(atoms):
    atoms[j] = ''.join([i for i in atoms[j] if not i.isdigit()])
    j = j + 1
atoms_no_dupes = []

for x in atoms:
    if x == "C1":
        y = 2
    if x not in atoms_no_dupes:
        atoms_no_dupes.append(x)
print(atoms_no_dupes)
Пример #18
0
from CifFile import ReadCif

import os
from shutil import copy

d = '/home/kenneth/proj/proXtal/amcsd/cif/mineral/'

for f in os.listdir(d):
    cf = ReadCif(d + f)

    labels = cf['global']['_atom_site_label']

    subs = "Fe"

    if len(list(filter(lambda x: subs in x, labels))) > 0:

        copy(f, d + 'Fe/')
Пример #19
0
    def run(config, input, output):

        dir = config['xcDir']

        # Copy cif2cell related input to cif2cellInput here. Later we will be overriding some settings,
        # so we want to keep the original input intact.
        cif2cellInput = {
            key: input[key]
            for key in input if key.startswith('cif2cell.')
        }

        # Generate any data that is needed from generic input and populate cif2cellInput with
        # global data (needed for all feff runs.)
        cif2cellInput['cif2cell.cif_input'] = input['cif_input']

        # Set executable directory for this exchange
        cif2celldir = config['cif2cell']

        # Set input file - cif2cell takes input from command line, so no input file, however, maybe we can take commands from input file?
        #inpf = os.path.join(dir, 'cif2cell.in')

        # Loop over targets in output. Not sure if there will ever be more than one output target here.
        if set(output.keys()).issubset(
                set([
                    'cell_vectors', 'cell_struct_xyz_red', 'cell_scaling_iso',
                    'cell_scaling_abc', 'number_density', 'supercell'
                ])):
            # Set output and error files
            with open(os.path.join(dir, 'corvus.CIF2CELL.stdout'),
                      'wb') as out, open(
                          os.path.join(dir, 'corvus.CIF2CELL.stderr'),
                          'wb') as err:
                # Copy necessary files to dir
                cif_file = cif2cellInput['cif2cell.cif_input'][0][0]
                shutil.copy(cif_file, dir)

                # Loop over executables: This is specific to feff. Other codes
                # will more likely have only one executable.
                executables = ['cif2cell']

                args = [cif_file, '-p', 'cif', '-o', 'out.cif']
                iExec = 0
                for executable in executables:
                    runExecutable(cif2celldir, dir, executable, args, out, err)

                # For now, I am only passing the directory.
                print('Setting output')
                outCif = os.path.join(dir, 'out.cif')
                cifFile = ReadCif(outCif)
                # cifFile now contains an object that acts like a dictionary
                # of dictionaries, with the outer keys the data for each
                # structure listed in the cif, next the normal cif data, like
                # '_cell_angle_alpha', and '_atom_site_fract_[xyz]'.
                # Note that as indicated above, the cif can hold multiple
                # structures. For now I will assume that there is only one.
                cif_dict = cifFile[list(cifFile.keys())[0]]

                # Now lets make all of the data structures that we want out of
                # this. We are using the CellData structure from cif2cell's
                # uctools package. Right now, I have cif2cell write another
                # cif file first, then we read it in and analyze it. This is
                # because we don't want to reproduce all of the sanity check
                # that cif2cell already has in it.
                cell_data = CellData()
                # Fill the cell data object from the cif file.
                cell_data.getFromCIF(cif_dict)

                # Calculate cell structure for the primitive cell for now. Supercells are
                # represented as P1, so this will not harm that.
                cell_data.primitive()

                # The cell_data structure now has all cell data that we need
                # in it, or a method to get that data.
                if 'cell_vectors' in output:
                    output['cell_vectors'] = cell_data.latticevectors

                if 'cell_struct_xyz_red' in output:
                    xred = []
                    for atom in cell_data.atomdata:
                        if atom:
                            xred = xred + [atom[0].position]

                    output['cell_struct_xyz_red'] = xred

                if 'cell_scaling_iso' in output:
                    output['cell_scaling_iso'] = [[cell_data.lengthscale]]

                if 'cell_scaling_abc' in output:
                    # For now I will output cell_data_abc as 1, since cif2cell seems to put things into lengthscale
                    # and a,b,c are for convenience.
                    output['cell_scaling_abc'] = [[1.0, 1.0, 1.0]]

        elif 'cell_structure' in output:
            # Return path to cif file.
            output['cell_structure'] = [['Working']]
Пример #20
0
def read_cif(filename: str_PathLike, pps: muti_Dict = [{}], orbitals: muti_Dict = [{}], masses: muti_Dict = [{}], magmoms: muti_Dict = [{}], move: muti_Dict = [{}], abfs: muti_Dict = [{}]) -> List[Stru]:
    """Read CIF(Crystallographic Information Framework) file and return `Stru` object

    :params filename: name of cif file
    """


    raise UserWarning("Can only read direct positions, generating equivalent atoms based on site symmetry or Wackoff positions are not supported now")
    from CifFile import ReadCif
    strulist = []

    def list_elem2split(l):
        def list_split(s):
            return s.split('(')[0]
        return list(map(list_split, l))

    def get_elements(string):
        return re.search(r'([A-Z][a-z]?)', string).group(0)

    cf = ReadCif(filename)
    for index, name in enumerate(cf.keys()):
        data = cf[name]
        fract_x = list_elem2split(data["_atom_site_fract_x"])
        fract_y = list_elem2split(data["_atom_site_fract_y"])
        fract_z = list_elem2split(data["_atom_site_fract_z"])
        if "_atom_site_type_symbol" in data.keys():
            elements = list(map(get_elements, data["_atom_site_type_symbol"]))
        else:
            elements = list(map(get_elements, data["_atom_site_label"]))
        scaled_positions = defaultdict(list)
        for i, elem in enumerate(elements):
            scaled_positions[elem].append([np.array(fract_x[i], dtype=float), np.array(
                fract_y[i], dtype=float), np.array(fract_z[i], dtype=float)])

        la = float(data["_cell_length_a"].split('(')[0])
        lb = float(data["_cell_length_b"].split('(')[0])
        lc = float(data["_cell_length_c"].split('(')[0])
        alpha = float(data["_cell_angle_alpha"].split('(')[0])
        beta = float(data["_cell_angle_beta"].split('(')[0])
        gamma = float(data["_cell_angle_gamma"].split('(')[0])

        if "_symmetry_cell_setting" in data.keys():
            crystal_system = data["_symmetry_cell_setting"]
        elif "_symmetry_Int_Tables_number" in data.keys():
            crystal_system = Spacegroup().get_crystal_system(
                data["_symmetry_Int_Tables_number"])
        elif "_space_group_name_Hall" in data.keys():
            crystal_system = Spacegroup().get_crystal_system(
                Hall2Number[data["_space_group_name_Hall"]])
        elif "_symmetry_space_group_name_H-M" in data.keys():
            crystal_system = Spacegroup().get_crystal_system(
                Hall2Number[HM2Hall[data["_symmetry_space_group_name_H-M "]]])
        else:
            crystal_system = 'unknown'

        cell = conventional_cell(
            la, lb, lc, alpha, beta, gamma, crystal_system)

        strulist.append(Stru(lat0=1/BOHR_TO_A, cell=cell, pps=pps[index], scaled_positions=scaled_positions,
                             orbitals=orbitals[index], masses=masses[index], magmoms=magmoms[index], move=move[index], abfs=abfs[index]))

    return strulist
Пример #21
0
 def data_harvest(self, cif_file):                
                 
     #Resets the dataframes/dictionaries            
                 
     for item in self.search_items:
         self.results[item] = []
         self.errors[item] = []
     self.temp_df = pd.DataFrame()
     
     #This notation is from the CifFile module and allows for easy searching of parameters
     
     cif = ReadCif(cif_file)
     
     #Identifies datablocks within the CIF file
     
     data_blocks = []
     
     with open (cif_file, 'rt') as f:
         for line in f:
             if line.startswith('data_'):
                 data_blocks.append(line.strip('\n').strip('data_'))
                 
     number_of_structures = len(data_blocks)
     
             
     #For each search item in the config file, the cif file is searched 
     
     for item in self.search_items:
         self.logger.info('File ' + pathlib.Path(cif_file.lower()).stem + ' opened. Searching for parameter ' + item)
         
         #This parameter is blanked every loop because each cif is searched multiple times
         
         self.cif_list = []
         
         #Since the CifFile module works by looking at data-blocks, use the found list of data blocks to search through the CIF
         
         for experiment in data_blocks:
             try:
                 raw = cif[experiment][item]
             except:
                 self.logger.critical('Failed to find ' + item + ' in ' + pathlib.Path(cif_file.lower()).stem)
                 exit()
             
             self.logger.info('File ' + pathlib.Path(cif_file.lower()).stem + ' - found parameter ' + item)
             self.cif_list.append(pathlib.Path(cif_file.lower()).stem)
             
             #Check if an error value is present and separates the value and the error, converts the error to the correct decimal point, and assigns an error of 0 if no error present 
             
             if '(' in raw:
                 temp = raw.split('(')
                 temp2 = temp[1].strip(')')
                 self.results[item].append(float(temp[0]))
                 if '.' in raw:
                     temp3 = temp[0].split('.')
                     self.errors[item].append(int(temp2)*10**-(int(len(temp3[1]))))
                 else:
                     self.errors[item].append(int(temp2))
             
             else:
                 self.results[item].append(float(raw))
                 self.errors[item].append(0)
      
     #Creates a column in the data frame to include the file name of the CIF
     
     self.temp_df['CIF File'] = self.cif_list
     
     #Adds all of the data to one data frame 
         
     for para in self.search_items:
         if len(self.results[para]) != 0:
             self.temp_df[para] = self.results[para]
             self.temp_df[para + '_error'] = self.errors[para]
     
     return self.temp_df, number_of_structures, data_blocks
Пример #22
0
    # ASE functions
    from ase.io import read as read_cif

elif package == 'pymatgen':
    # Pymatgen functions
    from pymatgen import Structure
    read_cif = Structure.from_file

elif package == 'pycifrw':
    # PyCifRW
    from CifFile import ReadCif as read_cif

elif package == 'pycifrw-fast':
    # PyCifRW
    from CifFile import ReadCif
    read_cif = lambda x: ReadCif(x, scantype="flex")

elif package == 'pycodcif':
    # pycodcif
    from pycodcif import parse

    def get_content(file):
        datablocks, error_count, error_messages = parse(file)
        return datablocks

    read_cif = get_content

extension = '.cif'
directory = 'structures_0108'
paths = glob.glob("{}/*{}".format(directory, extension))
Пример #23
0
'''
Created on Mar 17, 2018

@author: Mammon
'''

from CifFile import ReadCif
cf = ReadCif("molecule.cif")
my_data = cf.first_block()

#print(my_data)
print(my_data)

columns = [
    "_atom_site_label", "_atom_site_type_symbol", "_atom_site_fract_x",
    "_atom_site_fract_y", "_atom_site_fract_z"
]

#store the loop inside excel
from openpyxl import Workbook
wb = Workbook()

#get current active sheet
ws = wb.active
#change title of the sheet
ws.title = "Sonification"

#change the background color of the tab hoding this title
ws.sheet_properties.tabColor = "1072BA"
for i in range(1, 6):
    ws.cell(row=1, column=i, value=columns[i - 1])
Пример #24
0
    def finalise_parameters(self, file_name, index, current_temp):

        #Finalises the CIF with important parameters such as beamline parameters

        cif = ReadCif(file_name)

        try:
            data_block = cif[str(index + 1)]
        except TypeError as error:
            os.remove(file_name)
            self.logger.info(f'Cif empty due to poor refinement - {error}')
        else:

            self.C = data_block['_diffrn_reflns_number']
            self.A = data_block['_diffrn_reflns_theta_min']
            self.B = data_block['_diffrn_reflns_theta_max']

            beamline = self.cfg['User_Parameters_Full_Pipeline'][
                'Experiment_Configuration']['beamline']

            if beamline != '':

                data_block['_computing_data_collection'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_data_collection']
                data_block['_exptl_absorp_correction_type'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_absorp_correction']
                data_block['_diffrn_radiation_wavelength'] = self.cfg[
                    'User_Parameters_Full_Pipeline'][
                        'Experiment_Configuration']['wavelength']
                data_block['_diffrn_radiation_type'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_radiation_type']
                data_block['_diffrn_source'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_radiation_type']
                data_block['_diffrn_measurement_device_type'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_detector']
                data_block['_diffrn_measurement_method'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_measurement_method']
                data_block['_computing_cell_refinement'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_cell_refinement']
                data_block['_computing_data_reduction'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_cell_refinement']
                data_block['_computing_structure_solution'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_structure_soln']
                data_block['_diffrn_radiation_monochromator'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_monochromator']
                data_block['_diffrn_radiation_source'] = self.cfg[
                    'Instrument_Parameters'][beamline + '_source']

            data_block['_chemical_formula_moiety'] = self.cfg[
                'User_Parameters_Full_Pipeline']['Crystal_Descriptions'][
                    'chemical_formula']
            data_block['_exptl_crystal_colour'] = self.cfg[
                'User_Parameters_Full_Pipeline']['Crystal_Descriptions'][
                    'crystal_colour']
            data_block['_exptl_crystal_description'] = self.cfg[
                'User_Parameters_Full_Pipeline']['Crystal_Descriptions'][
                    'crystal_habit']
            data_block['_cell_measurement_temperature'] = current_temp
            data_block['_diffrn_ambient_temperature'] = current_temp
            data_block['_exptl_crystal_size_max'] = self.cfg[
                'User_Parameters_Full_Pipeline']['Crystal_Descriptions'][
                    'max_crystal_dimension']
            data_block['_exptl_crystal_size_mid'] = self.cfg[
                'User_Parameters_Full_Pipeline']['Crystal_Descriptions'][
                    'middle_crystal_dimension']
            data_block['_exptl_crystal_size_min'] = self.cfg[
                'User_Parameters_Full_Pipeline']['Crystal_Descriptions'][
                    'min_crystal_dimension']
            data_block['_cell_measurement_reflns_used'] = self.C
            data_block['_cell_measurement_theta_min'] = self.A
            data_block['_cell_measurement_theta_max'] = self.B

            with open('edited.cif', 'w') as updated:
                updated.write(cif.WriteOut())

            os.rename('edited.cif', file_name)
    '_cell_length_c', '_cell_angle_alpha', '_cell_angle_beta',
    '_cell_angle_gamma', '_cell_volume', '_exptl_crystal_density_diffrn',
    '_symmetry_space_group_name_h-m'
]

for i, row in dfMin.iterrows():

    cifFolder = os.path.join(base, row.MineralName)
    if os.path.exists(
            cifFolder):  #row.mineralName not in ['Angelaite','Arsenopyrite']:

        for cifFile in [
                f for f in os.listdir(cifFolder) if f.endswith(".cif")
        ]:
            try:
                cf = ReadCif(os.path.join(cifFolder, cifFile))

                # '_atom_site_label','_atom_site_fract_x','_atom_site_fract_y','_atom_site_fract_z','_atom_site_occupancy','_atom_site_u_iso_or_equiv'])
                cifDic = {key: cf['global'][key] for key in cifKeys}
                cifDic['Temperature'] = 'False'
                cifDic['Pressure'] = 'False'

                #remove new line from '_publ_section_title'
                cifDic['_publ_section_title'] = cifDic[
                    '_publ_section_title'].replace("\n",
                                                   " ").replace('\r',
                                                                " ").strip()
                # print(cifDic)
                #with multiplicity
                if '_atom_site_occupancy' in cf[
                        'global']:  #keep if it doesnt have multiple occupancies
Пример #26
0
if __name__ == '__main__':

    parser = argparse.ArgumentParser(
        prog='susViewer',
        description=
        'Convert chi-tensor to appropriate coordinate system for visualization in standard molecular viewers'
    )
    parser.add_argument('cifname')
    parser.add_argument('-b', '--blockname')
    parser.add_argument('-s', '--scale')
    parser.add_argument('-r', '--repr')

    _cmd_line_args = parser.parse_args()

    _cifname = _cmd_line_args.cifname
    cf = ReadCif(_cifname)

    _blockname = _cmd_line_args.blockname
    _type = _cmd_line_args.repr
    _scale = _cmd_line_args.scale
    structure = cc.crystalStructure(_cifname, blockname=_blockname)

    if _scale is None:
        _scale = 1
    else:
        _scale = float(_scale)
    # The following is not obsolete. It is used to find the right block, as not everything can be
    # done using the structure as loaded from the CIF.
    if _blockname is None:
        _blockname = structure.block
    if _type is None:
Пример #27
0
    def read(self):
        """
        Read data from the CIF file
        """
        with open(self.filename, 'r') as f:
            ciffile = ReadCif(f)
            for block in ciffile:
                spacegroup = Spacegroup(int(block['_space_group_IT_number']))

                cellpar = np.array([
                    block['_cell_length_a'],
                    block['_cell_length_b'],
                    block['_cell_length_c'],
                    block['_cell_angle_alpha'],
                    block['_cell_angle_beta'],
                    block['_cell_angle_gamma'],
                ],
                                   dtype=float)

                try:
                    site_labels = block['_atom_site_label']
                except KeyError:
                    print('Could not get site labels from cif file')
                try:
                    occupancies = np.array(block['_atom_site_occupancy'],
                                           dtype=float)
                except KeyError:
                    print('Could not get occupancies from cif file')
                try:
                    fract_x = np.array(block['_atom_site_fract_x'],
                                       dtype=float)
                    fract_y = np.array(block['_atom_site_fract_y'],
                                       dtype=float)
                    fract_z = np.array(block['_atom_site_fract_z'],
                                       dtype=float)
                except KeyError:
                    warn(
                        'Could not get fractional coordinates from cif file, getting absolute coordinates instead.'
                    )
                    try:
                        x = np.array(block['_atom_site_cartn_x'], dtype=float)
                        y = np.array(block['_atom_site_cartn_y'], dtype=float)
                        z = np.array(block['_atom_site_cartn_z'], dtype=float)
                    except KeyError:
                        warn(
                            'Could not get absolute coordinates from cif file')
                        x = [np.nan] * len(block)
                        y = [np.nan] * len(block)
                        z = [np.nan] * len(block)
                    finally:
                        fract_x = x / cellpar[0]
                        fract_y = y / cellpar[1]
                        fract_z = z / cellpar[2]
                else:
                    x = fract_x * cellpar[0]
                    y = fract_y * cellpar[1]
                    z = fract_y * cellpar[2]
                finally:
                    x = x.T
                    y = y.T
                    z = z.T
                    fract_x = fract_x.T
                    fract_y = fract_y.T
                    fract_z = fract_z.T

                    positions = np.array([x, y, z])
                    fractional_positions = np.array(
                        [fract_x, fract_y, fract_z])
                try:
                    symbols = block['_atom_site_type_symbol']
                except KeyError:
                    print(
                        'Could not get atom site chemical symbols from cif file'
                    )
                try:
                    dwf = block['_atom_site_B_iso_or_equiv']
                except KeyError:
                    print('Could not get Debye-Waller factors from cif file')

                basis_atoms = []
                for label, occ, fx, fy, fz, symbol, B in zip(
                        site_labels, occupancies, fractional_positions[0],
                        fractional_positions[1], fractional_positions[2],
                        symbols, dwf):
                    atom = CIFAtom(cellpar,
                                   symbol=symbol,
                                   occupancy=occ,
                                   fractional_position=(fx, fy, fz),
                                   dwf=B,
                                   site_label=label)
                    basis_atoms.append(atom)

                atoms = []
                for atom in basis_atoms:
                    equivalent_sites, kinds = spacegroup.equivalent_sites(
                        atom.fractional_position,
                        onduplicates='warn',
                        occupancies=atom.occupancy)
                    for site in equivalent_sites:
                        position = site * cellpar[:3]
                        equivalent_atom = CIFAtom(cellpar,
                                                  fractional_position=site,
                                                  site_label=atom.site_label,
                                                  symbol=atom.symbol,
                                                  dwf=atom.dwf,
                                                  occupancy=atom.occupancy)
                        atoms.append(equivalent_atom)
                self.atoms = atoms
                self.crystal = Crystal(atoms, cellpar)
Пример #28
0
 def __init__(self, filename, **kwargs):
     # ReadCIF would get confused between local files and URLs
     # Therefore, more clear to pass an open file
     self._handle = open(filename, mode="r")
     self.file = ReadCif(self._handle, **kwargs)
Пример #29
0
#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Andrezio
#
# Created:     23/07/2017
# Copyright:   (c) Andrezio 2017
# Licence:     <your licence>
#-------------------------------------------------------------------------------

from CifFile import ReadCif
<<<<<<< HEAD
cf = ReadCif('UO2_STAR_246851.cif')
lambida=1.54
=======
cf = ReadCif('u308.cif')

>>>>>>> f26a11b75e998002824160efa1c337672e8cb21b
chave = cf.keys()


##for k,v in cf[chave[0]].items():
##    print k

<<<<<<< HEAD
value= cf[chave[0]]['_cell_length_a']

value=value.split('(')[0]

=======
Пример #30
0
    def data_harvest(self, cif_file):

        #Resets the dataframes/dictionaries

        for item in self.search_items:
            self.results[item] = []
            self.errors[item] = []
        self.temp_df = pd.DataFrame()
        self.bond_df = pd.DataFrame()
        self.angle_df = pd.DataFrame()
        self.torsion_df = pd.DataFrame()

        #This notation is from the CifFile module and allows for easy searching of parameters

        cif = ReadCif(cif_file)

        #Identifies datablocks within the CIF file

        self.data_blocks = []

        with open(cif_file, 'rt') as f:
            for line in f:
                if line.startswith('data_'):
                    self.data_blocks.append(line.strip('\n').strip('data_'))

        number_of_structures = len(self.data_blocks)

        #For each search item in the config file, the cif file is searched

        for item in self.search_items:
            self.logger.info('File ' + pathlib.Path(cif_file.lower()).stem +
                             ' opened. Searching for parameter ' + item)

            #This parameter is blanked every loop because each cif is searched multiple times

            self.cif_list = []

            #Since the CifFile module works by looking at data-blocks, use the found list of data blocks to search through the CIF

            for experiment in self.data_blocks:
                try:
                    raw = cif[experiment][item]
                except:
                    self.logger.critical('Failed to find ' + item + ' in ' +
                                         pathlib.Path(cif_file.lower()).stem)
                    exit()

                self.logger.info('File ' +
                                 pathlib.Path(cif_file.lower()).stem +
                                 ' - found parameter ' + item)
                self.cif_list.append(pathlib.Path(cif_file.lower()).stem)

                #Check if an error value is present and separates the value and the error, converts the error to the correct decimal point, and assigns an error of 0 if no error present

                if type(raw) != list:
                    self.parameter_tidy(raw, item)
                else:
                    for param in raw:
                        self.parameter_tidy(param, item)

        #Creates a column in the data frame to include the file name of the CIF

        self.temp_df['CIF File'] = self.cif_list

        #Adds all of the data to one data frame

        bond_paras = [
            '_geom_bond_atom_site_label_1', '_geom_bond_atom_site_label_2',
            '_geom_bond_distance'
        ]
        angle_paras = [
            '_geom_angle_atom_site_label_1', '_geom_angle_atom_site_label_2',
            '_geom_angle_atom_site_label_3', '_geom_angle'
        ]
        torsion_paras = [
            '_geom_torsion_atom_site_label_1',
            '_geom_torsion_atom_site_label_2',
            '_geom_torsion_atom_site_label_3',
            '_geom_torsion_atom_site_label_4', '_geom_torsion'
        ]

        for para in self.search_items:
            if len(self.results[para]) == len(self.cif_list):
                self.temp_df[para] = self.results[para]
                self.temp_df[para + '_error'] = self.errors[para]
            elif len(self.results[para]) != 0 and para in bond_paras:
                self.bond_df[para] = self.results[para]
                self.bond_df[para + '_error'] = self.errors[para]
            elif len(self.results[para]) != 0 and para in angle_paras:
                self.angle_df[para] = self.results[para]
                self.angle_df[para + '_error'] = self.errors[para]
            elif len(self.results[para]) != 0 and para in torsion_paras:
                self.torsion_df[para] = self.results[para]
                self.torsion_df[para + '_error'] = self.errors[para]

        try:
            self.bond_df['Cif File'], self.bond_df[
                'Data Block'] = self.generate_cif_list(self.bond_df)
        except:
            pass
        try:
            self.angle_df['Cif File'], self.angle_df[
                'Data Block'] = self.generate_cif_list(self.angle_df)
        except:
            pass
        try:
            self.torsion_df['Cif File'], self.torsion_df[
                'Data Block'] = self.generate_cif_list(self.torsion_df)
        except:
            pass

        return self.temp_df, number_of_structures, self.data_blocks, self.bond_df, self.angle_df, self.torsion_df