Changeset 1261 for trunk/GSASIIIO.py


Ignore:
Timestamp:
Mar 24, 2014 5:22:41 PM (8 years ago)
Author:
toby
Message:

reorg exports to implement directory selection

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIIO.py

    r1259 r1261  
    18401840######################################################################
    18411841class ExportBaseclass(object):
    1842     '''Defines a base class for the exporting of GSAS-II results
     1842    '''Defines a base class for the exporting of GSAS-II results.
     1843
     1844    This class is subclassed in the various exports/G2export_*.py files. Those files
     1845    are imported in :meth:`GSASII.GSASII._init_Exports` which defines the
     1846    appropriate menu items for each one and the .Exporter method is called
     1847    directly from the menu item.
     1848   
    18431849    '''
    18441850    def __init__(self,
     
    18671873        self.phasenam = None # a list of selected phases
    18681874        self.histnam = None # a list of selected histograms
    1869         self.filename = None # name of file to be written
     1875        self.filename = None # name of file to be written (single export) or template (multiple files)
     1876        self.dirname = '' # name of directory where file(s) will be written
     1877        self.fullpath = '' # name of file being written -- full path
    18701878       
    18711879        # items that should be defined in a subclass of this class
     
    18761884
    18771885    def InitExport(self,event):
    1878         '''Determines the type of menu that called the Exporter.
     1886        '''Determines the type of menu that called the Exporter and
     1887        misc initialization.
    18791888        '''
     1889        self.filename = None # name of file to be written (single export)
     1890        self.dirname = '' # name of file to be written (multiple export)
    18801891        if event:
    18811892            self.currentExportType = self.G2frame.ExportLookup.get(event.Id)
     
    18991910        return file0
    19001911
    1901     def ExportSelect(self,AskFile=True):
    1902         '''Selects histograms or phases when needed. Sets a default file name.
    1903 
    1904         :param bool AskFile: if AskFile is True (default) get the name of the file
    1905           in a dialog
     1912    def ExportSelect(self,AskFile='ask'):
     1913        '''Selects histograms or phases when needed. Sets a default file name when
     1914        requested in self.filename; always sets a default directory in self.dirname.
     1915
     1916        :param bool AskFile: Determines how this routine processes getting a
     1917          location to store the current export(s).
     1918          * if AskFile is 'ask' (default option), get the name of the file to be written;
     1919            self.filename and self.dirname are always set. In the case where
     1920            multiple files must be generated, the export routine should do this
     1921            based on self.filename as a template.
     1922          * if AskFile is 'dir', get the name of the directory to be used;
     1923            self.filename is not used, but self.dirname is always set. The export routine
     1924            will always generate the file name.
     1925          * if AskFile is 'single', get only the name of the directory to be used when
     1926            multiple items will be written (as multiple files) are used
     1927            *or* a complete file name is requested when a single file
     1928            name is selected. self.dirname is always set and self.filename used
     1929            only when a single file is selected.
     1930          * if AskFile is 'default', creates a name of the file to be used from
     1931            the name of the project (.gpx) file. If the project has not been saved,
     1932            then the name of file is requested.
     1933            self.filename and self.dirname are always set. In the case where
     1934            multiple file names must be generated, the export routine should do this
     1935            based on self.filename.
     1936          * if AskFile is 'default-dir', sets self.dirname from the project (.gpx)
     1937            file. If the project has not been saved, then a directory is requested.
     1938            self.filename is not used.
     1939
    19061940        :returns: True in case of an error
    19071941        '''
    19081942       
     1943        numselected = 1
    19091944        if self.currentExportType == 'phase':
    19101945            if len(self.Phases) == 0:
     
    19211956                self.phasenam = [choices[i] for i in phasenum]
    19221957                if not self.phasenam: return True
     1958                numselected = len(self.phasenam)
    19231959            else:
    19241960                choices = sorted(self.Phases.keys())
     
    19261962                if phasenum is None: return True
    19271963                self.phasenam = [choices[phasenum]]
     1964                numselected = len(self.phasenam)
    19281965        elif self.currentExportType == 'single':
    19291966            if len(self.xtalDict) == 0:
     
    19391976                if not hnum: return True
    19401977                self.histnam = [choices[i] for i in hnum]
     1978                numselected = len(self.histnam)
    19411979            else:
    19421980                choices = sorted(self.xtalDict.values())
     
    19441982                if hnum is None: return True
    19451983                self.histnam = [choices[hnum]]
     1984                numselected = len(self.histnam)
    19461985        elif self.currentExportType == 'powder':
    19471986            if len(self.powderDict) == 0:
     
    19571996                if not hnum: return True
    19581997                self.histnam = [choices[i] for i in hnum]
     1998                numselected = len(self.histnam)
    19591999            else:
    19602000                choices = sorted(self.powderDict.values())
     
    19622002                if hnum is None: return True
    19632003                self.histnam = [choices[hnum]]
     2004                numselected = len(self.histnam)
    19642005        elif self.currentExportType == 'image':
    19652006            if len(self.Histograms) == 0:
     
    19792020                    if hnum is None: return True
    19802021                    self.histnam = [choices[hnum]]
     2022                numselected = len(self.histnam)
    19812023        if self.currentExportType == 'map':
    19822024            # search for phases with maps
     
    20112053                    if phasenum is None: return True
    20122054                    self.phasenam = [mapPhases[phasenum]]
    2013 
    2014         if AskFile:
    2015             self.filename = self.askSaveFile()
     2055            numselected = len(self.phasenam)
     2056
     2057        # items selected, now set self.dirname and usually self.filename
     2058        if AskFile == 'ask' or (AskFile == 'single' and numselected == 1) or (
     2059            AskFile == 'default' and not self.G2frame.GSASprojectfile
     2060            ):
     2061            filename = self.askSaveFile()
     2062            if not filename: return True
     2063            self.dirname,self.filename = os.path.split(filename)
     2064        elif AskFile == 'dir' or AskFile == 'single' or (
     2065            AskFile == 'default-dir' and not self.G2frame.GSASprojectfile
     2066            ):
     2067            self.dirname = self.askSaveDirectory()
     2068            if not self.dirname: return True
     2069        elif AskFile == 'default-dir' or AskFile == 'default':
     2070            self.dirname,self.filename = os.path.split(
     2071                os.path.splitext(self.G2frame.GSASprojectfile)[0] + self.extension
     2072                )
    20162073        else:
    2017             self.filename = self.defaultSaveFile()
    2018         if not self.filename: return True
     2074            raise Exception('This should not happen!')
    20192075       
    2020     # def SetupExport(self,event,AskFile=True):
    2021     #     '''Determines the type of menu that called the Exporter. Selects histograms
    2022     #     or phases when needed. Better to replace with individual calls to
    2023     #     self.InitExport() and self.ExportSelect() so that the former can be called prior
    2024     #     to self.LoadTree()
    2025 
    2026     #     :param bool AskFile: if AskFile is True (default) get the name of the file
    2027     #       in a dialog
    2028     #     :returns: True in case of an error
    2029     #     '''
    2030     #     self.ExportInit(event)
    2031     #     return self.ExportSelect(AskFile)
    2032                    
    20332076    def loadParmDict(self):
    20342077        '''Load the GSAS-II refinable parameters from the tree into a dict (self.parmDict). Update
     
    22272270        '''Ask the user to supply a file name
    22282271
    2229         :returns: a file name (str)
     2272        :returns: a file name (str) or None if Cancel is pressed
    22302273        '''
    22312274        defnam = os.path.splitext(
     
    22482291        return filename
    22492292
     2293    def askSaveDirectory(self):
     2294        '''Ask the user to supply a directory name. Path name is used as the
     2295        starting point for the next export path search.
     2296
     2297        :returns: a directory name (str) or None if Cancel is pressed
     2298        '''
     2299        if self.G2frame.exportDir:
     2300            startdir = self.G2frame.exportDir
     2301        elif self.G2frame.GSASprojectfile:
     2302            startdir = os.path.split(self.G2frame.GSASprojectfile)[0]
     2303        elif self.G2frame.dirname:
     2304            startdir = self.G2frame.dirname
     2305        else:
     2306            startdir = ''
     2307        dlg = wx.DirDialog(
     2308            self.G2frame, 'Input directory where file(s) will be written', startdir,
     2309            wx.DD_DEFAULT_STYLE)
     2310        dlg.CenterOnParent()
     2311        try:
     2312            if dlg.ShowModal() == wx.ID_OK:
     2313                filename = dlg.GetPath()
     2314                self.G2frame.exportDir = filename
     2315            else:
     2316                filename = None
     2317        finally:
     2318            dlg.Destroy()
     2319        return filename
     2320
    22502321    # Tools for file writing.
    2251     def OpenFile(self,fil=None):
     2322    def OpenFile(self,fil=None,mode='w'):
    22522323        '''Open the output file
    22532324
    22542325        :param str fil: The name of the file to open. If None (default)
    2255           the name defaults to self.filename.
     2326          the name defaults to self.dirname + self.filename.
     2327          If an extension is supplied, it is not overridded,
     2328          but if not, the default extension is used.
    22562329        :returns: the file object opened by the routine which is also
    22572330          saved as self.fp
    22582331        '''
    22592332        if not fil:
    2260             fil = self.filename
    2261         self.fp = open(fil,'w')
     2333            if not os.path.splitext(self.filename)[1]:
     2334                self.filename += self.extension
     2335            fil = os.path.join(self.dirname,self.filename)
     2336        self.fullpath = fil
     2337        self.fp = open(fil,mode)
    22622338        return self.fp
     2339
    22632340    def Write(self,line):
    22642341        '''write a line of output, attaching a line-end character
Note: See TracChangeset for help on using the changeset viewer.