0

I have a trajectory file from a molecular simulation that is written in netCDF format. I would like to convert this file to .csv format so that I can apply further Python-based analysis of the proximity between molecules. The trajectory file contains information corresponding to 3D Cartesian coordinates for all 6500 atoms of my simulation for each time step.

I have used the below script to convert this netCDF file to a .csv file using the netCDF4 and Pandas modules. My code is given below.

import netCDF4
import pandas as pd

fp='TEST4_simulate1.traj'
dataset = netCDF4.Dataset(fp, mode='r')
cols = list(dataset.variables.keys())
list_dataset = []
for c in cols:
    list_dataset.append(list(dataset.variables[c][:]))
#print(list_dataset)
df_dataset = pd.DataFrame(list_dataset)
df_dataset = df_dataset.T
df_dataset.columns = cols
df_dataset.to_csv("file_path.csv", index = False)

A small selection of the output .csv file is given below. Notice that a set of ellipses are given between the first and last set of 3 atomic coordinates.

time,spatial,coordinates
12.0,b'x',"[[  33.332325 -147.24976  -107.131   ]
 [  34.240444 -147.80115  -107.4043  ]
 [  33.640083 -146.47362  -106.41945 ]
 ...
 [  70.31757   -16.499006 -186.13313 ]
 [  98.310844   65.95696    76.43664 ]
 [  84.08772    52.676186  145.48856 ]]"

How can I modify this code so that the entirety of my atomic coordinates are written to my .csv file?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
  • Maybe it's just a bad idea to save three dimensional data as csv. – Joooeey May 05 '22 at 14:11
  • Does this answer your question? [Is there a concise way to show all rows in pandas for just the current command?](https://stackoverflow.com/questions/30876193/is-there-a-concise-way-to-show-all-rows-in-pandas-for-just-the-current-command) – Joooeey May 05 '22 at 14:12

0 Answers0