source: trunk/imports/G2phase_xyz.py @ 4573

Last change on this file since 4573 was 4573, checked in by toby, 13 months ago

RB extensions and minor bug fixes

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: $
4# $Author: $
5# $Revision: $
6# $URL: $
7# $Id: $
8########### SVN repository information ###################
9#
10'''
11*Module G2phase_xyz: read coordinates from an xyz file*
12-------------------------------------------------------
13
14A short routine to read in a phase from an xyz Cartesian coordinate file
15
16'''
17
18from __future__ import division, print_function
19import sys
20import os.path
21import math
22import random as ran
23import numpy as np
24import GSASIIobj as G2obj
25import GSASIIspc as G2spc
26import GSASIIlattice as G2lat
27import GSASIIpath
28GSASIIpath.SetVersionNumber("$Revision: $")
29
30class XYZ_ReaderClass(G2obj.ImportPhase):
31    'Routine to import Phase information from a XYZ file'
32    def __init__(self):
33        super(self.__class__,self).__init__( # fancy way to say ImportPhase.__init__
34            extensionlist=('.xyz','.XYZ'),
35            strictExtension=True,
36            formatName = 'XYZ',
37            longFormatName = 'XYZ Cartesian coordinate file import'
38            )
39    def ContentsValidator(self, filename):
40        '''Taking a stab a validating: 1st line should be a number
41        '''
42        fp = open(filename,'r')
43        try:
44            int(fp.readline().strip())
45        except:
46            fp.close()
47            return False
48        fp.close()
49        return True
50
51    def Reader(self,filename, ParentFrame=None, **unused):
52        'Read a PDF file using :meth:`ReadPDBPhase`'
53        self.errors = 'Error opening file'
54        fp = open(filename, 'Ur')
55        self.Phase = {}
56        natom = int(fp.readline().strip())
57        Title = os.path.basename(filename)
58        skip = fp.readline()
59        line = 2
60        SGData = G2obj.P1SGData # P 1
61        self.warnings += '\nNo space group in file, set to "P 1".'
62        self.warnings += "Change this in phase's General tab."
63        cell = [10.,10.,10.,90.,90.,90.]
64        Volume = G2lat.calc_V(G2lat.cell2A(cell))
65
66        counts = {}
67        Atoms = []
68        for i in range(natom):
69            line += 1
70            self.errors = 'Error reading at line '+str(line)
71            l = fp.readline()
72            Type = l.split()[0]
73            XYZ = [float(x)/10. for x in l.split()[1:4]]
74            if Type not in counts:
75                counts[Type] = 0
76            counts[Type] += 1
77            Aname = Type + str(counts[Type])
78            Atoms.append([Aname,Type.strip().capitalize(),'',XYZ[0],XYZ[1],XYZ[2],
79                    1.0,'1',1,'I',0.0,0,0,0,0,0,0,ran.randint(0,sys.maxsize)])
80        fp.close()
81        self.errors = 'Error after read complete'
82        self.Phase = G2obj.SetNewPhase(Name=Title,SGData=SGData,cell=cell+[Volume,])
83        self.Phase['General']['Type'] = 'nuclear'
84        self.Phase['General']['AtomPtrs'] = [3,1,7,9]   
85        self.Phase['Atoms'] = Atoms
86        return True
Note: See TracBrowser for help on using the repository browser.