      SUBROUTINE SECSM (CARD,IUO,IOBS,B,NX,FATAL)

*** WRITE CONSTRAINTS FOR VELOCITY SMOOTHING RECORD

      IMPLICIT DOUBLE PRECISION (A-H,O-Z)
      IMPLICIT INTEGER (I-N)

      DIMENSION COEF(3), SC(3)
      DIMENSION IWESCR(-1:1,-1:1)
      DATA IWESCR /1,1,1, 1,0,1, 1,1,1/

      PARAMETER (MXSSN = 9999)
      CHARACTER*80 CARD
      LOGICAL FATAL
      LOGICAL ADDCON
      LOGICAL ELFLAG,DFFLAG
      LOGICAL LMSL,LSS,LUP
      LOGICAL LBB,LGF,LCS,LVD,LVA,LVZ,LVS,
     &        LVR,LVG,LVC,LIS,LPS,LPG,LDR,LOS,LAP
      DIMENSION B(*),NX(*)
      DIMENSION IC(31),C(31)
      COMMON /CONST/ PI,PI2,RAD,RADSEC,TWOPI
      COMMON /FLAGS/ ELFLAG(MXSSN),DFFLAG(MXSSN)
      COMMON /OPT/ AX,E2,DMSL,DGH,VM,VP,CTOL,ITMAX,ITMIN,IMODE,
     &             LMSL,LSS,LUP
      COMMON /OPRINT/ CRIT,LBB,LGF,LCS,LVD,LVA,LVZ,LVS,
     &                LVR,LVG,LVC,LIS,LPS,LPG,LDR,LOS,LAP
      COMMON /STRUCT/ NSTA,NAUX,NUNK,IDIM,NSTAS,NOBS,NCON,NZ,NCD
      COMMON /CDGRID/ GRDLX, GRDUX, GRDLY, GRDUY, ICNTX, ICNTY,
     &                ITREF

      COMMON /SMOOTH/ SMCAL

      JDUM = 0
      SMCAL = 1.D-9 

      READ (CARD,1) ISCN, ISCE, ISCU, NDIST

    1 FORMAT(2X, 4I5)
      NDIST2 = NDIST*NDIST

      SC(1) = (DBLE(ISCN) * SMCAL) / DBLE(NDIST2)
      SC(2) = (DBLE(ISCE) * SMCAL) / DBLE(NDIST2)
      SC(3) = (DBLE(ISCU) * SMCAL) / DBLE(NDIST2)




      IF (CARD(3:8).EQ.'     ') SC(1) = SMCAL
      IF (CARD(9:14).EQ.'     ') SC(2) = SMCAL
      IF (CARD(15:20).EQ.'     ') SC(3) = SMCAL


      DO 60 I = 1, ICNTX + 1
        DO 50 J = 1, ICNTY + 1
          CALL GRDLOC(I,J,GLON, GLAT)
          CALL RADCUR(GLAT,RMER,RPV)
          RPAR = RPV * DCOS(GLAT)
          XSTEP = ((GRDUX - GRDLX)/ICNTX) * RPAR
          YSTEP = ((GRDUY - GRDLY)/ICNTY) * RMER
          COEF(1) = RMER/RADSEC
          COEF(2) = RPAR/RADSEC
          COEF(3) = 1.D0

          DO  40 JJ = 1, 3   
            INC = 0
            IISTA = -1
            IISTO =  1
            IJSTA = -1
            IJSTO =  1
  
            IF (I.EQ.1) IISTA = 0
            IF (I.EQ.ICNTX+1) IISTO = 0 
            IF (J.EQ.1) IJSTA = 0
            IF (J.EQ.ICNTY+1) IJSTO = 0 

            DO 20 II = IISTA, IISTO
               DO 10 IJ = IJSTA, IJSTO
                  IF (IWESCR(II,IJ).NE.0) THEN
                     INC = INC + 1
                     IC(INC) = IUNGRD(I+II, J+IJ,JJ)
                     DIST2 = (II*XSTEP)*(II*XSTEP)
     &                         + (IJ*YSTEP)*(IJ*YSTEP)
                     C(INC) = COEF(JJ) / DIST2
                  ENDIF
   10          CONTINUE
   20       CONTINUE
            IC(INC + 1) = IUNGRD(I,J,JJ)
            C(INC + 1) = 0.D0
            DO 30 JI = 1, INC
               C(INC + 1) = C(INC + 1) - C(JI)
   30       CONTINUE
            
            LENG = INC + 1
            KIND = 21 + JJ
            IOBS = IOBS + 1
            NCON = NCON + 1


            OBSB = 0.D0

            CALL COMPSM(KIND,OBS0,LENG,C,IC,B)

            CMO = OBS0 - OBSB


            IF (.NOT.ADDCON(IC,LENG,NX)) THEN
               WRITE(6,666)
  666          FORMAT("OERROR - INSUFFICIENT STORAGE FOR"
     &                " CONNECTIVITY",/)
               CALL ABORT2
            ENDIF
            WRITE (IUO) KIND,I,J,IC,C,LENG,CMO,OBSB,SC(JJ),
     &                  IOBS,JDUM,JDUM,JDUM

   40     CONTINUE
   50   CONTINUE
   60 CONTINUE

*** ECHO OBSERVATION NUMBERS

      IF (LCS) THEN
        CALL LINE (1)
        WRITE (6,3) IOBS,CARD
    3   FORMAT (1X,I6,3X,A80)
      ENDIF
      RETURN
      END
