function velocity_arrows_sequence ( xy_filename, uv_filename, scale ) %% VELOCITY_ARROWS_SEQUENCE creates velocity plots from a sequence of datasets. % % Discussion: % % This program can be used to turn data files into a sequence of PNG % images, suitable for an animation. % % Although this program can handle a single input file, its most useful % application is in processing many files, with 'consecutive' names, such % as "fred01.txt", "fred02.txt", "fred03.txt" and so on. The user only % has to tell the program to process the first file, and it will automatically % continue to process files until it reaches the end of the sequence of names. % % The input files: % =============== % % The XY input data file stores the coordinates of the points, which are presumed % to be fixed over all the data files, one pair of values per line. % % Each UV input data file is assumed to store the value of the horizontal and % vertical velocity at the corresponding point, one pair of values per line. % % Internal processing: % =================== % % Once the first file is processed, the routine checks to see if there % is a second "consecutive" file, and it so, it is also processed, and % so on, until presumably all files in the sequence have been handled. % % Usage: % % velocity_arrows_sequence ( xy_filename, uv_filename, scale ) % % Note that a literal filename must be enclosed in single quotes. % Thus, a typical invocation might be % % velocity_arrows_sequence ( 'xy.txt', 'u0001.txt', 1.0 ) % % But if you simply say % % velocity_arrows_sequence % % the program will give you a chance to enter the data interactively. % % Modified: % % 28 November 2005 % % Author: % % John Burkardt % % Parameters: % % Input, string XY_FILENAME, the name of the XY input file. % % Input, string UV_FILENAME, the name of the (first) UV input file. % % Input, real SCALE, the scale factor for the arrow size. Setting SCALE to 1 % results in the default vector size. % fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE:\n' ); fprintf ( 1, ' Given one XY file, and a sequence of UV files,\n' ); fprintf ( 1, ' display the velocity fields\n' ); fprintf ( 1, ' and save each image in a PNG file.\n' ); % % If at least one command line argument, it's the X file name. % if ( nargin < 1 ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE:\n' ); xy_filename = input ( ' Enter the XY file, such as ''xy.txt'':' ); end if ( ~file_exist ( xy_filename ) ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE - Fatal error!\n' ); fprintf ( 1, ' The file "%s" does not exist.\n', xy_filename ); return end % % If two command line arguments, the second is the UV file name. % if ( nargin < 2 ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE:\n' ); uv_filename = input ( ... ' Enter the (first) UV file, such as ''uv01.txt'':' ); end if ( ~file_exist ( uv_filename ) ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE - Fatal error!\n' ); fprintf ( 1, ' The file "%s" does not exist.\n', uv_filename ); return end % % If three command line arguments, the third is the scale factor. % if ( nargin < 3 ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE:\n' ); scale = input ( ' Enter the scale factor:' ); end % % Save the name of the first input file, and set the name of the first PNG file. % uv_root = uv_filename; png_root = file_name_ext_swap ( uv_root, 'png' ); png_filename = png_root; fprintf ( 1, '\n' ); fprintf ( 1, ' The first PNG file will be %s\n', png_root ); % % Read the data in the XY file. % xy = load ( xy_filename ); x = xy(:,1); y = xy(:,2); node_num = length ( x ); if ( node_num < 1 ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE - Fatal error!\n' ); fprintf ( 1, ' The number of nodes in the XY file is less than 1.\n' ); error ( 'VELOCITY_ARROWS_SEQUENCE - Fatal error!' ); end % % Save data for the frame. % x_min = min ( x ); x_max = max ( x ); y_min = min ( y ); y_max = max ( y ); delta = 0.05 * max ( x_max - x_min, y_max - y_min ); x_frame = [ x_min - delta, ... x_max + delta, ... x_max + delta, ... x_min - delta, ... x_min - delta ]; y_frame = [ y_min - delta, ... y_min - delta, ... y_max + delta, ... y_max + delta, ... y_min - delta ]; % % Determine the velocity magnitude scale over the WHOLE range of datasets. % mag_max = 0.0; uv_filename = uv_root; while ( 1 ) uv = load ( uv_filename ); node_num2 = length ( uv(:,1) ); if ( node_num2 ~= node_num ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE - Fatal error!\n' ); fprintf ( 1, ' The number of nodes in %s is %d.\n', ... uv_filename, node_num2 ); fprintf ( 1, ' The number of nodes in %s is %d.\n', ... xy_filename, node_num ); error ( 'VELOCITY_ARROWS_SEQUENCE - Fatal error!' ); end mag = sqrt ( uv(:,1).^2 + uv(:,2).^2 ); mag_max = max ( mag_max, max ( mag ) ); uv_filename = file_name_inc ( uv_filename ); if ( ~file_exist ( uv_filename ) ) break end end if ( mag_max == 0.0 ) fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE - Fatal error!\n' ); fprintf ( 1, ' The input data is entirely zero.\n' ); fprintf ( 1, ' No plot is possible.\n' ); error ( 'VELOCITY_ARROWS_SEQUENCE - The input data is zero!' ); end % % Create the plots. % uv_filename = uv_root; frame_num = 0; % % Loop over all files. % while ( 1 ) uv = load ( uv_filename ); quiver ( x, y, uv(:,1), uv(:,2), scale / mag_max, 'b' ); axis equal hold on k = convhull ( x, y ); plot ( x(k), y(k), 'r' ); hold on x_min = min ( x ); x_max = max ( x ); y_min = min ( y ); y_max = max ( y ); delta = 0.05 * max ( x_max - x_min, y_max - y_min ); plot ( x_frame, y_frame, 'w' ); hold off xlabel ( 'X', 'FontName', 'Helvetica', 'FontWeight', 'bold', ... 'FontSize', 16 ); ylabel ( 'Y', 'FontName', 'Helvetica', 'FontWeight', 'bold', ... 'FontSize', 16, 'Rotation', 0 ); title ( uv_filename, 'FontName', 'Helvetica', 'FontWeight', ... 'bold', 'FontSize', 16 ); % % Here's where we take a snapshot of the current image, and save it to % a PNG file. Doing this was surprisingly complicated, and the help % system was not very clear. Surely there's a more straightforward way! % F = getframe; [ XX, map ] = frame2im ( F ); imwrite ( XX, png_filename, 'PNG' ); frame_num = frame_num + 1; % % Prepare for next iteration. % uv_filename = file_name_inc ( uv_filename ); if ( ~file_exist ( uv_filename ) ) break end png_filename = file_name_inc ( png_filename ); end fprintf ( 1, '\n' ); fprintf ( 1, ' Processed %d frames.\n', frame_num ); fprintf ( 1, '\n' ); fprintf ( 1, 'VELOCITY_ARROWS_SEQUENCE:\n' ); fprintf ( 1, ' Normal end of execution.\n' );