Class: AVM::Observation

Inherits:
Object
  • Object
show all
Defined in:
lib/avm/observation.rb

Overview

An individual observation by a single instrument w/ specific settings. Astronomical images are made of one or more Observations.

Constant Summary collapse

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 ]
AVM_SINGLES =
AVM_SINGLE_FIELDS.zip(AVM_SINGLE_METHODS)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, options = {}) ⇒ Observation

Returns a new instance of Observation.



11
12
13
14
# File 'lib/avm/observation.rb', line 11

def initialize(image, options = {})
  @image, @options = image, options
  @options[:start_time] = @options[:string_start_time] || @options[:start_time]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



16
17
18
# File 'lib/avm/observation.rb', line 16

def method_missing(method)
  @options[method]
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



9
10
11
# File 'lib/avm/observation.rb', line 9

def image
  @image
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/avm/observation.rb', line 9

def options
  @options
end

Class Method Details

.add_to_document(document, observations) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/avm/observation.rb', line 60

def self.add_to_document(document, observations)
  field_values = {}
  AVM_SINGLES.each do |name, method|
    observations.each do |observation|
      field_values[name] ||= []
      field_values[name] << (observation.send(method) || '-')
    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

.from_xml(image, document) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/avm/observation.rb', line 36

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?
end

Instance Method Details

#start_timeObject



24
25
26
# File 'lib/avm/observation.rb', line 24

def start_time
  (Time.parse(@options[:start_time]) rescue nil)
end

#string_start_timeObject



28
29
30
# File 'lib/avm/observation.rb', line 28

def string_start_time
  start_time ? start_time.strftime('%Y-%m-%dT%H:%M') : nil
end

#to_hObject



32
33
34
# File 'lib/avm/observation.rb', line 32

def to_h
  Hash[@options.keys.reject { |key| key == :string_start_time }.collect { |key| [ key, send(key) ] }]
end

#wavelengthObject



20
21
22
# File 'lib/avm/observation.rb', line 20

def wavelength
  (wavelength = @options[:wavelength]) ? wavelength.to_f : nil
end