source: trunk/kSUBGROUPSMAG.py @ 4372

Last change on this file since 4372 was 4372, checked in by toby, 4 years ago

streamline Post access to web pages; try http before https

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 8.6 KB
Line 
1# -*- coding: utf-8 -*-
2"""
3*kSUBGROUPSMAG: Interface to Bilbao k-SUBGROUPSMAG web page*
4-------------------------------
5
6Extraction of magnetic space groups for a given space group and a propagation vector
7from the k-SUBGROUPSMAG web page on the Bilbao Crystallographic server
8
9"""
10########### SVN repository information ###################
11# $Date: 2020-03-18 22:25:22 +0000 (Wed, 18 Mar 2020) $
12# $Author: toby $
13# $Revision: 4372 $
14# $URL: trunk/kSUBGROUPSMAG.py $
15# $Id: kSUBGROUPSMAG.py 4372 2020-03-18 22:25:22Z toby $
16########### SVN repository information ###################
17from __future__ import division, print_function
18try:
19    import HTMLParser as HTML
20except ImportError: 
21    import html.parser as HTML # Python 3
22import GSASIIspc as G2spc
23import GSASIIIO as G2IO
24import GSASIIpath
25GSASIIpath.SetBinaryPath()
26submagSite = 'http://www.cryst.ehu.es/cgi-bin/cryst/programs/subgrmag1_k.pl'
27
28class TableParser(HTML.HTMLParser):
29    def __init__(self):
30        HTML.HTMLParser.__init__(self)
31        self.spgp = ''
32        self.in_sp = False
33        self.in_pre = False
34        self.in_sub = False
35        self.MV = ''
36        self.BNS = ''
37        self.beg = False
38        self.SPGPs = []
39        self.MVs = []
40        self.BNSs = []
41   
42    def handle_starttag(self, tag, attrs):
43#        print('tag:',tag,self.beg,self.in_sp)
44        if tag == 'i' and self.beg:
45            if not self.in_sp:
46                self.in_sp = True
47                self.spgp = ''
48                self.BNS = ''
49        elif tag == 'pre':
50            self.in_pre = True
51            self.MV = ''
52        elif tag == 'sub':
53            self.in_sub = True
54   
55    def handle_data(self, data):
56#        print('*',data)
57        if self.in_sp:
58            if 'No.' in data:
59                self.spgp += data.split('(')[0]        #pick up trailing number!
60                self.in_sp = False
61                self.SPGPs.append(self.spgp)
62                self.BNSs.append(self.BNS)
63#                print('space group:',self.spgp,' BNS: ',self.BNS)
64            else:
65                if self.in_sub and len(self.spgp) == 1:
66                    self.spgp += '_'
67                self.spgp += data
68                if len(self.spgp) == 3 and '_' in self.spgp:
69                    self.spgp += ' '
70                    self.BNS = data
71                    if 'P_S' in self.spgp:      #fix ambiguous std. symbol for triclinics
72                        self.spgp.replace('_S','_c')
73                        self.BNS = 'c'
74#                print(self.spgp,self.BNS)
75        if self.in_pre:
76            self.MV = data
77   
78    def handle_endtag(self, tag):
79#        print ('    end tag: %s'%(tag))
80        if tag == 'script':
81            self.beg = True
82        elif tag == 'pre':
83            self.in_pre = False
84            MV = self.MV.split()
85            MV = ['[[%s,%s,%s],[%s,%s,%s],[%s,%s,%s]]'%(MV[0],MV[4],MV[8],MV[1],MV[5],MV[9],MV[2],MV[6],MV[10]),'[%s.,%s.,%s.]'%(MV[3],MV[7],MV[11])]
86            self.MVs.append(MV)
87#            print('MV:',self.MV)
88        elif tag == 'sub':
89            self.in_sub = False
90           
91def GetNonStdSubgroupsmag(SGData, kvec,star=False,landau=False,maximal=False):
92    '''Run Bilboa's k-Subgroupsmag for a non-standard space group.
93    This requires doing a post to the Bilboa site, which returns all
94    magnetic subgroups of the entered subgroup as the text of a web page
95    with a table containing the BNS magnetic space group symbol, the
96    transformation matrix and index for each subgroup.
97
98    :params list kvec: propogation vector as a list of three numbers
99    :params SGData: space group object (see :ref:`Space Group object<SGData_table>`)
100
101    :returns: (error,text) error: if True no error or False; where
102      text containts a possible web page text
103    '''
104    print('''
105    For use of k-SUBGROUPSMAG, please cite:
106      Symmetry-Based Computational Tools for Magnetic Crystallography,
107      J.M. Perez-Mato, S.V. Gallego, E.S. Tasci, L. Elcoro, G. de la Flor, and M.I. Aroyo
108      Annu. Rev. Mater. Res. 2015. 45,217-48.
109      doi: 10.1146/annurev-matsci-070214-021008
110    ''')
111    starmag = 'no'
112    if star:
113        starmag = 'yes'
114    land = 'no'
115    if landau:
116        land = 'yes'
117    celtodas = 'no'
118    limite = 'spgroup'
119    if maximal:
120        limite = 'maximal'
121    postdict = {'centrosymmetry':'0','crystalsystem':'0','landau':land,
122               'eleccion':'subgrmag1_k','inicio':'nostandard','celtodas':celtodas,
123               'limite':limite,'list':'Submit','listado':'lista','starmagnetica':starmag,
124               'pointgroup':'0','polarity':'0','sub':'1.1',
125               'super':'','tipog':'gmag','wyckoffstrain':''}
126    text,table = G2spc.SGPrint(SGData)
127    OpList = G2spc.TextOps(text,table,reverse=True)
128#    GenList = G2spc.TextGen(SGData,reverse=True)
129    for item in OpList:
130        item += '\n'
131    sym = ""
132    for i in OpList:
133        if sym: sym += '\n'
134        #if sym: sym += ' ' # use this for testing to generate an error in place of previous
135        sym += i.lower()
136    postdict['generators'] = sym
137    for j in [1,2,3]:
138        if kvec[3*j-3] == ' ':
139            break
140        for i,k in zip(('x','y','z'),kvec[3*j-3:3*j]):
141            postdict['km%d%s'%(j,i)] = k
142    page = G2IO.postURL(submagSite,postdict)
143    if not page:
144        print('connection error - not on internet?')
145        return None,None
146    page = page.replace('<font style= "text-decoration: overline;">','<font>-')
147    p = TableParser()
148    p.feed(page)
149    nItms = len(p.MVs)
150    result = list(zip(p.SPGPs,p.BNSs,p.MVs,range(nItms),nItms*[[],],nItms*['[]',]))
151    return result,range(nItms)
152
153def GetNonStdSubgroups(SGData, kvec,star=False,landau=False,maximal=False):
154    '''Run Bilboa's SUBGROUPS for a non-standard space group.
155    This requires doing a post to the Bilboa site, which returns all
156    subgroups of the entered space group as the text of a web page
157    with a table containing the space group symbol, the
158    transformation matrix and index for each subgroup.
159
160    :params list kvec: propogation vector as a list of nine string fractions or blank
161    :params SGData: space group object (see :ref:`Space Group object<SGData_table>`)
162
163    :returns: (error,text) error: if True no error or False; where
164      text containts a possible web page text
165    '''
166    print('''
167    For use of SUBGROUPS, please cite:
168      Symmetry-Based Computational Tools for Magnetic Crystallography,
169      J.M. Perez-Mato, S.V. Gallego, E.S. Tasci, L. Elcoro, G. de la Flor, and M.I. Aroyo
170      Annu. Rev. Mater. Res. 2015. 45,217-48.
171      doi: 10.1146/annurev-matsci-070214-021008
172    ''')
173    starmag = 'no'
174    if star:
175        starmag = 'yes'
176    land = 'no'
177    if landau:
178        land = 'yes'
179    celtodas = 'no'
180    limite = 'spgroup'
181    if maximal:
182        limite = 'maximal'
183    postdict = {'centrosymmetry':'0','crystalsystem':'0','landau':land,
184               'eleccion':'subgrmag1_k','inicio':'nostandard','celtodas':celtodas,
185               'limite':limite,'list':'Submit','listado':'lista','starmagnetica':starmag,
186               'pointgroup':'0','polarity':'0','sub':'1',
187               'super':'','tipog':'gesp','wyckoffstrain':''}
188    text,table = G2spc.SGPrint(SGData)
189    OpList = G2spc.TextOps(text,table,reverse=True)
190#    GenList = G2spc.TextGen(SGData,reverse=True)
191    for item in OpList:
192        item += '\n'
193    sym = ""
194    for i in OpList:
195        if sym: sym += '\n'
196        #if sym: sym += ' ' # use this for testing to generate an error in place of previous
197        sym += i.lower()
198    postdict['generators'] = sym
199    for j in [1,2,3]:
200        if kvec[3*j-3] == ' ':
201            break
202        for i,k in zip(('x','y','z'),kvec[3*j-3:3*j]):
203            postdict['knm%d%s'%(j,i)] = k
204    if not page:
205        print('connection error - not on internet?')
206        return None,None
207    page = page.replace('<font style= "text-decoration: overline;">','<font>-')
208    p = TableParser()
209    p.feed(page)
210    nItms = len(p.MVs)
211    result = list(zip(p.SPGPs,p.MVs,range(nItms),range(nItms),nItms*[0,]))
212    return result,range(nItms)
213
214def test():
215    SGData = G2spc.SpcGroup('p -3 m 1')[1]
216    results,baseList = GetNonStdSubgroupsmag(SGData,('1/3','1/3','1/2',' ',' ',' ',' ',' ',' ',' '))
217    if results:
218        print(results)
219        for [spgp,mv,bns,gid,altList,supList] in results.text:
220            if gid in baseList:
221                print('Space group:',spgp, 'BNS:',bns)
222                print('MV')
223                print(mv)
224    results,baseList = GetNonStdSubgroupsmag(SGData,('1/3','1/3','1/2',' ',' ',' ',' ',' ',' ',' '))
225    if results:
226        for [spgp,mv,gid,altList,supList] in results.text:
227            if gid in baseList:
228                print('Space group:',spgp)
229                print('MV')
230                print(mv)
231       
232
233if __name__ == '__main__':
234    # run self-tests
235    selftestquiet = False
236    test()
237    print ("OK")
Note: See TracBrowser for help on using the repository browser.