%%
% From Matthieu Boquet, mboquet@leosphere.com, 2 July 2015
%

function DATA=readrtd_WLS_V2(filepath)

% Lecture d'un seul fichier rtd

if nargin==0

        [flpth,pth]=uigetfile('*.rtd');

    filepath=[pth,flpth];

end

 

PARAMS=rtd_parser_WLS_V2(filepath);

data=txt2mat(filepath,PARAMS.HeaderSize+2,5+max(PARAMS.i_W),'InfoLevel',0,'ReplaceChar',{'V1','/ ',': '});

 

%%

Year=data(:,1);

Month=data(:,2);

Day=data(:,3);

Hour=data(:,4);

Minute=data(:,5);

Second=data(:,6);

DATA.Azi=data(:,5+PARAMS.i_Azi);

DATA.Azi(DATA.Azi==1)=-1;

DATA.Ele=(90-PARAMS.ScanAngle)+0*DATA.Azi;

DATA.Ele(DATA.Azi==-1)=90;

try

    DATA.Alpha=data(:,5+PARAMS.i_Alpha);

    DATA.Beta=data(:,5+PARAMS.i_Beta);

    DATA.Gamma=data(:,5+PARAMS.i_Gamma);

end

 

 

DATA.CNR=data(:,5+PARAMS.i_CNR);

DATA.Vr=data(:,5+PARAMS.i_Vr);

DATA.Disp=data(:,5+PARAMS.i_Disp);

DATA.Vh=data(:,5+PARAMS.i_Vh);

DATA.Dir=data(:,5+PARAMS.i_Dir);

DATA.U=data(:,5+PARAMS.i_U);

DATA.V=data(:,5+PARAMS.i_V);

DATA.W=data(:,5+PARAMS.i_W);

 

DATA.time=datenum(Year,Month,Day, Hour, Minute, Second);

DATA.Altitude=PARAMS.Range;

DATA.Range=PARAMS.Range;

% DATA.Range=DATA.Altitude/cosd(PARAMS.ScanAngle);

DATA.RTD=PARAMS;

if isfield(PARAMS,'CNR_Threshold');

    DATA.IC=0*DATA.Vr;

    DATA.IC(DATA.CNR>=PARAMS.CNR_Threshold)=100;

end

DATA.Type_Scenario='DBS';

DATA.Origine='rtd';

 

if exist([filepath 'w'],'file');

    dataw=txt2mat([filepath 'w'],'InfoLevel',0,'ReplaceChar',{'V1','/ ',': '});

    DATAW.time=datenum(dataw(:,1:6));

    DATAW.Vz=dataw(:,7:end);

    timez=0.5*(DATA.time(find(diff(DATA.Azi)==-270))+DATA.time(1+find(diff(DATA.Azi)==-270)));

    Vz=interp1(DATAW.time,DATAW.Vz,timez,'nearest','extrap');

    DATA.time=[DATA.time ; timez];

    [DATA.time,rang]=sort(DATA.time);

    DATA.Vr=[DATA.Vr;Vz];

    DATA.Vr=DATA.Vr(rang,:);

    DATA.CNR=[DATA.CNR;0.5*(DATA.CNR(find(diff(DATA.Azi)==-270),:)+DATA.CNR(1+find(diff(DATA.Azi)==-270),:))];

    DATA.CNR=DATA.CNR(rang,:);

       

    DATA.Disp=[DATA.Disp;0.5*(DATA.Disp(find(diff(DATA.Azi)==-270),:)+DATA.Disp(1+find(diff(DATA.Azi)==-270),:))];

    DATA.Disp=DATA.Disp(rang,:);

   

    DATA.IC=[DATA.IC;0.5*(DATA.IC(find(diff(DATA.Azi)==-270),:)+DATA.IC(1+find(diff(DATA.Azi)==-270),:))];

    DATA.IC=DATA.IC(rang,:);

   

    DATA.Vh=[DATA.Vh;0.5*(DATA.Vh(find(diff(DATA.Azi)==-270),:)+DATA.Vh(1+find(diff(DATA.Azi)==-270),:))];

    DATA.Vh=DATA.Vh(rang,:);

   

    DATA.Dir=[DATA.Dir;0.5*(DATA.Dir(find(diff(DATA.Azi)==-270),:)+DATA.Dir(1+find(diff(DATA.Azi)==-270),:))];

    DATA.Dir=DATA.Dir(rang,:);

   

    DATA.U=[DATA.U;0.5*(DATA.U(find(diff(DATA.Azi)==-270),:)+DATA.U(1+find(diff(DATA.Azi)==-270),:))];

    DATA.U=DATA.U(rang,:);

   

    DATA.V=[DATA.V;0.5*(DATA.V(find(diff(DATA.Azi)==-270),:)+DATA.V(1+find(diff(DATA.Azi)==-270),:))];

    DATA.V=DATA.V(rang,:);

   

    

    DATA.W=[DATA.W;Vz];

    DATA.W=DATA.W(rang,:);

   

    DATA.Azi=[DATA.Azi;-1+0*Vz(:,1)];DATA.Azi=DATA.Azi(rang);

    DATA.Ele=[DATA.Ele;90+0*Vz(:,1)];DATA.Ele=DATA.Ele(rang);

