%------ run_HiWinGS_2013_v091514.m

%	Runs flux calculations for HiWinGS
%   Based on LB 062014 scripts but edited & commented by BWB
%   uses modified read_gps for reprocessed raw files, much faster

%   NOTE: RAW TRH AND SON DATA FILES MUST BE CLEANED UP BEFORE THESE
%   SCRIPTS WILL RUN.  SEE COMMENTS IN read_son AND read_tairh SCRIPTS.

%% initialize
close all;
fclose all;
clear;

datasetv='v3';
read_parameters_HiWinGS_2013;   % set paths and other run variables

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

clearit=1;
printit=1;
yyyy='2013';

decorr=1;  %to decorrelate winds from motion...
dcname='';
if decorr==1
    dcname='vdecorr';
end;

%% load 1 minute final met data for entire cruise
A =importdata('/Volumes/User/bblomquist/Documents/Cruises/HiWinGS_2013/MeanNBulkfluxes/HiWinGS_2013_1minMeans_r1.txt');
A = A.data;
yday = A(:,1);	% decimal DOY

% arrays to keep track of bad sonic data
badSonPnts = [];
missingPnts = [];
jday_badPnts = [];

%% DOY 283 to 300
rge=283:300;
for ddd=rge
    [m,d]=yd2md(str2num(yyyy), ddd);
    Vdate=[str2num(yyyy) m d];
    
    for hhh=00:23;
		read_hp_HiWinGS_2013;               % Read slow met file
		read_shp_HiWinGS_2013;              % Read SCS file
		read_motion_HiWinGS_2013;           % Read motion pak file
		read_son_HiWinGS_2013;              % Read sonic file
		read_gps_HiWinGS_2013_bwb;          % Read Hemisphere gps file
        
        % save date/time & # bad pnts from read_son
        badSonPnts = [badSonPnts; badPnts'];
        missingPnts = [missingPnts; missing'];
        jday_badPnts = [jday_badPnts; badTime'];
        clear badPnts missing badTime;
        
        % Get best heading data and convert to radians...  
        % (using the SCS heading as slightly better)
        rdcon = pi/180;
        tref = 0:1/10:35999/10;               % 10 Hz for 1 hr
        jd_mot = ddd+(hhh+tref/3600)/24;      % as decimal DOY
        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
        
        % Screen for NaNs
        ii = find(isnan(heading));    % indices of NaNs
        if ~isempty(ii)
            jj = (ii<300);       % NaNs in the first 300 points
            kk = (ii>36000-300); % NaNs in the last 300 points
            ll = ~xor(jj,kk);   % all others
            % Replace NaN with median of +/- 20 point range around NaN
            % Or, median of first 300 points if near the ends
            % Could expand range of points if long sequences of NaN are an issue
            heading(ii(jj)) = nanmedian1(heading(1:ii(jj)+300));
            heading(ii(ll)) = nanmedian1(heading(ii(ll)-20:ii(ll)+20));
            heading(ii(kk)) = nanmedian1(heading(ii(kk)-300:end));
        end;

		shead = sin(heading./180.*pi);
    	chead = cos(heading./180.*pi);
		heading = heading/180*pi;       % convert to radians
        
		% Read licor file
		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
            close all;  % dummy operation, continue
        end;
        
 		close all;
    end;
end;

%% DOY 301 Hr 0-21
rge=301; 
for ddd=rge
	[m,d]=yd2md(str2num(yyyy), ddd);
    Vdate=[str2num(yyyy) 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_bwb;
        
        % save date/time & # bad pnts from read_son
        badSonPnts = [badSonPnts; badPnts'];
        missingPnts = [missingPnts; missing'];
        jday_badPnts = [jday_badPnts; badTime'];
        clear badPnts missing badTime;

        % Get best heading data and convert to radians...  
        % (using the SCS heading as slightly better)
        rdcon = pi/180;
        tref = 0:1/10:35999/10;               % 10 Hz for 1 hr
        jd_mot = ddd+(hhh+tref/3600)/24;      % as decimal DOY
        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
        
        % Screen for NaNs
        ii = find(isnan(heading));    % indices of NaNs
        if ~isempty(ii)
            jj = (ii<300);       % NaNs in the first 300 points
            kk = (ii>36000-300); % NaNs in the last 300 points
            ll = ~xor(jj,kk);   % all others
            % Replace NaN with median of +/- 20 point range around NaN
            % Or, median of first 300 points if near the ends
            % Could expand range of points if long sequences of NaN are an issue
            heading(ii(jj)) = nanmedian1(heading(1:ii(jj)+300));
            heading(ii(ll)) = nanmedian1(heading(ii(ll)-20:ii(ll)+20));
            heading(ii(kk)) = nanmedian1(heading(ii(kk)-300:end));
        end;

		shead = sin(heading./180.*pi);
    	chead = cos(heading./180.*pi);
		heading = heading/180*pi;       % convert to radians
        
%		Read licor file
    	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
            close all;  % dummy operation, continue
        end;

    	close all;
    end;
end;

%% 30122 crash of some instruments
% After that period MP completely out

% For DOY 302, 1000 - 2300 partial day
rge=302; 
for ddd=rge
	[m,d]=yd2md(str2num(yyyy), ddd);
    Vdate=[str2num(yyyy) 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_bwb;
        
        % save date/time & # bad pnts from read_son
        badSonPnts = [badSonPnts; badPnts'];
        missingPnts = [missingPnts; missing'];
        jday_badPnts = [jday_badPnts; badTime'];
        clear badPnts missing badTime;

        % Get best heading data and convert to radians...  
        % (using the SCS heading as slightly better)
        rdcon = pi/180;
        tref = 0:1/10:35999/10;               % 10 Hz for 1 hr
        jd_mot = ddd+(hhh+tref/3600)/24;      % as decimal DOY
        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
        
        % Screen for NaNs
        ii = find(isnan(heading));    % indices of NaNs
        if ~isempty(ii)
            jj = (ii<300);       % NaNs in the first 300 points
            kk = (ii>36000-300); % NaNs in the last 300 points
            ll = ~xor(jj,kk);   % all others
            % Replace NaN with median of +/- 20 point range around NaN
            % Or, median of first 300 points if near the ends
            % Could expand range of points if long sequences of NaN are an issue
            heading(ii(jj)) = nanmedian1(heading(1:ii(jj)+300));
            heading(ii(ll)) = nanmedian1(heading(ii(ll)-20:ii(ll)+20));
            heading(ii(kk)) = nanmedian1(heading(ii(kk)-300:end));
        end;

		shead = sin(heading./180.*pi);
    	chead = cos(heading./180.*pi);
		heading = heading/180*pi;       % convert to radians
        
%		Read licor file
    	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
            close all;  % dummy operation, continue
        end;

    	close all;
    end;
end;

%% For DOY 303 to 316
% DOY 302 to 308, no PSD sonic and MP
%   For this period UH process is only data
% No lsr either after 30122
% Sonic & TRH back on 30918
%   Uses UH_mp to correct PSD sonic and look at lag diff on W

rge=303:305; 
for ddd=rge
    [m,d]=yd2md(str2num(yyyy), ddd);
    Vdate=[str2num(yyyy) 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_bwb;
        
        % save date/time & # bad pnts from read_son
        badSonPnts = [badSonPnts; badPnts'];
        missingPnts = [missingPnts; missing'];
        jday_badPnts = [jday_badPnts; badTime'];
        clear badPnts missing badTime;

        % Get best heading data and convert to radians...  
        % (using the SCS heading as slightly better)
        rdcon = pi/180;
        tref = 0:1/10:35999/10;               % 10 Hz for 1 hr
        jd_mot = ddd+(hhh+tref/3600)/24;      % as decimal DOY
        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
        
        % Screen for NaNs
        ii = find(isnan(heading));    % indices of NaNs
        if ~isempty(ii)
            jj = (ii<300);       % NaNs in the first 300 points
            kk = (ii>36000-300); % NaNs in the last 300 points
            ll = ~xor(jj,kk);   % all others
            % Replace NaN with median of +/- 20 point range around NaN
            % Or, median of first 300 points if near the ends
            % Could expand range of points if long sequences of NaN are an issue
            heading(ii(jj)) = nanmedian1(heading(1:ii(jj)+300));
            heading(ii(ll)) = nanmedian1(heading(ii(ll)-20:ii(ll)+20));
            heading(ii(kk)) = nanmedian1(heading(ii(kk)-300:end));
        end;

		shead = sin(heading./180.*pi);
    	chead = cos(heading./180.*pi);
		heading = heading/180*pi;       % convert to radians
        
        % Call UH process first, since UH motion data will be used for PSD
        % flux calculations after 309 1800
        
        try
            UH_process;
        catch
            close all;  % dummy operation, continue
        end;
        
        % use UH motionPak 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;

%		Read licor file
		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;
        
        close all;
    end;
end;

%% For DOY 306 0000 to 1300  (no scs for 1400-1600)
rge=306; 
for ddd=rge
    [m,d]=yd2md(str2num(yyyy), ddd);
    Vdate=[str2num(yyyy) m d];
       
    for hhh=00:13;
		read_hp_HiWinGS_2013;
		read_shp_HiWinGS_2013;
        read_motion_HiWinGS_2013;
		read_son_HiWinGS_2013;
		read_gps_HiWinGS_2013_bwb;
        
        % save date/time & # bad pnts from read_son
        badSonPnts = [badSonPnts; badPnts'];
        missingPnts = [missingPnts; missing'];
        jday_badPnts = [jday_badPnts; badTime'];
        clear badPnts missing badTime;

        % Get best heading data and convert to radians...  
        % (using the SCS heading as slightly better)
        rdcon = pi/180;
        tref = 0:1/10:35999/10;               % 10 Hz for 1 hr
        jd_mot = ddd+(hhh+tref/3600)/24;      % as decimal DOY
        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
        
        % Screen for NaNs
        ii = find(isnan(heading));    % indices of NaNs
        if ~isempty(ii)
            jj = (ii<300);       % NaNs in the first 300 points
            kk = (ii>36000-300); % NaNs in the last 300 points
            ll = ~xor(jj,kk);   % all others
            % Replace NaN with median of +/- 20 point range around NaN
            % Or, median of first 300 points if near the ends
            % Could expand range of points if long sequences of NaN are an issue
            heading(ii(jj)) = nanmedian1(heading(1:ii(jj)+300));
            heading(ii(ll)) = nanmedian1(heading(ii(ll)-20:ii(ll)+20));
            heading(ii(kk)) = nanmedian1(heading(ii(kk)-300:end));
        end;

		shead = sin(heading./180.*pi);
    	chead = cos(heading./180.*pi);
		heading = heading/180*pi;       % convert to radians
        
        % Call UH process first, since UH motion data will be used for PSD
        % flux calculations after 309 1800
        
        try
            UH_process;
        catch
            close all;  % dummy operation, continue
        end;
        
        % use UH motionPak 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;

%		Read licor file
		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;
        
        close all;
    end;
end;

%% For DOY 306 1700 to 2300
rge=306; 
for ddd=rge
    [m,d]=yd2md(str2num(yyyy), ddd);
    Vdate=[str2num(yyyy) m d];
       
    for hhh=17:23;
		read_hp_HiWinGS_2013;
		read_shp_HiWinGS_2013;
        read_motion_HiWinGS_2013;
		read_son_HiWinGS_2013;
		read_gps_HiWinGS_2013_bwb;
        
        % save date/time & # bad pnts from read_son
        badSonPnts = [badSonPnts; badPnts'];
        missingPnts = [missingPnts; missing'];
        jday_badPnts = [jday_badPnts; badTime'];
        clear badPnts missing badTime;

        % Get best heading data and convert to radians...  
        % (using the SCS heading as slightly better)
        rdcon = pi/180;
        tref = 0:1/10:35999/10;               % 10 Hz for 1 hr
        jd_mot = ddd+(hhh+tref/3600)/24;      % as decimal DOY
        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
        
        % Screen for NaNs
        ii = find(isnan(heading));    % indices of NaNs
        if ~isempty(ii)
            jj = (ii<300);       % NaNs in the first 300 points
            kk = (ii>36000-300); % NaNs in the last 300 points
            ll = ~xor(jj,kk);   % all others
            % Replace NaN with median of +/- 20 point range around NaN
            % Or, median of first 300 points if near the ends
            % Could expand range of points if long sequences of NaN are an issue
            heading(ii(jj)) = nanmedian1(heading(1:ii(jj)+300));
            heading(ii(ll)) = nanmedian1(heading(ii(ll)-20:ii(ll)+20));
            heading(ii(kk)) = nanmedian1(heading(ii(kk)-300:end));
        end;

		shead = sin(heading./180.*pi);
    	chead = cos(heading./180.*pi);
		heading = heading/180*pi;       % convert to radians
        
        % Call UH process first, since UH motion data will be used for PSD
        % flux calculations after 309 1800
        
        try
            UH_process;
        catch
            close all;  % dummy operation, continue
        end;
        
        % use UH motionPak 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;

%		Read licor file
		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;
        
        close all;
    end;
end;

%% For DOY 307 to 316
rge=307:316; 
for ddd=rge
    [m,d]=yd2md(str2num(yyyy), ddd);
    Vdate=[str2num(yyyy) 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_bwb;
        
        % save date/time & # bad pnts from read_son
        badSonPnts = [badSonPnts; badPnts'];
        missingPnts = [missingPnts; missing'];
        jday_badPnts = [jday_badPnts; badTime'];
        clear badPnts missing badTime;

        % Get best heading data and convert to radians...  
        % (using the SCS heading as slightly better)
        rdcon = pi/180;
        tref = 0:1/10:35999/10;               % 10 Hz for 1 hr
        jd_mot = ddd+(hhh+tref/3600)/24;      % as decimal DOY
        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
        
        % Screen for NaNs
        ii = find(isnan(heading));    % indices of NaNs
        if ~isempty(ii)
            jj = (ii<300);       % NaNs in the first 300 points
            kk = (ii>36000-300); % NaNs in the last 300 points
            ll = ~xor(jj,kk);   % all others
            % Replace NaN with median of +/- 20 point range around NaN
            % Or, median of first 300 points if near the ends
            % Could expand range of points if long sequences of NaN are an issue
            heading(ii(jj)) = nanmedian1(heading(1:ii(jj)+300));
            heading(ii(ll)) = nanmedian1(heading(ii(ll)-20:ii(ll)+20));
            heading(ii(kk)) = nanmedian1(heading(ii(kk)-300:end));
        end;

		shead = sin(heading./180.*pi);
    	chead = cos(heading./180.*pi);
		heading = heading/180*pi;       % convert to radians
        
        % Call UH process first, since UH motion data will be used for PSD
        % flux calculations after 309 1800
        
        try
            UH_process;
        catch
            close all;  % dummy operation, continue
        end;
        
        % use UH motionPak 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;

%		Read licor file
		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;
        
        close all;
    end;
end;

%% concatenate hourly files  da_v3_295_14.txt

for ddd=283:316
    jd=sprintf('%03i',ddd);
    files = dir([drive_root,'procturb/da/da_',datasetv,'_',jd,'*',dcname,'.txt']);
    N = length(struct2cell(files));
    if N > 0
        filecat([drive_root,'procturb/da/da_',datasetv,'_',jd,'*',dcname,'.txt'],[drive_root,'procturb/daily/',cruise,'_da_',datasetv,'_',jd,'_',dcname,'.txt']);
        filecat([drive_root,'procturb/cr/cr_',datasetv,'_',jd,'*',dcname,'.txt'],[drive_root,'procturb/daily/',cruise,'_cr_',datasetv,'_',jd,'_',dcname,'.txt']);
        filecat([drive_root,'procturb/co/co_',datasetv,'_',jd,'*',dcname,'.txt'],[drive_root,'procturb/daily/',cruise,'_co_',datasetv,'_',jd,'_',dcname,'.txt']);
        filecat([drive_root,'procturb/sp/sp_',datasetv,'_',jd,'*',dcname,'.txt'],[drive_root,'procturb/daily/',cruise,'_sp_',datasetv,'_',jd,'_',dcname,'.txt']);
    end;
    clear files N
end;
filecat([drive_root,'procturb/daily/',cruise,'_da_',datasetv,'_*_',dcname,'.txt'],[drive_root,'procturb/total/',cruise,'_da_',datasetv,'_',dcname,'.txt']);
filecat([drive_root,'procturb/daily/',cruise,'_cr_',datasetv,'_*_',dcname,'.txt'],[drive_root,'procturb/total/',cruise,'_cr_',datasetv,'_',dcname,'.txt']);
filecat([drive_root,'procturb/daily/',cruise,'_co_',datasetv,'_*_',dcname,'.txt'],[drive_root,'procturb/total/',cruise,'_co_',datasetv,'_',dcname,'.txt']);
filecat([drive_root,'procturb/daily/',cruise,'_sp_',datasetv,'_*_',dcname,'.txt'],[drive_root,'procturb/total/',cruise,'_sp_',datasetv,'_',dcname,'.txt']);


for ddd=283:316
    jd=sprintf('%03i',ddd);
    files = dir([drive_root,'procturb/da/da_UH_',datasetv,'_',jd,'*',dcname,'.txt']);
    N = length(struct2cell(files));
    if N > 0
        filecat([drive_root,'procturb/da/da_UH_',datasetv,'_',jd,'*',dcname,'.txt'],[drive_root,'procturb/daily/',cruise,'_da_UH_',datasetv,'_',jd,'_',dcname,'.txt']);
        filecat([drive_root,'procturb/sp/sp_UH_',datasetv,'_',jd,'*',dcname,'.txt'],[drive_root,'procturb/daily/',cruise,'_sp_UH_',datasetv,'_',jd,'_',dcname,'.txt']);
    end;
    clear files N
end;
filecat([drive_root,'procturb/daily/',cruise,'_da_UH_',datasetv,'_*_',dcname,'.txt'],[drive_root,'procturb/total/',cruise,'_da_UH_',datasetv,'_',dcname,'.txt']);
filecat([drive_root,'procturb/daily/',cruise,'_sp_UH_',datasetv,'_*_',dcname,'.txt'],[drive_root,'procturb/total/',cruise,'_sp_UH_',datasetv,'_',dcname,'.txt']);

