
function [data_new]=match_old_new_Framstrait(data_g,data_a)

%---------
%function [prof_new]=match_old_new_Framstrait(prof_global,prof_aste)
% function to expand trimmed Framstrait file to global size
% Input:
%  data_g: global data set, 77565 valid profiles, 85 depth lev
%  data_a: can either be global or trimmed (aste)
%  if data_a = aste, expand to global with appropriate nan
%  if data_a = global, blanking out appropriate nan
% Output:
%  data_new, of size data_g, with only 77269 valid profiles (remove 296), 63 klev
%
% atn 15.Aug.2017
%---------

%======= first filter: depth ================
  if(isfield(data_g,'prof_U')==0);
    fld={'T','Tweight','Testim','Terr','Tflag',...
         'S','Sweight','Sestim','Serr','Sflag'};
    tmp=sum(data_g.prof_Tweight,1)+sum(data_g.prof_Sweight,1);
  else;
    fld={'T','Tweight','Testim','Terr','Tflag',...
         'S','Sweight','Sestim','Serr','Sflag',...
         'U','Uweight','Uestim','Uerr','Uflag',...
         'V','Vweight','Vestim','Verr','Vflag'};
    tmp=sum(data_g.prof_Tweight,1)+sum(data_g.prof_Sweight,1)+...
        sum(data_g.prof_Uweight,1)+sum(data_g.prof_Vweight,1);
  end;

  iz=find(tmp~=0);  iz=1:min(iz(end)+1,data_g.nr);	%take 1 down to max of iz plus 1

%leave data_g as full to facilitate adding/subtracing with old iteration outputs
  %for k=1:size(fld,2);eval(['data_g.prof_' fld{k} '=data_g.prof_' fld{k} '(:,iz);']);end;
  %data_g.prof_depth=data_g.prof_depth(iz);
  %data_g.nr=length(iz);
  %%check to make sure prof_depth is a row vector:
  %if(size(data_g.prof_depth,1)>size(data_g.prof_depth,2));data_g.prof_depth=data_g.prof_depth';end;

%======== second filter: zero weights =========
%first, get id that we need to nan out:  (Victor filtered 2 things: lat<-39 (340) and sum(wt,2)=0

if(isfield(data_g,'prof_U')==0);
  fld={'YYYYMMDD','HHMMSS','lon','lat','descr','date','basin','point',...
       'T','Tweight','S','Sweight','Testim','Sestim','Terr','Serr','Tflag','Sflag',...
       'interp_XC11','interp_YC11','interp_XCNINJ','interp_YCNINJ',...
       'interp_i','interp_j','interp_lon','interp_lat','interp_weights'};
  wtot=sum(data_g.prof_Tweight,2)+sum(data_g.prof_Sweight,2);
else;
  fld={'YYYYMMDD','HHMMSS','lon','lat','descr','date','basin','point',...
       'T','Tweight','S','Sweight','Testim','Sestim','Terr','Serr','Tflag','Sflag',...
       'U','Uweight','V','Vweight','Uestim','Vestim','Uerr','Verr','Uflag','Vflag',...
       'interp_XC11','interp_YC11','interp_XCNINJ','interp_YCNINJ',...
       'interp_i','interp_j','interp_lon','interp_lat','interp_weights'};
  wtot=sum(data_g.prof_Tweight,2)+sum(data_g.prof_Sweight,2)+...
       sum(data_g.prof_Uweight,2)+sum(data_g.prof_Vweight,2);
end;

%need to repserve these fields for continuous search of "break"
fld_keep={'date','YYYYMMDD','lon','lat','basin'};

  np_g=data_g.np;
  np_a=data_a.np;

  ii_filter2=find(wtot~=0);     %77565-296 = 77269
  ij_filter2=find(wtot==0);	%296

%keyboard

%pre-filling global with nan , then fill in with new data:
%keep a record of original prof_date, then replace, because we need this intact
  data_k=[];
  for ifld=1:size(fld_keep,2);
    eval(['data_k.prof_' fld_keep{ifld} '=data_g.prof_' fld_keep{ifld} ';']);
  end;

  if(np_a==np_g);	%if both are global, just need to nan out
    data_tmp=data_a;
    for k=1:size(fld,2);
      eval(['tmp=data_a.prof_' fld{k} ';']);
      sz=size(tmp);if(sz(2)==np_a);tmp=tmp';end;aa=whos('tmp');
      sz=size(tmp);if(sz(2)==1);izp=1;elseif(sz(2)==data_g.nr);izp=iz;else;izp=1:sz(2);end;	
      if(strcmp(aa.class,'double'));
        tmp(ij_filter2,izp)=nan;
      else;
        tmp=char(tmp);
        tmp(ij_filter2,:)=nan;
      end;
      eval(['data_tmp.prof_' fld{k} '=tmp;']);
    end;

  else;
    data_tmp=data_g;
    for k=1:size(fld,2);
      %fprintf('%i ',k);fprintf('prof_%s ',fld{k});
      eval(['tmp=data_g.prof_' fld{k} ';']);
      sz=size(tmp);if(sz(2)==np_g);tmp=tmp';end;aa=whos('tmp');
      if(strcmp(aa.class,'double'));
        tmp=nan.*tmp;
      else;
        tmp=char(tmp);sz=size(tmp);
        eval(['tmp1=data_a.prof_' fld{k} ';']);tmp1=char(tmp1);
        sz1=size(tmp1);if(sz1(2)==np_a);tmp1=tmp1';end;
%swap out descr to global if the trimmed version is truncated:
        if(sz(2)>sz1(2));
          clear tmp1;tmp1=tmp(ii_filter2,:);
        end;
        eval(['data_a.prof_' fld{k} '=tmp1;']);
      end;
      sz=size(tmp);if(sz(2)==1);izp=1;elseif(sz(2)==data_g.nr);izp=iz;else;izp=1:sz(2);end;	
      eval(['data_tmp.prof_' fld{k} '=tmp;']);
      eval(['data_tmp.prof_' fld{k} '(ii_filter2,izp)=data_a.prof_' fld{k} ';']);
    end;

  end;

  for ifld=1:size(fld_keep,2);
    eval(['data_tmp.prof_' fld_keep{ifld} '=data_k.prof_' fld_keep{ifld} ';']);
  end;

  %check to make sure prof_depth is a row vector:
  if(size(data_g.prof_depth,1)>size(data_g.prof_depth,2));data_g.prof_depth=data_g.prof_depth';end;
  data_tmp.prof_depth=data_g.prof_depth;

  data_tmp.nd=data_g.nd;
  data_tmp.np=data_g.np;
  data_tmp.nr=data_g.nr;


  data_new=data_tmp;

return

