Changeset 1068 for trunk/exports/G2cif.py
- Timestamp:
- Sep 27, 2013 6:50:08 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/exports/G2cif.py
r1067 r1068 13 13 ''' 14 14 15 # TODO: set def names for phase/hist save & load, bond pub flags,...15 # TODO: set bond pub flags? 16 16 17 17 import datetime as dt … … 21 21 import cPickle 22 22 import copy 23 import re 23 24 import wx 24 25 import wx.lib.scrolledpanel as wxscroll … … 222 223 223 224 class EditCIFtemplate(wx.Dialog): 224 '''Create a dialog for editing a CIF template 225 '''Create a dialog for editing a CIF template. The edited information is 226 placed in cifblk. If the CIF is saved as a file, the name of that file 227 is saved as ``self.newfile``. 225 228 226 229 :param wx.Frame parent: parent frame or None … … 239 242 Note that the values for each looped CIF item, such as _a, 240 243 are contained in a list, for example as cifblk["_a"] 244 245 :param str defaultname: specifies the default file name to be used for 246 saving the CIF. 241 247 ''' 242 def __init__(self,parent,cifblk,loopstructure ):248 def __init__(self,parent,cifblk,loopstructure,defaultname): 243 249 OKbuttons = [] 244 250 self.cifblk = cifblk 245 251 self.loopstructure = loopstructure 246 252 self.newfile = None 253 self.defaultname = defaultname 247 254 global CIFdic # once this is loaded, keep it around 248 255 if CIFdic is None: … … 254 261 savebtn = wx.Button(self, wx.ID_CLOSE, "Save as template") 255 262 OKbuttons.append(savebtn) 256 savebtn.Bind(wx.EVT_BUTTON,self._on Close)263 savebtn.Bind(wx.EVT_BUTTON,self._onSave) 257 264 OKbtn = wx.Button(self, wx.ID_OK, "Use") 258 265 OKbtn.SetDefault() … … 277 284 '''Display the dialog 278 285 279 :returns: True ,unless Cancel has been pressed.286 :returns: True unless Cancel has been pressed. 280 287 ''' 281 288 return (self.ShowModal() == wx.ID_OK) 282 def _on Close(self,event):289 def _onSave(self,event): 283 290 'Save CIF entries in a template file' 284 291 dlg = wx.FileDialog( 285 292 self, message="Save as CIF template", 286 293 defaultDir=os.getcwd(), 287 defaultFile= "",294 defaultFile=self.defaultname, 288 295 wildcard="CIF (*.cif)|*.cif", 289 296 style=wx.SAVE | wx.CHANGE_DIR … … 321 328 322 329 :param dict cifdic: optional CIF dictionary definitions 330 :param list OKbuttons: A list of wx.Button objects that should 331 be disabled when information in the CIF is invalid 323 332 :param (other): optional keyword parameters for wx.ScrolledPanel 324 333 ''' … … 493 502 return ent 494 503 rw1 = rw.ResizeWidget(self) 495 #print item496 #print dct[item]497 504 ent = G2gd.ValidatedTxtCtrl( 498 505 rw1,dct,item,size=(100, 20), … … 505 512 class CIFtemplateSelect(wx.BoxSizer): 506 513 '''Create a set of buttons to show, select and edit a CIF template 507 514 515 :param frame: wx.Frame object of parent 516 :param panel: wx.Panel object where widgets should be placed 508 517 :param str tmplate: one of 'publ', 'phase', or 'instrument' to determine 509 518 the type of template 519 :param dict G2dict: GSAS-II dict where CIF should be placed. The key 520 "CIF_template" will be used to store either a list or a string. 521 If a list, it will contain a dict and a list defining loops. If 522 an str, it will contain a file name. 523 :param function repaint: reference to a routine to be called to repaint 524 the frame after a change has been made 525 :param str title: A line of text to show at the top of the window 526 :param str defaultname: specifies the default file name to be used for 527 saving the CIF. 510 528 ''' 511 def __init__(self,frame,panel,tmplate,G2dict, repaint, title ):529 def __init__(self,frame,panel,tmplate,G2dict, repaint, title, defaultname=''): 512 530 wx.BoxSizer.__init__(self,wx.VERTICAL) 513 531 self.cifdefs = frame 514 532 self.dict = G2dict 515 533 self.repaint = repaint 516 self.fil= 'template_'+tmplate+'.cif'534 templateDefName = 'template_'+tmplate+'.cif' 517 535 self.CIF = G2dict.get("CIF_template") 536 if defaultname: 537 self.defaultname = defaultname.encode('ascii','replace').strip().replace(' ','_') 538 self.defaultname = re.sub(r'[^a-zA-Z0-9_-]','',self.defaultname) 539 self.defaultname = tmplate + "_" + self.defaultname + ".cif" 540 else: 541 self.defaultname = '' 542 518 543 txt = wx.StaticText(panel,wx.ID_ANY,title) 519 544 self.Add(txt,0,wx.ALIGN_CENTER) … … 526 551 527 552 if not self.CIF: # empty or None 528 for pth in sys.path: 529 if os.path.exists(os.path.join(pth,self.fil)): 530 self.CIF = os.path.join(pth,self.fil) 531 CIFtxt = "Template: "+self.fil 553 for pth in [os.getcwd()]+sys.path: 554 fil = os.path.join(pth,self.defaultname) 555 if os.path.exists(fil) and self.defaultname: 556 self.CIF = fil 557 CIFtxt = "Template: "+self.defaultname 532 558 break 533 559 else: 534 print CIF+' not found in path!' 535 self.CIF = None 536 CIFtxt = "none! (No template found)" 560 for pth in sys.path: 561 fil = os.path.join(pth,templateDefName) 562 if os.path.exists(fil): 563 self.CIF = fil 564 CIFtxt = "Template: "+templateDefName 565 break 566 else: 567 print(self.CIF+' not found in path!') 568 self.CIF = None 569 CIFtxt = "none! (No template found)" 537 570 elif type(self.CIF) is not list and type(self.CIF) is not tuple: 538 571 if not os.path.exists(self.CIF): … … 551 584 # show str, button to select file; button to edit (if CIF defined) 552 585 but = wx.Button(panel,wx.ID_ANY,"Select Template File") 553 but.Bind(wx.EVT_BUTTON,self. onGetTemplateFile)586 but.Bind(wx.EVT_BUTTON,self._onGetTemplateFile) 554 587 hbox = wx.BoxSizer(wx.HORIZONTAL) 555 588 hbox.Add(but,0,0,2) 556 589 but = wx.Button(panel,wx.ID_ANY,"Edit Template") 557 but.Bind(wx.EVT_BUTTON,self. onEditTemplateContents)590 but.Bind(wx.EVT_BUTTON,self._onEditTemplateContents) 558 591 hbox.Add(but,0,0,2) 559 592 #self.Add(hbox,0,wx.ALIGN_CENTER) 560 593 self.Add(hbox) 561 def onGetTemplateFile(self,event):594 def _onGetTemplateFile(self,event): 562 595 dlg = wx.FileDialog( 563 self.cifdefs, message=" Save as CIF template",596 self.cifdefs, message="Read CIF template file", 564 597 defaultDir=os.getcwd(), 565 defaultFile= "",598 defaultFile=self.defaultname, 566 599 wildcard="CIF (*.cif)|*.cif", 567 600 style=wx.OPEN | wx.CHANGE_DIR 568 601 ) 569 if dlg.ShowModal() == wx.ID_OK: 570 self.dict["CIF_template"] = dlg.GetPath() 571 dlg.Destroy() 602 ret = dlg.ShowModal() 603 fil = dlg.GetPath() 604 dlg.Destroy() 605 if ret == wx.ID_OK: 606 import CifFile as cif # PyCifRW from James Hester 607 try: 608 cf = cif.ReadCif(fil) 609 if len(cf.keys()) == 0: raise Exception,"No CIF data_ blocks found" 610 if len(cf.keys()) != 1: 611 print('\nWarning: CIF has more than one block, '+fil) 612 self.dict["CIF_template"] = fil 613 except Exception as err: 614 print('\nError reading CIF: '+fil) 615 dlg = wx.MessageDialog(self.cifdefs, 616 'Error reading CIF '+fil, 617 'Error in CIF file', 618 wx.OK) 619 dlg.ShowModal() 620 dlg.Destroy() 621 print(err.message) 622 return 572 623 self.repaint() #EditCIFDefaults() 573 else: 574 dlg.Destroy() 575 576 def onEditTemplateContents(self,event): 624 625 def _onEditTemplateContents(self,event): 577 626 import CifFile as cif # PyCifRW from James Hester 578 627 if type(self.CIF) is list or type(self.CIF) is tuple: … … 580 629 else: 581 630 dblk,loopstructure = CIF2dict(cif.ReadCif(self.CIF)) 582 dlg = EditCIFtemplate(self.cifdefs,dblk,loopstructure )631 dlg = EditCIFtemplate(self.cifdefs,dblk,loopstructure,self.defaultname) 583 632 val = dlg.Post() 584 633 if val: 585 634 if dlg.newfile: # results saved in file 586 635 self.dict["CIF_template"] = dlg.newfile 587 print 'saved'588 636 else: 589 637 self.dict["CIF_template"] = [dlg.cifblk,dlg.loopstructure] 590 print 'edited'591 638 self.repaint() #EditCIFDefaults() # note that this does a dlg.Destroy() 592 639 else: 593 print 'cancelled'594 640 dlg.Destroy() 595 641 … … 599 645 600 646 class ExportCIF(G2IO.ExportBaseclass): 647 '''Used to create a CIF of an entire project 648 ''' 601 649 def __init__(self,G2frame): 602 650 super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__ … … 700 748 #WriteCIFitem('_refine_ls_matrix_type','userblocks') 701 749 750 def GetCIF(G2dict,tmplate,defaultname=''): 751 CIFobj = G2dict.get("CIF_template") 752 if defaultname: 753 defaultname = defaultname.encode('ascii','replace').strip().replace(' ','_') 754 defaultname = re.sub(r'[^a-zA-Z0-9_-]','',defaultname) 755 defaultname = tmplate + "_" + defaultname + ".cif" 756 else: 757 defaultname = '' 758 templateDefName = 'template_'+tmplate+'.cif' 759 if not CIFobj: # copying a template 760 for pth in [os.getcwd()]+sys.path: 761 fil = os.path.join(pth,defaultname) 762 if os.path.exists(fil) and defaultname: break 763 else: 764 for pth in sys.path: 765 fil = os.path.join(pth,templateDefName) 766 if os.path.exists(fil): break 767 else: 768 print(CIFobj+' not found in path!') 769 return 770 fp = open(fil,'r') 771 txt = fp.read() 772 fp.close() 773 elif type(CIFobj) is not list and type(CIFobj) is not tuple: 774 if not os.path.exists(CIFobj): 775 print("Error: template file has disappeared:"+CIFobj) 776 return 777 fp = open(CIFobj,'r') 778 txt = fp.read() 779 fp.close() 780 else: 781 txt = dict2CIF(CIFobj[0],CIFobj[1]).WriteOut() 782 # remove the PyCifRW header, if present 783 #if txt.find('PyCifRW') > -1 and txt.find('data_') > -1: 784 txt = "# GSAS-II edited template follows "+txt[txt.index("data_")+5:] 785 #txt = txt.replace('data_','#') 786 print '***** Template ********' 787 print txt 788 WriteCIFitem(txt) 789 702 790 def WritePubTemplate(): 703 ''' TODO: insert the publication template ``template_publ.cif`` or somemodified704 version for this project. Store this in the GPX file?791 '''insert the publication template ``template_publ.cif`` or a modified 792 version 705 793 ''' 706 print getCallerDocString()707 708 def WritePhaseTemplate( ):709 ''' TODO: insert the phase template ``template_phase.cif`` or somemodified794 GetCIF(self.OverallParms['Controls'],'publ') 795 796 def WritePhaseTemplate(phasenam): 797 '''insert the phase template ``template_phase.cif`` or a modified 710 798 version for this project 711 799 ''' 712 print getCallerDocString()713 714 def WritePowderTemplate( ):800 GetCIF(self.Phases[phasenam]['General'],'phase',phasenam) 801 802 def WritePowderTemplate(hist): 715 803 '''TODO: insert the phase template ``template_instrument.cif`` or some modified 716 804 version for this project 717 805 ''' 718 print getCallerDocString() 719 720 def WriteSnglXtalTemplate(): 806 histblk = self.Histograms[hist]["Sample Parameters"] 807 GetCIF(histblk,'powder',histblk['InstrName']) 808 809 def WriteSnglXtalTemplate(hist): 721 810 '''TODO: insert the single-crystal histogram template 722 811 for this project 723 812 ''' 724 print getCallerDocString() 813 histblk = self.Histograms[hist]["Instrument Parameters"][0] 814 GetCIF(histblk,'single',histblk['InstrName']) 725 815 726 816 def FormatSH(phasenam): … … 1135 1225 mult = at[cmult] 1136 1226 if not massDict.get(at[ct]): 1137 print 'No mass found for atom type '+at[ct]1138 print 'Will not compute cell contents for phase '+phasenam1227 print('Error: No mass found for atom type '+at[ct]) 1228 print('Will not compute cell contents for phase '+phasenam) 1139 1229 return 1140 1230 cellmass += massDict[at[ct]]*mult*fval … … 1246 1336 AtomLabels,DistArray,AngArray = G2stMn.RetDistAngle(DisAglCtls,DisAglData) 1247 1337 except KeyError: # inside DistAngle for missing atom types in DisAglCtls 1248 print '**** ERROR - try again but do "Reset" to fill in missing atom types ****'1338 print('**** ERROR - try again but do "Reset" to fill in missing atom types ****') 1249 1339 1250 1340 # loop over interatomic distances for this phase … … 1352 1442 blockid = datablockidDict.get(hist) 1353 1443 if not blockid: 1354 print 1355 phasenam)+" histogram "+str(hist) 1444 print("Internal error: no block for data. Phase "+str( 1445 phasenam)+" histogram "+str(hist)) 1356 1446 histlist = [] 1357 1447 break … … 1555 1645 for ref in histblk['Reflection Lists'][phasenam]: 1556 1646 if DEBUG: 1557 print 'DEBUG: skip reflection list'1647 print('DEBUG: skipping reflection list') 1558 1648 break 1559 1649 if hklmin is None: … … 1631 1721 1632 1722 if DEBUG: 1633 print 'DEBUG: skip profile list'1723 print('DEBUG: skipping profile list') 1634 1724 else: 1635 1725 for x,yobs,yw,ycalc,ybkg in zip(histblk['Data'][0], … … 1755 1845 self.cifdefs.SetTitle('Edit CIF settings') 1756 1846 vbox = wx.BoxSizer(wx.VERTICAL) 1847 but = wx.Button(self.cifdefs, wx.ID_ANY,'Edit CIF Author') 1848 but.Bind(wx.EVT_BUTTON,EditAuthor) 1849 vbox.Add(but,0,wx.ALIGN_CENTER,3) 1850 but = wx.Button(self.cifdefs, wx.ID_ANY,'Edit Instrument Name(s)') 1851 but.Bind(wx.EVT_BUTTON,EditInstNames) 1852 vbox.Add(but,0,wx.ALIGN_CENTER,3) 1757 1853 cpnl = wxscroll.ScrolledPanel(self.cifdefs,size=(300,300)) 1758 1854 cbox = wx.BoxSizer(wx.VERTICAL) 1759 but = wx.Button(cpnl, wx.ID_ANY,'Edit CIF Author')1760 but.Bind(wx.EVT_BUTTON,EditAuthor)1761 cbox.Add(but,0,wx.ALIGN_CENTER,3)1762 but = wx.Button(cpnl, wx.ID_ANY,'Edit Instrument Name(s)')1763 but.Bind(wx.EVT_BUTTON,EditInstNames)1764 cbox.Add(but,0,wx.ALIGN_CENTER,3)1765 1855 G2gd.HorizontalLine(cbox,cpnl) 1766 1856 cbox.Add( … … 1779 1869 cpnl,'phase',phasedict['General'], 1780 1870 EditCIFDefaults, 1781 title), 1871 title, 1872 phasenam), 1782 1873 0,wx.EXPAND|wx.ALIGN_LEFT|wx.ALL) 1783 1874 cpnl.SetSizer(cbox) … … 1797 1888 cpnl,'powder',histblk["Sample Parameters"], 1798 1889 EditCIFDefaults, 1799 title), 1890 title, 1891 histblk["Sample Parameters"]['InstrName']), 1800 1892 0,wx.EXPAND|wx.ALIGN_LEFT|wx.ALL) 1801 1893 cpnl.SetSizer(cbox) … … 1809 1901 cpnl,'single',histblk["Instrument Parameters"][0], 1810 1902 EditCIFDefaults, 1811 title), 1903 title, 1904 histblk["Instrument Parameters"][0]['InstrName']), 1812 1905 0,wx.EXPAND|wx.ALIGN_LEFT|wx.ALL) 1813 1906 cpnl.SetSizer(cbox) … … 1990 2083 WritePubTemplate() 1991 2084 WriteOverall() 1992 WritePhaseTemplate( )2085 WritePhaseTemplate(phasenam) 1993 2086 # report the phase info 1994 2087 WritePhaseInfo(phasenam) … … 2007 2100 FormatInstProfile(histblk["Instrument Parameters"],histblk['hId']) 2008 2101 +'\n'+FormatPhaseProfile(phasenam)) 2009 WritePowderTemplate( )2102 WritePowderTemplate(hist) 2010 2103 WritePowderData(hist) 2011 2104 elif hist.startswith("HKLF") and not self.quickmode: 2012 WriteSnglXtalTemplate( )2105 WriteSnglXtalTemplate(hist) 2013 2106 WriteSingleXtalData(hist) 2014 2107 else: … … 2073 2166 i = self.Phases[phasenam]['pId'] 2074 2167 WriteCIFitem('\ndata_'+self.CIFname+"_phase_"+str(i)) 2075 print "debug, processing ",phasenam2076 2168 WriteCIFitem('# Information for phase '+str(i)) 2077 2169 WriteCIFitem('_pd_block_id',datablockidDict[phasenam]) 2078 2170 # report the phase 2079 WritePhaseTemplate( )2171 WritePhaseTemplate(phasenam) 2080 2172 WritePhaseInfo(phasenam) 2081 2173 # preferred orientation … … 2106 2198 WriteCIFitem('# Information for histogram '+str(i)+': '+hist) 2107 2199 WriteCIFitem('_pd_block_id',datablockidDict[hist]) 2108 WritePowderTemplate( )2200 WritePowderTemplate(hist) 2109 2201 WritePowderData(hist) 2110 2202 for i in sorted(self.xtalDict.keys()): … … 2116 2208 WriteCIFitem('# Information for histogram '+str(i)+': '+hist) 2117 2209 WriteCIFitem('_pd_block_id',datablockidDict[hist]) 2118 WriteSnglXtalTemplate( )2210 WriteSnglXtalTemplate(hist) 2119 2211 WriteSingleXtalData(hist) 2120 2212
Note: See TracChangeset
for help on using the changeset viewer.