      MODULE s_EXTAMB

      IMPLICIT NONE

#include "COM_CPU.inc"

      CONTAINS

C*
      SUBROUTINE EXTAMB(NFIL,FILNAM,FILTYP,AMBLOD,AMBID,SATSYS)
CC
CC NAME       :  EXTAMB
CC
CC PURPOSE    :  EXTRACT ESSENTIAL INFORMATION CONCERNING AMBIGUITYG
CC               RESOLUTION (QIF,SIGMA) FROM GPSEST OUTPUT FILES
CC
CC PARAMETERS :
CC         IN :  NFIL   : TOTAL NUMBER OF OUTPUT FILES        I*4
CC               FILNAM(I),I=1,..,NFIL: LIST OF FILE NAMES    CH*32(*)
CC               FILTYP(I),I=1,..,NFIL: OUTPUT FILE INDEX     I*4(*)
CC                        = 0: BAD OUTPUT FILE
CC                        = 1: GPSEST
CC                        = 2: ADDNEQ
CC                        = 3: ADDNEQ2
CC                        =11: GPSEST  (V5.0)
CC                        =13: ADDNEQ2 (V5.0)
CC               AMBLOD : GNSS-WISE OUTPUT                 LOGICAL1
CC                        = TRUE  : YES
CC                        = FALSE : NO
CC               AMBID  : #AR_ID                              CH*3
CC               SATSYS : GNSS SELECTION                  t_satsys
CC               ESTXTR : File content information        t_estxtr(*)
CC
CC REMARKS    :  ---
CC
CC AUTHOR     :  S.SCHAER
CC
CC CREATED    :  16-Jan-1996
CC
CC CHANGES    :  17-Jan-1996 SS: PRINT FINAL QIF STATISTICS
CC               18-Jan-1996 SS: PRINT NUMBER OF FILES
CC               04-Apr-1996 MR: PUT LABEL 310 INTO DO LOOP
CC               05-Jun-1996 LM: DATA STATEMENT OUT OF ORDER
CC               11-Jan-1997 MR: ALLOW TO EXTRACT SIGMA STRATEGY
CC               15-Jan-1997 SS: BOTH OUTPUT FORMATS MODIFIED
CC               26-Feb-1997 SS: HANDLE MELBOURNE-WUEBBENA FILES
CC               30-Oct-1997 SS: SIGMA OF ZERO-DIFFERENCE OBS
CC               10-May-1999 SS: ALLOW MULTI-BASELINE FILES
CC               07-Feb-2000 HU: WRITE AMB FRAC PARTS ONLY IF FILE OPEN
CC               14-Mar-2000 SS: CONSIDER ADDNEQ2 OUTPUT FILES
CC               20-Dec-2000 SS: VARIABLE "TYPOUT" REMOVED
CC               06-Aug-2001 DS: EXTRACT RECTYP
CC               27-Mar-2002 RD: ERROR HANDLING FOR SIGMA=***
CC               04-Jul-2002 SS: ESTIMATE ORBIT ACCURACY
CC               17-Sep-2002 SS: HANDLE CASE WITH NO UNKNOWN AMB
CC               11-Dec-2002 CU: GPSEST V5.0 OUTPUT
CC               08-Sep-2003 HU: RECNAM CHR16 -> CHR20
CC               22-Jan-2004 HB: COMMENT write(*,*)-statement
CC               21-Jun-2005 MM: COMLFNUM.inc REMOVED, m_bern ADDED
CC               23-Jun-2005 MM: IMPLICIT NONE AND DECLARATIONS ADDED
CC               29-Aug-2007 SS: HANDLE GLONASS AMBIGUITY RESOLUTION
CC               31-Aug-2007 MK/SS: HANDLE KINEMATIC CASE WITHOUT ERROR
CC               31-Aug-2007 HU/SS: ERROR HANDLING IMPROVED, USE SYMING
CC               15-Feb-2008 SL: EXTRACT BASELINE LENGTH W/O SLOPE TAB
CC               26-Mar-2010 RD: REPLACE MAXFIL BY NFIL
CC               24-Nov-2010 SL: STATION NAMES TO OUTPUT, M_BERN W/ ONLY
CC               17-Feb-2011 MM: NEW OPTIONS (AMBLOD, AMBID)
CC               10-Jul-2012 RD: USE SYMINVG INSTEAD OF SYMING
CC               27-Jun-2013 SC: use FILENAMELENGTH
CC               16-Jul-2013 SC: use sr PRTFLNAM
CC               15-Apr-2014 LP: USE MAXSYS,SATSYS; CHANGE AMBLOD TO LOGICAL
CC               01-Dec-2016 AM: Add srName, call cpu_enter and cpu_exit
CC               01-Mar-2017 AV/ST: Fixed format statements
CC               07-Apr-2017 ST: Move IMPLICIT NONE to module scope
CC               07-Apr-2017 ST: Add #include "COM_CPU.inc"
CC               07-Apr-2017 ST: Add IF(srfDebug) in front of CALL cpu_
CC               27-Sep-2017 SS: Enabled ZERO_AR
CC               27-Sep-2017 SS: Corrected NRED if any non-GPS involved
CC               27-Sep-2017 SS: Corrected format statements using srName
CC               29-Sep-2017 SS: Supported special counting mode to count
CC                               only WL/L5-resolved NL/L3 ambiguities
CC                               (if AMBID set to NLR, or L3R)
CC               15-Mar-2018 SS: Properly initialized IFREQ0 (and more)
CC               04-Jun-2018 SS: Allowed N?R for ZERO_AR testing purposes
CC               24-Oct-2019 AV: Support new GPSEST output using ADDNEQ2 output
CC                               Use t_estxtr
CC               03-Dec-2019 AV: Bugfix jump statement
CC               24-Jan-2020 AV: Bugfix ipgm test (13 -> 3)
CC               05-Aug-2021 AV: Support new ambiguity listing format
CC                               Add GPS Quarter Cycle flag
CC                               Add GLONASS freq. behaviour flag
CC               06-Aug-2021 RD: Adjust the format for "Tot"-summary lines
CC               06-Aug-2021 RD: Remove unused variables; clean integer<->real tests
CC               13-Oct-2021 RD: Extract station name also for LEOs
CC               14-Sep-2022 SS: Corrected typo and renamed QTR to QCB
CC               05-Feb-2024 EB: >100% amb. resolved (QIF, WL) -> corrected
CC                               number of reference ambiguities in summary
CC
CC COPYRIGHT  :  ASTRONOMICAL INSTITUTE
CC      1996     UNIVERSITY OF BERN
CC               SWITZERLAND
CC
C*
      USE m_bern,   ONLY: lfnErr, lfn001, lfn002, lfnLoc,
     1                    fileNameLength, fileExtLength, staNameLength
      USE m_cpu,    ONLY: cpu_enter, cpu_exit
      USE m_global, ONLY: G_SVNSYS,MAXSYS,t_satsys
      USE f_ikf
      USE s_opnfil
      USE s_opnerr
      USE s_solve
      USE s_syminvg
      USE s_fparse
      USE s_exitrc
      USE s_gtflna
      USE f_prtflnam

