function Dateout=date_transform(Date,unitsin,unitsout)
%C transforms Date or time informations from one unit into another
%C units accepted are:
%C seconds since DATESTR
%C minutes since DATESTR
%C hours since DATESTR
%C days since DATESTR
%C years since DATESTR
%C year (years)
%C julday (juldays; julian days)
%
% for output in addition:
% yearday
% for input in addition:
% month since DATESTR  (als Integer werte)
%  
% J.Holfort  
  
if nargin~=3; error('exactly 3 arguments needed'); end

time0_in=[0 0];
time0_out=[0 0];

%C Unit der Eingabe entschluesseln 
text=upper(unitsin);
if strcmpi(text,'datenum'); text='DAYS' ;time0_in(1)=1721059; end
i_sec =index(text,'SECOND');
i_min =index(text,'MINUTE');
i_hou =index(text,'HOUR');
i_day =index(text,'DAY');
i_jul =index(text,'JULDAY');
if(i_jul<=0  &  i_day>0)
    i_jul =index(text,'JULIAN');
end
i_month=index(text,'MONTH');
i_year=index(text,'YEAR');

% ueberpruefen ob nicht was doppelt
if(i_day > 0  &  i_jul > 0); i_day=0; end
I_ein=0;
if(i_sec  > 0) I_ein=I_ein+1; end
if(i_min  > 0) I_ein=I_ein+1; end
if(i_hou  > 0) I_ein=I_ein+1; end
if(i_day  > 0) I_ein=I_ein+1; end
if(i_jul  > 0) I_ein=I_ein+1; end
if(i_month> 0) I_ein=I_ein+1; end
if(i_year > 0) I_ein=I_ein+1; end
if(I_ein > 1  |  I_ein <= 0); 
    Dateout=Date;
    fehler(-1,text); return
end

i_since=index(text,'SINCE');
if(i_since > 0) 
    text=text(i_since+6:end);
    dumy=zeitdecode(text,'JUL','YYYY');
    time0_in(1)=fix(dumy);  % as with since 1/1/1940 
    % it is assumed that the 1/1/1940 is day zero
    time0_in(2)=mod(dumy,1)*24*60;
end
if(i_sec > 0 & i_since <=0); fehler(2,text); end
if(i_min > 0 & i_since <=0); fehler(2,text); end
if(i_jul > 0 & i_since > 0) 
    fprintf('%s\n','date_transfrom warning');
    fprintf('%s\n','julian day with since, assume it is days');
end
%%if(i_since <=0 & i_year > 0)
%%   time0_in(1)=julianday(1,1,-1);
%%end

%C Unit der Ausgabe entschluesseln 
text=upper(unitsout);
if strcmpi(text,'datenum'); text='DAYS' ;time0_out(1)=1721059; end
j_sec =index(text,'SECOND');
j_min =index(text,'MINUTE');
j_hou =index(text,'HOUR');
j_day =index(text,'DAY');
j_jul =index(text,'JULDAY');
if(j_jul <=0  &  j_day > 0) 
    j_jul =index(text,'JULIAN');
end
j_year=index(text,'YEAR');
j_yearday=index(text,'YEARDAY');
if j_yearday > 0;
    j_day=0;
end

%! ueberpruefen ob nicht was doppelt
if(j_day > 0  &  j_jul > 0); j_day=0; end
J_ein=0;
if(j_sec  > 0) J_ein=J_ein+1; end
if(j_min  > 0) J_ein=J_ein+1; end
if(j_hou  > 0) J_ein=J_ein+1; end
if(j_day  > 0) J_ein=J_ein+1; end
if(j_jul  > 0) J_ein=J_ein+1; end
if(j_year > 0) J_ein=J_ein+1; end
if(J_ein > 1  |  J_ein <= 0); fehler(1,text); end


j_since=index(text,'SINCE');
if(j_since > 0)
    text=text(j_since+6:end);
    dumy=zeitdecode(text,'JUL','YYYY');
    time0_out(1)=fix(dumy)+0;  % JHXX23 +0 instead of +1
    time0_out(2)=mod(dumy,1)*24*60;
