function [varargout] = getSacFiles(varargin)
% [Sacs] = getSacFiles(varargin);
% This function returns a vector of SAC file names given information of
% the SAC file. The input format relies on the PASSCAL/IRIS file format,
% which is of the form:
%
% STACODE.*.*.CHAN.YYYY.*JJJ.*SAC
%
% The file names must include a 3-spance Julian day and year in the file
% name, as well as a year.
%
% USAGE
% [Seis] = getSacFiles('STATION','CHAN',date1,date2);
% [Seis] = getSacFiles('SAC','CHAN',date1,date2);
% [Seis] = getSacFiles('SAC','SAC',date1,date2);
% [Seis] = getSacFiles('STATION','CHAN',hour1,hour2,Julian day,year);
% [Seis] = getSacFiles('STATION','SAC',hour1,hour2,Julian day,year);
% [Seis] = getSacFiles('SAC','SAC',hour1,hour2,Julian day,year);
%
% INPUT
%
% rec: The station string as it appears in the .SAC file name. Input
% may be 'SAC' to specify any .SAC file
% comp: The channel string, as it appears in the .SAC file name. Input
% may be 'SAC', to specify any .SAC file
% date1: Two options: date1 may be a date-column vector, or a scalar.
% If date1 is a scalar, it is taken to be the hours into the day.
% date2: Same as date1. If date1 is the start time, the date2 is the
% end time.
% day: The Julian day of the record. It must be input only in the
% case that if date1 and date2 are scalars.
% yr: The year appearing in the SAC file name. It must input only if
% date1 and date2 are scalars.
%
% OUTPUT
%
% Sacs: A cell array containing the Sac file names that match the
% requested seismograms.
%
% EXAMPLES
%
% [Sacs] = getSacFiles('NL1','ELN',date1,date2); %get files between date1
% %and date2
% [Sacs] = getSacFiles('NL1','ELZ',2,4,204,2007); %get files from [2,4],
% %on Julian day 204, the
% %year 2007
% [Sacs] = getSacFiles('NL1','ELZ',date1,date2,2007); %year ignored
% Start function
D = dir;
I = not([D.isdir]);
D = D(I);
C = struct2cell(D);
C = {C{1,:}};
% Initialize Seis and Sacs
I = regexp(C,'SAC');
full = cellfun(@length,I);
I = logical(full);
C = C(I);
Sacs = C(:);
ntotal = length(Sacs);
if(nargin==0)
return;
else
if(nargin>=1),
rec = varargin{1};
if(~isempty(rec))
I = regexp(C,rec);
full = cellfun(@length,I);
I = logical(full);
C = C(I);
Sacs =intersect(Sacs,C(:));
end;
end;
if(nargin>=2),
comp = varargin{2};
if(~isempty(comp))
I = regexp(C,comp);
full = cellfun(@length,I);
I = logical(full);
C = C(I);
Sacs = intersect(Sacs,C(:));
end;
end;
if(nargin>=3),
date1 = (varargin{3});
if(~isscalar(date1))
%find the julian day from the date
boy = date1;
boy(2:3)= 1;
boy(4:6)= 0;
day = 1+floor(timediff(date1,boy)./(3600*24));
day = sprintf('%s.%.3i%s.','\',day,'\');
%find files with same day
I = regexp(C,day);
full = cellfun(@length,I);
I = logical(full);
C = C(I);
Sacs = intersect(Sacs,C(:));
%find files with same year
I = regexp(C,num2str(date1(1)));
full = cellfun(@length,I);
I = logical(full);
C = C(I);
Sacs = intersect(Sacs,C(:));
end;
end;
if(nargin>=4),
date2 = (varargin{4});
end;
if(nargin>=5),
day = sprintf('%s.%.3i%s.','\',varargin{5},'\');
%day = num2str(varargin{5});
I = regexp(C,day);
full = cellfun(@length,I);
I = logical(full);
C = C(I);
Sacs = intersect(Sacs,C(:));
end;
if(nargin>=6),
yr = num2str(varargin{6});
I = regexp(C,yr);
full = cellfun(@length,I);
I = logical(full);
C = C(I);
Sacs = intersect(Sacs,C(:));
end;
end;
Sacs = unique(Sacs);
L = size(Sacs,1);
if(L==0), disp(sprintf('\nNo files found in current directory')), end;
if(nargout>=1), varargout{1} = Sacs; end;
if(nargout>=2), varargout{2} = ntotal; end;