function out_file = sartopo_make_shapefile(csvfile);

%%need sar file to define along track and cross-track directions

%define small major axis threshold (degrees);
small_thresh = 10;

%define the output file
str = strfind(csvfile,'.csv');
out_file = [csvfile(1:str) 'shp'];

%load teh sartopo file
data = csvread(csvfile);
num = size(data,1);

if num < 4;
    out_file = nan;
    disp(['Less Than Four Footprints Returning: ' csvfile]);
    return
end;

%%print an update to the screen
screen_prefix = ['Finished Making Ellipse'];
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;


%define the headers/variables
head = {'wlonc','latc','inc','xtrack_sz','atrack_sz','height','height_error','mask'};

%define the variables from the data
for i=1:length(head);
    eval([head{i} ' = data(:,' num2str(i) ');']);
end;

% %get the line and sample positions of all of the center poitns
% [line,sample] = bidr_proj(lat,-wlon,sar_file);

%only perform the analysis on the good indices
ind = find( (xtrack_sz > 0) & (atrack_sz > 0) );
gind = 1:num;
gind = gind(ind);

%step through and create the ellipse values and put them in a shapefile
%structure for ARCGIS
cnt = 1;
for i=gind;
   %get the center of the ellipse
   lat0 = latc(i);
   lon0 = -wlonc(i);
   if i==num;
       lon1 = -wlonc(i-1);
       lat1 = latc(i-1);
   else;
       lon1 = -wlonc(i+1);
       lat1 = latc(i+1);
   end;   
%    if abs( lon0 - mean([pty] -180) ) < 2;
%         lon0 = lon0 + 180;
%         lat0 = -lat0;
%     end;
    if lon0 < (-180); lon0 = lon0 + 360; end;
    if lon0 > ( 180); lon0 = lon0 - 360; end;
    if lon1 < (-180); lon1 = lon1 + 360; end;
    if lon1 > ( 180); lon1 = lon1 - 360; end;
    if abs( lon0 - lon1 ) > 10;
        lon1 = lon1+360;
    end;
%     if abs(lon0 - lon1) > 10;
%         disp(['Issue! At Data Point ' num2str(i)]);
%         return;
%         out_file = nan;
%     end;
    lat0s(i) = lat0;
    lon0s(i) = lon0;
    lat1s(i) = lat1;
    lon1s(i) = lon1;
    
    %%derive the along-track and cross track vectors using the sar file
    
    %%assume that vector to next point is along track dimension
    atrack_vec = [lat1-lat0,lon1-lon0];
    atrack_vec = atrack_vec./norm(atrack_vec);
    xtrack_vec = [atrack_vec(2) -atrack_vec(1)];
%     rot = pi/2;
%     xtrack_vec = atrack_vec*[cos(rot) -sin(rot); sin(rot) cos(rot)];
%     xtrack_vec = xtrack_vec./norm(xtrack_vec);
   
    %find the lat lon points of the four corners of the ellipse
    pt1x = xtrack_vec(1)./2.*xtrack_sz(i).*((2.*pi.*2575)./360).^(-1)+lat0;
    pt1y = xtrack_vec(2)./2.*xtrack_sz(i).*(cos(lat0.*pi./180).*(2.*pi.*2575)./360).^(-1)+lon0;    
    pt2x = (-1).*xtrack_vec(1)./2.*xtrack_sz(i).*((2.*pi.*2575)./360).^(-1)+lat0;
    pt2y = (-1).*xtrack_vec(2)./2.*xtrack_sz(i).*(cos(lat0.*pi./180).*(2.*pi.*2575)./360).^(-1)+lon0;
    pt3x = atrack_vec(1)./2.*atrack_sz(i).*((2.*pi.*2575)./360).^(-1)+lat0;
    pt3y = atrack_vec(2)./2.*atrack_sz(i).*(cos(lat0.*pi./180).*(2.*pi.*2575)./360).^(-1)+lon0;    
    pt4x = (-1).*atrack_vec(1)./2.*atrack_sz(i).*((2.*pi.*2575)./360).^(-1)+lat0;
    pt4y = (-1).*atrack_vec(2)./2.*atrack_sz(i).*(cos(lat0.*pi./180).*(2.*pi.*2575)./360).^(-1)+lon0;
    ptx = [pt1x pt2x pt3x pt4x];
    pty = [pt1y pt2y pt3y pt4y];
   
    %derive semi-major and semi-minor axis for geographic ellipse
    %%semimajor axis is always in cross-track dimension
    %%semiminor axis is always in along-track dimension
    
    a = sqrt( (pt1x-pt2x).^2 + (pt1y-pt2y).^2 );
    b = sqrt( (pt3x-pt4x).^2 + (pt3y-pt4y).^2 );
