%% mf_analyze, print and plot results fo simulation
% TO 090223 100718

clear variables
close all

film=1;  % if film==1, make avi file else not

load name
load(basename);  % this yields the stored matrices that make up the grid and the model
load underneath

%% Start

[xGr,yGr,zGr,xm,ym,zm,Dx,Dy,Dz,Nx,Ny,Nz]=modelsize3(xGr,yGr,zGr);

%% load the unformatted files with the heads, the concentration and the budget terms
H=readDat([basename,'','.hds']);
H=maskHC(H,IBOUND & H(1).values<1000);   % read heads and mask inactive cells with NaN

C=readMT3D(['MT3D001','','.UCN']);
C=maskHC(C,IBOUND);   % read concentrations and mask inactive with NaN
C=maskHC(C,ICBUND);   % read concentrations and mask inactive with NaN

B=readBud([basename,'','.bgt'],'FLOWRIGHTFACE');  B=maskHC(B,IBOUND); % read budgetfile and mask
for i=1:length(B)
  for j=1:length(B(i).term)
      B(i).term{j}(IBOUND==0 | H(1).values>1000)=NaN;
  end
end

%% Set up contours and first plot to initialize animation

ifrf=strmatch('FLOWRIGHTFACE',B(1).label);
minPsi=Inf; maxPsi=-Inf;
for i=1:length(B)
    FRF=XS(B(i).term{ifrf});  % Flow right face
    Psi=flipud([zeros(size(FRF(1,1:end-1))); cumsum(flipud(FRF(:,1:end-1)))]);
    minPsi=min([minPsi;Psi(:)]); maxPsi=max([maxPsi;Psi(:)]);
end

% Stream function mask, to not plot stream lines above sand body
PSIMASK=NaN(size(zGr(:)*xGr(2:end-1)));
PSIMASK(inpolygon(ones(size(zGr(:)))*xGr(2:end-1),zGr(:)*ones(size(xGr(2:end-1))),xSand,zSand))=1;
PSIMASK(inpolygon(ones(size(zGr(:)))*xGr(2:end-1),zGr(:)*ones(size(xGr(2:end-1))),xCanL,zCanL))=NaN;
PSIMASK(inpolygon(ones(size(zGr(:)))*xGr(2:end-1),zGr(:)*ones(size(xGr(2:end-1))),xCanR,zCanR))=NaN;
PSIMASK(inpolygon(ones(size(zGr(:)))*xGr(2:end-1),zGr(:)*ones(size(xGr(2:end-1))),xCanM,zCanM))=NaN;

concs= 0.05:0.1:0.95;      % Conc is always between 0 and 1
chead=40:0.5:100;      % Heads is always between base and top of model
cpsi =minPsi:(maxPsi-minPsi)/50:maxPsi; % Psi-range is computed above

%%
foto='Series5_01_p70.jpg';
load(['.' filesep 'Photos' filesep 'photostruct.mat']); % yields struct d
i=strmatchi(foto,{d.name});
fig=ImageModel(['Photos' filesep foto],d(i).uMdl,d(i).vMdl,d(i).DX,d(i).DY); 

%set(fig,'position',get(0,'ScreenSize'));

if film
    if ismac
        aviobj=avifile(basename,'compression','none',...
            'fps',10,...
            'videoname','Pennink, series 5, March 1905');
    else
        aviobj=avifile(basename,'compression','Cinepak',...
            'fps',10,...
            'videoname','Pennink, series 5, March 1905');
        end
end

FRF=XS(B(1).term{strmatch('FLOWRIGHTFACE',B(1).label)});
Psi=flipud([zeros(size(FRF(1,1:end-1))); cumsum(flipud(FRF(:,1:end-1)))]).*PSIMASK;

[c,hc]=contourf(xm,squeeze(zm),XS(C(1).values),concs,'g');
[c,hh]=contour (xm,squeeze(zm),XS(H(1).values),chead,'r');
[c,hp]=contour (xGr(2:end-1),squeeze(zGr),maskHC(Psi,PSIMASK),cpsi,'w');

title(sprintf('time = %g min',C(1).time));
xlabel('x [m]');                 % set x-label
ylabel('z [m]');                 % set y-label
set(gca','color',[1 1 0.8])      % light blue background color

drawnow

F=getframe(fig); 

if film, aviobj=addframe(aviobj,F); end

%% Show all the figures and put them in the avi file

for i=2:length(C)
    period=C(i).period;
    tstp  =C(i).tstp;                   % choose a timestep period present in the file
    
    ih=find([H.period]==period & [H.tstp]<=tstp,1,'last');  % H from same stress period as C
    ib=find([B.period]==period & [B.tstp]<=tstp,1,'last');  % B from same stress period as C
    set(hh,'zdata',XS(H(ih).values));  % recontour h
    set(hc,'zdata',XS(C(i).values));   % contour   C
    
    %fprintf('%d %d %d\n',i,ih,ib);
    
    FRF=XS(B(ib).term{strmatch('FLOWRIGHTFACE',B(1).label)}); FRF(isnan(FRF))=0;
    Psi=flipud([zeros(size(FRF(1,1:end-1))); cumsum(flipud(FRF(:,1:end-1)))]).*PSIMASK;
    set(hp,'zdata',maskHC(Psi,PSIMASK));  % contour stream function
    
    title(sprintf('time = %g min',C(i).time));

   drawnow

    F = getframe(fig);
    if film, aviobj = addframe(aviobj,F); end
end

if film, aviobj=close(aviobj); end