end

 

 

 

function INFO = rtd_parser_WLS_V2(filepath)

 

if nargin==0

    [filename, pathname] = uigetfile('*.rtd','Config_AP files','MultiSelect', 'off');

    filepath=[pathname filename];

elseif exist(filepath,'file')~=2

    [filename, pathname] = uigetfile([filepath '*.rtd'],'Config_AP files','MultiSelect', 'off');

    filepath=[pathname filename];

 

end

 

 

 

file_2_parse = fileread(filepath);

file_2_parse = file_2_parse(1:10000);

 

exp = 'HeaderSize=(.)*?\n';

txt = regexp(file_2_parse, exp, 'tokens');

txt=txt{1};

INFO.HeaderSize = str2num(txt{:});

 

 

exp = 'ID System=(.)*?\n';

txt = regexp(file_2_parse, exp, 'tokens');

txt=txt{1};

INFO.ID_System = txt{:};

 

exp = 'ID Client=(.)*?\n';

txt = regexp(file_2_parse, exp, 'tokens');

txt=txt{1};

INFO.ID_Client = txt{:};

 

exp = 'Location=(.)*?\n';

txt = regexp(file_2_parse, exp, 'tokens');

txt=txt{1};

INFO.Localisation = txt{:};

try

    exp = 'GPS Location=(.)*?N (.)*?E';

    txt = regexp(file_2_parse, exp, 'tokens');

    LongLat=str2double(txt{:});

    INFO.Latitude = LongLat(1);

    INFO.Longitude=LongLat(2);

catch

    INFO.Latitude = NaN;

    INFO.Longitude=NaN;

end

% ********************

% Scenario Parameters:

% ********************

 

% ********************

% Acquisition Parameters:

% ********************

try

    exp = 'Pulses / Line of Sight=(.)*?\n';

    txt = regexp(file_2_parse, exp, 'tokens');

    INFO.Pulses_per_LOS = str2double(txt{:});

catch

    reply = input('Nombres de Pulses /LOS non trouvé, entrez le Nomvre de pluses ', 's');

    INFO.Pulses_per_LOS=str2num(reply);

end

 

    exp = 'FFT Window Width=(.)*?\n';

    txt = regexp(file_2_parse, exp, 'tokens');

    INFO.NFFT = str2double(txt{:});

   

    

    exp = 'Pulse Repetition Rate \(Hz\)=(.)*?\n';

    txt = regexp(file_2_parse, exp, 'tokens');

    INFO.PRF = str2double(txt{:});

   

    exp = 'Pulse Duration \(s\)=(.)*?\n';

    txt = regexp(file_2_parse, exp, 'tokens');

    INFO.PulseMode = str2double(txt{:})/1e-9;

   

    

    exp = 'ScanAngle \(°\)=(.)*?\n';

    txt = regexp(file_2_parse, exp, 'tokens');

    INFO.ScanAngle = str2double(txt{:});

   

    exp = 'DirectionOffset \(°\)=(.)*?\n';

    txt = regexp(file_2_parse, exp, 'tokens');

    INFO.DirectionOffset = str2double(txt{:});

   

    

 

 

