Changeset 1997 for trunk/GSASIIIO.py


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)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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):
Note: See TracChangeset for help on using the changeset viewer.