	program dststorm
C DST-STORM SELECT STORM DAYS= DST-min=<-100 WITH 24-HRS Dst-indices
C INCLUDE INPUT <dstyr.D24> TO DETECT STORM ONSET FOR DAYS DstMIN=<-100; DEL-Dst>100%
C Rejected version because dev% from prec. 27-days aver. is unable to detect storm periods
C T.L. Gulyaeva                                                        April, 2005
      DIMENSION IM(12),DA(366,0:24),DEV(366,0:23)
	DIMENSION XDEV(0:23)
	CHARACTER*22 INFILE
	CHARACTER*24 OUTFILE,INDEV
	CHARACTER*10 BL,YRINP,RE(366),D1,D2,SYEAR,AME
	REAL YEAR
	COMMON DEV,DA
      DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/
		BL=" "

 100	WRITE(*,'(A\)') ' ENTER YEAR    :'
      READ (*,*) SYEAR
	READ (SYEAR,*) YEAR
	YEAR1=YEAR
	INFILE='D:\UAPKP\dst\dst'
	OUTFILE='D:\UAPKP\dst\dststrm.out'

	AME(1:)=SYEAR(3:)
		INDEV=INFILE
	INFILE(17:)=SYEAR
	INDEV(17:)=AME
	INDEV(19:)='.D24'
      IF ((YEAR/4.).EQ.IFIX(YEAR/4.)) THEN 
	IM(2)=29 
	ELSE 
	IM(2)=28
	ENDIF

      IDNR=365
      IF (IM(2).EQ.29) IDNR=366
C	PAUSE ' '
	OPEN(111,FILE=INFILE)
	DO 15 J=1,IDNR

	READ (111,12,END=2,ERR=99) YRINP,XDEV
   12  FORMAT (A10,10X,24(F4.0))

C	IF (YRINP(3:3).EQ.BL) YRINP(3:3)="0"
C	IF (YRINP(5:5).EQ.BL) YRINP(5:5)="0"
	RE(J)=YRINP
C Interpolation from 8 to 24 hourly values
	DAV=0.
	DO 14 K=0,23
	DA(J,K)=xdev(K)
	DAV=DAV+XDEV(K)
   14 CONTINUE
	DA(J,24)=DAV/24.
   15 CONTINUE

   2   CONTINUE
       CLOSE (UNIT=111)

		OPEN(110,FILE=INDEV)
 	DO 16 J=1,IDNR
	READ (110,13,END=3,ERR=99) XDEV
   13	FORMAT(10X,24(F5.0),1X)
	DO K=0,23
	DEV(J,K)=XDEV(K)
	ENDDO
   16 CONTINUE
    3   CONTINUE
       CLOSE (UNIT=110)

	OPEN(112,FILE=OUTFILE,ACCESS='APPEND',STATUS='old')	
c		OPEN(112,FILE=OUTFILE,ACCESS='APPEND')	
      DO 810 J=1,IDNR
      AMIN=0.
	DEVMAX=0.
	CNT=0
	CNTD=0
	IFL=0
      DO K=0,23
      IF (DA(J,K).LT.AMIN) THEN 
	AMIN=DA(J,K) 
	IUTMIN=K
	ENDIF
	IF (DEV(J,K).GT.DEVMAX) THEN
	DEVMAX=DEV(J,K)
	IUTDEVX=K
	ENDIF
	ENDDO

      IF (AMIN.LE.-100.) THEN 
	  IFL=1
	  GOTO 710
	ELSE
        GOTO 810
	ENDIF
	
C GO TO SUBROUTINE DETECTING <STORM-START;STORM-END> USING KPYR.D27
  710 CONTINUE
      D1='' 
      D2=''
	IT1=0
	IT2=0
	ADST=0.
	IDAY=J

	CALL DETECT(IDAY,RE,D1,IT1,D2,IT2,IUTT,ADST,AMIN,IUTMIN)
C	IF (ADST.EQ.0.) GOTO 810
	AVER=DA(J,24)
	WRITE(112,170) D1,IT1,RE(J),IDAY,AVER,AMIN,IUTMIN,D2,IT2,IUTT,ADST
	WRITE(*,170) D1,IT1,RE(J),IDAY,AVER,AMIN,IUTMIN,D2,IT2,IUTT,ADST
