function [outfile,struct] = analyze_shp_manual(shpfile,shpfile_files,shpfile_shps,filelist);

%define start time
eta_start = now;

%define the strings
clear str
str{1} = 'a1';
str{2} = 'c1';

%check inputs
invar = {'shpfile','shpfile_files','shpfile_shps','filelist'};
for i=1:length(invar);
    if ~exist(invar{i},'var');
        eval([ invar{i} ' =[];']);
    end;
end;

%define default input for empty lakes
if isempty(shpfile);
    shpfile = 'Empty_Lake_Unit_v2.shp';
    shpfile_files = 'Empty_Lake_Unit_v2_files.mat';
    shpfile_shps  = 'Empty_Lake_Unit_v2_shapes.mat';
end;
if isempty(shpfile_files);
    pos = strfind( shpfile,'.shp');
    shpfile_files = [shpfile(1:pos(end)-1) '_files.mat'];
end;
if isempty(shpfile_shps);
    pos = strfind( shpfile,'.shp');
    shpfile_shps = [shpfile(1:pos(end)-1) '_shapes.mat'];
end;

%define default input for the 060309 filelist
if isempty(filelist);;
    filelist = 'E:\Titan\BIDR\Scripts\BIDR_File_List_060309.txt';
end;
if ischar(filelist);
    pos = strfind(filelist,'txt');
    if length(pos) > 0;
        files =  read_filelist(filelist);
    else;
        files = filelist;
    end;
end;
files_f = fields(files);
files_num = length(files_f);

%load the requisit files
shp = shaperead(shpfile);
load(shpfile_files);
if exist(shpfile_shps) ~= 2;
    disp(['Using Default No Intersections With Other Polygons!']);
    for i=1:length(shp);
        inshp(i).num = 0;
    end;
else;
    load(shpfile_shps);
end;

%set up the figures
pos1 = [22   250   850   850];
fig1 = figure('Position',pos1);

update_plot = 1;
if update_plot;
    pos2 = [1028   250   850   850];
    fig2 = figure('Position',pos2);
    hold on;
    ax2 = gca;    
    xlabel('Incidence Angle (\circ)');
    ylabel('\sigma_o');    
    xlim([5,50]);
    ylim([-30,2]);
end;


