Ran update-copyright.py
[hooke.git] / contrib / mfp_igor_scripts / h5export.py
1 # Copyright (C) 2010-2012 Alberto Gomez-Casado <a.gomezcasado@tnw.utwente.nl>
2 #                         W. Trevor King <wking@tremily.us>
3 #
4 # This file is part of Hooke.
5 #
6 # Hooke is free software: you can redistribute it and/or modify it under the
7 # terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option) any
9 # later version.
10 #
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
18
19 import h5py
20 import numpy
21 import os
22 import sys
23
24 h5file=os.path.realpath(sys.argv[-1])
25 h5dir=os.path.dirname(h5file)
26
27 f=h5py.File(h5file)
28
29 exportdir=os.path.join(h5dir,'exported')
30 try:
31         os.mkdir(exportdir)
32 except:
33         print 'mkdir error, maybe the export directory already exists?'
34
35 def h5exportfunc(name):
36         Deflname=name
37         if Deflname.endswith('Defl'):      #search for _Defl dataset            
38                 LVDTname=str.replace(Deflname,'Defl','LVDT')  #and correspondant LVDT dataset
39                 Defldata=f[Deflname][:]   #store the data in local var
40                 LVDTdata=f[LVDTname][:]
41                 #find useful attr (springc)
42                 try:
43                         notes=f[Deflname].attrs['IGORWaveNote']
44                         springmatch=notes.index("SpringConstant: ")+len("SpringConstant: ")
45                         springc=notes[springmatch:].split("\r",1)[0]  #probably extracting the leading numbers can be way more elegant than this
46                         print Deflname  
47                 except:
48                         print 'Something bad happened with '+Deflname+', ignoring it'
49                         return None
50                         #returning anything but None halts the visit procedure
51                 
52                 fp=open(os.path.join(exportdir,name.replace('/',''))+'.txt','w')  
53                 #uses the full HDF5 path (slashes out) to avoid potential overwriting             
54                 #write attr
55                 fp.writelines("IGP-HDF5-Hooke\n")
56                 fp.writelines('SpringConstant: '+springc+'\n\n')
57                 fp.writelines('App x\tApp y\tRet x\tRet y\n')
58                 #write LVDT and Defl data
59                 half=Defldata.size/2
60                 for i in numpy.arange(0,half):
61                         fp.writelines(str(LVDTdata[i])+'\t'+str(Defldata[i])+'\t'+str(LVDTdata[i+half])+'\t'+str(Defldata[i+half])+'\n')        
62                 #close the file
63                 fp.close()
64                 return None
65
66
67 f.visit(h5exportfunc)