	subroutine subrankf(JYR,JMN,JDY,DDMAX,JMAX,DDMIN,JMIN)
C.....................................................July 2015
C Ranking vs occurrence for 27 prec days before JYR, JMN, JDY
C Output DDMAX = YYMMDD for JMAX, and DDMIN = YYMMDD for JMIN
C
C Source Vs data=> JAX(27) for 27 prec days
c from hourly IONEX maps
c
	DIMENSION IM(12),JAX(27),LDAJ(27)
     +,JX1(27),JX2(27)
	CHARACTER*64 infilev,txt
	CHARACTER*6 YMD(27),Y1(27),Y2(27),ABC,DDMAX,DDMIN
	CHARACTER*2 AYR,AYR_pre
      DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/
C
	DO i=1,27
	YMD(i)='000000'
	JAX(i)=0
	LDAJ(i)=0
	ENDDO
C
	year=float(jyr)
	if(int(year/4.)*4.eq.jyr) THEN 
	ndnr=366
	im(2)=29
	                          ELSE
	ndnr=365
	im(2)=28
	ENDIF

	call blet2(jyr,AYR)
C
 	lda=ndoy(jyr,jmn,jdy)
C
	jyr_pre=jyr
	IF (lda.lt.28) then	 ! needs using prec year data >>>>>>>>>
      jyr_pre=jyr-1
	if (jyr_pre.lt.0) jyr_pre=99
	year_pre=float(jyr_pre)
	  IF(int(year_pre/4.)*4.eq.jyr_pre) THEN 
	  ndnr_pre=366
	                          ELSE
	  ndnr_pre=365
	  ENDIF
	ENDIF
C
	call blet2(jyr_pre,AYR_pre)
C
	k_pre=0
 	do j=1,27
	ldaj(j)=lda-j ! days lda_1, lda_2,...,lda_27
	if (ldaj(j).lt.1) then
	 k_pre=k_pre+1
	ldaj(j)=ndnr_pre-k_pre+1
	endif
	enddo

 13   FORMAT(A64)
  14   FORMAT(A6,123X,I4)


CC READING DATA OF 27 prec days
C??      infilev='c:\msdev\projects\counts\vstocYR.txt'	! from Vs(TEC)-maps geogr.coord.
      infilev='c:\msdev\projects\Mapf\vsfocYR.txt'	!from Vfs-maps geogr.coord.
C
	if (k_pre.gt.0) then
	infilev(29:30)=AYR_pre
	OPEN(10,file=infilev,action='READ')
	read(10,13) txt ! extra line
	nn=0
	DO kk=1,ldaj(27)-1
	read(10,14) ABC,ind ! extra lines
	nn=nn+1
	enddo
	DO kk=1,k_pre
	nn=nn+1
	read(10,14,end=123) YMD(kk),JAX(kk)
	ENDDO
  123	close(unit=10)
      endif
C
	if (k_pre.eq.27)  GOTO 125  ! avoid reading current year
C
	infilev(29:30)=AYR
   	OPEN(11,FILE=infilev,action='READ')
	read(11,13) txt  ! extra line
	nn=0
	if (lda.gt.28) then
C Only current year after 28 Jan
	DO kk=1,ldaj(27)-1
	read(11,14) ABC,ind ! extra lines
	nn=nn+1
	enddo
	endif
C
	DO kk=k_pre+1,27
	read(11,14,end=124)  YMD(kk),JAX(kk)
	ENDDO
  124	close(unit=11)
C
C ========================================
C
C Ranking Vs, %
C
  125	num=11
	mmend=28
  879    DO N=1,27
      JX1(N)=JAX(N)
	Y1(N)=YMD(N)
      ENDDO 
C
      MM=0
	do n=1,27
	JX2(n)=0
	Y2(n)='000000'
 	enddo
C
  880 JXX=0
CREM MAX X1(N)
  890 DO N=1,27
  900 IF (JXX.lt.JX1(N)) THEN 
      JXX=JX1(N)
	ABC=Y1(N)
	ENDIF
      ENDDO      !N
  920 CONTINUE
      IF (JXX.eq.0) then 
	MMEND=MMEND-1
	Y2(MMEND)=ABC
	GOTO 980
	ENDIF
  930 DO 960 N=1,27
      IF (JX1(N).lt.JXX) GOTO 960
      MM=MM+1 
      JX2(MM)=JXX 
	Y2(MM)=ABC
	IF (ABC.ne.Y1(N)) Y2(MM)=Y1(N)
	JX1(N)=0
	Y1(N)='000000'
  960 CONTINUE      ! N
  970 IF (MM.lt.27) GOTO 880
  980 XMM=FLOAT(MM)

C----------------------------------------------------------
c   
  801 continue            ! 
	DDMAX=Y2(1)
	JMAX=JX2(1)
	DDMIN=Y2(27)
	JMIN=JX2(27)
Crem		DO N=1,27
Crem 	write(*,73) Y2(N),JX2(N)
Crem	ENDDO
Crem       goto 909 ! end-return
  73	format(1X,A6,1X,I4)
C
C    2 CONTINUE
C           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  909 continue
      RETURN
      END

