function [varargout]=list_surveys(varargin)

% function to show all the surveys that are in the netcdf and match the
% surveypairs
% syntax:  list_surveys                             to get the table
% or  Surveys=list_surveys                     No table, but a structure % with the indices of the survey pairs (matching surveys) and jetski and walking surveys

if nargout == 0
scrnout=1 ; % 1 shows the table with surveys in the command window 
else
    scrnout=0 ;
end


%% Load data
% locate vlugtenburg directory on this computer, so you can call this function in all subdirectories of vlugtenburg
curdir=pwd;
ind_v=findstr(curdir, 'vlugtenburg');
pathvlugtenburg=(curdir(1:ind_v+10));  % gives back someting like F:\check_outs\tudelft\vlugtenburg\

NetCDFfilename=[pathvlugtenburg '\vlugtenburg.nc'];
time = nc_varget(NetCDFfilename,'time',[0 0],[-1 -1]);
surveytype = nc_varget(NetCDFfilename,'survey_type',[0 0],[-1 -1]);

%%
m_time = datenum(time);

%% Matt 12/5/2010
% find indices walking or jetski 
indjet=[]; indwalk=[];
for idate=1:length(m_time)
        if surveytype(idate,1) == 'J'
            indjet=[indjet idate];    %vector containing all indices of jetski surveys
        else 
             indwalk=[indwalk idate]; %vector containing all indices of walking surveys
        end
end

% find survey pairs (walking and jetski within 7 days of eachother)
survey_pairs=[]; onlyjet=[];
for idate=1:length(m_time)
    if surveytype(idate,1) == 'J'
        ind = find (abs(m_time(indwalk,:) - m_time(idate,:)) < 7);   % find the walking survey within 7 days of the jetski survey
        if length(ind) == 1
            survey_pairs=[survey_pairs  ;  indwalk(ind) idate];  %% Matrix with all survey pairs first is the walking one, second is the jetski
        else
            onlyjet =[onlyjet ; idate];  %% vector with jetski surveys without walking,, not really nessecary
        end
    end
end

[m_time_sort,ind_sort] = sort(m_time);   %% MAKE A VECTOR WITH THE INDEXES TO CHRONOLOGICAL ORDER  

if scrnout == 1; 
    %%% make table
    fprintf('%10s %15s %5s %15s %5s %15s\n ','Survey','Jetski','index','Walking','index','Surveypair_index')
    i_date=0; %counter surveys
    while i_date+1 <= length(m_time)
        i_date=i_date+1; %counter surveys ( I made a counter here so we can skip two places if it is a survey pair)

        % extra: print a row with the year number if we move to the next year
        if i_date == 1          %% if it is the first entry,  start bij putting the 2009
            fprintf('%5s\n',datestr(m_time(ind_sort(i_date)), 'yyyy'))
        else
            if str2num(datestr(m_time(ind_sort(i_date)), 'yyyy'))-str2num(datestr(m_time(ind_sort(i_date-1)), 'yyyy')) >= 1  %% check if we move to a new year between two consecutive surveys
                fprintf('%5s\n',datestr(m_time(ind_sort(i_date)), 'yyyy'))  % if it is the case, paste in a row with the year number
            end
        end
        %%%

        fprintf('%12s',[datestr(m_time(ind_sort(i_date)),'mmm') '   ']);  % first cell in a row is the month
        % Check if this is a combined or a single survey
        [ind_pair,jorw_ind]=find(survey_pairs == (ind_sort(i_date)));  %% if it is part of a pair it will give back the survey pair index
        if ind_pair >=1 % if it is part of a pair, then
            fprintf('%15s %5.0f %15s %5.0f %10.0f',datestr(m_time(survey_pairs(ind_pair,2))),survey_pairs(ind_pair,2),datestr(m_time(survey_pairs(ind_pair,1))),survey_pairs(ind_pair,1),ind_pair)
            i_date=i_date+1; % skip an extra place to avoid getting a double entry
        else % so it is not a pair , then it is a single survey (jetski or walking)
            if surveytype(ind_sort(i_date),1) == 'J'
                fprintf('%15s %5.0f %15s %5.0f',datestr(m_time(ind_sort(i_date))),ind_sort(i_date),'',[])
            else % then it is a walking so paste the dat in the other column
                fprintf('%15s %5.0f %15s %5.0f','',[],datestr(m_time(ind_sort(i_date))),ind_sort(i_date))
            end
        end
        fprintf('\n')

    end