try

     exp = 'Wavelength \(nm\)=(.)*?\n';

    txt = regexp(file_2_parse, exp, 'tokens');

    INFO.Wavelength= str2double(txt{:})*1e-9;

catch

  INFO.Wavelength= 1.55*1e-6; 

end

 

try

    exp = 'CNRThreshold=(.)*?\n';

    txt = regexp(file_2_parse, exp, 'tokens');

    txt=txt{1};

    INFO.CNR_Threshold = str2double(txt{:});

catch

%     reply = input('Entrez le CNR Threshold ', 's');

%     INFO.CNR_Threshold=str2num(reply);

end

   

exp = 'Altitudes \(m\)=\t(.)*?\n';

txt = regexp(file_2_parse, exp, 'tokens');

altitudes=regexp(txt{:},'\t','split');

INFO.Range = str2double(altitudes{:});

 

 

exp = '[\n]Timestamp(.)*[\n]*';

txt = regexp(file_2_parse, exp,'match');

txt=txt{:};

txts=regexp(txt,'\t','split');

txts(strcmp('',txts))=[];

Matches=regexpi(txts,'Position');

INFO.i_Azi=find(~cellfun(@isempty,Matches));

Matches=regexpi(txts,'CNR|Carrier');

INFO.i_CNR=find(~cellfun(@isempty,Matches));

Matches=regexpi(txts,'Radial Wind Speed \(m/s\)');

INFO.i_Vr=find(~cellfun(@isempty,Matches));

Matches=regexpi(txts,'Radial Wind Speed Dispersion \(m/s\)');

INFO.i_Disp=find(~cellfun(@isempty,Matches));

 

Matches=regexpi(txts,'m Wind Speed \(m/s\)');

INFO.i_Vh=find(~cellfun(@isempty,Matches));

 

Matches=regexpi(txts,'Wind Direction \(°\)');

INFO.i_Dir=find(~cellfun(@isempty,Matches));

 

Matches=regexpi(txts,'X-wind \(m/s\)');

INFO.i_U=find(~cellfun(@isempty,Matches));

Matches=regexpi(txts,'Y-wind \(m/s\)');

INFO.i_V=find(~cellfun(@isempty,Matches));

Matches=regexpi(txts,'Z-wind \(m/s\)');

INFO.i_W=find(~cellfun(@isempty,Matches));

try

    Matches=regexpi(txts,'Alpha|Roll');

    INFO.i_Alpha=find(~cellfun(@isempty,Matches));

   

    Matches=regexpi(txts,'Beta|Pitch');

    INFO.i_Beta=find(~cellfun(@isempty,Matches));

   

    Matches=regexpi(txts,'Gamma|Yaw');

    INFO.i_Gamma=find(~cellfun(@isempty,Matches));

end

 

 function out=fileread(filename)

%FILEREAD Return contents of file as string vector.

%   TEXT = FILEREAD('FILENAME') returns the contents of the file FILENAME as a

%   MATLAB string.

%

%   See also FREAD, TEXTSCAN, LOAD, WEB.

 

% Copyright 1984-2011 The MathWorks, Inc.

% $Revision: 1.5.4.10 $  $Date: 2011/09/03 22:42:50 $

 

% Validate input args

narginchk(1, 1);

 

% get filename

if ~ischar(filename),

    error(message('MATLAB:fileread:filenameNotString'));

end

 

% do some validation

if isempty(filename),

    error(message('MATLAB:fileread:emptyFilename'));

end

 

% open the file

[fid, msg] = fopen(filename);

if fid == (-1)

    error(message('MATLAB:fileread:cannotOpenFile', filename, msg));

end

 

try

    % read file

    out = fread(fid,'*char')';

catch exception

    % close file

    fclose(fid);

    throw(exception);

end

 

% close file

fclose(fid);

