function [amp,pha] = func_get_3d_ap_mod2(...
    M_tide_k,namet,post_06apr2015,...
    nobs,starttime,latitude)
%old:    time,centraltime,latitude)
%new:    nobs,starttime,latitude)

%30Aug2019: In this call, similar to within t_tide, the most important quantities are:
%length(time), starttime.  EVERYTHING ELSE is computed from these two quantities.
%The values of the vector [time] is not used anywhere else.  So, if we want to simplify
%the function, should just call it nobs=length(time) as an incoming argument and get rid of time.

%dirRoot='/nobackupp6/atnguye4/llc2160/global/';
%dirRoot='/work/03901/atnguyen/llc2160/global/';
%load([dirRoot 'matlab/tide_constituent.mat']);  %load in 35 tidal components got from t_tide
load(['~/matlab/atn_tools/tide_constituent.mat']);  %load in 35 tidal components got from t_tide
%load('/Users/atnguyen/DATA/Model/llc1080/aste1080x1260x540x90/run_tides_run2_hourly_it0000_pk0001577880/tide_constituent.mat');

%0. get the centraltime, identical to how t_tide gets it:
  dt=1;			%hourly, this is assumed, must make sure we always get hourly data 
  %nobs=length(time);
  nobsu=nobs-rem(nobs-1,2);
%7.may.2018: add centraltime correction below back in
  %centraltime=stime+floor(nobsu./2)./24.0*dt;	%daynum, input
  %centraltime=centraltime+floor(nobsu./2)./24.0*dt;	%daynum, input
%7.may.2018: done adding
%30Aug2019: since we're following the steps of t_tide, it's best to pass in the argument starttime, and
%then just calculate centraltime as below
  centraltime=starttime+floor(nobsu./2)./24.0*dt;	%exactly as in t_tide

if(post_06apr2015==0);
%this is the order of the tidal components
  nameu=['M2  ';'S2  ';'N2  ';'K1  ';'O1  ';'M4  ';'M6  ';'S4  ';'MS4 '];
  itideref=[15,17,14,8,6,24,31,27,26];             %grab 9 most important const.
  mu=size(nameu,1);
  isinref=[3:2:2*mu+1];
  icosref=[2:2:2*mu];
else;
  nameu=['Q1  ';'O1  ';'K1  ';'N2  ';'M2  ';'S2  '];
  mu=size(nameu,1);
  for k=1:size(nameu,1);
    itideref(k)=strmatch(nameu(k,:),tide_constituent.name);
  end
%tc=[ones(length(t),1) cos((2*pi)*t*fu') sin((2*pi)*t*fu') ]; from t_tide
  icosref=2:mu+1;
  isinref=(mu+1)+[1:mu];
end

%now matching the tide component we want:
itide=strmatch(namet,nameu);
isin =isinref(itide);
icos =icosref(itide);

%for ifile=1:3
%  if(ifile==1);fname=ufname;elseif(ifile==2);fname=vfname;else;fname=wfname;end;
  %fprintf('processing %s\n',fname);
  %load(fname);	%M_tide_k [nx ny 2*mu+1]

sz=size(M_tide_k);
  if(length(sz)==3);M_tide_k=reshape(M_tide_k,sz(1)*sz(2),sz(3));end;	%now 2d

  coscoef=M_tide_k(:,icos);
  sincoef=M_tide_k(:,isin);
  meancoef=M_tide_k(:,1);

%now making a series of steps to converting A and B to amplitude + phase and later add nodal correction
%first, convert to amp_plus and amp_minus (raw,not yet included nodal corr)
  ap=(coscoef-i.*sincoef)./2;	%this is correct, the "plus"  ap has a -i
  am=(coscoef+i.*sincoef)./2;	%this is correct, the "minus" am has a +i

%A. correct for prefiltering:
  fu=tide_constituent.freq(itideref(itide));
  corr_fs=[0 1e6];
  corr_fac=[1 1];
  corrfac=interp1(corr_fs,corr_fac,fu);
  corrfac(corrfac>100|corrfac<.01|isnan(corrfac))=1;
  for ifu=1:length(fu)
    ap(:,ifu)=ap(:,ifu).*corrfac(ifu);
    am(:,ifu)=am(:,ifu).*conj(corrfac(ifu));
  end

%B. nodal correction
%%to do nodal correction right, should use the function as identically from t_tide.  The reason we separate is so that we can pass in 2D/3D fields M_tide_k
%  ray=1;constitnames=nameu;dt=1;nobs=length(time);nobsu=nobs-rem(nobs-1,2);inf.iname=[];inf.irefname=[];shallownames=[];
%  [nameu,fu,ju,namei,fi,jinf,jref]=constituents(ray/(dt*nobsu),constitnames,shallownames,inf.iname,inf.irefname,centraltime);
%%for constitnames='M2  ': nameu=M2;fu=0.0805;ju=48;namei=[];fi=[];jinf=[];jref=[];
%
%for case start-time + lat + NO_REFERENCE (copying from t_tide,constituents, etc.)
  ltype='nodal';
  const=t_getconsts;
  ju=zeros(size(namet,1),1);
  for k=1:length(ju);ju(k)=strmatch(namet,const.name);end;%ju=48 for M2

  ii=find(latitude(:)==0);if(length(ii)>0);latitude(ii)=nan;end;
  [v,u,f]=t_vuf_2d(ltype,centraltime,ju,latitude);	%[0.5858 0.0020 1.0336] for M2, identical to from t_vuf
  vu=(v+u)*360;						% total phase correction (degrees)  , 211.6339 for M2 at centraltime=datenum(2014,1,1)+182.4583
  nodcor=...
 ['Greenwich phase computed with nodal corrections applied to amplitude \n and phase relative to center time'];

  epsp=angle(ap).*180./pi;
  epsm=angle(am).*180./pi;	%bug found 30Aug2019: /180, now changed to *180
  ap=abs(ap);
  am=abs(am);

  ap=ap./f;			% apply nodal correction
  am=am./f;			% apply nodal correction

  fmaj=ap+am;			% major axis, also amplitude
  fmin=ap-am;			% minor axis
  gp=mod(vu-epsp,360);		% pos. Greenwich phase in deg
  gm=mod(vu+epsm,360);		% neg. Greenwich phase in deg

  finc=(epsp+epsm)./2;
  finc=mod(finc,180);		% Ellipse inclination in deg
  finc=t_cluster_2d(finc,180);	% avoid 360deg wrap

  pha=mod(gp+finc,360);		% Greenwich phase in deg
  pha=t_cluster_2d(pha,360);	% avoid 360deg wrap

%atn: 7.may.2018: add real and imag part to amp, otherwise it's always just a real part
  %amp=fmaj;
  clear i
  amp=fmaj+i.*fmin;

%  if(ifile==1);
%    ua=fmaj;up=pha;
%  elseif(ifile==2);
%    va=fmaj;vp=pha;
%  else;
%    wa=fmaj;wp=pha;
%  end;

%end;

  if(length(sz)==3);
    amp=reshape(amp,sz(1),sz(2),length(fu));
    pha=reshape(pha,sz(1),sz(2),length(fu));
  end;

return
