function [fldOut,fldOut0]=get_obcsNSEW(fldIn0,fldIn,mygrid0,mygrid,flag_global,flag_case)

%idea: for any obcs, we need: indices [ix,jy]
% input: ix (global)
%        jy (global)
%        imask, relative, tells which one along ix and jy to blank out
%        face
%        nfx0,nfy0
%        nfx ,nfy
%        mygrid0
%        mygrid
%        flag_global: report if mygrid0 and mygrid are global (1) or regional (0) grids
%        flag_case: 1 for special (Gibraltar) or 0 for generic
%want:
% fldOut.obcs=[N,S,E,W]
%         .iC1 [2 x L] : global and local
%         .iC2 [2 x L] : global and local
%         .ivel [2 x L] : global and local (all nan if this is not the right obcs)
%         .jC1 [2 x L] : global and local
%         .jC2 [2 x L] : global and local
%         .jvel [2 x L] : global and local
%         .imask [1 x L] , relative, tells which ones along iC[1,2], ivel, ,jC[1,2],jvel to blank out
%
%         .dxg [1 x L]
%         .dyg [1 x L]
%         .scalex
%         .scaley

fldOut=[];
fldOut0=[];

for iloop=1:2;
  if(iloop==1);str='';else;str='0';end;
  eval(['fname=fieldnames(fldIn' str ');']);
  for k=1:size(fname,1);
    eval(['fldOut' str '.' char(fname(k)) '=fldIn' str '.' char(fname(k)) ';']);
  end;
end;

%define fullsize global:
nx0=fldIn0.nx;
nfx0_full=[nx0 nx0 nx0 3*nx0 3*nx0];
nfy0_full=[3*nx0 3*nx0 nx0 nx0 nx0];
nfx0=fldIn0.nfx;
nfy0=fldIn0.nfy;

nx =fldIn.nx;
nfx_full=[nx nx nx 3*nx 3*nx];
nfy_full=[3*nx 3*nx nx nx nx];
nfx=fldIn.nfx;
nfy=fldIn.nfy;

fac=nx/nx0;

%ix=fldIn.ix;			%MUST be global
%jy=fldIn.jy;			%MUST be global
face=fldIn.face;
obcsstr=fldIn.obcsstr;
sshiftx=fldIn.sshiftx;
sshifty=fldIn.sshifty;
sshiftx0=fldIn0.sshiftx;
sshifty0=fldIn0.sshifty;

%shift
if(flag_global==0);
  sshiftx_l  = -sshiftx;	%to get ix_local, subtract sshiftx_l from global ix_g
  sshifty_l  = -sshifty; 	%to get jy_local, subtract sshifty_l from global jy_g
  sshiftx_g  = 0;
  sshifty_g  = 0;
  sshiftx0_l = -sshiftx0;	%to get ix_local, subtract sshiftx_l from global ix_g
  sshifty0_l = -sshifty0;	%to get jy_local, subtract sshifty_l from global jy_g
  sshiftx0_g = 0;
  sshifty0_g = 0;
else;
  sshiftx_g  = sshiftx;		%to get ix_global, add sshiftx to ix_local
  sshifty_g  = sshifty;		%to get jy_global, add sshifty to jy_local
  sshiftx_l  = 0;
  sshifty_l  = 0;
  sshiftx0_g = sshiftx0;	%to get ix_global, add sshiftx to ix_local
  sshifty0_g = sshifty0;	%to get jy_global, add sshifty to jy_local
  sshiftx0_l = 0;
  sshifty0_l = 0;
end;

%indices
%fldOut0.obcs=obcsstr;
fldOut0.iC1(1,:)=fldIn0.ix;           %global, ALWAYS these ix are provided ONLY in global
fldOut0.iC1(2,:)=fldIn0.ix+sshiftx0_l; %local
fldOut0.jC1(1,:)=fldIn0.jy;           %global
fldOut0.jC1(2,:)=fldIn0.jy+sshifty0_l; %local
fldOut0.imask   =fldIn0.imask;		%keep