C Local parameters
C ----------------
      CHARACTER(LEN=*), PARAMETER :: srName = 'extamb'

C DECLARATIONS INSTEAD OF IMPLICIT
C --------------------------------
      INTEGER*4 I     , IAR   , IAR0  , IFIL  , IFREQ , IFREQ0, IFREQ1,
     1          IL    , IMUL  , IOSTAT, IPGM  , IPRAMB, IPRQIF, IRC   ,
     1          IRCAMB, IRCQIF, NSING , ITER  , JL    , NAMB1 ,
     1          NBAST , NDOF  , NFIL  , NOBS  , IONE  , NNON  ,
     1          ISVN1 , ISVN2 , NRED  , IZD   , IZD0  , IRCODE,io1,io2

      REAL*8    FPL3  , FPL5  , FPLR  , RMS1  , RMS1T , RMS2  ,
     1          RMS2T , RMSL3 , RMSL5 , RMSTOT, SLOPE , SLOPET, XFAC  ,
     1          XOBS  , XRAT

      CHARACTER*133 LINE
      CHARACTER(LEN=fileNameLength), DIMENSION(:)      :: FILNAM
      CHARACTER(LEN=fileNameLength)                    :: FILQIF
      CHARACTER(LEN=fileNameLength)                    :: FILAMB
      CHARACTER(LEN=fileNameLength)                    :: FILOUT
      CHARACTER(LEN=fileNameLength)                    :: BASFIL
      CHARACTER(LEN=fileNameLength)        :: NODE,DEVICE,DIRNAM
      CHARACTER(LEN=fileExtLength)         :: EXT,VER
      CHARACTER(LEN=staNameLength), DIMENSION(2)       :: stName

      CHARACTER*20  RECTYP(2)
      CHARACTER*8   BASNAM,WUBNAM
      CHARACTER*3   AMBID
      CHARACTER*4   staNam4(2)
      CHARACTER*7   AMBSTR
      CHARACTER*3   FRC
      CHARACTER*1   GLOFRQ, TMPGLO, ALLGLO
      CHARACTER*1   GPSQCB, TMPGPS, ALLGPS
      CHARACTER(LEN=MAXSYS) ::  AMBSYS,SYSSTR

      REAL*8        ANOR(3),BNOR(2)
      REAL*8        XSOL(2),XOA(2,2)
      REAL*8        XOSTA(3,2),SLOPES(MAXSYS)
      REAL*8        RMS1S(MAXSYS),RMS2S(MAXSYS)
      REAL*8        FPL3M(MAXSYS),FPL3MT(MAXSYS)
      REAL*8        FPL3R(MAXSYS),FPL3RT(MAXSYS)
      REAL*8        FPL5M(MAXSYS),FPL5MT(MAXSYS)
      REAL*8        FPL5R(MAXSYS),FPL5RT(MAXSYS)
      REAL*8        XSOLV(MAXSYS),NSOLV(MAXSYS)
      REAL*8        XSOLVT(MAXSYS),NSOLVT(MAXSYS)
      REAL*8        FPL5RA,FPL3RA,FPL5RAT,FPL3RAT
      REAL*8        NSOLA,NSOLAT,XSOLA,XSOLAT

      INTEGER*4     FILTYP(*),PARFLG(2)
      INTEGER*4     NAMB(MAXSYS,2),NAMBT(MAXSYS,2),NAMBA(2),NAMBAT(2)
      INTEGER*4     NREF(MAXSYS,2)
      INTEGER*4     ISYS,IPART,ISYSH
      INTEGER*4     NBAS(MAXSYS)
      INTEGER*4     IVERS, IREF
      INTEGER*4     ntref(2)

      LOGICAL       AMBLOD

      TYPE(t_satsys)                :: satsys

C DEBUG: entry
C ------------
      IF (srfDebug) CALL cpu_enter(srName)

C RETURN, IF NEITHER QIF STATISTICS NOR FRACTIONAL PARTS REQUESTED
C ----------------------------------------------------------------
      CALL GTFLNA(0,'QIFOUT ',FILQIF,IRCQIF)
      CALL GTFLNA(0,'AMBOUT ',FILAMB,IRCAMB)

      IF (IRCQIF.EQ.1.AND.IRCAMB.EQ.1) GOTO 999

C INITIALIZE LINE IDENTIFIER
C --------------------------
      AMBSTR = '#AR'
      IF (LEN_TRIM(AMBID) > 0) AMBSTR = TRIM(AMBSTR)//"_"//TRIM(AMBID)

C INITIALIZE FLAGS FOR HEADER PRINTING
C ------------------------------------
      IPRQIF=1
      IPRAMB=1

      IAR0=0
      IFREQ0=0
      IFREQ=0
      IZD0=-1

C OPEN SUMMARY FILES
C ------------------
      IF (IRCQIF.EQ.0) THEN
        CALL OPNFIL(LFN001,FILQIF,'UNKNOWN','FORMATTED',
     1    ' ',' ',IOSTAT)
        CALL OPNERR(LFNERR,LFN001,IOSTAT,FILQIF,srName)
      ENDIF
      IF (IRCAMB.EQ.0) THEN
        CALL OPNFIL(LFN002,FILAMB,'UNKNOWN','FORMATTED',
     1    ' ',' ',IOSTAT)
        CALL OPNERR(LFNERR,LFN002,IOSTAT,FILAMB,srName)
      ENDIF

C LOOP OVER ALL GPSEST OUTPUT FILES
C ---------------------------------
      FPL5MT = 0.D0
      FPL5RT = 0.D0
      FPL3MT = 0.D0
      FPL3RT = 0.D0

      NAMB   =  0
      NAMBT  = 0
      NAMBAT = 0

      RMS1T  = 0.D0
      RMS2T  = 0.D0
      SLOPET = 0.D0
      RMS1S  = 0.D0
      RMS2S  = 0.D0
      SLOPES = 0.d0

      NBAST  = 0
      NBAS   = 0

C INITIALIZE VARIABLES CONCERNING ORBIT ACCURACY ESTIMATION
      DO I=1,3
        ANOR(I)=0.D0
      ENDDO
      DO I=1,2
        BNOR(I)=0.D0
      ENDDO
      RMSTOT=0.D0
      NOBS=0

      DO 110 IFIL=1,NFIL
        DO ISYSH=1,MAXSYS
          AMBSYS(ISYSH:ISYSH)=' '
        ENDDO
        NAMB  = 0
        NAMBA = 0
        NREF  = 0
        GLOFRQ=' '
        GPSQCB=' '

C SKIP BAD OUTPUT FILE
        IPGM=FILTYP(IFIL)
        IF (IPGM.NE.1.AND.IPGM.NE.11.AND.IPGM.NE.3) GOTO 110

C OPEN GPSEST OUTPUT FILE
        FILOUT=FILNAM(IFIL)
        CALL OPNFIL(LFNLOC,FILOUT,'OLD','FORMATTED',
     1    'READONLY',' ',IOSTAT)
        CALL OPNERR(LFNERR,LFNLOC,IOSTAT,FILOUT,srName)

