function WCs_regular_time_axis( WC_nums , avg_pd_min , flag_v2_std_or_mc  )
%WCs_regular_time_axis Put the WindCube (WC) instruments onto a regular
%predictable time axis with no missing times and NaN for missing data.

% Author: Paul T. Quelet
%
% Last Update Noted: 18 June 2015
%
% Made with Matlab 2014b
% IMPORTANT NOTE: GRAPHICS WILL NOT WORK WITH EARLIER VERSIONS OF CODE
%
% Example function call:
% WCs_regular_time_axis( 2 : 4 , 2 , 'mc' )
%
% =========================================================================
% INPUT                 DESCRIPTION                 TYPE        UNITS
% -------------------------------------------------------------------------
% WC_nums               Wind Cube Numbers           double      [unitless] 1 is 49, 2 is 61, 3 is 68, 4 is 16
% 
% avg_pd_min            Avg. Period of WC STA files integer     [minutes]
%
% flag_v2_std_or_mc 'std' for standard files        string
%                   'mc'  for motion compensated files (normal)
% =========================================================================

%% I. Set Parameters

% Set time difference threshold

num_sec_thresh                          = 5.0 ;

% Fraction of a day of accuracy for datenumber test (~5 seconds):

time_threshold                          = seconds( num_sec_thresh ) ;

% Note the current experiement:

curr_exper_abbr                         = 'XPIA' ;

% Other experiments string abbreviations include the following:
% 'BLADE' , 'NWTC' , 'CWEX11' , 'TWICS' , 'TODS' , 'CWEX13' , 'LUMEX'

%% II. Load relvant data from WindCubes

% Set in directory for WindCube data:

inDir_WC                                = 'E:\XPIA_Research\XPIA_Dataviewer_Matlab_Plot_Codes\WLS7\WC_calc_output\' ;

% Loop over the WCs and load the data:

for j_wc = WC_nums
    
    % Assign information base upon Wind Cube Number:
    
    switch j_wc
        case 1
            % WCnum                       = 49 ;
            % WCnum_str                   = num2str( WCnum ) ;
        case 2
            WCnum                       = 61 ;
            WCnum_str                   = num2str( WCnum ) ;
        case 3
            WCnum                       = 68 ;
            WCnum_str                   = num2str( WCnum ) ;
        case 4
            WCnum                       = 16 ;
            WCnum_str                   = num2str( WCnum ) ;
    end
    
    load( strcat( inDir_WC , curr_exper_abbr , '_' , WCnum_str, '_' , num2str( avg_pd_min ) , 'min_avg_' , flag_v2_std_or_mc , '.mat' ) )
    
    % Get the relevant variables and store from each WindCube
    
    switch j_wc
        case 1
            % dn_49                       = datenum( double( WC_datevec ) ) ;
            % ws_49                       = Vh ;
            % alt_49                      = alt ;
        case 2
            dn_61                       = datenum( double( WC_datevec ) ) ;
            alt_61                      = alt ;
            ws_61                       = Vh ;
            Azim_61                     = Azim ;
            I_61                        = I2 ;
            TKE_61                      = TKE2 ;
            Avail_61                    = Avail ;
        case 3
            dn_68                       = datenum( double( WC_datevec ) ) ;
            alt_68                      = alt ;
            ws_68                       = Vh ;
            Azim_68                     = Azim ;
            I_68                        = I2 ;
            TKE_68                      = TKE2 ;
            Avail_68                    = Avail ;
        case 4
            dn_16                       = datenum( double( WC_datevec ) ) ;
            alt_16                      = alt ;
            ws_16                       = Vh ;
            Azim_16                     = Azim ;
            I_16                        = I2 ;
            TKE_16                      = TKE2 ;
            Avail_16                    = Avail ;
    end
    
end % (j_wc) End loop over WindCube Numbers

clearvars -except dn_* alt_* ws_* Azim_* I_* TKE_* Avail_* curr_exper_abbr WC_nums avg_pd_min time_threshold flag_v2_std_or_mc

%% III. Set the starting / ending dates of the timeseries (user input)

