function status
% Function STATUS is part of program HAVOC, a least squares traverse
% adjustment program.  Function STATUS determines the status of each
% observation

%============================================================================
% Function:  status
%
% 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   4 August   2006
%
% Remarks:
%  This function determines the "status" of an observation in a least squares
%  adjustment.  Observations are either directions (dirns), distances (dists), 
%  constrained bearings (cbrgs), constrained distances (cdists) or
%  constrained angles (cangles).  Each observation is referenced by station
%  "at" and station "to" and in the case of an angle, station "at", station 
%  "left" and station "right".  Stations are either fixed (code = 1) or
%  floating (code = 2).  The status of each observation is given by the
%  STATUS TABLE
%
%  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.
%
%  The STATUS TABLE for directions, distances, constrained bearings and
%  constrained distances is
%
%              STATION
%  STATUS    AT       TO  
%    1    floating  floating
%    2    fixed     floating
%    3    floating  fixed
%    4    fixed     fixed
%
%  STATUS: initially set to 1.
%  RULE:   If AT is a fixed station, add 1 to STATUS.
%          If TO is a fixed station, add 2 to STATUS. 
%
%  The STATUS TABLE for constrained angles is
%
%                    STATION
%  STATUS    AT       LEFT      RIGHT
%    1    floating  floating  floating
%    2    fixed     floating  floating
%    3    floating  fixed     floating
%    4    fixed     fixed     floating
%    5    floating  floating  fixed
%    6    fixed     floating  fixed
%    7    floating  fixed     fixed
%    8    fixed     fixed     fixed
%
%  STATUS: initially set to 1.
%  RULE:   If AT    is a fixed station, add 1 to STATUS.
%          If LEFT  is a fixed station, add 2 to STATUS. 
%          If RIGHT is a fixed station, add 4 to STATUS. 
%
% Variables
%  last      serial no. of last station
%  next      serial no. of next station
%
%  fixed     no. of fixed stations
%  floating  no. of floating stations
%  dirns     no. of observed directions
%  dists     no. of observed distances
%  cbrgs     no. of constrained bearings
%  cdists    no. of constrained distances
%  cangles   no. of constrained angles
%  orients   no. of orientation constants
%  n         total number of observations (n = dirns + dists)
%  u         no. of unknowns (u = 2*floating + orients)
%  c         no. of constraints
%
%============================================================================

global STATION OBS JOB X Z VFACT Qxx               

fixed    = JOB(1).fixed;       % number of fixed stations
floating = JOB(1).floating;    % number of floating stations
dirns    = JOB(1).dirns;       % number of directions
dists    = JOB(1).dists;       % number of distances
cbrgs    = JOB(1).cbrgs;       % number of constrained bearings
cdists   = JOB(1).cdists;      % number of constrained distances
cangles  = JOB(1).cangles;     % number of constrained angles

%
% calculate the status of each observation in the network

for i = 1:(dirns+dists+cbrgs+cdists+cangles)
    status = 1;
    if OBS(i).code < 7
       for j = 1:(fixed+floating)
          if OBS(i).at == STATION(j).serial && STATION(j).code == 1
              status = status+1;
          end    
          if OBS(i).to == STATION(j).serial && STATION(j).code == 1
              status = status+2;
          end    
      end    
   end    
   if OBS(i).code == 7
      for j = 1:(fixed+floating)
          if OBS(i).at    == STATION(j).serial && STATION(j).code == 1
              status = status+1;
          end    
          if OBS(i).left  == STATION(j).serial && STATION(j).code == 1
              status = status+2;
          end    
          if OBS(i).right == STATION(j).serial && STATION(j).code == 1
              status = status+4;
          end    
      end    
   end
   OBS(i).status = status;
end    

% calculate the number of orientation constants in the network
orients = 0;
for i = 1:(dirns+dists+cbrgs+cdists+cangles)
	if OBS(i).code == 3
   next = OBS(i).at;
     if i == 1
       orients = 1;
       last    = next;
     elseif next ~= last
       orients = orients+1;
     end
     last = next;
	end    
end    

JOB(1).orients = orients;
JOB(1).n       = dirns+dists;
JOB(1).u       = (2*floating)+orients;
JOB(1).c       = cbrgs+cdists+cangles;
JOB(1).iter    = 0;

return