C EXTRACT BASELINE NAME
        IRCODE=1
        DO IL=1,100000
          READ(LFNLOC,'(A)',END=310) LINE
          IF (LINE(1:30).EQ.' FILE  OBSERVATION FILE HEADER') GOTO 410
        ENDDO

410     READ(LFNLOC,'(//,7X,A32,44X,A20,2X,A20)') BASFIL,
     1       RECTYP(1),RECTYP(2)

        CALL FPARSE(0,BASFIL,NODE,DEVICE,DIRNAM,BASNAM,EXT,VER,IRC)
        IRCODE=2
        IF (IRC.NE.0) GOTO 310

C CHECK WHETHER FURTHER BASELINES ARE LISTED
        READ(LFNLOC,'(A)') LINE

C HANDLE MELBOURNE-WUEBBENA OBSERVATION FILES
        CALL FPARSE(0,LINE,NODE,DEVICE,DIRNAM,WUBNAM,EXT,VER,IRC)
        IF (BASNAM.EQ.WUBNAM) READ(LFNLOC,'(A)') LINE

C CHECK WHETHER MULTI-BASELINE FILE IS PROCESSED
        IF (LINE.NE.' ') THEN
          WRITE(LFNERR,940) srName, PRTFLNAM(FILOUT,32)
940       FORMAT(/,' ### SR ',A,': MORE THAN ONE BASELINE FOUND',
     1      /,16X,'FILE NAME: ',A,/)
          IMUL=1
          CALL FPARSE(0,FILOUT,NODE,DEVICE,DIRNAM,BASNAM,EXT,VER,IRC)
        ELSE
          IMUL=0
        ENDIF

C STATION NAMES IF NO COORDINATE SECTION IS AVAILABLE (e.g., for LEOs)
        IRCODE=1
        DO IL=1,100000
          READ(LFNLOC,'(A)',END=310) LINE
          IF (LINE(1:30).EQ.' FILE TYP FREQ.  STATION 1     ') EXIT
        ENDDO

        READ(LFNLOC,'(//,17X,A16,1X,A16)') stName(1:2)

C LOOK FOR AR STRATEGY
        IRCODE=3
        DO IL=1,100000
          READ(LFNLOC,'(A)',END=310) LINE
          IF (LINE(1:31).EQ.
     1      ' AMBIGUITY RESOLUTION STRATEGY:') GOTO 210
        ENDDO
210     CONTINUE

        READ(LFNLOC,'(//,A)') LINE
        IF (LINE(1:22).EQ.' QUASI-IONOSPHERE-FREE') THEN
          IAR=4
        ELSEIF (LINE(1:22).EQ.' SIGMA-DEPENDENT AMBIG') THEN
          IAR=3
        ELSE
          IRCODE=4
          GOTO 310
        ENDIF
        IF (IAR0.EQ.0) THEN
          IAR0=IAR
        ELSEIF (IAR.NE.IAR0) THEN
          WRITE(LFNERR,980)
980       FORMAT(/,' *** SR EXTAMB: MIXTURE OF AMBIGUITY RESOLUTION ',
     1      'STRATEGIES NOT ALLOWED',/)
          CALL EXITRC(2)
        ENDIF

C EXTRACT BASELINE LENGTH
        IRCODE=8
        DO IL=1,100000

          READ(LFNLOC,1003,END=310) LINE
          IF(LINE(2:31) .EQ. 'GPS QUARTER-CYCLE PHASE BIASES') THEN
            IF(LINE(34:47).EQ.'NOT CONSIDERED')            GPSQCB = 'N'
            IF(LINE(34:58).EQ.'CONSIDERED (IF INDICATED)') GPSQCB = 'I'
            IF(LINE(34:52).EQ.'CONSIDERED (ALWAYS)')       GPSQCB = 'A'
            IF(LINE(34:53).EQ.'CONSIDERED (ONLY FOR')      GPSQCB = 'S'
          ENDIF
          IF(LINE(13:38).EQ.'GLONASS RESOLUTION BETWEEN') THEN
            IF(LINE(60:65).EQ.'ALWAYS')              GLOFRQ = 'A'
            IF(LINE(60:64).EQ.'NEVER' )              GLOFRQ = 'N'
            IF(LINE(60:77).EQ.'SAME RECEIVER TYPE' ) GLOFRQ = 'T'
            IF(LINE(60:78).EQ.'SAME RECEIVER MODEL') GLOFRQ = 'M'
          ENDIF

1003      FORMAT(A133)
          IF(LINE(1:33).EQ.' NUM  STATION NAME     OBS E/F/C '.OR.
     1       LINE(1:33).EQ.' num  Station name     obs e/f/h ')THEN
            READ(LFNLOC,'(/,6X,A4,23X,F15.4,1X,F15.4,1X,F15.4)',
     1        iostat=io1) staNam4(1),XOSTA(1,1),XOSTA(2,1),XOSTA(3,1)
            IF (staNam4(1).EQ.'    ') staNam4(1) = stName(1)(1:4)
            READ(LFNLOC,'(6X,A4,23X,F15.4,1X,F15.4,1X,F15.4)',
     1        iostat=io2) staNam4(2),XOSTA(1,2),XOSTA(2,2),XOSTA(3,2)
            IF (staNam4(2).EQ.'    ') THEN
              IZD=1
            ELSE
              IZD=0
            ENDIF
            IF (IZD0.EQ.-1) THEN
              IZD0=IZD
            ELSEIF (IZD.NE.IZD0) THEN
              WRITE(LFNERR,981)
981           FORMAT(/,' *** SR EXTAMB: MIXTURE OF DIFFERENCING LEVELS',
     1          ' NOT ALLOWED',/)
              CALL EXITRC(2)
            ENDIF
            IF (IZD.EQ.0) THEN
              SLOPE=DSQRT((XOSTA(1,2)-XOSTA(1,1))**2
     1                   +(XOSTA(2,2)-XOSTA(2,1))**2
     1                   +(XOSTA(3,2)-XOSTA(3,1))**2)
            ELSE
              SLOPE=0D0
            ENDIF
CC            WRITE(LFNERR,1099) XOSTA(1,1)
CC1099        FORMAT(/,' EXTAMB ',F15.4,/)
CC
CC          IF (LINE(8:12).EQ.'| O |') THEN
CC            READ(LINE(13:24),'(F12.4)') SLOPE
CC
            IF (IMUL.EQ.1) SLOPE=0.D0
            SLOPE=SLOPE/1.D3
            SLOPET=SLOPET+SLOPE
            NBAST=NBAST+1
            GOTO 240
          ELSEIF (LINE.EQ.' AMBIGUITY RESOLUTION:') THEN
            NBAST=NBAST+1
            SLOPE=0D0
            GOTO 240
          ENDIF
        ENDDO
240     CONTINUE

