# ASCII2KML

## PURPOSE

WinRiver ASCII to Google Earth KML

## SYNOPSIS

function [log_text,zPathName,zFileName] = ASCII2KML(inpath,infile)

## DESCRIPTION

     WinRiver ASCII to Google Earth KML

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- ASCII2KML_GUI ASCII2KML_GUI M-file for ASCII2KML_GUI.fig

## SUBFUNCTIONS

- function pwr_kml(name,latlon)

## SOURCE CODE

    0001 function [log_text,zPathName,zFileName] = ASCII2KML(inpath,infile)
    0002 % WinRiver ASCII to Google Earth KML
    0003
    0004 % This program reads in an ASCII file or files generated from WinRiver
    0005 % Classic ASCII output and outputs the GPS position into a KML file which
    0006 % can be displayed in Google Earth.
    0007 %
    0008 %(adapted from code by J. Czuba)
    0009
    0010 % P.R. Jackson 9/4/09
    0011 % Last Modified: Frank L. Engel, 3/13/2013
    0012
    0013 %% Read and Convert the Data
    0014
    0015 % Determine Files to Process
    0016 % Prompt user for directory containing files
    0017 current_file = fullfile(inpath,infile);
    0018 [zFileName,zPathName] = uigetfile({'*_ASC.TXT','ASCII (*_ASC.TXT)'}, ...
    0019     'Select the ASCII Output Files', ...
    0020     current_file, ...
    0021     'MultiSelect','on');
    0022 if ~ischar(zFileName) && ~iscell(zFileName) % User hit cancel, get out quick
    0023     log_text = {};
    0024     zPathName = inpath;
    0025     zFileName = infile;
    0026     return
    0027 end
    0028
    0029 % Determine number of files to be processed
    0030 if  isa(zFileName,'cell')
    0031     z=size(zFileName,2);
    0032     zFileName=sort(zFileName);
    0033 else
    0034     z=1;
    0035     zFileName={zFileName}
    0036 end
    0037 %msgbox('Loading Data...Please Be Patient','Conversion Status','help','replace');
    0038 % Read in Selected Files
    0039 % % Initialize row counter
    0040 % j=0;
    0041 % st=['A'; 'B'; 'C'; 'D'; 'E'; 'F'];
    0042 % Begin master loop
    0043 log_text = {...
    0044     'Writing KML Files to directory:';
    0045     zPathName};
    0046 wbh = waitbar(0,'Writing KML Files...Please Be Patient');
    0047 for zi=1:z
    0048     % Open txt data file
    0049     if  isa(zFileName,'cell')
    0050         fileName=strcat(zPathName,filesep,zFileName(zi));
    0051         fileName=char(fileName);
    0052     else
    0053         fileName=strcat(zPathName,zFileName);
    0054     end
    0055
    0056     % screenData 0 leaves bad data as -32768, 1 converts to NaN
    0057     screenData=1;
    0058
    0059     % tfile reads the data from an RDI ASCII output file and puts the
    0060     % data in a Matlab data structure with major groups of:
    0061     % Sup - supporing data
    0062     % Wat - water data
    0063     % Nav - navigation data including GPS.
    0064     % Sensor - Sensor data
    0065     % Q - discharge related data
    0066     ignoreBS = 0;
    0067     [A(zi)]=tfile(fileName,screenData,ignoreBS);
    0068     %Extract only Lat lon data
    0069     latlon(:,1)=A(zi).Nav.lat_deg(:,:);
    0070     latlon(:,2)=A(zi).Nav.long_deg(:,:);
    0071
    0072     %Rescreen data to remove missing data (30000 value)
    0073     indx1 = find(abs(latlon(:,1)) > 90);
    0074     indx2 = find(abs(latlon(:,2)) > 180);
    0075     latlon(indx1,1)=NaN;
    0076     latlon(indx2,2)=NaN;
    0077
    0078     indx3 = find(~isnan(latlon(:,1)) & ~isnan(latlon(:,2)));
    0079     latlon = latlon(indx3,:);
    0080
    0081     clear A
    0082
    0083     % Determine the file name
    0084     idx=strfind(fileName,'.');
    0085     namecut = fileName(1:idx(end)-5);
    0086
    0087     % Write latitude and longitude into a KML file
    0088     %msgbox('Writing KML Files...','Conversion Status','help','replace');
    0089     pwr_kml(namecut,latlon);
    0090
    0091     clear latlon idx namecut
    0092     waitbar(zi/z); %update the waitbar
    0093 end
    0094 delete(wbh) %remove the waitbar
    0095 msgbox('Conversion Complete','Conversion Status','help','replace');
    0096
    0097 % % Save the paths
    0098 % if exist('LastDir.mat') == 2
    0099 %     save('LastDir.mat','ascii2kmlpath','-append')
    0100 % else
    0101 %     save('LastDir.mat','ascii2kmlpath')
    0102 % end
    0103
    0104 %%
    0105 function pwr_kml(name,latlon)
    0106 %makes a kml file for use in google earth
    0107 %input:  name of track, one matrix containing latitude and longitude
    0108 %usage:  pwr_kml('track5',latlon)
    0109
    0110 header=['<kml xmlns="http://earth.google.com/kml/2.0"><Placemark><description>"' name '"</description><LineString><tessellate>1</tessellate><coordinates>'];
    0111 footer='</coordinates></LineString></Placemark></kml>';
    0112
    0113 fid = fopen([name 'ShipTrack.kml'], 'wt');
    0114 d=flipud(rot90(fliplr(latlon)));
    0115 fprintf(fid, '%s \n',header);
    0116 fprintf(fid, '%.6f,%.6f,0.0 \n', d);
    0117 fprintf(fid, '%s', footer);
    0118 fclose(fid);
    0119