From 85aa6e376cd558e1b25ee998f4a54ff509e538f4 Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
Date: Sat, 27 Jan 2024 18:19:22 +0000
Subject: [PATCH] Define FixedBlock container

---
 src/MRIBuilder.jl              |  4 +--
 src/containers/containers.jl   |  2 ++
 src/containers/fixed_blocks.jl | 52 ++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 2 deletions(-)
 create mode 100644 src/containers/fixed_blocks.jl

diff --git a/src/MRIBuilder.jl b/src/MRIBuilder.jl
index cfe382e..dfca2d6 100644
--- a/src/MRIBuilder.jl
+++ b/src/MRIBuilder.jl
@@ -32,8 +32,8 @@ export ConcreteBlocks, AbstractConcreteBlock
 import .Wait: WaitBlock
 export WaitBlock
 
-import .Containers: Sequence
-export Sequence
+import .Containers: Sequence, FixedBlock
+export Sequence, FixedBlock
 
 import .Gradients: PulsedGradient, InstantGradientBlock, FixedGradient
 export PulsedGradient, InstantGradientBlock, FixedGradient
diff --git a/src/containers/containers.jl b/src/containers/containers.jl
index e134de9..28e1a9e 100644
--- a/src/containers/containers.jl
+++ b/src/containers/containers.jl
@@ -6,8 +6,10 @@ The most important of these is [`Sequence`](@ref).
 To get the children blocks use [`get_children_blocks`](@ref).
 """
 module Containers
+include("fixed_blocks.jl")
 include("sequences.jl")
 import ..BuildingBlocks: ContainerBlock, get_children_blocks
 
+import ..FixedBlocks: FixedBlock
 import .Sequences: Sequence
 end
\ No newline at end of file
diff --git a/src/containers/fixed_blocks.jl b/src/containers/fixed_blocks.jl
new file mode 100644
index 0000000..086f725
--- /dev/null
+++ b/src/containers/fixed_blocks.jl
@@ -0,0 +1,52 @@
+
+module FixedBlocks
+
+import ...BuildingBlocks: ContainerBlock
+import ...Gradients: FixedGradient
+import ...Pulses: FixedPulse
+import ...Variables: duration, variables
+
+"""
+    FixedBlock(duration=nothing; pulse_delay=0., pulse=nothing, gradient_delay=0., gradient=nothing, readout_times=[])
+
+Create a [`BuildingBlock`](@ref) object that has been fully defined.
+It can contain an RF pulse ([`FixedPulse`](@ref)), a gradient ([`FixedGradient`](@ref)), and any number of readouts.
+
+The pulse and gradient will play after their respective delays in ms (relative to the start of this block).
+
+If the `duration` is specified the block will last that long.
+Otherwise, the block will last as long as the latest block.
+"""
+struct FixedBlock <: ContainerBlock
+    duration :: Float64
+    pulse_delay :: Float64
+    pulse :: Union{Nothing, FixedPulse}
+    gradient_delay :: Float64
+    gradient :: Union{Nothing, FixedGradient}
+    readout_times :: Vector{Float64}
+    function FixedBlock(duration=nothing; pulse_delay=0., pulse=nothing, gradient_delay=0., gradient=nothing, readout_times=[])
+        @assert pulse_delay >= 0.
+        @assert gradient_delay >= 0.
+        if pulse isa Tuple
+            pulse = FixedPulse(pulse...)
+        end
+        if gradient isa Tuple
+            gradient = FixedGradient(gradient...)
+        end
+        real_duration = max(pulse_delay + duration(pulse), gradient_delay + duration(gradient), length(readout_times) == 0 ? 0. : maximum(readout_times))
+        if isnothing(duration)
+            duration = real_duration
+        else
+            @assert duration >= real_duration
+        end
+        new(duration, pulse_delay, pulse, gradient_delay, gradient, Float64.(readout_times))
+    end
+end
+
+duration(fb::FixedBlock) = fb.duration
+
+variables(::Type{<:FixedBlock}) = []
+
+
+
+end
\ No newline at end of file
-- 
GitLab