% Set the times in between to examine:

master_datetime_start                   = datetime( 2015, 05 , 24 , 00 , 00 , 00 ) ; % yyyy , mm , dd , hh , mm , ss
master_datetime_end                     = datetime( 2015, 06 , 01 , 00 , 00 , 00 ) ; % yyyy , mm , dd , hh , mm , ss

%% IV. Extract the dates relevant for the case study:

for j_wc = WC_nums
    switch j_wc
        case 1
            display( 'No WC49') ;
        case 2
            % First convert into the new datetime format:
            datetime_61                 = datetime( dn_61 , 'ConvertFrom' , 'datenum' ) ;
            % Use the new "isbetween" function to get new true/false vectors to plug into the wind speed arrays:
            time_range_inds_61          = isbetween( datetime_61 , master_datetime_start , master_datetime_end ) ;
            % Get the smaller datetime arrays:
            datetime_61                 = datetime_61( time_range_inds_61 ) ;
            % Get the smaller matrices for the specified dates:
            ws_61                       = ws_61( time_range_inds_61 , : ) ;
            Azim_61                     = Azim_61( time_range_inds_61 , : ) ;
            I_61                        = I_61( time_range_inds_61 , : ) ;
            TKE_61                      = TKE_61( time_range_inds_61 , : ) ;
            Avail_61                    = Avail_61( time_range_inds_61 , : ) ;
        case 3
            datetime_68                 = datetime( dn_68 , 'ConvertFrom' , 'datenum' ) ;
            time_range_inds_68          = isbetween( datetime_68 , master_datetime_start , master_datetime_end ) ;
            datetime_68                 = datetime_68( time_range_inds_68 ) ;
            ws_68                       = ws_68( time_range_inds_68 , : ) ;
            Azim_68                     = Azim_68( time_range_inds_68 , : ) ;
            I_68                        = I_68( time_range_inds_68 , : ) ;
            TKE_68                      = TKE_68( time_range_inds_68 , : ) ;
            Avail_68                    = Avail_68( time_range_inds_68 , : ) ;
        case 4
            datetime_16                 = datetime( dn_16 , 'ConvertFrom' , 'datenum' ) ;
            time_range_inds_16          = isbetween( datetime_16 , master_datetime_start , master_datetime_end ) ;
            datetime_16                 = datetime_16( time_range_inds_16 ) ;
            ws_16                       = ws_16( time_range_inds_16 , : ) ;
            Azim_16                     = Azim_16( time_range_inds_16 , : ) ;
            I_16                        = I_16( time_range_inds_16 , : ) ;
            TKE_16                      = TKE_16( time_range_inds_16 , : ) ;
            Avail_16                    = Avail_16( time_range_inds_16 , : ) ;
    end % End switch statement over WindCube number (j_wc)
end % End loop over WindCube numbers (j_wc)

%% V. Setup the Master Time Axis:

% Setup a master time axis with equal avg_pd_min strides from the beginning to end date times:
regTA                                   = ( master_datetime_start : minutes( avg_pd_min ) : master_datetime_end )' ;

% Construct the number of WC instruments more columns of the same length as the regular time axis, and start filled with NaN:
masterTime                              = NaN( length( regTA ) , length( WC_nums ) ) ;

%% VI. Insert Data Into Master Timeseries

