Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. import string
  4. from operator import add
  5. from math import floor
  6.  
  7.  
  8. class MyFormatter(string.Formatter):
  9. def format_field(self, value, format_spec):
  10. #print(value)
  11. #print(format_spec)
  12. ss = string.Formatter.format_field(self,value,format_spec)
  13. a,b = format_spec.split('.');
  14. newA = str(int(a)-1)
  15. #print(a)
  16. #print(newA)
  17. newSpec = "{}.{}".format(newA,b)
  18. #print(newSpec)
  19. ssNew = string.Formatter.format_field(self,value,newSpec)
  20. #print(ssNew)
  21. if format_spec.endswith('E'):
  22. if ( 'E' in ss):
  23. mantissa, exp = ss.split('E')
  24. num=int(exp[1:])
  25. if num >= 0 and num < 100:
  26. mantissa, exp = ssNew.split('E')
  27. return mantissa + 'E' + exp[0] + '0' + exp[1:]
  28. elif num > 100:
  29. return mantissa + 'E' + exp
  30. else :
  31. print("the exponential out of range")
  32. return mantissa + 'E' + exp
  33. return ss
  34.  
  35.  
  36. def writeData(filename,data):
  37. xmin=1
  38. ymin=1
  39. zmin=1
  40. xmax=data.shape[0]+xmin-1
  41. ymax=data.shape[1]+ymin-1
  42. zmax=data.shape[2]+zmin-1
  43. file=open(filename,"wb")
  44. dimension="{:6d}{:6d}{:6d}".format(xmax-xmin+1,ymax-ymin+1,zmax-zmin+1)
  45. file.write(dimension+" \n")
  46. FORMAT='{0:16.7E}'
  47. for i in range(xmin-1,xmax):
  48. for j in range(ymin-1,ymax):
  49. for k in range(zmin-1,zmax):
  50. position="{:6d}{:6d}{:6d}".format(i+2-xmin,j+2-ymin,k+2-zmin)
  51. value='{:>16}{:>16}{:>16}'.format(MyFormatter().format(FORMAT,data[i,j,k,0]),MyFormatter().format(FORMAT,data[i,j,k,1]),MyFormatter().format(FORMAT,data[i,j,k,2]))
  52. file.write('{}{}{} \n'.format(position,value,value))
  53. file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement