PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mfp_igor_scripts/h5export.py

http://hooke.googlecode.com/
Python | 49 lines | 42 code | 5 blank | 2 comment | 3 complexity | a5e674db8278ffbbaf32a8a43e315f3d MD5 | raw file
Possible License(s): LGPL-3.0
  1. import h5py
  2. import numpy
  3. import os
  4. import sys
  5. h5file=os.path.realpath(sys.argv[-1])
  6. h5dir=os.path.dirname(h5file)
  7. f=h5py.File(h5file)
  8. exportdir=os.path.join(h5dir,'exported')
  9. try:
  10. os.mkdir(exportdir)
  11. except:
  12. print 'mkdir error, maybe the export directory already exists?'
  13. def h5exportfunc(name):
  14. Deflname=name
  15. if Deflname.endswith('Defl'): #search for _Defl dataset
  16. LVDTname=str.replace(Deflname,'Defl','LVDT') #and correspondant LVDT dataset
  17. Defldata=f[Deflname][:] #store the data in local var
  18. LVDTdata=f[LVDTname][:]
  19. #find useful attr (springc)
  20. try:
  21. notes=f[Deflname].attrs['IGORWaveNote']
  22. springmatch=notes.index("SpringConstant: ")+len("SpringConstant: ")
  23. springc=notes[springmatch:].split("\r",1)[0] #probably extracting the leading numbers can be way more elegant than this
  24. print Deflname
  25. except:
  26. print 'Something bad happened with '+Deflname+', ignoring it'
  27. return None
  28. #returning anything but None halts the visit procedure
  29. fp=open(os.path.join(exportdir,name.replace('/',''))+'.txt','w')
  30. #uses the full HDF5 path (slashes out) to avoid potential overwriting
  31. #write attr
  32. fp.writelines("IGP-HDF5-Hooke\n")
  33. fp.writelines('SpringConstant: '+springc+'\n\n')
  34. fp.writelines('App x\tApp y\tRet x\tRet y\n')
  35. #write LVDT and Defl data
  36. half=Defldata.size/2
  37. for i in numpy.arange(0,half):
  38. fp.writelines(str(LVDTdata[i])+'\t'+str(Defldata[i])+'\t'+str(LVDTdata[i+half])+'\t'+str(Defldata[i+half])+'\n')
  39. #close the file
  40. fp.close()
  41. return None
  42. f.visit(h5exportfunc)