function get_all_variables(name,diry,swf)
% function get_all_variables(name,diry,swf);
% this routine automates the getting of variables out of the history files
% that I used to do by reapeted typing...
%
%   This script is set up to use matlab in parallel. It checks to see if a
%   matlabpool has been opened. If not it starts one then closes it,
%   otherwise it leaves it open.
%
%               Aaron Bever, 30 September 2009
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% if(~matlabpool('size')>0);
%     mpclose = 1;
%     matlabpool open
% end
% 
if nargin==1
    diry = './';
end

if(nargin<3)
    swf=0;
end

if(~swf)
    if(~exist([diry,'bed_massR.mat'],'file'));
        disp('Getting bedmass');
        bed_mass(name,1,diry);
    end
end
if(~exist([diry,'velocity_tavg.mat'],'file'));
disp('Getting velocities using parallel matlab');
% This might be faster to create distributed arrays before hand.
% instead of all the (:)'s
fname = dir([diry,name]);
fname = fname(1).name;
llm_all
% ulon = distributed(ulon);
% ulat = distributed(ulat);
% umask = distributed(umask)
% vlon = distributed(vlon);
% vlat = distributed(vlat);
% vmask = distributed(vmask)
% rlon = distributed(rlon);
% rlat = distributed(rlat);
% mask = distributed(mask);



us = get_variable('u',name,diry,20);
usf = squeeze(mean(us(921:1009,:,:)));
us = squeeze(mean(us));

vs = get_variable('v',name,diry,20);
vsf = squeeze(mean(vs(921:1009,:,:)));
vs = squeeze(mean(vs));

ub = get_variable('u',name,diry,1);
ubf = squeeze(mean(ub(921:1009,:,:)));
ub = squeeze(mean(ub));

vb = get_variable('v',name,diry,1);
vbf = squeeze(mean(vb(921:1009,:,:)));
vb = squeeze(mean(vb));

ubar = get_variable('ubar',name,diry);
ubarf = squeeze(mean(ubar(921:1009,:,:)));
ubar = squeeze(mean(ubar));

vbar = get_variable('vbar',name,diry);
vbarf = squeeze(mean(vbar(921:1009,:,:)));
vbar = squeeze(mean(vbar));

%     usf = usf{:};
%     us = us{:};
%     ubf = ubf{:};
%     ub = ub{:};
%     ubar = ubar{:};
%     ubarf = ubarf{:};
%     vsf = vsf{:};
%     vs = vs{:};
%     vbf = vbf{:};
%     vb = vb{:};
%     vbar = vbar{:};
%     vbarf = vbarf{:};

disp('Regridding velocities')
p = find(umask(:,:)==1);
usf = griddata(ulon(p),ulat(p),usf(p),rlon(:,:),rlat(:,:));
us = griddata(ulon(p),ulat(p),us(p),rlon(:,:),rlat(:,:));
ubf = griddata(ulon(p),ulat(p),ubf(p),rlon(:,:),rlat(:,:));
ub = griddata(ulon(p),ulat(p),ub(p),rlon(:,:),rlat(:,:));
ubar = griddata(ulon(p),ulat(p),ubar(p),rlon(:,:),rlat(:,:));
ubarf = griddata(ulon(p),ulat(p),ubarf(p),rlon(:,:),rlat(:,:));
    
p = find(vmask(:,:)==1);
vsf = griddata(vlon(p),vlat(p),vsf(p),rlon(:,:),rlat(:,:));
vs = griddata(vlon(p),vlat(p),vs(p),rlon(:,:),rlat(:,:));
vbf = griddata(vlon(p),vlat(p),vbf(p),rlon(:,:),rlat(:,:));
vb = griddata(vlon(p),vlat(p),vb(p),rlon(:,:),rlat(:,:));
vbar = griddata(vlon(p),vlat(p),vbar(p),rlon(:,:),rlat(:,:));
vbarf = griddata(vlon(p),vlat(p),vbarf(p),rlon(:,:),rlat(:,:));

save velocity_tavg.mat usf us ubf ub ubar ubarf vsf vs vbf vb vbar vbarf

clear usf us ubf ub ubar ubarf vsf vs vbf vb vbar vbarf
end
if(~exist('Hwave','var'));
    Hwave = get_variable('Hwave',name,diry);
    HF = Hwave(921:1009,:,:);
    Hwave = squeeze(mean(Hwave));
    
    Dwave = get_variable('Dwave',name,diry);
    DF = Dwave(921:1009,:,:);
    Dwave = squeeze(mean(Dwave));
    
    save Hwave.mat Hwave HF Dwave DF
end
if(~exist([diry,'stress.mat'],'file'));
disp('Getting Bed Stress');
bustrc = get_variable('bustrc',name,diry);
bustrcf = squeeze(mean(bustrc(921:1009,:,:)));
bustrc = squeeze(mean(bustrc));

bvstrc = get_variable('bvstrc',name,diry);
bvstrcf = squeeze(mean(bvstrc(921:1009,:,:)));
bvstrc = squeeze(mean(bvstrc));

bustrw = get_variable('bustrw',name,diry);
bustrwf = squeeze(mean(bustrw(921:1009,:,:)));
bustrw = squeeze(mean(bustrw));

bvstrw = get_variable('bvstrw',name,diry);
bvstrwf = squeeze(mean(bvstrw(921:1009,:,:)));
bvstrw = squeeze(mean(bvstrw));


save stress.mat bustrc bustrcf bvstrc bvstrcf bustrw bustrwf bvstrw bvstrwf

clear bustrc bustrcf bvstrc bvstrcf bustrw bustrwf bvstrw bvstrwf
end

if(~exist([diry,'stress_at_S4.mat'],'file'));
disp('Getting stress at the S4s using parallel matlab');
offset = 0;

if(~exist('mI','var'))
    disp('Calculating I/J locations')
    
    fname = dir([diry,name]);
    fname = fname(1).name;
    llm_all

    StI = [177.95131 177.95373 177.96478 177.96231];
    StJ = [-38.730443 -38.720727 -38.696380 -38.721163];
    
   
    for i=1:length(StI)
        [mI(i),mJ(i)] = near2d(rlon,rlat,StI(i),StJ(i));
        mI(i)=mI(i)+offset; mJ(i)=mJ(i)+offset;
    end
end
disp('Getting Stresses')
for i=1:length(mI);
    bustrc(i,:) = get_ptvariable('bustrc',name,[mI(i) mJ(i)],0);
    bvstrc(i,:) = get_ptvariable('bvstrc',name,[mI(i) mJ(i)],0);
    bustrw(i,:) = get_ptvariable('bustrw',name,[mI(i) mJ(i)],0);
    bvstrw(i,:) = get_ptvariable('bvstrw',name,[mI(i) mJ(i)],0);
end

save stress_at_S4.mat bustrc bvstrc bustrw bvstrw offset
clear bustrc bvstrc bustrw bvstrw offset
end

% if(exist('mpclose','var'))
%     matlabpool close
% end