Skip to content

Instantly share code, notes, and snippets.

@sabman
Created December 2, 2009 12:27
Show Gist options
  • Save sabman/247174 to your computer and use it in GitHub Desktop.
Save sabman/247174 to your computer and use it in GitHub Desktop.
sample.rb
module Prod
class Sample < ProdDb
set_table_name :samples
set_sequence_name :autogenerated
set_primary_key :sampleno
set_date_columns :entrydate, :qadate, :acquiredate, :confid_until, :lastupdate
belongs_to :survey, :foreign_key => :eno
has_many :sampledata, :foreign_key => :sampleno
comma :sampledata do
survey :surveyname
survey :surveyid
survey :startdate
survey :enddate
sampleno
sampleid
sample_type
top_depth
base_depth
start_lon
start_lat
start_depth
end_lon
end_lat
end_depth
access_code
acquiredate
comments
eno
grain_size_mud
grain_size_sand
grain_size_gravel
grain_size_bulk
grain_size_mean
carbonate_content_mud
carbonate_content_sand
carbonate_content_gravel
carbonate_content_bulk
biogenic_silica_average
biogenic_silica_stddev
start_water_depth
end_water_depth
end
comma :repository do
sampleno
sampleid
sample_type
comments
eno
survey :surveyname
survey :surveyid
survey :startdate
survey :enddate
end
# TODO: change these start/end lat/lon/depth methods to be created dynamically using method_missing or other meta-magic
def start_lat
return nil if geom.nil?
if geom.as_georuby.is_a?(Polygon)
return geom.as_georuby.rings[0].points.first.y
elsif geom.as_georuby.is_a?(Point)
return geom.as_georuby.y
end
end
def start_lon
nil if geom.nil?
if geom.as_georuby.is_a?(Polygon)
return geom.as_georuby.rings[0].points.first.x
elsif geom.as_georuby.is_a?(Point)
return geom.as_georuby.x
end
end
def start_depth
nil if geom.nil?
if geom.as_georuby.is_a?(Polygon)
return geom.as_georuby.rings[0].points.first.z
elsif geom.as_georuby.is_a?(Point)
return geom.as_georuby.z
end
end
def end_lat
nil if geom.nil?
if geom.as_georuby.is_a?(Polygon)
return geom.as_georuby.rings[0].points.last.y
elsif geom.as_georuby.is_a?(Point)
return nil
end
end
def end_lon
nil if geom.nil?
if geom.as_georuby.is_a?(Polygon)
return geom.as_georuby.rings[0].points.last.x
elsif geom.as_georuby.is_a?(Point)
return nil
end
end
def end_depth
nil if geom.nil?
if geom.as_georuby.is_a?(Polygon)
return geom.as_georuby.rings[0].points.last.z
elsif geom.as_georuby.is_a?(Point)
return nil
end
end
def grain_size
qualf = ['<63um','<62.5um', '63um - 2000um','62.5um - 2000um', '>2000um','2mm - 64mm', 'bulk', 'D [4, 3] - Volume weighted mean']
g1 = sampledata.find_by_property_and_qualifier('grain size', qualf[0])
g1a = sampledata.find_by_property_and_qualifier('grain size', qualf[1])
g2 = sampledata.find_by_property_and_qualifier('grain size', qualf[2])
g2a = sampledata.find_by_property_and_qualifier('grain size', qualf[3])
g3 = sampledata.find_by_property_and_qualifier('grain size', qualf[4])
g3a = sampledata.find_by_property_and_qualifier('grain size', qualf[5])
g4 = sampledata.find_by_property_and_qualifier('grain size', qualf[6])
g5 = sampledata.find_by_property_and_qualifier('grain size', qualf[7])
mud = merge_quant_value(g1.try(:quant_value), g1a.try(:quant_value))
sand = merge_quant_value(g2.try(:quant_value), g2a.try(:quant_value))
gravel= merge_quant_value(g3.try(:quant_value), g3a.try(:quant_value))
{
:mud => mud,
:sand => sand,
:gravel => gravel,
:bulk => g4.try(:quant_value),
:mean => g5.try(:quant_value)
}
end
def carbonate_content
qulf = ['<63um','<62.5um', '63um - 2000um','62.5um - 2000um', '>2000um','2mm - 64mm', 'bulk']
cond = ["property = ? OR property = ?", 'carbonate content', 'caco3']
#carbonate_data = sampledata.find_by_qualifier(qulf, :conditions => cond)
c1 = sampledata.find_by_qualifier(qulf[0], :conditions => cond)
c1a = sampledata.find_by_qualifier(qulf[1], :conditions => cond)
c2 = sampledata.find_by_qualifier(qulf[2], :conditions => cond)
c2a = sampledata.find_by_qualifier(qulf[3], :conditions => cond)
c3 = sampledata.find_by_qualifier(qulf[4], :conditions => cond)
c3a = sampledata.find_by_qualifier(qulf[5], :conditions => cond)
c4 = sampledata.find_by_qualifier(qulf[6], :conditions => cond)
mud = merge_quant_value(c1.try(:quant_value), c1a.try(:quant_value))
sand = merge_quant_value(c2.try(:quant_value), c2a.try(:quant_value))
gravel= merge_quant_value(c3.try(:quant_value), c3a.try(:quant_value))
{
:mud => mud,
:sand => sand,
:gravel => gravel,
:bulk => c4.try(:quant_value)
}
end
def biogenic_silica
avg = sampledata.find_by_property_and_qualifier('biogenic silica','average')
stdev = sampledata.find_by_property_and_qualifier('biogenic silica','deviation')
{
:average => avg.try(:quant_value),
:deviation => stdev.try(:quant_value)
}
end
def water_depth
start_depth = sampledata.find_by_property_and_qualifier('water depth', 'start')
end_depth = sampledata.find_by_property_and_qualifier('water depth', 'end')
if start_depth and end_depth
return {:start_depth => start_depth.try(:quant_value), :end_depth => end_depth.try(:quant_value)}
else
depth = sampledata.find_by_property('water depth')
return {:start_depth => depth.try(:quant_value), :end_depth => nil}
end
end
# TODO: figure out what rocks we are interested in
def rocks
end
def self.find_all_by_bbox(bounds)
bbox = GeoRuby::SimpleFeatures::Polygon.from_coordinates(bounds.as_coordinates, 4326)
self.find_all_by_geom(bbox)
end
def self.find_all_by_geom(g, opts=[])
cond = ["SDO_ANYINTERACT(#{table_name}.geom, #{g.as_sdo_geometry}) = 'TRUE'"]
cond = [cond, opts].join(' AND ') unless opts.empty?
Sample.find(:all, :conditions => cond)
end
def grain_size_mud; grain_size[:mud]; end
def grain_size_sand; grain_size[:sand]; end
def grain_size_gravel; grain_size[:gravel]; end
def grain_size_bulk; grain_size[:bulk]; end
def grain_size_mean; grain_size[:mean]; end
def carbonate_content_mud; carbonate_content[:mud]; end
def carbonate_content_sand; carbonate_content[:sand]; end
def carbonate_content_gravel; carbonate_content[:gravel]; end
def carbonate_content_bulk; carbonate_content[:bulk]; end
def biogenic_silica_average; biogenic_silica[:average]; end
def biogenic_silica_stddev; biogenic_silica[:deviation]; end
def start_water_depth; water_depth[:start_depth]; end
def end_water_depth; water_depth[:end_depth]; end
private
def merge_quant_value(q1, q2)
v = nil
if q1.nil? and q2.nil?
v = nil
elsif !q1.nil? and !q2.nil?
v = q1 + q2
else
v = q1 || q2
end
v
end
end # class Sample
end # module Prod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment