Changeset 3815 for trunk/GSASIIfiles.py


Ignore:
Timestamp:
Feb 11, 2019 11:26:52 AM (3 years ago)
Author:
toby
Message:

Add recalibrate & more image load/set routines

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIfiles.py

    r3814 r3815  
    743743        File.write(key+':'+str(data[key])+'\n')
    744744    File.close()
    745    
     745
     746def RereadImageData(ImageReaderlist,imagefile,ImageTag=None,FormatName=''):
     747    '''Read a single image with an image importer. This is called to
     748    reread an image after it has already been imported, so it is not
     749    necessary to reload metadata.
     750
     751    Based on :func:`GetImageData.GetImageData` which this can replace
     752    where imageOnly=True
     753
     754    :param list ImageReaderlist: list of Reader objects for images
     755    :param str imagefile: name of image file
     756    :param int/str ImageTag: specifies a particular image to be read from a file.
     757      First image is read if None (default).
     758    :param str formatName: the image reader formatName
     759
     760    :returns: an image as a numpy array
     761    '''
     762    # determine which formats are compatible with this file
     763    primaryReaders = []
     764    secondaryReaders = []
     765    for rd in ImageReaderlist:
     766        flag = rd.ExtensionValidator(imagefile)
     767        if flag is None:
     768            secondaryReaders.append(rd)
     769        elif flag:
     770            if not FormatName:
     771                primaryReaders.append(rd)
     772            elif FormatName == rd.formatName:
     773                primaryReaders.append(rd)
     774    if len(secondaryReaders) + len(primaryReaders) == 0:
     775        print('Error: No matching format for file '+imagefile)
     776        raise Exception('No image read')
     777    errorReport = ''
     778    if not imagefile:
     779        return
     780    for rd in primaryReaders+secondaryReaders:
     781        rd.ReInitialize() # purge anything from a previous read
     782        rd.errors = "" # clear out any old errors
     783        if not rd.ContentsValidator(imagefile): # rejected on cursory check
     784            errorReport += "\n  "+rd.formatName + ' validator error'
     785            if rd.errors:
     786                errorReport += ': '+rd.errors
     787                continue
     788        flag = rd.Reader(imagefile,None,blocknum=ImageTag)
     789        if flag: # this read succeeded
     790            if rd.Image is None:
     791                raise Exception('No image read. Strange!')
     792            if GSASIIpath.GetConfigValue('Transpose'):
     793                print ('Transposing Image!')
     794                rd.Image = rd.Image.T
     795            #rd.readfilename = imagefile
     796            return rd.Image
     797    else:
     798        print('Error reading file '+imagefile)
     799        print('Error messages(s)\n'+errorReport)
     800        raise Exception('No image read')
     801
     802def readMasks(filename,masks,ignoreThreshold):
     803    '''Read a GSAS-II masks file'''
     804    File = open(filename,'r')
     805    save = {}
     806    oldThreshold = masks['Thresholds'][0]
     807    S = File.readline()
     808    while S:
     809        if S[0] == '#':
     810            S = File.readline()
     811            continue
     812        [key,val] = S.strip().split(':',1)
     813        if key in ['Points','Rings','Arcs','Polygons','Frames','Thresholds']:
     814            if ignoreThreshold and key == 'Thresholds':
     815                S = File.readline()
     816                continue
     817            save[key] = eval(val)
     818            if key == 'Thresholds':
     819                save[key][0] = oldThreshold
     820                save[key][1][1] = min(oldThreshold[1],save[key][1][1])
     821        S = File.readline()
     822    File.close()
     823    masks.update(save)
     824    # CleanupMasks
     825    for key in ['Points','Rings','Arcs','Polygons']:
     826        masks[key] = masks.get(key,[])
     827        masks[key] = [i for i in masks[key] if len(i)]
Note: See TracChangeset for help on using the changeset viewer.