Changeset 3136 for trunk/GSASIIIO.py


Ignore:
Timestamp:
Oct 23, 2017 11:39:16 AM (4 years ago)
Author:
vondreele
Message:

make GSAS-II python 3.6 compliant & preserve python 2.7 use;changes:
do from future import division, print_function for all GSAS-II py sources
all menu items revised to be py 2.7/3.6 compliant
all wx.OPEN --> wx.FD_OPEN in file dialogs
all integer divides (typically for image pixel math) made explicit with ; ambiguous ones made floats as appropriate
all print "stuff" --> print (stuff)
all print >> pFile,'stuff' --> pFile.writeCIFtemplate('stuff')
all read file opens made explicit 'r' or 'rb'
all cPickle imports made for py2.7 or 3.6 as cPickle or _pickle; test for '2' platform.version_tuple[0] for py 2.7
define cPickleload to select load(fp) or load(fp,encoding='latin-1') for loading gpx files; provides cross compatibility between py 2.7/3.6 gpx files
make dict.keys() as explicit list(dict.keys()) as needed (NB: possible source of remaining py3.6 bugs)
make zip(a,b) as explicit list(zip(a,b)) as needed (NB: possible source of remaining py3.6 bugs)
select unichr/chr according test for '2' platform.version_tuple[0] for py 2.7 (G2pwdGUI * G2plot) for special characters
select wg.EVT_GRID_CELL_CHANGE (classic) or wg.EVT_GRID_CELL_CHANGED (phoenix) in grid Bind
maxint --> maxsize; used in random number stuff
raise Exception,"stuff" --> raise Exception("stuff")
wx 'classic' sizer.DeleteWindows?() or 'phoenix' sizer.Clear(True)
wx 'classic' SetToolTipString?(text) or 'phoenix' SetToolTip?(wx.ToolTip?(text)); define SetToolTipString?(self,text) to handle the choice in plots
status.SetFields? --> status.SetStatusText?
'classic' AddSimpleTool? or 'phoenix' self.AddTool? for plot toolbar; Bind different as well
define GetItemPydata? as it doesn't exist in wx 'phoenix'
allow python versions 2.7 & 3.6 to run GSAS-II
Bind override commented out - no logging capability (NB: remove all logging code?)
all import ContentsValidator? open filename & test if valid then close; filepointer removed from Reader
binary importers (mostly images) test for 'byte' type & convert as needed to satisfy py 3.6 str/byte rules

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIIO.py

    r3023 r3136  
    2020This module needs some work to separate wx from non-wx routines
    2121'''
    22 """GSASIIIO: functions for IO of data
    23    Copyright: 2008, Robert B. Von Dreele (Argonne National Laboratory)
    24 """
    2522# If this is being used for GSASIIscriptable, don't depend on wx
     23from __future__ import division, print_function
    2624try:
    2725    import wx
     
    3432import numpy as np
    3533import copy
    36 import cPickle
     34import platform
     35if '2' in platform.python_version_tuple()[0]:
     36    import cPickle
     37else:
     38    import _pickle as cPickle
    3739import sys
    3840import re
     
    124126    File.close()
    125127    if Comments:
    126        print 'Comments on file:'
     128       print ('Comments on file:')
    127129       for Comment in Comments:
    128             print Comment
     130            print (Comment)
    129131            if 'wavelength' in Comment:
    130132                wave = float(Comment.split('=')[1])
     
    170172        imagefile = Imagefile
    171173    if not os.path.exists(imagefile):
    172         print 'Image file '+imagefile+' not found'
     174        print ('Image file '+imagefile+' not found')
    173175        fil = imagefile.replace('\\','/') # windows?!
    174176        # see if we can find a file with same name or in a similarly named sub-dir
     
    178180            prevpth = pth
    179181            if os.path.exists(os.path.join(G2frame.dirname,fil)):
    180                 print 'found image file '+os.path.join(G2frame.dirname,fil)
     182                print ('found image file '+os.path.join(G2frame.dirname,fil))
    181183                imagefile = os.path.join(G2frame.dirname,fil)
    182184                G2frame.GPXtree.UpdateImageLoc(treeId,imagefile)
     
    202204            prevpth = pth
    203205            if os.path.exists(os.path.join(pth,fil)):
    204                 print 'found image file '+os.path.join(pth,fil)
     206                print ('found image file '+os.path.join(pth,fil))
    205207                imagefile = os.path.join(pth,fil)
    206208                G2frame.GPXtree.UpdateImageLoc(treeId,imagefile)
     
    214216        wildcard = 'Image format (*'+prevext+')|*'+prevext
    215217        dlg = wx.FileDialog(G2frame, 'Previous image file ('+prevnam+') not found; open here', '.', prevnam,
    216                             wildcard,wx.OPEN)
     218                            wildcard,wx.FD_OPEN)
    217219        try:
    218220            dlg.SetFilename(''+ospath.split(imagefile)[1])
     
    390392        print('Error: No matching format for file '+imagefile)
    391393        raise Exception('No image read')
    392     fp = None
    393394    errorReport = ''
    394395    if not imagefile:
    395396        return
    396     fp = open(imagefile,'Ur')
    397397    for rd in primaryReaders+secondaryReaders:
    398398        rd.ReInitialize() # purge anything from a previous read
    399         fp.seek(0)  # rewind
    400399        rd.errors = "" # clear out any old errors
    401         if not rd.ContentsValidator(fp): # rejected on cursory check
     400        if not rd.ContentsValidator(imagefile): # rejected on cursory check
    402401            errorReport += "\n  "+rd.formatName + ' validator error'
    403402            if rd.errors:
     
    409408            ParentFrame = G2frame
    410409        if GSASIIpath.GetConfigValue('debug'):
    411             flag = rd.Reader(imagefile,fp,ParentFrame,blocknum=ImageTag)
     410            flag = rd.Reader(imagefile,ParentFrame,blocknum=ImageTag)
    412411        else:
    413412            flag = False
    414413            try:
    415                 flag = rd.Reader(imagefile,fp,ParentFrame,blocknum=ImageTag)
     414                flag = rd.Reader(imagefile,ParentFrame,blocknum=ImageTag)
    416415            except rd.ImportException as detail:
    417416                rd.errors += "\n  Read exception: "+str(detail)
     
    424423                raise Exception('No image read. Strange!')
    425424            if GSASIIpath.GetConfigValue('Transpose'):
    426                 print 'Transposing Image!'
     425                print ('Transposing Image!')
    427426                rd.Image = rd.Image.T
    428427            #rd.readfilename = imagefile
     
    458457        raise Exception('No image read')
    459458    errorReport = ''
    460     fp = open(imagefile,'Ur')
    461459    rdbuffer = {} # create temporary storage for file reader
    462460    for rd in primaryReaders+secondaryReaders:
    463461        rd.ReInitialize() # purge anything from a previous read
    464         fp.seek(0)  # rewind
    465462        rd.errors = "" # clear out any old errors
    466         if not rd.ContentsValidator(fp): # rejected on cursory check
     463        if not rd.ContentsValidator(imagefile): # rejected on cursory check
    467464            errorReport += "\n  "+rd.formatName + ' validator error'
    468465            if rd.errors:
     
    477474            repeat = False
    478475            if GSASIIpath.GetConfigValue('debug'):
    479                 flag = rd.Reader(imagefile,fp,ParentFrame,blocknum=block,Buffer=rdbuffer)
     476                flag = rd.Reader(imagefile,ParentFrame,blocknum=block,Buffer=rdbuffer)
    480477            else:
    481478                flag = False
    482479                try:
    483                     flag = rd.Reader(imagefile,fp,ParentFrame,blocknum=block,Buffer=rdbuffer)
     480                    flag = rd.Reader(imagefile,ParentFrame,blocknum=block,Buffer=rdbuffer)
    484481                except rd.ImportException as detail:
    485482                    rd.errors += "\n  Read exception: "+str(detail)
     
    492489                    raise Exception('No image read. Strange!')
    493490                if GSASIIpath.GetConfigValue('Transpose'):
    494                     print 'Transposing Image!'
     491                    print ('Transposing Image!')
    495492                    rd.Image = rd.Image.T
    496493                rd.Data['ImageTag'] = rd.repeatcount
     
    507504def SaveMultipleImg(G2frame):
    508505    if not G2frame.GPXtree.GetCount():
    509         print 'no images!'
     506        print ('no images!')
    510507        return
    511508    choices = G2gd.GetGPXtreeDataNames(G2frame,['IMG ',])
     
    549546    'Write an image as a python pickle - might be better as an .edf file?'
    550547    File = open(filename,'wb')
    551     cPickle.dump([Comments,Data,Npix,image],File,1)
     548    cPickle.dump([Comments,Data,Npix,image],File,2)
    552549    File.close()
    553550    return
     
    561558    LastSavedUsing = None
    562559    file = open(G2frame.GSASprojectfile,'rb')
    563     if showProvenance: print 'loading from file: ',G2frame.GSASprojectfile
     560    if showProvenance: print ('loading from file: '+G2frame.GSASprojectfile)
    564561    #G2frame.SetTitle("GSAS-II data tree: "+
    565562    #                 os.path.split(G2frame.GSASprojectfile)[1])
    566563    G2frame.SetTitle("GSAS-II data tree: "+
    567                      os.path.split(G2frame.GSASprojectfile)[1],1)
     564        os.path.split(G2frame.GSASprojectfile)[1],1)
    568565    wx.BeginBusyCursor()
    569566    try:
    570567        while True:
    571568            try:
    572                 data = cPickle.load(file)
     569                if '2' in platform.python_version_tuple()[0]:
     570                    data = cPickle.load(file)
     571                else:
     572                    data = cPickle.load(file,encoding='latin-1')
    573573            except EOFError:
    574574                break
     
    578578            if datum[0].startswith('PWDR'):               
    579579                if 'ranId' not in datum[1][0]: # patch: add random Id if not present
    580                     datum[1][0]['ranId'] = ran.randint(0,sys.maxint)
     580                    datum[1][0]['ranId'] = ran.randint(0,sys.maxsize)
    581581                G2frame.GPXtree.SetItemPyData(Id,datum[1][:3])  #temp. trim off junk (patch?)
    582582            elif datum[0].startswith('HKLF'):
    583583                if 'ranId' not in datum[1][0]: # patch: add random Id if not present
    584                     datum[1][0]['ranId'] = ran.randint(0,sys.maxint)
     584                    datum[1][0]['ranId'] = ran.randint(0,sys.maxsize)
    585585                G2frame.GPXtree.SetItemPyData(Id,datum[1])
    586586            else:
     
    641641    if not G2frame.GPXtree.IsEmpty():
    642642        file = open(G2frame.GSASprojectfile,'wb')
    643         print 'save to file: ',G2frame.GSASprojectfile
     643        print ('save to file: '+G2frame.GSASprojectfile)
    644644        # stick the file name into the tree and version info into tree so they are saved.
    645645        # (Controls should always be created at this point)
     
    665665                    item2, cookie2 = G2frame.GPXtree.GetNextChild(item, cookie2)                           
    666666                item, cookie = G2frame.GPXtree.GetNextChild(G2frame.root, cookie)                           
    667                 cPickle.dump(data,file,1)
     667                cPickle.dump(data,file,2)
    668668            file.close()
    669669            pth = os.path.split(os.path.abspath(G2frame.GSASprojectfile))[0]
     
    766766            'wtFactor':1.0,
    767767            'Dummy':False,
    768             'ranId':ran.randint(0,sys.maxint),
     768            'ranId':ran.randint(0,sys.maxsize),
    769769            'Offset':[0.0,0.0],'delOffset':0.02*Ymax,'refOffset':-0.1*Ymax,'refDelt':0.1*Ymax,
    770770            'qPlot':False,'dPlot':False,'sqrtPlot':False,'Yminmax':[Ymin,Ymax]
     
    801801            File.write('%.3f,%.3f\n'%(x,y))   
    802802    File.close()
    803     print ' XY data saved to: ',filename
     803    print (' XY data saved to: '+filename)
    804804           
    805805def PDFSave(G2frame,exports,PDFsaves):
     
    821821                iqfile.write("%15.6g %15.6g\n" % (q,iq))
    822822            iqfile.close()
    823             print ' I(Q) saved to: ',iqfilename
     823            print (' I(Q) saved to: '+iqfilename)
    824824           
    825825        if PDFsaves[1]:     #S(Q)
     
    835835                sqfile.write("%15.6g %15.6g\n" % (q,sq))
    836836            sqfile.close()
    837             print ' S(Q) saved to: ',sqfilename
     837            print (' S(Q) saved to: '+sqfilename)
    838838           
    839839        if PDFsaves[2]:     #F(Q)
     
    849849                fqfile.write("%15.6g %15.6g\n" % (q,fq))
    850850            fqfile.close()
    851             print ' F(Q) saved to: ',fqfilename
     851            print (' F(Q) saved to: '+fqfilename)
    852852           
    853853        if PDFsaves[3]:     #G(R)
     
    863863                grfile.write("%15.6g %15.6g\n" % (r,gr))
    864864            grfile.close()
    865             print ' G(R) saved to: ',grfilename
     865            print (' G(R) saved to: '+grfilename)
    866866       
    867867        if PDFsaves[4]: #pdfGUI file for G(R)
     
    922922                grfile.write("%15.2F %15.6F\n" % (r,gr))
    923923            grfile.close()
    924             print ' G(R) saved to: ',grfilename
     924            print (' G(R) saved to: '+grfilename)
    925925   
    926926def PeakListSave(G2frame,file,peaks):
    927927    'Save powder peaks to a data file'
    928     print 'save peak list to file: ',G2frame.peaklistfile
     928    print ('save peak list to file: '+G2frame.peaklistfile)
    929929    if not peaks:
    930930        dlg = wx.MessageDialog(G2frame, 'No peaks!', 'Nothing to save!', wx.OK)
     
    937937        file.write("%10.4f %12.2f %10.3f %10.3f \n" % \
    938938            (peak[0],peak[2],peak[4],peak[6]))
    939     print 'peak list saved'
     939    print ('peak list saved')
    940940             
    941941def IndexPeakListSave(G2frame,peaks):
    942942    'Save powder peaks from the indexing list'
    943943    file = open(G2frame.peaklistfile,'wa')
    944     print 'save index peak list to file: ',G2frame.peaklistfile
     944    print ('save index peak list to file: '+G2frame.peaklistfile)
    945945    wx.BeginBusyCursor()
    946946    try:
     
    957957    finally:
    958958        wx.EndBusyCursor()
    959     print 'index peak list saved'
     959    print ('index peak list saved')
    960960   
    961961class MultipleChoicesDialog(wx.Dialog):
     
    13161316            *or* a complete file name is requested when a single file
    13171317            name is selected. self.dirname is always set and self.filename used
    1318             only when a single file is selected.
     1318            only when a single file is selected. 
    13191319          * if AskFile is 'default', creates a name of the file to be used from
    13201320            the name of the project (.gpx) file. If the project has not been saved,
     
    15181518        elif varyList is None:
    15191519            # old GPX file from before pre-constraint varyList is saved
    1520             print ' *** Old refinement: Please use Calculate/Refine to redo  ***'
     1520            print (' *** Old refinement: Please use Calculate/Refine to redo  ***')
    15211521            raise Exception(' *** Export aborted ***')
    15221522        else:
     
    15271527        except:
    15281528            # this really should not happen
    1529             print ' *** ERROR - constraints are internally inconsistent ***'
     1529            print (' *** ERROR - constraints are internally inconsistent ***')
    15301530            errmsg, warnmsg = G2mv.CheckConstraints(varyList,constDict,fixedList)
    1531             print 'Errors',errmsg
    1532             if warnmsg: print 'Warnings',warnmsg
     1531            print ('Errors'+errmsg)
     1532            if warnmsg: print ('Warnings'+warnmsg)
    15331533            raise Exception(' *** CIF creation aborted ***')
    15341534        # add the constrained values to the parameter dictionary
     
    15621562            sub = G2gd.GetGPXtreeItemId(self.G2frame,self.G2frame.root,'Phases')
    15631563            if not sub:
    1564                 print 'no phases found'
     1564                print ('no phases found')
    15651565                return True
    15661566            item, cookie = self.G2frame.GPXtree.GetFirstChild(sub)
     
    16401640        if self.SeqRefdata and self.SeqRefhist:
    16411641            print('Note that dumpTree does not show sequential results')
    1642         print '\nOverall'
     1642        print ('\nOverall')
    16431643        if mode == 'type':
    16441644            def Show(arg): return type(arg)
     
    16461646            def Show(arg): return arg
    16471647        for key in self.OverallParms:
    1648             print '  ',key,Show(self.OverallParms[key])
    1649         print 'Phases'
     1648            print ('  '+key+Show(self.OverallParms[key]))
     1649        print ('Phases')
    16501650        for key1 in self.Phases:
    1651             print '    ',key1,Show(self.Phases[key1])
    1652         print 'Histogram'
     1651            print ('    '+key1+Show(self.Phases[key1]))
     1652        print ('Histogram')
    16531653        for key1 in self.Histograms:
    1654             print '    ',key1,Show(self.Histograms[key1])
     1654            print ('    '+key1+Show(self.Histograms[key1]))
    16551655            for key2 in self.Histograms[key1]:
    1656                 print '      ',key2,Show(self.Histograms[key1][key2])
     1656                print ('      '+key2+Show(self.Histograms[key1][key2]))
    16571657
    16581658    def defaultSaveFile(self):
     
    19281928                obj.Writer(TreeName,filename)
    19291929                return
    1930             except Exception,err:
     1930            except Exception:
    19311931                print('Export Routine for '+extension+' failed.')
    1932                 print err
    19331932    else:
    19341933        print('No Export routine supports extension '+extension)
     
    19611960            return
    19621961    if exporttype == 'Phase':
    1963         phaselist = obj.Phases.keys()
     1962        phaselist = list(obj.Phases.keys())
    19641963        if len(obj.Phases) == 0:
    19651964            G2G.G2MessageBox(G2frame,'There are no phases in sequential ref.','Warning')
     
    20122011
    20132012def ReadDIFFaX(DIFFaXfile):
    2014     print 'read ',DIFFaXfile
     2013    print ('read '+DIFFaXfile)
    20152014    Layer = {'Laue':'-1','Cell':[False,1.,1.,1.,90.,90.,90,1.],'Width':[[10.,10.],[False,False]],
    20162015        'Layers':[],'Stacking':[],'Transitions':[],'Toler':0.01,'AtInfo':{}}
Note: See TracChangeset for help on using the changeset viewer.