summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/lua-physical/physical-data.lua
blob: efc1a30ffaf12114c7e837917e9bd92d64080095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
--[[
This file contains the methods for accessing physical data.

Copyright (c) 2020 Thomas Jenni

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
local prefix = ... and (...):match '(.-%.?)[^%.]+$' or ''
local N = require(prefix..'physical-number')


-- N class
local Data = {}
Data.__index = Data


--
-- ASTRONOMICAL DATA
--
Data.Astronomical = {}

-- make the table callable
setmetatable(Data.Astronomical, {
	__call = function(class, ...)
		return Data.Astronomical.get(...)
	end
})

function Data.Astronomical.get(name, key)
	local data = require(prefix..'physical-astronomical')

	-- return all names
	if name == nil then
		local names = {}
		for name,_ in pairs(data) do
			names[#names+1] = name
		end
		return names
	end

	if data[name] ~= nil then
		local row = data[name]

		-- return all keys
		if key == nil then
			local result = {}
			for key,_ in pairs(row) do
				result[key] = row[key]
			end
			return result
		end
		
		if row[key] ~= nil then
			return row[key] 
		end
	end

	return nil
end



--
-- ISOTOPE DATA
--
Data.Isotope = {}

-- make the table callable
setmetatable(Data.Isotope, {
	__call = function(class, ...)
		return Data.Isotope.get(...)
	end
})

-- get isotope data
--
-- 1) Get the mass excess of Helium-5 by the following commands
-- Data.Isotope("5He","MassExcess") or
-- Data.Isotope("5-He","MassExcess") or
-- Data.Isotope("5Helium","MassExcess") or
-- Data.Isotope("5-Helium","MassExcess") or
-- Data.Isotope("He5","MassExcess") or
-- Data.Isotope("He-5","MassExcess") or
-- Data.Isotope("Helium5","MassExcess") or
-- Data.Isotope("Helium-5","MassExcess")
--
-- 2) Get the mass excess of all helium isotopes
-- Data.Isotope("He","MassExcess") or
-- Data.Isotope("Helium","MassExcess")
-- 
-- 3) Get the half life of Lithium-3
-- Data.Isotope({3,3},"HalfLife")
--
-- 4) Get the half life of all isotopes
-- Data.Isotope(nil,"HalfLife")
-- 
-- 5) Get the names of all isotopes
-- Data.Isotope(nil,nil)

function Data.Isotope.get(isotope,key)
	local data = require(prefix..'physical-isotope')

	-- match 1) and 2) or 4)
	if type(isotope) == "string" then
		return Data.Isotope.getByName(isotope, key)

	-- match 3) or 4)
	elseif type(isotope) == "table" then
		local A = isotope[1]
		local Z = isotope[2]

		if type(A) == "number" and type(Z) == "number" then
			return Data.Isotope.getByAZ(A, Z, key)
		end

	-- match 5)
	else

		local Akey = data.keys["A"]
		local Zkey = data.keys["Z"]
		
		local names = {}
		for _,row in ipairs(data.data) do
			local A = row[Akey]
			local Z = row[Zkey]

			names[#names + 1] = tostring(A)..data.symbols[Z+1]
		end
		return names 
	end

	return nil
end


-- returns isotope data by the isotope name.
function Data.Isotope.getByName(isotope, key)
	local data = require(prefix..'physical-isotope')

	-- match "Helium-5", "Helium5", "He-5" or "He5"
	local name, A   = string.match(isotope, "^([%a]+)%-?([%d]+)$")
    if name ~= nil then
    	local Z = data.names[name]
    	if Z == nil then
    		Z = data.symbols[name]
    	end

    	A = tonumber(A)

    	if A ~= nil and Z ~= nil then
    		return Data.Isotope.getByAZ(A, Z, key)
    	end
    end 

    -- match "5-Helium", "5Helium", "5-He" or "5He5"
    local A,name   = string.match(isotope, "^([%d]+)%-?([%a]+)$")
    if name ~= nil then
    	local Z = data.names[name]
    	if Z == nil then
    		Z = data.symbols[name]
    	end

    	A = tonumber(A)

    	if A ~= nil and Z ~= nil then
    		return Data.Isotope.getByAZ(A, Z, key)
    	end
    end

    local name   = string.match(isotope, "^([%a]+)$")
    if name ~= nil then
    	local Z = data.names[name]
    	if Z == nil then
    		Z = data.symbols[name]
    	end

    	if Z ~= nil then
    		return Data.Isotope.getByZ(Z, key)
    	end
    end

    return nil
end


-- return isotopes with a certain A and Z
function Data.Isotope.getByAZ(A, Z, key)
	local data = require(prefix..'physical-isotope')

	-- get row number
	local i = data.indexAZ[tonumber(tostring(A)..tostring(Z))]
	if i == nil then
		error("Isotope with (A,Z) = ("..tostring(A)..","..tostring(Z)..")  not found in 'Data.Isotope'.")
	end

	return Data.Isotope._getValues(data.data[i],key)
end


-- return all isotope data with a certain Z
function Data.Isotope.getByZ(Z, key)
	local data = require(prefix..'physical-isotope')

	local Akey = data.keys["A"]

	local result = {}
	for _, i in ipairs(data.indexZ[Z+1]) do
		local name = tostring(data.data[i][Akey])..data.symbols[Z+1]
		result[name] = Data.Isotope._getValues(data.data[i],key)
	end
	
	return result
end


-- return key value pairs from a specific row
function Data.Isotope._getValues(row, keys)
	local data = require(prefix..'physical-isotope')

	if keys == nil then
		keys = data.keys

	elseif type(keys) == "string" then
		keys = {keys}

	elseif type(keys) ~= "table" then
		error("Unknown key type '"..tostring(key).."'.")
	end

	local result = {}
	for _,key in ipairs(keys) do

		-- name and symbol
		if key == "name" then
			result["name"] =  data.names[row[data.keys["Z"]] + 1].."-"..row[data.keys["A"]]

		elseif key == "symbol" then
			result["symbol"] = row[data.keys["A"]]..data.symbols[row[data.keys["Z"]] + 1]
		
		elseif key ~= "" then

			local i = data.keys[key]
			local di = data.dkeys[key]

			local x = row[i]
			local dx = row[di]

			-- invalid key
			if x == nil then
				error("Key "..key.." not found in 'Data.Isotope'.")
			end

			-- get value
			local value = x

			-- append uncertainty
			if type(x) == "number" and type(dx) == "number" then
				value = N(x, dx)
			end

			-- append unit
			local unit = data.units[i]
			if type(x)=="number" and unit ~= "" then
				value = value * _G["_"..unit]
			end

			result[key] = value
		end
	end

	if #keys == 1 then
		return result[keys[1]]
	else
		return result
	end
end


return Data