Changeset 1261


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

reorg exports to implement directory selection

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASII.py

    r1257 r1261  
    18621862        self.GSASprojectfile = ''
    18631863        self.dirname = os.path.expanduser('~')       #start in the users home directory by default; may be meaningless
     1864        self.exportDir = None  # the last directory used for exports, if any.
    18641865        self.undofile = ''
    18651866        self.TreeItemDelete = False
  • 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
  • trunk/exports/G2export_CIF.py

    r1130 r1261  
    7474
    7575#***** define functions for export method =======================================
    76         def openCIF(filnam):
    77             'opens the output file'
    78             if DEBUG:
    79                 self.fp = sys.stdout
    80             else:
    81                 self.fp = open(filnam,'w')
    82 
    83         def closeCIF():
    84             'close the output file'
    85             if not DEBUG:
    86                 self.fp.close()
    87            
    8876        def WriteCIFitem(name,value=''):
    8977            '''Write CIF data items to the file. Formats values as needed.
     
    15661554        # create a dict with refined values and their uncertainties
    15671555        self.loadParmDict()
    1568         if self.ExportSelect(AskFile=(self.mode=='simple')):    # set export parameters
    1569             return
     1556        if self.mode=='simple':
     1557            if self.ExportSelect('ask'): return
     1558        else:
     1559            if self.ExportSelect('default'): return
    15701560        # Someday: get restraint & constraint info
    15711561        #restraintDict = self.OverallParms.get('Restraints',{})
     
    17191709        #======================================================================
    17201710        print('Writing CIF output to file '+str(self.filename)+"...")
    1721         openCIF(self.filename)
     1711        self.OpenFile()
    17221712        if self.currentExportType == 'single' or self.currentExportType == 'powder':
    17231713            #======Data only CIF (powder/xtal) ====================================
     
    19111901
    19121902        WriteCIFitem('#--' + 15*'eof--' + '#')
    1913         closeCIF()
     1903        self.CloseFile()
    19141904        print("...export completed")
     1905        print('file '+str(self.fullpath))
    19151906        # end of CIF export
    19161907
  • trunk/exports/G2export_PDB.py

    r1123 r1261  
    127127        # create a dict with refined values and their uncertainties
    128128        self.loadParmDict()
    129         if self.ExportSelect(AskFile=True):    # set export parameters
    130             return
     129        if self.ExportSelect():    # set export parameters; ask for file name
     130            return
     131        filename = self.filename
    131132        for phasenam in self.phasenam:
    132133            phasedict = self.Phases[phasenam] # pointer to current phase info
    133134            General = phasedict['General']
    134135            if General['Type'] != 'macromolecular':
    135                 print 'not macromolecular phase'
    136                 return
     136                print 'phase '+str(phasenam)+' not macromolecular, skipping'
     137                continue
    137138            i = self.Phases[phasenam]['pId']
    138139            if len(self.phasenam) > 1: # if more than one filename is included, add a phase #
    139                 nam,ext = os.path.splitext(self.filename)
    140                 fil = nam+"_"+str(i)+ext
    141             else:
    142                 fil = self.filename
    143             fp = self.OpenFile(fil)
     140                self.filename = os.path.splitext(filename)[1] + "_" + str(i) + self.extension
     141            fp = self.OpenFile()
    144142            Atoms = phasedict['Atoms']
    145143            cx,ct,cs,cia = General['AtomPtrs']
     
    199197            self.Write('END')
    200198            self.CloseFile()
    201             print('Phase '+str(phasenam)+' written to file '+str(fil))
     199            print('Phase '+str(phasenam)+' written to PDB file '+str(self.fullpath))
    202200
    203201class ExportPhaseCartXYZ(G2IO.ExportBaseclass):
     
    225223        # create a dict with refined values and their uncertainties
    226224        self.loadParmDict()
    227         if self.ExportSelect(AskFile=True):    # set export parameters
    228             return
     225        if self.ExportSelect():    # set export parameters; ask for file name
     226            return
     227        filename = self.filename
    229228        for phasenam in self.phasenam:
    230229            phasedict = self.Phases[phasenam] # pointer to current phase info
    231230            General = phasedict['General']
    232231            i = self.Phases[phasenam]['pId']
    233             if len(self.phasenam) > 1: # if more than one filename is included, add a phase #
    234                 nam,ext = os.path.splitext(self.filename)
    235                 fil = nam+"_"+str(i)+ext
    236             else:
    237                 fil = self.filename
    238             fp = self.OpenFile(fil)
    239232            Atoms = phasedict['Atoms']
    240233            if not len(Atoms):
    241234                print('**** ERROR - Phase '+str(phasenam)+' has no atoms! ****')
    242                 return
     235                continue
     236            if len(self.phasenam) > 1: # if more than one filename is included, add a phase #
     237                self.filename = os.path.splitext(filename)[1] + "_" + str(i) + self.extension
     238            fp = self.OpenFile()
    243239            cx,ct,cs,cia = General['AtomPtrs']
    244240            Cell = General['Cell'][1:7]
     
    251247                self.Write(fmt.format(atom[ct],*xyz))
    252248            self.CloseFile()
    253             print('Phase '+str(phasenam)+' written to file '+str(fil))
     249            print('Phase '+str(phasenam)+' written to XYZ file '+str(self.fullpath))
    254250   
  • trunk/exports/G2export_csv.py

    r1236 r1261  
    6161        # create a dict with refined values and their uncertainties
    6262        self.loadParmDict()
    63         if self.ExportSelect( # set export parameters
    64             AskFile=True     # prompt the user for a file name
    65             ): return
    66         self.OpenFile(self.filename)
     63        if self.ExportSelect(): return # set export parameters; get file name
     64        self.OpenFile()
    6765        # if more than one format is selected, put them into a single file
    6866        for phasenam in self.phasenam:
     
    108106                        line += ","
    109107                self.Write(line)
    110             print('Phase '+str(phasenam)+' written to file '+str(self.filename))                       
     108            print('Phase '+str(phasenam)+' written to file '+str(self.fullpath))
    111109        self.CloseFile()
    112110
     
    135133        self.loadTree()
    136134        if self.ExportSelect( # set export parameters
    137             AskFile=False # use the default file name, which is ignored
     135            AskFile='single' # get a file name/directory to save in
    138136            ): return
    139137        filenamelist = []
    140138        for hist in self.histnam:
    141             fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
    142             self.filename = fileroot + self.extension
     139            if len(self.histnam) > 1:
     140                # multiple files: create a unique name from the histogram
     141                fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
     142                # create an instrument parameter file
     143                self.filename = os.path.join(self.dirname,fileroot + self.extension)
    143144            self.OpenFile()
    144145            histblk = self.Histograms[hist]
     
    158159                self.Write(line)
    159160            self.CloseFile()
    160             print('Histogram '+str(hist)+' written to file '+str(self.filename))
     161            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
    161162
    162163class ExportPowderReflCSV(G2IO.ExportBaseclass):
     
    181182        # load all of the tree into a set of dicts
    182183        self.loadTree()
    183         if self.ExportSelect( # set export parameters
    184             AskFile=False # use the default file name
    185             ): return
     184        if self.ExportSelect(): return  # set export parameters, get file name
    186185        self.OpenFile()
    187186        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
     
    201200                self.Write(fmt.format(h,k,l,pos,Fobs,Fcalc,phase,mult,i))
    202201        self.CloseFile()
    203         print(str(hist)+'reflections written to file '+str(self.filename))
     202        print(str(hist)+'reflections written to file '+str(self.fullpath))
    204203
    205204class ExportSingleCSV(G2IO.ExportBaseclass):
     
    225224        # load all of the tree into a set of dicts
    226225        self.loadTree()
    227         if self.ExportSelect( # set export parameters
    228             AskFile=False # use the default file name
    229             ): return
     226        if self.ExportSelect(): return  # set export parameters, get file name
    230227        self.OpenFile()
    231228        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
     
    238235            self.Write(fmt.format(h,k,l,dsp,Fobs,sigFobs,Fcalc,phase,mult))
    239236        self.CloseFile()
    240         print(str(hist)+' written to file '+str(self.filename))                       
     237        print(str(hist)+' written to file '+str(self.fullname))                       
    241238
    242239class ExportStrainCSV(G2IO.ExportBaseclass):
     
    262259        # load all of the tree into a set of dicts
    263260        self.loadTree()
    264         if self.ExportSelect( # set export parameters
    265             AskFile=True # use the default file name
    266             ): return
     261        if self.ExportSelect(): return  # set export parameters, get file name
    267262        self.OpenFile()
    268263        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
     
    283278                self.Write(fmt2.format(dat[1],dat[0],dat[2]))           
    284279        self.CloseFile()
    285         print(str(hist)+' written to file '+str(self.filename))                       
    286 
     280        print(str(hist)+' written to file '+str(self.fullpath))
  • trunk/exports/G2export_examples.py

    r1236 r1261  
    5757        # create a dict with refined values and their uncertainties
    5858        self.loadParmDict()
    59         if self.ExportSelect( # set export parameters
    60             AskFile=True     # prompt the user for a file name
    61             ): return
    62         self.OpenFile(self.filename)
     59        if self.ExportSelect(): return # set export parameters; prompt for file name
     60        self.OpenFile()
    6361        # if more than one format is selected, put them into a single file
    6462        for phasenam in self.phasenam:
     
    114112                        vals.append(G2mth.ValEsd(val,sig))
    115113                    self.Write(fmt.format(*vals))
    116             print('Phase '+str(phasenam)+' written to file '+str(self.filename))                       
     114            print('Phase '+str(phasenam)+' written to file '+str(self.fullpath))
    117115        self.CloseFile()
    118116
     
    140138        self.loadTree()
    141139        if self.ExportSelect( # set export parameters
    142             AskFile=False # use the default file name
    143             #AskFile=True
     140            AskFile='default' # base name on the GPX file name
    144141            ): return
    145142        self.OpenFile()
     
    162159            self.Write(strg)
    163160        self.CloseFile()
    164         print(str(hist)+' written to file '+str(self.filename))
     161        print(str(hist)+' written to file '+str(self.fullpath))
    165162       
    166163class ExportPowderReflText(G2IO.ExportBaseclass):
     
    186183        self.loadTree()
    187184        if self.ExportSelect( # set export parameters
    188             AskFile=False # use the default file name
    189             #AskFile=True
     185            AskFile='default' # base name on the GPX file name
    190186            ): return
    191187        self.OpenFile()
     
    205201                self.Write(fmt.format(hklfmt.format(h,k,l),pos,Fobs,Fcalc,phase,mult))
    206202        self.CloseFile()
    207         print(str(hist)+'reflections written to file '+str(self.filename))                       
     203        print(str(hist)+'reflections written to file '+str(self.fullpath))                       
    208204
    209205class ExportSingleText(G2IO.ExportBaseclass):
     
    230226        self.loadTree()
    231227        if self.ExportSelect( # set export parameters
    232             AskFile=False # use the default file name
    233             #AskFile=True
     228            AskFile='default' # base name on the GPX file name
    234229            ): return
    235230        self.OpenFile()
     
    247242            self.Write(fmt.format(hklfmt.format(h,k,l),dsp,Fobs,sigFobs,Fcalc,phase,mult))
    248243        self.CloseFile()
    249         print(str(hist)+' written to file '+str(self.filename))                       
    250 
     244        print(str(hist)+' written to file '+str(self.fullpath))                       
     245
  • trunk/exports/G2export_image.py

    r1191 r1261  
    4343        # load all of the tree into a set of dicts
    4444        self.loadTree()
    45         if self.ExportSelect(False): return
     45        if self.ExportSelect(): return # select one image; ask for a file name
    4646        # process the selected image(s) (at present only one image)
    4747        for i in sorted(self.histnam):
     48            filename = os.path.join(
     49                self.dirname,
     50                os.path.splitext(self.filename)[0] + self.extension
     51                )
    4852            imgFile = self.Histograms[i].get('Data',(None,None))
    4953            Comments,Data,Npix,Image = G2IO.GetImageData(self.G2frame,imgFile)
    50             scipy.misc.imsave(self.filename,Image)
    51             print('Image '+str(imgFile)+' written to file '+str(self.filename))                   
     54            scipy.misc.imsave(filename,Image)
     55            print('Image '+str(imgFile)+' written to file '+str(filename))
     56           
  • trunk/exports/G2export_map.py

    r1123 r1261  
    5353        # load all of the tree into a set of dicts
    5454        self.loadTree()
    55         if self.ExportSelect( # set export parameters
    56             AskFile=True          # prompt the user for a file name
    57             ): return
     55        if self.ExportSelect(): return  # set export parameters, get file name
     56        filename = self.filename
    5857        for phasenam in self.phasenam:
    5958            phasedict = self.Phases[phasenam] # pointer to current phase info           
    6059            rho = phasedict['General']['Map'].get('rho',[])
    6160            if not len(rho):
    62                 return
    63             print self.filename
    64             self.OpenFile(self.filename)
     61                print "There is no map for phase "+str(phasenam)
     62                continue
     63            if len(self.phasenam) > 1: # if more than one filename is written, add a phase # -- not in use yet
     64                i = self.Phases[phasenam]['pId']
     65                self.filename = os.path.splitext(filename)[1] + "_" + mapData['MapType'] + str(i) + self.extension
     66            self.OpenFile()
    6567            self.Write("Map of Phase "+str(phasenam)+" from "+str(self.G2frame.GSASprojectfile))
    6668            # get cell parameters & print them
     
    7476                    for k in range(nz):
    7577                        self.Write(str(rho[i,j,k]))
    76             print('map from Phase '+str(phasenam)+' written to file '+str(self.filename))
    7778            self.CloseFile()
    78             return
     79            print('map from Phase '+str(phasenam)+' written to file '+str(self.fullpath))
    7980
    8081class ExportMapCCP4(G2IO.ExportBaseclass):
     
    9495
    9596    # Tools for file writing.
    96     def OpenFile(self,fil=None):
    97         '''Open the output file as binary
    98 
    99         :param str fil: The name of the file to open. If None (default)
    100           the name defaults to self.filename.
    101         :returns: the file object opened by the routine which is also
    102           saved as self.fp
    103         '''
    104         if not fil:
    105             fil = self.filename
    106         self.fp = open(fil,'wb')
    107         return self.fp
    108        
    10997    def Write(self,data,dtype):
    11098        import struct
     
    122110        # load all of the tree into a set of dicts
    123111        self.loadTree()
    124         if self.ExportSelect( # set export parameters
    125             AskFile=False     # I'll make the file name
    126             ): return
     112        if self.ExportSelect(): return  # set export parameters, get file name
     113        filename = self.filename
    127114        for phasenam in self.phasenam:
    128115            phasedict = self.Phases[phasenam] # pointer to current phase info
     
    131118           
    132119            if not len(rho):
    133                 return
     120                print "There is no map for phase "+str(phasenam)
     121                continue
     122            if len(self.phasenam) > 1: # if more than one filename is written, add a phase # -- not in use yet
     123                i = self.Phases[phasenam]['pId']
     124                self.filename = os.path.splitext(filename)[1] + "_" + mapData['MapType'] + str(i) + self.extension
    134125            cell = phasedict['General']['Cell'][1:7]
    135126            nx,ny,nz = rho.shape
    136             filename,ext = os.path.splitext(self.filename)
    137             self.filename = filename+'_'+phasenam+'_'+mapData['MapType']+ext
    138             self.OpenFile(self.filename)
     127            self.OpenFile(mode='wb')
    139128            for n in rho.shape: self.Write(n,'i')  #nX,nY,nZ
    140129            self.Write(2,'i')           #mode=2 float map
     
    156145            for x in rho.flatten('F'):
    157146                self.Write(x,'f')
    158             print('map from Phase '+str(phasenam)+' written to file '+str(self.filename))
    159147            self.CloseFile()
    160             return
     148            print('map from Phase '+str(phasenam)+' written to file '+str(self.fullpath))
  • trunk/exports/G2export_pwdr.py

    r1236 r1261  
    3838        self.multiple = True
    3939
     40    def WriteInstFile(self,hist,Inst):
     41        '''Write an instrument parameter file
     42        '''
     43        prmname = os.path.splitext(self.filename)[0] + '.prm'
     44        prmname = os.path.join(self.dirname,prmname)
     45        self.OpenFile(prmname)
     46        self.Write( '            123456789012345678901234567890123456789012345678901234567890        ')
     47        self.Write( 'INS   BANK      1                                                               ')
     48        self.Write(('INS   HTYPE   %sR                                                              ')%(Inst['Type'][0]))
     49        if 'Lam1' in Inst:              #Ka1 & Ka2
     50            self.Write(('INS  1 ICONS%10.7f%10.7f    0.0000               0.990    0     0.500   ')%(Inst['Lam1'][0],Inst['Lam2'][0]))
     51        elif 'Lam' in Inst:             #single wavelength
     52            self.Write(('INS  1 ICONS%10.7f%10.7f    0.0000               0.990    0     0.500   ')%(Inst['Lam'][1],0.0))
     53        self.Write( 'INS  1 IRAD     0                                                               ')
     54        self.Write( 'INS  1I HEAD                                                                    ')
     55        self.Write( 'INS  1I ITYP    0    0.0000  180.0000         1                                 ')
     56        self.Write(('INS  1DETAZM%10.3f                                                          ')%(Inst['Azimuth'][0]))
     57        self.Write( 'INS  1PRCF1     3    8   0.00100                                                ')
     58        self.Write(('INS  1PRCF11     %15.6g%15.6g%15.6g%15.6g   ')%(Inst['U'][1],Inst['V'][1],Inst['W'][1],0.0))
     59        self.Write(('INS  1PRCF12     %15.6g%15.6g%15.6g%15.6g   ')%(Inst['X'][1],Inst['Y'][1],Inst['SH/L'][1]/2.,Inst['SH/L'][1]/2.))
     60        self.CloseFile()
     61        print('Parameters from '+str(hist)+' written to file '+str(prmname))
     62        return prmname
     63
    4064    def Exporter(self,event=None):
    4165        '''Export one or more sets of powder data as FXYE file(s)
     
    4670        self.loadTree()
    4771        if self.ExportSelect( # set export parameters
    48             AskFile=False # use the default file name, which is ignored
     72            AskFile='single' # get a file name/directory to save in
    4973            ): return
    5074        filenamelist = []
    5175        for hist in self.histnam:
    52             fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
    53             # create an instrument parameter file
    54             self.filename = fileroot + '.prm'
     76            if len(self.histnam) > 1:
     77                # multiple files: create a unique name from the histogram
     78                fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
     79                # create an instrument parameter file
     80                self.filename = os.path.join(self.dirname,fileroot + self.extension)
     81            else:
     82                # use the supplied name, but force the extension
     83                self.filename= os.path.splitext(self.filename)[0] + self.extension
     84               
     85            histblk = self.Histograms[hist]
     86            prmname = self.WriteInstFile(hist,histblk['Instrument Parameters'][0])
     87           
    5588            self.OpenFile()
    56             histblk = self.Histograms[hist]
    57             Inst = histblk['Instrument Parameters'][0]
    58             self.Write( '            123456789012345678901234567890123456789012345678901234567890        ')
    59             self.Write( 'INS   BANK      1                                                               ')
    60             self.Write(('INS   HTYPE   %sR                                                              ')%(Inst['Type'][0]))
    61             if 'Lam1' in Inst:              #Ka1 & Ka2
    62                 self.Write(('INS  1 ICONS%10.7f%10.7f    0.0000               0.990    0     0.500   ')%(Inst['Lam1'][0],Inst['Lam2'][0]))
    63             elif 'Lam' in Inst:             #single wavelength
    64                 self.Write(('INS  1 ICONS%10.7f%10.7f    0.0000               0.990    0     0.500   ')%(Inst['Lam'][1],0.0))
    65             self.Write( 'INS  1 IRAD     0                                                               ')
    66             self.Write( 'INS  1I HEAD                                                                    ')
    67             self.Write( 'INS  1I ITYP    0    0.0000  180.0000         1                                 ')
    68             self.Write(('INS  1DETAZM%10.3f                                                          ')%(Inst['Azimuth'][0]))
    69             self.Write( 'INS  1PRCF1     3    8   0.00100                                                ')
    70             self.Write(('INS  1PRCF11     %15.6g%15.6g%15.6g%15.6g   ')%(Inst['U'][1],Inst['V'][1],Inst['W'][1],0.0))
    71             self.Write(('INS  1PRCF12     %15.6g%15.6g%15.6g%15.6g   ')%(Inst['X'][1],Inst['Y'][1],Inst['SH/L'][1]/2.,Inst['SH/L'][1]/2.))
    72             self.CloseFile()
    73             print('Parameters from '+str(hist)+' written to file '+str(self.filename))
    74             prmname = self.filename
    75            
    76             self.filename = fileroot + self.extension
    77             self.OpenFile()
    78             histblk = self.Histograms[hist]
    7989            self.Write(hist[5:])
    8090            self.Write('Instrument parameter file:'+os.path.split(prmname)[1])
     
    95105                self.Write(line)
    96106            self.CloseFile()
    97             print('Histogram '+str(hist)+' written to file '+str(self.filename))
     107            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
    98108
    99109
     
    121131        self.loadTree()
    122132        if self.ExportSelect( # set export parameters
    123             AskFile=False # use the default file name, which is ignored
     133            AskFile='single' # get a file name/directory to save in
    124134            ): return
    125135        filenamelist = []
    126136        for hist in self.histnam:
    127             fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
    128            
    129             self.filename = fileroot + self.extension
     137            if len(self.histnam) > 1:
     138                # multiple files: create a unique name from the histogram
     139                fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
     140                # create an instrument parameter file
     141                self.filename = os.path.join(self.dirname,fileroot + self.extension)
     142            else:
     143                # use the supplied name, but force the extension
     144                self.filename= os.path.splitext(self.filename)[0] + self.extension
     145
    130146            self.OpenFile()
    131147            histblk = self.Histograms[hist]
     
    142158                self.Write(line)
    143159            self.CloseFile()
    144             print('Histogram '+str(hist)+' written to file '+str(self.filename))
     160            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
  • trunk/exports/G2export_shelx.py

    r1123 r1261  
    4949        # create a dict with refined values and their uncertainties
    5050        self.loadParmDict()
    51         if self.ExportSelect(  # set export parameters
    52             AskFile=True): return
     51        if self.ExportSelect(): return  # set export parameters;
     52        filename = self.filename
    5353        for phasenam in self.phasenam:
    5454            phasedict = self.Phases[phasenam] # pointer to current phase info           
    5555            i = self.Phases[phasenam]['pId']
    5656            if len(self.phasenam) > 1: # if more than one filename is included, add a phase #
    57                 nam,ext = os.path.splitext(self.filename)
    58                 fil = nam+"_"+str(i)+ext
    59             else:
    60                 fil = self.filename
    61             fp = self.OpenFile(fil)
     57                self.filename = os.path.splitext(filename)[1] + "_" + str(i) + self.extension
     58            fp = self.OpenFile()
    6259            # title line
    6360            self.Write("TITL from "+str(self.G2frame.GSASprojectfile)+", phase "+str(phasenam))
     
    124121            self.Write('END')
    125122            self.CloseFile()
    126             print('Phase '+str(phasenam)+' written to file '+str(fil))
     123            print('Phase '+str(phasenam)+' written to file '+str(self.fullpath))
Note: See TracChangeset for help on using the changeset viewer.