%fldOut.obcs=obcsstr;
fldOut.iC1(1,:)=fldIn.ix;		%global, ALWAYS these ix are provided ONLY in global
fldOut.iC1(2,:)=fldIn.ix+sshiftx_l;	%local
fldOut.jC1(1,:)=fldIn.jy;		%global
fldOut.jC1(2,:)=fldIn.jy+sshifty_l;	%local
fldOut.imask   =fldIn.imask;		%keep

for iloop=1:2;
  if(iloop==1);str='';else;str='0';end;
  if(strmatch(obcsstr,'E')==1);
    eval(['fldOut' str '.iC2 =fldOut' str '.iC1-1;']);
    eval(['fldOut' str '.ivel=fldOut' str '.iC1;']);
    eval(['fldOut' str '.jC2 =zeros(size(fldOut' str '.jC1));']);
    eval(['fldOut' str '.jvel=zeros(size(fldOut' str '.jC1));']);
  elseif(strmatch(obcsstr,'W')==1);
    eval(['fldOut' str '.iC2 =fldOut' str '.iC1+1;']);
    eval(['fldOut' str '.ivel=fldOut' str '.iC2;']);
    eval(['fldOut' str '.jC2 =zeros(size(fldOut' str '.jC1));']);
    eval(['fldOut' str '.jvel=zeros(size(fldOut' str '.jC1));']);
  elseif(strmatch(obcsstr,'N')==1);
    eval(['fldOut' str '.iC2 =zeros(size(fldOut' str '.iC1));']);
    eval(['fldOut' str '.ivel=zeros(size(fldOut' str '.iC1));']);
    eval(['fldOut' str '.jC2 =fldOut' str '.jC1-1;']);
    eval(['fldOut' str '.jvel=fldOut' str '.jC1;']);
  elseif(strmatch(obcsstr,'S')==1);
    eval(['fldOut' str '.iC2 =zeros(size(fldOut' str '.iC1));']);
    eval(['fldOut' str '.ivel=zeros(size(fldOut' str '.iC1));']);
    eval(['fldOut' str '.jC2 =fldOut' str '.jC1+1;']);
    eval(['fldOut' str '.jvel=fldOut' str '.jC2;']);
  end;
end;

%keyboard
%d[x,y]g, [x,y]c, [x,y]g
if(flag_global==1);ind=1;elseif(flag_global==0);ind=2;end;
%indg=ind;
%%atn 7.aug.2018, getting this to work with global llc90, the only exception
%if(nx0==90);indg=1;end;
%%done 7.aug.2018
if(strcmp(obcsstr,'N')==1|strcmp(obcsstr,'S')==1);
  dx0=mygrid0.dxg{face}(fldOut0.iC1(ind,:),fldOut0.jvel(ind,1))';	%row vector
  dx =mygrid.dxg{face}(fldOut.iC1(ind,:) ,fldOut.jvel(ind,1))';
  dy0=zeros(1,length(dx0));
  dy =zeros(1,length(dx));
  fldOut0.xg = mygrid0.xg{face}(fldOut0.iC1(ind,:) , fldOut0.jvel(ind,1))';
  fldOut0.yg = mygrid0.yg{face}(fldOut0.iC1(ind,:) , fldOut0.jvel(ind,1))';
  fldOut.xg  = mygrid.xg{face}(fldOut.iC1(ind,:) , fldOut.jvel(ind,1))';
  fldOut.yg  = mygrid.yg{face}(fldOut.iC1(ind,:) , fldOut.jvel(ind,1))';
elseif(strcmp(obcsstr,'E')==1|strcmp(obcsstr,'W')==1);
  dy0=mygrid0.dyg{face}(fldOut0.ivel(ind,1),fldOut0.jC1(ind,:));	%row vector
  dy =mygrid.dyg{face}(fldOut.ivel(ind,1) ,fldOut.jC1(ind,:));
  dx0=zeros(1,length(dy0));
  dx =zeros(1,length(dy));
  fldOut0.xg = mygrid0.xg{face}(fldOut0.ivel(ind,1) , fldOut0.jC1(ind,:));
  fldOut0.yg = mygrid0.yg{face}(fldOut0.ivel(ind,1) , fldOut0.jC1(ind,:));
  fldOut.xg  = mygrid.xg{face}(fldOut.ivel(ind,1) , fldOut.jC1(ind,:));
  fldOut.yg  = mygrid.yg{face}(fldOut.ivel(ind,1) , fldOut.jC1(ind,:));
