function [GDHOUR,T]=growdh(MX,MN,DAYLEN,BASET,T)
% Calculate growing degree hours. 
% Usage:
%   [GDHOUR,T]=growdh(MX,MN,DAYLEN,BASET,T)
% 
% Input:
%  MX........Maximum value
%  MN........Minimum values
%  DAYLEN....Length of day
%  BASET.....Base temperature (values above this threshold count
%            towards growting degree hours).
%  T.........Temperature curve generated by this program (appears
%            as input b/c of Fortran legacy)
% Output
%  GDHOUR....Growing degree hours 
%  T.........Temperature curve modeled from MX and MN
    
    
    if MN == 0; MN=0.01; end
    if MX == MN; MX=MX+0.01; end
    DT=MX-MN;
    IDL=fix(DAYLEN);
    %     CALCULATE HOURLY TEMPERATURES (DAYTIME)
    T(1)=MN;
    T(2:IDL+1)=DT*sin(pi()/(DAYLEN+4)*(1:IDL))+MN; 

    %     CALCULATE SUNSET TIME AND TEMPERATURE
    TS1=DT*sin(pi()/(DAYLEN+4)*DAYLEN)+MN;
    if TS1 <=0; TS1=0.01; end

    %     CALCULATE HOURLY TEMPERATURES (NIGHTTIME)
    T(IDL+2:end)=TS1-(TS1-MN)/(log(24-DAYLEN))*log((1:(23-IDL)));

    %     GROWING DEGREE HOURS
    GHRS=T-BASET;
    GHRS(GHRS<0)=0;
    GDHOUR=sum(GHRS);

end