 
 (defn low-res-sample
   "sample values off of a lower resolution grid than the MODIS grid."
-  [dataset ascii-info mod-source grid-source]
+  [dataset m-res ascii-info pixel-tap grid-source]
   (<- [?dataset ?m-res ?t-res ?mod-h ?mod-v ?sample ?line ?outval]
       (grid-source ?row ?col ?outval)
       (identity dataset :> ?dataset)
       (identity "00" :> ?t-res)
-      (mod-source ?m-res ?mod-h ?mod-v ?sample ?line)
-      (modis-sample ascii-info ?m-res ?mod-h ?mod-v ?sample ?line :> ?row ?col)))
+      (pixel-tap ?mod-h ?mod-v ?sample ?line)
+      (modis-sample ascii-info m-res ?mod-h ?mod-v ?sample ?line :> ?row ?col)))
 
 ;; TODO: Right now, we use the 0.1 as an indicator for conversion
 ;; direction, between modis and wgs84. Replace with a calculation.

  aggregator (sum, max, etc.).  If lower, each MODIS pixel is tagged with the ASCII
  grid cell that it falls within.  Note that the ASCII grid must be in WGS84, with row
  indices prepended and the ASCII header lopped off."
  ([m-res dataset tile-seq ascii-path & [agg]]
     (let [ascii-info ((keyword dataset) datasets)
           grid-source (ascii-source ascii-path)]
       (if (>= (:cellsize ascii-info) 0.01)
         (low-res-sample dataset ascii-info (mod-pixels tile-seq) grid-source)
         (high-res-sample dataset m-res grid-source ascii-info agg)))))

;; THE JOB!


(defn static-chunker
  "Last thing we need is the chunk-tap!"
  [m-res tile-seq dataset agg ascii-path output-path]
  (io/with-fs-tmp [fs tmp-dir]
    (let [pix-tap (pixel-generator tmp-dir m-res tile-seq)
          src (sample-modis m-res dataset pix-tap ascii-path agg)]
      (?- (chunk-tap output-path)
          (sparse-windower (tilestringer src) ["?sample" "?line"] "?val" 1200 20 0)))))

(defn s3-path [path]
  (str "s3n://AKIAJ56QWQ45GBJELGQA:6L7JV5+qJ9yXz1E30e3qmm4Yf7E1Xs4pVhuEL8LV@" path))

(def country-tiles [[27 7] [27 8] [27 9] [28 7] [28 8] [28 9] [29 8] [29 9] [30 8] [30 9] [31 8] [31 9]])

(defn -main
  [dataset ascii-path output-path]
  (static-chunker "1000"
                  country-tiles
                  dataset
                  c/sum
                  ascii-path
                  (s3-path output-path)))
