disp('read_whoi_AI_whots2012.m')
fclose('all');

clear dir
% 
dr=dir(['D:\WHOTS_2012\Hiialakai\Combines_instruments\all_ai.dat']);
e=dr(1).name;

%Date       time  bpr      hrh     atmp    LWR   prlev   SST      SWR  wE      wN    spd   hdg    vane
disp(['Reading whoi file ',e]);
flist=fopen(['D:\WHOTS_2012\Hiialakai\Combines_instruments\all_ai.dat']);
temp=[];AIdata=[];
if flist>0,                                 %if the file exists,
    while ~feof(flist)
%             temp=textscan(flist,'%f %f %f %*c %f %f %f %*c %f %f %*c %f %f %*c %f %f %*c %f %f %*c %f %f %f %f %f  %*[^\n]','delimiter', ' ', 'treatAsEmpty', {'SBE-21','****','FWD','Temp'}, 'headerlines', 1,  'emptyvalue', NaN);
%             temp=textscan(flist,'%4f/%2f/%2f %2f:%2f %f %*c %f %*c %f %*c %f %*3c %f %*c %f %*3c %f %*3c %f %*3c %f %f %*c %f %f %*c %f %*[^\n]','delimiter', ' ', 'headerlines', 1,  'emptyvalue', NaN, 'treatAsEmpty', {'Na','N'});
            temp=textscan(flist,'%4f/%2f/%2f %2f:%2f %f %f %f %f %f %f %f %f %f %f %f %f %*[^\n]','delimiter', ' ','MultipleDelimsAsOne',1, 'headerlines', 1,  'emptyvalue', NaN, 'treatAsEmpty', {'Na','N'});
            AIdata=[AIdata cell2mat(temp)'];        
    end;  %end while
end;      %     end if list
fclose(flist);

jd_ai=datenum(AIdata(1,:),AIdata(2,:),AIdata(3,:),AIdata(4,:),AIdata(5,:),0)-datenum(Vdate(1),1,0);   %Day-Of-Year
llpos=find(jd_ai>=ddd & jd_ai<ddd+1);
jd_ai=jd_ai(llpos);
ai_bpr=AIdata(6,llpos); %BP (mb)
ai_hrh=AIdata(7,llpos); %RH(%)
ai_atmp=AIdata(8,llpos); %air temp (C)
ai_LWR=AIdata(9,llpos);  %IR (W/m2)
% ai_prlev=AIdata(10,llpos); %?
% SST=AIdata(11,llpos); %N/A
ai_SWR=AIdata(12,llpos); %Solar (W/m2)
ai_wE=AIdata(13,llpos); %relative East component
ai_wN=AIdata(14,llpos); %relative North component
% spd=AIdata(15,llpos); %sog from WHOI GPS (apparently crappy as per Chris says)
% hdg=AIdata(16,llpos); %cog from WHOI GPS (apparently crappy as per Chris says)
% vane=AIdata(17,llpos); %indicator

ai_bpri=intavg(jd_ai,ai_bpr,jd_ref);
ai_hrhi=intavg(jd_ai,ai_hrh,jd_ref);
ai_atmpi=intavg(jd_ai,ai_atmp,jd_ref);
ai_LWRi=intavg(jd_ai,ai_LWR,jd_ref);
ai_SWRi=intavg(jd_ai,ai_SWR,jd_ref);
ai_wEi=intavg(jd_ai,ai_wE,jd_ref);
ai_wNi=intavg(jd_ai,ai_wN,jd_ref);

%corrects WHOI wind to true
sogx=gpsspeedi.*cos(gpsheadi*rdcon);
sogy=gpsspeedi.*sin(gpsheadi*rdcon);
% lrgcm=lrgcm';lrgsm=lrgsm';
ai_wN1=ai_wNi.*lrgcm-ai_wEi.*lrgsm;
ai_wE1=ai_wNi.*lrgsm+ai_wEi.*lrgcm;
[lagc,lag,nobs]=lagcov(lrgcm,ai_wEi,100);lag(lagc==min(lagc))
ai_wN1(3:end)=ai_wNi(3:end).*lrgcm(1:end-2)-ai_wEi(3:end).*lrgsm(1:end-2);
ai_wE1(3:end)=ai_wNi(3:end).*lrgsm(1:end-2)+ai_wEi(3:end).*lrgcm(1:end-2);
ai_wN1t=ai_wN1+sogx;
ai_wE1t=ai_wE1+sogy;
ai_wndspdi=sqrt(ai_wN1t.^2+ai_wE1t.^2); %m/s
ai_wnddiri=mod(atan2(-ai_wE1t,-ai_wN1t)*180/pi+360,360); %- sign converst 'to' to 'from' convention

% %test
% e=load([way_proc_data_flux cruise '_proc_son_1min_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd)  '.txt'])';
% psdN=e(2,:);psdW=e(3,:);
% psdN1=psdN.*lrgcm+psdW.*lrgsm;
% psdE1=psdN.*lrgsm-psdW.*lrgcm;
% psdN1t=psdN1+sogx';
% psdE1t=psdE1+sogy';
% psdndspdi=sqrt(psdN1t.^2+psdE1t.^2); %m/s
% psdnddiri=mod(atan2(-psdE1t,-psdN1t)*180/pi+360,360); %- sign converst 'to' to 'from' convention

