ruby-avm-library/lib/avm/observation.rb

76 lines
2.1 KiB
Ruby
Raw Normal View History

2011-03-15 20:56:27 +00:00
module AVM
class Observation
2011-03-16 14:22:59 +00:00
AVM_SINGLE_FIELDS = %w{Facility Instrument Spectral.ColorAssignment Spectral.Band Spectral.Bandpass Spectral.CentralWavelength Temporal.StartTime Temporal.IntegrationTime DatasetID}
AVM_SINGLE_METHODS = [ :facility, :instrument, :color_assignment, :band, :bandpass, :wavelength, :string_start_time, :integration_time, :dataset_id ]
2011-03-15 20:56:27 +00:00
AVM_SINGLES = AVM_SINGLE_FIELDS.zip(AVM_SINGLE_METHODS)
attr_reader :image, :options
def initialize(image, options = {})
2011-03-16 14:22:59 +00:00
options[:start_time] = options[:string_start_time] if options[:string_start_time]
2011-03-15 20:56:27 +00:00
@image, @options = image, options
end
def method_missing(method)
@options[method]
end
2011-03-16 14:22:59 +00:00
def wavelength
@options[:wavelength] ? @options[:wavelength].to_f : nil
end
2011-03-15 20:56:27 +00:00
def start_time
(Time.parse(@options[:start_time]) rescue nil)
end
def string_start_time
2011-03-16 14:22:59 +00:00
if start_time
start_time.strftime('%Y-%m-%dT%H:%M')
else
nil
end
end
def self.from_xml(image, document)
observation_parts = {}
document.get_refs do |refs|
AVM_SINGLES.each do |name, method|
if node = refs[:avm].at_xpath(".//avm:#{name}")
observation_parts[method] = node.text.split(',').collect(&:strip)
end
end
end
begin
observation = {}
observation_parts.each do |method, parts|
if part = parts.shift
observation[method] = part if part != '-'
end
end
image.create_observation(observation) if !observation.empty?
end while !observation.empty?
2011-03-15 20:56:27 +00:00
end
def self.add_to_document(document, observations)
field_values = {}
AVM_SINGLES.each do |name, method|
observations.each do |observation|
field_values[name] ||= []
2011-03-16 14:22:59 +00:00
field_values[name] << (observation.send(method) || '-')
2011-03-15 20:56:27 +00:00
end
end
document.get_refs do |refs|
field_values.each do |name, value|
refs[:avm].add_child(%{<avm:#{name}>#{value.join(',')}</avm:#{name}>})
end
end
end
end
end