From octave-sources-request at bevo dot che dot wisc dot edu Mon Jan 27 14:10:38 2003 Subject: len2metric.m From: Stefan Burger To: octave-sources at bevo dot che dot wisc dot edu Date: Mon, 27 Jan 2003 07:44:39 -0600 --------------Boundary-00=_NVKDELGPOPY7XD8Y209C Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable --------------Boundary-00=_NVKDELGPOPY7XD8Y209C Content-Type: text/plain; charset="us-ascii"; name="len2metric.m" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="len2metric.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} {} len2metric (@var{unitstr}, @var{val}, @var{metstr}, ) ## Converts non-SI or US length measures to metric 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 'm', meters. ## ## Examples: ## ## Convert 5.5 inch to millimeters (mm): ## result = len2metric("inch", 5.5, "mm"). ## ## Convert 3.8 miles to meters (m): result = len2metric("mile", 3.8, "m"). ## In this example, "m" does not need to be specified, as meters are default. ## Alternatively, it can be used this way: result = 3.8 * len2metric("mile") ## ## Converting a vector or matrix of numbers: ## len2metric("ft", [1 2; 3 4]) ## converts the matrix of values to a matrix of same size, with values in meters. ## ans = ## 0.91440 1.82880 ## 2.74320 3.65760 ## Note: The same can be achieved this way: ## result = [1 2; 3 4] * len2metric("ft"). ## ## Currently, the following units are supported: ## in, inch, ft, foot, USsurveyfoot, yd, yard, mi, mile, USsurveymile, fathom, ## nauticmile, mil, rd, rod, chain, ## micron, angstrom, astronomicalunit, AU ## ## Metric units: ## km, m, dm, cm, mm, um, nm. ## ## References: ## See NIST Guide to SI units, Appendix B8, ## at www.nist.gov/Pubs/SP811/appenB8.html ## ## at end deftypefn ## at seealso{metric2len, square2metric, vol2metric, weight2metric, force2metric} ## Author: Stefan Burger ## Description: converts non-SI length measures to SI unit measures. function v = len2metric (unitstr, val, metstr) if (nargin ==1) val = 1.0; metstr = "m"; %% if no value given, default to one. %% if no metric string given, default to meters. %% reason: usage val * len2metric("inch") endif if (nargin == 2) metstr = "m"; %% if not specified, default to meters elseif (nargin < 1 || nargin > 3 ) usage("len2metric(unitstr [, val, metstr])"); endif if (! (isstr(unitstr) && isstr(metstr))) usage("len2metric(unitstr [, val, metstr])"); endif %% %% definitions %% inch = 2.54E-2; %% 1 inch equals 0.0254m %% references cite this value as an exact value. %% basis for most other values that relate to inch foot = 12 * inch; USsurveyfoot = 1200 / 3937; %% old definition for foot in the U.S., %% relative difference of 2e-6 to international foot. %% still in use, esp. for geographic data, %% also base for several other derived units, such as fathom, acre, etc. %% again mostly in geographic area. %% exact value, see references. %% Start of conversion routines: %% check for input length unit, determine conversion factor %% switch (unitstr) case {"in"; "inch"} fv = inch; case {"USsurvey-inch"} fv = USsurveyfoot / 12; %% Don't know wether this is relevant, but let's keep it. case {"ft"; "foot"} fv = foot; case {"USsurvey-foot"} fv = USsurveyfoot; case {"mi"; "mile"} fv = 5280 * foot; %% 1 mile equals 5280 foot.; case {"USsurvey-mile"} fv = 5280 * USsurveyfoot; %% same as mile, but based on old inch, %% used for geographic measures. case {"nauticmile"} fv = 1852; %% nautic mile defined as 1852m. case {"yd"; "yard"} fv = 3 * foot; %% one yard equals 3 foot. case {"fathom"} fv = 6 * USsurveyfoot; case {"mil"} fv = inch*1E-3; case {"rd"; "rod"} fv = 16.5 * USsurveyfoot; %% rod is still derived from the old defintion of inch in the U.S. case {"chain"} fv = 66 * USsurveyfoot; %% 4 rods make up one chain. case {"angstrom"} fv = 1E-10; case {"astronomicalunit"; "AU"} fv = 1.495979E11; case {"micron"} fv = 1E-6; %% %% Case also metric units as input units: %% Be able to convert from (metric) len to metric len, %% e.g., km to m. case {"m", "meter"} fv = 1; case {"km", "kilometer"} fv = 1E3; case {"dm", "decimeter"} fv = 1E-1; case {"cm", centimeter} fv = 1E-2; case {"mm", "millimeter"} fv = 1E-3; case {"um", "micrometer"} fv = 1E-6; case {"nm", "nanometer"} fv = 1E-9; %% case {"version"} fv = 0.1; %% version number val = 1; %% override input, so that version is returned metstr = "m"; %% override input, so that version is returned %% Version number is added to be able to distinguish %% possible further versions of this file, and to check for %% versions that possibly contain errors. otherwise error("len2metric: unrecognized option"); endswitch %% determine the conversion factor from meters to desired %% metric unit. switch (metstr) case {"m", "meter"} fm = 1; case {"km", "kilometer"} fm = 1E-3; case {"dm", "decimeter"} fm = 1E1; case {"cm", "centimeter"} fm = 1E2; case {"mm", "millimeter"} fm = 1E3; case {"um", "micrometer"} fm = 1E6; case {"nm", "nanometer"} fm = 1E9; otherwise error("len2metric: unrecognized option"); endswitch %% The most simple part: Putting the result together. v = val* fv * fm; endfunction --------------Boundary-00=_NVKDELGPOPY7XD8Y209C--