1 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE StandaloneDeriving #-}
4 {-# LANGUAGE UnicodeSyntax #-}
13 import Data
.Text
( unlines, pack
)
14 import System
.Exit
( ExitCode( ExitSuccess
) )
15 import System
.FilePath.Posix
( replaceExtension
)
17 import Hkl
.DataSource
( DataItem
( DataItemH5
) )
18 import Hkl
.Nxs
( Nxs
( Nxs
)
20 , DataFrameH5Path
( XrdFlatH5Path
)
22 import Hkl
.Python
( PyVal
25 import Hkl
.Script
( Py2
26 , Script
( Py2Script
)
33 FlatNpy ∷
FilePath → Flat Npy
34 deriving instance (Show) (Flat a
)
36 scriptPy2Flat ∷
[Nxs XrdFlat
] →
FilePath → Script Py2
37 scriptPy2Flat ns output
= Py2Script
(script
, scriptName
)
39 script
= Data
.Text
.unlines $
40 map pack
["#!/bin/env python"
43 , "from h5py import File"
45 , "NEXUSFILES = " ++ toPyVal nxs
'
46 , "IMAGEPATHS = " ++ toPyVal hpaths
47 , "OUTPUT = " ++ toPyVal output
51 , "with File(NEXUSFILES[0], mode='r') as f:"
52 , " imgs = f[IMAGEPATHS[0]]"
53 , " flat = numpy.sum(imgs[:], axis=0)"
54 , " n = imgs.shape[0]"
55 , "for idx, (nxs, h5path) in enumerate(zip(NEXUSFILES[1:], IMAGEPATHS[1:])):"
56 , " with File(nxs, mode='r') as f:"
58 , " flat += numpy.sum(imgs[:], axis=0)"
59 , " n += imgs.shape[0]"
60 , "numpy.save(OUTPUT, flat.astype('f') / n)"
63 nxs
' = [f |
(Nxs f _
) ← ns
]
66 hpaths
= [h |
(Nxs _
(XrdFlatH5Path
(DataItemH5 h _
))) ← ns
]
69 scriptName
= output `replaceExtension`
"py"
71 computeFlat ∷
[Nxs XrdFlat
] →
FilePath →
IO (Flat Npy
)
73 -- create the python script.
74 let script
= scriptPy2Flat ns o
75 -- execute this script.
76 ExitSuccess ← run script
False
77 -- return the filepath of the generated file.
80 instance PyVal
(Flat a
) where
81 toPyVal
(FlatNpy v
) = "numpy.load(" ++ show v
++ ")"