function [Nut,NutMatrix]=nutation1980(JD,Units,MatType)
%--------------------------------------------------------------------------
% nutation1984 function                                              ephem
% Description: Calculate the IAU 1980 Nutation series for a set of JDs.
% Input  : - Column vector of JDs.
%          - Output units {'rad','deg','as','mas'}. Default is 'rad'.
%          - Type of nutation matrix (see nutation2rotmat.m):
%            'f' : full precision, (default).
%            'l' : linearized matrix.
% Output : - A two column matrix of nutation in [long., obliq].
%          - A cube of nutation rotation matrices (see nutation2rotmat.m).
% Reference: http://hpiers.obspm.fr/eop-pc/models/nutations/nut.html
% Tested : Matlab R2014a
%     By : Eran O. Ofek                    Jun 2014
%    URL : http://weizmann.ac.il/home/eofek/matlab/
% Example: [Nut,NutMatrix]=nutation1984(2451545+[1:1:100].');
% Reliable: 2
%--------------------------------------------------------------------------

RAD = 180./pi;

Def.Units = 'rad';
Def.MatType = 'f';
if (nargin==1),
     Units = Def.Units;
     MatType = Def.MatType;
elseif (nargin==2),
     MatType = Def.MatType;
else
    % do nothing
end


T   = (JD.' - 2451545.0)./36525.0;

% Mean elongation of the Moon from the Sun:
D = (297.85036 + 445267.111480.*T - 0.0019142.*T.*T + T.*T.*T./189474)./RAD;

% Mean anomaly of the Sun (Earth):
M = (357.52772 + 35999.050340.*T - 0.0001603.*T.*T - T.*T.*T./300000)./RAD;

% Mean anomaly of the Moon:
Mt = (134.96298 + 477198.867398.*T + 0.0086972.*T.*T + T.*T.*T./56250)./RAD;

% Moon's argument of latitude
F = (93.27191 + 483202.017538.*T - 0.0036825.*T.*T + T.*T.*T./327270)./RAD;

% Longitude of ascending node of the Moon's mean orbit
Om = (125.04452 - 1934.136261.*T + 0.0020708.*T.*T + T.*T.*T./450000)./RAD;

% nutation in longitude [ 0."0001] - nutation in obliquity [ 0."0001]
% lm ls F D Om   period psi                   eps
% Mt M F D Om
Table=[  0  0  0  0  1 -6798.4 -171996.0    -174.2   92025.0       8.9     
  0  0  2 -2  2   182.6  -13187.0      -1.6    5736.0      -3.1     
  0  0  2  0  2    13.7   -2274.0      -0.2     977.0      -0.5     
  0  0  0  0  2 -3399.2    2062.0       0.2    -895.0       0.5     
  0 -1  0  0  0  -365.3   -1426.0       3.4      54.0      -0.1     
  1  0  0  0  0    27.6     712.0       0.1      -7.0       0.0     
  0  1  2 -2  2   121.7    -517.0       1.2     224.0      -0.6     
  0  0  2  0  1    13.6    -386.0      -0.4     200.0       0.0     
  1  0  2  0  2     9.1    -301.0       0.0     129.0      -0.1     
  0 -1  2 -2  2   365.2     217.0      -0.5     -95.0       0.3     
 -1  0  0  2  0    31.8     158.0       0.0      -1.0       0.0     
  0  0  2 -2  1   177.8     129.0       0.1     -70.0       0.0     
 -1  0  2  0  2    27.1     123.0       0.0     -53.0       0.0     
  1  0  0  0  1    27.7      63.0       0.1     -33.0       0.0     
  0  0  0  2  0    14.8      63.0       0.0      -2.0       0.0     
 -1  0  2  2  2     9.6     -59.0       0.0      26.0       0.0     
 -1  0  0  0  1   -27.4     -58.0      -0.1      32.0       0.0     
  1  0  2  0  1     9.1     -51.0       0.0      27.0       0.0     
 -2  0  0  2  0  -205.9     -48.0       0.0       1.0       0.0     
 -2  0  2  0  1  1305.5      46.0       0.0     -24.0       0.0     
  0  0  2  2  2     7.1     -38.0       0.0      16.0       0.0     
  2  0  2  0  2     6.9     -31.0       0.0      13.0       0.0     
  2  0  0  0  0    13.8      29.0       0.0      -1.0       0.0     
  1  0  2 -2  2    23.9      29.0       0.0     -12.0       0.0     
  0  0  2  0  0    13.6      26.0       0.0      -1.0       0.0     
  0  0  2 -2  0   173.3     -22.0       0.0       0.0       0.0     
 -1  0  2  0  1    27.0      21.0       0.0     -10.0       0.0     
  0  2  0  0  0   182.6      17.0      -0.1       0.0       0.0     
  0  2  2 -2  2    91.3     -16.0       0.1       7.0       0.0     
 -1  0  0  2  1    32.0      16.0       0.0      -8.0       0.0     
  0  1  0  0  1   386.0     -15.0       0.0       9.0       0.0     
  1  0  0 -2  1   -31.7     -13.0       0.0       7.0       0.0     
  0 -1  0  0  1  -346.6     -12.0       0.0       6.0       0.0     
  2  0 -2  0  0 -1095.2      11.0       0.0       0.0       0.0     
 -1  0  2  2  1     9.5     -10.0       0.0       5.0       0.0     
  1  0  2  2  2     5.6      -8.0       0.0       3.0       0.0     
  0 -1  2  0  2    14.2      -7.0       0.0       3.0       0.0     
  0  0  2  2  1     7.1      -7.0       0.0       3.0       0.0     
  1  1  0 -2  0   -34.8      -7.0       0.0       0.0       0.0     
  0  1  2  0  2    13.2       7.0       0.0      -3.0       0.0     
 -2  0  0  2  1  -199.8      -6.0       0.0       3.0       0.0     
  0  0  0  2  1    14.8      -6.0       0.0       3.0       0.0     
  2  0  2 -2  2    12.8       6.0       0.0      -3.0       0.0     
  1  0  0  2  0     9.6       6.0       0.0       0.0       0.0     
  1  0  2 -2  1    23.9       6.0       0.0      -3.0       0.0     
  0  0  0 -2  1   -14.7      -5.0       0.0       3.0       0.0     
  0 -1  2 -2  1   346.6      -5.0       0.0       3.0       0.0     
  2  0  2  0  1     6.9      -5.0       0.0       3.0       0.0     
  1 -1  0  0  0    29.8       5.0       0.0       0.0       0.0     
  1  0  0 -1  0   411.8      -4.0       0.0       0.0       0.0     
  0  0  0  1  0    29.5      -4.0       0.0       0.0       0.0     
  0  1  0 -2  0   -15.4      -4.0       0.0       0.0       0.0     
  1  0 -2  0  0   -26.9       4.0       0.0       0.0       0.0     
  2  0  0 -2  1   212.3       4.0       0.0      -2.0       0.0     
  0  1  2 -2  1   119.6       4.0       0.0      -2.0       0.0     
  1  1  0  0  0    25.6      -3.0       0.0       0.0       0.0     
  1 -1  0 -1  0 -3232.9      -3.0       0.0       0.0       0.0     
 -1 -1  2  2  2     9.8      -3.0       0.0       1.0       0.0     
  0 -1  2  2  2     7.2      -3.0       0.0       1.0       0.0     
  1 -1  2  0  2     9.4      -3.0       0.0       1.0       0.0     
  3  0  2  0  2     5.5      -3.0       0.0       1.0       0.0     
 -2  0  2  0  2  1615.7      -3.0       0.0       1.0       0.0     
  1  0  2  0  0     9.1       3.0       0.0       0.0       0.0     
 -1  0  2  4  2     5.8      -2.0       0.0       1.0       0.0     
  1  0  0  0  2    27.8      -2.0       0.0       1.0       0.0     
 -1  0  2 -2  1   -32.6      -2.0       0.0       1.0       0.0     
  0 -2  2 -2  1  6786.3      -2.0       0.0       1.0       0.0     
 -2  0  0  0  1   -13.7      -2.0       0.0       1.0       0.0     
  2  0  0  0  1    13.8       2.0       0.0      -1.0       0.0     
  3  0  0  0  0     9.2       2.0       0.0       0.0       0.0     
  1  1  2  0  2     8.9       2.0       0.0      -1.0       0.0     
  0  0  2  1  2     9.3       2.0       0.0      -1.0       0.0     
  1  0  0  2  1     9.6      -1.0       0.0       0.0       0.0     
  1  0  2  2  1     5.6      -1.0       0.0       1.0       0.0     
  1  1  0 -2  1   -34.7      -1.0       0.0       0.0       0.0     
  0  1  0  2  0    14.2      -1.0       0.0       0.0       0.0     
  0  1  2 -2  0   117.5      -1.0       0.0       0.0       0.0     
  0  1 -2  2  0  -329.8      -1.0       0.0       0.0       0.0     
  1  0 -2  2  0    23.8      -1.0       0.0       0.0       0.0     
  1  0 -2 -2  0    -9.5      -1.0       0.0       0.0       0.0     
  1  0  2 -2  0    32.8      -1.0       0.0       0.0       0.0     
  1  0  0 -4  0   -10.1      -1.0       0.0       0.0       0.0     
  2  0  0 -4  0   -15.9      -1.0       0.0       0.0       0.0     
  0  0  2  4  2     4.8      -1.0       0.0       0.0       0.0     
  0  0  2 -1  2    25.4      -1.0       0.0       0.0       0.0     
 -2  0  2  4  2     7.3      -1.0       0.0       1.0       0.0     
  2  0  2  2  2     4.7      -1.0       0.0       0.0       0.0     
  0 -1  2  0  1    14.2      -1.0       0.0       0.0       0.0     
  0  0 -2  0  1   -13.6      -1.0       0.0       0.0       0.0     
  0  0  4 -2  2    12.7       1.0       0.0       0.0       0.0     
  0  1  0  0  2   409.2       1.0       0.0       0.0       0.0     
  1  1  2 -2  2    22.5       1.0       0.0      -1.0       0.0     
  3  0  2 -2  2     8.7       1.0       0.0       0.0       0.0     
 -2  0  2  2  2    14.6       1.0       0.0      -1.0       0.0     
 -1  0  0  0  2   -27.3       1.0       0.0      -1.0       0.0     
  0  0 -2  2  1  -169.0       1.0       0.0       0.0       0.0     
  0  1  2  0  1    13.1       1.0       0.0       0.0       0.0     
 -1  0  4  0  2     9.1       1.0       0.0       0.0       0.0     
  2  1  0 -2  0   131.7       1.0       0.0       0.0       0.0     
  2  0  0  2  0     7.1       1.0       0.0       0.0       0.0     
  2  0  2 -2  1    12.8       1.0       0.0      -1.0       0.0     
  2  0 -2  0  1  -943.2       1.0       0.0       0.0       0.0     
  1 -1  0 -2  0   -29.3       1.0       0.0       0.0       0.0     
 -1  0  0  1  1  -388.3       1.0       0.0       0.0       0.0     
 -1 -1  0  2  1    35.0       1.0       0.0       0.0       0.0     
  0  1  0  1  0    27.3       1.0       0.0       0.0       0.0];

DLon = bsxfun(@plus,Table(:,7),Table(:,8)*T).*sin(Table(:,1)*Mt + Table(:,2)*M + Table(:,3)*F + Table(:,4)*D + Table(:,5)*Om);

DObl = bsxfun(@plus,Table(:,9),Table(:,10)*T).*cos(Table(:,1)*Mt + Table(:,2)*M + Table(:,3)*F + Table(:,4)*D + Table(:,5)*Om);

% convert to arcsec
DLon = sum(DLon).'.*0.0001; %./(3600.*RAD);
DObl = sum(DObl).'.*0.0001; %./(3600.*RAD);
switch lower(Units)
    case 'rad'
        ConvFactor = 1./(RAD.*3600);
    case 'deg'
        ConvFactor = 1./RAD;
    case 'as'
        ConvFactor = 1;
    case 'mas'
        ConvFactor = 1000;
    otherwise
        error('Unknown Units option');
end
DLon = DLon.*ConvFactor;
DObl = DObl.*ConvFactor;

Nut = [DLon, DObl];
   
if (nargout>1),
    NutMatrix = nutation2rotmat(Nut,JD,MatType);
end

