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

77 lines
2.3 KiB
Ruby
Raw Normal View History

2011-03-15 20:56:27 +00:00
module AVM
2011-03-23 18:10:02 +00:00
# An individual observation by a single instrument w/ specific settings.
# Astronomical images are made of one or more Observations.
2011-03-15 20:56:27 +00:00
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 = {})
@image, @options = image, options
2011-03-23 18:10:02 +00:00
@options[:start_time] = @options[:string_start_time] || @options[:start_time]
2011-03-15 20:56:27 +00:00
end
def method_missing(method)
@options[method]
end
2011-03-16 14:22:59 +00:00
def wavelength
2011-03-23 18:10:02 +00:00
(wavelength = @options[:wavelength]) ? wavelength.to_f : nil
2011-03-16 14:22:59 +00:00
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-23 18:10:02 +00:00
start_time ? start_time.strftime('%Y-%m-%dT%H:%M') : nil
2011-03-16 14:22:59 +00:00
end
def to_h
Hash[@options.keys.reject { |key| key == :string_start_time }.collect { |key| [ key, send(key) ] }]
end
2011-03-16 14:22:59 +00:00
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)
2011-03-16 14:22:59 +00:00
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