function outfile = shp_find_swaths(shpfile,restrictions,filelist);

%define start
eta_real_start = now;

%check inputs
if ~exist('shpfile','var');
    shpfile = [];
end;
if isempty(shpfile);
    shpfile = ['E:\Titan\BIDR\Shapefiles\Empty_Lake_Unit_v2.shp'];
end;
if ~exist('file_list','var');
    file_list =[];
end;
if isempty(file_list);
    file_list = ['E:\Titan\BIDR\Scripts\BIDR_File_List_060309.txt'];
end;
if ~exist('restrictions','var');
    restrictions  = [];
end;
    

%define output
pos = strfind(shpfile,'.shp');
outfile = [shpfile(1:pos(end)-1) '_files.mat'];

%load the file list
files = read_filelist(file_list);
files_f = fields(files);

%find out if this is windows or linux
%determine the operation system (linux or windows)
str = computer;
if strcmp(str,'PCWIN') | strcmp(str,'PCWIN64');
     windows = 1;
else;
     windows=0;
end;
if windows;
    base_dir0 = '\net\radar\raid1\hayes\Titan\BIDR\';
    base_dir = 'E:\Titan\BIDR\';
end;

%fine the positions in the file list we are interested in
for i=1:length(files_f);
    pos = strfind(files_f{i},'a1');
    pos2 = strfind(files_f{i},'aa1');
    if ~isempty(pos) & isempty(pos2);
        files_ind(i) = 1;
    else;
        files_ind(i) = 0;
    end;
