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