      program extpianiajm3
C.........................................................May. 2020
C  W(TEC) indices based on JPL monthly median
C.........................................................Dec. 2020
C 3h UT indices QU, QL, QE quantifying ion activity indices 
C
C Include weight e(glat)
C
C.........................................................Nov. 2020
C Weighted PIA -pos ion activity, NIA - neg ion activity
C extracted from global Wc(TEC) index derived from JPL GIM-TEC (IONEX)
C for ADY_pre and forecast for DD2
C
	DIMENSION weg(4),IM(12),iwneg(0:8),iwpos(0:8),iwran(0:8)  ! 3h UT 
	CHARACTER*128 INFILE,OUTFILEQ !global weighted QU,QL,QE = range index
	CHARACTER*2 AYR,AUT,AMN,ADY,AUTOUT(0:7)
	CHARACTER*4 AYEAR
	INTEGER ivsr(0:72) !  set of 3h-W indices for long=-180:5:180 at given lati
		DATA WEG/0.,0.6931,1.0986,1.3863/  ! =ln1, ln2, ln3, ln4
            DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/
C
C
      infile='d:\web\dwc\YY\MM\wmMMDDUT.jYY'   ! current mn
C
      outfileq='d:\web\graf\datm\wjmYY_3.txt'   !  Cnt(W-) 	 WL
	do iut=0,7
	iut3=iut*3
	call blet2(iut3,aut)
	autout(iut)=aut
	enddo
   17 format(A2)
   31 FORMAT('YYMMDD',1X,8(2X,A2,1X),8(3X,A2,1X),8(2X,A2,1X))
   32 FORMAT(73(1X,I4))
C
  330	CONTINUE
         DO 700 iyear=1994,2022 ! YEARnorm
C      iyear=2020  !Temp
	call blet4(iyear,AYEAR)
	IYR=iyear-iyear/100*100   ! current yr
	z1=iyear/4.0
      jz=int(z1)*4

      IF(jz.EQ.iyear) THEN
               IM(2)=29
	dnr=366.
        ELSE
                IM(2)=28
	  	dnr=365.
	       ENDIF
      AYR=AYEAR(3:4)  ! for current day  	
	infile(12:13)=ayr
	infile(28:29)=ayr
	outfileq(21:22)=ayr
C
	OPEN(11,file=outfileq,access='APPEND')
C
C month-to-month cycle
C
        imn2=12
	if (iyear.eq.2022) imn2=4 !Temp
	    DO 51 imn=1,imn2        ! MONNNNNNNNNNNNNNNNN
	call blet2(imn,AMN)
	infile(15:16)=AMN
	infile(20:21)=AMN
         idy1=1
	idy2=im(imn)
	mndy=im(imn)
	call blet2(imn,amn)
	infile(15:16)=amn
	infile(20:21)=amn
	mndy=im(imn)
C
C  Day-to-day cycle
C
      DO 52 idy=idy1,idy2		 !DAYYYYYYYYYYYYYY
	call blet2(idy,ADY)
	infile(22:23)=ady
C
	IF ((AMN.eq.'01').and.(ADY.eq.'01')) THEN
	write(*,31) (autout(k),k=0,7),(autout(k),k=0,7),(autout(k),k=0,7)
	write(11,31) (autout(k),k=0,7),(autout(k),k=0,7),(autout(k),k=0,7)
	ENDIF
C  UT cycle
C
      DO k=0,7
	iwneg(k)=0
	iwpos(k)=0
	iwran(k)=0
	ENDDO
	 wdyp=0.
	wdyn=0.
	wdyr=0.
	DO 53 iut=0,7	  ! UTTTTTTTTTTTTTT
	iut1=iut*3
C
	sumn=0.		 ! count of Wf <-1
	sump=0.		 ! count of Wf >+1
	icntp=0
	swegp=0.
      icntn=0
	swegn=0.
C
C 3h UT period:
C
	DO 153 iut3=iut1,iut1+2
	call blet2(iut3,AUT)
      infile(24:25)=aut
C
   	OPEN(10,file=infile,action='READ')
C
C/ cycle for lats:
       glat=-90.0	                  ! AD
	  do 54 ilat=1,71
	glat=glat+2.5				  ! AD
	elat=1.0+0.0001235*glat**2	  ! AD
	  read(10,32) (ivsr(k),k=0,72) 
C
	DO 333 ilon=0,72
          if (ivsr(ilon).gt.1) then
        iad=ivsr(ilon)
c	sump=sump+weg(iad)*ivsr(ilon)
	sump=sump+elat*weg(iad)*ivsr(ilon)  ! AD
	icntp=icntp+1
	swegp=swegp+weg(iad)
	goto 333
	endif
          if (ivsr(ilon).lt.-1) then
        iad=-ivsr(ilon)
c	sumn=sumn+weg(iad)*ivsr(ilon)
	sumn=sumn+elat*weg(iad)*ivsr(ilon)   ! AD
	icntn=icntn+1
	swegn=swegn+weg(iad)
	goto 333
	endif
  333	CONTINUE
   54 CONTINUE
	close(unit=10)
  153 CONTINUE
      xpos=0.
      if (icntp.gt.0.) then
C		xpos=sump/swegp		 ! /sum(weights)
		xpos=sump/icntp		 ! /cnt(Wf+)
	endif
      xneg=0.
      if (icntn.gt.0.) then
C			xneg=sumn/swegn	  ! /sum(weights)
			xneg=sumn/icntn	  ! /cnt(Wf-)
	endif
	xrange=xpos-xneg
	wdyp=wdyp+xpos
	wdyn=wdyn+xneg
      wdyr=wdyr+xrange
      iwneg(iut)=nint(xneg*100.)
	iwpos(iut)=nint(xpos*100.)
	iwran(iut)=nint(xrange*100.)
   53	CONTINUE           ! end UT cycle
       iwpos(8)=nint(wdyp/8.*100.)  ! Daily mean not to output
	iwneg(8)=nint(wdyn/8.*100.)	   ! Daily mean not to output
	iwran(8)=nint(wdyr/8.*100.)	   ! Daily mean not to output
	write(*,33) AYR,AMN,ADY,(iwpos(k),k=0,7),(iwneg(k),k=0,7)
	+,(iwran(k),k=0,7)
	write(11,33) AYR,AMN,ADY,(iwpos(k),k=0,7),(iwneg(k),k=0,7)
	+,(iwran(k),k=0,7)
   33 format(3A2,8(1X,I4),8(1X,I5),8(1X,I4))
   52 CONTINUE  ! daily cycle
   51 CONTINUE  ! Monthly cycle
      close(unit=11)
  700 CONTINUE              ! END YEAR CYCLE
      pause ' '
	STOP
	END
