refactoring and read more avm data

This commit is contained in:
John Bintz 2011-03-16 15:32:50 -04:00
parent 88ded74b76
commit 1e3080ddc7
11 changed files with 314 additions and 38 deletions

View File

@ -0,0 +1,18 @@
module AVM
module ControlledVocabulary
class << self
def included(klass)
klass::TERMS.each do |type|
new_klass = Class.new do
def to_s
self.class.to_s.split('::').last
end
end
klass.const_set(type.to_sym, new_klass)
end
end
end
end
end

View File

@ -0,0 +1,10 @@
require 'avm/controlled_vocabulary'
module AVM
module CoordinateFrame
TERMS = %w{ICRS FK5 FK4 ECL GAL SGAL}
include ControlledVocabulary
end
end

View File

@ -0,0 +1,10 @@
require 'avm/controlled_vocabulary'
module AVM
module CoordinateSystemProjection
TERMS = %w{TAN SIN ARC AIT CAR CEA}
include ControlledVocabulary
end
end

View File

@ -2,6 +2,9 @@ require 'avm/creator'
require 'avm/xmp' require 'avm/xmp'
require 'avm/image_type' require 'avm/image_type'
require 'avm/image_quality' require 'avm/image_quality'
require 'avm/spatial_quality'
require 'avm/coordinate_system_projection'
require 'avm/coordinate_frame'
require 'avm/observation' require 'avm/observation'
module AVM module AVM
@ -16,7 +19,20 @@ module AVM
'Date', 'Date',
'ID', 'ID',
'Type', 'Type',
'Image.ProductQuality' 'Image.ProductQuality',
'Spatial.Equinox',
'Spatial.Rotation',
'Spatial.Notes',
'Spatial.FITSheader',
'Spatial.Quality',
'Spatial.CoordsystemProjection',
'Spatial.CDMatrix',
'Spatial.Scale',
'Spatial.ReferencePixel',
'Spatial.ReferenceDimension',
'Spatial.ReferenceValue',
'Spatial.Equinox',
'Spatial.CoordinateFrame'
] ]
AVM_SINGLE_METHODS = [ AVM_SINGLE_METHODS = [
@ -27,7 +43,20 @@ module AVM
:date, :date,
:id, :id,
:type, :type,
:quality :quality,
:spatial_equinox,
:spatial_rotation,
:spatial_notes,
:fits_header,
:spatial_quality,
:coordinate_system_projection,
:spatial_cd_matrix,
:spatial_scale,
:reference_pixel,
:reference_dimension,
:reference_value,
:equinox,
:coordinate_frame
] ]
AVM_SINGLE_MESSAGES = [ AVM_SINGLE_MESSAGES = [
@ -38,16 +67,50 @@ module AVM
:string_date, :string_date,
:id, :id,
:image_type, :image_type,
:image_quality :image_quality,
:spatial_equinox,
:spatial_rotation,
:spatial_notes,
:fits_header,
:spatial_quality,
:coordinate_system_projection,
:spatial_cd_matrix,
:spatial_scale,
:reference_pixel,
:reference_dimension,
:reference_value,
:equinox,
:coordinate_frame
] ]
AVM_SINGLES = AVM_SINGLE_FIELDS.zip(AVM_SINGLE_METHODS) AVM_SINGLES = AVM_SINGLE_FIELDS.zip(AVM_SINGLE_METHODS)
AVM_TO_FLOAT = [
:spatial_rotation,
:spatial_cd_matrix,
:spatial_scale,
:reference_pixel,
:reference_dimension,
:reference_value
]
attr_reader :creator, :observations attr_reader :creator, :observations
def initialize(options = {}) def initialize(options = {})
@creator = AVM::Creator.new(self) @creator = AVM::Creator.new(self)
@options = options @options = options
AVM_TO_FLOAT.each do |field|
if @options[field]
case @options[field]
when Array
@options[field].collect!(&:to_f)
else
@options[field] = @options[field].to_f
end
end
end
@observations = [] @observations = []
end end
@ -95,11 +158,23 @@ module AVM
end end
def image_type def image_type
(AVM::ImageType.const_get(@options[:type].to_sym).new rescue nil) cv_class_instance_for(AVM::ImageType, :type)
end end
def image_quality def image_quality
(AVM::ImageQuality.const_get(@options[:quality].to_sym).new rescue nil) cv_class_instance_for(AVM::ImageQuality, :quality)
end
def spatial_quality
cv_class_instance_for(AVM::SpatialQuality, :spatial_quality)
end
def coordinate_frame
cv_class_instance_for(AVM::CoordinateFrame, :coordinate_frame)
end
def coordinate_system_projection
cv_class_instance_for(AVM::CoordinateSystemProjection, :coordinate_system_projection)
end end
def date def date
@ -129,7 +204,11 @@ module AVM
AVM_SINGLES.each do |tag, field| AVM_SINGLES.each do |tag, field|
if node = refs[:avm].at_xpath("./avm:#{tag}") if node = refs[:avm].at_xpath("./avm:#{tag}")
options[field] = node.text if !(list_items = node.search('.//rdf:li')).empty?
options[field] = list_items.collect(&:text)
else
options[field] = node.text
end
end end
end end
@ -168,6 +247,10 @@ module AVM
def rdf_li(text) def rdf_li(text)
%{<rdf:li>#{text}</rdf:li>} %{<rdf:li>#{text}</rdf:li>}
end end
def cv_class_instance_for(mod, field)
(mod.const_get(@options[field].to_sym).new rescue nil)
end
end end
end end

View File

@ -1,14 +1,10 @@
require 'avm/controlled_vocabulary'
module AVM module AVM
module ImageQuality module ImageQuality
%w{Good Moderate Poor}.each do |type| TERMS = %w{Good Moderate Poor}
klass = Class.new do
def to_s
self.class.to_s.split('::').last
end
end
AVM::ImageQuality.const_set(type.to_sym, klass) include AVM::ControlledVocabulary
end
end end
end end

View File

@ -1,14 +1,10 @@
require 'avm/controlled_vocabulary'
module AVM module AVM
module ImageType module ImageType
%w{Observation Artwork Photographic Planetary Simulation Chart Collage}.each do |type| TERMS = %w{Observation Artwork Photographic Planetary Simulation Chart Collage}
klass = Class.new do
def to_s
self.class.to_s.split('::').last
end
end
AVM::ImageType.const_set(type.to_sym, klass) include ControlledVocabulary
end
end end
end end

View File

@ -0,0 +1,10 @@
require 'avm/controlled_vocabulary'
module AVM
module SpatialQuality
TERMS = %w{Full Position}
include AVM::ControlledVocabulary
end
end

View File

@ -21,6 +21,21 @@ describe AVM::Image do
let(:redshift) { 'Redshift' } let(:redshift) { 'Redshift' }
let(:light_years) { 'Light years' } let(:light_years) { 'Light years' }
let(:coordinate_frame) { 'ICRS' }
let(:equinox) { '100' }
let(:reference_value) { [ 100, 50 ] }
let(:reference_dimension) { [ 200, 150 ] }
let(:reference_pixel) { [ 25, 15 ] }
let(:spatial_scale) { [ 40, 35 ] }
let(:spatial_rotation) { 10 }
let(:coordinate_system_projection) { 'TAN' }
let(:spatial_quality) { 'Full' }
let(:spatial_notes) { 'Spatial Notes' }
let(:fits_header) { 'FITS header' }
let(:spatial_cd_matrix) { [ 1, 2, 3, 4 ] }
it "should have spatial information"
def self.with_all_options def self.with_all_options
let(:options) { { let(:options) { {
:title => title, :title => title,
@ -35,7 +50,19 @@ describe AVM::Image do
:type => type, :type => type,
:quality => image_quality, :quality => image_quality,
:redshift => redshift, :redshift => redshift,
:light_years => light_years :light_years => light_years,
:coordinate_frame => coordinate_frame,
:equinox => equinox,
:reference_value => reference_value,
:reference_dimension => reference_dimension,
:reference_pixel => reference_pixel,
:spatial_scale => spatial_scale,
:spatial_rotation => spatial_rotation,
:coordinate_system_projection => coordinate_system_projection,
:spatial_quality => spatial_quality,
:spatial_notes => spatial_notes,
:fits_header => fits_header,
:spatial_cd_matrix => spatial_cd_matrix,
} } } }
end end
@ -52,6 +79,19 @@ describe AVM::Image do
its(:id) { should == id } its(:id) { should == id }
its(:image_type) { should be_a_kind_of eval("AVM::ImageType::#{type}") } its(:image_type) { should be_a_kind_of eval("AVM::ImageType::#{type}") }
its(:image_quality) { should be_a_kind_of eval("AVM::ImageQuality::#{image_quality}") } its(:image_quality) { should be_a_kind_of eval("AVM::ImageQuality::#{image_quality}") }
its(:coordinate_frame) { should be_a_kind_of eval("AVM::CoordinateFrame::#{coordinate_frame}") }
its(:equinox) { should == equinox }
its(:reference_value) { should == reference_value }
its(:reference_dimension) { should == reference_dimension }
its(:reference_pixel) { should == reference_pixel }
its(:spatial_scale) { should == spatial_scale }
its(:spatial_rotation) { should == spatial_rotation }
its(:coordinate_system_projection) { should be_a_kind_of eval("AVM::CoordinateSystemProjection::#{coordinate_system_projection}") }
its(:spatial_quality) { should be_a_kind_of eval("AVM::SpatialQuality::#{spatial_quality}") }
its(:spatial_notes) { should == spatial_notes }
its(:fits_header) { should == fits_header }
its(:spatial_cd_matrix) { should == spatial_cd_matrix }
end end
describe '#initialize' do describe '#initialize' do
@ -72,21 +112,18 @@ describe AVM::Image do
context "nothing in it" do context "nothing in it" do
let(:file_path) { 'spec/sample_files/image/nothing.xmp' } let(:file_path) { 'spec/sample_files/image/nothing.xmp' }
its(:title) { should be_nil } [ :title, :headline, :description, :distance_notes,
its(:headline) { should be_nil } :spectral_notes, :reference_url, :credit, :date,
its(:description) { should be_nil } :id, :image_type, :image_quality, :redshift,
its(:distance_notes) { should be_nil } :light_years, :coordinate_frame, :equinox, :reference_value,
its(:spectral_notes) { should be_nil } :reference_dimension, :reference_pixel, :spatial_scale,
its(:reference_url) { should be_nil } :spatial_rotation, :coordinate_system_projection, :spatial_quality, :spatial_notes,
its(:credit) { should be_nil } :fits_header, :spatial_cd_matrix
its(:date) { should be_nil } ].each do |field|
its(:id) { should be_nil } its(field) { should be_nil }
its(:image_type) { should be_nil } end
its(:image_quality) { should be_nil }
its(:redshift) { should be_nil }
its(:light_years) { should be_nil }
end end
context "image in it" do context "image in it" do
context "distance in light years" do context "distance in light years" do
let(:file_path) { 'spec/sample_files/image/light_years.xmp' } let(:file_path) { 'spec/sample_files/image/light_years.xmp' }

View File

@ -39,6 +39,45 @@
<rdf:li>Redshift</rdf:li> <rdf:li>Redshift</rdf:li>
</rdf:Seq> </rdf:Seq>
</avm:Distance> </avm:Distance>
<avm:Spatial.CoordinateFrame>ICRS</avm:Spatial.CoordinateFrame>
<avm:Spatial.Equinox>100</avm:Spatial.Equinox>
<avm:Spatial.ReferenceValue>
<rdf:Seq>
<rdf:li>100</rdf:li>
<rdf:li>50</rdf:li>
</rdf:Seq>
</avm:Spatial.ReferenceValue>
<avm:Spatial.ReferenceDimension>
<rdf:Seq>
<rdf:li>200</rdf:li>
<rdf:li>150</rdf:li>
</rdf:Seq>
</avm:Spatial.ReferenceDimension>
<avm:Spatial.ReferencePixel>
<rdf:Seq>
<rdf:li>25</rdf:li>
<rdf:li>15</rdf:li>
</rdf:Seq>
</avm:Spatial.ReferencePixel>
<avm:Spatial.Scale>
<rdf:Seq>
<rdf:li>40</rdf:li>
<rdf:li>35</rdf:li>
</rdf:Seq>
</avm:Spatial.Scale>
<avm:Spatial.Rotation>10</avm:Spatial.Rotation>
<avm:Spatial.CoordsystemProjection>TAN</avm:Spatial.CoordsystemProjection>
<avm:Spatial.Quality>Full</avm:Spatial.Quality>
<avm:Spatial.Notes>Spatial Notes</avm:Spatial.Notes>
<avm:Spatial.FITSheader>FITS header</avm:Spatial.FITSheader>
<avm:Spatial.CDMatrix>
<rdf:Seq>
<rdf:li>1</rdf:li>
<rdf:li>2</rdf:li>
<rdf:li>3</rdf:li>
<rdf:li>4</rdf:li>
</rdf:Seq>
</avm:Spatial.CDMatrix>
</rdf:Description> </rdf:Description>
</rdf:RDF> </rdf:RDF>
</x:xmpmeta> </x:xmpmeta>

View File

@ -12,7 +12,6 @@
<rdf:li xml:lang="x-default">Description</rdf:li> <rdf:li xml:lang="x-default">Description</rdf:li>
</rdf:Alt> </rdf:Alt>
</dc:description> </dc:description>
</rdf:Description> </rdf:Description>
<rdf:Description rdf:about="" <rdf:Description rdf:about=""
xmlns:Iptc4xmpCore="http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/"> xmlns:Iptc4xmpCore="http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/">
@ -34,6 +33,45 @@
<avm:Type>Observation</avm:Type> <avm:Type>Observation</avm:Type>
<avm:Image.ProductQuality>Good</avm:Image.ProductQuality> <avm:Image.ProductQuality>Good</avm:Image.ProductQuality>
<avm:Distance><rdf:Seq><rdf:li>Light years</rdf:li></rdf:Seq></avm:Distance> <avm:Distance><rdf:Seq><rdf:li>Light years</rdf:li></rdf:Seq></avm:Distance>
<avm:Spatial.CoordinateFrame>ICRS</avm:Spatial.CoordinateFrame>
<avm:Spatial.Equinox>100</avm:Spatial.Equinox>
<avm:Spatial.ReferenceValue>
<rdf:Seq>
<rdf:li>100</rdf:li>
<rdf:li>50</rdf:li>
</rdf:Seq>
</avm:Spatial.ReferenceValue>
<avm:Spatial.ReferenceDimension>
<rdf:Seq>
<rdf:li>200</rdf:li>
<rdf:li>150</rdf:li>
</rdf:Seq>
</avm:Spatial.ReferenceDimension>
<avm:Spatial.ReferencePixel>
<rdf:Seq>
<rdf:li>25</rdf:li>
<rdf:li>15</rdf:li>
</rdf:Seq>
</avm:Spatial.ReferencePixel>
<avm:Spatial.Scale>
<rdf:Seq>
<rdf:li>40</rdf:li>
<rdf:li>35</rdf:li>
</rdf:Seq>
</avm:Spatial.Scale>
<avm:Spatial.Rotation>10</avm:Spatial.Rotation>
<avm:Spatial.CoordsystemProjection>TAN</avm:Spatial.CoordsystemProjection>
<avm:Spatial.Quality>Full</avm:Spatial.Quality>
<avm:Spatial.Notes>Spatial Notes</avm:Spatial.Notes>
<avm:Spatial.FITSheader>FITS header</avm:Spatial.FITSheader>
<avm:Spatial.CDMatrix>
<rdf:Seq>
<rdf:li>1</rdf:li>
<rdf:li>2</rdf:li>
<rdf:li>3</rdf:li>
<rdf:li>4</rdf:li>
</rdf:Seq>
</avm:Spatial.CDMatrix>
</rdf:Description> </rdf:Description>
</rdf:RDF> </rdf:RDF>
</x:xmpmeta> </x:xmpmeta>

View File

@ -39,6 +39,45 @@
<rdf:li>Redshift</rdf:li> <rdf:li>Redshift</rdf:li>
</rdf:Seq> </rdf:Seq>
</avm:Distance> </avm:Distance>
<avm:Spatial.CoordinateFrame>ICRS</avm:Spatial.CoordinateFrame>
<avm:Spatial.Equinox>100</avm:Spatial.Equinox>
<avm:Spatial.ReferenceValue>
<rdf:Seq>
<rdf:li>100</rdf:li>
<rdf:li>50</rdf:li>
</rdf:Seq>
</avm:Spatial.ReferenceValue>
<avm:Spatial.ReferenceDimension>
<rdf:Seq>
<rdf:li>200</rdf:li>
<rdf:li>150</rdf:li>
</rdf:Seq>
</avm:Spatial.ReferenceDimension>
<avm:Spatial.ReferencePixel>
<rdf:Seq>
<rdf:li>25</rdf:li>
<rdf:li>15</rdf:li>
</rdf:Seq>
</avm:Spatial.ReferencePixel>
<avm:Spatial.Scale>
<rdf:Seq>
<rdf:li>40</rdf:li>
<rdf:li>35</rdf:li>
</rdf:Seq>
</avm:Spatial.Scale>
<avm:Spatial.Rotation>10</avm:Spatial.Rotation>
<avm:Spatial.CoordsystemProjection>TAN</avm:Spatial.CoordsystemProjection>
<avm:Spatial.Quality>Full</avm:Spatial.Quality>
<avm:Spatial.Notes>Spatial Notes</avm:Spatial.Notes>
<avm:Spatial.FITSheader>FITS header</avm:Spatial.FITSheader>
<avm:Spatial.CDMatrix>
<rdf:Seq>
<rdf:li>1</rdf:li>
<rdf:li>2</rdf:li>
<rdf:li>3</rdf:li>
<rdf:li>4</rdf:li>
</rdf:Seq>
</avm:Spatial.CDMatrix>
</rdf:Description> </rdf:Description>
</rdf:RDF> </rdf:RDF>
</x:xmpmeta> </x:xmpmeta>