function elbonian_network_map(filename, printfile, figure_n)
%
% file:      	elbonian_network_map.m, (c) Matthew Roughan, Tue Mar 10 2009
% created: 	Tue Mar 10 2009 
% author:  	Matthew Roughan 
% email:   	matthew.roughan@adelaide.edu.au
% 
% This program draws a picture of Elbonia, and if given, a prospective network
%
% INPUTS:
%   filename: the name of a file containing the prospective network
%   printfile: the name of an output file (must end in .eps)
%
% OUTPUTS:
%   the function does not output any variables, but does create one file
%       printfile (if given as an input)
%   which holds the figure that is drawn
%
% OTHER FILES
%   this program expects the following files to be in the current directory
%       elbonian_city_locations.dat
%       elbonian_border.dat
%


% read in the input files
[city_index, city_name, city_x, city_y] = read_cities();
[border_x, border_y] = textread('elbonian_border.dat', '%f %f', 'delimiter', '|');
N = length(city_index);

% draw map of Elbonia
if (nargin < 3)
  figure
else
  figure(figure_n);
end
hold off
fill(border_x, border_y, [0.9, 0.9, 0.9])
hold on
plot(city_x, city_y, 'o', 'linewidth', 4, 'markersize', 4);
% set(gca,'xlim', [0 aspect_ratio*scale]);
% set(gca,'ylim', [0 scale]);
daspect([1 1 1]);
d = 3;
for i=1:N
  h(i) = text(city_x(i)+d, city_y(i), char(city_name(i)), ...
	      'verticalalignment', 'middle');
end
set(gca,'fontsize', 12);
xlabel('km');
ylabel('km');
text(220, 270, 'Elbonia', 'fontsize', 32);

if (nargin > 0 & ~isempty(filename))
  [pop1, pop2, links_type, links_n, links_capacity, ...
   group_id, group_members, date, ...
   network_cost, router_cost, total_fibre_cost, total_line_card_cost] = read_links(filename)
  
  % check the costs are as expected
  [X,Y] = meshgrid(city_x, city_y);
  distance = sqrt((X-X').^2 + (Y-Y').^2);
  for i=1:length(pop1)
    links_distances(i,1) = distance(pop1(i), pop2(i));
  end
  [total_cost, fibre_cost, line_card_cost] = ...
      compute_link_cost(links_type, links_n, links_distances);
%   if (abs(sum(total_cost) - (network_cost-router_cost)) > 2)
%     error(sprintf('network costs doesn''t equate: %f and %f\n', ...
% 		  network_cost-router_cost, sum(total_cost)));
%   end
%   if (abs(sum(fibre_cost) - total_fibre_cost) > 2)
%     error(sprintf(' costs doesn''t equate: %f and %f\n', ...
% 		  total_fibre_cost, sum(fibre_cost)));
%   end
%   if (abs(sum(line_card_cost) - total_line_card_cost) > 2)
%     error(sprintf(' costs doesn''t equate: %f and %f\n', ...
% 		  total_line_card_cost, sum(line_card_cost)));
%   end
  
  % plot the links
  for i=1:length(pop1)
    if (links_type(i) == 1)
      plot([city_x(pop1(i)) city_x(pop2(i))], ...
	   [city_y(pop1(i)) city_y(pop2(i))], 'r-', 'linewidth', links_n(i));
    else
      plot([city_x(pop1(i)) city_x(pop2(i))], ...
	   [city_y(pop1(i)) city_y(pop2(i))], 'r-', 'linewidth', 4*links_n(i));
    end
  end
  % title(sprintf('Group %s, members %s', strrep(group_id,'_',' '), group_members))
end

if (nargin >= 2 & ~isempty(printfile))
  if (~isempty(strfind(printfile, '.eps')))
    print('-depsc', printfile);
  elseif (~isempty(strfind(printfile, '.png')))
    print('-dpng', printfile);
  else
    error('invalid printfilename: must end in .eps or .png');
  end
end

