# VMT_ReadFiles

## PURPOSE

Read in multiple ASCII output files from WinRiver or WinRiverII.

## SYNOPSIS

function [zPathName,zFileName,savefile,A,z] = VMT_ReadFiles(zPathName,zFileName)

## DESCRIPTION

     Read in multiple ASCII output files from WinRiver or WinRiverII.

     Added save path functionality (PRJ, 6-23-10)
     (adapted from code by J. Czuba)

     See also: tfile (utils folder)

     P.R. Jackson, USGS, 12-11-08
     Last modified: F.L. Engel, USGS, 2/20/2013

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- VMT --- THE VELOCITY MAPPING TOOLBOX ---

## SUBFUNCTIONS

- function A = initStructure(z)

## SOURCE CODE

    0001 function [zPathName,zFileName,savefile,A,z] = VMT_ReadFiles(zPathName,zFileName)
    0002 % Read in multiple ASCII output files from WinRiver or WinRiverII.
    0003 %
    0004 % Added save path functionality (PRJ, 6-23-10)
    0005 % (adapted from code by J. Czuba)
    0006 %
    0007 % See also: tfile (utils folder)
    0008 %
    0009 % P.R. Jackson, USGS, 12-11-08
    0010 % Last modified: F.L. Engel, USGS, 2/20/2013
    0011
    0012
    0013
    0014 %% Read in multiple ASCII .TXT Files
    0015 % This program reads in multiple ASCII text files into a single structure.
    0016
    0017 if ischar(zFileName)
    0018     zFileName = {zFileName};
    0019 elseif iscellstr(zFileName)
    0020     zFileName = sort(zFileName);
    0021 end
    0022 %msgbox('Loading Data...','VMT Status','help','replace')
    0023
    0024 %% Read in Selected Files
    0025 % Initialize the data structure
    0026 z = length(zFileName);
    0027 A = initStructure(z);
    0028
    0029 % Begin master loop
    0030 for zi=1:z
    0031     % Open txt data file
    0032     fileName = fullfile(zPathName,zFileName{zi});
    0033
    0034     % screenData 0 leaves bad data as -32768, 1 converts to NaN
    0035     % ignoreBS 0 processes BS data, 1 ignores it
    0036     screenData = 1;
    0037     ignoreBS   = 0;
    0038
    0039     % tfile reads the data from an RDI ASCII output file and puts the
    0040     % data in a Matlab data structure with major groups of:
    0041     % Sup - supporing data
    0042     % Wat - water data
    0043     % Nav - navigation data including GPS.
    0044     % Sensor - Sensor data
    0045     % Q - discharge related data
    0046
    0047     try
    0048         [A(zi)]=tfile(fileName,screenData,ignoreBS);
    0049
    0050         [~,file_name,extension] = fileparts(fileName);
    0051         new_message = strrep(['Loading file ' file_name extension],'_','\_');
    0052 %         if ishandle(h_waitbar)
    0053 %             waitbar(zi/z,h_waitbar,new_message)
    0054 %         else
    0055 %             h_waitbar = waitbar(zi/z,new_message,'Name','Loading Files');
    0056 %         end
    0057
    0058     catch err
    0059
    0060         erstg = {'                                                      ',...
    0061                  'An unknown error occurred when reading the ASCII file.',...
    0062                  'Occasionally this occurs due to a corrupt ASCII file with',...
    0063                  'formatting errors. Please regenerate the ASCII file and ',...
    0064                  'retry loading into VMT. An error may also occur if there ',...
    0065                  'are white spaces or special characters (e.g. *?<>|) in ',...
    0066                  'the filenames or paths. Ensure no such spaces or ',...
    0067                  'characters exist and try loading the files again.'};
    0068
    0069         if isdeployed
    0070         errLogFileName = fullfile(pwd,...
    0071             ['errorLog' datestr(now,'yyyymmddHHMMSS') '.txt']);
    0072         msgbox({erstg;...
    0073             ['  Error code: ' err.identifier];...
    0074             ['Error details are being written to the following file: '];...
    0075             errLogFileName},...
    0076             'VMT Status: Unexpected Error',...
    0077             'error');
    0078         fid = fopen(errLogFileName,'W');
    0079         fwrite(fid,err.getReport('extended','hyperlinks','off'));
    0080         fclose(fid);
    0081         rethrow(err)
    0082     else
    0083         msgbox(['An unexpected error occurred. Error code: ' err.identifier],...
    0084             'VMT Status: Unexpected Error',...
    0085             'error');
    0086         rethrow(err);
    0087     end
    0088     end
    0089
    0090     % Check the units to ensure they are metric
    0091
    0092     if ~strcmp(A(zi).Sup.units,'cm')
    0093         erstg = {'                                                      ',...
    0094                  'Units in ASCII file are not metric.  VMT only accepts data'...
    0095                  'in metric units.  Please change the units in WinRiver II'...
    0096                  'and export your ASCII files again before reloading into VMT.'};
    0097         errordlg([zFileName(zi) erstg],'VMT Status','replace');
    0098         error('VMT:unitsChk', 'Input not in metic units')
    0099     end
    0100
    0101 end
    0102
    0103 % Get rid of the waitbar
    0104 % if ishandle(h_waitbar)
    0105 %     delete(h_waitbar)
    0106 % end
    0107
    0108 % Save data returned by tfile to .mat with same prefix as ASCII
    0109 [file_root_name,the_rest] = strtok(zFileName,'_');
    0110 file_numbers = strtok(the_rest,'_');
    0111
    0112 save_dir = fullfile(zPathName,'VMTProcFiles');
    0113 [~,mess,~] = mkdir(save_dir);
    0114 % disp(mess)
    0115
    0116 savefile = [file_root_name{1} '_' file_numbers{1} '_' file_numbers{end} '.mat'];
    0117 savefile = fullfile(save_dir,savefile);
    0118
    0119 %%%%%%%%%%%%%%%%%%%%%%
    0120 % Embedded Functions %
    0121 %%%%%%%%%%%%%%%%%%%%%%
    0122
    0123 function A = initStructure(z)
    0124    Sup = struct('absorption_dbpm',{}, ...
    0125                 'bins',{}, ...
    0126                 'binSize_cm',{}, ...
    0127                 'nBins',{}, ...
    0128                 'blank_cm',{}, ...
    0129                 'draft_cm',{}, ...
    0130                 'ensNo',{}, ...
    0131                 'nPings',{}, ...
    0132                 'noEnsInSeg',{}, ...
    0133                 'noe',{}, ...
    0134                 'note1',{}, ...
    0135                 'note2',{}, ...
    0136                 'intScaleFact_dbpcnt',{}, ...
    0137                 'intUnits',{}, ...
    0138                 'vRef',{}, ...
    0139                 'wm',{}, ...
    0140                 'units',{}, ...
    0141                 'year',{}, ...
    0142                 'month',{}, ...
    0143                 'day',{}, ...
    0144                 'hour',{}, ...
    0145                 'minute',{}, ...
    0146                 'second',{}, ...
    0147                 'sec100',{}, ...
    0148                 'timeElapsed_sec',{}, ...
    0149                 'timeDelta_sec100',{});
    0150    Wat = struct('binDepth',{}, ...
    0151                 'backscatter',{}, ...
    0152                 'vDir',{}, ...
    0153                 'vMag',{}, ...
    0154                 'vEast',{}, ...
    0155                 'vError',{}, ...
    0156                 'vNorth',{}, ...
    0157                 'vVert',{}, ...
    0158                 'percentGood',{});
    0159    Nav = struct('bvEast',{}, ...
    0160                 'bvError',{}, ...
    0161                 'bvNorth',{}, ...
    0162                 'bvVert',{}, ...
    0163                 'depth',{}, ...
    0164                 'dsDepth',{}, ...
    0165                 'dmg',{}, ...
    0166                 'length',{}, ...
    0167                 'totDistEast',{}, ...
    0168                 'totDistNorth',{}, ...
    0169                 'altitude',{}, ...
    0170                 'altitudeChng',{}, ...
    0171                 'gpsTotDist',{}, ...
    0172                 'gpsVariable',{}, ...
    0173                 'gpsVeast',{}, ...
    0174                 'gpsVnorth',{}, ...
    0175                 'lat_deg',{}, ...
    0176                 'long_deg',{}, ...
    0177                 'nSats',{}, ...
    0178                 'hdop',{});
    0179    Sensor = struct('pitch_deg',{}, ...
    0180                    'roll_deg',{}, ...
    0181                    'heading_deg',{}, ...
    0182                    'temp_degC',{});
    0183      Q = struct('endDepth',{}, ...
    0184                 'endDist',{}, ...
    0185                 'bot',{}, ...
    0186                 'end',{}, ...
    0187                 'meas',{}, ...
    0188                 'start',{}, ...
    0189                 'top',{}, ...
    0190                 'unit',{}, ...
    0191                 'startDepth',{}, ...
    0192                 'startDist',{});
    0193             A(z).Sup = Sup;
    0194             A(z).Wat = Wat;
    0195             A(z).Nav = Nav;
    0196             A(z).Sensor = Sensor;
    0197             A(z).Q = Q;
    0198
    0199             % [EOF] initStructure