	program rankwocjc
C.....................................................Sep. 2014
C Ranking w, n, p occurrence
C.....................................................Nov. 2012
C Ranking wj, nj, pj results from JPL maps
C
C IONEX maps
C for glat=-87.5:2.5:87.5, glong=-180:5:165  (5112 => 840 grids)
C Correction of (W+max-W-max)*(1+cntstrm/5112)
C.....................................................Feb. 2012
C
C.....................................................Aug.2011
C
C Input: maps of W-index: tcMNDYUT.wYR files 
C             for glat=-85:5:85, glong=-180:15:165  (=840 grids)
C Correction of (W+max-W-max)*(1+cntstrm/840)
C
C........................................................Apr.2008
C...... Count RANGE only when W+=>3, and/or W-<=-3 ..............
C
c........................................................Oct.2007
C Correction of (W+max-W-max)*(1+cntstrm/600)
C cntstrm=total number of W+=3,W+=4, W-=-3,W-=-4 for the IONEX-map
C
c .......................................................Sep. 2007
C Range of TEC indices from 35 latitude results TEC.fYR.........
C
c from hourly IONEX maps
c
	DIMENSION IM(12)
	+,JDATW(31),JDATP(31),JDATN(31),JX1(31),JX2(31)
	 INTEGER*2 IYR,MN,idy1,idy2,imn1,imn2
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*64 infilew,infilep,infilen
	+,outfilew,outfilep,outfilen
	CHARACTER*133 txt
	CHARACTER*2 AYR,AMN,CMN
	CHARACTER*6 YYMMDD,YMD(31),Y1(31),Y2(31),ABC
	CHARACTER*4 AYEAR
      DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/
C

	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT

	WRITE(*,'(\A\)') ' FIRST RUN PROBHWPJ.FOR '
	pause ' '

C start
      WRITE(*,'(\A\)') ' ENTER YEAR OF INPUT FILE='
      READ(*,'(A)') AYEAR
crem	AYEAR='1999'
C  1      WRITE(*,'(\A\)') ' ENTER LAST MONTH OF INPUT FILE='
  1      WRITE(*,'(\A\)') ' ENTER IMN1, IMN2 OF INPUT FILE='
      READ(*,*) IMN1,IMN2
crem	imn=12  !CTEMP
	ayr=ayear(3:4)
	read(ayr,*) year
	iyr=int(year)

C Cycle on months:
Crem	DO 1130 mn=1,imn
	DO 1130 mn=imn1,imn2
Ctemp	mn=imn
	call blet2(mn,AMN)
	if(int(year/4.)*4.eq.iyr) THEN 
	ndnr=366
	im(2)=29
	                          ELSE
	ndnr=365
	im(2)=28
	ENDIF

crem      WRITE(*,'(\A\)') ' ENTER DY1 & DY2 ='
crem      READ(*,13) idy1,idy2
C ADD
	idy1=1
	idy2=im(mn)
 13   FORMAT(I2)

CC CALCULATION OF DAYS OF YEAR	
crem      infilew='c:\msdev\projects\wtec\wjtocYR.txt'	! from W-maps geogr.coord.
C      infilew='wjtocYR.txt'	! from W-maps geogr.coord.
      infilew='wjticYR.txt'	! from W-maps magn.coord. -15d 
	infilew(6:7)=AYR
 	infilep=infilew
	infilen=infilew
	infilep(1:1)='p'
	infilen(1:1)='n'
	outfilew='wjrankYRoc.txt'
	outfilew(7:8)=AYR
	outfilep=outfilew
	outfilep(1:1)='p' 
	 outfilen=outfilew
	outfilen(1:1)='n' 
  	OPEN(111,FILE=infilew)
	OPEN(112,FILE=infilep)
	OPEN(113,FILE=infilen)
	read(111,14) txt  ! extra line
	read(112,14) txt  ! extra line
	read(113,14) txt  ! extra line
   14	format(1X,A133)
C
C
	lda1=ndoy(iyr,mn,idy1)  ! day-of-year
	mndy=im(mn)
  130	format(A133)
C
  197 continue
  	OPEN(11,FILE=outfilew,ACCESS='APPEND')
	OPEN(12,FILE=outfilep,ACCESS='APPEND')
	OPEN(13,FILE=outfilen,ACCESS='APPEND')

C
C 
C ========================================

c/       DO 500 idy=idy1,idy2
c/	lda=ndoy(iyr,mn,idy)  ! day-of-year
c/	call blet2(idy,ady)
C
C -----------------------------------------------------
  12	FORMAT(A6,123X,I4)
C ===== Input datafile 
 	DO jj=1,mndy	  ! 
  131  	READ (111,12,END=31,ERR=2) YYMMDD,JZZZ
	CMN=yymmdd(3:4)
	if (CMN.eq.AMN) then
	YMD(jj)=YYMMDD
	JDATW(jj)=JZZZ
	                 else
	goto 131
	endif
	enddo
   31	close(unit=111)
C
   	DO jj=1,mndy	  ! 
  132  	READ (112,12,END=32,ERR=2) YYMMDD,JZZZ
	if (yymmdd(3:4).eq.AMN) then
	JDATP(jj)=JZZZ
	                 else
	goto 132
	endif
	enddo
   32	close(unit=112)
C
 	DO jj=1,mndy	  ! 
  133  	READ (113,12,END=33,ERR=2) YYMMDD,JZZZ
	if (yymmdd(3:4).eq.AMN) then
	JDATN(jj)=JZZZ
	                 else
	goto 133
	endif
	enddo
   33	close(unit=113)
C
C Ranking Wp
C
	iflag=0
	num=11
  879    DO N=1,mndy
      JX1(N)=JDATW(N)
	Y1(N)=YMD(N)
      ENDDO 
C
      MM=0
	do n=1,mndy
	JX2(n)=0
 	Y2(n)='000000'
 	enddo
C
  880 JXX=0
	MMEND=mndy+1
CREM MAX X1(N)
  890 DO N=1,mndy
  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,mndy
      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.mndy) GOTO 880
  980 XMM=FLOAT(MM)

C----------------------------------------------------------
c   
  801 continue            ! 
      iflag=iflag+1
	if (iflag.eq.3) then
		DO N=1,mndy
 	write(*,73) Y2(N),JX2(N)
	write(13,73) Y2(N),JX2(N)
	enddo
       goto 909 ! end-stop
	endif
  73	format(1X,A6,1X,I4)
C
	if (iflag.eq.1) then
	DO N=1,mndy
	write(*,73) Y2(N),JX2(N)
	write(11,73) Y2(N),JX2(N)
	enddo
	do k=1,mndy
	jdatw(k)=jdatp(k)
	enddo
	num=12
	goto 879
	endif
C
	if (iflag.eq.2) then
	DO N=1,mndy
	write(*,73) Y2(N),JX2(N)
	write(12,73) Y2(N),JX2(N)
	enddo
	do k=1,mndy
	jdatw(k)=jdatn(k)
	enddo
	num=13
	goto 879
	endif
C
  500	CONTINUE ! next day
C
c-   72 format(A6,24(1X,F4.1),1X,F6.1)
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  909 continue
	CLOSE(unit=11)
	CLOSE(unit=12)
	CLOSE(unit=13)
 1130	continue  ! cycle mn-to-mn
	pause ' '
      STOP
      END

