function outfile = shp_find_shp(shpfile,shpfiles2);

%define start
eta_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('shpfiles2','var');
    shpfiles2 =[];
end;
if isempty(shpfiles2);
    shpfiles2{1} = ['E:\Titan\BIDR\Shapefiles\Dark_Lake_Unit_v2.shp'];
    shpfiles2{2} = ['E:\Titan\BIDR\Shapefiles\Granular_Lake_Unit_v2.shp'];
elseif ischar(shpfiles2);
    shpfiles2{1} = shpfiles2;
end;

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

%load the shpfile
shp = shaperead(shpfile);

%load the other shapefiles
numshp = length(shpfiles2);
for i=1:numshp;
    eval(['shp2_' num2str(i) ' = shaperead(shpfiles2{i});']);
end;

%%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);
    inshp(i).num = 0;
    inshp(i).lat = lat;
    inshp(i).lon = lon;
    clear names;    
    if ~isempty(ind);
        for j=1:numshp;
            shp2 = eval(['shp2_' num2str(j)]);
            for k=1:length(shp2);
                lat2 = shp2(k).Y;
                lon2 = shp2(k).X;
                ind2 = find( ~isnan(lat) & ~isnan(lon) );
                if ~isempty(ind2);
                   %check given polygon
                   mask = inpoly([lat2; lon2]',[lat; lon]');
                   %now check poylgon for +/- 360
                   mask2 = inpoly([lat2; lon2]',[lat; lon+360]');
                   mask3 = inpoly([lat2; lon2]',[lat; lon-360]');                   
                   gind = find(mask | mask2 | mask3);
                   if isempty(gind);
                       %Polygon Not in Master Polygon!
                   else;
                       %Polygon has points within Master Polygon!
                       inshp(i).num = inshp(i).num+1;
                       if ~exist('names','var');
                           names{1} = shpfiles2{j};
                           ids{1} = shp2.Id;
                           lats{1} = lat2;
                           lons{1} = lon2;
                           
                       else;
                           names{end+1} = shpfiles2{j};
                           ids{end+1} = shp2.Id;
                           lats{end+1} = lat2;
                           lons{end+1} = lon2;
                       end;
                   end;
                end;
            end;
        end;
    end;
    %fill the names array
    if ~exist('names','var');
        names = {''};
        ids = {[]};
        lats = {[]};
        lons = {[]};
    end;
    inshp(i).names = names;
    inshp(i).ids = ids;
    inshp(i).lats = lats;
    inshp(i).lons = lons;    
    
    %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);
        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 ' hrs)'];
        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,'-v7.3','inshp');

%get finish time
totmin = (now - eta_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...']);


