SUBROUTINE FKLSMO
*-- Author : S.Burke / J.V. Morris
      SUBROUTINE FKLSMO(JPL,IERR)
**********************************************************************                                        
*                                                                    *                                        
* Kalman Smoothing from plane JPL+1 to plane JPL.                    *                                        
*                                                                    *                                        
* ERROR CONDITIONS;                                                  *                                        
*       IERR =   0  ;  normal termination                            *                                        
*   ->  IERR = 101  ;  no smoothed data at plane JPL+1               *                                        
*   ->  IERR = 102  ;  no projected data at plane JPL+1              *                                        
*       IERR =   5  ;  smoothing already done ... but continue       *                                        
*       IERR =  11  ;  smoothed covariance matrix n.p.d.             *                                        
*       IERR =  12  ;  covariance of smoothed residuals n.p.d.       *                                        
*       IERR = 116  ;  theta > pi/2: reset to pi/4                   *                                        
*       IERR =  17  ;  theta > 1 (warning)                           *                                        
*                                                                    *                                        
*   ->  Fatal errors                                                 *                                        
*                                                                    *                                        
* NB Error 12 is not considered fatal, but the chi-sq will be zero   *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
      IMPLICIT DOUBLE PRECISION (A-H,O-Z)                               
                                                                        
      PARAMETER (IUTIL=0,IROUT=4)                                       
*KEEP,FKECODE.                                                                                                
      PARAMETER (IWARN=0,IFATAL=1,IFPRO=2,IFFLT=3,IFSMO=4,IFPRS=5,      
     &           IFPAS=6,IFPAF=7)                                       
      PARAMETER (IINF1=1,IINF2=2,IINF3=3,IINV=4,IDONE=5)                
      PARAMETER (IICV=6,IMCV=7,IOCV=11,IRCV=12,IOVCV=13,                
     &           ITHGP2=16,ITHG1=17)                                    
      PARAMETER (IFREE=20,IFREE1=30,IFREE2=40,IFREE3=50)                
*KEND.                                                                                                        
                                                                        
*                                                                                                             
* Common block definitions                                                                                    
*                                                                                                             
*KEEP,FKNPL.                                                                                                  
      CHARACTER*5 CKDBG                                                 
      PARAMETER (CKDBG='FKDBG')                                         
      PARAMETER (NPL=72)                                                
      LOGICAL LTRUE,LFIRST,LTRPL,LTRPLD                                 
      DOUBLE PRECISION TRUE,RTRUE,CHITRU,SPRO,CPRO,SFIL,CFIL            
     &,                SSMT,CSMT,SSMTR,CSMTR,DPRO,CBPRO,QPRO,QGAIN      
     &,                RPRO,CRPRO,RFIL,CRFIL,RSMT,CRSMT,CHIFIL,CHISMT   
*                                                                                                             
* Per-track values can go in H1WORK; note that LTRUE and LFIRST must                                          
* be set at least per event.                                                                                  
*                                                                                                             
* This is about 36k words long; the remaining common blocks are                                               
* about 3.6k in total. Some of this could be in /H1WORK/, but the                                             
* blocks would have to be reorganised.                                                                        
*                                                                                                             
      COMMON /H1WORK/                                                   
* /FKPROJ/                                                                                                    
     &                SPRO(5,NPL),CPRO(5,5,NPL)                         
* /FKFILT/                                                                                                    
     &,               SFIL(5,NPL),CFIL(5,5,NPL)                         
* /FKSMTH/                                                                                                    
     &,               SSMT(5,NPL),CSMT(5,5,NPL)                         
     &,               SSMTR(5,NPL),CSMTR(5,5,NPL)                       
* /FKINT/                                                                                                     
     &,               DPRO(5,5,NPL),CBPRO(5,5,NPL),QPRO(5,5,NPL)        
     &,               QGAIN(5,5,NPL),IAPROX,LFIRST                      
* /FKRSID/                                                                                                    
     &,               RPRO(2,NPL),CRPRO(2,2,NPL),RFIL(2,NPL)            
     &,               CRFIL(2,2,NPL),RSMT(2,NPL),CRSMT(2,2,NPL)         
     &,               CHIFIL(NPL),CHISMT(NPL)                           
* /FKTRUE/                                                                                                    
     &,               TRUE(5,NPL),RTRUE(5,NPL),CHITRU(NPL),LTRUE        
* /FKDBG/                                                                                                     
     &,               LTRPL(NPL),LTRPLD(NPL)                            
*KEEP,FKCNTL.                                                                                                 
      COMMON /FKCNTL/ LUN,IPR,ITR,IPL,JSTART,JSTOP,JLAST,JSTEP          
*KEEP,FKCONS.                                                                                                 
      DOUBLE PRECISION ZPL,DZPL,RADL                                    
      COMMON /FKCONS/ ZPL(NPL),DZPL(NPL),RADL(NPL)                      
*KEEP,FKTRUE.                                                                                                 
*KEEP,FKFLAG.                                                                                                 
      LOGICAL LPRO,LFIL,LSMT,LMES,LRAD,LRPRO,LRFIL,LRSMT,LPOINT,LBLOCK  
      COMMON /FKFLAG/ LPRO(NPL),LFIL(NPL),LSMT(NPL),LMES(NPL)           
     &,               LRAD(NPL),LRPRO,LRFIL,LRSMT,LPOINT,LBLOCK         
*KEEP,FKPROJ.                                                                                                 
*KEEP,FKFILT.                                                                                                 
*KEEP,FKSMTH.                                                                                                 
*KEEP,FKRSID.                                                                                                 
*KEEP,FKINT.                                                                                                  
*KEND.                                                                                                        
                                                                        
