%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RASTER TO XYZ
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Produced by Joseph Wheaton
% Last Updated: 18 September 2007
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% This script reads in an ASCII raster file and then outputs a CSV file in
% XYZ format with a point (row) for each grid cell. It is slow because
% most of the IO part of the code can not be vectorized, but it does
% the trick.
%------Read in Data -------------------------------------------------
% Prompts User to select the most recent DEM file name
[filename, pathname]=uigetfile('*.asc',strcat('Load the ASCII raster you wish to convert.'));
filename_NewDEM=[pathname filename];
fid=fopen(filename_NewDEM,'r'); % opens file to fid in read only mode
dum1=fscanf(fid,'%s',1); % assigns first part of header (ncols) to dummy variable dum1 (%s is string notation)
nx=fscanf(fid,'%u',1); % stores actual number of collumns to nx (%u is decimal notation)
dum2=fscanf(fid,'%s',1); % assigns second line header info (nrows) to dummy variable dum2
ny=fscanf(fid,'%u',1); % stores actual number of rows to ny
dum3=fscanf(fid,'%s',1); % assigns third line header info (xllcorner) to dummy variable dum3
xll=fscanf(fid,'%f',1); % stores actual lower left x coordinate corner to xll (%f is fixed point notation)
dum4=fscanf(fid,'%s',1); % assigns fourth line header info (yllcorner) to dummy variable dum4
yll=fscanf(fid,'%f',1); % stores actual lower left y coordinate corner to yll
dum5=fscanf(fid,'%s',1); % assigns fifth line header info (cellsize) to dummy variable dum5
lx=fscanf(fid,'%f',1); % stores cell size in lx
dum6=fscanf(fid,'%s',1); % assigns sixth line header info (nodata) to dummy variable dum6
nodata=fscanf(fid,'%f',1); % stores the no data ARC tag to nodata
Z=fscanf(fid,'%f',[nx,ny]); % stores all of the cell data in a double array dimensioned according to nx and ny
fclose(fid); % closes the file
fprintf('Done reading ARC data.\n');
fprintf('\n')
%------Make the X,Y,Z Arrays -------------------------------------------------
xleft = xll + (0.5*lx);
ytop = yll + (ny*lx) - (0.5*lx);
xPos = xleft;
yPos = ytop;
fprintf('Processing points... be patient. \n');
for i=1:ny; % Begin main gridcell loop (rows)
for j=1:nx; % Begin gridcell loop (collumns)
if ((Z(j,i) == nodata) | (Z(j,i) == 0));
X(j,i) = nodata;
Y(j,i) = nodata;
else
X(j,i) = xPos;
Y(j,i) = yPos;
end
% Advance for next collmn
xPos = xPos + lx;
end % End gridcell loop (collumns)
% At end of row reset xPos
xPos = xleft;
% and advance yPos to next row
yPos = yPos - lx;
end
%------Write the Output File ---------------------
% SORT OUT FILE NAME AND PATH:
[filename,pathname]=uiputfile('*.csv','Choose a name to save the XYZ file to'); % Select final file name
csv_file_name=[pathname filename '.csv'];
fprintf('Creating file... be patient. \n');
%Write DoD data to a temporary file in ARC format
fid1= fopen(csv_file_name, 'w');
% Write Header
fprintf(fid1,'Easting,Northing,Elevation\n');
i=0;
j=0;
for i=1:ny; % Begin main gridcell loop (rows)
for j=1:nx; % Begin gridcell loop (collumns)
if (Z(j,i) ~= nodata);
fprintf(fid1, '%12.3f,%12.3f,%6.3f\n', X(j,i), Y(j,i), Z(j,i));
end
end % End gridcell loop (collumns)
end % End main gridcell loop (rows)
fclose(fid1);
fprintf('It worked... imagine that. Done writing %s', csv_file_name);
% DONE