function [y]=buildmat(pp,wl,cid,id,file,ncol);
%
% where pp='y' for plot points or 'n' for do not plot
%       wl='y' for plot points with labels
%       cid=column number to subselect by
%       id=[xx yy]; vector to select with
%      tmp=filename string
%      ncol=number of columns to use
%      [y]= matrix with lon,lat, and values as columns
%
% A MATLAB PROGRAM TO BUILD A MATRIX BY READING SPECIFIC COLUMNS
% OF MULTIPLE ASCII DATA FILES
%
% RETURNS A m x n MATRIX Y WHERE M IS THE #PTS IN THE TIMES SERIES
% AND N IS THE TOTAL NUMBER OF VARIABLES
%
% Modified for "ATSEE" use to read a single file and prompt
% users for longitude and latitude colunms
%
if nargin<1;
  pp=input('Do you want symbols at each point? (y/n)','s');
end;
if nargin<2;
  wl=input('Do you want labels at each point? (y/n)','s');
end;
pp
wl

disp(' ')
%disp(' Note: Keep track of the order in which you load variables.')
%
disp(' ')
%nf=input('How many files do you want to read from? ');
%
%for i=1:nf;
if nargin<5;
 file=input('Enter data filename (with path and filespec): ','s');
end;

tmp=file;
%
% find the file extension
%
 ie=find(tmp=='.');
 ext=tmp(ie+1:length(tmp));
 tmp=tmp(1:ie-1)
%
% Now, step through the filename until a slash is found.
% (or a colon or neither). Note that this is the beginning of the pathname.
%
% First, check for colons
%
  ip=0;
  for j=1:length(tmp);
    if tmp(j) == ':'
    ip=j;
    end
  end
%
% next, check for last backslash
%
  for j=1:length(tmp);
    if tmp(j) == '/'
    ip=j;
    end
  end
%
% if neither was found than ip=0 ... no path was supplied
%
    root=tmp(ip+1:length(tmp));
%
 eval(['load ',file,';']);
 eval(['xxx=',root,';'])
%
 disp(' ');
 disp(' Assuming your data is column delimited');
 disp(' ');
if nargin<6;
  ncol=input('How many columns (including lat & lon) do you want to use?: ');
end;
  for k=1:ncol
    if k==1;
      wcol=input('Which column is longitude variable in? ');
    elseif k==2;
      wcol=input('Which column is latitude variable in? ');
    else;
      wcol=input('Which column is next variable in? ');
    end;
    y1=xxx(:,wcol);
    y1=y1';
%    if i == 1
      if k == 1
        y=y1;
      else
        y=[y;y1];
      end
%    else
%      y=[y;y1];
%    end
  end
end

y=y';


% convert to decimal degrees if necessary
        if mean(y(:,1))>99;
           [y(:,1),y(:,2)]=ddmm2dec(y(:,1),y(:,2));
        end;

% make long negative if necssary
        if mean(y(:,1))>0;
           y(:,1)=y(:,1)*(-1);
        end;

% subselection if called for
if nargin>2;
  id=find(xxx(:,cid)>=id(1)&xxx(:,cid)<=id(1));
  y=y(id,:);
end;

disp(' ')
disp('If you called this routine with an output variable "[y]",');
disp('you now have a matrix holding your data with the first')
disp('two columns being longitude and latitude, respectively')


%pp=input('Do you want to plot these points? (y/n)','s');
if pp=='y';
% wl=input('Do you want to plot with labels? (y/n)','s')
  if wl=='y';
    plot(y(:,1),y(:,2),'r.','markersize',60);
    [ry,cy]=size(y);
    for i=1:ry;
      text(y(i,1),y(i,2),num2str(y(i,3)),...
     'horizontalalignment','center','color','k');
    end;
  else;
    plot(y(:,1),y(:,2),'r.','markersize',40);
  end;
else;
%  [ry,cy]=size(y);
%  plot([min(y(:,1)) max(y(:,1))],[min(y(:,2)) max(y(:,2))],'+','markersize',2);
%  for i=1:ry;
%    text(y(i,1),y(i,2),num2str(y(i,3)),...
%    'horizontalalignment','center','color','w');
%  end;
end;

  

