Changeset 1997 for trunk/exports


Ignore:
Timestamp:
Oct 10, 2015 11:58:12 AM (6 years ago)
Author:
toby
Message:

Add API for direct image read (G2IO.ExportPowderList?) and powder exports w/o GUI (G2IO.ExportPowderList?); redo export to add new method (Writer)

Location:
trunk/exports
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/exports/G2export_csv.py

    r1836 r1997  
    126126        self.multiple = True
    127127
     128    def Writer(self,TreeName,filename=None):
     129        self.OpenFile(filename)
     130        histblk = self.Histograms[TreeName]
     131        WriteList(self,("x","y_obs","weight","y_calc","y_bkg"))
     132        digitList = 2*((13,3),) + ((13,5),) + 2*((13,3),)
     133        for vallist in zip(histblk['Data'][0],
     134                       histblk['Data'][1],
     135                       histblk['Data'][2],
     136                       histblk['Data'][3],
     137                       histblk['Data'][4],
     138                       #histblk['Data'][5],
     139                       ):
     140            line = ""
     141            for val,digits in zip(vallist,digitList):
     142                if line: line += ','
     143                line += G2py3.FormatValue(val,digits)
     144            self.Write(line)
     145        self.CloseFile()
     146       
    128147    def Exporter(self,event=None):
    129148        '''Export a set of powder data as a csv file
     
    138157        filenamelist = []
    139158        for hist in self.histnam:
    140             if len(self.histnam) > 1:
    141                 # multiple files: create a unique name from the histogram
    142                 fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
    143                 # create an instrument parameter file
    144                 self.filename = os.path.join(self.dirname,fileroot + self.extension)
    145             self.OpenFile()
    146             histblk = self.Histograms[hist]
    147             WriteList(self,("x","y_obs","weight","y_calc","y_bkg"))
    148             digitList = 2*((13,3),) + ((13,5),) + 2*((13,3),)
    149             for vallist in zip(histblk['Data'][0],
    150                            histblk['Data'][1],
    151                            histblk['Data'][2],
    152                            histblk['Data'][3],
    153                            histblk['Data'][4],
    154                            #histblk['Data'][5],
    155                            ):
    156                 line = ""
    157                 for val,digits in zip(vallist,digitList):
    158                     if line: line += ','
    159                     line += G2py3.FormatValue(val,digits)
    160                 self.Write(line)
    161             self.CloseFile()
     159            #if len(self.histnam) > 1:
     160            #    # multiple files: create a unique name from the histogram
     161            #    fileroot = G2obj.MakeUniqueLabel(self.MakePWDRfilename(hist),filenamelist)
     162            #    # create an instrument parameter file
     163            #    self.filename = os.path.join(self.dirname,fileroot + self.extension)
     164            self.Writer(hist)
    162165            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
    163166
  • trunk/exports/G2export_examples.py

    r1902 r1997  
    132132        self.multiple = False # only allow one histogram to be selected
    133133
    134     def Exporter(self,event=None):
    135         '''Export a set of powder data as a text file
    136         '''
    137         # the export process starts here
    138         self.InitExport(event)
    139         # load all of the tree into a set of dicts
    140         self.loadTree()
    141         if self.ExportSelect( # set export parameters
    142             AskFile='default' # base name on the GPX file name
    143             ): return
    144         self.OpenFile()
    145         hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
    146         histblk = self.Histograms[hist]
     134    def Writer(self,TreeName,filename=None):
     135        self.OpenFile(filename)
     136        histblk = self.Histograms[TreeName]
    147137        hfmt = 5*"{:12s} "
    148138        digitList = 2*((13,3),) + ((13,5),) + 2*((13,3),)
     
    161151            self.Write(strg)
    162152        self.CloseFile()
     153       
     154    def Exporter(self,event=None):
     155        '''Export a set of powder data as a text file
     156        '''
     157        # the export process starts here
     158        self.InitExport(event)
     159        # load all of the tree into a set of dicts
     160        self.loadTree()
     161        if self.ExportSelect( # set export parameters
     162            AskFile='default' # base name on the GPX file name
     163            ): return
     164        hist = self.histnam[0] # there should only be one histogram, in any case take the 1st
     165        self.Writer(hist)
    163166        print(str(hist)+' written to file '+str(self.fullpath))
    164167       
  • trunk/exports/G2export_pwdr.py

    r1675 r1997  
    6262        return prmname
    6363
     64    def Writer(self,TreeName,filename=None,prmname=''):
     65        '''Write a single PWDR entry to a FXYE file
     66        '''
     67        histblk = self.Histograms[TreeName]
     68        self.OpenFile(filename) # ***rethink
     69        self.Write(TreeName[5:])
     70        if prmname: self.Write('Instrument parameter file:'+os.path.split(prmname)[1])
     71        x = 100*np.array(histblk['Data'][0])
     72        # convert weights to sigmas; use largest weight as minimum esd
     73        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
     74        s[s==0] = np.max(s)
     75        s = 1./s
     76        self.Write('BANK 1 %d %d CONS %.2f %.2f 0 0 FXYE' % (
     77            len(x),len(x),x[0],(x[1]-x[0])
     78            ))
     79#            for X,Y,S in zip(x,histblk['Data'][1],s):
     80#                self.Write("{:15.6g} {:15.6g} {:15.6g}".format(X,Y,S))
     81        for XYS in zip(x,histblk['Data'][1],s):
     82            line = ''
     83            for val in XYS:
     84                line += G2py3.FormatPadValue(val,(15,6))
     85            self.Write(line)
     86        self.CloseFile()
     87       
    6488    def Exporter(self,event=None):
    6589        '''Export one or more sets of powder data as FXYE file(s)
     
    6791        # the export process starts here
    6892        self.InitExport(event)
    69         # load all of the tree into a set of dicts
    70         self.loadTree()
     93        self.loadTree() # load all of the tree into a set of dicts
    7194        if self.ExportSelect( # set export parameters
    7295            AskFile='single' # get a file name/directory to save in
     
    82105                # use the supplied name, but force the extension
    83106                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            
    88             self.OpenFile()
    89             self.Write(hist[5:])
    90             self.Write('Instrument parameter file:'+os.path.split(prmname)[1])
    91             x = 100*np.array(histblk['Data'][0])
    92             # convert weights to sigmas; use largest weight as minimum esd
    93             s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
    94             s[s==0] = np.max(s)
    95             s = 1./s
    96             self.Write('BANK 1 %d %d CONS %.2f %.2f 0 0 FXYE' % (
    97                 len(x),len(x),x[0],(x[1]-x[0])
    98                 ))
    99 #            for X,Y,S in zip(x,histblk['Data'][1],s):
    100 #                self.Write("{:15.6g} {:15.6g} {:15.6g}".format(X,Y,S))
    101             for XYS in zip(x,histblk['Data'][1],s):
    102                 line = ''
    103                 for val in XYS:
    104                     line += G2py3.FormatPadValue(val,(15,6))
    105                 self.Write(line)
    106             self.CloseFile()
     107
     108            prmname = self.WriteInstFile(TreeName,histblk['Instrument Parameters'][0])
     109            self.Writer(hist,prmname=prmname)
    107110            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
    108111
     
    122125        self.exporttype = ['powder']
    123126        self.multiple = True
     127       
     128    def Writer(self,TreeName,filename=None):
     129        GSASIIpath.IPyBreak()
     130
     131        self.OpenFile()
     132        histblk = self.Histograms[TreeName]
     133        self.Write('/*')    #The ugly c comment delimiter used in topas!
     134        self.Write('# '+TreeName[5:])  #evidently this by itself fails in topas
     135        self.Write('*/')
     136        x = np.array(histblk['Data'][0])
     137        # convert weights to sigmas; use largest weight as minimum esd
     138        s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
     139        s[s==0] = np.max(s)
     140        s = 1./s
     141        for XYS in zip(x,histblk['Data'][1],s):
     142            line = ''
     143            for val in XYS:
     144                line += G2py3.FormatPadValue(val,(15,6))
     145            self.Write(line)
     146        self.CloseFile()
    124147
    125148    def Exporter(self,event=None):
     
    144167                self.filename= os.path.splitext(self.filename)[0] + self.extension
    145168
    146             self.OpenFile()
    147             histblk = self.Histograms[hist]
    148             self.Write('/*')    #The ugly c comment delimiter used in topas!
    149             self.Write('# '+hist[5:])  #evidently this by itself fails in topas
    150             self.Write('*/')
    151             x = np.array(histblk['Data'][0])
    152             # convert weights to sigmas; use largest weight as minimum esd
    153             s = np.sqrt(np.maximum(0.,np.array(histblk['Data'][2])))
    154             s[s==0] = np.max(s)
    155             s = 1./s
    156             for XYS in zip(x,histblk['Data'][1],s):
    157                 line = ''
    158                 for val in XYS:
    159                     line += G2py3.FormatPadValue(val,(15,6))
    160                 self.Write(line)
    161             self.CloseFile()
     169            self.Writer(hist)
    162170            print('Histogram '+str(hist)+' written to file '+str(self.fullpath))
Note: See TracChangeset for help on using the changeset viewer.