how to save calculated PDF in Diffpy-CMI

246 views
Skip to first unread message

hy...@hotmail.com

unread,
Dec 10, 2015, 1:04:30 PM12/10/15
to diffpy-users

Hi everyone,


I tried one of the cmi_exchange tutorial examples for calculating C60 PDF.

Could you please teach me how to save the calculated PDF in two column txt file in Diffpy-CMI? 

The script I am using is:

###################################################### 

from diffpy.Structure import loadStructure
from diffpy.srreal.pdfcalculator import DebyePDFCalculator
from matplotlib.pyplot import plot, show


c60 = loadStructure('c60.stru')
dpc = DebyePDFCalculator()
dpc.qmax = 20
dpc.rmax = 20
r3, g3 = dpc(c60, qmin=0)
r4, g4 = dpc(c60, qmin=1)


plot(r3, g3, r4, g4)
show()

##########################################################


Thank you very much for your help!


Hyunjeong
 

Simon Billinge

unread,
Dec 10, 2015, 4:16:53 PM12/10/15
to diffpy-users
I have my own little script I use (pasted below).  I am sure there is a better way but you are welcome to use it.... :)

def writefile(filename,x,y):
    '''helper function that will write files of data after processing them
    
    Writes as two-columns of space-separated x and y values. This type of file
    can be read again easily using loadData for example.
    
    arguments:
    filename - string - output file name
    x - list of x-values
    y - list of y-values
    '''
    f = open(filename,'w')
    it = 0
    for iw in y:
        f.write(str(x[it])+' '+str(y[it])+'\n')
        it+=1
    f.close()
    return

--
You received this message because you are subscribed to the Google Groups "diffpy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to diffpy-users...@googlegroups.com.
To post to this group, send email to diffpy...@googlegroups.com.
Visit this group at http://groups.google.com/group/diffpy-users.
For more options, visit https://groups.google.com/d/optout.



--
----
Prof. Simon Billinge

Applied Physics & Applied Mathematics
Columbia University
500 West 120th Street
Room 200 Mudd, MC 4701
New York, NY 10027
Tel: (212)-854-2918 (o) 851-7428 (lab)

Condensed Matter Physics and Materials Science Dept.
Brookhaven National Laboratory
P.O. Box 5000
Upton, NY 11973-5000
(631)-344-5661

email: sb2896 at columbia dot edu
home: http://thebillingegroup.com

Pavol Juhas

unread,
Dec 11, 2015, 2:12:31 AM12/11/15
to diffpy...@googlegroups.com
> On Thu, Dec 10, 2015 at 2:51 AM, <hy...@hotmail.com> wrote:
...
> > Could you please teach me how to save the calculated PDF in two column txt
> > file in Diffpy-CMI?
> >
> > The script I am using is:
> >
> > ######################################################
> >
> > from diffpy.Structure import loadStructure
> > from diffpy.srreal.pdfcalculator import DebyePDFCalculator
> > from matplotlib.pyplot import plot, show
> >
> >
> > c60 = loadStructure('c60.stru')
> > dpc = DebyePDFCalculator()
> > dpc.qmax = 20
> > dpc.rmax = 20
> > r3, g3 = dpc(c60, qmin=0)
> > r4, g4 = dpc(c60, qmin=1)
...


Hi Hyunjeong,

Another option of saving the text data is to use the numpy.savetxt
function, which saves 1D or 2D arrays to a text file. The r3, g3
variables returned from dpc(c60, ...) are both 1D numpy arrays so we
need to join them as columns in a 2D array data and than use it with
savetxt:

data = numpy.column_stack([r3, g3])
numpy.savetxt('c60.dat', data)

This will write a c60.dat text file in the working directory of your
Python script. The numpy.savetxt function can take several optional
arguments, for example

numpy.savetxt('c60.dat', data, header='r g')

would start the text file with a "# r g" header line and

numpy.savetxt('c60.dat', data, fmt='%g')

would write values with a compact 6-digit precision. For a complete
description of numpy.savetxt, see
http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.savetxt.html#numpy.savetxt

Hope this helps,

Pavol

--
Dr. Pavol Juhas
Condensed Matter Physics and Materials Science Department
building 510B

hy...@hotmail.com

unread,
Dec 11, 2015, 9:00:13 AM12/11/15
to diffpy-users, pju...@bnl.gov

Thank you, Simon and Pavol.
It works!

Frederick Michel

unread,
May 15, 2021, 11:34:29 AM5/15/21
to diffpy-users
Hi Pavol,
A newbie question:
Should I copy-and-paste the text you have into the same script that is used to calculate the pdf for c60 (as an example)? In fact, I tried doing this already and get the following error:

Traceback (most recent call last):

  File "c60.py", line 18, in <module>

    data = numpy.column_stack([r3, g3])

NameError: name 'numpy' is not defined


Thanks,
Marc

Andy Anker

unread,
May 16, 2021, 5:50:15 AM5/16/21
to diffpy-users
Hi Marc,

Your error is caused because you have not imported numpy yet (or you have defined it differently).
Insert "Import numpy" in the top of your script and my guess is that it works.

Best
Andy

Reply all
Reply to author
Forward
0 new messages