Changeset 1261 for trunk/exports


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

reorg exports to implement directory selection

Location:
trunk/exports
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • 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.