end;
fldOut0.xc = mygrid0.xc{face}(fldOut0.iC1(ind,1),fldOut0.jC1(ind,:));
fldOut0.yc = mygrid0.yc{face}(fldOut0.iC1(ind,1),fldOut0.jC1(ind,:));
fldOut.xc  = mygrid.xc{face}(fldOut.iC1(ind,1),fldOut.jC1(ind,:));
fldOut.yc  = mygrid.yc{face}(fldOut.iC1(ind,1),fldOut.jC1(ind,:));

%now get scale
%atn 8.aug.2018: 
% there seems to be a bug here: scale is taken as ratio of dx1./dx0 for non Gibraltar Strait,
% but dy0./dy1 for Gibraltar Strait!!! and the way the code is done in step2a, 
% scale are mult with [U,V], not [U0,V0] !! MUST reverse one of these, likely the generic case.
% MUST check the rest of all the obcs extractions to track down this bug in other obcs.
if(flag_case==0);	%generic
  if(strcmp(obcsstr,'N')==1|strcmp(obcsstr,'S')==1);
    Lsc=length(fldIn0.ix);
    dx1=reshape(dx,fac,Lsc);
    dx1=sum(dx1,1);
    %scx= dx1./dx0;
    scx= dx0./dx1;
    dy1=zeros(size(dx1));scy= zeros(size(scx));
  elseif(strcmp(obcsstr,'E')==1|strcmp(obcsstr,'W')==1);
    Lsc=length(fldIn0.jy);
    dy1=reshape(dy,fac,Lsc);
    dy1=sum(dy1,1);
    %scy= dy1./dy0;
    scy= dy0./dy1;
    dx1=zeros(size(dy1));scx= zeros(size(scy));
  end;

  scx=repmat(scx,[fac 1]);scx=reshape(scx,fac*Lsc,1)';	%row
  scy=repmat(scy,[fac 1]);scy=reshape(scy,fac*Lsc,1)';	%row

else;	%Gibraltar Strait

%07May2017: need to take care with imask:
  dy0=dy0.*fldIn0.imask;
  dx0=dx0.*fldIn0.imask;
  dy=dy.*fldIn.imask;
  dx=dx.*fldIn.imask;

  dy1=sum(dy).*ones(size(dy));
  dx1=sum(dx).*ones(size(dx));
  if(strcmp(obcsstr,'N')==1|strcmp(obcsstr,'S')==1);
    scx=(sum(dx0).*ones(size(dx)))./dx1;		%a single number over entire width of ob
    scy=zeros(size(scx));
  elseif(strcmp(obcsstr,'E')==1|strcmp(obcsstr,'W')==1);
    scy=(sum(dy0).*ones(size(dy)))./dy1;		%a single number over entire width of ob
    scx=zeros(size(scy));
  end;
end;

figure(3);clf;
  subplot(221);plot(dy0,'ks-');grid;hold on;plot(dy1,'r.-');hold off;title('dy');legend('coarse','fine');
  subplot(222);plot(scy,'ks-');grid;title(num2str(mean(scy)));ll=legend([fldOut0.name ' dy1/dy0']);set(ll,'interpreter','none');
  subplot(223);plot(dx0,'ks-');grid;hold on;plot(dx1,'r.-');hold off;title('dx');legend('coarse','fine');
  subplot(224);plot(scx,'ks-');grid;title(num2str(mean(scx)));ll=legend([fldOut0.name ' dx1/dx0']);set(ll,'interpreter','none');

%%7.aug.2018
%if(fldIn0.face<3);
%  if(sum(fldOut.iC1(1,:)-fldOut.iC1(2,:))==0 & sum(fldOut.iC1(1,:))==0);
%  end;
%end;
%---

fldOut.scaleX=scx;		%to be applied to uvel at fldOut.ivel
fldOut.scaleY=scy;		%to be applied to vvel at fldOut.jvel
fldOut.dxg   =dx;
fldOut.dyg   =dy;

fldOut0.dxg  =dx0;
fldOut0.dyg  =dy0;

return
