function [yyyy,doy]=yyyymmdd2doy(yyyymmdd);
% function [yyyy,doy]=yyyymmdd2doy(yyyymmdd);
%

yyyy = fix(yyyymmdd/10000);
mm = fix((yyyymmdd - yyyy*10000)/100);
dd = yyyymmdd - yyyy*10000 - mm*100;


leapyears = [1988 1992 1996 2000 2004 2008 2012 2016 2020 2024];

iy=find(yyyy==leapyears);

doy=0;
mcount=1;
while (mcount < mm)
   if (mcount == 1 | mcount == 3 | mcount == 5 | mcount == 7 | mcount == 8 | mcount == 10 | mcount == 12) 
      doy = doy + 31;
   elseif (mcount == 4 | mcount == 6 | mcount == 9 | mcount == 11 ) 
      doy = doy + 30;
   elseif (mcount == 2 ) 
      if (length(iy)==0) 
         doy = doy + 28;
      else
         doy = doy + 29;
      end
   else
      disp('problem: month not 1-12');
   end
   mcount=mcount+1;
end
doy = doy + dd;
