Changeset 1115


Ignore:
Timestamp:
Oct 18, 2013 4:00:32 PM (8 years ago)
Author:
toby
Message:

rework exports for new types

Location:
trunk
Files:
2 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASII.py

    r1106 r1115  
    3030import wx
    3131import matplotlib as mpl
    32 import wx.lib.inspection as wxeye
    3332try:
    3433    import OpenGL as ogl
     
    11541153            singlemenu, help='Export single crystal histogram(s)')
    11551154
     1155        imagemenu = wx.Menu()
     1156        item = menu.AppendMenu(
     1157            wx.ID_ANY, 'Images as',
     1158            imagemenu, help='Export powder image(s)')
     1159
     1160        mapmenu = wx.Menu()
     1161        item = menu.AppendMenu(
     1162            wx.ID_ANY, 'Maps as',
     1163            mapmenu, help='Export density map(s)')
     1164
     1165        # pdfmenu = wx.Menu()
     1166        # item = menu.AppendMenu(
     1167        #     wx.ID_ANY, 'PDFs as',
     1168        #     pdfmenu, help='Export pair distribution function(s)')
     1169
    11561170        # find all the exporter files
    11571171        pathlist = sys.path
     
    12021216                elif typ == "single":
    12031217                    submenu = singlemenu
     1218                elif typ == "image":
     1219                    submenu = imagemenu
     1220                elif typ == "map":
     1221                    submenu = mapmenu
     1222                # elif typ == "pdf":
     1223                #     submenu = pdfmenu
    12041224                else:
    12051225                    print("Error, unknown type in "+str(obj))
     
    12141234        #code to debug an Exporter. much is hard-coded below, but code is reloaded before
    12151235        # each use allowing faster development
    1216         # def DebugText(event):
     1236        # def DebugExport(event):
    12171237        #     reload(G2IO)
    1218         #     import dev_export_example
    1219         #     reload(dev_export_example)
    1220         #     dev_export_example.ExportSingleText(self).Exporter(event)
     1238        #     #import dev_export as dev
     1239        #     import G2export_csv as dev
     1240        #     reload(dev)
     1241        #     #dev.ExportTest(self).Exporter(event)
     1242        #     dev.ExportPowderCSV(self).Exporter(event)
     1243        #     dev.ExportPowderReflCSV(self).Exporter(event)
    12211244        # item = menu.Append(
    12221245        #     wx.ID_ANY,kind=wx.ITEM_NORMAL,
    1223         #     help="debug text",text="test Export")
    1224         # self.Bind(wx.EVT_MENU, DebugText, id=item.GetId())
    1225         # self.ExportLookup[item.GetId()] = 'single'
     1246        #     help="debug exporter",text="test Export")
     1247        # self.Bind(wx.EVT_MENU, DebugExport, id=item.GetId())
     1248        # #self.ExportLookup[item.GetId()] = 'image'
     1249        # self.ExportLookup[item.GetId()] = 'powder'
    12261250           
    12271251    def _Add_ExportMenuItems(self,parent):
     
    27332757    # may want to do this if we can
    27342758    application = GSASIImain(0)
    2735     if wxInspector: wxeye.InspectionTool().Show()
     2759    if wxInspector:
     2760        import wx.lib.inspection as wxeye
     2761        wxeye.InspectionTool().Show()
    27362762
    27372763    #application.main.OnRefine(None)
  • trunk/GSASIIIO.py

    r1110 r1115  
    156156    file extension
    157157
    158     :param wx.Frame G2frame: main GSAS-II Frame and data object
     158    :param wx.Frame G2frame: main GSAS-II Frame and data object. Note: not used!
    159159
    160160    :param str imagefile: name of image file
     
    17591759        self.powderDict = {}
    17601760        self.xtalDict = {}
    1761         # updated in SetupExport, when used
     1761        # updated in InitExport:
    17621762        self.currentExportType = None # type of export that has been requested
     1763        # updated in ExportSelect (when used):
    17631764        self.phasenam = None # a list of selected phases
    17641765        self.histnam = None # a list of selected histograms
     
    17691770        # The following types are defined: 'project', "phase", "powder", "single"
    17701771        self.multiple = False # set as True if the class can export multiple phases or histograms
    1771         # ignored for "project" exports
    1772 
    1773     def SetupExport(self,event,AskFile=True):
    1774         '''Determines the type of menu that called the Exporter. Selects histograms
    1775         or phases when needed.
     1772        # self.multiple is ignored for "project" exports
     1773
     1774    def InitExport(self,event):
     1775        '''Determines the type of menu that called the Exporter.
     1776        '''
     1777        if event:
     1778            self.currentExportType = self.G2frame.ExportLookup.get(event.Id)
     1779
     1780    def ExportSelect(self,AskFile=True):
     1781        '''Selects histograms or phases when needed. Sets a default file name.
    17761782
    17771783        :param bool AskFile: if AskFile is True (default) get the name of the file
     
    17791785        :returns: True in case of an error
    17801786        '''
    1781         if event:
    1782             self.currentExportType = self.G2frame.ExportLookup.get(event.Id)
    1783         if AskFile:
    1784             self.filename = self.askSaveFile()
    1785         else:
    1786             self.filename = self.defaultSaveFile()
    1787         if not self.filename: return True
    17881787       
    17891788        if self.currentExportType == 'phase':
     
    17911790                self.G2frame.ErrorDialog(
    17921791                    'Empty project',
    1793                     'Project does not contain any data or phases or they are not interconnected.')
     1792                    'Project does not contain any phases.')
    17941793                return True
    17951794            elif self.multiple:
     
    18131812                self.G2frame.ErrorDialog(
    18141813                    'Empty project',
    1815                     'Project does not contain any single crystal data or data is not connected to a phase.')
     1814                    'Project does not contain any single crystal data.')
    18161815                return True
    18171816            elif self.multiple:
     
    18351834                self.G2frame.ErrorDialog(
    18361835                    'Empty project',
    1837                     'Project does not contain any powder data or data is not connected to a phase.')
     1836                    'Project does not contain any powder data.')
    18381837                return True
    18391838            elif self.multiple:
     
    18531852                    if hnum is None: return True
    18541853                    self.histnam = [choices[hnum]]
     1854        elif self.currentExportType == 'image':
     1855            if len(self.Histograms) == 0:
     1856                self.G2frame.ErrorDialog(
     1857                    'Empty project',
     1858                    'Project does not contain any images.')
     1859                return True
     1860            elif len(self.Histograms) == 1:
     1861                self.histnam = self.Histograms.keys()
     1862            else:
     1863                choices = sorted(self.Histograms.keys())
     1864                hnum = G2gd.ItemSelector(choices,self.G2frame,multiple=self.multiple)
     1865                if hnum is None: return True
     1866                if self.multiple:
     1867                    self.histnam = [choices[i] for i in hnum]
     1868                else:
     1869                    self.histnam = [choices[hnum]]
     1870        if AskFile:
     1871            self.filename = self.askSaveFile()
     1872        else:
     1873            self.filename = self.defaultSaveFile()
     1874        if not self.filename: return True
     1875       
     1876    # def SetupExport(self,event,AskFile=True):
     1877    #     '''Determines the type of menu that called the Exporter. Selects histograms
     1878    #     or phases when needed. Better to replace with individual calls to
     1879    #     self.InitExport() and self.ExportSelect() so that the former can be called prior
     1880    #     to self.LoadTree()
     1881
     1882    #     :param bool AskFile: if AskFile is True (default) get the name of the file
     1883    #       in a dialog
     1884    #     :returns: True in case of an error
     1885    #     '''
     1886    #     self.ExportInit(event)
     1887    #     return self.ExportSelect(AskFile)
    18551888                   
    18561889    def loadParmDict(self):
     
    19401973        '''
    19411974        self.OverallParms = {}
     1975        self.powderDict = {}
     1976        self.xtalDict = {}
     1977        if self.G2frame.PatternTree.IsEmpty(): return # nothing to do
     1978        histType = None       
     1979        if self.currentExportType == 'phase':
     1980            # if exporting phases load them here
     1981            sub = G2gd.GetPatternTreeItemId(self.G2frame,self.G2frame.root,'Phases')
     1982            if not sub:
     1983                print 'no phases found'
     1984                return True
     1985            item, cookie = self.G2frame.PatternTree.GetFirstChild(sub)
     1986            while item:
     1987                phaseName = self.G2frame.PatternTree.GetItemText(item)
     1988                self.Phases[phaseName] =  self.G2frame.PatternTree.GetItemPyData(item)
     1989                item, cookie = self.G2frame.PatternTree.GetNextChild(sub, cookie)
     1990            return
     1991        elif self.currentExportType == 'single':
     1992            histType = 'HKLF'
     1993        elif self.currentExportType == 'powder':
     1994            histType = 'PWDR'
     1995        elif self.currentExportType == 'image':
     1996            histType = 'IMG'
     1997
     1998        if histType: # Loading just one kind of tree entry
     1999            item, cookie = self.G2frame.PatternTree.GetFirstChild(self.G2frame.root)
     2000            while item:
     2001                name = self.G2frame.PatternTree.GetItemText(item)
     2002                if name.startswith(histType):
     2003                    if self.Histograms.get(name): # there is already an item with this name
     2004                        if name[-1] == '9':
     2005                            name = name[:-1] + '10'
     2006                        elif name[-1] in '012345678':
     2007                            name = name[:-1] + str(int(name[:-1])+1)
     2008                        else:                           
     2009                            name += '-1'
     2010                    self.Histograms[name] = {}
     2011                    self.Histograms[name]['Data'] = self.G2frame.PatternTree.GetItemPyData(item)[1]
     2012                    item2, cookie2 = self.G2frame.PatternTree.GetFirstChild(item)
     2013                    while item2:
     2014                        child = self.G2frame.PatternTree.GetItemText(item2)
     2015                        self.Histograms[name][child] = self.G2frame.PatternTree.GetItemPyData(item2)
     2016                        item2, cookie2 = self.G2frame.PatternTree.GetNextChild(item, cookie2)
     2017                item, cookie = self.G2frame.PatternTree.GetNextChild(self.G2frame.root, cookie)
     2018            # index powder and single crystal histograms by number
     2019            for hist in self.Histograms:
     2020                if hist.startswith("PWDR"):
     2021                    d = self.powderDict
     2022                elif hist.startswith("HKLF"):
     2023                    d = self.xtalDict
     2024                else:
     2025                    return                   
     2026                i = self.Histograms[hist].get('hId')
     2027                if i is None and not d.keys():
     2028                    i = 0
     2029                elif i is None or i in d.keys():
     2030                    i = max(d.keys())+1
     2031                d[i] = hist
     2032            return
     2033        # else standard load: using all interlinked phases and histograms
    19422034        self.Histograms,self.Phases = self.G2frame.GetUsedHistogramsAndPhasesfromTree()
    1943         if self.G2frame.PatternTree.IsEmpty(): return # nothing to do
    19442035        item, cookie = self.G2frame.PatternTree.GetFirstChild(self.G2frame.root)
    19452036        while item:
     
    19502041            item, cookie = self.G2frame.PatternTree.GetNextChild(self.G2frame.root, cookie)
    19512042        # index powder and single crystal histograms
    1952         self.powderDict = {}
    1953         self.xtalDict = {}
    19542043        for hist in self.Histograms:
    19552044            i = self.Histograms[hist]['hId']
     
    20452134          cell values and `cellSig` has their uncertainties.
    20462135        """
    2047         phasedict = self.Phases[phasenam] # pointer to current phase info           
    2048         pfx = str(phasedict['pId'])+'::'
    2049         A,sigA = G2stIO.cellFill(pfx,phasedict['General']['SGData'],self.parmDict,self.sigDict)
    2050         cellSig = G2stIO.getCellEsd(pfx,
    2051                                     phasedict['General']['SGData'],A,
    2052                                     self.OverallParms['Covariance'])  # returns 7 vals, includes sigVol
    2053         cellList = G2lat.A2cell(A) + (G2lat.calc_V(A),)
    2054         return cellList,cellSig
     2136        phasedict = self.Phases[phasenam] # pointer to current phase info
     2137        try:
     2138            pfx = str(phasedict['pId'])+'::'
     2139            A,sigA = G2stIO.cellFill(pfx,phasedict['General']['SGData'],self.parmDict,self.sigDict)
     2140            cellSig = G2stIO.getCellEsd(pfx,
     2141                                        phasedict['General']['SGData'],A,
     2142                                        self.OverallParms['Covariance'])  # returns 7 vals, includes sigVol
     2143            cellList = G2lat.A2cell(A) + (G2lat.calc_V(A),)
     2144            return cellList,cellSig
     2145        except KeyError:
     2146            cell = phasedict['General']['Cell'][1:]
     2147            return cell,7*[0]
    20552148   
    20562149    def GetAtoms(self,phasenam):
     
    20682161            their standard uncertainty (or a negative value)
    20692162          * td is contains a list with either one or six pairs of numbers:
    2070             if one number it is U\ :sub:`iso` and with six it is
     2163            if one number it is U\ :sub:`iso` and with six numbers it is
    20712164            U\ :sub:`11`, U\ :sub:`22`, U\ :sub:`33`, U\ :sub:`12`, U\ :sub:`13` & U\ :sub:`23`
    20722165            paired with their standard uncertainty (or a negative value)
  • trunk/GSASIIgrid.py

    r1110 r1115  
    13131313                    ParentFrame,title, header, ChoiceList,
    13141314                    style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.OK|wx.CENTRE)
    1315             pass
     1315            # I would like to add a select all and toggle all button
     1316            # to this dialog in some manner, but that is going to
     1317            # require that I recode the entire dialog -- TODO someday
    13161318        else:
    13171319            if useCancel:
     
    13251327        if dlg.ShowModal() == wx.ID_OK:
    13261328            if multiple:
     1329                dlg.Destroy()
    13271330                return dlg.GetSelections()
    13281331            else:
     1332                dlg.Destroy()
    13291333                return dlg.GetSelection()
    13301334        else:
     1335            dlg.Destroy()
    13311336            return None
    13321337        dlg.Destroy()
  • trunk/exports/G2export_CIF.py

    r1102 r1115  
    15161516
    15171517        # the export process starts here
     1518        self.InitExport(event)
    15181519        # load all of the tree into a set of dicts
    15191520        self.loadTree()
    15201521        # create a dict with refined values and their uncertainties
    15211522        self.loadParmDict()
    1522         if self.SetupExport(event,                         # set export parameters
    1523                             AskFile=(self.mode=='simple')
    1524                             ): return
     1523        if self.ExportSelect(AskFile=(self.mode=='simple')):    # set export parameters
     1524            return
    15251525        # Someday: get restraint & constraint info
    15261526        #restraintDict = self.OverallParms.get('Restraints',{})
     
    18421842class ExportPhaseCIF(ExportCIF):
    18431843    '''Used to create a simple CIF of at most one phase. Uses exact same code as
    1844     :class:`ExportCIF` except that `self.mode` is set to "simple". Shows up in menu as
    1845     Quick CIF
     1844    :class:`ExportCIF` except that `self.mode` is set to "simple" in `self.InitExport`.
     1845    Shows up in menu as Quick CIF.
    18461846
    18471847    :param wx.Frame G2frame: reference to main GSAS-II frame
     
    18621862    '''Used to create a simple CIF containing diffraction data only. Uses exact same code as
    18631863    :class:`ExportCIF` except that `self.mode` is set to "simple" and `self.currentExportType`
    1864     is set to "single" or "powder" in `self.SetupExport`. Shows up in menus as Data-only CIF.
     1864    is set to "single" or "powder" in `self.InitExport`. Shows up in menus as Data-only CIF.
    18651865
    18661866    :param wx.Frame G2frame: reference to main GSAS-II frame
  • trunk/exports/G2export_PDB.py

    r1102 r1115  
    116116           
    117117        # the export process starts here
     118        self.InitExport(event)
    118119        # load all of the tree into a set of dicts
    119120        self.loadTree()
    120121        # create a dict with refined values and their uncertainties
    121122        self.loadParmDict()
    122         if self.SetupExport(event,AskFile=True):                         # set export parameters
     123        if self.ExportSelect(AskFile=True):    # set export parameters
    123124            return
    124125        for phasenam in self.phasenam:
     
    213214        '''
    214215        # the export process starts here
     216        self.InitExport(event)
    215217        # load all of the tree into a set of dicts
    216218        self.loadTree()
    217219        # create a dict with refined values and their uncertainties
    218220        self.loadParmDict()
    219         if self.SetupExport(event,AskFile=True):                       # set export parameters
     221        if self.ExportSelect(AskFile=True):    # set export parameters
    220222            return
    221223        for phasenam in self.phasenam:
  • trunk/exports/G2export_examples.py

    r1102 r1115  
    88# $Id$
    99########### SVN repository information ###################
    10 '''Code to demonstrate how export routines are created
     10'''Code to demonstrate how GSAS-II data export routines are created
    1111'''
    1212import os.path
     
    3030        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
    3131            G2frame=G2frame,
    32             formatName = 'text',
     32            formatName = 'Text file',
    3333            extension='.txt',
    3434            longFormatName = 'Export phase as text file'
     
    4141        '''
    4242        # the export process starts here
     43        self.InitExport(event)
    4344        # load all of the tree into a set of dicts
    4445        self.loadTree()
    4546        # create a dict with refined values and their uncertainties
    4647        self.loadParmDict()
    47         if self.SetupExport( # set export parameters
    48             event,                         
     48        if self.ExportSelect( # set export parameters
    4949            AskFile=True     # prompt the user for a file name
    5050            ): return
     
    114114        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
    115115            G2frame=G2frame,
    116             formatName = 'text',
     116            formatName = 'Text file',
    117117            extension='.txt',
    118118            longFormatName = 'Export powder data as text file'
     
    125125        '''
    126126        # the export process starts here
    127         # load all of the tree into a set of dicts
    128         self.loadTree()
    129         print 'Exporter',self.defaultSaveFile()
    130         if self.SetupExport( # set export parameters
    131             event,
     127        self.InitExport(event)
     128        # load all of the tree into a set of dicts
     129        self.loadTree()
     130        if self.ExportSelect( # set export parameters
    132131            AskFile=False # use the default file name
    133132            #AskFile=True
     
    136135        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
    137136        histblk = self.Histograms[hist]
    138         #inst = histblk['Instrument Parameters'][0]
    139         #hId = histblk['hId']
    140         #pfx = ':' + str(hId) + ':'
    141137        fmt = 2*"{:12.3f} " + "{:12.5f} " + 2*"{:12.3f} "
    142138        hfmt = 5*"{:>12s} "
     
    171167        '''Export a set of powder reflections as a text file
    172168        '''
    173         # load all of the tree into a set of dicts
    174         self.loadTree()
    175         if self.SetupExport( # set export parameters
    176             event,
     169        self.InitExport(event)
     170        # load all of the tree into a set of dicts
     171        self.loadTree()
     172        if self.ExportSelect( # set export parameters
    177173            AskFile=False # use the default file name
    178174            #AskFile=True
     
    204200        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
    205201            G2frame=G2frame,
    206             formatName = 'reflection list as text',
     202            formatName = 'Text file',
    207203            extension='.txt',
    208204            longFormatName = 'Export reflection list as a text file'
     
    215211        '''
    216212        # the export process starts here
    217         # load all of the tree into a set of dicts
    218         self.loadTree()
    219         if self.SetupExport( # set export parameters
    220             event,
     213        self.InitExport(event)
     214        # load all of the tree into a set of dicts
     215        self.loadTree()
     216        if self.ExportSelect( # set export parameters
    221217            AskFile=False # use the default file name
    222218            #AskFile=True
  • trunk/exports/G2export_shelx.py

    r1102 r1115  
    2626        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
    2727            G2frame=G2frame,
    28             formatName = 'SHELX',
     28            formatName = 'SHELX .ins',
    2929            extension='.ins',
    3030            longFormatName = 'Export phase as SHELX .ins file'
     
    3838        import re
    3939        # the export process starts here
     40        self.InitExport(event)
    4041        # load all of the tree into a set of dicts
    4142        self.loadTree()
    4243        # create a dict with refined values and their uncertainties
    4344        self.loadParmDict()
    44         if self.SetupExport(event,                         # set export parameters
    45                             AskFile=True
    46                             ): return
     45        if self.ExportSelect(  # set export parameters
     46            AskFile=True): return
    4747        for phasenam in self.phasenam:
    4848            phasedict = self.Phases[phasenam] # pointer to current phase info           
     
    5656            # title line
    5757            self.Write("TITL from "+str(self.G2frame.GSASprojectfile)+", phase "+str(phasenam))
    58             # get & write cell parameters
    59             pfx = str(phasedict['pId'])+'::'
    60             A,sigA = G2stIO.cellFill(pfx,phasedict['General']['SGData'],self.parmDict,self.sigDict)
    61             self.Write("CELL 0.5 {:.5f} {:.5f} {:.5f} {:.3f} {:.3f} {:.3f}".format(*G2lat.A2cell(A)))
     58            # get & write cell parameters
     59            cell,sig = self.GetCell(phasenam)
     60            self.Write("CELL 0.5 {:.5f} {:.5f} {:.5f} {:.3f} {:.3f} {:.3f}".format(*cell[:6]))
    6261            # Shelx lattice number
    6362            lattnum = {'P':1,'I':2,'R':2,'F':3,'A':4,'B':5,'C':6}.get(phasedict['General']['SGData']['SGLatt'],0)
Note: See TracChangeset for help on using the changeset viewer.