function [Au, PHIu, Av, PHIv, w]=ep2app(SEMA, ECC, INC, PHA)
%
% Convert tidal ellipse parameters into amplitude and phase lag parameters.
% Its inverse is app2ep.m
%
% Zhigang Xu
% April 6, 2000
%
% Document:  tidal_ellipse_v2.ps
% 
   Wp = (1+ECC)/2*SEMA;
   Wm = (1-ECC)/2*SEMA;
   THETAp = INC-PHA;
   THETAm = INC+PHA;

   %convert degress into radians
   THETAp = THETAp/180*pi;
   THETAm = THETAm/180*pi;

   %Calculate wp and wm.
   wp = Wp.*exp(-i*THETAp);
   wm = Wm.*exp( i*THETAm);
   
   if nargout == 5
      dot = pi/18;
      ot = [0:dot:2*pi-dot];
      w = wp'.*exp(i*ot)+wm.*exp(-i*ot);
   end

   % Calculate cAu, cAv --- complex amplitude of u and v
   cAu = wp+wm;
   cAv = -i*(wm-wp);
   Au  = abs(cAu);
   Av  = abs(cAv);   
   PHIu = angle(cAu)*180/pi;
   PHIv = angle(cAv)*180/pi;
   
   % flip angles in the range of [-180 0) to the range of [180 360).
   id = PHIu < 0; PHIu(id) = PHIu(id) + 360;
   id = PHIv < 0; PHIv(id) = PHIv(id) + 360;

%Authorship Copyright:
%
%    The author of this program retains the copyright of this program, while
% you are welcome to use and distribute this program as long as you credit 
% the author properly. Particularly, you are expected to retain the
% origianl author's name in this original version of the program 
% or any of its modifcations that you might make. Enjoying my program!
%
%Author Info:
%_______________________________________________________________________
%  Zhigang Xu, Ph.D.                            
%  (pronounced as Tsi Gahng Hsu)
%  Research Scientist
%  Coastal Circulation                   
%  Bedford Institute of Oceanography     
%  1 Challenge Dr.
%  P.O. Box 1006                    Phone  (902) 426-2307 (o)       
%  Dartmouth, Nova Scotia           Fax    (902) 426-7827            
%  CANADA B2Y 4A2                   email zhigangx@emerald.bio.dfo.ca    
%_______________________________________________________________________
%