C	PAUSE ' '
  170	FORMAT(1X,A10,I3,2X,A10,1X,I3,2F6.0,I4,2X,A10,I3,1X,I4,F6.0)
  810 CONTINUE
C	ENDDO
C
      CLOSE (UNIT=112)
      YEAR=YEAR+1
      IF (YEAR.LT.2005) GOTO 100
   99	CONTINUE
C	CLOSE (UNIT=112)
      STOP
	END
C******************************************************************************
C      SUBROUTINE TO DETECT <STORM-START;STORM-END>
	SUBROUTINE DETECT(JDAY,RE,D1,IT1,D2,IT2,IUT,AADST,DEX,IMX)
	DIMENSION DA(366,0:24),DEV(366,0:23)
	CHARACTER*10 D1,D2,RE(366)
	INTEGER C1,C2
	COMMON DEV,DA
	
C START-OF-STORM DETECTING================
      J1=JDAY
C      DEX=0.
C      DO K=0,23
C      IF (DEV(J1,K).GT.DEX) GOTO 940
C      DEX=DEV(J1,K) 
C	IMX=K
C 940	CONTINUE
C	ENDDO
C	IF (DEX.LT.100.) GOTO 1560
C  PRINT IMX,DEX 
  970    C1=-1  
	K1=23
      IF (J1.EQ.JDAY) K1=IMX
      K=K1
 1000 CONTINUE
C PRINT K;DEV(J1,K)
      IF (DEV(J1,K).GE.100.) THEN 
	C1=K  
	GOTO 1030
	                     ELSE
      ISTR1=K+1 
	GOTO 1060
	ENDIF
 1030 K=K-1
      IF (K.GE.0) GOTO 1000
      IF (C1.GE.0) THEN 
	J1=J1-1 
	GOTO 970
	ENDIF

1060  IF (ISTR1.LT.24) GOTO 1080
      J1=J1+1 
	 ISTR1=0
1080  D1=RE(J1)
      IT1=ISTR1 
C PRINT D1,IT1 

C END-OF-STORM DETECTING =================
      J2=JDAY
 1140  C2=-1
	 K1=0
1150  IF (J2.EQ.JDAY) K1=IMX

      DO K=K1,23
C     PRINT K;DEV(J2,K)
       IF (DEV(J2,K).GE.100.) THEN 
       C2=K 
	 GOTO 1200
	               ELSE
 1190 ISTR2=K-1  
      GOTO 1220
	             ENDIF
 1200 CONTINUE
	 ENDDO
 1210 IF (C2.EQ.23) THEN 
      J2=J2+1  
	GOTO 1140
	ENDIF
 1220 IF (ISTR2.GE.0) GOTO 1240
      J2=J2-1 
	ISTR2=23
 1240 D2=RE(J2)
      IT2=ISTR2 
C PRINT D2,IT2

C DERIVE STORM DURATION=========== 
 1290 I1=IMX-ISTR1
      IF (J1.LT.J2) I1=24-ISTR1
      I2=ISTR2+1
      IF (J1.EQ.J2) I2=ISTR2-IMX+1
C PRINT "i1=";I1,"i2=";I2
      II=I1+I2
 1350 IF ((J2-J1).GT.1) THEN
         GOTO 1360 
	   ELSE 
	   GOTO 1370
	ENDIF
 1360  II=II+(J2-J1-1)*24
 1370 IUT=II
C
C Calculation of AP-MEAN, KP-MEAN FOR THE STORM DURATION =========
      SAP=0.
      L1=ISTR1 
	 L2=ISTR2 
	 JJ=J1
      IF (J2.GT.J1) L2=23
	
	      DO 116 L=L1,L2
      SAP=SAP+DA(JJ,L)
  116    CONTINUE

      IF (J2.EQ.J1) GOTO 1520
1450  L1=0  
      L2=ISTR2 
	 JJ=J2
      
	      DO 117 L=L1,L2
      SAP=SAP+DA(JJ,L)
  117    CONTINUE

      IF (J2.EQ.(J1+1)) GOTO 1520
      L1=0 
	 L2=23 
	  JJ=J1
 1490  JJ=JJ+1
	      DO 118 L=L1,L2
      SAP=SAP+DA(JJ,L)
  118    CONTINUE

 1510 IF (JJ.LT.(J2-1)) GOTO 1490
 1520     AADST=IFIX(SAP/II+.5)  
 1560	CONTINUE
      RETURN
	END