disp('ftp to NOAA server in Boulder v2015')
% ftp zipped files to voodoo/incoming and optionally send a confirmation email
%Notes: PSD ftp server times out after 30s or 60s of inactivities"...

clear dir    %to make sure we don't keep the Wind direction variable called also 'dir'!
clear tpsd   %clear it just in case

%compress selected files into a big zip file
jd=sprintf('%03i',ddd);
[m,d]=yd2md(str2num(yyyy), ddd);
zipfilename=[cruise, '_', ship, '_', 'flux', '_', sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd)];
% zipfilename=[cruise, '_', yyyy,'_',sprintf('%02i_%02i',m,d), '_', ship, '_', 'flux',  '_', jd];
if exist(fullfile(path_raw_data,[yyyy(3:4),jd],['FM',yyyy(3:4),jd,'.log']),'file')
    entrynames = zip(fullfile(path_raw_data,[zipfilename,'.zip']),{fullfile(path_proc_data,['*', jd, '.txt']),fullfile(path_raw_images,['*',jd,graphformat]),fullfile(path_raw_data,[yyyy(3:4),jd],['*',yyyy(3:4),jd,'.log'])});
else
    entrynames = zip(fullfile(path_raw_data,[zipfilename,'.zip']),{[path_proc_data, '*', jd, '.txt'],fullfile(path_raw_images,['*',jd,graphformat])});
end
%Note: if it takes too long to ftp the data from the ship, we could just zip
%the txt file as the compressed size is ~6 times smaller than when done
%with png files.  Option could be to zip  the txt files and send them(~<200 Kb), and
%then send th png files. Maybe this could avoid any possible timeout
%issues?

MEftp=[];   %Empty Message Error ftp
ns=120;     %number of seconds to pause before retrying ftp (is increasing later on in the code)
dn=0;       %counter for number of entries/trials
nemax=9;    %max number of entries/trials desired
statusflag=0;



while statusflag==0
    if dn==nemax    %after trying nemax ftp, abort
        break
    end

    try
        %Starts connection to ftp server
        tpsd = ftp('140.172.38.114','psdguest','psdguest');   %connects to ftp1.esrl.noaa.gov with user name and password.
        pasv(tpsd)  %uses passive option of ftp code from file exchange
        binary(tpsd);  %set FTP transfer type to binary (already binary by default, but just in case...)
        cd(tpsd,'psd3/')              %change from incoming directory on FTP server to psd3
        mkdir(tpsd,'whots_2015/');     %creates lbariteau folder in case it does not exist. If it already exists, does nothing.
        cd(tpsd,'whots_2015')     %change current directory on FTP server to psd3/lbariteau

        %now check if zip files on local drive are already present on the ftp server. If so, deletes them to be able to resend them
        dirlocal=dir(fullfile(path_raw_data,'*.zip'));  %list directory contents of way_proc_data_flux folder (zip file only)
        dirftp=dir(tpsd,'*.zip');                       %list directory contents of ftp directory  (zip file only)
        if ~isempty(dirftp)  && ~isempty(dirlocal)
            ii=find(ismember(char(dirftp(:).name),char(dirlocal(:).name),'rows'));  %check if file names on ftp are on the way_proc_data_flux directory, i.e. check
            % the files that might be already uploaded on the server
            if ii
                for ll=1:length(ii)
                    delete(tpsd,dirftp(ii(ll)).name);      %remove the zip files already present. If not done, mput function will crash.
                end;
            end;
        end;

        %starts process of sending zip file one by one (if more than one)
        for nf=1:length(dirlocal)    %send every zip files (should be 1 in normal situations)
            %Upload file one at a time
            mput(tpsd,fullfile(path_raw_data, dirlocal(nf).name));
            dir(tpsd,'*')
            dirftp=dir(tpsd,'*.zip');
%             if ~isempty(dirftp)  && ~isempty(dirlocal)
%                 ii=find(ismember(char(dirlocal(nf).name),char(dirftp(:).name),'rows'));  %check if file has been uploaded to server
%                 if ii  %if so delete file on local drive
%                     delete([way_proc_data_flux, dirlocal(nf).name]);
%                 end
%             end;
        end;

        statusflag=1;       %'success';
        close(tpsd);        %close connection to FTP server
        clear tpsd          %deletes FTP object

    catch MEftp
        if ismember({'tpsd'},who)   %if connection was made, close coonection
            close(tpsd);            %close connection to FTP server
            clear tpsd              %deletes FTP object
        end
        disp([MEftp.identifier, sprintf(' . Will retry ftp in %4.0f minutes...', (ns+ns*dn*4)/60)])
        pause(ns+ns*dn*4);           %pause program for ns seconds
        %Note: With ns=120s (2min) and nemax=9, the pause will be 120s at 1st
        %iteration, then 10min (120+120*1*4=600s), then 18min, ... til 58min. The total
        %cumulated pause time is thus 4h.
        dn=dn+1;
        statusflag=0;       %'failed';
        continue     %Pass control to the next iteration of WHILE loop when error, ie retry to send failed file
    end;   %end try/catch

end;  %end while