end
%%% End make table

% output survey_pairs works as follows... a combined survey is a row in the matrix, first column is the walking survey index, second is the jetski survey index
% example
% if you want to see the profile at transect number 7 of the last combined survey type:
%
% alt=nc_varget('Vlugtenburg.nc','altitude');
% figure
% plot([ squeeze(alt(survey_pairs(end,1),7,:)) squeeze(alt(survey_pairs(end,2),7,:))])

%% define output
Output_struct = struct('Survey_pairs',survey_pairs,'indwalk',indwalk, 'indjet',indjet)   ;  % feel free to add stuff here
if nargout == 1
   varargout{1} = Output_struct;   
end





%% Sierds shizzle, pre 12-5-2010
% 
%     year_first = str2num(datestr(min(m_time),'yyyy'));
%     month_first = str2num(datestr(min(m_time),'mm'));
% 
%     year_last = str2num(datestr(max(m_time),'yyyy'));
%     month_last = str2num(datestr(max(m_time),'mm'));
% 
%     fprintf('%10s %15s %15s\n','Survey','Jetski','Walking')
% 
%     for i = year_first:1:year_last
%         % we have to make a division between first year, intermediate years and
%         % last year !
%         fprintf('%5.0f\n',i);
%         if i == year_first;
%             for t = month_first:1:12
%                 select_year(i,t,surveytype,m_time)
%             end
%         elseif i == year_last
%             for t = 1:1:month_last
%                 select_year(i,t,surveytype,m_time)
%             end
%         else
%             for t = 1:1:12
%                 select_year(i,t,surveytype,m_time)
%             end
% 
%         end
%     end
% 
% 
%     function select_year(year,month,surveytype,m_time);
%     %    fprintf('%10s',datestr(datenum(num2str(t),'mm'),'mmm'));
%     %         List walks
%     Walking_dates = select_dates(year,month,'Walking',surveytype,m_time);
%     %         List Jetski
%     Jetski_dates = select_dates(year,month,'Jetski',surveytype,m_time);
%     for k = 1:max([length(Walking_dates(:,1)) length(Jetski_dates(:,1))])
%         if k == 1
%             fprintf('%12s',[datestr(datenum(num2str(month),'mm'),'mmm') '   ']);
%         else
%             fprintf('%12s','');
%         end
% 
%         if length(Jetski_dates(:,1)) >= k & length(Walking_dates(:,1)) >= k
%             fprintf('%15s %15s\n',Jetski_dates(k,:),Walking_dates(k,:))
%         elseif length(Jetski_dates(:,1)) >= k
%             fprintf('%15s %15s\n',Jetski_dates(k,:),'')
%         elseif length(Walking_dates(:,1)) >= k
%             fprintf('%15s %15s\n','',Walking_dates(k,:))
%         end
%     end
% 
% 
%         function selection = select_dates(year2,month2,type,surveytype,m_time)
% 
%             Begintime = datenum([num2str(year2) '/' num2str(month2) '/01'],'yyyy/mm/dd');
%             Endtime = datenum([num2str(year2) '/' num2str(month2+1) '/01'],'yyyy/mm/dd');
% 
%             selection = datestr(m_time(m_time > Begintime &...
%                 m_time < Endtime & surveytype(:,1) == type(1)));%&...
% 
%         end
% 
%    
%% Even older shizzle by sierd 
%surveytype(:,1) == 'J' ));





% %%
% function selection = select_dates(year,month,surveytype,m_time)
%
% Begintime = datenum([num2str(year) num2str(month) '01'],'yyyymmdd')
% Endtime = datenum([num2str(year) num2str(month+1) '01'],'yyyymmdd')-1
%
% selection = datestr(m_time(m_time > Begintime &...
%             m_time < Endtime &...
%             surveytype(:,1) == 'J' ))
