*-----------------------------------------------------------------------
*     TMSTLS : THE ROUTINE FOR DRAWING STREAM LINES (COORD. OF SPHERIC.)
*-----------------------------------------------------------------------
*     COPYRIGHT (C) 2000-2017 GFD DENNOU CLUB. ALL RIGHTS RESERVED.
*-----------------------------------------------------------------------
      SUBROUTINE TMZTLS( L, T, U, V, NX, NY ,TRX,TRY,NM,X,Y,TMPU,TMPV )

      IMPLICIT NONE

*-- AGREENMENT
      INTEGER  NX        !* GRID NUMBERS FOR X-DIRECTION
      INTEGER  NY        !* GRID NUMBERS FOR Y-DIRECTION
      REAL     U(NX,NY)  !* VECTOR COMPONENT OF X-DIRECTION
      REAL     V(NX,NY)  !* VECTOR COMPONENT OF Y-DIRECTION
      REAL     L(NX)     !* GRID POINTS OF ZONAL DIR. [UNIT:DEGREE]
      REAL     T(NY)     !* GRID POINTS OF MERIDIONAL DIR. [UNIT:DEGREE]

*-- INTERNAL VARIABLES
      REAL     TRX(2,NX*NY,*)  !* STREAM LINE OF X-COORDINATE
      REAL     TRY(2,NX*NY,*)  !* STREAM LINE OF Y-COORDINATE
      INTEGER  I, J, K             !* TMP VALUES
      INTEGER  COUNTER             !* NUMBER OF STREAM LINE
      INTEGER  NM(2,*)         !* ARRAY NUMBER IN EACH STREAM LINE
      REAL     X(*), Y(*)
      REAL     TMPU(NX,*), TMPV(NX,*)
      REAL     ARROW_THRES
      REAL     DXA, DYA
      REAL     ARROW_LENG
      REAL     VXMIN, VXMAX, VYMIN, VYMAX
      REAL     UXMIN, UXMAX, UYMIN, UYMAX
      REAL     VXRATIO, VYRATIO
      REAL     RCOE
      REAL     CIRC_FLAG
      REAL     UNDEF
      REAL     RAD
      PARAMETER(RAD=6.387)
      INTEGER  N, M, IBLKGE, ARRCNT
      LOGICAL  NO_SHORT, END_ARR

*-- EXTERNAL FUNCTION
      REAL     RFPI
      EXTERNAL RFPI

      RCOE=RFPI()/180.0

*-- GETTING TMPACK'S PARAMETERS
      CALL TMRGET( 'ARRWINTV', ARROW_THRES )
      CALL TMLGET( 'NODRSHRT', NO_SHORT )

      CALL GLRGET( 'RMISS', UNDEF )
      CALL GLLSET( 'LMISS', .TRUE. )

*-- GETTING VIEWPORT PARAMETERS
      CALL SGQVPT( VXMIN, VXMAX, VYMIN, VYMAX)
      CALL SGQWND( UXMIN, UXMAX, UYMIN, UYMAX)
      VXRATIO=(VXMAX-VXMIN)/(UXMAX-UXMIN)
      VYRATIO=(VYMAX-VYMIN)/(UYMAX-UYMIN)

*-- CONVERTING DEGREE TO RADIAN
      DO 11 I=1,NX
         X(I)=L(I)*RCOE
 11   CONTINUE
      DO 12 J=1,NY
         Y(J)=T(J)*RCOE
 12   CONTINUE

*-- CONVERTING VECTORS TO THEMSELVES INCLUDING SCALE PARAMETER
      DO 20 J=1,NY
         DO 21 I=1,NX
            IF(U(I,J).NE.UNDEF.AND.V(I,J).NE.UNDEF
     &         .AND.COS(Y(J)).NE.0.0)THEN
               TMPU(I,J)=U(I,J)/COS(Y(J))
               TMPV(I,J)=V(I,J)
            ELSE
               TMPU(I,J)=UNDEF
               TMPV(I,J)=UNDEF
            END IF
 21      CONTINUE
 20   CONTINUE

      DO 71 K=1,2
         DO 70 I=1,NX*NY
            NM(K,I)=0
 70      CONTINUE
 71   CONTINUE

*-- DOING TMSTLN ROUTINE
      CALL TMSTLN( X, Y, TMPU, TMPV, NX, NY, TRX, TRY, NM )

*-- GETTING STREAM LINE NUMBERS
      CALL TMIGET( 'STLNNUM', COUNTER )

