close('all');
fclose('all');
clear;

datasetv='_v2';
read_parameters_HiWinGS_2013;

expp='HiWinGS_2013';
drive='C:\data\CRUISES\HiWinGS_2013\Knorr\flux\Raw';
% drive_w='C:\Data\CRUISES\DYNAMO_2011\flux_data';  
drive_w='C:\data\CRUISES\HiWinGS_2013\flux_data';  

graphformat='.png';  %select graphics format files
graphdevice='-dpng'; %select graphic device


clearit=1;
printit=1;
year='2013';

yyyy='2013';

decorr=0;  %to decorrelate winds from motion...
dcname=[];
if decorr==1
    dcname='_vdecorr';
end;

rge=283:300; 
for ddd=rge
    [m,d]=yd2md(str2num(year), ddd);
    Vdate=[str2num(year) m d];
       
    for hhh=00:23;   %
      read_hp_HiWinGS_2013;      %
      read_shp_HiWinGS_2013;     %
      read_motion_HiWinGS_2013;  %
      read_son_HiWinGS_2013;     %
      read_gps_HiWinGS_2013;     %

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      
      %uses the SCS heading as slightly better
%       if ddd+hhh/24>=(298+15/24) & ddd+hhh/24<=(300+16/24)
          jd_mot=ddd+(hhh+tmotion/3600)/24;
          lrgc=cos(lrg'*rdcon);        %convert to N
          lrgs=sin(lrg'*rdcon);        %convert to E
            lrgcm=interp1(jd_scs(~isnan(jd_scs)),lrgc(~isnan(jd_scs)),jd_mot); 
            lrgsm=interp1(jd_scs(~isnan(jd_scs)),lrgs(~isnan(jd_scs)),jd_mot); 
          heading=mod(atan2(lrgsm,lrgcm)/rdcon+360,360);         %laser ring gyro
%       else
%           read_head_pitch_HiWinGS_2013; % 
%           heading=headx_pitch;
%       end
      
      ii=(find(isnan(heading)));
      try
        heading(find(isnan(heading)))=heading(find(isnan(heading))+1);
        heading(find(isnan(heading)))=heading(find(isnan(heading))+1);
      catch
        if ii(1) == 1        
            ll=1;
            while   ii(1)==1
                heading(1)=heading(ii(ll)+1);
                ii=(find(isnan(heading)));
                ll=ll+1;
            end
        end    
        if ii(end) == 36000 & ~isempty(ii)
            ll=0;
            while   ii(end) == 36000
                heading(end)=heading(ii(end-ll)-1);
                ii=(find(isnan(heading)));
                if isempty(ii);    break;  end;                 
                ll=ll+1;
            end
        end  
        ii=(find(isnan(heading)));
        for ll=1:length(ii)
            heading(ii(ll))=heading(ii(ll)-1);  
        end    
%           disp('use meddian')        
      end
      shead=sin(heading./180.*pi);     %can use also unwrap function to avoid heading problem between 360 and 0 deg.
      chead=cos(heading./180.*pi);

heading=heading/180*pi;                     %convert to radians

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

      licor_prefix='lic';
      read_co2_HiWinGS_2013;    %
      
%       %gets the surface current speed and direction from ADCP
%       ii=find(MMF(:,1)>=ddd+hhh/24-1/60/24 & MMF(:,1)<ddd+(hhh+1)/24);
%       Cspd=MMF(ii,7); %Current speed (m/s)
% %       Cdir=MMF(ii,8)+180; %Current direction (deg). +180 is to convert 'from' to 'to'
%        Cdir=MMF(ii,8); %Current direction (deg) 'from' -->needed that way in nau_correct.!
     
      nau_correct_HiWinGS_2013;
      nau_process_HiWinGS_2013;  

      try
        UH_process;
      catch
        continue
      end
      
      close all;
    end;
end;

rge=301; 
for ddd=rge
    [m,d]=yd2md(str2num(year), ddd);
    Vdate=[str2num(year) m d];
       
    for hhh=00:21;   %
      read_hp_HiWinGS_2013;      %
      read_shp_HiWinGS_2013;     %
      read_motion_HiWinGS_2013;  %
      read_son_HiWinGS_2013;     %
      read_gps_HiWinGS_2013;     %

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      
      %uses the SCS heading as slightly better
%       if ddd+hhh/24>=(298+15/24) & ddd+hhh/24<=(300+16/24)
          jd_mot=ddd+(hhh+tmotion/3600)/24;
          lrgc=cos(lrg'*rdcon);        %convert to N
          lrgs=sin(lrg'*rdcon);        %convert to E
            lrgcm=interp1(jd_scs(~isnan(jd_scs)),lrgc(~isnan(jd_scs)),jd_mot); 
            lrgsm=interp1(jd_scs(~isnan(jd_scs)),lrgs(~isnan(jd_scs)),jd_mot); 
          heading=mod(atan2(lrgsm,lrgcm)/rdcon+360,360);         %laser ring gyro
%       else
%           read_head_pitch_HiWinGS_2013; % 
%           heading=headx_pitch;
%       end
      
      ii=(find(isnan(heading)));
      try
        heading(find(isnan(heading)))=heading(find(isnan(heading))+1);
        heading(find(isnan(heading)))=heading(find(isnan(heading))+1);
      catch
        if ii(1) == 1        
            ll=1;
            while   ii(1)==1
                heading(1)=heading(ii(ll)+1);
                ii=(find(isnan(heading)));
                ll=ll+1;
            end
        end    
        if ii(end) == 36000 & ~isempty(ii)
            ll=0;
            while   ii(end) == 36000
                heading(end)=heading(ii(end-ll)-1);
                ii=(find(isnan(heading)));
                if isempty(ii);    break;  end;                 
                ll=ll+1;
            end
        end  
        ii=(find(isnan(heading)));
        for ll=1:length(ii)
            heading(ii(ll))=heading(ii(ll)-1);  
        end    
%           disp('use meddian')        
      end
      shead=sin(heading./180.*pi);     %can use also unwrap function to avoid heading problem between 360 and 0 deg.
      chead=cos(heading./180.*pi);

heading=heading/180*pi;                     %convert to radians

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

      licor_prefix='lic';
      read_co2_HiWinGS_2013;    %
      
%       %gets the surface current speed and direction from ADCP
%       ii=find(MMF(:,1)>=ddd+hhh/24-1/60/24 & MMF(:,1)<ddd+(hhh+1)/24);
%       Cspd=MMF(ii,7); %Current speed (m/s)
% %       Cdir=MMF(ii,8)+180; %Current direction (deg). +180 is to convert 'from' to 'to'
%        Cdir=MMF(ii,8); %Current direction (deg) 'from' -->needed that way in nau_correct.!
     
      nau_correct_HiWinGS_2013;
      nau_process_HiWinGS_2013;  

      try
        UH_process;
      catch
        continue
      end
      
      close all;
    end;
end;

%30122 crash of some instruments
% after that period MP completely out --> uses UH_mp to correct PSD sonic.
% and look at lag diff on W
%Doy 302 to 308 no PSD sonic and MP
% sonic & TRH back on 30918
% no lsr either after
rge=302; 
for ddd=rge
    [m,d]=yd2md(str2num(year), ddd);
    Vdate=[str2num(year) m d];
       
    for hhh=10:23;   %
      read_hp_HiWinGS_2013;      %
      read_shp_HiWinGS_2013;     %
      read_motion_HiWinGS_2013;  %
      read_son_HiWinGS_2013;     %
      read_gps_HiWinGS_2013;     %

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      
      %uses the SCS heading as slightly better
%       if ddd+hhh/24>=(298+15/24) & ddd+hhh/24<=(300+16/24)
          jd_mot=ddd+(hhh+tmotion/3600)/24;
          lrgc=cos(lrg'*rdcon);        %convert to N
          lrgs=sin(lrg'*rdcon);        %convert to E
            lrgcm=interp1(jd_scs(~isnan(jd_scs)),lrgc(~isnan(jd_scs)),jd_mot); 
            lrgsm=interp1(jd_scs(~isnan(jd_scs)),lrgs(~isnan(jd_scs)),jd_mot); 
          heading=mod(atan2(lrgsm,lrgcm)/rdcon+360,360);         %laser ring gyro
%       else
%           read_head_pitch_HiWinGS_2013; % 
%           heading=headx_pitch;
%       end
      
      ii=(find(isnan(heading)));
      try
        heading(find(isnan(heading)))=heading(find(isnan(heading))+1);
        heading(find(isnan(heading)))=heading(find(isnan(heading))+1);
      catch
        if ii(1) == 1        
            ll=1;
            while   ii(1)==1
                heading(1)=heading(ii(ll)+1);
                ii=(find(isnan(heading)));
                ll=ll+1;
            end
        end    
        if ii(end) == 36000 & ~isempty(ii)
            ll=0;
            while   ii(end) == 36000
                heading(end)=heading(ii(end-ll)-1);
                ii=(find(isnan(heading)));
                if isempty(ii);    break;  end;                 
                ll=ll+1;
            end
        end  
        ii=(find(isnan(heading)));
        for ll=1:length(ii)
            heading(ii(ll))=heading(ii(ll)-1);  
        end    
%           disp('use meddian')        
      end
      shead=sin(heading./180.*pi);     %can use also unwrap function to avoid heading problem between 360 and 0 deg.
      chead=cos(heading./180.*pi);

heading=heading/180*pi;                     %convert to radians

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

      licor_prefix='lic';
      read_co2_HiWinGS_2013;    %
      
%       %gets the surface current speed and direction from ADCP
%       ii=find(MMF(:,1)>=ddd+hhh/24-1/60/24 & MMF(:,1)<ddd+(hhh+1)/24);
%       Cspd=MMF(ii,7); %Current speed (m/s)
% %       Cdir=MMF(ii,8)+180; %Current direction (deg). +180 is to convert 'from' to 'to'
%        Cdir=MMF(ii,8); %Current direction (deg) 'from' -->needed that way in nau_correct.!
     
      nau_correct_HiWinGS_2013;
      nau_process_HiWinGS_2013;  

      try
        UH_process;
      catch
        continue
      end
      
      close all;
    end;
end;

%30122 crash of some instruments
% after that period MP completely out --> uses UH_mp to correct PSD sonic.
% and look at lag diff on W
%Doy 302 to 308 no PSD sonic and MP
% sonic 7 TRH back on 30918
% no lsr either after

%UH 305/07 has a glitch
rge=303:308; 
for ddd=rge
    [m,d]=yd2md(str2num(year), ddd);
    Vdate=[str2num(year) m d];
       
    for hhh=00:11;   %
      read_hp_HiWinGS_2013;      %
      read_shp_HiWinGS_2013;     %
      read_motion_HiWinGS_2013;  %
      read_son_HiWinGS_2013;     %
      read_gps_HiWinGS_2013;     %

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      
      %uses the SCS heading as slightly better
%       if ddd+hhh/24>=(298+15/24) & ddd+hhh/24<=(300+16/24)
      if length(lrg(isnan(lrg)))~=360
          jd_mot=ddd+(hhh+tmotion/3600)/24;
          lrgc=cos(lrg'*rdcon);        %convert to N
          lrgs=sin(lrg'*rdcon);        %convert to E
            lrgcm=interp1(jd_scs(~isnan(jd_scs)),lrgc(~isnan(jd_scs)),jd_mot); 
            lrgsm=interp1(jd_scs(~isnan(jd_scs)),lrgs(~isnan(jd_scs)),jd_mot); 
          heading=mod(atan2(lrgsm,lrgcm)/rdcon+360,360);         %laser ring gyro
      else
          read_head_pitch_HiWinGS_2013; % 
          heading=headx_pitch;
      end
      
      ii=(find(isnan(heading)));
      try
        heading(find(isnan(heading)))=heading(find(isnan(heading))+1);
        heading(find(isnan(heading)))=heading(find(isnan(heading))+1);
      catch
        if ii(1) == 1        
            ll=1;
            while   ii(1)==1
                heading(1)=heading(ii(ll)+1);
                ii=(find(isnan(heading)));
                ll=ll+1;
            end
        end    
        if ii(end) == 36000 & ~isempty(ii)
            ll=0;
            while   ii(end) == 36000
                heading(end)=heading(ii(end-ll)-1);
                ii=(find(isnan(heading)));
                if isempty(ii);    break;  end;                 
                ll=ll+1;
            end
        end  
        ii=(find(isnan(heading)));
        for ll=1:length(ii)
            heading(ii(ll))=heading(ii(ll)-1);  
        end    
%           disp('use meddian')        
      end
      shead=sin(heading./180.*pi);     %can use also unwrap function to avoid heading problem between 360 and 0 deg.
      chead=cos(heading./180.*pi);

heading=heading/180*pi;                     %convert to radians

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

      licor_prefix='lic';
      read_co2_HiWinGS_2013;    %
      
%       %gets the surface current speed and direction from ADCP
%       ii=find(MMF(:,1)>=ddd+hhh/24-1/60/24 & MMF(:,1)<ddd+(hhh+1)/24);
%       Cspd=MMF(ii,7); %Current speed (m/s)
% %       Cdir=MMF(ii,8)+180; %Current direction (deg). +180 is to convert 'from' to 'to'
%        Cdir=MMF(ii,8); %Current direction (deg) 'from' -->needed that way in nau_correct.!
     
      nau_correct_HiWinGS_2013;
      nau_process_HiWinGS_2013;  

%       try
        UH_process;
%       catch
%         continue
%       end
      
      close all;
    end;
end;


%30122 crash of some instruments
% after that period MP completely out --> uses UH_mp to correct PSD sonic.
% and look at lag diff on W
%Doy 302 to 308 no PSD sonic and MP
% sonic 7 TRH back on 30918
% no lsr either after
rge=309:318; 
for ddd=rge
    [m,d]=yd2md(str2num(year), ddd);
    Vdate=[str2num(year) m d];
       
    for hhh=00:23;   %
      read_hp_HiWinGS_2013;      %
      read_shp_HiWinGS_2013;     %
      read_motion_HiWinGS_2013;  %
      read_son_HiWinGS_2013;     %
      read_gps_HiWinGS_2013;     %

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      
      %uses the SCS heading as slightly better
%       if ddd+hhh/24>=(298+15/24) & ddd+hhh/24<=(300+16/24)
          jd_mot=ddd+(hhh+tmotion/3600)/24;
          lrgc=cos(lrg'*rdcon);        %convert to N
          lrgs=sin(lrg'*rdcon);        %convert to E
            lrgcm=interp1(jd_scs(~isnan(jd_scs)),lrgc(~isnan(jd_scs)),jd_mot); 
            lrgsm=interp1(jd_scs(~isnan(jd_scs)),lrgs(~isnan(jd_scs)),jd_mot); 
          heading=mod(atan2(lrgsm,lrgcm)/rdcon+360,360);         %laser ring gyro
%       else
%           read_head_pitch_HiWinGS_2013; % 
%           heading=headx_pitch;
%       end
      
      ii=(find(isnan(heading)));
      try
        heading(find(isnan(heading)))=heading(find(isnan(heading))+1);
        heading(find(isnan(heading)))=heading(find(isnan(heading))+1);
      catch
        if ii(1) == 1        
            ll=1;
            while   ii(1)==1
                heading(1)=heading(ii(ll)+1);
                ii=(find(isnan(heading)));
                ll=ll+1;
            end
        end    
        if ii(end) == 36000 & ~isempty(ii)
            ll=0;
            while   ii(end) == 36000
                heading(end)=heading(ii(end-ll)-1);
                ii=(find(isnan(heading)));
                if isempty(ii);    break;  end;                 
                ll=ll+1;
            end
        end  
        ii=(find(isnan(heading)));
        for ll=1:length(ii)
            heading(ii(ll))=heading(ii(ll)-1);  
        end    
%           disp('use meddian')        
      end
      shead=sin(heading./180.*pi);     %can use also unwrap function to avoid heading problem between 360 and 0 deg.
      chead=cos(heading./180.*pi);

heading=heading/180*pi;                     %convert to radians

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

      
      try
        UH_process;
      catch
        continue
      end
%-------------------------------------------------------------------
%uses UH MP to correct PSD sonic
    accx=(AccX')/0.94992;
    accy=(AccY')/0.91555;
    accz=(AccZ')/0.94844;
    ratex=(RateX')/1;
    ratey=(RateY')/1;
    ratez=(RateZ')/1;
%-------------------------------------------------------------------
   
      licor_prefix='lic';
      read_co2_HiWinGS_2013;    %
      
%       %gets the surface current speed and direction from ADCP
%       ii=find(MMF(:,1)>=ddd+hhh/24-1/60/24 & MMF(:,1)<ddd+(hhh+1)/24);
%       Cspd=MMF(ii,7); %Current speed (m/s)
% %       Cdir=MMF(ii,8)+180; %Current direction (deg). +180 is to convert 'from' to 'to'
%        Cdir=MMF(ii,8); %Current direction (deg) 'from' -->needed that way in nau_correct.!
     
      nau_correct_HiWinGS_2013;
      nau_process_HiWinGS_2013;  

      

    if hhh==12 | hhh==00
        figure(7);hold on;semilogx(Fson_UH(1,:),Fson_UH(1,:).*nanmean(Cuw_UH),'k-')
        if printit==1
             print(graphdevice,[drive_w '\Cospectra_' num2str(ddd) num2str(hhh) graphformat]);
        end;

        figure(12);hold on;loglog(Fson_UH(1,:),Fson_UH(1,:).*nanmean(SW_UH),'k-',Fson_UH(1,:),Fson_UH(1,:).*nanmean(SU_UH),'m-')
        if printit==1
            print(graphdevice,[drive_w '\Psd_v2_' num2str(ddd) num2str(hhh) graphformat]);
        end;   

    end;

      close all;
    end;
end;



for ddd=283:317
    jd=sprintf('%04i',ddd);
    filecat([drive_w '\procturb\da_' jd '*' dcname '.prn'],[drive_w '\procturb\' cruise '_da' jd dcname '.txt']);
    filecat([drive_w '\procturb\cr_' jd '*' dcname '.prn'],[drive_w '\procturb\' cruise '_cr' jd dcname '.txt']);
    filecat([drive_w '\procturb\co_' jd '*' dcname '.prn'],[drive_w '\procturb\' cruise '_co' jd dcname '.txt']);
    filecat([drive_w '\procturb\sp_' jd '*' dcname '.prn'],[drive_w '\procturb\' cruise '_sp' jd dcname '.txt']);
end
filecat([drive_w '\procturb\' cruise '_da*' dcname '.txt'],[drive_w '\procturb\' cruise '_da' datasetv dcname '.prn']);
filecat([drive_w '\procturb\' cruise '_cr*' dcname '.txt'],[drive_w '\procturb\' cruise '_cr' datasetv dcname '.prn']);
filecat([drive_w '\procturb\' cruise '_co*' dcname '.txt'],[drive_w '\procturb\' cruise '_co' datasetv dcname '.prn']);
filecat([drive_w '\procturb\' cruise '_sp*' dcname '.txt'],[drive_w '\procturb\' cruise '_sp' datasetv dcname '.prn']);


for ddd=283:317
    jd=sprintf('%03i',ddd);
    filecat([drive_w '\procturb\da_UH' jd '*' dcname '.prn'],[drive_w '\procturb\' cruise '_daUH' jd dcname '.txt']);
    filecat([drive_w '\procturb\sp_UH' jd '*' dcname '.prn'],[drive_w '\procturb\' cruise '_spUH' jd dcname '.txt']);
end
filecat([drive_w '\procturb\UH\' cruise '_daUH*' dcname '.txt'],[drive_w '\procturb\UH\' cruise '_daUH' datasetv dcname '.prn']);
filecat([drive_w '\procturb\UH\' cruise '_spUH*' dcname '.txt'],[drive_w '\procturb\UH\' cruise '_spUH' datasetv dcname '.prn']);