 (defn jvm-time-ms []
   (quot (System/nanoTime) 1000000))
 
-(defn compute-time-from-core [tags]
-  (when (@state :burst-init-time)
-    (when-let [t (tags "ElapsedTime-ms")]
-      (log "ElapsedTime-ms" t)
-      (+ (Double/parseDouble t)
-         (@state :burst-init-time)))))
-
 (defn elapsed-time [state]
   (if (state :start-time) (- (jvm-time-ms) (state :start-time)) 0))
+
+(defn core-time-from-tags [tags]
+  (try (Double/parseDouble (tags "ElapsedTime-ms")) (catch Exception e nil)))
+
+(defn burst-time [tags state]
+  (when (and (:burst-time-offset state) (get tags "ElapsedTime-ms"))
+    (+ (core-time-from-tags tags)
+       (:burst-time-offset state))))
 
 ;; image metadata
 

              0))
         z-ref))))

(defn store-new-z-reference []
  (let [z-drive (@state :default-z-drive)]
    (when (and (or (not (core isContinuousFocusEnabled))
                   (core isContinuousFocusDrive z-drive))
               (not (z-in-msp z-drive)))
      (state-assoc! :reference-z (get-z-stage-position z-drive)))))

;; startup and shutdown

    (Thread/sleep 5))
  (let [md (Metadata.)
        pix (core popNextImageMD md)
        tags (parse-core-metadata md)]
    {:pix pix :tags (dissoc tags "StartTime-ms")}))
    
(defn collect-burst-images [event out-queue]
  (when (first-trigger-missing?) (pop-burst-image)) ; drop first image if first trigger doesn't happen
  (swap! state assoc :burst-time-offset nil)
  (dorun
    (map-indexed
      (fn [i evt]
        (when-not (@state :stop)
          (let [image (pop-burst-image)]
            (when (zero? i)
              (swap! state assoc
                     :burst-time-offset (- (elapsed-time @state)
                                           (core-time-from-tags (image :tags)))))
            (.put out-queue (make-TaggedImage (annotate-image image evt @state))))
          (when (core isBufferOverflowed)
            (swap! state assoc :stop true)
            (ReportingUtils/showError "Circular buffer overflowed."))))
      (event :burst-data)))
  (while (and (not (@state :stop)) (. mmc isSequenceRunning))
    (Thread/sleep 5)))

