Code Sample: Modify AeroVis files for Matlab

This is one in a series of code examples. Others: Timing for C++

You may find yourself in a situation where you have to modify the data directly in an AeroVis input file.  For example, I had to change all my objectives into metric units.  The following code, “aerovischangeformat.m” took the old aerovis input file, “wrrcaseC_aerovis.out” and changed it to “wrrcaseC_aerovis_metric.out”.

This is a good exercise to use Matlab’s low level file input/output, as explained here, to do some powerful stuff.  I’d suggest modifying this code for your own purposes if you need it.  To use these codes, copy and paste them into new text files from the website.

aerovischangeformat.m

 filename_base = 'wrrcaseC_aerovis';
 extension = '.out';
 modifier = '_metric';
 num_headerlines = 5;
 num_columns = 7;
 header_rewrite_ix = 2;
 header_rewrites = {'# <DATA_HEADER> Cost, Reliability, Dropped Transfers , Surplus Water, Rights, Options, Leases'};


%% Perform calcs

%open file
 fid = fopen([filename_base extension]);

%read headers
 for i = 1:num_headerlines
     headers{i} = fgetl(fid);
 end

%get data
 rawdata = fscanf(fid, '%f', inf);
 fclose(fid);

%reshape data
 raw_it = 1;
 row_it = 1;
 while 1
     if raw_it > length(rawdata)
        break;
     end
     for col_it = 1:num_columns
         data(row_it, col_it) = rawdata(raw_it);
         raw_it = raw_it + 1;
     end
     row_it = row_it + 1;
 end

%modify data
 data(:,1) = data(:,1)./1000000; %in millions
 data(:,3) = data(:,3).*(1233.48184/1e6); %acre-foot into cubic m
 data(:,4) = data(:,4).*(1233.48184/1e6); %acre-foot into cubic m
 data(:,5) = data(:,5).*(1233.48184/1e6); %acre-foot into cubic m
 data(:,6) = data(:,6).*(1233.48184/1e6); %acre-foot into cubic m
 data(:,7) = data(:,7).*(1233.48184/1e6); %acre-foot into cubic m

%write new file
 fid = fopen([filename_base modifier extension], 'wt');

  rewrite_it = 1;
 for i = 1:length(headers)
     if i ~= header_rewrite_ix(rewrite_it)
          fprintf(fid, '%s\n', headers{i});
     else
          fprintf(fid, '%s\n', header_rewrites{rewrite_it});
          if ( (rewrite_it + 1) <= length(header_rewrites))
              rewrite_it = rewrite_it + 1;
          end
     end
 end

%now write data
 outputformat = '%15.6f';
 for i = 1:num_columns-1
     outputformat = [outputformat '\t%15.6f'];
 end
 outputformat = [outputformat '\n'];

row_it = 1;
 while 1
     fprintf(fid, outputformat, data(row_it,:));
     row_it = row_it+1;
     if (row_it > size(data,1)) break; end;
 end
 fprintf(fid, '#');

fclose(fid);

wrrcaseC_aerovis.out

 # <GEN_HEADER> RS, Run, Gen, Popsize, Archive, NFE, Eperf, Eind, Time
 # <DATA_HEADER> cost, reliability, drops, surplus, rights, leases, options
 #
 2 6 16 1454 957 469224 0.000 0.000000 196470.880000
 #
 9000000.00000 0.98287 33956.00000 11818.40000 30190.00000 17504.66900 84891.78000
 9000000.00000 0.98550 33357.30000 11737.50000 30143.00000 17475.20000 84418.66000
 9000000.00000 0.98737 34495.60000 11854.20000 30009.00000 18333.50000 85987.38000
 9000000.00000 0.98405 35056.30000 11918.00000 30105.00000 17504.29000 86682.51000
 9000000.00000 0.98548 32555.40000 11619.40000 30212.00000 17511.52000 83590.19000
 #

wrrcaseC_aerovis_metric.out

 # <GEN_HEADER> RS, Run, Gen, Popsize, Archive, NFE, Eperf, Eind, Time
 # <DATA_HEADER> Cost, Reliability, Dropped Transfers , Surplus Water, Rights, Options, Leases
 #
 2 6 16 1454 957 469224 0.000 0.000000 196470.880000
 #
 9.000000 0.982870 41.884109 14.577782 37.238817 21.591691 104.712469
 9.000000 0.985500 41.145624 14.477993 37.180843 21.555342 104.128884
 9.000000 0.987370 42.549696 14.621940 37.015557 22.614039 106.063872
 9.000000 0.984050 43.241309 14.700637 37.133971 21.591224 106.921302
 9.000000 0.985480 40.156495 14.332319 37.265953 21.600142 103.106981
 #

Advertisements

3 thoughts on “Code Sample: Modify AeroVis files for Matlab

  1. Pingback: Code Sample: Timing Functions for C++ « Pat Reed Group Research Tips Blog

  2. Pingback: Using linux “split” « Pat Reed Group Research Tips Blog

  3. Pingback: Water Programming Blog Guide (3) – Water Programming: A Collaborative Research Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s