function [jdk,m2,m4]=demod(jd,w);
%function [jdk,m2,m4]=demod(jd,w);
%running tidal analysis for M2 tide (assuming hourly data)
[m,n]=size(w);
%for jj=1:n,
  jj=1
  k=0
  for i=1:6:m,
    k=k+1;
    ind=i:min(i+23,m);
    if(length(find(w(ind)==NaN))==0),
      ucoef=tidefit(jd(ind),real(w(ind,jj)));
      vcoef=tidefit(jd(ind),imag(w(ind,jj)));
      m2(k,:)=ellipse(ucoef(2,:),ucoef(3,:),vcoef(2,:),vcoef(3,:));
  % M4
      m4(k,:)=ellipse(ucoef(4,:),ucoef(5,:),vcoef(4,:),vcoef(5,:));
    else
      m2(k,:)=NaN*ones(1,4);
      m4(k,:)=NaN*ones(1,4);
    end
    jdk(k)=jd(i+12);
  end