C EXTRACT NUMBER OF AMBIGUITIES (BEFORE AR)
        IRCODE=5
        DO IL=1,100000
          READ(LFNLOC,'(A)',END=310) LINE
          IF (LINE(1:15).EQ.' PARAMETER TYPE') THEN
            IRCODE=6
            DO JL=1,100000
              READ(LFNLOC,'(A)',END=310) LINE
              IF (LINE(1:12).EQ.' AMBIGUITIES') THEN
                READ(LINE(51:59),'(I9)') NAMB1
                GOTO 220
              ENDIF
            ENDDO
          ELSEIF (LINE(1:15).EQ.' Parameter type') THEN
            IRCODE=6
            DO JL=1,100000
              READ(LFNLOC,'(A)',END=310) LINE
              IF (LINE(1:12).EQ.' Ambiguities') THEN
                READ(LINE(52:64),'(I13)') NAMB1
                GOTO 220
              ENDIF
            ENDDO
          ENDIF
        ENDDO
220     CONTINUE

C EXTRACT RMS OF UNIT WEIGHT (BEFORE AR)
        IRCODE=7
        DO IL=1,100000
          READ(LFNLOC,'(A)',END=310) LINE
          IF (LINE(1:37).EQ.
     1      ' A POSTERIORI SIGMA OF UNIT WEIGHT  :') THEN
            READ(LINE(39:47),'(F9.4)',ERR=225) RMS1
            RMS1=RMS1*1.D3
            RMS1T=RMS1T+RMS1**2
            GOTO 230
225         RMS1=1D6
            GOTO 230
          ELSEIF (LINE(1:37).EQ.
     1      ' A posteriori RMS of unit weight     ') THEN
            READ(LINE(39:51),'(F13.5)',ERR=226) RMS1
            RMS1=NINT(RMS1*10000)*1.D-1
            RMS1T=RMS1T+RMS1**2
            GOTO 230
226         RMS1=1D6
            GOTO 230
          ENDIF
        ENDDO
230     CONTINUE

C EXTRACT FRACTIONAL PARTS AND NUMBER OF AMBIGUITIES (AFTER AR)
        FPL5M=0.D0
        FPL5R=0.D0
        FPL3M=0.D0
        FPL3R=0.D0

C HANDLE CASE WITH NO UNKNOWN AMBIGUITIES
        IF (NAMB1.EQ.0) THEN
          RMS2=RMS1
          RMS2T=RMS2T+RMS2**2

          WRITE(LFNERR,925) srName, PRTFLNAM(FILOUT,32)
925       FORMAT(/,' ### SR ',A,': NO UNKNOWN AMBIGUITIES FOUND',
     1      /,16X,'FILE NAME: ',A,/)

          GOTO 270
        ENDIF

        IRCODE=9
        IPART=1
        DO 250 IL=1,100000
          READ(LFNLOC,'(A)',END=310) LINE
          IVERS = 0
          IF (LINE(1:34).EQ. ' AMBI  FILE SAT. EPOCH FRQ WLF CLU') THEN
            IVERS = 1
          ELSEIF(LINE(1:37).EQ.' AMBI  FILE SAT. S_EPO  L_EPO FRQ WLF')
     1          THEN
            IVERS = 2
          ENDIF

          IF(IVERS>0) THEN
            READ(LFNLOC,'(/)',END=310)
            IRCODE=10
            IREF = 0
            DO JL=1,100000
              READ(LFNLOC,'(A)',END=310) LINE
              IF (LINE.EQ.' ') THEN
                IF(IREF .EQ. 0) THEN
                  IREF = 1
                  CYCLE
                ELSE
                  IPART= 2
                  GOTO 250
                ENDIF
              ENDIF

              IF(IVERS==1)THEN
                READ(LINE,'(10X,I5,7X,I3,51X,A3)') ISVN1,IFREQ,FRC
              ELSEIF(IVERS==2)THEN
                READ(LINE,'(10X,I5,7X,7X,I3,51X,A3)') ISVN1,IFREQ,FRC
              ELSE
                CYCLE
              ENDIF

              IF(IREF .EQ. 0) THEN
                IF (FRC.EQ.'.  ') CYCLE
C TO COUNT ONLY NL/L3 AMBIGUITIES FOR WHICH WL/L5 WAS RESOLVED
                IF (AMBID.EQ.'NLR'.OR.AMBID.EQ.'L3R') THEN
                  IF (FRC.EQ.'   ') CYCLE
                ENDIF
                IF (AMBID(1:1).EQ.'N'.AND.AMBID(3:3).EQ.'R') THEN
                  IF (FRC.EQ.'   ') CYCLE
                ENDIF
              ENDIF
              ISYS=ISVN1/100+1
              IF (ISYS.GT.MAXSYS) THEN
                WRITE(LFNERR,'(/,3A,/,2A,/)')
     1               " *** SR ",srName,": UNSUPPORTED GNSS FOUND",
     1               "                GNSS: ",G_SVNSYS(ISYS-1)
                CALL EXITRC(2)
              ENDIF
              AMBSYS(ISYS:ISYS)=G_SVNSYS(ISYS-1)
              NAMB(ISYS,IPART)=NAMB(ISYS,IPART)+1
              IF(IREF.EQ.1) THEN
                NREF(ISYS,IPART)=NREF(ISYS,IPART)+1
              ENDIF

              IF (IFREQ0.EQ.0 .AND. IFREQ.NE.3) THEN
                IF (IFREQ.NE.0) IFREQ0=IFREQ
CC              ELSEIF (IFREQ.NE.IFREQ0 .AND. IFREQ.NE.3 .AND.
CC     1                IAR.NE.4 .AND. IFREQ0.NE.-1) THEN
              ELSEIF (IFREQ.NE.IFREQ0 .AND. IFREQ.NE.3 .AND.
     1                IFREQ0.NE.-1) THEN
                IF (IAR.NE.4) WRITE(LFNERR,985)
985             FORMAT(/,' ### SR EXTAMB: MIXTURE OF FREQUENCIES ',
     1                   'FOUND',/)
                IFREQ0=-1
              ENDIF
            ENDDO
          ELSEIF (LINE(1:32).EQ.' AMBIGUITY RESOLUTION ITERATION:') THEN
            IRCODE=11
            READ(LINE(33:38),'(I6)') ITER
            IF (IAR.EQ.4) THEN
              READ(LFNLOC,'(/////)',END=310)
            ELSEIF (IAR.EQ.3) THEN
              READ(LFNLOC,'(////)',END=310)
            ELSE
              GOTO 250
            ENDIF
            IRCODE=12
            DO JL=1,100000
              READ(LFNLOC,'(A)',END=310) LINE
              IF (LINE.EQ.' ') GOTO 250
              IF (IAR.EQ.4) THEN
                READ(LINE(56:89),'(3F8.3,2I5)')
     1            FPL5,FPL3,RMSL3,ISVN1,ISVN2

C "RMSL5" NOT GIVEN
                RMSL5=0.D0
              ELSEIF (IAR.EQ.3) THEN
                READ(LINE,'(5X,I4,35X,F16.3,F8.3,2I5)')
     1            IFREQ,FPL5,RMSL5,ISVN1,ISVN2
                FPL5=FPL5-DNINT(FPL5)
                FPL3=0.D0
                RMSL3=0.D0
              ELSE
                GOTO 250
              ENDIF
