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