From octave-sources-request at bevo dot che dot wisc dot edu Mon Jan 27 14:10:43 2003 Subject: vol2metric.m From: Stefan Burger To: octave-sources at bevo dot che dot wisc dot edu Date: Mon, 27 Jan 2003 07:44:31 -0600 --------------Boundary-00=_LVKDXTUXW64KL8KMD6QC Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable --------------Boundary-00=_LVKDXTUXW64KL8KMD6QC Content-Type: text/plain; charset="us-ascii"; name="vol2metric.m" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="vol2metric.m" ## Copyright (C) 2003 Stefan Burger ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by the ## Free Software Foundation; either version 2, or (at your option) any ## later version. ## ## Octave is distributed in the hope that it will be useful, but WITHOUT ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ## for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. ## -*- texinfo -*- ## at deftypefn {Function File} {} vol2metric (@var{unitstr}, @var{val}, @var{metstr}, ) ## Converts non-SI or US volume measures to metric (SI) volume measures. ## Input: ## at var{unitstr} : unit to be converted from, ## at var{val} ; Value to be converted, ## at var{metstr} : unit to be converted to, optional, defaults to "m3", meters cubed. ## ## Currently, the following units, respectively unit strings, are supported: ## ## inches cubed: in3, cubic-inch, ## foots cubed: ft3, cubic-foot, ## yards cubed: yd3, cubic-yard, ## miles cubed: mi3, cubic-mile, ## U.S. gallons: US-gal, US-gallon, ## ## others: acrefoot, ## US-floz, US-fluid-ounce, ## Imperial-fluid-ounce IMP-floz, ## barrel, bbl, ## peck, pk, ## US-dry-pint, US-drypt, US-liquid-pint, US-liqpt ## US-dry-quart, US-dryqt, US-liquid-quart, US-liqqt, ## tablespoon, teaspoon, ## register ton, ## hogshead, firkin, ## Imperial gill, IMP-gill, US-gill ## ## Metric units: ## meters cubed: m3 ## kilometers cubed: km3 ## Liters: dm3, l, L, liter, Liter ## centimeters cubed: cm3 ## millimeters cubed: mm3 ## micrometers cubed: um3 ## nanometers cubed: nm3 ## ## References: ## See NIST Guide to SI units, Appendix B8, ## at www.nist.gov/Pubs/SP811/appenB8.html ## ## at end deftypefn ## at seealso{len2metric, area2metric and metric2vol} ## Author: Stefan Burger ## Description: converts volume values to SI unit values. function v = vol2metric (unitstr, val, metstr) if (nargin ==1) val = 1.0; metstr = "m3"; %% if no value given, default to one. %% if no metric string given, default to meters. %% reason: usage val * vol2metric("inch") endif if (nargin == 2) metstr = "m"; %% if not specified, default to meters elseif (nargin < 1 || nargin > 3 ) usage("vol2metric(unitstr [, val, metstr] )"); endif if (! (isstr(unitstr) && isstr(metstr))) usage("vol2metric(unitstr [, val, metstr] )"); endif %% definitions %% all definitions of value to convert to m^3 %% m^3 has been chosen for consistency of use, %% as vol2metric and area2metric also default to m^3. %% cubic_inch = (len2metric("inch")) ^3; %% cubic inch is (sic) an inch to the power of three. USgallon = 231 * cubic_inch; %% 1 gallon is 231 cubic inch. bushel = 2150.42 * cubic_inch; %% base for many dry measures. IMPgallon = 4.54609E-3; %% see NIST: Defined exact. %% check for input length unit, determine conversion factor switch (unitstr) case {"in3", "cubic-inch"} fv = cubic_inch; case {"ft3", "cubic-foot"} fv = (len2metric("foot")) ^3; case {"acrefoot"} fv = area2metric("acre") * len2metric("USsurvey-foot"); %% quite a fun concept: case {"mi3"; "cubic-mile"} fv = (len2metric("mile")) ^3; case {"yd3", "cubic-yard"} fv = (len2metric("yard")) ^3; case {"US-gal"; "US-gallon"} fv = USgallon; case {"IMP-gal", "IMP-gallon", "UK-gal", "UK-gallon"} fv = IMPgallon; case {"US-floz", "US-fluid-ounce"} fv = USgallon / 128; %% 16 US floz. equal 1 pint, therefore 128 floz. make up a US gallon. case {"Imperial-fluid-ounce", "IMP-floz"} fv = IMPfluidounce = 2.841306E-5; case {"barrel", "bbl"} fv = 42 * USgallon; case {"US-liquid-pint"; "US-liqpt"} fv = USgallon / 8; case {"US-liquid-quart"; "US-liqqt"} fv = USgallon / 4; case {"bushel", "bu"} fv = bushel; case {"peck"; "pk"} fv = bushel / 4; case {"US-dry-gallon", "US-drygl"} fv = US_dry_gallon; case {"US-dry-quart"; "US-dryqt"} fv = vol2metric("US-dry-gallon") / 4; case {"US-dry-pint"; "US-drypt"} fv = vol2metric("US-dry-quart") / 2; %% 2 dry pint are one dry quart. case {"tablespoon", "tbsp"} fv = vol2metric("US-floz") / 2; %% tablespoon is 1/16 cup, or 1/2 a floz. case {"cup"} fv = vol2metric("US-floz") * 8; case {"teaspoon"} fv = vol2metric("tablespoon") / 3; case {"register ton"} fv = 100 * cubic_inch; case {"hogshead"} fv = 63 * USgallon; case {"firkin"} fv = 9 * USgallon; case {"cord"} fv = 128 * vol2metric("ft3"); case {"US-gill", "US-gi"} fv = vol2metric("US-liqpt") / 4; %% 4 gills are one (liquid) pint. case {"UK-gill", "CDN-gill", "IMP-gill", "IMP-gi"} fv = 1.420653E-4; %% %% allow metric units for input, also case {"m3", "stere", "st"} %% meters cubed fv = 1; %% Note: stere, or short st, is usually used only for wood measures. case {"km3"} %% kilometer cubed fv = 1E9; case {"dm3", "l", "L", "liter", "Liter"} fv = 1E-3; case {"cm3"} %% centimeter cubed fv = 1E-6; case {"mm3"} %% millimeter cubed fv = 1E-9; case {"um3"} %% micrometer cubed fv = 1E-18; case {"nm3"} %% nanometer cubed fv = 1E-27; otherwise error("vol2metric: unrecognized option"); endswitch; switch (metstr) case {"m3"} %% meters cubed fm = 1; case {"km3"} %% kilometer cubed fm = 1E-9; case {"dm3", "l", "L", "liter", "Liter"} fm = 1E3; case {"dm3"} %% decimeter cubed, equals Liters fm = 1E3; case {"cm3"} %% centimeter cubed fm = 1E6; case {"mm3"} %% millimeter cubed fm = 1E9; case {"um3"} %% micrometer cubed fm = 1E18; case {"nm3"} %% nanometer cubed fm = 1E27; otherwise error("vol2metric: unrecognized option"); endswitch v = val * fv * fm; endfunction --------------Boundary-00=_LVKDXTUXW64KL8KMD6QC--