% Last modified: 06/09/2012 by Pete

%% Deterministic Flow Past an Obstacle Saving Script

% create save directory and save parameters at first time step
if ( T == 0 )
    
    savedir = sprintf( '%s/Save/%s', trunk, tag ) ;
    if ( ~exist( savedir, 'dir' ) )
        mkdir(savedir)
    end
    
    if ( ~exist( sprintf( '%s/obs', savedir ), 'dir' ) )
        mkdir( sprintf( '%s/obs', savedir ) )
    end
    
    if ( ~exist( sprintf( '%s/true', savedir ), 'dir' ) )
         mkdir( sprintf( '%s/true', savedir ) )
    end

    savefile = sprintf( '%s/true/param.mat', savedir ) ;
    save(savefile)
    fprintf( 'Saved ''%s''.\n', savefile )
    
end


%% plot

u_true = u ; v_true = v ;

if ( GenFig )
    
    figure(1), clf
    
    titlestr = 'true state' ;
    u_tmp = [ NaN ; u_true(2:end) ] ;
    v_tmp = [ NaN ; v_true(2:end) ] ;
    
    % render flow field with velocity and streamlines
    Pete_plotfield
    
end


%% save

% save observations at designated sampling times
if ( obsstep <= n_obssteps && T == ObsTimes(obsstep) )
    
    obs = H*[u_true;v_true] + truenoise*randn(n_obs,1) ;
    
    savefile = sprintf( '%s/obs/%05d.mat', savedir, k) ;
    save( savefile, 'obs' )
    fprintf( 'Saved ''%s''.\n', savefile )
    
    obsstep = obsstep + 1 ;
    
end

% save data
savefile = sprintf( '%s/true/%05d.mat', savedir,k) ;
save( savefile, 'u_true', 'v_true' )
fprintf( 'Saved ''%s''.\n', savefile )

if ( GenFig )
    
    % save Figure 1
    savefile = sprintf( '%s/true/%05d.png', savedir, k) ;
    print( 1, '-dpng', savefile )
    fprintf( 'Saved ''%s''.\n', savefile )
    
end

% save observations at designated sampling times
if ( obsstep <= n_obssteps && T == ObsTimes(obsstep) )
    
    obs = H*[u_true;v_true] + truenoise*randn(n_obs,1) ;
    
    savefile = sprintf( '%s/obs/%05d.mat', savedir, k) ;
    save( savefile, 'obs' )
    fprintf( 'Saved ''%s''.\n', savefile )
    
    obsstep = obsstep + 1 ;
    
end

% save data
% savefile = sprintf( '%s/true/%05d.mat', savedir, round(100*T) ) ;
% save( savefile, 'u_true', 'v_true' )
% fprintf( 'Saved ''%s''.\n', savefile )
% 
% if ( GenFig )
%     
%     % save Figure 1
%     savefile = sprintf( '%s/true/%05d.png', savedir, round(100*T) ) ;
%     print( 1, '-dpng', savefile )
%     fprintf( 'Saved ''%s''.\n', savefile )
%     
% end