Author Topic: Exporting electron density etc to XYZ file for plotting in other software  (Read 25834 times)

0 Members and 1 Guest are viewing this topic.

Offline serhan

  • Heavy QuantumATK user
  • ***
  • Posts: 98
  • Reputation: 1
    • View Profile
Thank you very much.

Cheers,
Serhan

Offline serhan

  • Heavy QuantumATK user
  • ***
  • Posts: 98
  • Reputation: 1
    • View Profile
Hi all, I'm trying to export electron density to file using the script given in this thread. I need to extract electron densities of lots of .nc files. If the exportED.py is like this
Code
scf=restoreSelfConsistenCalculation('/home/master/atk/1.nc')
electron_density=calculateElectronDensity(scf)
from export3d import *
export3D(electron_density)
And I execute this file as /home/master/atk/atk /home/master/atk/exportED.py > /home/master/atk/electronDensity1.dat & then, a file named electrondensity1.dat is successfully created in the directory. The code is excellent. However, I have dozens of .nc files of which the electron densities are to be extracted as: 1.nc -> electronDensity1.dat 2.nc -> electronDensity2.dat . . . etc. How can I achieve this as a batch mode instead of executing /home/master/atk/atk /home/master/atk/exportED.py > /home/master/atk/electronDensity1.dat & (when 1.nc is pointed in exportED.py) /home/master/atk/atk /home/master/atk/exportED.py > /home/master/atk/electronDensity2.dat & (when 2.nc is pointed in exportED.py) . . etc. Any help is greatly appreciated  ;) Cheers, Serhan
« Last Edit: July 14, 2009, 21:08 by serhan »

Offline Nordland

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 812
  • Reputation: 18
    • View Profile
Dear Serhan.

I would do it like this.

Quote
import glob
import sys
from export3d import *
nc_files = glob.glob('*.nc')

for nc_file in nc_files:
    sys.stdout = file('electronDensity'+nc_file[:-3]+'.dat','w')
    scf=restoreSelfConsistenCalculation(nc_file)
    electron_density=calculateElectronDensity(scf)
    export3D(electron_density)
    sys.stdout.close()

Copy this script (call it serhan.py ) into the folder with all the nc-files in, and run it using atk serhan.py in that folder,
and wolla! :) No shell scripts needed :)

Edit: Ups - correct an error in the script.
« Last Edit: July 16, 2009, 13:19 by Nordland »

Offline Anders Blom

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 5411
  • Country: dk
  • Reputation: 89
    • View Profile
    • QuantumATK at Synopsys
There are a couple of ways to approach this. You can handle the many files within the script itself, using the "glob" module. This is a bit more complex, however, so I'd recommend using simple bash looping. To do this, first we need to modify the code slightly, so that we can pass the name of the NC file on the command line:
Code
import sys
scf=restoreSelfConsistenCalculation(sys.argv[2])
Then, we execute the script as
Quote
for i in *.nc ; do atk exportED.py $i > electronDensity$i.dat ; done
You need to add paths etc. This has the slight disadvantage that the files will be called "electronDensity1.nc.dat". If you can live with that, all ok. If you prefer the filename "electronDensity1.dat", we just need to change it a bit:
Quote
for i in *.nc ; do j=`echo $i | sed 's/.nc//g'` ; atk exportED.py $i > $j.dat ; done

Offline serhan

  • Heavy QuantumATK user
  • ***
  • Posts: 98
  • Reputation: 1
    • View Profile
Thank you very much for your answers. I could do it now.

Cheers,
Serhan

Offline Nordland

  • QuantumATK Staff
  • Supreme QuantumATK Wizard
  • *****
  • Posts: 812
  • Reputation: 18
    • View Profile
Serhan, I have correct a bug in my script, if you are using my script, look above for the modification.