Class: Mspire::Isotope::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/mspire/isotope.rb

Constant Summary collapse

NIST_ISOTOPE_SITE =
'http://physics.nist.gov/cgi-bin/Compositions/stand_alone.pl?ele=&all=all&ascii=ascii2&isotype=some'

Class Method Summary collapse

Class Method Details

.isotopes_from_nist_site(deuterium_is_kind_of_hydrogen = true) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mspire/isotope.rb', line 53

def isotopes_from_nist_site(deuterium_is_kind_of_hydrogen=true)
  require 'mechanize'
  body = Mechanize.new.get(NIST_ISOTOPE_SITE).body.split("\n")
  body.delete_if {|l| l[/^(<|\/)/]}
  body.shift(22)
  isotopes = body.each_slice(8).map do |lines|
    arr = (1..4).to_a.map {|i| match lines[i] }
    rel, avg = match(lines[5]), match(lines[6])
    next if rel.nil?
    rel.size > 0 ? Isotope.from_nist_line(*arr, rel, avg) : nil
  end.compact!

  # deuterium should be grouped with hydrogen, not as its own element!
  isotopes.find {|iso| iso.element == :d }.element = :h if deuterium_is_kind_of_hydrogen

  # update the mono boolean if this is the highest abundance peak
  isotopes.group_by(&:element).values.each do |set|
    set.max_by(&:relative_abundance).mono = true
  end
  isotopes
end

.write_nist_info(filename) ⇒ Object



48
49
50
51
# File 'lib/mspire/isotope.rb', line 48

def write_nist_info(filename)
  isotopes = isotopes_from_nist_site
  File.write(filename, isotopes.map {|isotope| MEMBERS.map {|key| isotope.send(key) }}.to_yaml)
end