from Scientific.IO.NetCDF import NetCDFFile as Dataset import itertools as itx frame = 2000 inputfile = 'mdcrd' def grouper(n, iterable, fillvalue=None): args = [iter(iterable)] * n return itx.izip_longest(fillvalue=fillvalue, *args) formatxyz = "%12.7f%12.7f%12.7f%12.7f%12.7f%12.7f" formatxyz_size = 6 formatxyzshort = "%12.7f%12.7f%12.7f" formatxyzshort_size = 3 ncfile = Dataset(inputfile, 'r') variableNames = ncfile.variables.keys() #print variableNames shape = ncfile.variables['coordinates'].shape ''' do the header ''' print 'title ' + str(frame) print "%5i%15.7e" % (shape[1],ncfile.variables['time'][frame]) ''' do the xyz coordinates ''' xyz = ncfile.variables['coordinates'][frame] temp = grouper(2, xyz, "") for i in temp: z = tuple(itx.chain(*i)) if (len(z) == formatxyz_size): print formatxyz % z elif (len(z) == formatxyzshort_size): print formatxyzshort % z ''' do the velocities ''' try: xyz = ncfile.variables['velocities'][frame] temp = grouper(2, xyz, "") for i in temp: z = tuple(itx.chain(*i)) if (len(z) == formatxyz_size): print formatxyz % z elif (len(z) == formatxyzshort_size): print formatxyzshort % z except(KeyError): xyz = [0] * shape[2] xyz = [xyz] * shape[1] temp = grouper(2, xyz, "") for i in temp: z = tuple(itx.chain(*i)) if (len(z) == formatxyz_size): print formatxyz % z elif (len(z) == formatxyzshort_size): print formatxyzshort % z x = ncfile.variables['cell_angles'][frame] y = ncfile.variables['cell_lengths'][frame] ''' do cell lengths and angles ''' print formatxyz % tuple(itx.chain(y, x)) ncfile.close()