Changeset 2003 for trunk/GSASIIIO.py


Ignore:
Timestamp:
Oct 12, 2015 5:31:16 PM (6 years ago)
Author:
toby
Message:

Make Transpose a config opt; new routine to read images with import routine (G2IO.ReadImageData?) -- used in Debug mode; In debug mode, import exceptions not caught.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIIO.py

    r2001 r2003  
    4646DEBUG = False       #=True for various prints
    4747TRANSP = False      #=true to transpose images for testing
     48if GSASIIpath.GetConfigValue('Transpose'): TRANSP = True
    4849npsind = lambda x: np.sin(x*np.pi/180.)
    4950
     
    349350    G2frame.Image = Id
    350351   
     352def ReadImageData(G2frame,imagefile,imageOnly=False):
     353    '''Read a single image with an image importer. replacement for GetImageData
     354
     355    :param wx.Frame G2frame: main GSAS-II Frame and data object.
     356    :param str imagefile: name of image file
     357    :param bool imageOnly: If True return only the image,
     358      otherwise  (default) return more (see below)
     359
     360    :returns: an image as a numpy array or a list of four items:
     361      Comments, Data, Npix and the Image, as selected by imageOnly
     362
     363    '''
     364    # determine which formats are compatible with this file
     365    print 'Debug: using ReadImageData'
     366    primaryReaders = []
     367    secondaryReaders = []
     368    for rd in G2frame.ImportImageReaderlist:
     369        flag = rd.ExtensionValidator(imagefile)
     370        if flag is None:
     371            secondaryReaders.append(rd)
     372        elif flag:
     373            primaryReaders.append(rd)
     374    if len(secondaryReaders) + len(primaryReaders) == 0:
     375        print('Error: No matching format for file '+filename)
     376        raise Exception('No image read')
     377    fp = None
     378    errorReport = ''
     379    fp = open(imagefile,'Ur')
     380    for rd in primaryReaders+secondaryReaders:
     381        rd.ReInitialize() # purge anything from a previous read
     382        fp.seek(0)  # rewind
     383        rd.errors = "" # clear out any old errors
     384        if not rd.ContentsValidator(fp): # rejected on cursory check
     385            errorReport += "\n  "+rd.formatName + ' validator error'
     386            if rd.errors:
     387                errorReport += ': '+rd.errors
     388                continue
     389        rdbuffer = {} # create temporary storage for file reader
     390        if GSASIIpath.GetConfigValue('debug'):
     391            flag = rd.Reader(imagefile,fp,G2frame)
     392        else:
     393            flag = False
     394            try:
     395                flag = rd.Reader(imagefile,fp,G2frame)
     396            except rd.ImportException as detail:
     397                rd.errors += "\n  Read exception: "+str(detail)
     398            except Exception as detail:
     399                import traceback
     400                rd.errors += "\n  Unhandled read exception: "+str(detail)
     401                rd.errors += "\n  Traceback info:\n"+str(traceback.format_exc())
     402        if flag: # this read succeeded
     403            if rd.Image is None:
     404                raise Exception('No image read. Strange!')
     405            if GSASIIpath.GetConfigValue('Transpose'):
     406                rd.Image = rd.Image.T
     407            #rd.readfilename = imagefile
     408            if imageOnly:
     409                return rd.Image.T
     410            else:
     411                return rd.Comments,rd.Data,rd.Npix,rd.Image
     412    else:
     413        print('Error reading file '+filename)
     414        print('Error messages(s)\n'+errorReport)
     415        raise Exception('No image read')
     416   
    351417def GetImageData(G2frame,imagefile,imageOnly=False):
    352418    '''Read an image with the file reader keyed by the
     
    364430
    365431    '''
     432    if GSASIIpath.GetConfigValue('debug'):  # test out using replacement routine
     433        return ReadImageData(G2frame,imagefile,imageOnly)
     434   
    366435    ext = ospath.splitext(imagefile)[1]
    367436    Comments = []
     
    522591        return head,data,Npix,image
    523592   
    524        
    525593def GetGEsumData(filename,imageOnly=False):
    526594    'Read SUM file as produced at 1-ID from G.E. images'
Note: See TracChangeset for help on using the changeset viewer.