function get_profile(name,F,rect,diry);
% function get_profile(name,F,rect,diry);
%
% This function is used to loop through ROMS netcdf files and pull out the
% needed variables. It currently needs to be hardcoded in to
% change the river/bed sediment variables to match the model formulation.
% The file pulls out a one dimension horizontal by vertical 2D slice for making
% figures.
%
%   name = string to search for files (i.e. '*his*')
%   F = flag for using only fluvial (1) or bed (2) sediment. Currently not
%   set up. just insert something as a place holder.
%   diry = directory to search in
%   rect = the 2D (vertical/horizontal) rectangle of points to get. rect =
%   [sb nb wb eb], where 
%   nb = north boundary, etc. At this point nb/sb are the first dimension,
%   and eb/wb are the second dimension (in matlab). Either sb/nb or wb/eb 
%   must be the same value.
% IMPORTANT - rect needs to be indexed based on starting at zero!!!!!!!!!
%
%
%           Aaron Bever May 2009
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin == 4
    Fname=dir([diry,name]);
else
    diry = pwd;
    diry = [diry,'/'];
    Fname=dir([diry,name]);
end

if(~exist('rect','var'))
    beep
    disp('Variable "rect" is needed')
    beep
    return
end

sb = rect(1); nb = rect(2); wb = rect(3); eb = rect(4);

prof_time = get_variable('ocean_time',name,diry);

if(nb==sb)
    pm = 1./nc_varget(Fname(1).name,'pm',[sb wb],[1 eb-wb+1]);
    D = cumsum(pm);
    H = -nc_varget(Fname(1).name,'h',[sb wb],[1 eb-wb+1]);
elseif(wb==eb)
    pn = 1./nc_varget(Fname(1).name,'pn',[sb wb],[nb-sb+1 1]);
    D = cumsum(pn);
    H = -nc_varget(Fname(1).name,'h',[sb wb],[nb-sb+1 1]);
else
    beep
    disp('Bad "rect" variable, either nb=sb or eb=wb')
    beep
    return
end


U = [];
V = [];
W = [];
SED = [];
Z = [];
for jj=1:length(Fname)
    disp(Fname(jj).name);

    
    S = get_varsize(Fname(jj).name,'u');
    if(nb==sb);
        u = nc_varget(Fname(jj).name,'u',[0 0 sb wb],[S(1) S(2) 1 eb-wb+1]);
        v = nc_varget(Fname(jj).name,'v',[0 0 sb wb],[S(1) S(2) 1 eb-wb+1]);
        w = nc_varget(Fname(jj).name,'w',[0 0 sb wb],[S(1) S(2) 1 eb-wb+1]);
        s = nc_varget(Fname(jj).name,'mud_04',[0 0 sb wb],[S(1) S(2) 1 eb-wb+1]) + ...
            nc_varget(Fname(jj).name,'mud_05',[0 0 sb wb],[S(1) S(2) 1 eb-wb+1]);
    else(wb==eb);
        u = nc_varget(Fname(jj).name,'u',[0 0 sb wb],[S(1) S(2) nb-sb+1 1]);
        v = nc_varget(Fname(jj).name,'v',[0 0 sb wb],[S(1) S(2) nb-sb+1 1]);
        w = nc_varget(Fname(jj).name,'w',[0 0 sb wb],[S(1) S(2) nb-sb+1 1]);
        s = nc_varget(Fname(jj).name,'mud_04',[0 0 sb wb],[S(1) S(2) nb-sb+1 1]) + ...
            nc_varget(Fname(jj).name,'mud_05',[0 0 sb wb],[S(1) S(2) nb-sb+1 1]);
    end

    U = [U;u];
    V = [V;v];
    W = [W;w];
    SED = [SED;s];
    
    for i=1:S(1);
        z=depths2(Fname(jj).name,1,0,i);
        z=permute(z,[3,2,1]);
        z2(i,:,:)=z(:,sb:nb,wb:eb);
    end
    Z = [Z;z2]; clear z2;
    
end

save profile.mat U V SED prof_time D H Z rect