for j_wc = WC_nums
    
    % Assign information base upon Wind Cube Number:
    
    switch j_wc
        case 1
            % WCnum                   = 49 ;
            % WCnum_str               = num2str( WCnum ) ;
            % curr_datetime           = datetime_49 ;
            % num_heights_49          = 10 ;
        case 2
            WCnum                       = 61 ;
            WCnum_str                   = num2str( WCnum ) ;
            curr_datetime               = datetime_61 ;
            num_heights_61              = 10 ;
        case 3
            WCnum                       = 68 ;
            WCnum_str                   = num2str( WCnum ) ;
            curr_datetime               = datetime_68 ;
            num_heights_68              = 10 ;
        case 4
            WCnum                       = 16 ;
            WCnum_str                   = num2str( WCnum ) ;
            curr_datetime               = datetime_16 ;
            num_heights_16              = 11 ;
    end
    
    %% VII. Set time period of WC operation to find out when the are operational
    
    for i_time = 1 : length( regTA )
        
        diff_inds = find( abs( regTA( i_time ) - curr_datetime ) < time_threshold ) ; % Test if this is nonzero with find()
        
        % if exist( 'diff_inds' , 'var' ) == 1
        if diff_inds >= 1
            masterTime( i_time , j_wc - 1 ) = 1 ; % Note the time in the WC time row as a one (for "good")
        end
        
    end % End test for loop over length of time
    
    % Assign information base upon Wind Cube Number:
    
    switch j_wc
        case 1
            disp( 'Not able to transfer data right now...' ) ;
        case 2
            %% VIII. Create full length variables for WC61
            
            % Create the sizes of the matrices (total master times x number of measurement heights):
            
            ws_regTA_61                 = NaN( length( regTA ) , num_heights_61 ) ;
            wdir_regTA_61               = NaN( length( regTA ) , num_heights_61 ) ;
            I_regTA_61                  = NaN( length( regTA ) , num_heights_61 ) ;
            TKE_regTA_61                = NaN( length( regTA ) , num_heights_61 ) ;
            Avail_regTA_61              = NaN( length( regTA ) , num_heights_61 ) ;
            
            % Initialize the data time to zero:
            
            c_data_time                 = 0 ;
            
            % Loop through al the master times of the axis:
            
            for i_time = 1 : length( regTA )
                
                % Check if it is a "good" time for the column of this WindCube:
                
                if masterTime( i_time , 1 ) == 1 % Column 1 is the appropriate column for these times
                    
                    % If a good time, advance the time counter of the data:
                    
                    c_data_time         = c_data_time + 1 ;
                    
                    % Set the data values matrix to the appropriate slot of the master time:
                    
                    ws_regTA_61(    i_time , 1 : num_heights_61 ) = ws_61(    c_data_time , : ) ;
                    wdir_regTA_61(  i_time , 1 : num_heights_61 ) = Azim_61(  c_data_time , : ) ;
                    I_regTA_61(     i_time , 1 : num_heights_61 ) = I_61(     c_data_time , : ) ; 
                    TKE_regTA_61(   i_time , 1 : num_heights_61 ) = TKE_61(   c_data_time , : ) ; 
                    Avail_regTA_61( i_time , 1 : num_heights_61 ) = Avail_61( c_data_time , : ) ;
                    
                end % End check if good master time
                
            end % End loop over all the master times
            
        case 3
            
            %%  IX. Create full length variables for WC68
            
            ws_regTA_68                 = NaN( length( regTA ) , num_heights_68 ) ;
            wdir_regTA_68               = NaN( length( regTA ) , num_heights_68 ) ;
            I_regTA_68                  = NaN( length( regTA ) , num_heights_68 ) ;
            TKE_regTA_68                = NaN( length( regTA ) , num_heights_68 ) ;
            Avail_regTA_68              = NaN( length( regTA ) , num_heights_68 ) ;
            
            % Initialize the data time to zero:
            
            c_data_time                 = 0 ;
            
            % Loop through al the master times of the axis:
            
            for i_time = 1 : length( regTA )
                
                % Check if it is a "good" time for the column of this WindCube:
                
                if     length( WC_nums )  > 1
                    col_choice_68 = 2 ;
                elseif length( WC_nums ) == 1
                    col_choice_68 = 1 ;
                end
                
                if masterTime( i_time , col_choice_68 ) == 1 % Column 2 is the appropriate column for these times
                    
                    % If a good time, advance the time counter of the data:
                    
                    c_data_time         = c_data_time + 1 ;
                    
                    % Set the data values matrix to the appropriate slot of the master time:
                    
                    ws_regTA_68(    i_time , 1 : num_heights_68 ) = ws_68(    c_data_time , : ) ;
                    wdir_regTA_68(  i_time , 1 : num_heights_68 ) = Azim_68(  c_data_time , : ) ;
                    I_regTA_68(     i_time , 1 : num_heights_68 ) = I_68(     c_data_time , : ) ;
                    TKE_regTA_68(   i_time , 1 : num_heights_68 ) = TKE_68(   c_data_time , : ) ; 
                    Avail_regTA_68( i_time , 1 : num_heights_68 ) = Avail_68( c_data_time , : ) ;
                    
                end % End check if good master time
                
            end % End loop over all the master times
            
            
        case 4
            
            %%  X. Create full length variables for WC16
            
            ws_regTA_16                 = NaN( length( regTA ) , num_heights_16 ) ;
            wdir_regTA_16               = NaN( length( regTA ) , num_heights_16 ) ;
            I_regTA_16                  = NaN( length( regTA ) , num_heights_16 ) ;
            TKE_regTA_16                = NaN( length( regTA ) , num_heights_16 ) ;
            Avail_regTA_16              = NaN( length( regTA ) , num_heights_16 ) ;
            
            % Initialize the data time to zero:
            
            c_data_time                 = 0 ;
            
            % Loop through al the master times of the axis:
            
            for i_time = 1 : length( regTA )
                
                % Check if it is a "good" time for the column of this WindCube:
                
                if masterTime( i_time , end ) == 1 % Column 3 is the appropriate column for these times
                    
                    % If a good time, advance the time counter of the data:
                    
                    c_data_time         = c_data_time + 1 ;
                    
                    % Set the data values matrix to the appropriate slot of the master time:
                    
                    ws_regTA_16(    i_time , 1 : num_heights_16 ) = ws_16(    c_data_time , : ) ;
                    wdir_regTA_16(  i_time , 1 : num_heights_16 ) = Azim_16(  c_data_time , : ) ;
                    I_regTA_16(     i_time , 1 : num_heights_16 ) = I_16(     c_data_time , : ) ; 
                    TKE_regTA_16(   i_time , 1 : num_heights_16 ) = TKE_16(   c_data_time , : ) ; 
                    Avail_regTA_16( i_time , 1 : num_heights_16 ) = Avail_16( c_data_time , : ) ;
                    
                end % End check if good master time
                
            end % End loop over all the master times
            
    end % End switch statement choice of WC numbers
    
    %% XI. Save Data to File
        
    % Set the output directory for the saved data:
    
    outDir                              = 'E:\XPIA_Research\XPIA_Dataviewer_Matlab_Plot_Codes\WLS7\WC_regTA_output\' ; 
    
    % Set the output file name:
    
    save_file_name_str = strcat( curr_exper_abbr, '_' , WCnum_str , '_' , num2str( avg_pd_min ) , 'min_' , flag_v2_std_or_mc , '_regTA.mat' ) ;
    
    % Save the appropriate variables depending upon the WC device number:
    
    switch j_wc
        case 1
            save( strcat( outDir , save_file_name_str ) , 'regTA' , 'ws_regTA_49' , 'wdir_regTA_49' , 'I_regTA_49' , 'TKE_regTA_49' , 'Avail_regTA_49' , 'alt_49' ) ;
        case 2
            save( strcat( outDir , save_file_name_str ) , 'regTA' , 'ws_regTA_61' , 'wdir_regTA_61' , 'I_regTA_61' , 'TKE_regTA_61' , 'Avail_regTA_61' , 'alt_61' ) ;
        case 3
            save( strcat( outDir , save_file_name_str ) , 'regTA' , 'ws_regTA_68' , 'wdir_regTA_68' , 'I_regTA_68' , 'TKE_regTA_68' , 'Avail_regTA_68' , 'alt_68' ) ;
        case 4
            save( strcat( outDir , save_file_name_str ) , 'regTA' , 'ws_regTA_16' , 'wdir_regTA_16' , 'I_regTA_16' , 'TKE_regTA_16' , 'Avail_regTA_16' , 'alt_16' ) ;
    end % End switch statement over WC numbers (j_wc)
    
end % End loop over the Wind Cube Numbers

end % End WCs_regular_time_axis() function