Class: Argos::Ds
Overview
Constant Summary collapse
- START_REGEX =
/^\d{5} \d{5,6} +\d+ +\d+/
- START_REGEX_LEGACY =
/\s+\d\.\d{3}\s\d{9}\s+\w{4}$/
- LOCATION_CLASS =
[nil, "0","1","2","3","A","B","G","Z"]
Instance Attribute Summary collapse
-
#bundle ⇒ Object
Returns the value of attribute bundle.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#filesize ⇒ Object
readonly
Returns the value of attribute filesize.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#filtername ⇒ Object
readonly
Returns the value of attribute filtername.
- #log ⇒ Object
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#multiplicates ⇒ Object
readonly
Returns the value of attribute multiplicates.
-
#sha1 ⇒ Object
readonly
Returns the value of attribute sha1.
-
#updated ⇒ Object
readonly
Returns the value of attribute updated.
-
#valid ⇒ Object
readonly
Returns the value of attribute valid.
Instance Method Summary collapse
-
#combine_header_with_transmission(measurements, header) ⇒ Object
Header is is a space-separated string containing [0] Program number [1] Platform number [2] Number of lines of data per satellite pass [3] Number of sensors [4] Satellite identifier [5] Location class (lc) [6] Location date 2007-03-02 [7] Location UTC time [8] Latitude (decimal degrees) [9] Longitude, may be > 180 like 255.452°, equivalent to 255.452 - 360 = -104.548 (°E) [10] Altitude (km) [11] Frequency (calculated).
- #filter? ⇒ Boolean
-
#initialize ⇒ Ds
constructor
A new instance of Ds.
-
#merge(ds, measurement) ⇒ Array
Merges a DS header hash into each measurement.
-
#parse(filename = nil) ⇒ Argos::Ds
Parses Argos DS file and returns Argos::Ds -> Array.
-
#parse_message(contact) ⇒ Object
Pare one DS segment.
- #process_item_body(body_arr) ⇒ Object
-
#recursive_transmission_parse(body_arr) ⇒ Aray
@param [Array] body_arr.
- #source ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #transmission_package(data) ⇒ Object
- #type ⇒ Object
-
#unfold ⇒ Object
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.
Methods included from Ascii
argos?, factory, #latitudes, #longitudes, #platforms, #programs, source, type
Constructor Details
#initialize ⇒ Ds
Returns a new instance of Ds.
26 27 28 29 |
# File 'lib/argos/ds.rb', line 26 def initialize @errors = [] @log = Logger.new(STDERR) end |
Instance Attribute Details
#bundle ⇒ Object
Returns the value of attribute bundle.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def bundle @bundle end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def errors @errors end |
#filename ⇒ Object
Returns the value of attribute filename.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def filename @filename end |
#filesize ⇒ Object (readonly)
Returns the value of attribute filesize.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def filesize @filesize end |
#filter ⇒ Object
Returns the value of attribute filter.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def filter @filter end |
#filtername ⇒ Object (readonly)
Returns the value of attribute filtername.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def filtername @filtername end |
#log ⇒ Object
44 45 46 47 48 49 |
# File 'lib/argos/ds.rb', line 44 def log if @log.nil? @log = Logger.new(STDERR) end @log end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def @messages end |
#multiplicates ⇒ Object (readonly)
Returns the value of attribute multiplicates.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def multiplicates @multiplicates end |
#sha1 ⇒ Object (readonly)
Returns the value of attribute sha1.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def sha1 @sha1 end |
#updated ⇒ Object (readonly)
Returns the value of attribute updated.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def updated @updated end |
#valid ⇒ Object (readonly)
Returns the value of attribute valid.
18 19 20 |
# File 'lib/argos/ds.rb', line 18 def valid @valid end |
Instance Method Details
#combine_header_with_transmission(measurements, header) ⇒ Object
Header is is a space-separated string containing
[0] Program number
[1] Platform number
[2] Number of lines of data per satellite pass
[3] Number of sensors
[4] Satellite identifier
[5] Location class (lc)
[6] Location date 2007-03-02
[7] Location UTC time
[8] Latitude (decimal degrees)
[9] Longitude, may be > 180 like 255.452°, equivalent to 255.452 - 360 = -104.548 (°E)
[10] Altitude (km)
[11] Frequency (calculated)
The header varies in information elemenet, often either 0..4|5 or 0..11. Header examples (plit on “ ”):
["09660", "10788", "4", "3", "D", "0"]
["09660", "10788", "5", "3", "H", "2", "1992-04-06", "22:12:16", "78.248", "15.505", "0.000", "401649604"]
["09660", "10788", "2", "3", "D"]
www.argos-system.org/files/pmedia/public/r363_9_argos_users_manual-v1.5.pdf page 42
Warning, the parser does not support this header format from 1989 [AUO89.DAT] 19890800-19891000: [“09660”, “14653”, “10”, “41”, “14”, “1”, “-.42155E+1”, “00”, “112”, “17DD”]
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/argos/ds.rb', line 195 def combine_header_with_transmission(measurements, header) unless header.is_a? Array header = header.split(" ") end latitude = longitude = positioned = nil warn = [] errors = [] lc = header[5] if not header[6].nil? and not header[7].nil? positioned = convert_datetime(header[6]+" "+header[7]) end if header[8] != nil && valid_float?(header[8]) latitude = header[8].to_f end if header[9] != nil && valid_float?(header[9]) longitude = header[9].to_f if (180..360).include? longitude longitude = (longitude - 360) end end altitude = header[10] if not altitude.nil? altitude = altitude.to_f*1000 end if positioned.nil? and measurements.nil? warn << "missing-time" end if latitude.nil? or longitude.nil? warn << "missing-position" else unless latitude.between?(-90, 90) and longitude.between?(-180, 180) errors << "invalid-position" end end unless LOCATION_CLASS.include? lc errors << "invalid-lc" end # Satellites # ["A", "B", "K", "L", "M", "N", "P", "R"] document = { program: header[0].to_i, platform: header[1].to_i, lines: header[2].to_i, sensors: header[3].to_i, satellite: header[4], lc: lc, positioned: positioned, latitude: latitude, longitude: longitude, altitude: altitude, measurements: measurements, headers: header.size } if warn.any? document[:warn]=warn end if errors.any? document[:errors]=errors end document end |
#filter? ⇒ Boolean
31 32 33 |
# File 'lib/argos/ds.rb', line 31 def filter? not @filter.nil? end |
#merge(ds, measurement) ⇒ Array
Merges a DS header hash into each measurement
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/argos/ds.rb', line 307 def merge(ds, measurement) m = ds.select {|k,v| k != :measurements and k != :errors and k != :warn } m = m.merge(measurement) m = m.merge ({ technology: "argos", type: type, file: "file://"+filename, source: sha1 }) if not ds[:errors].nil? and ds[:errors].any? m[:errors] = ds[:errors].clone end if not ds[:warn].nil? and ds[:warn].any? m[:warn] = ds[:warn].clone end if not m[:sensor_data].nil? and m[:sensor_data].size != ds[:sensors] if m[:warn].nil? m[:warn] = [] end m[:warn] << "sensors-count-mismatch" end # Create id as SHA1 hash of measurement minus stuff that may vary (like filename) # # Possible improvement for is to base id on a static list of keys # :program, # :platform, # :lines, # :sensors, # :satellite, # :lc, # :positioned, # :latitude, # :longitude, # :altitude, # :headers, # :measured, # :identical, # :sensor_data, # :technology, # :type, # :source idbase = m.clone idbase.delete :errors idbase.delete :file idbase.delete :warn id = Digest::SHA1.hexdigest(idbase.to_json) m[:parser] = Argos.library_version m[:id] = id m[:bundle] = bundle m end |
#parse(filename = nil) ⇒ Argos::Ds
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
58 59 60 61 62 63 64 65 66 67 68 69 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/argos/ds.rb', line 58 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 = (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 = "Cannot parse file: #{filename}" raise ArgumentError, end last = (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 |
#parse_message(contact) ⇒ Object
Pare one DS segment
160 161 162 163 164 165 |
# File 'lib/argos/ds.rb', line 160 def (contact) header = contact[0] body = contact[1,contact.count] items = process_item_body(body) combine_header_with_transmission(items, header) end |
#process_item_body(body_arr) ⇒ Object
365 366 367 368 369 |
# File 'lib/argos/ds.rb', line 365 def process_item_body(body_arr) @buf ="" @transmission_arr = [] @transmission_arr = recursive_transmission_parse(body_arr) end |
#recursive_transmission_parse(body_arr) ⇒ Aray
@param [Array] body_arr
374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/argos/ds.rb', line 374 def recursive_transmission_parse(body_arr) if body_arr.nil? or body_arr.empty? return end @buf =@buf + " " + body_arr[0] if body_arr[1] =~ /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/ or body_arr[1]==nil @transmission_arr << transmission_package(@buf) @buf="" end recursive_transmission_parse(body_arr[1,body_arr.length]) @transmission_arr end |
#source ⇒ Object
412 413 414 |
# File 'lib/argos/ds.rb', line 412 def source @sha1 end |
#start ⇒ Object
404 405 406 |
# File 'lib/argos/ds.rb', line 404 def start positioned.map {|ds| ds [:positioned] }.first end |
#stop ⇒ Object
408 409 410 |
# File 'lib/argos/ds.rb', line 408 def stop positioned.map {|ds| ds [:positioned] }.last end |
#transmission_package(data) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/argos/ds.rb', line 388 def transmission_package(data) transmission_time = data[/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/,1] transmission_time = convert_datetime(transmission_time) identical = data.split(" ")[2].to_i data = data.strip[23,data.length] if not data.nil? sensor_data = data.split(" ") end { measured: transmission_time, identical: identical, sensor_data: sensor_data } end |
#type ⇒ Object
167 168 169 |
# File 'lib/argos/ds.rb', line 167 def type "ds" end |
#unfold ⇒ Object
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
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/argos/ds.rb', line 275 def unfold # First, grab all segments *without* measurements (if any) unfolded = .reject {|ds| ds.key?(:measurements) or ds[:measurements].nil? } log.debug "#{.size - unfolded.size} / #{.size} messages contained measurements" .select {|ds| ds.key?(:measurements) and not ds[:measurements].nil? }.each do |ds| ds[:measurements].each do |measurement| unfolded << merge(ds,measurement) 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 #{.size} ARGOS DS position and sensor messages into #{unfolded.size} new documents source:#{sha1} #{filename}" unfolded end |