SontekMAT2KML

PURPOSE ^

Creates KML of ADCP shiptracks for Sontek RSL v3.60 MAT-files

SYNOPSIS ^

function SontekMAT2KML(varargin)

DESCRIPTION ^

 Creates KML of ADCP shiptracks for Sontek RSL v3.60 MAT-files 

 This program reads in a Sontek RiverSurveyLive ENU MAT-file or files  and
 outputs the GPS position into a KML file which can be displayed in Google
 Earth.

(adapted from ASCII2KML)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function SontekMAT2KML(varargin)
0002 % Creates KML of ADCP shiptracks for Sontek RSL v3.60 MAT-files
0003 %
0004 % This program reads in a Sontek RiverSurveyLive ENU MAT-file or files  and
0005 % outputs the GPS position into a KML file which can be displayed in Google
0006 % Earth.
0007 %
0008 %(adapted from ASCII2KML)
0009 
0010 % Frank L. Engel, 6/4/2014
0011 
0012 % Parse arguments
0013 nVar = numel(varargin);
0014 if nVar == 2
0015     inpath = varargin{1};
0016     infile = varargin{2};
0017 elseif nVar == 1
0018     if strcmpi('-help',varargin{1})
0019         % Help menu
0020         HelpTxt = {...
0021             'Creates KML of ADCP shiptracks for Sontek RSL v3.60 MAT-files';...
0022             '';...
0023             'INPUTS';...
0024             '   inputfilepath: full path to the directory containing RSL';...
0025             '                  MAT-files';...
0026             '   inputfiles:     name(s) of the RiverSurveyorLive MAT-file';...
0027             '';...
0028             'OUTPUTS';...
0029             '   KML file with the same name as the inputfile(s)';...
0030             '';...
0031             'SUMMARY';...
0032             '   This program reads in a Sontek RiverSurveyLive ENU MAT-file';...
0033             '   or files  and outputs the GPS position into a KML file which';...
0034             '   can be displayed in Google Earth.';...
0035             '';...
0036             'EXAMPLES';...
0037             '   Load a MAT-file of ENU Bottom Track ADCP data produced in';...
0038             '   RiverSurveryorLive v3.60 to produce a KML file of the';...
0039             '   shiptrack.';...
0040             '      SontekMAT2KML <path> <filename.mat>';...
0041             '';...
0042             '   To make the program prompt for files, specify NO arguments:';...
0043             '      SontekMAT2KML';...
0044             '';...
0045             '   Display this help.';...
0046             '      SontekMAT2KML -help';
0047             '';...
0048             'Original code concept by Jon Czuba, modified by P.R. Jackson';...
0049             'for use in VMT (TDRI ASCII support).';...
0050             'Created by: Frank L. Engel, USGS';...
0051             'Last modified: 2014/05/30';
0052             };
0053         disp(HelpTxt)
0054         return
0055     else
0056         error('Incorrect number of input arguments or invalid switch')
0057     end
0058 elseif nVar == 0 % Prompt user to load files
0059     inpath = [];
0060     infile = [];
0061 else
0062     error('Incorrect number of input arguments or invalid switch')
0063 end
0064 
0065 
0066 % Read and Convert the Data
0067 % Determine Files to Process
0068 % Prompt user for directory containing files
0069 if ~isempty(inpath) && ~isempty(infile)
0070     current_file = fullfile(inpath,infile);
0071 else
0072     current_file = pwd;
0073 end
0074 [zFileName,zPathName] = uigetfile({'*.mat','RSL-MAT (*.mat)'}, ...
0075     'Convert RSL MAT-file(s) to KML: Select the RSL MAT Output Files', ...
0076     current_file, ...
0077     'MultiSelect','on');
0078 if ~ischar(zFileName) && ~iscell(zFileName) % User hit cancel, get out quick
0079     log_text = {};
0080     zPathName = inpath;
0081     zFileName = infile;
0082     return
0083 end
0084 
0085 % Determine number of files to be processed
0086 if  isa(zFileName,'cell')
0087     z=size(zFileName,2);
0088     zFileName=sort(zFileName);       
0089 else
0090     z=1;
0091     zFileName={zFileName}
0092 end
0093 %msgbox('Loading Data...Please Be Patient','Conversion Status','help','replace');
0094 % Read in Selected Files
0095 % % Initialize row counter
0096 % j=0;
0097 % st=['A'; 'B'; 'C'; 'D'; 'E'; 'F'];
0098 % Begin master loop
0099 log_text = {...
0100     'Writing KML Files to directory:';
0101     zPathName};
0102 wbh = waitbar(0,'Writing KML Files...Please Be Patient');
0103 for zi=1:z
0104     % Open txt data file
0105     if  isa(zFileName,'cell')
0106         fileName=strcat(zPathName,filesep,zFileName(zi));
0107         fileName=char(fileName);
0108     else
0109         fileName=strcat(zPathName,zFileName);
0110     end
0111     load(fileName)
0112 
0113     %Extract only Lat lon data
0114     latlon(:,1)=GPS.Latitude(:);
0115     latlon(:,2)=GPS.Longitude(:);
0116     
0117     %Rescreen data to remove missing data (30000 value)
0118     indx1 = find(abs(latlon(:,1)) > 90);
0119     indx2 = find(abs(latlon(:,2)) > 180);
0120     latlon(indx1,1)=NaN;
0121     latlon(indx2,2)=NaN;
0122     
0123     indx3 = find(~isnan(latlon(:,1)) & ~isnan(latlon(:,2)));
0124     latlon = latlon(indx3,:); 
0125     
0126     clear BottomTrack GPS Processing RawGPSData Setup Summary System Transformation_Matrices WaterTrack
0127     
0128     % Determine the file name
0129     [~,namecut,~] = fileparts(fileName);
0130     namecut = [zPathName namecut];
0131         
0132     % Write latitude and longitude into a KML file
0133     %msgbox('Writing KML Files...','Conversion Status','help','replace');
0134     pwr_kml(namecut,latlon);
0135     
0136     clear latlon idx namecut 
0137     waitbar(zi/z); %update the waitbar
0138 end
0139 delete(wbh) %remove the waitbar
0140 msgbox('Conversion Complete','Conversion Status','help','replace');
0141 
0142 % % Save the paths
0143 % if exist('LastDir.mat') == 2
0144 %     save('LastDir.mat','ascii2kmlpath','-append')
0145 % else
0146 %     save('LastDir.mat','ascii2kmlpath')
0147 % end
0148 
0149 % Original function (req. outputs for VMT integration)
0150 % [log_text,zPathName,zFileName] = SontekMAT2KML(varargin)
0151 
0152 %%
0153 function pwr_kml(name,latlon)
0154 %makes a kml file for use in google earth
0155 %input:  name of track, one matrix containing latitude and longitude
0156 %usage:  pwr_kml('track5',latlon)
0157 
0158 header=['<kml xmlns="http://earth.google.com/kml/2.0"><Placemark><description>"' name '"</description><LineString><tessellate>1</tessellate><coordinates>'];
0159 footer='</coordinates></LineString></Placemark></kml>';
0160 
0161 fid = fopen([name 'ShipTrack.kml'], 'wt');
0162 d=flipud(rot90(fliplr(latlon)));
0163 fprintf(fid, '%s \n',header);
0164 fprintf(fid, '%.6f,%.6f,0.0 \n', d);
0165 fprintf(fid, '%s', footer);
0166 fclose(fid);
0167

Generated on Thu 21-Aug-2014 10:40:31 by m2html © 2005