 (ns cmr.sizing.core
   (:require
+    [clojure.string :as string]
     [cmr.exchange.common.results.core :as results]
     [cmr.exchange.common.results.errors :as errors]
     [cmr.exchange.common.results.warnings :as warnings]

           (log/error estimate-or-errs)
           estimate-or-errs)
         (do
           (log/debug "Generated URLs:" (vec estimate-or-errs))
           (results/create estimate-or-errs :elapsed (util/timed start)
                                            :warnings warns)))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;   API   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;   Support & Utility Functions   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn get-measurement
  [variable]
  (let [dims (get-in variable [:umm :Dimensions])
        total-dimensionality (reduce * (map :Size dims))
        data-type (get-in variable [:umm :DataType])]
    (* total-dimensionality
       (util/data-type->bytes data-type))))

(defn estimate-binary-size
  [granule-count variables]
  (let [compression 1
        metadata 0
        measurements (reduce + (map get-measurement variables))]
    (+ (* granule-count compression measurements) metadata)))

(defn- -estimate-size
  [fmt granule-count vars]
  (case (keyword (string/lower-case fmt))
    :nc (estimate-binary-size granule-count vars)
    (do
      (log/errorf "Cannot estimate size for %s (not implemented)." fmt)
      {:errors ["not-implemented"]})))

;; XXX This function is nearly identical to one of the same name in
;;     cmr.ous.common -- we should put this somewhere both can use,
