f49b3a8df49d895170da7cc1bfefb387dcf3e49b
[hooke.git] / contrib / mfp_igor_scripts / h5export.py
1 import h5py
2 import numpy
3 import os
4 import sys
5
6 h5file=os.path.realpath(sys.argv[-1])
7 h5dir=os.path.dirname(h5file)
8
9 f=h5py.File(h5file)
10
11 exportdir=os.path.join(h5dir,'exported')
12 try:
13         os.mkdir(exportdir)
14 except:
15         print 'mkdir error, maybe the export directory already exists?'
16
17 def 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
49 f.visit(h5exportfunc)