function out_file = radiometry_make_shapefile(textfile);

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

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

%load the textfile delivered by Alice
data = textread(textfile);
num = data(1,1);
data = data(2:end,:);
if num ~= length(data);
    disp('Issue With Text File Format!');
end;

% %define update points
% updates = [1,2,5,10,20,50,75,90]; %percent complete
% for i=1:length(updates);
%     [trash,updates_points(i)] = min( abs([1:num]./num.*100 - updates(i) ));
% 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 = {'Tb','lon_given','lat_given','emiss','temp_name',...
        'p1_lon','p2_lon','p3_lon','p4_lon',...
        'p1_lat','p2_lat','p3_lat','p4_lat',...
        'rng','bnum' };

%fill in missing properties
if length(head) > size(data,2);
    for j=(size(data,2)+1):length(head);
        data(:,j) = -1;
    end;
end;
   
    
%define the variables from the data
for i=1:length(head);
    eval([head{i} ' = data(:,' num2str(i) ');']);
end;

%step through and create the ellipse values and put them in a shapefile
%structure for ARCGIS
for i=1:num;
    %get the points for the current ellipse
    str = {'lat','lon'};
    str2 = {'x','y'};
    for j=1:4;
        for k=1:2;
            eval(['pt' num2str(j) str2{k} '='...
                'p' num2str(j) '_' str{k} '(i);']);
        end;
        eval(['if pt' num2str(j) 'y > 180;' ...
            'pt' num2str(j) 'y = pt' num2str(j) 'y - 360; end;'])
    end;
    ptx = [pt1x,pt2x,pt3x,pt4x];
    pty = [pt1y,pt2y,pt3y,pt4y];
    
    %%see if we are cross 180 or 360
    if ( min( abs( pty - 360 ) ) < 2 ) | (min(abs(pty)) < 2);
        %convert to -180 to 180
        pty(pty > 180) = pty(pty > 180) -360;
    elseif ( min( abs( pty -180) ) < 2 );
        %convert to 0 to 360
        pty(pty < 0) = pty(pty < 0) + 360;
    end;    
    
%     %find intersection point using triangulation
%     xvals = cos( [pt1y,pt3y,pt2y,pt4y] .*pi/180) .* ...
%         cos( [pt1x,pt3x,pt2x,pt4x] .*pi/180);
%     yvals = sin( [pt1y,pt3y,pt2y,pt4y] .*pi/180) .* ...
%         cos( [pt1x,pt3x,pt2x,pt4x] .*pi/180);
%     zvals = sin( [pt1x,pt3x,pt2x,pt4x] .*pi/180);
%     A = [xvals(1),yvals(1),zvals(1)];
%     B  = [xvals(2),yvals(2),zvals(2)];
%     C  = [xvals(3),yvals(3),zvals(3)];
%     D =  [xvals(4),yvals(4),zvals(4)];
%     N = cross(B-A,C-A);
%     N = N./norm(N);
%     latc1 = -asin(N(3)).*180/pi;
%     lonc1= atan2(N(2),N(1))*180/pi-180;
%     N2 = cross(B-A,D-A);
%     N2 = N2./norm(N2);
%     latc2 = -asin(N2(3))*180/pi;
%     lonc2 = atan2(N2(2),N2(1))*180/pi-180;
%     N3 = cross(C-B,D-B);
%     N3 = N3./norm(N3);
%     latc3 = -asin(N3(3)).*180/pi;
%     lonc3= atan2(N3(2),N3(1))*180/pi-180;
%     N4 = cross(D-C,A-C);
%     N4 = N4./norm(N4);
%     latc4 = -asin(N4(3))*180/pi;
%     lonc4 = atan2(N4(2),N4(1))*180/pi-180;
%     lat0 = mean([latc1,latc2,latc3,latc4]);
%     lon0 = mean([lonc1,lonc2,lonc3,lonc4]);
    [lon0v,lat0v] = great_circ_int(pty,ptx);
%     lat0 = lat0v(1);
%     lon0 = lon0v(1);
    [minval,minind] =  min( abs( lon0v - mean(pty)));
    lat0 = lat0v(minind);
    lon0 = lon0v(minind);
    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;
    lat0s(i) = lat0;
    lon0s(i) = lon0;
    latcens(i) = lat_given(i);
    loncens(i) = lon_given(i);
    if loncens(i) < (-180); loncens(i) = loncens(i) + 360; end;
    if loncens(i) > ( 180); loncens(i) = loncens(i) - 360; end;
    
    pt1x = ptx(1);
    pt2x = ptx(2);
    pt3x = ptx(3);
    pt4x = ptx(4);
    pt1y = pty(1);
    pt2y = pty(2);
    pt3y = pty(3);
    pt4y = pty(4);
   
    
    %derive semi-major and semi-minor axis for geographic ellipse
    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 = a;
    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 = lat0n1 + major./2.*cos(t).*cos(az2) - ...
                 minor./2.*sin(t).*sin(az2);             
    lon = lon0n1 + 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;
    loncens(loncens > 180) = loncens(loncens > 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; 
    
    %populate the structure file
    struct(i).Geometry = 'Polygon';
    struct(i).BoundingBox = ...
        [min(lat) max(lat); min(lon) max(lon)];
    struct(i).Y = lat;
    struct(i).X = -lon;
    struct(i).Tb = Tb(i);
    struct(i).rng = rng(i);
    struct(i).bnum = bnum(i);
    struct(i).latcen1 = latcens(i);
    struct(i).loncen1 = -loncens(i);
    struct(i).latcen2 = lat0s(i);
    struct(i).loncen2 = -lon0s(i);
    struct(i).lat0 = lat0n1;%lat_given(i);
    struct(i).lon0 = lon0n1;%-lon_given(i);
    struct(i).az = az2;
    struct(i).major = major;
    struct(i).minor = minor;
    struct(i).emiss = emiss(i);
    struct(i).Id = i;
    
    if i==171;
        disp('Here');
    end;
    %print an update to the screen
    if mod(i,50)==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)']);
    