C WRITE HEADER LINES OF AMBIGUITY FRACTIONAL LISTING
              IF (IRCAMB.EQ.0.AND.IPRAMB.EQ.1) THEN
                IF (IZD.EQ.0) THEN
                  WRITE(LFN002,920) IAR
920               FORMAT(' Ambiguity Resolution Strategy ',I1,/,
     1              1X,31('-'),//,
     1              ' File     Sta1 Sta2    Length  Freq.  Amb.Frac.',
     1              '   Amb.RMS Iter. Sat1 Sat2',/,
     1              '                        (km)           (Cycles)',
     1              '   (Cycles)',/,1X,72('-'))
                ELSE
                  WRITE(LFN002,921) IAR
921               FORMAT(' Ambiguity Resolution Strategy ',I1,/,
     1              1X,31('-'),//,
     1              ' File     Station              Freq.  Amb.Frac.',
     1              '   Amb.RMS Iter. Sat1 Sat2',/,
     1              '                                       (Cycles)',
     1              '   (Cycles)',/,1X,72('-'))
                ENDIF
                IPRAMB=0
              ENDIF

C WRITE FRACTIONAL PARTS
              IF (IRCAMB.EQ.0) THEN
                IF (IAR.EQ.4) THEN
                  WRITE(LFN002,950) PRTFLNAM(BASNAM,32),staNam4(1),
     1              staNam4(2),SLOPE,5,FPL5,RMSL5,ITER,ISVN1,ISVN2
                  WRITE(LFN002,950) PRTFLNAM(BASNAM,32),staNam4(1),
     1              staNam4(2),SLOPE,3,FPL3,RMSL3,ITER,ISVN1,ISVN2
                ELSEIF (IAR.EQ.3) THEN
                  IF (IZD.EQ.0) THEN
                    WRITE(LFN002,950) PRTFLNAM(BASNAM,32),staNam4(1),
     1                staNam4(2),SLOPE,IFREQ,FPL5,RMSL5,ITER,ISVN1,ISVN2
                  ELSE
                    WRITE(LFN002,951) PRTFLNAM(BASNAM,32),staNam4(1),
     1                                 IFREQ,FPL5,RMSL5,ITER,ISVN1,ISVN2
                  ENDIF
                ENDIF
950             FORMAT(1X,A8,1X,A4,1X,A4,1X,F10.3,I5,2F11.3,I6,2I5)
951             FORMAT(1X,A8,1X,A4,16X,           I5,2F11.3,I6,2I5)
              ENDIF
CC C
CC C UPDATE STATISTICS
CC               FPL5=DABS(FPL5)
CC               FPL3=DABS(FPL3)
CC C
CC               IF (FPL5.GT.FPL5M) FPL5M=FPL5
CC               IF (FPL3.GT.FPL3M) FPL3M=FPL3
CC               FPL5R=FPL5R+FPL5**2
CC               FPL3R=FPL3R+FPL3**2
CC C
CC               IF (FPL5.GT.FPL5MT) FPL5MT=FPL5
CC               IF (FPL3.GT.FPL3MT) FPL3MT=FPL3
CC               FPL5RT=FPL5RT+FPL5**2
CC               FPL3RT=FPL3RT+FPL3**2

C UPDATE STATISTICS FOR EACH GNSS
              ISYS=ISVN1/100+1
              FPL5=DABS(FPL5)
              FPL3=DABS(FPL3)

              IF (FPL5.GT.FPL5M(ISYS)) FPL5M(ISYS)=FPL5
              IF (FPL3.GT.FPL3M(ISYS)) FPL3M(ISYS)=FPL3
              FPL5R(ISYS)=FPL5R(ISYS)+FPL5**2
              FPL3R(ISYS)=FPL3R(ISYS)+FPL3**2

              IF (FPL5.GT.FPL5MT(ISYS)) FPL5MT(ISYS)=FPL5
              IF (FPL3.GT.FPL3MT(ISYS)) FPL3MT(ISYS)=FPL3
              FPL5RT(ISYS)=FPL5RT(ISYS)+FPL5**2
              FPL3RT(ISYS)=FPL3RT(ISYS)+FPL3**2
            ENDDO
          ELSEIF (LINE(1:15).EQ.' PARAMETER TYPE' .OR.
     1            LINE(1:15).EQ.' Parameter type') THEN
            IRCODE=13
            GOTO 260
          ENDIF
250     CONTINUE
260     CONTINUE

C EXTRACT RMS OF UNIT WEIGHT (AFTER AR)
        IRCODE=14
        DO IL=1,100000
          READ(LFNLOC,'(A)',END=310) LINE
          IF (LINE(1:37).EQ.
     1      ' A POSTERIORI SIGMA OF UNIT WEIGHT  :') THEN
            READ(LINE(39:47),'(F9.4)',ERR=265) RMS2
            RMS2=RMS2*1.D3
            RMS2T=RMS2T+RMS2**2
            GOTO 270
265         RMS2=1D6
            GOTO 270
          ELSEIF (LINE(1:37).EQ.
     1      ' A posteriori RMS of unit weight     ') THEN
            READ(LINE(39:51),'(F13.5)',ERR=266) RMS2
            RMS2=NINT(RMS2*10000)*1.D-1
            RMS2T=RMS2T+RMS2**2
            GOTO 270
266         RMS2=1D6
            GOTO 270
          ENDIF
        ENDDO
270     CONTINUE

C CHECK WHETHER NON-GPS AMBIGUITIES INVOLVED
        NNON=0
        NTREF(:) = NREF(1,:)
        DO ISYS=2,MAXSYS
          IF (NAMB(ISYS,1).GT.0) NNON=NNON+1
          NTREF(1) = NTREF(1) + NREF(ISYS,1)
          NTREF(2) = NTREF(2) + NREF(ISYS,2)
        ENDDO

C REDUCE NUMBER OF AMBIGUITIES IN CASE OF GLONASS (OR GNSS)
        IF(NTREF(1) .EQ. 0) THEN
          IF (IZD.EQ.0) THEN
            NRED=1
            IF (IFREQ0.EQ.-1) NRED=2
            IF (NNON.EQ.0) NRED=0
          ELSE
            NRED=1
          ENDIF

          DO ISYS=1,MAXSYS
            IF (NAMB(ISYS,2).GT.0 .AND. NRED.GT.0) THEN
              IF (NAMB(ISYS,2).GE.NRED) THEN
                NAMB(ISYS,:)=NAMB(ISYS,:)-NRED
              ELSE
                NAMB(ISYS,1)=NAMB(ISYS,1)-NAMB(ISYS,2)
                NAMB(ISYS,2)=0
                WRITE(LFNERR,'(/,3A,/,2A,/,2A,/)')
     1            " ### SR ",srName,": REDUCTION PROBLEM",
     2            "                FILENAME: ",PRTFLNAM(BASNAM,32),
     3            "                GNSS    : ",G_SVNSYS(ISYS-1)
                CALL EXITRC(2)
              ENDIF
            ENDIF
          ENDDO
        ELSE

          DO ISYS=1,MAXSYS
            IF(NAMB(iSYS,1) .EQ. 0) CYCLE
            IF(NREF(ISYS,2) .EQ. 0) THEN
              NAMB(ISYS,1) = NAMB(ISYS,1) - NREF(ISYS,1)
              NAMB(ISYS,2) = NAMB(ISYS,2) - NREF(ISYS,1)
            ELSE
              NAMB(ISYS,:) = NAMB(ISYS,:) - NREF(ISYS,:)
            ENDIF
          ENDDO
        ENDIF