**********************************************************************                                        
*                                                                                                             
* Local arrays etc ...                                                                                        
*                                                                                                             
      DIMENSION SADJ(5),DINV(5,5),AGAIN(5,5)                            
                                                                        
**********************************************************************                                        
*                                                                                                             
* Initialisation and checks ...                                                                               
*                                                                                                             
      IERR = 0                                                          
                                                                        
* If JPL=JLAST then Smoothed is same as Filtered ...                                                          
      IF (JPL.EQ.JLAST) THEN                                            
* Does filtered data exist at plane JPL ?                                                                     
         IF (.NOT.LFIL(JPL)) THEN                                       
            CALL FKERR(IUTIL,IROUT,IFATAL,IINF1,IERR)
            RETURN                                                      
         ENDIF                                                          
         CALL UCOPY(SFIL(1,JPL),SSMT(1,JPL),10)                                                        
         CALL FKCOPY(CFIL(1,1,JPL),CSMT(1,1,JPL))
         GOTO 600                                                       
      ENDIF                                                             
                                                                        
* Number of next plane                                                                                        
      JPLN = JPL + JSTEP                                                
                                                                        
* Does smoothed data exist at previous plane?                                                                 
      IF (.NOT.LSMT(JPLN)) THEN                                         
         CALL FKERR(IUTIL,IROUT,IFATAL,IINF1,IERR)
         RETURN                                                         
      ENDIF                                                             
                                                                        
* RADL and LRAD are defined in an asymmetrical way                                                            
      JPLR = JPL + (JSTEP-1)/2                                          
                                                                        
* Does projected data exist at previous plane? (only needed with MS)                                          
      IF (LRAD(JPLR) .AND. .NOT.LPRO(JPLN)) THEN                        
         CALL FKERR(IUTIL,IROUT,IFATAL,IINF2,IERR)
         RETURN                                                         
      ENDIF                                                             
                                                                        
* Has smoothing to JPL already been done? Continue anyway ..... !?                                            
      IF (LSMT(JPL)) CALL FKERR(IUTIL,IROUT,IWARN,IDONE,IERR)
                                                                        
**********************************************************************                                        
                                                                        
      IF (LRAD(JPLR)) THEN                                              
*                                                                                                             
* Update the smoothed state vector at plane JPL+1 by removing the                                             
* estimated multiple scattering between planes JPL and JPL+1;                                                 
* I think that this is probably the most `correct' way to do it.                                              
* If there is no multiple scattering QGAIN is zero, so this can be                                            
* skipped. Then transform the adjusted state vector from JPL+1 to JPL.                                        
*                                                                                                             
         CALL FKADJ(SSMT(1,JPLN),SPRO(1,JPLN),QGAIN(1,1,JPL),SADJ)
         CALL FKNORM(SADJ,IFAIL)
         IF (IFAIL.NE.0) CALL FKERR(IUTIL,IROUT,IFAIL/100,IFAIL,IERR)
         IF (IFAIL.GE.100) RETURN                                       
         CALL FKTRAN(-DZPL(JPL),ZPL(JPLN),SADJ,SSMT(1,JPL),DINV)
*                                                                                                             
* Compute the smoothed covariance. This may be numerically unstable if                                        
* the multiple scattering is large (probably not the case).                                                   
*                                                                                                             
         CALL FKDMQD(DINV,QGAIN(1,1,JPL),AGAIN)
         CALL FKMXM(CSMT(1,1,JPLN),AGAIN,CSMT(1,1,JPL))
         CALL FKDQA(DINV,QPRO(1,1,JPL),AGAIN,CSMT(1,1,JPL))
      ELSE                                                              
* If there is no MS, smoothing is just back-extrapolation                                                     
* Transform the state vector and covariance from JPL to JPL+1 ...                                             
         CALL FKTRAN(-DZPL(JPL),ZPL(JPLN),SSMT(1,JPLN),
     &                                    SSMT(1,JPL),DINV)             
         CALL FKMUL(CSMT(1,1,JPLN),DINV,CSMT(1,1,JPL))
      ENDIF                                                             
                                                                        
      CALL FKNORM(SSMT(1,JPL),IFAIL)
      IF (IFAIL.NE.0) CALL FKERR(IUTIL,IROUT,IFAIL/100,IFAIL,IERR)
      IF (IFAIL.GE.100) RETURN                                          
                                                                        
  600 CONTINUE                                                          
                                                                        
      IF (LTRUE) THEN                                                   
         CALL FKRST(JPL,IFAIL)
         IF (IFAIL.NE.0) CALL FKERR(IUTIL,IROUT,IWARN,IFAIL,IERR)
      ENDIF                                                             
                                                                        
      IF (LRSMT) THEN                                                   
         IF (LMES(JPL)) THEN                                            
            CALL FKLRSD(JPL,SSMT(1,JPL),CSMT(1,1,JPL),-3,
     &               RSMT(1,JPL),CRSMT(1,1,JPL),CHISMT(JPL),IFAIL)      
            IF (IFAIL.NE.0) CALL FKERR(IUTIL,IROUT,IWARN,IFAIL,IERR)
         ELSE                                                           
            CALL VZERO(RSMT(1,JPL),4)                                                                  
            CALL VZERO(CRSMT(1,1,JPL),8)                                                               
            CHISMT(JPL) = 0.D0                                          
         ENDIF                                                          
      ENDIF                                                             
                                                                        
* Set flag to show smoothing is done                                                                          
      LSMT(JPL) = .TRUE.                                                
                                                                        
      RETURN                                                            
      END                                                               
*