function [FLD,varargout]=eccov4_lonlat(fldLoc,fldName,numRec,varargin);
%
%  Extract the lat-lon part of ecco v4 fields.
%     FLD=eccov4_lonlat(fldLoc,fldName,numRec) reads record
%     numRec (set to [] for all records) of fldName in fldLoc
%     ---> assumption : fldName designates a variable located at grid cell  
%     centers (e.g. RAC)
%
%     [FLD,MATE]=eccov4_lonlat(fldLoc,fldName,numRec,mateLoc,mateName,1)
%     reads record numRec of fldName,mateName in fldLoc,mateLoc
%     ---> assumption : fldName,mateName designate un-signed staggered
%          variables at grid cell edges (e.g. DXC,DYC)
%
%     [FLD,MATE]=eccov4_lonlat(fldLoc,fldName,numRec,mateLoc,mateName,-1) reads
%     reads record numRec of fldName,mateName in fldLoc,mateLoc and flip signs
%     ---> assumption : fldName,mateName designate staggered vector
%          variables at grid cell edges (e.g. UVELMASS,VVELMASS)
%
%     Examples:
%
%        fldLoc='nctiles_grid/GRID';
%        XC=eccov4_lonlat(fldLoc,'XC',1);
%        YC=eccov4_lonlat(fldLoc,'YC',1);
%
%        fldLoc='nctiles_grid/GRID'; fldName='DXC';
%        mateLoc='nctiles_grid/GRID'; mateName='DYC';
%        [DXC,DYC]=eccov4_lonlat(fldLoc,fldName,1,mateLoc,mateName,1);
%
%        fldLoc='nctiles_climatology/UVELMASS/UVELMASS';
%        mateLoc='nctiles_climatology/VVELMASS/VVELMASS';
%        [advx,advy]=eccov4_lonlat(fldLoc,'UVELMASS',1,mateLoc,'VVELMASS',-1);
%
%  (Version 3; 2017/01/27)

%optional input parameters
if nargin>3;
    mateLoc=varargin{1};
    mateName=varargin{2};
    changeSign=varargin{3};
end;

%determine directory and file names
[nameDir,nameFil]=eccov4_search(fldLoc,fldName);

%set dimensions
fil=fullfile(nameDir,filesep,nameFil(1).name);
tmp=ncread(fil,fldName);
siz=size(tmp);
strt=ones(size(siz)); cnt=siz;
if ~isempty(numRec)&(length(siz)>2);
    strt(end)=numRec; cnt(end)=1;
end;
    
%read from file
fld={};
for T=1:length(nameFil) %loop through tiles
    fil=fullfile(nameDir,filesep,nameFil(T).name);
    fld{T}=ncread(fil,fldName,strt,cnt);
end %for T loop through tiles

%re-arrange into faces
fld=eccov4_faces(fld);

if nargin>3;
    %determine directory and file names
    [nameDir,nameFil]=eccov4_search(mateLoc,mateName);
    
    %read from file
    mate={};
    for T=1:length(nameFil) %loop through tiles
        fil=fullfile(nameDir,filesep,nameFil(T).name);
        mate{T}=ncread(fil,mateName,strt,cnt);
    end %for T loop through tiles
    
    %re-arrange into faces
    mate=eccov4_faces(mate);
end;

if nargin<4;
    %extract longitude-latitude sector
    FLD=eccov4_array(fld);
    if strcmp(fldName,'YG')|strcmp(fldName,'RAZ'); FLD(181:360,1:270)=FLD(1:180,1:270); end;
    FLD=FLD(:,62:239,:,:);
    FLD=circshift(FLD,[142 0]);
    if strcmp(fldName,'XG'); FLD(1,:)=FLD(1,:)-360; end;
else;
    %extract first component:
    FLD=eccov4_array(fld);
    FLD=FLD(:,62:239,:,:);
    FLD(1:90,:,:,:)=fld{1}(:,62:239,:,:);
    FLD(91:180,:,:,:)=fld{2}(:,62:239,:,:);
    tmp1=flipdim(permute(mate{4},[2 1 3 4]),2);
    FLD(181:270,:,:,:)=tmp1(:,62:239,:,:);
    tmp1=flipdim(permute(mate{5},[2 1 3 4]),2);
    FLD(271:360,:,:,:)=tmp1(:,62:239,:,:);
    FLD=circshift(FLD,[142 0]);
    %extract second component: note index (and sign) shift
    MATE=eccov4_array(mate);
    MATE=MATE(:,62:239,:,:);
    MATE(1:90,:,:,:)=mate{1}(:,62:239,:,:);
    MATE(91:180,:,:,:)=mate{2}(:,62:239,:,:);
    tmp1=flipdim(permute(fld{4},[2 1 3 4]),2);
    MATE(181:270,:,:,:)=changeSign*tmp1(:,61:238,:,:);
    tmp1=flipdim(permute(fld{5},[2 1 3 4]),2);
    MATE(271:360,:,:,:)=changeSign*tmp1(:,61:238,:,:);
    MATE=circshift(MATE,[142 0]);
    %
    varargout{1}=MATE;
end;

function [nameDir,nameFil]=eccov4_search(fldLoc,fldName);

%determine directory and file names
nameFil=[];
if isempty(nameFil);
    nameFil=dir(fullfile([fldLoc '*.nc']));
    nameDir = fileparts(fldLoc);
end;
if isempty(nameFil);
    nameFil=dir(fullfile(fldLoc,filesep,[fldName '*.nc']));
    nameDir = fldLoc;
end;
if isempty(nameFil);
    nameFil=dir(fullfile(fldLoc,filesep,fldName,filesep,[fldName '*.nc']));
    nameDir = fullfile(fldLoc,filesep,fldName,filesep);
end;
if mod(length(nameFil),13)~=0|length(nameFil)==0; error('did not find files matching description'); end;

function [fldOut]=eccov4_faces(fldIn);

nn=sqrt(length(fldIn)/13);
fldOut{1}=[]; for ii=1:nn; mm=(ii-1)*3*nn;          fldOut{1}=cat(1,fldOut{1},cat(2,fldIn{mm+[1:3*nn]})); end;
fldOut{2}=[]; for ii=1:nn; mm=(ii-1)*3*nn+3*nn*nn;  fldOut{2}=cat(1,fldOut{2},cat(2,fldIn{mm+[1:3*nn]})); end;
fldOut{3}=[]; for ii=1:nn; mm=(ii-1)*nn+6*nn*nn;    fldOut{3}=cat(1,fldOut{3},cat(2,fldIn{mm+[1:nn]})); end;
fldOut{4}=[]; for ii=1:3*nn; mm=(ii-1)*nn+7*nn*nn;  fldOut{4}=cat(1,fldOut{4},cat(2,fldIn{mm+[1:nn]})); end;
fldOut{5}=[]; for ii=1:3*nn; mm=(ii-1)*nn+10*nn*nn; fldOut{5}=cat(1,fldOut{5},cat(2,fldIn{mm+[1:nn]})); end;

function [FLD]=eccov4_array(fld);

FLD=cat(1,fld{1},fld{2},...
    flipdim(permute(fld{4},[2 1 3 4]),2),...
    flipdim(permute(fld{5},[2 1 3 4]),2));
tmp2=flipdim(permute(fld{3},[2 1 3 4]),1);
FLD=cat(2,FLD,repmat(tmp2,[4 1]));
FLD(91:360,271:360)=NaN;