C SOME STATISTICS
        FPL5RA=SUM(FPL5R)
        FPL3RA=SUM(FPL3R)
        NAMBA(1)=SUM(NAMB(:,1))
        NAMBA(2)=SUM(NAMB(:,2))
        NSOLA=NAMBA(1)-NAMBA(2)
        IF (NAMBA(1).NE.0) THEN
          XSOLA=100.D0*NSOLA/NAMBA(1)
        ELSE
          XSOLA=0.D0
        ENDIF
        IF (NINT(NSOLA).NE.0) THEN
          FPL5RA=DSQRT(FPL5RA/NSOLA)
          FPL3RA=DSQRT(FPL3RA/NSOLA)
        ELSE
          FPL5RA=0.D0
          FPL3RA=0.D0
        ENDIF

        DO ISYS=1,MAXSYS
          NAMBT(ISYS,1:2)=NAMBT(ISYS,1:2)+NAMB(ISYS,1:2)
          NSOLV(ISYS)=NAMB(ISYS,1)-NAMB(ISYS,2)
          IF (NAMB(ISYS,1).NE.0) THEN
            XSOLV(ISYS)=100.D0*NSOLV(ISYS)/NAMB(ISYS,1)
          ELSE
            XSOLV(ISYS)=0.D0
          ENDIF
          IF (NINT(NSOLV(ISYS)).NE.0) THEN
            FPL5R(ISYS)=DSQRT(FPL5R(ISYS)/NSOLV(ISYS))
            FPL3R(ISYS)=DSQRT(FPL3R(ISYS)/NSOLV(ISYS))
          ELSE
            FPL5R(ISYS)=0.D0
            FPL3R(ISYS)=0.D0
          ENDIF
        ENDDO

C WRITE HEADER LINES OF QIF STATISTICS
        IFREQ1=IFREQ0
        IF (IFREQ0.EQ.-1) IFREQ1=1
        IF (IRCQIF.EQ.0.AND.IPRQIF.EQ.1) THEN
          IF (IAR.EQ.4) THEN
            WRITE(LFN001,910)
910         FORMAT(
     1        ' File     Sta1 Sta2    Length     Before     After    ',
     1        'Res  Sys     Max/RMS L5    Max/RMS L3   ',' QCB FRQ ',
     1        'Receiver 1           Receiver 2',/,
     1        '                        (km)    #Amb (mm)  #Amb (mm)  ',
     1        '(%)       (L5 Cycles)   (L3 Cycles)','        '/,
     1        1X,144('-'))
          ELSEIF (IAR.EQ.3) THEN
            IF (IZD.EQ.0) THEN
              WRITE(LFN001,915) IFREQ1,IFREQ1
915           FORMAT(
     1          ' File     Sta1 Sta2    Length     Before     After',
     1          '    Res  Sys     Max/RMS L',I1,'   ',' QCB FRQ ',
     1          'Receiver 1           Receiver 2',/,
     1          '                        (km)    #Amb (mm)  #Amb (mm)',
     1          '  (%)       (L',I1,' Cycles)','        '/,
     1          1X,130('-'))
            ELSE
              WRITE(LFN001,916) IFREQ1,IFREQ1
916           FORMAT(
     1          ' File     Station                 Before     After',
     1          '    Res  Sys     Max/RMS L',I1,'   ',' QCB FRQ ',
     1          'Receiver',/,
     1          '                                #Amb (mm)  #Amb (mm)',
     1          '  (%)       (L',I1,' Cycles)','        ',/,
     1          1X,130('-'))
            ENDIF
          ENDIF
          IPRQIF=0
        ENDIF

C WRITE GNSS-SPECIFIC STATISTICS
        IONE=0
        DO ISYS=1,MAXSYS
          IF (NAMB(ISYS,1).GT.0) THEN
            NBAS(ISYS)=NBAS(ISYS)+1
            SLOPES(ISYS)=SLOPES(ISYS)+SLOPE
            RMS1S(ISYS)=RMS1S(ISYS)+RMS1**2
            RMS2S(ISYS)=RMS2S(ISYS)+RMS2**2
            IONE=IONE+1
          ENDIF
        ENDDO

        ALLGPS='-'
        ALLGLO='-'
        DO ISYS=1,MAXSYS
          IF (.NOT.satsys%usesys(iSys)) CYCLE
          IF (NAMB(ISYS,1).EQ.0) CYCLE
          TMPGPS='-'
          TMPGLO='-'
          IF(ISYS==1) THEN
            TMPGPS = GPSQCB
            ALLGPS = GPSQCB
          ENDIF
          IF(ISYS==2 .AND. GLOFRQ.NE.' ') THEN
            TMPGLO = GLOFRQ
            ALLGLO = GLOFRQ
          ENDIF
          IF ((satsys%rnxchar.EQ.'M').AND.(.NOT.AMBLOD)) CYCLE
          IF ((satsys%rnxchar.EQ.'M').AND.(IONE.EQ.1)) CYCLE

          DO ISYSH=1,MAXSYS
            SYSSTR(ISYSH:ISYSH)=' '
          ENDDO
          SYSSTR(ISYS:ISYS)=AMBSYS(ISYS:ISYS)

          IF (IAR.EQ.4) THEN
            WRITE(LFN001,960)
     1        PRTFLNAM(BASNAM,32),staNam4(1),staNam4(2),
     1        SLOPE,NAMB(ISYS,1),RMS1,
     1        NAMB(ISYS,2),RMS2,XSOLV(ISYS),SYSSTR,FPL5M(ISYS),
     1        FPL5R(ISYS),FPL3M(ISYS),FPL3R(ISYS),TMPGPS,TMPGLO,
     1        RECTYP(1),RECTYP(2),TRIM(AMBSTR)
960         FORMAT(1X,A8,1X,A4,1X,A4,1X,F10.3,1X,2(I5,1X,F4.1,1X),
     1             F5.1,1X,A6,1X,2(2(F6.3,1X)),2(3X,A1),1X,A20,1X,A20,
     2             2X,A)
          ELSEIF (IAR.EQ.3) THEN
            IF (IZD.EQ.0) THEN
              WRITE(LFN001,965)
     1          PRTFLNAM(BASNAM,32),staNam4(1),staNam4(2),SLOPE,
     1          NAMB(ISYS,1),RMS1,
     1          NAMB(ISYS,2),RMS2,XSOLV(ISYS),SYSSTR,FPL5M(ISYS),
     1          FPL5R(ISYS),TMPGPS,TMPGLO,RECTYP(1),RECTYP(2),
     1          TRIM(AMBSTR)
