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