end
if(j_min > 0 & j_since <=0); fehler(2,text); end
if(j_sec > 0 & j_since <=0); fehler(2,text); end
if(j_jul > 0 & j_since > 0)
    fprintf('%s\n','date_transfrom warning');
    fprintf('%s\n',['julian day with since, assume it is' ...
            ' days']);
end
if(j_since <=0 & j_year > 0)
   time0_out(1)=julianday(1,1,0);
end

if(j_since > 0 & j_yearday)
    error('output yearday with since is nonsense')
end
if i_year>0  % transform into julday,minute
    IYYY=fix(Date);
    YD=julianday(1,1,IYYY+1)-julianday(1,1,IYYY);
    Date=julianday(1,1,IYYY)+mod(Date,1).*YD;
    JULDAY=fix(Date)+time0_in(1);
    MINUTE=(Date-fix(Date))*60*24+time0_in(2);
elseif (i_sec > 0)
    JULDAY=time0_in(1)+floor(Date/(24*60*60));
    MINUTE=rem(Date,(24*60*60))/60 +time0_in(2);
elseif (i_min > 0)
    JULDAY=time0_in(1)+floor(Date/(24*60));
    MINUTE=rem(Date,(24*60)) +time0_in(2);
elseif (i_hou > 0)
    JULDAY=time0_in(1)+floor(Date/(24));
    MINUTE=rem(Date,24)*60. +time0_in(2);
elseif (i_day > 0 | i_jul )
    JULDAY=fix(Date)+time0_in(1);
    MINUTE=(Date-fix(Date))*60*24+time0_in(2);
elseif (i_month > 0)
    [ID,MM,IYYY]=dayjulian(time0_in(1));
    Date=Date+MM;
    MINUTE=0;
    for ii=1:length(Date)
        jj=floor(mod(Date(ii)+MM,12)); if jj==0; jj=12; end
        kk=IYYY+floor(Date(ii)/12);
        if jj==0; jj=12; kk=kk-1; end
        JULDAY(ii)=julianday(ID,jj,kk);
    end
end
if (j_sec > 0)
    Date=(JULDAY-time0_out(1))*(60*24)...
        +MINUTE-time0_out(2)*60.;
elseif (j_min > 0)
    Date=(JULDAY-time0_out(1))*(60*24)...
        +MINUTE-time0_out(2);
elseif (j_hou > 0)
    Date=(JULDAY-time0_out(1))*(24)...
        +(MINUTE-time0_out(2))/60;
elseif(j_day > 0 | j_jul )
    Date=(JULDAY-time0_out(1))...
        +(MINUTE-time0_out(2))/(60*24)          ;
elseif(j_year > 0) 
    [ID,MM,IYYY]=dayjulian(JULDAY);
    if IYYY<0; YD=365; else;
    YD=julianday(1,1,IYYY+1)-julianday(1,1,IYYY); end;
    Date=IYYY+(MINUTE/(60*24)+JULDAY-julianday(1,1,IYYY))./YD;
    %if time0_out(1)~=julianday(1,1,-1)
      [ID,MM,IYYY]=dayjulian(time0_out(1));
      if IYYY==-1; YD=365; else;
      YD=julianday(1,1,IYYY+1)-julianday(1,1,IYYY); end;
      Date=Date-(IYYY+(time0_out(2)+julianday(ID,MM,IYYY)-julianday(1,1,IYYY))./YD);
      if IYYY<0; Date=Date-1; end
    %end
end
Dateout=Date;
if(j_yearday > 0);
    Dateout=rem(Dateout,1)*365.; % yearday 1 ist erst um 24:00 Uhr am ersten Januar
end
return

function []=fehler(i,text)
if abs(i)==1
    fprintf('%s\n','error in datetransform, unclear unit');
    fprintf('%s\n',text);
    if(i>0) error('1'); end
    %warning('1, output date equals inputdate')
elseif i==2
    fprintf('%s\n','error in datetransform');
    fprintf('%s\n','minutes or seconds have to have a since label');
    fprintf('%s\n',text);
    error('2');
end
return

function ii=index(text,str)
jj=strfind(text,str);
if isempty(jj); ii=0; else ii=jj(1); end
return