% %%print an update to the screen
% num = length(shp);
% screen_prefix = ['Finished Analyzing 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 each shape and calculate the statistics
for i=1:length(shp);
    %get the lat and lon
    lat = shp(i).Y;
    lon = shp(i).X;
    ind = find( ~isnan(lon) & ~isnan(lat) );
    lat = lat(ind);
    lon = lon(ind);
    %if there is no polygon, do not continue!
    if ~isempty(ind);
        
        %input the basic statistics
        struct(i).lat{1} = nan;
        struct(i).lon{1} = nan;
        struct(i).lat0 = nan;
        struct(i).lon0 = nan;
        struct(i).n_points = nan;                 
        struct(i).date = date;
        
       
        %put in null values for other statistics for now
        struct(i).avg = nan;
        struct(i).med = nan;
        struct(i).std = nan;
        struct(i).inc_avg = nan;
        struct(i).inc_med = nan;
        struct(i).inc_std = nan;
        struct(i).sar_vals{1} = nan;
        struct(i).inc_vals{1} = nan;
        struct(i).sar_file{1} = nan;
        struct(i).inc_file{1} = nan;
        
        
        %step through each of the files which contain the shape
        for j=1:infiles(i).num;
            %define the sar file
            sar_file = infiles(i).names{j};    
            
            %find the associated incidence angle file
            clear inc_file
            sar_file = strrep(sar_file,'/','\');
            [sar_dir,sar_file1,sar_file2] = fileparts(sar_file);
            sar_file = [sar_file1 sar_file2];
            for k=1:files_num;   
                [temp_dir,temp_file1,temp_file2] = fileparts(eval(['files.' files_f{k} ';']));
                temp_file = [temp_file1 temp_file2];
                if strcmp(sar_file,temp_file);
                    temp_str = strrep(files_f{k},str{1},str{2});                    
                    [inc_dir,inc_file1,inc_file2] = fileparts(eval(['files.' temp_str ';']));
                    inc_file = [sar_dir '\' inc_file1 inc_file2];
                    break
                end;
            end;
            sar_file = [sar_dir '\' sar_file1 sar_file2];
            if ~exist('inc_file','var');
                sar_file = strrep(sar_file,'\','/');
                for k=1:files_num;
                    if strcmp(sar_file,eval(['files.' files_f{k} ';']));
                        temp_str = strrep(files_f{k},str{1},str{2});
                        inc_file = eval(['files.' temp_str ';']);
                        break
                    end;
                end;
            end;
            if ~exist('inc_file','var');
%                 back_string= ['\b'];
%                 while length(back_string) < (2*length([screen_prefix,screen_suffix]));
%                     back_string = ['\b' back_string];
%                 end;
%                 fprintf(1,back_string);
                
                disp(['Coud Not File Incidence Angle Files For ' sar_file]);
                
%                 fprintf(1,[screen_prefix,screen_suffix]);
%                 back_string = ['\b'];
%                 while length(back_string) < 2*length(screen_suffix);
%                     back_string = ['\b' back_string];
%                 end;
                break
            end;
            
            %convert the polygon to sample and line space
            [line,sample,info] = bidr_proj(lat,lon,sar_file);
            
            %get the size of the image
            %info = pds_label_parse(sar_file);
            if isfield(info,'qube')
                sz = info.qube.coreitems;
                if ischar(sz); sz = str2num(sz); end;
                sz_s = sz(1);
                sz_l = sz(2);
            elseif isfield(info,'image');
                sz_s = info.image.linesamples;
                sz_l = info.image.lines;
                if ischar(sz_s); sz_s = str2num(sz_s); end;
                if ischar(sz_l); sz_l = str2num(sz_l); end;
            else;
                disp(['Could Not Find Image Size For ' sarfile]);
                break
            end;
            
            %get the samples and lines associated with polygon            
            bb_min_s = max([1 floor(min(sample))]);
            bb_max_s = min([sz_s ceil(max(sample))]);
            bb_min_l = max([1 floor(min(line))]);
            bb_max_l = min([sz_l ceil(max(line))]);
            
            sar_samples = repmat([bb_min_s:bb_max_s],1,length([bb_min_l:bb_max_l]));
            temp_s = length([bb_min_s:bb_max_s]);
            temp_l = length([bb_min_l:bb_max_l]);
            temp_l2 = [bb_min_l:bb_max_l];
            sar_lines = zeros(size(sar_samples));
            for cntl=1:temp_l;
                sar_lines( ((cntl-1).*temp_s+1):(cntl.*temp_s) ) = temp_l2(cntl);
            end;

            %read the data
            sar = sar_readpds(sar_file,sar_samples,sar_lines);
            sar = reshape(sar,bb_max_s - bb_min_s+1,bb_max_l - bb_min_l+1);
            inc = sar_readpds(inc_file,sar_samples,sar_lines);
            inc = reshape(inc,bb_max_s - bb_min_s+1,bb_max_l - bb_min_l+1);
            sar2 = sar;
            sar2(:) = nan;
            sind = find ( sar > 0);
            sar2(sind) = 10.*log10(sar(sind));
            
            %put the image up on the figure
            figure(fig1);            
            %imagesc(sar2,[-12,0]);colorbar;colormap gray;
            imagesc(sar,[0,1]);colorbar; colormap gray;
            hold(gca,'on');
            axis equal
            
            %overplot the lake
            p1 = plot(line-bb_min_l+1,sample-bb_min_s+1,'r','linewidth',2);
            hold(gca,'off');
           
            %give the titles
            title([num2str(i) ' Of ' num2str(length(shp))]);
            
            %get the mask
            h = imfreehand;            
            h_pos = getPosition(h);
            h_line = h_pos(:,1)+bb_min_l-1;
            h_samp = h_pos(:,2)+bb_min_s-1;
            [h_lat,h_lon] = bidr_proj_inv(h_samp,h_line,sar_file);
            h_ind = find( ~isnan(h_lat) & ~isnan(h_lon) );
            h_lat = h_lat(h_ind);
            h_lon = h_lon(h_ind);
            
            %decide if we wish to use the data
            ans = input('Use [y/n/<Default y>]? :','s');
            if isempty(ans); ans='y'; end;
            if ans(1)=='y';
                useflag = 1;
            else;
                useflag = 0;
            end;
            
            %create the mask array             
            mask = createMask(h);            
            sar = sar.*mask;
                        
            %clear the roi handle
            delete(h);
            
            
            %read in the good data and calculate the statistics
            gind = find(sar > -10 & sar < 10 & sar ~= 0);           
            if isempty(gind);               
                %read in the good data
                %input the statistics                
                struct(i).lat{1} = nan;
                struct(i).lon{1} = nan;
                struct(i).lat0 = nan;
                struct(i).lon0 = nan;
                struct(i).n_points = nan;
                struct(i).avg(j) = nan;
                struct(i).med(j) = nan;
                struct(i).std(j) = nan;
                struct(i).inc_avg(j) = nan;
                struct(i).inc_med(j) = nan;
                struct(i).inc_std(j) = nan;
                struct(i).sar_vals{j} = nan;
                struct(i).inc_vals{j} = nan;
                struct(i).use_flag(j) = useflag;
                struct(i).sar_file{j} = sar_file;
                struct(i).inc_file{j} = inc_file;
            else;
                sar = sar(gind);
                inc = inc(gind);
                struct(i).lat{j} = h_lat;
                struct(i).lon{j} = h_lon;
                struct(i).lat0(j) = mean(h_lat);
                struct(i).lon0(j) = mean(h_lon);
                struct(i).n_points(j) = length(h_lat);
                struct(i).avg(j) = mean(sar);
                struct(i).med(j) = median(sar);
                struct(i).std(j) = std(sar);
                struct(i).inc_avg(j) = mean(inc);
                struct(i).inc_med(j) = median(inc);
                struct(i).inc_std(j) = std(inc);
                struct(i).sar_vals{j} = sar;
                struct(i).inc_vals{j} = inc;
                struct(i).use_flag(j) = useflag;
                struct(i).sar_file{j} = sar_file;
                struct(i).inc_file{j} = inc_file;   
                
                if update_plot;   
                    if useflag;
                        plot(ax2,struct(i).inc_avg(j),10.*log10(struct(i).avg(j)),'b.','Markersize',16);
                    else;
                        plot(ax2,struct(i).inc_avg(j),10.*log10(struct(i).avg(j)),'g.','Markersize',16);
                    end;
                end;
                %disp([struct(i).inc_avg(j),struct(i).avg(j)]);
            end;
        end;
    end;
    
%     %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.*60.*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 ' min)'];
%         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
pos = strfind(shpfile,'.shp');
vnum = 1;
outfile = [shpfile(1:pos(end)-1) '_analysis_manual_v' num2str(vnum) '.mat'];
while exist(outfile,'file');
    vnum = vnum+1;
    outfile = [shpfile(1:pos(end)-1) '_analysis_manual_v' num2str(vnum) '.mat'];
end;   
save(outfile,'struct');

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

%close figure
if ishandle(fig1);
    close(fig1);
end;
if ishandle(fig2);
    %close(fig2);
end;

% %%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...']);
    

    

