function o = ox_units(ox,salt,temp,from_to);
% OX_UNITS - converts between 3 different oxygen units
%              mL/L, micromole/kg, micromole/L
%
% INPUTS:
%        ox: oxygen in original units
%        salt: salinity in PSU
%        temp: potential temperature.
%        from_to: string indicating original and desired units.
%                 possible cases are:
%                 mll2mml: mL/L to micromoles/L
%                 mll2mmk: mL/L to micromoles/kg
%                 mml2mmk: micromoles/L to micromoles/kg
%                 mml2mll: micromoles/L to mL/L
%                 mmk2mll: micromoles/kg to mL/L
%                 mmk2mml: micromoles/kg to micromoles/L
%           
%
%  Usage: o = ox_units(ox,salt,potentialtemp,from_to);
%

% Derrick Snowden, 2002;
%
% Based on code by
% Paul Robbins 1995
%
% 
% VERSION: 
%	$Revision: 1.5 $
%	$Date: 2008/02/18 19:37:50 $
%	$Id: ox_units.m,v 1.5 2008/02/18 19:37:50 duncombe Exp duncombe $
%
% CHANGELOG: 
%

if nargin < 4
    disp(['No units indicator (from_to) was passed.'])
    disp('')
    disp(['Valid units are:']);
    print_usage(1);
end

% First compute potential density assuming temp is potential temp
% equivalent to sw_pden(salt,temp,press,press_ref==0) if temp is in situ
pdens = sw_dens(salt,temp,0);

switch from_to
case 'mll2mmk' %ml/l to micromoles/kg
   o = ox*1e3./pdens/.022403;
case 'mll2mml' %ml/l to micromoles/l
   o = (ox*1e3./pdens/.022403).*pdens./1000;
case 'mml2mmk' % micromoles/l to micromoles/kg
   o = ox*1000./pdens;
case 'mml2mll' % micromoles/l to ml/l
   o = (ox.*1000./pdens) .* (.022403 .* pdens ./ 1000);
case 'mmk2mll' % micromoles/kg to ml/l
   o = ox .* (0.022403 .* pdens ./ 1000);
case 'mmk2mml' % micromoles/kg to micromoles/l
   o = ox.*pdens./1000;

otherwise
   disp(['Incorrect units passed to ox_units.'])
   disp('')
   disp(['Valid units are:']);
   print_usage(1);
end

return

function [] = print_usage(err)

disp(['mll2mml: ml/l to micromoles/l']);
   disp(['mll2mmk: ml/l to micromoles/kg']);
   disp(['mml2mmk: micromoles/l to micromoles/kg']);
   disp(['mml2mll: micromoles/l to ml/l']);
   disp(['mmk2mll: micromoles/kg to ml/l']);
   disp(['mmk2mml: micromoles/kg to micromoles/l']);
   disp('')
   error('Set from_to to one of the strings above;');
return
