def parse(filename=nil)

      self.clear # Needed if you parse multiple times
      @messages = []
      @valid = false

      if filename.nil?
        filename = @filename
      end


      filename = File.realpath(filename)
      @filename = filename
      if filename.nil? or not File.exists? filename
        raise ArgumentError, "Missing ARGOS DS file: \"#{filename}\""
      end
      @sha1 = Digest::SHA1.file(filename).hexdigest

      contact = []
      file = File.open(filename)
      @filesize = file.size
      @updated = file.mtime.utc

      log.debug "Parsing ARGOS DS file #{filename} source:#{sha1} (#{filesize} bytes)"
      if filter?
        log.debug "Using filter: #{@filtername.nil? ? filter : @filtername }"
      end

      firstline = file.readline
      file.rewind

      if firstline =~ START_REGEX_LEGACY
        return parse_legacy(file)
      end

      file.each_with_index do |line, c|
        line = line.strip

        #if (c+1) % 1000 == 0
        #  log.debug "Line: #{c+1}"
        #end

        if line =~ START_REGEX

          @valid = true

          if contact.any?
            item = parse_message(contact)

            if self.class.valid_item? item

              if not filter? or filter.call(item)
                @messages << item
              end

            else
              raise "Argos DS message #{filename}:#{c} lacks required program and/or platform"
            end
          end

          contact = [line]

        else
          # 2010-12-14 15:11:34  1         00           37           01           52
          if contact.any? and line != ""
            contact << line
          end
        end
      end

      if false == @valid
        #log.debug file.read
        message = "Cannot parse file: #{filename}"
        raise ArgumentError, message
      end

      last = parse_message(contact)

      # The last message
      if last
        if not filter? or filter.call(last)
          @messages << last
        end
      end

      log.debug "Parsed #{@messages.size} Argos DS messages into #{self.class.name} Array"
      @segments = @messages.size
      unfold.each do |d|
        self << d
      end

      @multiplicates = group_by { |e| e }.select { |k, v| v.size > 1 }.map(&:first)
      if multiplicates.any?
        log.warn "#{multiplicates.size} multiplicates in source sha1 #{sha1} #{filename}): #{multiplicates.map {|a|a[:id]} }"
        self.uniq!
        log.info "Unique DS messages: #{self.size} sha1: #{sha1} #{filename}"
      end
      self.sort_by! {|ds| ds[:measured]}
      self
    end
def parse(filename=nil)

      self.clear # Needed if you parse multiple times
      @messages = []
      @valid = false

      if filename.nil?
        filename = @filename
      end


      filename = File.realpath(filename)
      @filename = filename
      if filename.nil? or not File.exists? filename
        raise ArgumentError, "Missing ARGOS DS file: \"#{filename}\""
      end
      @sha1 = Digest::SHA1.file(filename).hexdigest

      contact = []
      file = File.open(filename)
      @filesize = file.size
      @updated = file.mtime.utc

      log.debug "Parsing ARGOS DS file #{filename} source:#{sha1} (#{filesize} bytes)"
      if filter?
        log.debug "Using filter: #{@filtername.nil? ? filter : @filtername }"
      end

      firstline = file.readline
      file.rewind

      if firstline =~ START_REGEX_LEGACY
        return parse_legacy(file)
      end

      file.each_with_index do |line, c|
        line = line.strip

        #if (c+1) % 1000 == 0
        #  log.debug "Line: #{c+1}"
        #end

        if line =~ START_REGEX

          @valid = true

          if contact.any?
            item = parse_message(contact)

            if self.class.valid_item? item

              if not filter? or filter.call(item)
                @messages << item
              end

            else
              raise "Argos DS message #{filename}:#{c} lacks required program and/or platform"
            end
          end

          contact = [line]

        else
          # 2010-12-14 15:11:34  1         00           37           01           52
          if contact.any? and line != ""
            contact << line
          end
        end
      end

      if false == @valid
        #log.debug file.read
        message = "Cannot parse file: #{filename}"
        raise ArgumentError, message
      end

      last = parse_message(contact)

      # The last message
      if last
        if not filter? or filter.call(last)
          @messages << last
        end
      end

      log.debug "Parsed #{@messages.size} Argos DS messages into #{self.class.name} Array"
      @segments = @messages.size
      unfold.each do |d|
        self << d
      end

      @multiplicates = group_by { |e| e }.select { |k, v| v.size > 1 }.map(&:first)
      if multiplicates.any?
        log.warn "#{multiplicates.size} multiplicates in source sha1 #{sha1} #{filename}): #{multiplicates.map {|a|a[:id]} }"
        self.uniq!
        log.info "Unique DS messages: #{self.size} sha1: #{sha1} #{filename}"
      end
      self.sort_by! {|ds| ds[:measured]}
      self
    end
Parses Argos DS file and returns Argos::Ds -> Array

 The parser loops all messages (stored in @messages), before #unfold
 creates a sorted Array of measurements

@param filename [String] Filename of Argos DS file
@return [Argos::Ds]