Changeset 2065 for trunk/GSASIIIO.py


Ignore:
Timestamp:
Nov 22, 2015 5:56:33 PM (6 years ago)
Author:
toby
Message:

Allow reading of multiple images from single file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIIO.py

    r2056 r2065  
    278278def ReadLoadImage(imagefile,G2frame):
    279279    '''Read a GSAS-II image file and load it into the data tree
     280    Called only from GSASII.OnImageRead (depreciated).
    280281    '''
    281282    # if a zip file, open and extract
     
    284285        if extractedfile is not None and extractedfile != imagefile:
    285286            imagefile = extractedfile
    286     Comments,Data,Npix,Image = GetImageData(G2frame,imagefile)
     287    Comments,Data,Npix,Image = GetImageData(G2frame,imagefile) # can only read 1st image
    287288    if Comments:
    288289        LoadImage2Tree(imagefile,G2frame,Comments,Data,Npix,Image)
    289290   
    290291def LoadImage2Tree(imagefile,G2frame,Comments,Data,Npix,Image):
    291     '''Load an image into the tree
     292    '''Load an image into the tree. Saves the location of the image, as well as the
     293    ImageTag (where there is more than one image in the file), if defined.
    292294    '''
    293    
    294295    ImgNames = []
    295296    if G2frame.PatternTree.GetCount(): # get a list of existing Image entries
     
    299300            if name.startswith('IMG'): ImgNames.append(name)       
    300301            item, cookie = G2frame.PatternTree.GetNextChild(G2frame.root, cookie)
    301     TreeName = G2obj.MakeUniqueLabel('IMG '+os.path.basename(imagefile),ImgNames)
     302    TreeLbl = 'IMG '+os.path.basename(imagefile)
     303    ImageTag = Data.get('ImageTag')
     304    if ImageTag:
     305        TreeLbl += ' #'+str(ImageTag)
     306        imageInfo = (imagefile,ImageTag)
     307    else:
     308        imageInfo = imagefile
     309    TreeName = G2obj.MakeUniqueLabel(TreeLbl,ImgNames)
    302310    Id = G2frame.PatternTree.AppendItem(parent=G2frame.root,text=TreeName)
    303311    G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Comments'),Comments)
     
    354362    G2frame.PatternTree.SetItemPyData(G2frame.PatternTree.AppendItem(Id,text='Stress/Strain'),
    355363        {'Type':'True','d-zero':[],'Sample phi':0.0,'Sample z':0.0,'Sample load':0.0})
    356     G2frame.PatternTree.SetItemPyData(Id,[Npix,imagefile])
     364    G2frame.PatternTree.SetItemPyData(Id,[Npix,imageInfo])
    357365    G2frame.PickId = Id
    358366    G2frame.PickIdText = G2frame.GetTreeItemsList(G2frame.PickId)
    359367    G2frame.Image = Id
    360    
    361 def ReadImageData(G2frame,imagefile,imageOnly=False):
    362     '''Read a single image with an image importer. Replacement for GetImageData
     368
     369def GetImageData(G2frame,imagefile,imageOnly=False,ImageTag=None):
     370    '''Read a single image with an image importer.
    363371
    364372    :param wx.Frame G2frame: main GSAS-II Frame and data object.
     
    366374    :param bool imageOnly: If True return only the image,
    367375      otherwise  (default) return more (see below)
     376    :param int/str ImageTag: specifies a particular image to be read from a file.
     377      First image is read if None (default).
    368378
    369379    :returns: an image as a numpy array or a list of four items:
     
    394404            if rd.errors:
    395405                errorReport += ': '+rd.errors
    396                 continue 
     406                continue
    397407        rdbuffer = {} # create temporary storage for file reader
    398408        if imageOnly:
     
    401411            ParentFrame = G2frame
    402412        if GSASIIpath.GetConfigValue('debug'):
    403             flag = rd.Reader(imagefile,fp,ParentFrame)
     413            flag = rd.Reader(imagefile,fp,ParentFrame,blocknum=ImageTag)
    404414        else:
    405415            flag = False
    406416            try:
    407                 flag = rd.Reader(imagefile,fp,ParentFrame)
     417                flag = rd.Reader(imagefile,fp,ParentFrame,blocknum=ImageTag)
    408418            except rd.ImportException as detail:
    409419                rd.errors += "\n  Read exception: "+str(detail)
     
    427437        print('Error messages(s)\n'+errorReport)
    428438        raise Exception('No image read')   
    429 def GetImageData(G2frame,imagefile,imageOnly=False):
    430     return ReadImageData(G2frame,imagefile,imageOnly)
     439
     440def ReadImages(G2frame,imagefile):
     441    '''Read one or more images from a file and put them into the Tree
     442    using image importers. Called only in :meth:`AutoIntFrame.OnTimerLoop`.
     443
     444    :param wx.Frame G2frame: main GSAS-II Frame and data object.
     445    :param str imagefile: name of image file
     446
     447    :returns: a list of the id's of the IMG tree items created
     448    '''
     449    # determine which formats are compatible with this file
     450    primaryReaders = []
     451    secondaryReaders = []
     452    for rd in G2frame.ImportImageReaderlist:
     453        flag = rd.ExtensionValidator(imagefile)
     454        if flag is None:
     455            secondaryReaders.append(rd)
     456        elif flag:
     457            primaryReaders.append(rd)
     458    if len(secondaryReaders) + len(primaryReaders) == 0:
     459        print('Error: No matching format for file '+filename)
     460        raise Exception('No image read')
     461    errorReport = ''
     462    fp = open(imagefile,'Ur')
     463    rdbuffer = {} # create temporary storage for file reader
     464    for rd in primaryReaders+secondaryReaders:
     465        rd.ReInitialize() # purge anything from a previous read
     466        fp.seek(0)  # rewind
     467        rd.errors = "" # clear out any old errors
     468        if not rd.ContentsValidator(fp): # rejected on cursory check
     469            errorReport += "\n  "+rd.formatName + ' validator error'
     470            if rd.errors:
     471                errorReport += ': '+rd.errors
     472                continue
     473        ParentFrame = G2frame
     474        block = 0
     475        repeat = True
     476        CreatedIMGitems = []
     477        while repeat: # loop if the reader asks for another pass on the file
     478            block += 1
     479            repeat = False
     480            if GSASIIpath.GetConfigValue('debug'):
     481                flag = rd.Reader(imagefile,fp,ParentFrame,blocknum=block,Buffer=rdbuffer)
     482            else:
     483                flag = False
     484                try:
     485                    flag = rd.Reader(imagefile,fp,ParentFrame,blocknum=block,Buffer=rdbuffer)
     486                except rd.ImportException as detail:
     487                    rd.errors += "\n  Read exception: "+str(detail)
     488                except Exception as detail:
     489                    import traceback
     490                    rd.errors += "\n  Unhandled read exception: "+str(detail)
     491                    rd.errors += "\n  Traceback info:\n"+str(traceback.format_exc())
     492            if flag: # this read succeeded
     493                if rd.Image is None:
     494                    raise Exception('No image read. Strange!')
     495                if GSASIIpath.GetConfigValue('Transpose'):
     496                    print 'Transposing Image!'
     497                    rd.Image = rd.Image.T
     498                LoadImage2Tree(imagefile,G2frame,rd.Comments,rd.Data,rd.Npix,rd.Image)
     499                repeat = rd.repeat
     500            CreatedIMGitems.append(G2frame.Image)
     501        if CreatedIMGitems: return CreatedIMGitems
     502    else:
     503        print('Error reading file '+filename)
     504        print('Error messages(s)\n'+errorReport)
     505        return []
     506        #raise Exception('No image read')   
    431507       
    432508def PutG2Image(filename,Comments,Data,Npix,image):
     
    437513    return
    438514   
    439 def GetG2Image(filename):
    440     'Read an image as a python pickle'
    441     File = open(filename,'rb')
    442     Comments,Data,Npix,image = cPickle.load(File)
    443     File.close()
    444     return Comments,Data,Npix,image
    445    
     515# should get moved to importer when ready to test
    446516def GetEdfData(filename,imageOnly=False):   
    447517    'Read European detector data edf file'
     
    507577        return head,data,Npix,image
    508578       
     579# should get moved to importer when ready to test
    509580def GetRigaku(filename,imageOnly=False):
    510581    'Read Rigaku R-Axis IV image file'
     
    538609        return head,data,Npix,image
    539610   
    540 def GetGEsumData(filename,imageOnly=False):
    541     'Read SUM file as produced at 1-ID from G.E. images'
    542     import struct as st
    543     import array as ar
    544     if not imageOnly:
    545         print 'Read GE sum file: ',filename   
    546     File = open(filename,'rb')
    547     if '.sum' in filename or '.cor' in filename:
    548         head = ['GE detector sum or cor data from APS 1-ID',]
    549         sizexy = [2048,2048]
    550     elif '.avg' in filename or '.ge' in filename:
    551         head = ['GE detector avg or ge* data from APS 1-ID',]
    552         sizexy = [2048,2048]
    553     else:
    554         head = ['GE detector raw data from APS 1-ID',]
    555         File.seek(18)
    556         size,nframes = st.unpack('<ih',File.read(6))
    557         sizexy = [2048,2048]
    558         pos = 8192
    559         File.seek(pos)
    560     Npix = sizexy[0]*sizexy[1]
    561     if '.sum' in filename or '.cor' in filename:
    562         image = np.array(ar.array('f',File.read(4*Npix)),dtype=np.int32)
    563     elif '.avg' in filename or '.ge' in filename:
    564         image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
    565     else:
    566         image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
    567         while nframes > 1:
    568             image += np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
    569             nframes -= 1
    570     image = np.reshape(image,(sizexy[1],sizexy[0]))
    571     data = {'pixelSize':[200,200],'wavelength':0.15,'distance':250.0,'center':[204.8,204.8],'size':sizexy} 
    572     File.close()   
    573     if imageOnly:
    574         return image
    575     else:
    576         return head,data,Npix,image
    577        
     611# should get moved to importer when ready to test       
    578612def GetImgData(filename,imageOnly=False):
    579613    'Read an ADSC image file'
     
    627661        return lines[1:-2],data,Npix,image
    628662       
     663# should get moved to importer when ready to test
    629664def GetMAR345Data(filename,imageOnly=False):
    630665    'Read a MAR-345 image plate image'
     
    687722        return head,data,Npix,image
    688723       
     724# should get moved to importer when ready to test
    689725def GetPNGData(filename,imageOnly=False):
    690726    '''Read an image in a png format, assumes image is converted from CheMin tif file
     
    703739    else:
    704740        return Comments,Data,Npix,Image.T
    705 
    706741   
    707 #def GetTifData(filename,imageOnly=False):
    708 #    import struct as st
    709 #    import array as ar
    710 #    File = open(filename,'rb')
    711 #    dataType = 5
    712 #    try:
    713 #        Meta = open(filename+'.metadata','Ur')
    714 #        head = Meta.readlines()
    715 #        for line in head:
    716 #            line = line.strip()
    717 #            if 'dataType=' in line:
    718 #                dataType = int(line.split('=')[1])
    719 #        Meta.close()
    720 #    except IOError:
    721 #        print 'no metadata file found - will try to read file anyway'
    722 #        head = ['no metadata file found',]
    723 #       
    724 #    tag = File.read(2)
    725 #    byteOrd = '<'
    726 #    if tag == 'II' and int(st.unpack('<h',File.read(2))[0]) == 42:     #little endian
    727 #        IFD = int(st.unpack(byteOrd+'i',File.read(4))[0])
    728 #    elif tag == 'MM' and int(st.unpack('>h',File.read(2))[0]) == 42:   #big endian
    729 #        byteOrd = '>'
    730 #        IFD = int(st.unpack(byteOrd+'i',File.read(4))[0])       
    731 #    else:
    732 #        lines = ['not a detector tiff file',]
    733 #        return lines,0,0,0
    734 #    File.seek(IFD)                                                  #get number of directory entries
    735 #    NED = int(st.unpack(byteOrd+'h',File.read(2))[0])
    736 #    IFD = {}
    737 #    for ied in range(NED):
    738 #        Tag,Type = st.unpack(byteOrd+'Hh',File.read(4))
    739 #        nVal = st.unpack(byteOrd+'i',File.read(4))[0]
    740 #        if Type == 1:
    741 #            Value = st.unpack(byteOrd+nVal*'b',File.read(nVal))
    742 #        elif Type == 2:
    743 #            Value = st.unpack(byteOrd+'i',File.read(4))
    744 #        elif Type == 3:
    745 #            Value = st.unpack(byteOrd+nVal*'h',File.read(nVal*2))
    746 #            x = st.unpack(byteOrd+nVal*'h',File.read(nVal*2))
    747 #        elif Type == 4:
    748 #            Value = st.unpack(byteOrd+nVal*'i',File.read(nVal*4))
    749 #        elif Type == 5:
    750 #            Value = st.unpack(byteOrd+nVal*'i',File.read(nVal*4))
    751 #        elif Type == 11:
    752 #            Value = st.unpack(byteOrd+nVal*'f',File.read(nVal*4))
    753 #        IFD[Tag] = [Type,nVal,Value]
    754 ##        print Tag,IFD[Tag]
    755 #    sizexy = [IFD[256][2][0],IFD[257][2][0]]
    756 #    [nx,ny] = sizexy
    757 #    Npix = nx*ny
    758 #    if 272 in IFD:
    759 #        ifd = IFD[272]
    760 #        File.seek(ifd[2][0])
    761 #        S = File.read(ifd[1])
    762 #        if 'PILATUS' in S:
    763 #            tifType = 'Pilatus'
    764 #            dataType = 0
    765 #            pixy = (172,172)
    766 #            File.seek(4096)
    767 #            if not imageOnly:
    768 #                print 'Read Pilatus tiff file: ',filename
    769 #            image = ar.array('L',File.read(4*Npix))
    770 #            image = np.array(np.asarray(image),dtype=np.int32)
    771 #    elif 262 in IFD and IFD[262][2][0] > 4:
    772 #        tifType = 'DND'
    773 #        pixy = (158,158)
    774 #        File.seek(512)
    775 #        if not imageOnly:
    776 #            print 'Read DND SAX/WAX-detector tiff file: ',filename
    777 #        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
    778 #    elif sizexy == [1536,1536]:
    779 #        tifType = 'APS Gold'
    780 #        pixy = (150,150)
    781 #        File.seek(64)
    782 #        if not imageOnly:
    783 #            print 'Read Gold tiff file:',filename
    784 #        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
    785 #    elif sizexy == [2048,2048] or sizexy == [1024,1024] or sizexy == [3072,3072]:
    786 #        if IFD[273][2][0] == 8:
    787 #            if IFD[258][2][0] == 32:
    788 #                tifType = 'PE'
    789 #                pixy = (200,200)
    790 #                File.seek(8)
    791 #                if not imageOnly:
    792 #                    print 'Read APS PE-detector tiff file: ',filename
    793 #                if dataType == 5:
    794 #                    image = np.array(ar.array('f',File.read(4*Npix)),dtype=np.float32)
    795 #                else:
    796 #                    image = np.array(ar.array('I',File.read(4*Npix)),dtype=np.int32)
    797 #        elif IFD[273][2][0] == 4096:
    798 #            if sizexy[0] == 3072:
    799 #                pixy =  (73,73)
    800 #                tifType = 'MAR225'           
    801 #            else:
    802 #                pixy = (158,158)
    803 #                tifType = 'MAR325'           
    804 #            File.seek(4096)
    805 #            if not imageOnly:
    806 #                print 'Read MAR CCD tiff file: ',filename
    807 #            image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
    808 #        elif IFD[273][2][0] == 512:
    809 #            tiftype = '11-ID-C'
    810 #            pixy = [200,200]
    811 #            File.seek(512)
    812 #            if not imageOnly:
    813 #                print 'Read 11-ID-C tiff file: ',filename
    814 #            image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)           
    815 #    elif sizexy == [4096,4096]:
    816 #        if IFD[273][2][0] == 8:
    817 #            if IFD[258][2][0] == 16:
    818 #                tifType = 'scanCCD'
    819 #                pixy = (9,9)
    820 #                File.seek(8)
    821 #                if not imageOnly:
    822 #                    print 'Read APS scanCCD tiff file: ',filename
    823 #                image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
    824 #        elif IFD[273][2][0] == 4096:
    825 #            tifType = 'Rayonix'
    826 #            pixy = (73.242,73.242)
    827 #            File.seek(4096)
    828 #            if not imageOnly:
    829 #                print 'Read Rayonix MX300HE tiff file: ',filename
    830 #            image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
    831 ##    elif sizexy == [960,960]:
    832 ##        tiftype = 'PE-BE'
    833 ##        pixy = (200,200)
    834 ##        File.seek(8)
    835 ##        if not imageOnly:
    836 ##            print 'Read Gold tiff file:',filename
    837 ##        image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32)
    838 #           
    839 #    else:
    840 #        lines = ['not a known detector tiff file',]
    841 #        return lines,0,0,0
    842 #       
    843 #    image = np.reshape(image,(sizexy[1],sizexy[0]))
    844 #    center = [pixy[0]*sizexy[0]/2000,pixy[1]*sizexy[1]/2000]
    845 #    data = {'pixelSize':pixy,'wavelength':0.10,'distance':100.0,'center':center,'size':sizexy}
    846 #    File.close()   
    847 #    if imageOnly:
    848 #        return image
    849 #    else:
    850 #        return head,data,Npix,image
    851 #   
    852742def ProjFileOpen(G2frame):
    853743    'Read a GSAS-II project file and load into the G2 data tree'
     
    17611651    '''Defines a base class for the reading of images
    17621652
    1763     Structure factors are read with a call to :meth:`GSASII.GSASII.OnImportImage`
     1653    Images are intially read with a call to :meth:`GSASII.GSASII.OnImportImage`
    17641654    which in turn calls :meth:`GSASII.GSASII.OnImportGeneric`, which calls
    17651655    methods :meth:`ExtensionValidator`, :meth:`ContentsValidator` and
    1766     :meth:`Reader`.
     1656    :meth:`Reader`. Images are also reread with :func:`GSASIIIO.GetImageData`
    17671657
    17681658    See :ref:`Writing a Import Routine<Import_Routines>`
    17691659    for an explanation on how to use import classes in general. The specifics
    17701660    for reading an image requires that the ``Reader()`` routine in the import
    1771     class need to do only a few things:...
     1661    class should set:
    17721662   
    1773     (should load :attr:`RefDict` item ``'RefList'`` with the reflection list,
    1774     (and set :attr:`Parameters` with the instrument parameters
    1775     (initialized with :meth:`InitParameters` and set with :meth:`UpdateParameters`).
     1663      * :attr:`Comments` (a list of strings),
     1664      * :attr:`Data` (a dict defining image parameters),
     1665      * :attr:`Npix` (the number of pixels in the image)
     1666      * :attr:`Image` (the actual image)
     1667      * optionally: :attr:`repeat` (set to True if there are additional images to
     1668        read in the file)
     1669     
     1670     
     1671    Note that the above is initialized with :meth:`InitParameters`.
    17761672    '''
    17771673    def __init__(self,formatName,longFormatName=None,extensionlist=[],
     
    17921688        self.Npix = 0
    17931689        self.Image = None
    1794 
    1795     def LoadImage(self,ParentFrame,imagefile):
     1690        self.repeat = False
     1691
     1692    def LoadImage(self,ParentFrame,imagefile,imagetag=None):
    17961693        '''Optionally, call this after reading in an image to load it into the tree.
    1797         This does saves time by preventing a reread of the same information.
     1694        This saves time by preventing a reread of the same information.
    17981695        '''
    17991696        if ParentFrame:
    18001697            ParentFrame.ImageZ = self.Image   # store the image for plotting
    18011698            ParentFrame.oldImagefile = imagefile # save the name of the last image file read
    1802            
     1699            ParentFrame.oldImageTag = imagetag   # save the tag of the last image file read           
    18031700
    18041701######################################################################
Note: See TracChangeset for help on using the changeset viewer.