Changeset 1997


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
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASII.py

    r1985 r1997  
    180180            help='',id=wx.ID_ANY,
    181181            kind=wx.ITEM_NORMAL,
    182             text='Add phase')
     182            text='Add new phase')
    183183        self.Bind(wx.EVT_MENU, self.OnAddPhase, id=item.GetId())
    184184        item = parent.Append(
     
    19021902                filelist.append(filename)   
    19031903        filelist = sorted(list(set(filelist))) # remove duplicates
    1904         exporterlist = []
     1904        self.exporterlist = []
    19051905        # go through the routines and import them, saving objects that
    19061906        # have export routines (method Exporter)
     
    19211921                        else:
    19221922                            exporter = clss[1](self) # create an export instance
    1923                             exporterlist.append(exporter)
     1923                            self.exporterlist.append(exporter)
    19241924            except AttributeError:
    19251925                print 'Import_'+errprefix+': Attribute Error'+str(filename)
     
    19301930            if fp: fp.close()
    19311931        # Add submenu item(s) for each Exporter by its self-declared type (can be more than one)
    1932         for obj in exporterlist:
     1932        for obj in self.exporterlist:
    19331933            #print 'exporter',obj
    19341934            for typ in obj.exporttype:
     
    23752375                imagefiles.sort()
    23762376                for imagefile in imagefiles:
    2377                     # if a zip file, open and extract
    2378                     if os.path.splitext(imagefile)[1].lower() == '.zip':
    2379                         extractedfile = G2IO.ExtractFileFromZip(imagefile,parent=self)
    2380                         if extractedfile is not None and extractedfile != imagefile:
    2381                             imagefile = extractedfile
    2382                     Comments,Data,Npix,Image = G2IO.GetImageData(self,imagefile)
    2383                     if Comments:
    2384                         Id = self.PatternTree.AppendItem(parent=self.root,text='IMG '+os.path.basename(imagefile))
    2385                         self.PatternTree.SetItemPyData(self.PatternTree.AppendItem(Id,text='Comments'),Comments)
    2386                         Imax = np.amax(Image)
    2387                         Imin = max(0.0,np.amin(Image))          #force positive
    2388                         if self.imageDefault:
    2389                             Data = copy.copy(self.imageDefault)
    2390                             Data['showLines'] = True
    2391                             Data['ring'] = []
    2392                             Data['rings'] = []
    2393                             Data['cutoff'] = 10
    2394                             Data['pixLimit'] = 20
    2395                             Data['edgemin'] = 100000000
    2396                             Data['calibdmin'] = 0.5
    2397                             Data['calibskip'] = 0
    2398                             Data['ellipses'] = []
    2399                             Data['calibrant'] = ''
    2400                             Data['GonioAngles'] = [0.,0.,0.]
    2401                             Data['DetDepthRef'] = False
    2402                         else:
    2403                             Data['type'] = 'PWDR'
    2404                             Data['color'] = 'Paired'
    2405                             Data['tilt'] = 0.0
    2406                             Data['rotation'] = 0.0
    2407                             Data['showLines'] = False
    2408                             Data['ring'] = []
    2409                             Data['rings'] = []
    2410                             Data['cutoff'] = 10
    2411                             Data['pixLimit'] = 20
    2412                             Data['calibdmin'] = 0.5
    2413                             Data['calibskip'] = 0
    2414                             Data['edgemin'] = 100000000
    2415                             Data['ellipses'] = []
    2416                             Data['GonioAngles'] = [0.,0.,0.]
    2417                             Data['DetDepth'] = 0.
    2418                             Data['DetDepthRef'] = False
    2419                             Data['calibrant'] = ''
    2420                             Data['IOtth'] = [2.0,5.0]
    2421                             Data['LRazimuth'] = [135,225]
    2422                             Data['azmthOff'] = 0.0
    2423                             Data['outChannels'] = 2500
    2424                             Data['outAzimuths'] = 1
    2425                             Data['centerAzm'] = False
    2426                             Data['fullIntegrate'] = False
    2427                             Data['setRings'] = False
    2428                             Data['background image'] = ['',-1.0]                           
    2429                             Data['dark image'] = ['',-1.0]
    2430                             Data['Flat Bkg'] = 0.0
    2431                         Data['setDefault'] = False
    2432                         Data['range'] = [(Imin,Imax),[Imin,Imax]]
    2433                         self.PatternTree.SetItemPyData(self.PatternTree.AppendItem(Id,text='Image Controls'),Data)
    2434                         Masks = {'Points':[],'Rings':[],'Arcs':[],'Polygons':[],'Frames':[],'Thresholds':[(Imin,Imax),[Imin,Imax]]}
    2435                         self.PatternTree.SetItemPyData(self.PatternTree.AppendItem(Id,text='Masks'),Masks)
    2436                         self.PatternTree.SetItemPyData(self.PatternTree.AppendItem(Id,text='Stress/Strain'),
    2437                             {'Type':'True','d-zero':[],'Sample phi':0.0,'Sample z':0.0,'Sample load':0.0})
    2438                         self.PatternTree.SetItemPyData(Id,[Npix,imagefile])
    2439                         self.PickId = Id
    2440                         self.PickIdText = self.GetTreeItemsList(self.PickId)
    2441                         self.Image = Id
    2442                 os.chdir(dlg.GetDirectory())           # to get Mac/Linux to change directory!               
    2443                 self.PatternTree.SelectItem(G2gd.GetPatternTreeItemId(self,Id,'Image Controls'))             #show last one
     2377                    G2IO.ReadLoadImage(imagefile,self)
     2378                os.chdir(dlg.GetDirectory())           # to get Mac/Linux to change directory!
     2379                self.PatternTree.SelectItem(G2gd.GetPatternTreeItemId(self,self.Image,'Image Controls'))             #show last image to be read
    24442380        finally:
    24452381            path = dlg.GetDirectory()           # to get Mac/Linux to change directory!
  • trunk/GSASIIIO.py

    r1962 r1997  
    3333import GSASIIgrid as G2gd
    3434import GSASIIspc as G2spc
     35import GSASIIobj as G2obj
    3536import GSASIIlattice as G2lat
    3637import GSASIIpwdGUI as G2pdG
     
    265266    dlg.CenterOnParent()
    266267    dlg.ShowModal()
    267 
     268   
     269def ReadLoadImage(imagefile,G2frame):
     270    '''Read a GSAS-II image file and load it into the data tree
     271    '''
     272    # get a list of existing Image entries
     273    ImgNames = []
     274    if G2frame.PatternTree.GetCount():
     275        item, cookie = G2frame.PatternTree.GetFirstChild(G2frame.root)
     276        while item:
     277            name = G2frame.PatternTree.GetItemText(item)
     278            if name.startswith('IMG'): ImgNames.append(name)       
     279            item, cookie = G2frame.PatternTree.GetNextChild(G2frame.root, cookie)
     280    # if a zip file, open and extract
     281    if os.path.splitext(imagefile)[1].lower() == '.zip':
     282        extractedfile = ExtractFileFromZip(imagefile,parent=G2frame)
     283        if extractedfile is not None and extractedfile != imagefile:
     284            imagefile = extractedfile
     285    Comments,Data,Npix,Image = GetImageData(G2frame,imagefile)
     286    if Comments:
     287        TreeName = G2obj.MakeUniqueLabel('IMG '+os.path.basename(imagefile),ImgNames)
     288        print 'b=','IMG '+os.path.basename(imagefile)
     289        print 'a=',TreeName
     290        Id = G2frame.PatternTree.AppendItem(parent=G2frame.root,text=TreeName)
     291        G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Comments'),Comments)
     292        Imax = np.amax(Image)
     293        Imin = max(0.0,np.amin(Image))          #force positive
     294        if G2frame.imageDefault:
     295            Data = copy.copy(G2frame.imageDefault)
     296            Data['showLines'] = True
     297            Data['ring'] = []
     298            Data['rings'] = []
     299            Data['cutoff'] = 10
     300            Data['pixLimit'] = 20
     301            Data['edgemin'] = 100000000
     302            Data['calibdmin'] = 0.5
     303            Data['calibskip'] = 0
     304            Data['ellipses'] = []
     305            Data['calibrant'] = ''
     306            Data['GonioAngles'] = [0.,0.,0.]
     307            Data['DetDepthRef'] = False
     308        else:
     309            Data['type'] = 'PWDR'
     310            Data['color'] = 'Paired'
     311            Data['tilt'] = 0.0
     312            Data['rotation'] = 0.0
     313            Data['showLines'] = False
     314            Data['ring'] = []
     315            Data['rings'] = []
     316            Data['cutoff'] = 10
     317            Data['pixLimit'] = 20
     318            Data['calibdmin'] = 0.5
     319            Data['calibskip'] = 0
     320            Data['edgemin'] = 100000000
     321            Data['ellipses'] = []
     322            Data['GonioAngles'] = [0.,0.,0.]
     323            Data['DetDepth'] = 0.
     324            Data['DetDepthRef'] = False
     325            Data['calibrant'] = ''
     326            Data['IOtth'] = [2.0,5.0]
     327            Data['LRazimuth'] = [135,225]
     328            Data['azmthOff'] = 0.0
     329            Data['outChannels'] = 2500
     330            Data['outAzimuths'] = 1
     331            Data['centerAzm'] = False
     332            Data['fullIntegrate'] = False
     333            Data['setRings'] = False
     334            Data['background image'] = ['',-1.0]                           
     335            Data['dark image'] = ['',-1.0]
     336            Data['Flat Bkg'] = 0.0
     337        Data['setDefault'] = False
     338        Data['range'] = [(Imin,Imax),[Imin,Imax]]
     339        G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Image Controls'),Data)
     340        Masks = {'Points':[],'Rings':[],'Arcs':[],'Polygons':[],'Frames':[],'Thresholds':[(Imin,Imax),[Imin,Imax]]}
     341        G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Masks'),Masks)
     342        G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Stress/Strain'),
     343            {'Type':'True','d-zero':[],'Sample phi':0.0,'Sample z':0.0,'Sample load':0.0})
     344        G2frame.PatternTree.SetItemPyData(Id,[Npix,imagefile])
     345        G2frame.PickId = Id
     346        G2frame.PickIdText = G2frame.GetTreeItemsList(G2frame.PickId)
     347        G2frame.Image = Id
     348   
    268349def GetImageData(G2frame,imagefile,imageOnly=False):
    269350    '''Read an image with the file reader keyed by the
     
    18791960    appropriate menu items for each one and the .Exporter method is called
    18801961    directly from the menu item.
    1881    
     1962
     1963    Routines may also define a .Writer method, which is used to write a single
     1964    file without invoking any GUI objects.
    18821965    '''
    18831966    def __init__(self,
     
    23682451                self.filename += self.extension
    23692452            fil = os.path.join(self.dirname,self.filename)
    2370         self.fullpath = fil
     2453        self.fullpath = os.path.abspath(fil)
    23712454        self.fp = open(fil,mode)
    23722455        return self.fp
     
    24632546        return atomslist
    24642547######################################################################
     2548def ExportPowderList(G2frame):
     2549    '''Returns a list of extensions supported by :func:`GSASIIIO:ExportPowder`
     2550   
     2551    :param wx.Frame G2frame: the GSAS-II main data tree window
     2552    '''
     2553    extList = []
     2554    for obj in G2frame.exporterlist:
     2555        if 'powder' in obj.exporttype:
     2556            try:
     2557                obj.Writer
     2558                extList.append(obj.extension)
     2559            except AttributeError:
     2560                pass
     2561    return extList
     2562
     2563def ExportPowder(G2frame,TreeName,fileroot,extension):
     2564    '''Writes a single powder histogram using the Export routines
     2565
     2566    :param wx.Frame G2frame: the GSAS-II main data tree window
     2567    :param str TreeName: the name of the histogram (PWDR ...) in the data tree
     2568    :param str fileroot: name for file to be written, extension ignored
     2569    :param str extension: extension for file to be written (start with '.'). Must
     2570      match a powder export routine that has a Writer object.
     2571    '''
     2572    filename = os.path.abspath(os.path.splitext(fileroot)[1]+extension)
     2573    for obj in G2frame.exporterlist:
     2574        if obj.extension == extension and 'powder' in obj.exporttype:
     2575            obj.currentExportType = 'powder'
     2576            obj.InitExport(None)
     2577            obj.loadTree() # load all histograms in tree into dicts
     2578            if TreeName not in obj.Histograms:
     2579                raise Exception('Histogram not found: '+hst)
     2580            try:
     2581                obj.Writer(TreeName,filename)
     2582                return
     2583            except AttributeError:
     2584                print('Export Routine for '+extension+' does not have a .Writer method')
     2585    else:
     2586        print('No Export routine supports extension '+extension)
    24652587
    24662588def ReadCIF(URLorFile):
  • 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.