1 {-# LANGUAGE DuplicateRecordFields #-}
2 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE OverloadedStrings #-}
6 Copyright : Copyright (C) 2014-2024 Synchrotron SOLEIL
7 L'Orme des Merisiers Saint-Aubin
8 BP 48 91192 GIF-sur-YVETTE CEDEX
11 Maintainer : Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
12 Stability : Experimental
13 Portability: GHC only (not tested)
15 module Hkl
.Binoculars
.Projections
29 import Control
.Monad
(zipWithM)
30 import Control
.Monad
.Catch
(MonadThrow
)
31 import Control
.Monad
.IO.Class
(MonadIO
(liftIO
))
32 import Control
.Monad
.Logger
(MonadLogger
, logDebugN
)
33 import Control
.Monad
.Trans
.Reader
(ReaderT
, runReaderT
)
34 import Data
.ByteString
(useAsCString
)
35 import Data
.Text
.Encoding
(encodeUtf8
)
36 import Foreign
.C
.String (CString
, withCString
)
37 import Foreign
.C
.Types
(CBool
, CSize
(..))
38 import Foreign
.ForeignPtr
(ForeignPtr
, newForeignPtr
,
40 import Foreign
.Marshal
.Alloc
(alloca
)
41 import Foreign
.Marshal
.Array (withArrayLen
)
42 import Foreign
.Ptr
(Ptr
, nullPtr
)
43 import Foreign
.Storable
(poke
)
44 import GHC
.Exts
(IsList
(..))
46 import Prelude
hiding (drop)
48 import Hkl
.Binoculars
.Config
53 import Hkl
.Utils
hiding (withCString
)
56 cmd
:: (HasIniConfig a
, ToIni
(Config a
), MonadLogger m
, MonadThrow m
, MonadIO m
)
57 => ReaderT
(Config a
) m
() -> Maybe FilePath -> Args a
-> m
()
60 content
<- liftIO
$ readConfig mf
61 capabilities
<- liftIO getCapabilities
62 let econf
= getConfig content args capabilities
65 logDebugN
"config red from the config file"
66 logDebugN
$ serializeConfig conf
67 runReaderT action conf
68 Left e
-> logErrorNSH e
73 withNPixels
:: Detector a DIM2
-> (CSize
-> IO r
) -> IO r
74 withNPixels d f
= f
(toEnum . size
. shape
$ d
)
76 withPixelsDims
:: Array F DIM3
Double -> (Int -> Ptr CSize
-> IO r
) -> IO r
77 withPixelsDims p
= withArrayLen
(map toEnum $ listOfShape
. extent
$ p
)
79 saveCube
:: Shape sh
=> FilePath -> String -> [Cube sh
] -> IO ()
80 saveCube o conf rs
= do
84 withCString o
$ \fn
->
85 withCString conf
$ \config
->
86 withForeignPtr fp
$ \p
->
87 c
'hkl_binoculars_cube_save_hdf5 fn config p
88 EmptyCube
-> return ()
90 newLimits
:: Limits
-> Double -> IO (ForeignPtr C
'HklBinocularsAxisLimits
)
91 newLimits
(Limits mmin mmax
) res
=
94 imin
'' <- case mmin
of
95 Nothing
-> pure nullPtr
97 poke imin
' (round (d
/ res
))
99 imax
'' <- case mmax
of
100 Nothing
-> pure nullPtr
102 poke imax
' (round (d
/ res
))
104 newForeignPtr p
'hkl_binoculars_axis_limits_free
105 =<< c
'hkl_binoculars_axis_limits_new imin
'' imax
''
107 withMaybeLimits
:: Shape sh
108 => Maybe (RLimits sh
)
110 -> (Int -> Ptr
(Ptr C
'HklBinocularsAxisLimits
) -> IO r
)
112 withMaybeLimits mls rs f
= do
114 Nothing
-> f
0 nullPtr
116 ptrs
<- zipWithM newLimits
(toList ls
) (toList rs
)
117 withForeignPtrs ptrs
$ \pts
-> withArrayLen pts f
119 withMaybeMask
:: Maybe Mask
-> (Ptr CBool
-> IO r
) -> IO r
120 withMaybeMask mm f
= case mm
of
122 (Just m
) -> withForeignPtr
(toForeignPtr m
) $ \ptr
-> f ptr
124 withMaybeSampleAxis
:: Maybe SampleAxis
-> (CString
-> IO r
) -> IO r
125 withMaybeSampleAxis Nothing f
= f nullPtr
126 withMaybeSampleAxis
(Just a
) f
= withSampleAxis a f
128 withResolutions
:: Shape sh
=> Resolutions sh
-> (Int -> Ptr
Double -> IO r
) -> IO r
129 withResolutions
= withArrayLen
. toList
131 withSampleAxis
:: SampleAxis
-> (CString
-> IO r
) -> IO r
132 withSampleAxis
(SampleAxis t
) = useAsCString
(encodeUtf8 t
)
138 newtype Space sh
= Space
(ForeignPtr C
'HklBinocularsSpace
)
141 newSpace
' :: Ptr C
'HklBinocularsSpace
-> IO (Space sh
)
142 newSpace
' p
= Space
<$> newForeignPtr p
'hkl_binoculars_space_free p
144 newSpace
:: (Shape sh1
, Shape sh
) => Detector a sh1
-> Int -> IO (Space sh
)
146 let nPixels
= toEnum . size
. shape
$ d
148 newSpace
' =<< c
'hkl_binoculars_space_new nPixels nDims