function WAVE=waverider_to_data(data_root,out_root,nhead,Jday_lims)
%
% Call WAVE=waverider_to_data(data_root,out_root,nhead,Jday_lims)
%
% Converts Waverider data file to Matlab DATA structure for use with view5, etc;
%
% Example call:     WAVE=waverider_to_data('01*','hce02_site1',12);
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
if nargin<3, nhead=12; end
if nargin<4, Jday_lims=[]; end
%
D=dir([data_root,'*.dat']); nfile=length(D);
if nfile>1, disp('WARNING: only processing first file that matched the data_root string'); end
%
fmt = '%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f';
%
jfile=1;
[month,day,year,hour,minute,sec,wave_h,wave_dir,wind_v1,wind_dir1,wind_v2,wind_dir2,air_temp,water_temp,voltage]= ...
    textread(D(jfile).name,fmt,-1,'headerlines',nhead);
year=year + 2000;
Jday=date_to_Jday(month,day,2000+year);
nday_year=365; if mod(year(1),4)==0, nday_year=366; end
hour=(year - year(1))*nday_year + (Jday - Jday(1))*24 + hour + minute/60 + sec/3600;
Jday_dec=Jday(1) + hour/24;
data=[wave_h,wave_dir,wind_v1,wind_v2,wind_dir1,wind_dir2,air_temp,water_temp,voltage];
labels={['Julian Day ',int2str(year(1))],  ...
        ['Wave H/Wave Dir/Wind V1/Wind V2/Wind Dir1/Wind Dir2/Air T/Water T/Volt']};
scales={Jday_dec(:)' [1:size(data,2)]}; dlab='Waverider Value'; iidB=0; iifig=0; title=' ';
WAVE=mat2data(data,labels,scales,dlab,title,iidB,iifig); 
WAVE.ii_tight=1;
if ~isempty(Jday_lims),
    disp('Parsing WAVE to include Jday_lims');
    WAVE=data_parse(WAVE,1,Jday_lims,0);
end
WAVE.title=[out_root,'_',int2str(WAVE.scales{1}(1)),'_',int2str(WAVE.scales{1}(end)),'_WAVE'],
str=['save ',WAVE.title,' WAVE']; disp(str); eval(str);
view5(WAVE);
