function [ alt , data , textData ] = importFile( file , flag_sta_or_rtd , ver_flag )
%
% function [alt,data,textData] = importFile(file,flag)
%
% M  = Number of times
% N  = Number of altitudes
% 57 = Default number of header lines at the top of a separate WC file:  ;
%
% =========================================================================
% importFile imports Windcube data from the specified "sta" or "rtd" file.
% =========================================================================
% INPUT             DESCRIPTION                  TYPE               UNITS
% -------------------------------------------------------------------------
% file              full path & full file name   string
% flag_sta_or_rtd   'sta' for sta file           string
%                   'rtd' for rtd file
% =========================================================================
% OUTPUT      DESCRIPTION                  TYPE                     UNITS
% -------------------------------------------------------------------------
% alt         altitudes                    [1xN]                    [m]
% data        lidar data (see Windcube     [M x 19N+1]    for 'sta'
%             user manual for more info)   [M x 8N]       for 'rtd'
% textData    header and timestamps        [M+57 x 19N+2] for 'sta'
%                                          [M+57 x 8N+4]  for 'rtd'
% =========================================================================

%% Specify the default number of header lines in the WC files

if     strcmp( ver_flag , 'v1' ) == 1 % WLS7v1 systems
    num_header_lines                        = 57 ;
elseif strcmp( ver_flag , 'v2' ) == 1 % WLS866v2 system
    num_header_lines                        = 41 ;
end

% import and store altitude data
newData1                                    = importdata( file ) ;
alt                                         = newData1.data( 1 , : ) ; % Extract the first row to get all the altitudes
alt                                         = alt' ; % Transpose to column vector of altitudes

% import the rest of the file: (\t for tab delimited, and skip the number of header lines)
newData2                                = importdata( file , '\t' , num_header_lines ) ;

%% Test if the imported data is a structure (has data from importdata() or similar):

if isstruct(newData2) == 1
    
    % store lidar data and get rid of bad entries
    data = newData2.data ;
    
    switch flag_sta_or_rtd
        case 'sta'
            data( data <= -500000 | data >= 50000 ) = NaN ;
            
            % store text data (headers and timestamps)
            textData                        = newData2.textdata ;
            
        case 'rtd'
            data( data <= -999 )            = NaN ;
            
            % store text data (headers and timestamps)
            textData = newData2.textdata ;
            
        otherwise
            error( 'flag_sta_or_rtd invalid file readin in importFile.m' )
            
    end
    
else % Not a structure, file empty of data, only has header
    
    data                                    = [] ;
    textData                                = {} ;
    
end % End of structure if statement

end % End of the importFile() function