%     c = sqrt( (pt1x-pt3x).^2 + (pt1y-pt3y).^2 );
%     d = sqrt( (pt1x-pt4x).^2 + (pt1y-pt4y).^2 );
%     e = sqrt( (pt2x-pt3x).^2 + (pt2y-pt3y).^2 );
%     f = sqrt( (pt2x-pt4x).^2 + (pt2y-pt4y).^2 );
%    [major,mind] = max([a,b,c,d,e,f]);
%    mind =1;
    [major,mind] = max([a,b]);
    minor = min([a,b]);
    if mind == 1;
        az2 = atan( (pt1y-pt2y)./(pt1x-pt2x) );
    else;
         az2 = atan( (pt4y-pt3y)./(pt4x-pt3x) );
    end;
%     switch mind
%         case 1
%             minor = b;
%             lon0n1 = mean([pt3y,pt4y]);
%             lat0n1 = mean([pt3x,pt4x]);
%             az2 = atan( (pt1y-pt2y)./(pt1x-pt2x) );
%         case 2
%             minor = a;
%             lon0n1 = mean([pt1y,pt2y]);
%             lat0n1 = mean([pt1x,pt2x]);
%             az2 = atan( (pt4y-pt3y)./(pt4x-pt3x) );
%         case 3
%             minor = f;
%             lon0n1 = mean([pt2y,pt4y]);
%             lat0n1 = mean([pt2x,pt4x]);
%             az2 = atan( (pt1y-pt3y)./(pt1x-pt3x) );
%         case 4
%             minor = e;
%             lon0n1 = mean([pt3y,pt2y]);
%             lat0n1 = mean([pt3x,pt2x]);
%             az2 = atan( (pt4y-pt1y)./(pt4x-pt1x) );
%         case 5
%             minor = d;
%             lon0n1 = mean([pt1y,pt4y]);
%             lat0n1 = mean([pt1x,pt4x]);
%             az2 = atan( (pt2y-pt3y)./(pt2x-pt3x) );
%         case 6;
%             minor = c;
%             lon0n1 = mean([pt3y,pt1y]);
%             lat0n1 = mean([pt3x,pt1x]);
%             az2 = atan( (pt4y-pt2y)./(pt4x-pt2x) );
%     end;
    ecc = sqrt( 1 - (minor./major).^2);

%     if major < small_thresh;
%         yes_small = 1;
%     else;
%         yes_small = 0;
%     end;       
    
    
    %transition the center of the ellipse to match the semi-minor axis  
    
    %create the ellipse    
    t = linspace(-pi,pi,100);
    lat = lat0 + major./2.*cos(t).*cos(az2) - ...
                 minor./2.*sin(t).*sin(az2);             
    lon = lon0 + major./2.*cos(t).*sin(az2) + ...
                 minor./2.*sin(t).*cos(az2);
                 
    %convert the longitude values into -180 to 180 
    lon(lon > 180) = lon(lon > 180) - 360;
    lon0s(lon0s > 180) = lon0s(lon0s > 180) - 360;  
    
    %if we pass through 180 in the middle fix it
    if ( max(lon) > 170 ) & ( min(lon) < -170 );
        lon(lon < 0) = lon(lon < 0) + 360;
    end; 
    
%     if (length(find(~isfinite(lat) | ~isfinite(lon))) > 0);
%         disp('Break');
%     end;
    
    %populate the structure file
    struct(cnt).Geometry = 'Polygon';
    struct(cnt).BoundingBox = ...
        [min(lat) max(lat); min(lon) max(lon)];
    struct(cnt).Y = lat;
    struct(cnt).X = lon;   
    struct(cnt).lat0 = lat0;%lat_given(i);
    struct(cnt).lon0 = lon0;%-lon_given(i);
    struct(cnt).az = az2;
    struct(cnt).major = major;
    struct(cnt).minor = minor;
    struct(cnt).height = height(i);
    struct(cnt).height_error = height_error(i);
    struct(cnt).inc = inc(i);
    struct(cnt).mask = mask(i);
    struct(cnt).xtrack_sz = xtrack_sz(i);
    struct(cnt).atrack_sz = atrack_sz(i);
    struct(cnt).Id = i;
    cnt = cnt+1;
    %print an update to the screen
    if mod(i,100)==0;
        fprintf(1,back_string);
       screen_suffix = [ ' ' num2str(i) ' Of  ' num2str(num)  ...
                 ' ( ' num2str(round(i./num.*100*10)/10) ' Percent Complete)'];
        fprintf(1, screen_suffix);
        back_string = ['\b'];
        while length(back_string) < 2*length(screen_suffix);
            back_string = ['\b' back_string];
        end;
    end;
end;

%write out the shapefile
shapewrite(struct,out_file);

%define a projection for the shapefile
%create projection files for the new shape files
%open and write a projection file for titan to go along with it
pos = strfind( out_file, '.shp' );
prj_file = [out_file(1:pos) 'prj'];
fid = fopen(prj_file,'w');
string = ['GEOGCS["GCS_Titan_2000",DATUM["D_Titan_2000",'...
    'SPHEROID["Titan_2000_IAU_IAG",2575000.0,0.0]],PRIMEM["Reference_Meridian",0.0],',...
    'UNIT["Degree",0.0174532925199433]]' '\n'];
fprintf(fid,string);
fclose(fid);

%%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! ( ' out_file ' Written)']);
    