require 'uri' module VORuby module Simple # Classes representing the different sorts of parameters commonly used # in the REST-based services like SIAP, SSAP and SCS. module Parameters # Represents a search radius. class Radius def initialize(radius) @radius = radius(radius) end def radius(radius=nil) if radius == nil return @radius else @radius = radius.to_f() end end def to_s URI.escape(sprintf("SR=%.2f", @radius)) end end # Represents astronomical longitude. class RightAscension def initialize(ra) ra(ra) end def ra(ra=nil) if ra == nil return @ra else if ra.instance_of?(String) if ra =~ /[-+]\d+:\d+:\d+(\.\d+)?/ @ra = RightAscension.convert_to_decimal_degrees(ra) else @ra = ra.to_f() end else @ra = ra.to_f() end end end def to_s URI.escape(sprintf("RA=%.3f", @ra)) end # Convert an RA in h:m:s format to decimal degrees. def self.convert_to_decimal_degrees(ra) hours, min, sec = ra.split(':') degrees = (hours.to_f() * 15.0) + (min.to_f() * 15.0 / 60.0) + (sec.to_f() * 15.0 / (60.0 * 60.0)) return degrees end end # Represents astronomical latitude. class Declination def initialize(dec) dec(dec) end def dec(dec=nil) if dec == nil return @dec else if dec.instance_of?(String) @dec = Declination.convert_to_decimal_degrees(dec) else @dec = dec.to_f() end end end def to_s URI.escape(sprintf("DEC=%.3f", @dec)) end # Convert a Dec in d:m:s format to decimal degrees. def self.convert_to_decimal_degrees(dec) degree, arcmin, arcsec = dec.split(':') degree_f = degree.to_f() arcmin_f = (arcmin.to_f() / 60.0) arcsec_f = (arcsec.to_f() / (60.0 * 60.0)) return (degree_f - arcmin_f - arcsec_f) if degree_f < 0 return degree_f + arcmin_f + arcsec_f end end # Represents an angular size on the sky. class Size def initialize(ra_width, dec_width=nil) if dec_width == nil ra_width(ra_width) dec_width(ra_width) else ra_width(ra_width) dec_width(dec_width) end end # Set or get the value of the RA width. # The width is specifed in decimal degrees. def ra_width(ra_width=nil) if ra_width == nil return @ra_width else @ra_width = ra_width.to_f() end end # Set or get the value of the declination width. # The width is specified in decimal degrees. def dec_width(dec_width=nil) if dec_width == nil return @dec_width else @dec_width = dec_width.to_f() end end # Convert the size to its SIAP-compliant form. def to_s if @ra_width == @dec_width # size = 0 is a special case--it test whether # the given point is in an image. if @ra_width == 0.0 return URI.escape("SIZE=0") else return URI.escape(sprintf("SIZE=%.4f", @ra_width)) end else return URI.escape(sprintf("SIZE=%.4f,%.4f", @ra_width, @dec_width)) end end end # Represents a position on the sky. class Position def initialize(ra, dec) ra(ra) dec(dec) end # Set or get the value of the right ascension. # The RA may be a decimal or a string in standard sexigesimal format. def ra(ra=nil) if ra == nil return @ra else if ra.instance_of?(RightAscension) @ra = ra else @ra = RightAscension.new(ra) end end end # Set or get the value of the declination. # The declination may be a decimal or a string in standard sexigesimal format. def dec(dec=nil) if dec == nil return @dec else if dec.instance_of?(Declination) @dec = dec else @dec = Declination.new(dec) end end end # Convert the position to its SIAP-compliant form. def to_s URI.escape(sprintf("POS=%.3f,%.3f", @ra.ra(), @dec.dec())) end end end end end