def unfold

      # First, grab all segments *without* measurements (if any)
      unfolded = messages.reject {|ds| ds.key?(:measurements) or ds[:measurements].nil? }.map {|m| m[:cardinality] = 0 }
      log.debug "#{messages.size - unfolded.size} / #{messages.size} messages contained measurements"

      messages.select {|ds|
        ds.key?(:measurements) and not ds[:measurements].nil?
      }.each do |ds|

        ds[:measurements].each_with_index do |measurement,cardinality|
          # Cardinality 0 will filter out repeated positions due to higher sensor sampling rate...
          unfolded << merge(ds,measurement, cardinality)
        end
      end

      unfolded = unfolded.sort_by {|ds|
        if not ds[:measured].nil?
          DateTime.parse(ds[:measured])
        elsif not ds[:positioned].nil?
          DateTime.parse(ds[:positioned])
        else
          ds[:program]
        end
      }

      log.info "Unfolded #{messages.size} ARGOS DS position and sensor messages into #{unfolded.size} new documents source:#{sha1} #{filename}"

      unfolded
    end
def unfold

      # First, grab all segments *without* measurements (if any)
      unfolded = messages.reject {|ds| ds.key?(:measurements) or ds[:measurements].nil? }.map {|m| m[:cardinality] = 0 }
      log.debug "#{messages.size - unfolded.size} / #{messages.size} messages contained measurements"

      messages.select {|ds|
        ds.key?(:measurements) and not ds[:measurements].nil?
      }.each do |ds|

        ds[:measurements].each_with_index do |measurement,cardinality|
          # Cardinality 0 will filter out repeated positions due to higher sensor sampling rate...
          unfolded << merge(ds,measurement, cardinality)
        end
      end

      unfolded = unfolded.sort_by {|ds|
        if not ds[:measured].nil?
          DateTime.parse(ds[:measured])
        elsif not ds[:positioned].nil?
          DateTime.parse(ds[:positioned])
        else
          ds[:program]
        end
      }

      log.info "Unfolded #{messages.size} ARGOS DS position and sensor messages into #{unfolded.size} new documents source:#{sha1} #{filename}"

      unfolded
    end
Merge position and all other top-level DS fields with each measurement line
 (containing sensor data)
 The 3 lines below will unfold to *2* documents, each with
 "positioned":2010-03-05T14:19:06Z, "platform": "23695", "latitude":"79.989", etc.
 23695 074772   3  4 M B 2010-03-05 14:19:06  79.989   12.644  0.036 401639707
       2010-03-05 14:17:35  1         01           25        37630           36
       2010-03-05 14:20:38  1         00           28           00           65