965           FORMAT(1X,A8,1X,A4,1X,A4,1X,F10.3,1X,2(I5,1X,F4.1,1X),
     1               F5.1,1X,A6,1X,2(F6.3,1X),2(3X,A1),1X,A20,1X,A20,
     1               2X,A)
            ELSE
              WRITE(LFN001,966)
     1          PRTFLNAM(BASNAM,32),staNam4(1),
     1          NAMB(ISYS,1),RMS1,
     1          NAMB(ISYS,2),RMS2,XSOLV(ISYS),SYSSTR,FPL5M(ISYS),
     1          FPL5R(ISYS),TMPGPS,TMPGLO,RECTYP(1),RECTYP(2),
     1          TRIM(AMBSTR)
966           FORMAT(1X,A8,1X,A4,17X,2(I5,1X,F4.1,1X),
     1               F5.1,1X,A6,1X,2(F6.3,1X),2(3X,A1),1X,A20,1X,A20,
     2               2X,A)
            ENDIF
          ENDIF
        ENDDO

C WRITE TOTAL STATISTICS
        IF (satsys%rnxchar.EQ.'M') THEN

          IF (IAR.EQ.4) THEN
            WRITE(LFN001,960)
     1        PRTFLNAM(BASNAM,32),staNam4(1),staNam4(2),SLOPE,
     1        NAMBA(1),RMS1,
     1        NAMBA(2),RMS2,XSOLA,AMBSYS,MAXVAL(FPL5M,1),
     1        FPL5RA,MAXVAL(FPL3M,1),FPL3RA,ALLGPS,ALLGLO,RECTYP(1),
     1        RECTYP(2),TRIM(AMBSTR)
          ELSEIF (IAR.EQ.3) THEN
            IF (IZD.EQ.0) THEN
              WRITE(LFN001,965)
     1          PRTFLNAM(BASNAM,32),staNam4(1),staNam4(2),SLOPE,
     1          NAMBA(1),RMS1,
     1          NAMBA(2),RMS2,XSOLA,AMBSYS,MAXVAL(FPL5M,1),
     1          FPL5RA,ALLGPS,ALLGLO,RECTYP(1),RECTYP(2),TRIM(AMBSTR)
            ELSE
              WRITE(LFN001,966)
     1          PRTFLNAM(BASNAM,32),staNam4(1),
     1          NAMBA(1),RMS1,
     1          NAMBA(2),RMS2,XSOLA,AMBSYS,MAXVAL(FPL5M,1),
     1          FPL5RA,GPSQCB,GLOFRQ,RECTYP(1),RECTYP(2),TRIM(AMBSTR)
            ENDIF
          ENDIF
        ENDIF

C VALUE FOR ORBIT ACCURACY
        IF (IAR.EQ.4) THEN
          FPLR=FPL3RA
        ELSEIF (IAR.EQ.3.AND.IFREQ0.EQ.1.AND.IZD.EQ.0) THEN
          FPLR=FPL5RA
        ELSE
          FPLR=0.D0
        ENDIF

        IF (FPLR.GT.0.D0) THEN
          NOBS=NOBS+1
          XOBS=(107.D0*FPLR)**2
          XFAC=(SLOPE/20000.D0)**2
          ANOR(1)=ANOR(1)+1.D0
          ANOR(2)=ANOR(2)+XFAC
          ANOR(3)=ANOR(3)+XFAC**2
          BNOR(1)=BNOR(1)+XOBS
          BNOR(2)=BNOR(2)+XFAC*XOBS
          RMSTOT=RMSTOT+XOBS**2
        ENDIF

C NEXT FILE
        CLOSE(LFNLOC)
        GOTO 110

310     WRITE(LFNERR,930) srName,PRTFLNAM(FILOUT,32),IRCODE
930     FORMAT(/,' ### SR ',A,': INFORMATION NOT FOUND',
     1    /,16X,'FILE NAME: ',A,
     1    /,16X,'POSITION : ',I4,/)
        CLOSE(LFNLOC)

110   CONTINUE

C OVERALL STATISTICS
      FPL5RAT=SUM(FPL5RT)
      FPL3RAT=SUM(FPL3RT)
      NAMBAT(1)=SUM(NAMBT(:,1))
      NAMBAT(2)=SUM(NAMBT(:,2))
      NSOLAT=NAMBAT(1)-NAMBAT(2)
      IF (NAMBAT(1).NE.0) THEN
        XSOLAT=100.D0*NSOLAT/NAMBAT(1)
      ELSE
        XSOLAT=0.D0
      ENDIF
      IF (NINT(NSOLAT).NE.0) THEN
        FPL5RAT=DSQRT(FPL5RAT/NSOLAT)
        FPL3RAT=DSQRT(FPL3RAT/NSOLAT)
      ELSE
        FPL5RAT=0.D0
        FPL3RAT=0.D0
      ENDIF

      DO ISYS=1,MAXSYS
        NSOLVT(ISYS)=NAMBT(ISYS,1)-NAMBT(ISYS,2)
        IF (NAMBT(ISYS,1).NE.0) THEN
          XSOLVT(ISYS)=100.D0*NSOLVT(ISYS)/NAMBT(ISYS,1)
        ELSE
          XSOLVT(ISYS)=0.D0
        ENDIF
        IF (NINT(NSOLVT(ISYS)).NE.0) THEN
          FPL5RT(ISYS)=DSQRT(FPL5RT(ISYS)/NSOLVT(ISYS))
          FPL3RT(ISYS)=DSQRT(FPL3RT(ISYS)/NSOLVT(ISYS))
        ELSE
          FPL5RT(ISYS)=0.D0
          FPL3RT(ISYS)=0.D0
        ENDIF
        IF (NBAS(ISYS).NE.0) THEN
          SLOPES(ISYS)=SLOPES(ISYS)/NBAS(ISYS)
          RMS1S(ISYS)=DSQRT(RMS1S(ISYS)/NBAS(ISYS))
          RMS2S(ISYS)=DSQRT(RMS2S(ISYS)/NBAS(ISYS))
        ELSE
          SLOPES(ISYS)=0.D0
          RMS1S(ISYS)=0.D0
          RMS2S(ISYS)=0.D0
        ENDIF
      ENDDO

      IF (NBAST.NE.0) THEN
        SLOPET=SLOPET/NBAST
        RMS1T=DSQRT(RMS1T/NBAST)
        RMS2T=DSQRT(RMS2T/NBAST)
      ELSE
        SLOPET=0.D0
        RMS1T=0.D0
        RMS2T=0.D0
      ENDIF

C PRINT FINAL STATISTIC
C ---------------------
      IF (IRCQIF.EQ.0) THEN

