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:

SUBFUNCTIONS ^

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 % Handle varied WRII file structures (line numbers are always the LAST
0110 % token, even if user adds a measurement number, date, or whatever)
0111 [file_root_name,the_rest] = strtok(zFileName,'_');
0112 % file_numbers = strtok(the_rest,'_');
0113 for i = 1:length(zFileName)
0114     d1(i) = textscan(zFileName{i},'%s','delimiter','_','multipleDelimsAsOne',1);
0115     idx(i) = find(strcmpi(d1{:,i},'ASC.TXT'))-1;
0116 end
0117 for i = 1:length(d1)
0118     d2  = d1{i}{idx(i)};
0119     file_numbers(i) = {d2(end-2:end)};
0120 end
0121 clear d1 d2
0122 
0123 save_dir = fullfile(zPathName,'VMTProcFiles');
0124 [~,mess,~] = mkdir(save_dir); 
0125 % disp(mess)
0126 
0127 savefile = [file_root_name{1} '_' file_numbers{1} '_' file_numbers{end} '.mat'];
0128 savefile = fullfile(save_dir,savefile);
0129 
0130 %%%%%%%%%%%%%%%%%%%%%%
0131 % Embedded Functions %
0132 %%%%%%%%%%%%%%%%%%%%%%
0133 
0134 function A = initStructure(z)
0135    Sup = struct('absorption_dbpm',{}, ...
0136                 'bins',{}, ...
0137                 'binSize_cm',{}, ...
0138                 'nBins',{}, ...
0139                 'blank_cm',{}, ...
0140                 'draft_cm',{}, ...
0141                 'ensNo',{}, ...
0142                 'nPings',{}, ...
0143                 'noEnsInSeg',{}, ...
0144                 'noe',{}, ...
0145                 'note1',{}, ...
0146                 'note2',{}, ...
0147                 'intScaleFact_dbpcnt',{}, ...
0148                 'intUnits',{}, ...
0149                 'vRef',{}, ...
0150                 'wm',{}, ...
0151                 'units',{}, ...
0152                 'year',{}, ...
0153                 'month',{}, ...
0154                 'day',{}, ...
0155                 'hour',{}, ...
0156                 'minute',{}, ...
0157                 'second',{}, ...
0158                 'sec100',{}, ...
0159                 'timeElapsed_sec',{}, ...
0160                 'timeDelta_sec100',{});
0161    Wat = struct('binDepth',{}, ...
0162                 'backscatter',{}, ...
0163                 'vDir',{}, ...
0164                 'vMag',{}, ...
0165                 'vEast',{}, ...
0166                 'vError',{}, ...
0167                 'vNorth',{}, ...
0168                 'vVert',{}, ...
0169                 'percentGood',{});
0170    Nav = struct('bvEast',{}, ...
0171                 'bvError',{}, ...
0172                 'bvNorth',{}, ...
0173                 'bvVert',{}, ...
0174                 'depth',{}, ...
0175                 'dsDepth',{}, ...
0176                 'dmg',{}, ...
0177                 'length',{}, ...
0178                 'totDistEast',{}, ...
0179                 'totDistNorth',{}, ...
0180                 'altitude',{}, ...
0181                 'altitudeChng',{}, ...
0182                 'gpsTotDist',{}, ...
0183                 'gpsVariable',{}, ...
0184                 'gpsVeast',{}, ...
0185                 'gpsVnorth',{}, ...
0186                 'lat_deg',{}, ...
0187                 'long_deg',{}, ...
0188                 'nSats',{}, ...
0189                 'hdop',{});
0190    Sensor = struct('pitch_deg',{}, ...
0191                    'roll_deg',{}, ...
0192                    'heading_deg',{}, ...
0193                    'temp_degC',{});
0194      Q = struct('endDepth',{}, ...
0195                 'endDist',{}, ...
0196                 'bot',{}, ...
0197                 'end',{}, ...
0198                 'meas',{}, ...
0199                 'start',{}, ...
0200                 'top',{}, ...
0201                 'unit',{}, ...
0202                 'startDepth',{}, ...
0203                 'startDist',{});
0204             A(z).Sup = Sup;
0205             A(z).Wat = Wat;
0206             A(z).Nav = Nav;
0207             A(z).Sensor = Sensor;
0208             A(z).Q = Q;
0209             
0210             % [EOF] initStructure

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