function ind=get_Arctic_index(lon,lat,hf,bathy)
%-----------------------------------------------------
%function ind=get_Arctic_index(lon,lat,hf,bathy)
%
% function to split [lon,lat] pairs into various Arctic
%  and high lat seas.  If input landmask hf, ind will return
%  the wet points w/in these basins.  
%
% input: lon,lat pair
%        hf: landmask, or ones(size(lon))
%        bathy: bathymetry, zeros(size(lon))
% output: structure ind
%         ind.iB (Barents), ind.iE (Eurasian), ind.iA (Amerasian),
%         ind.iCB (CanadaBasin), ind.iM (MakarovBasin), ind.iC (Chukchi)
%         ind.iN (NaresStrait), ind.iG (GreenlandSea), ind.iCA (CAA),
%         ind.iL (LabradorSea), ind.iBE (BeringSea)
%
% Usage after this:
%  index=zeros(size(lon));index(ind.iB)=1;index(ind.iE)=2; etc.
%
%----
% Note1: to make compatible with basin definition from eccov4:
% /ecco-version-4/input/input_all_from_pleiades/basin_masks_eccollc_90x50.bin (raid11)
% 4=Amerasian+Eurasian+BarentsSea+LabradorSea+NordicSea+CAA (all above 57N excluding BeringSea)
% 5=Bering Sea
% My new definition of basins to be used for llc270: --USE THIS---
% 'Pac=1','Atl=2','Ind=3','CAA=4','BeS=5','SCh=6','Crb=7','Okh=8','Hud=9','Med=10','Phi=11','NSe=12',
% 'ECh=13','Mar=14','SJp=15','Red=16','Per=17','AmB=18','EuB=19','BaS=20','GNS=21','Lab=22'
%----
% Note2: to make compatible with integers assigned to Arctic profiles,
%       here's a guide (copy from get_CTDindex.m)
% [1:NB, 2:ABmb, 3:MBsi, 4:MBgr, 5:CBmb, 6:CBckr, 7:CBckl, 8:CBak,
% [9]=SiS (Siberian Sea), [10]=CKak (ChukchiSea_alaska), [11]=CKru (ChukchiSea_russia)
% [12]=CKpl (ChukchiPlateau), [15]=Be (Bering Sea), [17]: CAA, [19]=jBS-Barents Sea, [20]=jKS-Kara Sea
% %for Fram Strait and Greenland Sea: add os=20 to definitions below:
% [1=ja0 (PolarWater), 2=ja(EGC_N), 3=jbc(AWrN), 4=jd1(AWrS), 5=jd2(WSCos), 6=je(WSC_S), 7=jf(SpitsbergenCoast),
%  8=jg(YermakPlateau), 9="j2,GB", 10="j3", 11="j4,WJMFZ/IcelandSea", 12="j5,EGC_S2", 13="j6,AWi_Ir_DMStrait_N", 
% 14="j7,EGC+IrC_DenmarkStrait_S", 15="j8", 16="j9"];
%---
%-----------------------------------------------------
% 12.08.09
% Make sure 0 < lon < 360
ii=find(lon<0);lon(ii)=lon(ii)+360;clear ii;

% Defining "A" and "E"
i1 = find(lat>79 & (lon>310 | lon<18));
i1q= find(lat>79 & (lon>240 & lon<=310));
i1p= find(lat>81 & (lon>=18 & lon< 100));
i2 = find(lat>66 &  lon>100 & lon<240);
i3 = find(lat>=79 & lat<82 & lon>=280 & lon<300);
ii = union(union(union(i1,i1p),i1q),i2);ii=setxor(ii,i3);
clear i1 i1p i2;lon1=lon(ii);lat1=lat(ii);
ind.iE = find(lon1>300 | lon1<142);ind.iA=setxor(1:length(lon1),ind.iE);

% Grid points outside [A,E]:
ij=setxor(1:length(lon(:)),ii);

%iG=find((lon(ij)>=336 | lon(ij)<18) & (lat(ij)>=66 & lat(ij)<79));
% 13-dec-2012: extend lon/lat range to cover all sea ice in greenland sea (just missing a bit w/ def above)
ind.iG=find((lon(ij)>=315 | lon(ij)<18) & (lat(ij)>=62 & lat(ij)<79));
ind.iB=find((lon(ij)>=18  & lon(ij)<100) & (lat(ij)>=66 & lat(ij)<82));

ind.iN=find(lon(ij)>=280 & lon(ij)<300 & lat(ij)>=79 & lat(ij)<82); %Nares Strait
ind.iL=find(lon(ij)>=295 & lon(ij)<316 & lat(ij)<68 & lat(ij)>=52);
iC1=find(lon(ij)>=240 & lon(ij)<300 & lat(ij)>=51 & lat(ij)<79);
iC2=find(lon(ij)>=300 & lon(ij)<316 & lat(ij)>=52 & lat(ij)<79);
ind.iCA=union(iC1,iC2);ind.iCA=setxor(ind.iCA,ind.iL);iCA=union(ind.iCA,ind.iN);

% Find locations where landmask <-> water
m1=find(hf(:)==1);

% Put domain indices in global grid:
ind.iE=intersect(ii(ind.iE),m1);ind.iA=intersect(ii(ind.iA),m1);ind.iG=intersect(ij(ind.iG),m1);
ind.iL=intersect(ij(ind.iL),m1);ind.iCA=intersect(ij(ind.iCA),m1);ind.iN=intersect(ij(ind.iN),m1);
ind.iB=intersect(ij(ind.iB),m1);

% Define "M" and "BC" within domain "A"
iM1=find(lon(ind.iA)>=142 & lon(ind.iA)<180);
iM2=find(lon(ind.iA)>=180 & lon(ind.iA)<300 & lat(ind.iA)>=85);
ind.iM=union(iM1,iM2);ind.iM=unique(ind.iM);ind.iM=ind.iA(ind.iM);
ind.iBC=setxor(ind.iA,ind.iM);

% added 12.29.09 to separate Chukchi from Beaufort+Chukchi Basin
ind.iC=find(lon(ind.iBC)>=180 & lon(ind.iBC)<=204 & lat(ind.iBC)>=66 & lat(ind.iBC)<79);ind.iC=ind.iBC(ind.iC);
ind.iBC=setxor(ind.iBC,ind.iC);

% added Bering Seas 12-dec-2012 to complete Arctic Ocean
ind.iBE=find(hf(:)==1 & lon(:)<=203 & lon(:)>=160 & lat(:)<66 & lat(:)>=53);

% added proxi-split between Amundsen and Nansen within "iE", 10.feb.2014
%lon: [127 to 142],[-60 to -5]
iAB1=find((lon(ind.iE)>127&lon(ind.iE)<=142)|(lon(ind.iE)<=355&lon(ind.iE)>=300));
iAB2=find(abs(bathy(ind.iE))>4000);
%iAB3=find(lat(ind.iE)<88);
iAB=unique([iAB1(:);iAB2(:)]);
%iAB=unique([iAB1(:);iAB2(:);iAB3(:)]);
%iAB=intersect(iAB(:),iAB3(:));
ind.iAB=ind.iE(iAB);
ind.iNB=setxor(ind.iE,ind.iAB);

fprintf('My new definition of basins to be used for llc270: --USE THIS--- \n');
fprintf('Pac=1,Atl=2,Ind=3,CAA=4,BeS=5,SCh=6,Crb=7,Okh=8,Hud=9,Med=10,Phi=11,NSe=12, \n');
fprintf('ECh=13,Mar=14,SJp=15,Red=16,Per=17,AmB=18,EuB=19,BaS=20,GNS=21,Lab=22 \n');

return