C GNSS-SPECIFIC STATISTIC
        IONE=0
        DO ISYS=1,MAXSYS
          IF (NAMBT(ISYS,1).GT.0) IONE=IONE+1
        ENDDO

        IF (IAR.EQ.4) THEN
          WRITE(LFN001,'(1X,144("-"))')
        ELSE
          WRITE(LFN001,'(1X,130("-"))')
        ENDIF
        DO ISYSH=1,MAXSYS
          AMBSYS(ISYSH:ISYSH)=' '
        ENDDO
        DO ISYS=1,MAXSYS
          IF (NAMBT(ISYS,1).EQ.0) CYCLE
          AMBSYS(ISYS:ISYS)=G_SVNSYS(ISYS-1)
          IF (.NOT.satsys%usesys(iSys)) CYCLE
          IF ((satsys%rnxchar.EQ.'M').AND.(.NOT.AMBLOD)) CYCLE
          IF ((satsys%rnxchar.EQ.'M').AND.(IONE.EQ.1)) CYCLE

          DO ISYSH=1,MAXSYS
            SYSSTR(ISYSH:ISYSH)=' '
          ENDDO
          SYSSTR(ISYS:ISYS)=G_SVNSYS(ISYS-1)

          IF (IAR.EQ.4) THEN
            WRITE(LFN001,970)
     1        NBAS(ISYS),SLOPES(ISYS),NAMBT(ISYS,1),RMS1S(ISYS),
     1        NAMBT(ISYS,2),RMS2S(ISYS),XSOLVT(ISYS),SYSSTR,
     1        FPL5MT(ISYS),FPL5RT(ISYS),FPL3MT(ISYS),FPL3RT(ISYS),
     1        TRIM(AMBSTR)
970         FORMAT(1X,'Tot:',I4,5X,5X,1X,F10.3,1X,
     1        2(I5,1X,F4.1,1X),F5.1,1X,A6,1X,2(2(F6.3,1X)),52X,A)
            ELSEIF (IAR.EQ.3) THEN
              IF (IZD.EQ.0) THEN
                WRITE(LFN001,975)
     1            NBAS(ISYS),SLOPES(ISYS),NAMBT(ISYS,1),RMS1S(ISYS),
     1            NAMBT(ISYS,2),RMS2S(ISYS),XSOLVT(ISYS),SYSSTR,
     1            FPL5MT(ISYS),FPL5RT(ISYS),TRIM(AMBSTR)
              ELSE
                WRITE(LFN001,976)
     1            NBAS(ISYS),             NAMBT(ISYS,1),RMS1S(ISYS),
     1            NAMBT(ISYS,2),RMS2S(ISYS),XSOLVT(ISYS),SYSSTR,
     1            FPL5MT(ISYS),FPL5RT(ISYS),TRIM(AMBSTR)
              ENDIF
975         FORMAT(1X,'Tot:',I4,5X,5X,1X,F10.3,1X,
     1        2(I5,1X,F4.1,1X),F5.1,1X,A6,1X,2(F6.3,1X),52X,A)
976         FORMAT(1X,'Tot:',I4,5X,5X,1X,10X,  1X,
     1        2(I5,1X,F4.1,1X),F5.1,1X,A6,1X,2(F6.3,1X),52X,A)
          ENDIF

        ENDDO

C WRITE TOTAL STATISTICS
        IF (satsys%rnxchar.EQ.'M') THEN
          IF (IAR.EQ.4) THEN
            WRITE(LFN001,970)
     1        NBAST,SLOPET,NAMBAT(1),RMS1T,NAMBAT(2),RMS2T,
     1        XSOLAT,AMBSYS,MAXVAL(FPL5MT,1),FPL5RAT,MAXVAL(FPL3MT,1),
     1        FPL3RAT,TRIM(AMBSTR)
          ELSEIF (IAR.EQ.3) THEN
            IF (IZD.EQ.0) THEN
              WRITE(LFN001,975)
     1          NBAST,SLOPET,NAMBAT(1),RMS1T,NAMBAT(2),RMS2T,
     1          XSOLAT,AMBSYS,MAXVAL(FPL5MT),FPL5RAT,TRIM(AMBSTR)
            ELSE
              WRITE(LFN001,976)
     1          NBAST,       NAMBAT(1),RMS1T,NAMBAT(2),RMS2T,
     1          XSOLAT,AMBSYS,MAXVAL(FPL5MT),FPL5RAT,TRIM(AMBSTR)
            ENDIF
          ENDIF
        ENDIF
        WRITE(LFN001,'(/)')

C PRINT ESTIMATED ORBIT ACCURACY
        NDOF=NOBS-2
        IF (NDOF.GT.0) THEN
          CALL SYMINVG(2,ANOR,0,NSING,PARFLG)
          CALL SOLVE(2,ANOR,BNOR,XSOL)
          DO I=1,2
            RMSTOT=RMSTOT-BNOR(I)*XSOL(I)
          ENDDO
          RMSTOT=DSQRT(RMSTOT/NDOF)
          DO I=1,2
            IF (XSOL(I).GT.0.D0) THEN
              XOA(1,I)=DSQRT(XSOL(I))
              XOA(2,I)=RMSTOT*DSQRT(ANOR(IKF(I,I)))/(2.D0*XOA(1,I))
            ELSE
              XOA(1,I)=0.D0
              XOA(2,I)=0.D0
            ENDIF
          ENDDO
          IF (XOA(2,2).GT.0.D0) THEN
            XRAT=XOA(1,2)/XOA(2,2)
          ELSE
            XRAT=0.D0
          ENDIF
          IF (XRAT.GT.10.D0) THEN
            WRITE(LFN001,935) (XOA(I,2),I=1,2),(XOA(I,1),I=1,2),
     1        XOA(1,1)/107.D0
935         FORMAT(' Estimated Orbit Accuracy: ',F6.1,'+-',F6.1,' mm',
     1        /,' Basic Noise of L3 Amb   : ',F6.1,'+-',F6.1,' mm / ',
     1        F6.3,' L3 Cycles',/)
          ELSE
            WRITE(LFNERR,955) srName
955         FORMAT(/,' ### SR ',A,': ORBIT ACCURACY NOT ESTIMATED',/)
          ENDIF
        ENDIF

        WRITE(lfn001,'("Legend:")')
        WRITE(lfn001,'(A,A,A,A,A,A)')
     1    ' QCB  consider GPS quarter-cycle bias :',
     1    ' - (not applicable)',
     1    ' N (never)',
     1    ' A (always)',
     1    ' I (indicated)',
     1    ' S (selected)'
        WRITE(lfn001,'(A,A,A,A,A,A,//)')
     1    ' FRQ  GLONASS resolution between freq.:',
     1    ' - (not applicable)',
     1    ' N (never)',
     1    ' A (always)',
     1    ' T (same rcvr. type)',
     1    ' M (same rcvr. model)'

        CLOSE (LFN001)
      ENDIF
      IF (IRCAMB.EQ.0) THEN
        WRITE(LFN002,*)
        CLOSE (LFN002)
      ENDIF

C DEBUG: exit
C -----------
999   IF (srfDebug) CALL cpu_exit(srName)

      RETURN
      END SUBROUTINE extamb

      END MODULE s_EXTAMB
