From a5498624a262dfde3215cfe9859e401fdef0c313 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 16 Mar 2011 10:22:59 -0400 Subject: [PATCH] read observations --- lib/avm/image.rb | 1 + lib/avm/observation.rb | 42 ++++++++- spec/avm/image_spec.rb | 2 + spec/avm/observation_spec.rb | 114 ++++++++++++++++++------- spec/sample_files/observation/none.xmp | 5 ++ spec/sample_files/observation/one.xmp | 17 ++++ spec/sample_files/observation/two.xmp | 17 ++++ 7 files changed, 161 insertions(+), 37 deletions(-) create mode 100644 spec/sample_files/observation/none.xmp create mode 100644 spec/sample_files/observation/one.xmp create mode 100644 spec/sample_files/observation/two.xmp diff --git a/lib/avm/image.rb b/lib/avm/image.rb index 5d8fcca..c3eea5f 100644 --- a/lib/avm/image.rb +++ b/lib/avm/image.rb @@ -116,6 +116,7 @@ module AVM image = new(options) image.creator.from_xml(self, document) + Observation.from_xml(image, document) image end diff --git a/lib/avm/observation.rb b/lib/avm/observation.rb index 8b2f1ee..7928f9d 100644 --- a/lib/avm/observation.rb +++ b/lib/avm/observation.rb @@ -1,12 +1,14 @@ module AVM class Observation - AVM_SINGLE_FIELDS = %w{Facility Instrument Spectral.ColorAssignment Spectral.Band Spectral.Bandpass Spectral.CentralWavelength Spectral.Notes Temporal.StartTime Temporal.IntegrationTime DatasetID} - AVM_SINGLE_METHODS = [ :facility, :instrument, :color_assignment, :band, :bandpass, :wavelength, :notes, :string_start_time, :integration_time, :dataset_id ] + 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) attr_reader :image, :options def initialize(image, options = {}) + options[:start_time] = options[:string_start_time] if options[:string_start_time] + @image, @options = image, options end @@ -14,12 +16,44 @@ module AVM @options[method] end + def wavelength + @options[:wavelength] ? @options[:wavelength].to_f : nil + end + def start_time (Time.parse(@options[:start_time]) rescue nil) end def string_start_time - start_time.strftime('%Y-%m-%dT%H:%M') + 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? end def self.add_to_document(document, observations) @@ -27,7 +61,7 @@ module AVM AVM_SINGLES.each do |name, method| observations.each do |observation| field_values[name] ||= [] - field_values[name] << observation.send(method) + field_values[name] << (observation.send(method) || '-') end end diff --git a/spec/avm/image_spec.rb b/spec/avm/image_spec.rb index 5f914d7..1d900a7 100644 --- a/spec/avm/image_spec.rb +++ b/spec/avm/image_spec.rb @@ -20,6 +20,8 @@ describe AVM::Image do let(:redshift) { 'Redshift' } let(:light_years) { 'Light years' } + it "should have spectral notes" + def self.with_all_options let(:options) { { :title => title, diff --git a/spec/avm/observation_spec.rb b/spec/avm/observation_spec.rb index ec8025c..c9e59ee 100644 --- a/spec/avm/observation_spec.rb +++ b/spec/avm/observation_spec.rb @@ -14,7 +14,6 @@ describe AVM::Observation do let(:band) { 'em.opt' } let(:bandpass) { 'B' } let(:wavelength) { 2.5 } - let(:notes) { 'Notes' } let(:start_time) { '2010-01-01' } let(:start_time_string) { '2010-01-01T00:00' } let(:integration_time) { '300' } @@ -27,32 +26,108 @@ describe AVM::Observation do :band => band, :bandpass => bandpass, :wavelength => wavelength, - :notes => notes, :start_time => start_time, :integration_time => integration_time, :dataset_id => dataset_id, } } - context 'defaults' do + let(:second_facility) { 'Chandra' } + let(:second_instrument) { 'X-Ray' } + let(:second_color_assignment) { 'Green' } + let(:second_band) { 'em.x-ray' } + let(:second_bandpass) { 'C' } + let(:second_wavelength) { nil } + let(:second_start_time) { '2010-01-02' } + let(:second_start_time_string) { '2010-01-02T00:00' } + let(:second_integration_time) { '400' } + let(:second_dataset_id) { '23456' } + + let(:second_options) { { + :facility => second_facility, + :instrument => second_instrument, + :color_assignment => second_color_assignment, + :band => second_band, + :bandpass => second_bandpass, + :wavelength => second_wavelength, + :start_time => second_start_time, + :integration_time => second_integration_time, + :dataset_id => second_dataset_id, + } } + + def self.check_defaults its(:facility) { should == facility } its(:instrument) { should == instrument } its(:color_assignment) { should == color_assignment } its(:band) { should == band } its(:bandpass) { should == bandpass } its(:wavelength) { should == wavelength } - its(:notes) { should == notes } its(:start_time) { should == Time.parse(start_time) } its(:integration_time) { should == integration_time } its(:dataset_id) { should == dataset_id } end + context 'defaults' do + check_defaults + end + + describe '.from_xml' do + let(:image) { AVM::Image.from_xml(File.read(file_path)) } + let(:observations) { image.observations } + + subject { observations } + + context 'no observations' do + let(:file_path) { 'spec/sample_files/observation/none.xmp' } + + it { should == [] } + end + + context 'one observation' do + let(:file_path) { 'spec/sample_files/observation/one.xmp' } + + its(:length) { should == 1 } + + context 'first observation' do + subject { observations.first } + + check_defaults + end + end + + context 'two observations' do + let(:file_path) { 'spec/sample_files/observation/two.xmp' } + + its(:length) { should == 2 } + + context 'first observation' do + subject { observations.first } + + check_defaults + end + + context 'second observation' do + subject { observations.last } + + its(:facility) { should == second_facility } + its(:instrument) { should == second_instrument } + its(:color_assignment) { should == second_color_assignment } + its(:band) { should == second_band } + its(:bandpass) { should == second_bandpass } + its(:wavelength) { should == second_wavelength } + its(:start_time) { should == Time.parse(second_start_time) } + its(:integration_time) { should == second_integration_time } + its(:dataset_id) { should == second_dataset_id } + end + end + end + describe '.add_to_document' do let(:xml) { image.to_xml } let(:avm) { xml.at_xpath('//rdf:Description[@rdf:about="AVM"]') } context 'none' do it "should not have any observation information" do - %w{Facility Instrument Spectral.ColorAssignment Spectral.Band Spectral.Bandpass Spectral.CentralWavelength Spectral.Notes Temporal.StartTime Temporal.IntegrationTime DatasetID}.each do |name| + %w{Facility Instrument Spectral.ColorAssignment Spectral.Band Spectral.Bandpass Spectral.CentralWavelength Temporal.StartTime Temporal.IntegrationTime DatasetID}.each do |name| avm.at_xpath("//avm:#{name}").should be_nil end end @@ -69,7 +144,6 @@ describe AVM::Observation do 'Spectral.Band' => band, 'Spectral.Bandpass' => bandpass, 'Spectral.CentralWavelength' => wavelength, - 'Spectral.Notes' => notes, 'Temporal.StartTime' => start_time_string, 'Temporal.IntegrationTime' => integration_time, 'DatasetID' => dataset_id @@ -82,30 +156,6 @@ describe AVM::Observation do context 'two' do let(:second_observation) { image.create_observation(second_options) } - let(:second_facility) { 'Chandra' } - let(:second_instrument) { 'X-Ray' } - let(:second_color_assignment) { 'Green' } - let(:second_band) { 'em.x-ray' } - let(:second_bandpass) { 'C' } - let(:second_wavelength) { nil } - let(:second_notes) { 'More Notes' } - let(:second_start_time) { '2010-01-02' } - let(:second_start_time_string) { '2010-01-02T00:00' } - let(:second_integration_time) { '400' } - let(:second_dataset_id) { '23456' } - - let(:second_options) { { - :facility => second_facility, - :instrument => second_instrument, - :color_assignment => second_color_assignment, - :band => second_band, - :bandpass => second_bandpass, - :wavelength => second_wavelength, - :notes => second_notes, - :start_time => second_start_time, - :integration_time => second_integration_time, - :dataset_id => second_dataset_id, - } } before { observation ; second_observation } @@ -116,8 +166,7 @@ describe AVM::Observation do 'Spectral.ColorAssignment' => [ color_assignment, second_color_assignment ].join(','), 'Spectral.Band' => [ band, second_band ].join(','), 'Spectral.Bandpass' => [ bandpass, second_bandpass ].join(','), - 'Spectral.CentralWavelength' => [ wavelength, second_wavelength ].join(','), - 'Spectral.Notes' => [ notes, second_notes ].join(','), + 'Spectral.CentralWavelength' => [ wavelength, second_wavelength || '-' ].join(','), 'Temporal.StartTime' => [ start_time_string, second_start_time_string ].join(','), 'Temporal.IntegrationTime' => [ integration_time, second_integration_time ].join(','), 'DatasetID' => [ dataset_id, second_dataset_id ].join(',') @@ -125,7 +174,6 @@ describe AVM::Observation do avm.at_xpath("//avm:#{name}").text.should == value.to_s end end - end end end diff --git a/spec/sample_files/observation/none.xmp b/spec/sample_files/observation/none.xmp new file mode 100644 index 0000000..fefd1c7 --- /dev/null +++ b/spec/sample_files/observation/none.xmp @@ -0,0 +1,5 @@ + + + + + diff --git a/spec/sample_files/observation/one.xmp b/spec/sample_files/observation/one.xmp new file mode 100644 index 0000000..66f26c1 --- /dev/null +++ b/spec/sample_files/observation/one.xmp @@ -0,0 +1,17 @@ + + + + HST + ACS + Blue + em.opt + B + 2.5 + 2010-01-01T00:00 + 300 + 12345 + + + + diff --git a/spec/sample_files/observation/two.xmp b/spec/sample_files/observation/two.xmp new file mode 100644 index 0000000..d30b454 --- /dev/null +++ b/spec/sample_files/observation/two.xmp @@ -0,0 +1,17 @@ + + + + HST,Chandra + ACS,X-Ray + Blue,Green + em.opt,em.x-ray + B,C + 2.5,- + 2010-01-01T00:00,2010-01-02 + 300,400 + 12345,23456 + + + +