function [dat,vals,vstr]=extract_hab(fname) % extract_hab: code that will extract inst_heights info from a .xls % % xls is based on the mooring log with inst. heights added % outputs a usable structure for applying to existing netcdf files % as part of the water_depth correction process. Called by % do_adj_tripod_dep.m % % usage [dat, dnums, dstrs]=extract_hab('test_mbay_senshab.xls'); % expects the name of an excel file as input % outputs: dat is a structure containing these fields: % tripod_id, habname, hab, sn- % the last 3 are cell arrays % dnums are the numeric elements returned by xlsread % dstrs are the string elements returned by xlsread % % this can only run on PC because xlsread doesn't work well on linux. % HAVE TO make column 1 & _sn columns 'text' and any with _cmab 'numeric' % %emontgomery 11/17/08 % % Program written in Matlab v7.4.0336 (2007a) % Program updated in Matlab 7.2.0.232 (R2006a) % Program ran on PC with Windows XP Professional OS. % % "Although this program has been used by the USGS, no warranty, % expressed or implied, is made by the USGS or the United States % Government as to the accuracy and functioning of the program % and related program material nor shall the fact of distribution % constitute any such warranty, and no responsibility is assumed % by the USGS in connection therewith." if ~ispc disp ('this can only run on a PC due to xlsread- sorry!') return else if ~exist(fname,'file') disp ('file name entered does not exist, please choose another') [fname, pathname, filterindex] = uigetfile('*.xls','Open a xls file with heights above bottom'); end [vals, vstr, vraw]=xlsread(fname); % often there are more labels than data, so usr length(vals) as limit [rws,cols]=size(vals); % look at the title line and find the columns with _cmab and _sn xx=strfind(vstr(1,1:cols),'_sn'); k=1; for kk=1:length(xx) tf=isempty(xx{kk}); if tf==0 snloc(k)=kk; k=k+1; end end clear xx; k=1; xx=strfind(vstr(1,1:cols),'_cmab'); for kk=1:length(xx) tf=isempty(xx{kk}); if tf==0 habloc(k)=kk; k=k+1; end end clear xx tf kk k; tmpsn={'?'}; % make sure we don't run into trouble with lengths later if length(habloc) > length(snloc) ff=[habloc;habloc]; ff(2,1:length(snloc))=snloc; snloc=ff(2,:); end % loop through the rows to collect each instrumnet's height and sn into a % structure- row 1 has the titles, and the rest have content, thus start % the loop at 2 and put things into jj-1 kntr=1; for jj=2:rws % there are still some fields where content may be in an unexpected % place, height should be in vals, but will be nan if missing idx=find(~isnan(vals(jj-1,habloc))); if isempty(idx) disp(['no inst. heights for tripod ' char(vstr(jj))]) kntr=kntr+1; else locs=find(habloc(idx)>16); %get beyond the header info section % now put the bits we need into the structure if strcmp(vstr(jj,1),'') %check whether mooring ID is numeric dat(jj-kntr).tripodid=cellstr(num2str(vals(jj-1,1))); else dat(jj-kntr).tripodid=vstr(jj,1); end dat(jj-kntr).habname=vstr(1,habloc(idx(locs))); dat(jj-kntr).hab=vals(jj-1,habloc(idx(locs))); % _sn files should all be text in the input .xls file for tt=1:length(locs) if ~isnan(vals(jj-kntr,snloc(idx(locs(tt))))) tmpsn(tt)=cellstr(num2str(vals(jj-1,snloc(idx(locs(tt)))))); elseif strcmp(vstr(jj,snloc(idx(locs(tt)))),'') tmpsn(tt)=cellstr('0'); else tmpsn(tt)=cellstr(vstr(jj,snloc(idx(locs(tt))))); end end dat(jj-kntr).sn=tmpsn; clear idx locs tmpsn disp ([num2str(jj) ' records processed']) end %if isempty idx end disp(['finished parsing ' fname]) end