function readdata
% Function READDATA is part of program HAVOC, a least 
% squares traverse adjustment program.  
% Function READDATA reads an ASCII data file containing
% all the information for the adjustment

%============================================================================
% Function:  readdata
%
% Author:
%  Rod Deakin, 
%  School of Mathematical & Geospatial Science, 
%  RMIT University,
%  GPO Box 2476V, MELBOURNE VIC 3001
%  AUSTRALIA
%  email: rod.deakin@rmit.edu.au
%
% Date:
%  Version 1.0   3 August   2006
%                minor change 03-Aug-2017
%
% Functions required:
%  decdeg
%
% Remarks:
%  This function reads the data from an ASCII data file.  Each line of the 
%  data file is assumed to be a record containing FIVE elements separated by 
%  whitespace.  If a record has a % symbol in column 1 then the line is
%  regarded as a comment line and ignored.  The first element in a record
%  is a code which defines the type of data.  
%                                   (1)   (2)     (3)   (4)     (5)
%  code = 1: Fixed Station,         code  serial  east  north   dummy
%  code = 2: Floating Station,      code  serial  east  north   dummy
%  code = 3: Observed Direction,    code  at      to    D.MMSS  st.dev.
%  code = 4: Observed Distance,     code  at      to    metres  st.dev.
%  code = 5: Constrained Bearing,   code  at      to    D.MMSS  dummy
%  code = 6: Constrained Distance,  code  at      to    metres  dummy
%  code = 7: Constrained Angle,     code  at      left  right   D.MMSS
%
%  The function determines the number of fixed and floating stations in the
%  network as well as the number of observed directions and distances, the
%  number of constrained bearings, distances and angles.  This information
%  together with the data from the file is copied into the global structures
%  STATION, OBS and JOB
%
%  NOTE:  If there are no fixed stations then it is assumed that the
%  adjustment is a free net adjustment.  Any constraints will be ignored
%  and only inner constraints allowed.  These inner constraints will be
%  determined and added automatically.
%
%============================================================================

global STATION OBS JOB X Z VFACT Qxx               

%-------------------------------------------------------------------------
% 1. Call the User Interface (UI) to choose the input data file name
% 2. Concatenate strings to give the path and file name of the input file
% 3. Strip off the extension from the file name to give the rootName
% 4. Add extension ".out" to rootName to give the output filename
% 5. Concatenate strings to give the path and file name of the output file
%-------------------------------------------------------------------------
filepath = strcat('c:\projects\adjust\','*.txt');
[infilename,inpathname] = uigetfile(filepath);
infilepath = strcat(inpathname,infilename);
rootName   = strtok(infilename,'.');
outfilename = strcat(rootName,'.out');
outfilepath = strcat(inpathname,outfilename);
JOB(1).infile  = infilepath;
JOB(1).outfile = outfilepath;

%----------------------------------------------------------
% 1. Load the data into an array whose name is the rootName
% 2. set fileTemp = rootName
% 3. Copy columns of data into individual arrays
%----------------------------------------------------------
% load(infilepath);              % This and the following line commented-out 03-Aug-2017
% fileTemp = eval(rootName);
fileTemp = load(infilepath);     % this line added Thu-03-Aug-2017
n = size(fileTemp,1);
fixed    = 0;  % number of fixed stations
floating = 0;  % number of floating stations
dirns    = 0;  % number of observed directions
dists    = 0;  % number of observed distances
cbrgs    = 0;  % number of constrained bearings
cdists   = 0;  % number of constrained distances
cangles  = 0;  % number of constrained angles
for k = 1:n
    code = fileTemp(k,1);
    if code == 1
        j = fixed+floating+1;
        STATION(j).serial = fileTemp(k,2);
        STATION(j).east   = fileTemp(k,3);
        STATION(j).north  = fileTemp(k,4);
        STATION(j).code   = code;
        fixed = fixed+1;
    end    
    if code == 2
        j = fixed+floating+1;
        STATION(j).serial = fileTemp(k,2);
        STATION(j).east   = fileTemp(k,3);
        STATION(j).north  = fileTemp(k,4);
        STATION(j).code   = code;
        floating = floating+1;
    end    
    if code == 3
        j = dirns+dists+cbrgs+cdists+cangles+1;
        OBS(j).at    = fileTemp(k,2);
        OBS(j).to    = fileTemp(k,3);
        OBS(j).left  = 0;
        OBS(j).right = 0;
        OBS(j).val   = decdeg(fileTemp(k,4));
        OBS(j).sigma = fileTemp(k,5);
        OBS(j).code  = code;
        dirns = dirns+1;
    end    
    if code == 4
        j = dirns+dists+cbrgs+cdists+cangles+1;
        OBS(j).at    = fileTemp(k,2);
        OBS(j).to    = fileTemp(k,3);
        OBS(j).left  = 0;
        OBS(j).right = 0;
        OBS(j).val   = fileTemp(k,4);
        OBS(j).sigma = fileTemp(k,5);
        OBS(j).code  = code;
        dists        = dists+1;
    end    
    if code == 5
        j = dirns+dists+cbrgs+cdists+cangles+1;
        OBS(j).at    = fileTemp(k,2);
        OBS(j).to    = fileTemp(k,3);
        OBS(j).left  = 0;
        OBS(j).right = 0;
        OBS(j).val   = decdeg(fileTemp(k,4));
        OBS(j).sigma = 0.0;
        OBS(j).code  = code;
        cbrgs        = cbrgs+1;
    end    
    if code == 6
        j = dirns+dists+cbrgs+cdists+cangles+1;
        OBS(j).at    = fileTemp(k,2);
        OBS(j).to    = fileTemp(k,3);
        OBS(j).left  = 0;
        OBS(j).right = 0;
        OBS(j).val   = fileTemp(k,4);
        OBS(j).sigma = 0.0;
        OBS(j).code  = code;
        cdists       = cdists+1;
    end    
    if code == 7
        j = dirns+dists+cbrgs+cdists+cangles+1;
        OBS(j).at    = fileTemp(k,2);
        OBS(j).to    = 0;
        OBS(j).left  = fileTemp(k,3);
        OBS(j).right = fileTemp(k,4);
        OBS(j).val   = decdeg(fileTemp(k,5));
        OBS(j).sigma = 0.0;
        OBS(j).code  = code;
        cangles      = cangles+1;
    end    
end    
JOB(1).fixed    = fixed;
JOB(1).floating = floating;
JOB(1).dirns    = dirns;
JOB(1).dists    = dists;
JOB(1).cbrgs    = cbrgs;
JOB(1).cdists   = cdists;
JOB(1).cangles  = cangles;

return

