Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 228 lines (167 sloc) 6.965 kB
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
1 """This module provides a way to grab and store raw data for atomic weights."""
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
2 import os
3 import re
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
4 import urllib2
5
6 import numpy as np
7 import tables as tb
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
8
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
9 from pyne import nucname
087317c @scopatz Added compression to nuc_data.
scopatz authored
10 from pyne.dbgen.api import BASIC_FILTERS
02308ff Added simple XS grabber, parser, persister to dbgen
Anthony Scopatz authored
11 from pyne.dbgen.kaeri import grab_kaeri_nuclide, parse_for_natural_isotopes
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
12
13 # Note that since ground state and meta-stable isotopes are of the same atomic weight,
14 # the meta-stables have been discluded from the following data sets.
15
16
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
17 def grab_kaeri_atomic_abund(build_dir=""):
18 """Grabs the KAERI files needed for the atomic abundance calculation,
19 if not already present.
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
20
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
21 Parameters
22 ----------
23 build_dir : str
24 Major directory to place html files in. 'KAERI/' will be appended.
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
25 """
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
26 # Add kaeri to build_dir
27 build_dir = os.path.join(build_dir, 'KAERI')
28 try:
29 os.makedirs(build_dir)
30 except OSError:
31 pass
32 already_grabbed = set(os.listdir(build_dir))
33
34 # Grab and parse elemental summary files.
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
35 natural_nuclides = set()
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
36 for element in nucname.name_zz.keys():
37 htmlfile = element + '.html'
5cc5712 Fixed to grab nuclides from KAERI for atomic abundance.
Anthony Scopatz authored
38 if htmlfile not in already_grabbed:
39 grab_kaeri_nuclide(element, build_dir)
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
40
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
41 natural_nuclides = natural_nuclides | parse_for_natural_isotopes(os.path.join(build_dir, htmlfile))
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
42
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
43 # Grab natural nuclide files
44 for nuc in natural_nuclides:
45 nuc = nucname.name(nuc)
46 htmlfile = nuc + '.html'
5cc5712 Fixed to grab nuclides from KAERI for atomic abundance.
Anthony Scopatz authored
47 if htmlfile not in already_grabbed:
48 grab_kaeri_nuclide(nuc, build_dir)
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
49
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
50
51
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
52 atomic_abund_regex = re.compile('<li>Atomic Percent Abundance: (\d+[.]?\d*?)%')
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
53
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
54 def parse_atomic_abund(build_dir=""):
55 """Builds and returns a dictionary from nuclides to atomic abundence fractions."""
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
56 build_dir = os.path.join(build_dir, 'KAERI')
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
57
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
58 # Grab and parse elemental summary files.
59 natural_nuclides = set()
60 for element in nucname.name_zz.keys():
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
61 htmlfile = element + '.html'
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
62 natural_nuclides = natural_nuclides | parse_for_natural_isotopes(os.path.join(build_dir, htmlfile))
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
63
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
64 atomic_abund = {}
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
65
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
66 for nuc in natural_nuclides:
67 nuc_name = nucname.name(nuc)
68 htmlfile = os.path.join(build_dir, nuc_name + '.html')
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
69
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
70 with open(htmlfile, 'r') as f:
71 for line in f:
72 m = atomic_abund_regex.search(line)
73 if m is not None:
74 val = float(m.group(1)) * 0.01
75 atomic_abund[nuc] = val
76 break
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
77
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
78 return atomic_abund
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
79
80
81
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
82 def grab_atmoic_mass_adjustment(build_dir=""):
83 """Grabs the current atomic mass adjustment from the Atomic
84 Mass Data Center. These are courtesy of Georges Audi and
85 Wang Meng via a private communication, April 2011."""
86 mass_file = 'mass.mas114'
87 bd_files = os.listdir(build_dir)
88 if mass_file in bd_files:
89 return
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
90
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
91 mass = urllib2.urlopen('http://amdc.in2p3.fr/masstables/Ame2011int/mass.mas114')
92 with open(os.path.join(build_dir, mass_file), 'w') as f:
93 f.write(mass.read())
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
94
95
2b18d57 some fixes to parsed AMDC mass file.
Anthony Scopatz authored
96 # Note, this regex specifically leaves our free neutrons
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
97 #amdc_regex = re.compile('[ \d-]*? (\d{1,3})[ ]{1,4}(\d{1,3}) [A-Z][a-z]? .*? (\d{1,3}) ([ #.\d]{10,11}) ([ #.\d]{1,10})[ ]*?$')
98 amdc_regex = re.compile('[ \d-]*? (\d{1,3})[ ]{1,4}(\d{1,3}) [A-Z][a-z]? .*? (\d{1,3}) ([ #.\d]{5,12}) ([ #.\d]+)[ ]*?$')
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
99
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
100 def parse_atmoic_mass_adjustment(build_dir=""):
101 """Parses the atomic mass adjustment data into a list of tuples of
102 the nuclide, atomic mass, and error."""
103 mass_file = 'mass.mas114'
104 f = open(os.path.join(build_dir, mass_file), 'r')
105
106 atomic_masses = []
107
108 for line in f:
109 m = amdc_regex.search(line)
110 if m is None:
111 continue
112
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
113 nuc = (10000 * int(m.group(1))) + (10 * int(m.group(2)))
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
114 mass = float(m.group(3)) + 1E-6 * float(m.group(4).strip().replace('#', ''))
115 error = 1E-6 * float(m.group(5).strip().replace('#', ''))
116
117 atomic_masses.append((nuc, mass, error))
118
119 f.close()
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
120
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
121 return atomic_masses
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
122
2b18d57 some fixes to parsed AMDC mass file.
Anthony Scopatz authored
123
124
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
125
126 atomic_weight_desc = {
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
127 'nuc_name': tb.StringCol(itemsize=6, pos=0),
128 'nuc_zz': tb.IntCol(pos=1),
129 'mass': tb.FloatCol(pos=2),
130 'error': tb.FloatCol(pos=3),
131 'abund': tb.FloatCol(pos=4),
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
132 }
133
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
134 atomic_weight_dtype = np.dtype([
135 ('nuc_name', 'S6'),
136 ('nuc_zz', int),
137 ('mass', float),
138 ('error', float),
139 ('abund', float),
140 ])
141
142 def make_atomic_weight_table(nuc_data, build_dir=""):
143 """Makes an atomic weight table in the nuc_data library.
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
144
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
145 Parameters
146 ----------
147 nuc_data : str
148 Path to nuclide data file.
149 build_dir : str
150 Directory to place html files in.
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
151 """
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
152 # Grab raw data
153 atomic_abund = parse_atomic_abund(build_dir)
154 atomic_masses = parse_atmoic_mass_adjustment(build_dir)
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
155
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
156 A = {}
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
157
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
158 # Add normal isotopes to A
159 for nuc_zz, mass, error in atomic_masses:
160 try:
161 nuc_name = nucname.name(nuc_zz)
162 except RuntimeError:
163 continue
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
164
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
165 if nuc_zz in atomic_abund:
166 A[nuc_zz] = nuc_name, nuc_zz, mass, error, atomic_abund[nuc_zz]
167 else:
168 A[nuc_zz] = nuc_name, nuc_zz, mass, error, 0.0
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
169
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
170 # Add naturally occuring elements
171 for element in nucname.name_zz:
172 nuc_zz = nucname.zzaaam(element)
173 A[nuc_zz] = element, nuc_zz, 0.0, 0.0, 0.0
174
175 for nuc, abund in atomic_abund.items():
176 zz = nuc / 10000
177 element_zz = zz * 10000
178 element = nucname.zz_name[zz]
179
180 nuc_name, nuc_zz, nuc_mass, _error, _abund = A[nuc]
181 elem_name, elem_zz, elem_mass, _error, _abund = A[element_zz]
182
183 new_elem_mass = elem_mass + (nuc_mass * abund)
c48583f Have working atomic weight lib.
Anthony Scopatz authored
184 A[element_zz] = element, element_zz, new_elem_mass, 0.0, 0.0
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
185
186
187 A = sorted(A.values(), key=lambda x: x[1])
c48583f Have working atomic weight lib.
Anthony Scopatz authored
188 #A = np.array(A, dtype=atomic_weight_dtype)
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
189
190 # Open the HDF5 File
087317c @scopatz Added compression to nuc_data.
scopatz authored
191 kdb = tb.openFile(nuc_data, 'a', filters=BASIC_FILTERS)
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
192
193 # Make a new the table
194 Atable = kdb.createTable("/", "atomic_weight", atomic_weight_desc,
195 "Atomic Weight Data [amu]", expectedrows=len(A))
196 Atable.append(A)
518cab8 Stubbing out atomic weight scraping.
Anthony Scopatz authored
197
198 # Ensure that data was written to table
199 Atable.flush()
200
201 # Close the hdf5 file
202 kdb.close()
203
204
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
205
206
b77c9f9 @scopatz nuc_data_make fix to allow user-supplied datapath.
scopatz authored
207 def make_atomic_weight(args):
db834af Added dbgen docs.
Anthony Scopatz authored
208 """Controller function for adding atomic_weights."""
b77c9f9 @scopatz nuc_data_make fix to allow user-supplied datapath.
scopatz authored
209 nuc_data, build_dir = args.nuc_data, args.build_dir
210
bab27b3 Some changes to dbgen.
Anthony Scopatz authored
211 if os.path.exists(nuc_data):
212 with tb.openFile(nuc_data, 'r') as f:
213 if hasattr(f.root, 'atomic_weight'):
214 return
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
215
5fe4bbe Added more stuff to get the scrapper to run.
Anthony Scopatz authored
216 # First grab the atomic abundance data
217 print "Grabing the atomic abundance from KAERI"
218 grab_kaeri_atomic_abund(build_dir)
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
219
220 # Then grab mass data
221 print "Grabing atomic mass data from AMDC"
222 grab_atmoic_mass_adjustment(build_dir)
223
bf80e2c Amlost done with atomic weights.
Anthony Scopatz authored
224 # Make atomic weight table once we have the array
225 print "Making atomic weight data table."
226 make_atomic_weight_table(nuc_data, build_dir)
66a2d1d Parsed AMDC mass file.
Anthony Scopatz authored
227
Something went wrong with that request. Please try again.