%MSAC_MREAD - Read multiple SAC time series files and return an array of
%             structures containing them.
%
%  [sac_trace_structure] = msac_mread(wildcard) ;
%
%  This reads multiple SAC files, specified by the wilcard. This
%  is expanded by the using the MATLAB ls command. 
%
%  [sac_trace_structure,namelist] =  msac_mread(wildcard) ;
%
%  This syntax also returns the list of filenames which matched the wildcard;
%  this is useful for writing the set of files out again after processing.
%

%------------------------------------------------------------------------------%
%                                                                              %
%   (c) James Wookey, University of Bristol, 2006                              %
%                                                                              %
%   Version 1.0                                                                %
%                                                                              %
%   This program is free software; you can redistribute it and/or modify       %
%   it under the terms of the GNU General Public License as published by       %
%   the Free Software Foundation; either version 2 of the License, or          %
%   (at your option) any later version.                                        %
%                                                                              %
%   This program is distributed in the hope that it will be useful,            %
%   but WITHOUT ANY WARRANTY; without even the implied warranty of             %
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              %
%   GNU General Public License for more details.                               %
%                                                                              %
%   You should have received a copy of the GNU General Public License          %
%   along with this program; if not, write to:                                 %
%                                                                              %
%   Free Software Foundation, Inc., 59 Temple Place, Suite 330,                %
%   Boston, MA  02111-1307  USA                                                %
%                                                                              %
%   See http://www.gnu.org/licenses/gpl.txt for full GPL terms.                %
%                                                                              %
%------------------------------------------------------------------------------%
% Version history

% v1.0 - Original version
% 2006-06-15

function [varargout] = msac_mread(wildcard) ;

[s,raw] = system(['ls ',wildcard,' | cat ']) ;
filenames = strread(raw,'%q') ;
 
[nfiles ndum] = size(filenames) ;

for ifile=1:nfiles
%  read each file using msac_read
   fname = char(filenames(ifile)) ;
   traces(ifile) = msac_read(fname) ;
end

if (nargout==2)
   varargout = {traces,filenames} ;
elseif (nargout==1)
   varargout = {traces} ;
else
   error('Not enough outputs specified for MSAC_MREAD') ;
end

return
% end of MSAC_MREAD.M

