| 1 |
#!/bin/sh |
| 2 |
# \ |
| 3 |
# this line restarts tclsh \ |
| 4 |
exec tclsh $0 "$@" |
| 5 |
|
| 6 |
# |
| 7 |
# Usage: unitconv unit value |
| 8 |
# or: unitconv unit expr |
| 9 |
# |
| 10 |
# |
| 11 |
|
| 12 |
# supported units |
| 13 |
|
| 14 |
set energy_units { |
| 15 |
au |
| 16 |
ry |
| 17 |
ev |
| 18 |
kjm |
| 19 |
kcalm |
| 20 |
kj |
| 21 |
kcal |
| 22 |
erg |
| 23 |
} |
| 24 |
set length_units { |
| 25 |
angs |
| 26 |
bohr |
| 27 |
m |
| 28 |
} |
| 29 |
set allowed_units "$energy_units $length_units" |
| 30 |
|
| 31 |
|
| 32 |
# check for correctness of specified unit on command-line |
| 33 |
|
| 34 |
set unit [string tolower [lindex $argv 0]] |
| 35 |
|
| 36 |
if { [lsearch -nocase $allowed_units $unit] < 0 } { |
| 37 |
puts stderr "input unit not known; must be one of [join $allowed_units {, }]" |
| 38 |
exit |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
# ------------------------------------------------------------------------ |
| 43 |
# PHYSICAL CONSTANTS |
| 44 |
# |
| 45 |
|
| 46 |
# Physical constants, SI (NIST CODATA 2006), Web Version 5.1 |
| 47 |
# http://physics.nist.gov/constants |
| 48 |
|
| 49 |
set j 1.000000000000 |
| 50 |
set au 4.35974394e-18; # J |
| 51 |
set ev 1.602176487e-19; # J |
| 52 |
set mol 6.02214129e23 |
| 53 |
|
| 54 |
# thermo-kilocalorie (International Standard ISO 31-4: Quantities and |
| 55 |
# units – Part 4: Heat. Annex B (informative): Other units given for |
| 56 |
# information, especially regarding the conversion |
| 57 |
# factor. International Organization for Standardization, 1992.) |
| 58 |
|
| 59 |
set kcal 4184; # J |
| 60 |
|
| 61 |
# from wikipedia: |
| 62 |
set erg 1e-7; # J |
| 63 |
|
| 64 |
#-------------------------------- |
| 65 |
# Energy Units (use J as default) |
| 66 |
#-------------------------------- |
| 67 |
|
| 68 |
set kj 1000.00000000000000 |
| 69 |
set ry [expr $au / 2.0] |
| 70 |
set kjm [expr $kj / $mol] |
| 71 |
set kcalm [expr $kcal / $mol] |
| 72 |
|
| 73 |
|
| 74 |
#----------------------------------------- |
| 75 |
# Distance Units (use Angstrom as default) |
| 76 |
#----------------------------------------- |
| 77 |
set angs 1.0 |
| 78 |
set bohr 0.52917720859 |
| 79 |
set m 1.e10 |
| 80 |
# ------------------------------------------------------------------------ |
| 81 |
|
| 82 |
|
| 83 |
# |
| 84 |
# evaluate & convert user specified expression/value |
| 85 |
# |
| 86 |
|
| 87 |
upvar #0 $unit un_fac |
| 88 |
|
| 89 |
# parse the command line arguments |
| 90 |
|
| 91 |
foreach item [lrange $argv 1 end] { |
| 92 |
append numbers [format "%s " $item] |
| 93 |
} |
| 94 |
set orig_value [expr 1.0 * ($numbers)]; |
| 95 |
|
| 96 |
# store result either in J or Angstroms !!! |
| 97 |
|
| 98 |
set value [expr $un_fac * $orig_value]; |
| 99 |
|
| 100 |
|
| 101 |
# |
| 102 |
# PRINTOUT |
| 103 |
# |
| 104 |
puts stdout "$orig_value $unit = " |
| 105 |
|
| 106 |
if { [lsearch -nocase $energy_units $unit] > -1 } { |
| 107 |
|
| 108 |
# do we have energy ? |
| 109 |
|
| 110 |
foreach _un $energy_units { |
| 111 |
upvar #0 $_un unval |
| 112 |
set val [expr $value / $unval] |
| 113 |
puts stdout " = $val $_un" |
| 114 |
} |
| 115 |
|
| 116 |
} elseif { [lsearch -nocase $length_units $unit] > -1 } { |
| 117 |
|
| 118 |
# ... or length ? |
| 119 |
|
| 120 |
foreach _un $length_units { |
| 121 |
upvar #0 $_un unval |
| 122 |
set val [expr $value / $unval] |
| 123 |
puts stdout " = $val $_un" |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
exit 0 |