end;
if ~isempty(restrictions);
    files_ind2 = files_ind;
    files_ind(:) = 0;
    for i=1:length(files_f);
        for j=1:length(restrictions);
            pos = strfind(files_f{i},restrictions{j});
            if ~isempty(pos) & files_ind2(i) ~= 0;
                files_ind(i)=1;
            end;
        end;
    end;
    for i=1:length(files_f);
        if files_ind(i) ==1;
            sar_file = eval(['files.' files_f{i}]);
            if windows;
                sar_file = strrep(sar_file,'/','\');
                sar_file = strrep(sar_file,base_dir0,base_dir);
                %if exist(sar_file) ~= 2;
                    disp([sar_file]);
                %end;
            end;
        end;
    end;    
end;
for i=1:length(files_f);
    if files_ind(i) ==1;
        sar_file = eval(['files.' files_f{i}]);
        if windows;
            sar_file = strrep(sar_file,'/','\');
            sar_file = strrep(sar_file,base_dir0,base_dir);
            if exist(sar_file) ~= 2;
                disp([sar_file ': Not Found']);
                files_ind(i) = 0;
            end;
        end;
    end;
end;
    
%load the shpfile
shp = shaperead(shpfile);


%to speed things up make a cell array of info structures to avoid rerunning
%      pds_label_parse.m all the itme
message = 'Reading File Labels....';
fprintf(1,message);
back_string = ['\b'];
while length(back_string) < 2*length(message);
    back_string = ['\b' back_string];
end;
for j=1:length(files_f);
    sar_file = eval(['files.' files_f{j}]);
    if windows;
        sar_file = strrep(sar_file,'/','\');
        sar_file = strrep(sar_file,base_dir0,base_dir);
    end;
    if exist(sar_file)==2 & files_ind(j)==1;
        info = pds_label_parse(sar_file);
        info_struct{j} = info;
    else;
        info_struct{j} = [];
    end;
end;
fprintf(1,back_string);

%define start
eta_start = now;

%%print an update to the screen
num = length(shp);
screen_prefix = ['Finished Checking Shape'];
screen_suffix = [ ' ' num2str(0) ' Of  ' num2str(num)  ...
    ' ( ' num2str(0) '  Percent Complete)'];
fprintf(1,[screen_prefix,screen_suffix]);
back_string = ['\b'];
while length(back_string) < 2*length(screen_suffix);
    back_string = ['\b' back_string];
end;

%step through the shapes and figure out which files they fit into
for i=1:length(shp);
    lat = shp(i).Y;
    lon = shp(i).X;
    ind = find( ~isnan(lat) & ~isnan(lon) );
    lat = lat(ind);
    lon = lon(ind);
    infiles(i).num = 0;
    clear names;    
    if ~isempty(ind);
        for j=find(files_ind);
            sar_file = eval(['files.' files_f{j}]);    
            if windows;
                sar_file = strrep(sar_file,'/','\');
                sar_file = strrep(sar_file,base_dir0,base_dir);
            end;
            if isempty(info_struct{j});
                disp('Break');
            end;
            [line,sample,info] = bidr_proj(lat,lon,info_struct{j});
            if isfield(info,'qube');
                sz = str2num(info.qube.coreitems);
                if isstr(sz); sz = num2str(sz); end;
                s_sz = sz(1);
                l_sz = sz(2);
                timeval = info.qube.starttime;                
            else;                
                s_sz = (info.image.linesamples);
                l_sz = (info.image.lines);
                if ischar(s_sz); s_sz = str2num(s_sz); end;
                if ischar(l_sz); l_sz = str2num(l_sz); end;
                timeval = info.starttime;
            end;
            bb_min_l = floor(min(line(line > 0)));
            bb_max_l = ceil(max(line( line > 0)));
            bb_min_s = floor(min(sample( sample > 0)));
            bb_max_s = ceil(max(sample( sample > 0)));
            if isempty(bb_min_l) | isempty(bb_max_l) | isempty(bb_min_s) | isempty(bb_max_s);
                nolake = 1;
            else;
                nolake = 0;
            end;
            if nolake | bb_min_l > l_sz | bb_max_l < 1 | bb_min_s > s_sz | bb_max_s < 1;
                %lake not in this swath
            else;
                %%Lake May Be In This Swath!
                %read in the border pixels
                gind = find( (sample >= 1) & (sample <= s_sz) & ...
                             (line   >= 1) & (line   <= l_sz) );
                sar_vals = sar_readpds(sar_file,sample(gind),line(gind));
                test_ind = find( sar_vals > -0.5 );
                if numel(test_ind) > 0;
                    %get the time value for this swath   
                    yr = str2num(timeval(1:4));
                    pos1 = strfind(timeval,'-');
                    pos2 = strfind(timeval,'t');                    
                    doy = str2num(timeval((pos1(end)+1):(pos2(1)-1)));
                    pos3 = strfind(timeval,':');
                    hh = str2num(timeval( (pos2(1)+1):(pos3(1)-1)));
                    mm = str2num(timeval( (pos3(1)+1):(pos3(2)-1)));
                    ss = str2num(timeval( (pos3(2)+1):(pos3(2)+2)));
                    ds = doy2date(doy,yr);
                    dv = datevec(ds);
                    month = dv(2);
                    day = dv(3);
                    dateval = ( datenum(yr,month,day,hh,mm,ss) - ...
                                datenum(2000,1,1,12,0,0) ).*24.*60.*60; %seconds from J2000;                                            
                    infiles(i).num = infiles(i).num+1;
                    if ~exist('names','var');
                        names{1} = sar_file;
                        times{1} = dateval;
                    else;
                        names{end+1} = sar_file;
                        times{end+1} = dateval;
                    end;                
                end;
            end;
        end;
    end;
    
    %fill the names array
    if ~exist('names','var');
        names = {''};
    end;
    infiles(i).names = names;
    infiles(i).times = times;
    
    %print an update to the screen
    if (i==1 | mod(i,20)==0);
        eta_end = now;
        eta_time = (eta_end-eta_start).*24./i.*(length(shp)-i);
        if eta_time < 1;
            eta_time = eta_time.*60;
            eta_str = ' min';
        else;
            eta_str = ' hrs';
        end;
        eta_time = num2str(round(eta_time.*100)/100);
        
    end;
    if mod(i,1)==0;
        fprintf(1,back_string);
        screen_suffix = [ ' ' num2str(i) ' Of  ' num2str(num)  ...
            ' ( ' num2str(round(i./num.*100*10)/10) ' Percent Complete ' ...
            'ETA: ' eta_time eta_str];
        fprintf(1, screen_suffix);
        back_string = ['\b'];
        while length(back_string) < 2*length(screen_suffix);
            back_string = ['\b' back_string];
        end;
    end;
end;

%save the output file
save(outfile,'infiles');

%get finish time
totmin = (now - eta_real_start).*24.*60;

%%clean up screen
back_string= ['\b'];
while length(back_string) < (2*length([screen_prefix,screen_suffix]));
    back_string = ['\b' back_string];
end;
fprintf(1,back_string);
disp(['All Done! ( ' outfile ' Written) Took ' num2str(totmin) ' minutes...']);


