function THERM=thermistor_to_data(data_root,out_root,nskip,nhead,Jday_lims)
%
% Call THERM=waverider_to_data(data_root,out_root,nskip,nhead,Jday_lims)
%
% Converts Waverider data file to Matlab DATA structure for use with view5, etc;
%
% Example call:     THERM=thermistor_to_data('*','test',4,16,[286 287])
%
%
% 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<4, nhead=16; end
if nargin<5 | isempty(Jday_lims), Jday_lims=[-999 999]; end
%
raw_files=dir([data_root,'*.Raw']); nfile=length(raw_files);
for jfile=1:nfile,
    disp([int2str(jfile),': ',raw_files(jfile).name])
end
iif=str2num(input('Enter file numbers to process: ','s'));
if isempty(iif), iif=[1:nfile]; end
nfile=length(iif); DD=cell(nfile,1);
for jfile=1:nfile;
    DD{jfile}=raw_files(iif(jfile)).name;
end
%
done=0;
while ~done,
    file_ax=str2num(input(['Enter axis values for ',int2str(nfile),' files: ([]=default)'],'s'));
    if isempty(file_ax), file_ax=[30 40 50 60 70 90]; disp(file_ax); end
    if length(file_ax(:))~=nfile, disp('Try again - must be nfile long'); else, done=1; end
end
nrow_max=10000; data=NaN*ones(nrow_max,nfile); time_ax=data;
for jfile=1:nfile,
	[coef1,dum,coef2,dum,coef3,dum,coef4]=textread(DD{jfile},'%f%c%f%c%f%c%f',1,'headerlines',11);
%
    fid=fopen(DD{jfile});
    for jline=1:nhead, [tline]=fgetl(fid); end
    byte0=ftell(fid); 
    tline=fgetl(fid); 
    bytes_per_line=ftell(fid) - byte0;
    [Jday_dec0,year,tline]=tline_interpret(tline,fid);
    tline=fgetl(fid); 
    [Jday_dec1,year,tline]=tline_interpret(tline,fid);
    dt=Jday_dec1 - Jday_dec0;
    if Jday_dec0<Jday_lims(1),
        time_to_skip=Jday_lims(1)-Jday_dec0;
        lines_to_skip=floor(time_to_skip/dt);
        bytes_to_skip=lines_to_skip * bytes_per_line + byte0;
        stat=fseek(fid,bytes_to_skip,-1);
%         tline=fgetl(fid); [Jday_dec0,year,tline]=tline_interpret(tline,fid);
    end
    done=0; nrow=0; 
    while ~done,
        tline=fgetl(fid);
        if length(tline)>=25,
            [Jday_dec,year,tline]=tline_interpret(tline,fid);
            if Jday_dec>=Jday_lims(1),
                val=str2num(tline(21:24)); 
                temperature=coef1 + coef2*val + coef3*val.^2 + coef4*val.^3;
                nrow=nrow + 1;
                if nrow==1 & jfile==1,  Jday0=floor(Jday_dec); year0=year; end
                time_ax(nrow,jfile)=Jday_dec;
                data(nrow,jfile)=temperature;
                if nrow==nrow_max,
                    data=[data; NaN*ones(nrow_max,nfile)]; time_ax=[time_ax; NaN*ones(nrow_max,nfile)];
                end
                if nskip>0, 
                    stat=fseek(fid,round(nskip)*bytes_per_line,0);
                    if stat<0, done=1; disp('End of file on nskip'); end
                end
                if mod(nrow,1000)==0, 
                    disp(['Finished row ',int2str(nrow),' of file ',int2str(jfile),': Jday = ',num2str(Jday_dec)]); 
                end
                if Jday_dec > Jday_lims(2),
                    done=1; nrow_file(jfile)=nrow;
                    disp(['Passed Jday_lims(2): Jday = ',num2str(Jday_dec),'; Jday_lims(2) = ',num2str(Jday_lims(2))])
                end
            end
        elseif ~ischar(tline),
            done=1; nrow_file(jfile)=nrow;
            disp(['End of file reached'])
        end
    end
end
%
[npt_max,jfile_max]=max(nrow_file);
Jday_dec_ax=time_ax(1:npt_max,jfile_max)';
%
labels={['Julian Day ',int2str(year0)],  ['Depth - m']};
scales={Jday_dec_ax file_ax(:)'}; dlab='Temperature - deg C'; iidB=0; iifig=0; title=DD{1};
THERM=mat2data(data(1:npt_max,:),labels,scales,dlab,title,iidB,iifig); 
THERM.ii_tight=1;
THERM.title=[out_root,'_',int2str(THERM.scales{1}(1)),'_',int2str(THERM.scales{1}(end)),'_THERM'];
THERM.file_names=cell(nfile,1);
for jfile=1:nfile, THERM.file_names{jfile}=DD{jfile}; end
%
str=['save ',THERM.title,' THERM']; disp(str); eval(str);
view5(THERM);
%
%%%
%
function [Jday_dec,year,tline]=tline_interpret(tline,fid)
%
while length(tline)<25, tline=fgetl(fid); end

year=str2num(tline(1:4)); month=str2num(tline(6:7));
day=str2num(tline(9:10)); hour=str2num(tline(12:13));
minute=str2num(tline(15:16)); sec=str2num(tline(18:19));
Jday=date_to_Jday(month,day,year);
hour=hour + minute/60 + sec/3600;
Jday_dec=Jday + hour/24;
