Changeset 3217


Ignore:
Timestamp:
Jan 8, 2018 4:37:12 PM (4 years ago)
Author:
toby
Message:

request file/dir name before saving a PDF

Location:
trunk
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIIO.py

    r3216 r3217  
    826826    print (' XY data saved to: '+filename)
    827827           
     828def askSaveDirectory(G2frame):
     829    '''Ask the user to supply a directory name. Path name is used as the
     830    starting point for the next export path search.
     831
     832    :returns: a directory name (str) or None if Cancel is pressed
     833    '''
     834    pth = G2G.GetExportPath(G2frame)
     835    dlg = wx.DirDialog(
     836            G2frame, 'Input directory where file(s) will be written', pth,
     837            wx.DD_DEFAULT_STYLE)
     838    dlg.CenterOnParent()
     839    try:
     840        if dlg.ShowModal() == wx.ID_OK:
     841            filename = dlg.GetPath()
     842            G2frame.LastExportDir = filename
     843        else:
     844            filename = None
     845    finally:
     846        dlg.Destroy()
     847    return filename
     848
     849def askSaveFile(G2frame,defnam,extension,longFormatName):
     850    '''Ask the user to supply a file name
     851
     852    :returns: a file name (str) or None if Cancel is pressed
     853    '''
     854
     855    pth = G2G.GetExportPath(G2frame)
     856    dlg = wx.FileDialog(
     857        G2frame, 'Input name for file to write', pth, defnam,
     858        longFormatName+' (*'+extension+')|*'+extension,
     859        wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
     860    dlg.CenterOnParent()
     861    try:
     862        if dlg.ShowModal() == wx.ID_OK:
     863            filename = dlg.GetPath()
     864            G2frame.LastExportDir = os.path.split(filename)[0]
     865            filename = os.path.splitext(filename)[0]+extension # make sure extension is correct
     866        else:
     867            filename = None
     868    finally:
     869        dlg.Destroy()
     870    return filename
     871
    828872def PDFSave(G2frame,exports,PDFsaves):
    829873    'Save a PDF I(Q), S(Q), F(Q) and G(r)  in column formats'
    830874    import scipy.interpolate as scintp
     875    if len(exports) > 1:
     876        dirname = askSaveDirectory(G2frame)
     877        if not dirname: return
     878    else:
     879        defnam = exports[0].replace(' ','_')[5:]
     880        filename = askSaveFile(G2frame,defnam,'.gr','G(r) file, etc.')
     881        if not filename: return
     882        dirname,filename = os.path.split(filename)
     883        filename = os.path.splitext(filename)[0]
    831884    for export in exports:
     885        if len(exports) > 1:
     886            filename = export.replace(' ','_')[5:]
    832887        PickId = G2gd.GetGPXtreeItemId(G2frame, G2frame.root, export)
    833888        PDFControls = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame, PickId,'PDF Controls'))
    834889        if PDFsaves[0]:     #I(Q)
    835             iqfilename = ospath.join(G2frame.dirname,export.replace(' ','_')[5:]+'.iq')
     890            iqfilename = ospath.join(dirname,filename+'.iq')
    836891            iqdata = PDFControls['I(Q)'][0]
    837892            iqfxn = scintp.interp1d(iqdata[0],iqdata[1],kind='linear')
     
    847902           
    848903        if PDFsaves[1]:     #S(Q)
    849             sqfilename = ospath.join(G2frame.dirname,export.replace(' ','_')[5:]+'.sq')
     904            sqfilename = ospath.join(dirname,filename+'.sq')
    850905            sqdata = PDFControls['S(Q)'][1]
    851906            sqfxn = scintp.interp1d(sqdata[0],sqdata[1],kind='linear')
     
    861916           
    862917        if PDFsaves[2]:     #F(Q)
    863             fqfilename = ospath.join(G2frame.dirname,export.replace(' ','_')[5:]+'.fq')
     918            fqfilename = ospath.join(dirname,filename+'.fq')
    864919            fqdata = PDFControls['F(Q)'][1]
    865920            fqfxn = scintp.interp1d(fqdata[0],fqdata[1],kind='linear')
    866             fqfile = open(sqfilename,'w')
     921            fqfile = open(fqfilename,'w')
    867922            fqfile.write('#T F(Q) %s\n'%(export))
    868923            fqfile.write('#L Q     F(Q)\n')
    869             qnew = np.arange(sqdata[0][0],sqdata[0][-1],0.005)
     924            qnew = np.arange(fqdata[0][0],fqdata[0][-1],0.005)
    870925            fqnew = zip(qnew,fqfxn(qnew))
    871926            for q,fq in fqnew:
     
    875930           
    876931        if PDFsaves[3]:     #G(R)
    877             grfilename = ospath.join(G2frame.dirname,export.replace(' ','_')[5:]+'.gr')
     932            grfilename = ospath.join(dirname,filename+'.gr')
    878933            grdata = PDFControls['G(R)'][1]
    879934            grfxn = scintp.interp1d(grdata[0],grdata[1],kind='linear')
     
    892947            Inst = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame, pId,'Instrument Parameters'))[0]
    893948            Limits = G2frame.GPXtree.GetItemPyData(G2gd.GetGPXtreeItemId(G2frame, pId,'Limits'))
    894             grfilename = ospath.join(G2frame.dirname,export.replace(' ','_')[5:]+'.gr')
     949            grfilename = ospath.join(dirname,filename+'.gr')
    895950            grdata = PDFControls['G(R)'][1]
    896951            qdata = PDFControls['I(Q)'][1][0]
Note: See TracChangeset for help on using the changeset viewer.