*-- CONVERTING TRY(RADIAN) TO TRY(DEGREE)
      DO 30 K=1,2
         DO 31 I=1,COUNTER
            DO 32 J=1,NM(K,I)
               IF(TRX(K,J,I).NE.UNDEF.AND.TRY(K,J,I).NE.UNDEF)THEN
                  TRX(K,J,I)=TRX(K,J,I)/RCOE
                  TRY(K,J,I)=TRY(K,J,I)/RCOE
               END IF
 32         CONTINUE
 31      CONTINUE
 30   CONTINUE

*-- SETTING PARAMETER FOR ARROW. (NOW, CONSTANT VALUES)
      CALL SGLSET('LPROP',.FALSE.)
      CALL SGRSET('CONST',0.01)

*-- DRAWING STREAM LINES
      DO 82 K=1,2
         DO 81 I=1,COUNTER

*-- CALCULATING THE INTERVAL LENGTH OF DRAWING ARROWS.
            ARROW_LENG=0.0
            ARRCNT=0
            IF((NM(K,I).GT.1).AND.(NM(K,I).LE.NX*NY))THEN
               DO 80 J=2,NM(K,I)
                  IF(TRX(K,J,I).NE.UNDEF.AND.TRY(K,J,I).NE.UNDEF.AND.
     &               TRX(K,J-1,I).NE.UNDEF.AND.TRY(K,J-1,I).NE.UNDEF
     &               )THEN
                     DXA=(TRX(K,J,I)-TRX(K,J-1,I))*VXRATIO
                     DYA=(TRY(K,J,I)-TRY(K,J-1,I))*VYRATIO
                     ARROW_LENG=ARROW_LENG+SQRT(DXA*DXA+DYA*DYA)
                     IF(ARROW_LENG.GE.ARROW_THRES)THEN
                        CALL SGLAU( TRX(K,J-1,I), TRY(K,J-1,I),
     &                              TRX(K,J,I), TRY(K,J,I) )
                        ARROW_LENG=0.0
                        ARRCNT=ARRCNT+1
                     END IF
                  END IF
 80            CONTINUE

*-- IF NO_SHORT IS TRUE, NO DRAWING STREAM LINE WHOSE ARROW NUMBER IS ZERO
*-- 流線の始点と終点が近似点にいれば, 閉じた流線とみなして描く.
*-- 矢羽は終点に描く.
               IF(ARRCNT.EQ.0)THEN
                  IF(NO_SHORT.EQV..TRUE.)THEN
                     IF(NM(K,I).LE.2)THEN
                        GO TO 81
                     ELSE
                        N=IBLKGE( L, NX, TRX(K,1,I) )
                        M=IBLKGE( L, NX, TRX(K,NM(K,I),I) )
                        IF(ABS(N-M).GT.1)THEN
                           GO TO 81
                        ELSE
*-- 一周しているのか, 短いだけなのかのチェック
*--  始点, 終点, 終点１つ前の順に増減していれば一周.
                           CIRC_FLAG=
     &                        (TRX(K,NM(K,I),I)-TRX(K,NM(K,I)-1,I))
     &                        *(TRX(K,1,I)-TRX(K,NM(K,I),I))
                           IF(CIRC_FLAG.LT.0.0)THEN  ! 単に短いだけ
                              GO TO 81
                           ELSE
                              N=IBLKGE( T, NY, TRY(K,1,I) )
                              M=IBLKGE( T, NY, TRY(K,NM(K,I),I) )
                              IF(ABS(N-M).GT.1)THEN
                                 GO TO 81
                              ELSE
                                 CIRC_FLAG=
     &                             (TRY(K,NM(K,I),I)-TRY(K,NM(K,I)-1,I))
     &                             *(TRY(K,1,I)-TRY(K,NM(K,I),I))
                                 IF(CIRC_FLAG.LT.0.0)THEN
                                    GO TO 81
!                              ELSE
!                                 CALL SGLAU( TRX(NM(I)-1,I), TRY(NM(I)-1,I),
!     &                                       TRX(NM(I),I), TRY(NM(I),I) )
                                 END IF
                              END IF
                           END IF
                        END IF
                     END IF
                  ELSE
                     IF(END_ARR.EQV..TRUE.)THEN
                        CALL SGLAU( TRX(K,NM(K,I)-1,I),
     &                              TRY(K,NM(K,I)-1,I),
     &                              TRX(K,NM(K,I),I),
     &                              TRY(K,NM(K,I),I) )
                     END IF
                  END IF
               END IF

               CALL UULIN( NM(K,I), TRX(K,1:NM(K,I),I),
     &                     TRY(K,1:NM(K,I),I) )

            END IF

 81      CONTINUE

 82   CONTINUE

      CALL MSGDMP( 'M', 'TMSTLS', 'DRAWING FINISHED.' )

      END SUBROUTINE TMZTLS

