Module: Argos::Ascii

Included in:
Diag, Ds
Defined in:
lib/argos/ascii.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.argos?(filename) ⇒ Boolean

Argos ASCII file?

Parameters:

  • filename (String)

    Argos (DS or DIAG) file

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
# File 'lib/argos/ascii.rb', line 33

def self.argos?(filename)
  case type(filename)
  when "ds", "diag"
    true
  else
    false
  end
end

.factory(type) ⇒ Argos::Ds Argos::Diag

Factory for Argos::Ds / Argos::Diag

Parameters:

  • type (String)

    : Argos (DS or DIAG) file type (or filename)

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/argos/ascii.rb', line 47

def self.factory(type)
  
  # Auto-detect file format if not "ds" or "diag"
  if not ["ds","diag"].include? type
    if argos? type
      type = self.type(type)
    end
  end
  
  case type
  when "ds"
    Ds.new
  when "diag"
    Diag.new
  else
    raise ArgumentError, "Unknown Argos type: #{type}"
  end
end

.source(argos) ⇒ Hash

Source fingerprint of Argos file (sha1 hash, segment and document counts, etc.)

Parameters:

Returns:

  • (Hash)

    Source hash



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/argos/ascii.rb', line 70

def self.source(argos)
  
  argos.parse(argos.filename)
  
  latitude_mean = longitude_mean = nil
  if argos.latitudes.any?
    latitude_mean = (argos.latitudes.inject{ |sum, latitude| sum + latitude } / argos.latitudes.size).round(3)
  end
  if argos.longitudes.any?
    longitude_mean = (argos.longitudes.inject{ |sum, longitude| sum + longitude } / argos.latitudes.size).round(3)
  end
  
  
  source = {
    id: argos.source,
    technology: "argos",
    collection: "tracking",
    type: argos.type,
    programs: argos.programs,
    platforms: argos.platforms,
    start: argos.start,
    stop: argos.stop,
    north: argos.latitudes.max,
    east: argos.longitudes.max,
    south: argos.latitudes.min,
    west: argos.longitudes.min,
    latitude_mean: latitude_mean,
    longitude_mean: longitude_mean,
    file: "file://"+argos.filename,
    bytes: argos.filesize,
    modified: argos.updated.utc.iso8601,
    messages: argos.messages.size,
    filter: argos.filtername.nil? ? argos.filter : argos.filtername,
    size: argos.size,
    parser: Argos.library_version
  }
  if argos.multiplicates.any?
    source[:multiplicates] = argos.multiplicates.map {|a| a[:id]}
  end
  if argos.errors.any?
    source[:errors] = argos.errors
  end
  source
  
end

.type(filename) ⇒ String

Detect Argos ASCII filetype (“ds” or “diag” or nil)

“ds”|“diag”

Parameters:

  • filename (String)

    Argos (DS or DIAG) file

Returns:

  • (String)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/argos/ascii.rb', line 9

def self.type filename
  
  if File.file? filename
    # Avoid invalid byte sequence in UTF-8 (ArgumentError)
    firstline = File.open(filename, :encoding => "iso-8859-1") {|f| f.readline}
  else
    firstline = filename
  end
  
  case firstline
    when Argos::Ds::START_REGEX, Argos::Ds::START_REGEX_LEGACY
      "ds"
    when Argos::Diag::START_REGEX
      "diag"
    when "", nil
      raise ArgumentError, "Not a file or empty string: #{filename}"
    else nil
  end 
end

Instance Method Details

#latitudesObject



117
118
119
# File 'lib/argos/ascii.rb', line 117

def latitudes
  select {|a| a.key? :latitude and a[:latitude].is_a? Float }.map {|a| a[:latitude]}.sort
end

#longitudesObject



121
122
123
# File 'lib/argos/ascii.rb', line 121

def longitudes
  select {|a| a.key? :longitude and a[:longitude].is_a? Float }.map {|a| a[:longitude]}.sort
end

#platformsObject



125
126
127
# File 'lib/argos/ascii.rb', line 125

def platforms
  map {|a| a[:platform]}.uniq.sort
end

#programsObject



129
130
131
# File 'lib/argos/ascii.rb', line 129

def programs
  map {|a| a[:program]}.uniq.sort
end