function get_Cadiz_tidaldata_sv
%%
%Get Cadiz Tidal gauge data%
% Haris Plomaritis, University of Cadiz
% version 1.0 05/06/2010

clear all
clc

disp(' ');
disp('READING TIDAL DATA FROM: http://indamar.ieo.es/');
disp('READING CADIZ TIDAL DATA...');
disp('Haris Plomaritis, University of Cadiz')
disp('Department of Earth Sciences');
disp('version 1.1 05/06/2010')


[buoy,status] = urlread('http://indamar.ieo.es/mareas/realdata/realcado.txt');

[DATES, datain] = strread(buoy,'%24q %5.3f','headerlines',2,'delimiter','\n');
DATES_WEB = cell2mat(DATES);

%%
YY = str2num(DATES_WEB(:,3:6));
MM = str2num(DATES_WEB(:,8:9));
DD = str2num(DATES_WEB(:,11:12));
HH = str2num(DATES_WEB(:,14:15));
MN = str2num(DATES_WEB(:,17:18));
SS = str2num(DATES_WEB(:,20:21));

DATE = [YY MM DD HH MN SS];
data = [DATE datain];
[j,k] = size(data);

%% 
% FIND AND FILL THE GAPS

[data]=fix_tidaldata(data,3/12);

%%  
% READ DATA FROM FILE 

 A = importdata('Cadiz_SL_cor.txt',' ',0);

DATES_F= char(A.textdata(:,2));
HOURS_F= char(A.textdata(:,3));
% datain = A.data;
YY_F = str2num(DATES_F(end,1:4));
MM_F = str2num(DATES_F(end,6:7));
DD_F = str2num(DATES_F(end,9:10));
HH_F = str2num(HOURS_F(end,1:2));
MN_F = str2num(HOURS_F(end,4:5));
SS_F = str2num(HOURS_F(end,7:8));

DATE_F = [YY_F MM_F DD_F HH_F MN_F SS_F];
dnum_F = datenum(DATE_F);
disp(' ');
disp(' ');
disp(['FIRST DATA ON LINE:      ' datestr(datenum(DATE(1,:)))])
disp(' ');
disp(['LAST DATA ON LINE :      ' datestr(datenum(DATE(end,:)))])
disp(' ');
disp(['LAST DATA ON FILE :      ' datestr(dnum_F)])
%%
% COMPARE THE DATES ON THE WEB WITH THE 
dnum_web = datenum(data(:,1:6));

if dnum_web(end) <= dnum_F
    disp(' ');
    disp('NO NEW DATA TO BE DOWNLOADED. SORRY FOR ANY INCONVENIENCE CAUSED!!! ')
else
    tol=0.0001;
    ind_str = find(dnum_web-dnum_F <= tol);
    ind_str = max(ind_str)+1; % to stear from the next time-step
    if numel(ind_str)~=0;
       data_new = data(ind_str:end,:);
       disp(' ');
       disp(['FIRST NEW DATA ON FILE:  ' datestr(datenum(data_new(1,1:6)))])
    else 
        step_norm=double(vpa(1/12,16));
        step_d=double(vpa(step_norm/24,12));
        gap_time = datevecfix([dnum_F(end)+step_d:step_d:dnum_web(1)-step_d]); 
        gap_length = length(gap_time);  % THERE IS A GAP BETWEEN THE DATA ON THE WEB AND THE DATA ON THE FILE 
        gap_fill   = [gap_time NaN*ones(gap_length,1)];
        data_new = [gap_fill; data];
        disp(' ');
        disp(['FIRST NEW DATA ON FILE:      ' datestr(datenum(data_new(1,1:6)))])
    end
   % MAKE TEXT AS IN FILE
    indnan = find(isnan(data_new(:,7)));       % find Nans
    if numel(indnan)~=0;
        data_new(indnan,7)=-9999;
    else
    end

    DATE_ALL = datestr(data_new(:,1:6),31);
    [len,lon] = size(DATE_ALL);
    for i=1:len
        DATE_T(i,:) = ['8 ' DATE_ALL(i,:) '   '];
    end

    tide_data = [DATE_T num2str(data_new(:,7),'%5.3f')];
    
    
%     fid = fopen('C:\Documents and Settings\H\Desktop\Cadiz_SLtest1.txt','at');
%     [len,lon] = size(tide_data);
%     for i=1:len
%         fprintf(fid,'\n       %29s', tide_data(i,:));
%     end

    %fprintf('\n')
 %   fclose(fid);
    fid = fopen('Cadiz_SL_cor.txt','at');
    [len,lon] = size(tide_data);
    for i=1:len
        fprintf(fid,'\n       %29s', tide_data(i,:));
    end

    %fprintf('\n')
    fclose(fid);
end
disp(' ')
disp(' ')
disp(' ')
disp('EVERYTHING OK ?')
disp(' ')
disp('PRESS ANY KEY TO TERMINATE !!!')

pause


             