Ignore:
Timestamp:
Aug 19, 2020 8:14:36 AM (3 years ago)
Author:
vondreele
Message:

Add csv exporter for reflectometry data

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/exports/G2export_csv.py

    r4522 r4548  
    465465            print('Histogram '+hist+' written to file '+self.fullpath)
    466466
     467class ExportREFDCSV(G2IO.ExportBaseclass):
     468    '''Used to create a csv file for a reflectometry data set
     469
     470    :param wx.Frame G2frame: reference to main GSAS-II frame
     471    '''
     472    def __init__(self,G2frame):
     473        super(self.__class__,self).__init__( # fancy way to say <parentclass>.__init__
     474            G2frame=G2frame,
     475            formatName = 'CSV file',
     476            extension='.csv',
     477            longFormatName = 'Export reflectometry data as comma-separated (csv) file'
     478            )
     479        self.exporttype = ['refd']
     480        #self.multiple = False # only allow one histogram to be selected
     481        self.multiple = True
     482
     483    def Writer(self,TreeName,filename=None):
     484        self.OpenFile(filename)
     485        histblk = self.Histograms[TreeName]
     486        # if len(self.Histograms[TreeName]['Models']['Size']['Distribution']):
     487        #     self.Write('"Size Distribution"')
     488        #     Distr = np.array(self.Histograms[TreeName]['Models']['Size']['Distribution'])
     489        #     WriteList(self,("bin_pos","bin_width","bin_value"))
     490        #     digitList = 2*((13,3),)+((13,4,'g'),)
     491        #     for bindata in Distr.T:
     492        #         line = ""
     493        #         for val,digits in zip(bindata,digitList):
     494        #             if line: line += ','
     495        #             line += G2py3.FormatValue(val,digits)
     496        #         self.Write(line)           
     497        self.Write('"Reflectometry data"')
     498        Parms = self.Histograms[TreeName]['Instrument Parameters'][0]
     499        for parm in Parms:
     500            if parm in ['Type','Source',]:
     501                line = '"Instparm: %s","%s"'%(parm,Parms[parm][0])
     502            elif parm in ['Lam',]:
     503                line = '"Instparm: %s",%10.6f'%(parm,Parms[parm][1])
     504            else:
     505                line = '"Instparm: %s",%10.2f'%(parm,Parms[parm][1])
     506            self.Write(line)
     507        WriteList(self,("q","y_obs","y_sig","y_calc","y_bkg"))
     508        digitList = 5*((13,5,'g'),)
     509        for vallist in zip(histblk['Data'][0],
     510                       histblk['Data'][1],
     511                       1./np.sqrt(histblk['Data'][2]),
     512                       histblk['Data'][3],
     513                       histblk['Data'][4],
     514                       ):
     515            line = ""
     516            for val,digits in zip(vallist,digitList):
     517                if line: line += ','
     518                line += G2py3.FormatValue(val,digits)
     519            self.Write(line)
     520        self.CloseFile()
     521       
     522    def Exporter(self,event=None):
     523        '''Export a set of reflectometry data as a csv file
     524        '''
     525        # the export process starts here
     526        self.InitExport(event)
     527        # load all of the tree into a set of dicts
     528        self.loadTree()
     529        if self.ExportSelect( # set export parameters
     530            AskFile='single' # get a file name/directory to save in
     531            ): return
     532        filenamelist = []
     533        for hist in self.histnam:
     534            if len(self.histnam) == 1:
     535                name = self.filename
     536            else:    # multiple files: create a unique name from the histogram
     537                name = self.MakePWDRfilename(hist)
     538            fileroot = os.path.splitext(G2obj.MakeUniqueLabel(name,filenamelist))[0]
     539            # create the file
     540            self.filename = os.path.join(self.dirname,fileroot + self.extension)
     541            self.Writer(hist)
     542            print('Histogram '+hist+' written to file '+self.fullpath)
     543
    467544class ExportSingleCSV(G2IO.ExportBaseclass):
    468545    '''Used to create a csv file with single crystal reflection data
Note: See TracChangeset for help on using the changeset viewer.