function [Fout,bathy,bathy_mask,X,Y] = merge_N_S_CSIDE(Sdat,Ndat,Field,pass,makebathy);
%
% USAGE: [Fout,bathy,bathy_mask,X,Y] = merge_N_S_CSIDE(Sdat,Ndat,Field,pass,makebathy);
%
% Sdat and Ndat are matfile structures and Field is the field name 
% to be merged over entire domain (eg. Field = 'TEMP', or 'DYE').
% pass is the index number w/in Field to grab. make bathy is logical
% (1) grids bathy and (0) skips it.


%% horizontal coords
SX = Sdat.X;
SY = Sdat.Y;
NX = Ndat.X;
NY = Ndat.Y;

%% create coords for whole domain
Xmin = min([SX(:); NX(:)]); Xmax = max([SX(:); NX(:)]);
Ymin = min([SY(:); NY(:)]); Ymax = max([SY(:); NY(:)]);

dx = mode(diff(SX)); dy = mode(diff(SY));% assuming they N/S have same dx dy

%% hopefully there is a union of SX NX and X, etc.
X = Xmin:dx:Xmax;  nx = length(X);
Y =[Ymin:dy:Ymax]';ny = length(Y);

%% create domains
Fout = nan(ny,nx);
bathy = nan(ny,nx);
bathy_mask = nan(ny,nx);

%% find unions
[iSX,iX] = ismember(SX,X);iSX = find(iSX==1);
[iSY,iY] = ismember(SY,Y);iSY = find(iSY==1);

if isempty(iSX) | isempty(iSY)
disp(' domains do not over lap! ')
Fout = [];
bathy = [];
bathy_mask = [];
X = [];
Y = [];
return
end

Fout(iY,iX) = eval(sprintf('Sdat.%s(iSY,iSX,pass)',Field));

% make bathymetry by default
if exist('makebathy','var')
    if makebathy==1
        % if there is not a bathy variable make from scratch
        if exist('Sdat.bathymetry','var')
            bathy(iY,iX) = Sdat.bathymetry(iSY,iSX);
            bathy_mask(iY,iX) = Sdat.bathymetry_mask(iSY,iSX);
        else
            [bathy_mask,bathy] = make_bathymetry_mask_CSIDE([],dx,dy,[Xmin Xmax Ymin Ymax]);
        end
    else
        bathy = [];
        bathy_mask = [];
    end
else 
        bathy(iY,iX) = Sdat.bathymetry(iSY,iSX);
        bathy_mask(iY,iX) = Sdat.bathymetry_mask(iSY,iSX);
end

[iNX,iX] = ismember(NX,X);iNX = find(iNX==1);
[iNY,iY] = ismember(NY,Y);iNY = find(iNY==1);
Fout(iY,iX) = eval(sprintf('Ndat.%s(iNY,iNX,pass)',Field));
if exist('makebathy','var')
    if makebathy==1
        if exist('Sdat.bathymetry','var')
        bathy(iY,iX) = Ndat.bathymetry(iNY,iNX);
        bathy_mask(iY,iX) = Ndat.bathymetry_mask(iNY,iNX);
        end
    else
        bathy = [];
        bathy_mask = [];
    end
else 
    bathy(iY,iX) = Ndat.bathymetry(iNY,iNX);
    bathy_mask(iY,iX) = Ndat.bathymetry_mask(iNY,iNX);
end
