      program extpianiaWs
C.........................................................Apr 2021
C Shub W(GTEC) index
C.........................................................Dec. 2020
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:24),iwpos(0:24),iwran(0:24)
	CHARACTER*128 INFILE,OUTFILEN,OUTFILEP,OUTFILER !global weighted Wfp,Wfn,Wfr = range index
	CHARACTER*2 AYR,AUT,AMN,ADY,AUTOUT(0:23)
	CHARACTER*4 AYEAR
	INTEGER ivsr(0:72) !  set of Wf 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\gtc\YY\MM\wgMMDDUT.sYY'   ! current mn
      outfilen='d:\web\graf\dats\wlnYYs.txt'   !  Cnt(W-) 	 WL
      outfilep='d:\web\graf\dats\wupYYs.txt'   !  Cnt(W+) 	 WU
      outfiler='d:\web\graf\dats\werYYs.txt'   !  Cnt(Wrange) WE
C
	do iut=0,23
	call blet2(iut,aut)
	autout(iut)=aut
	enddo
   17 format(A2)
   31 FORMAT('YYMMDD',1X,24(2X,A2,1X),2X,'DAY')
   32 FORMAT(73(1X,I4))
C
         DO 700 iyear=1994,2020 ! YEARRRRRRRRRRRRRRRRRRRRRRRRRR
	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
	outfilen(21:22)=AYR
	outfilep(21:22)=AYR
	outfiler(21:22)=AYR
	OPEN(11,file=outfilen,access='APPEND')
	OPEN(12,file=outfilep,access='APPEND')
	OPEN(13,file=outfiler,access='APPEND')
C
C
	    DO 51 imn=1,12        ! MONNNNNNNNNNNNNNNNN
	call blet2(imn,AMN)
	infile(15:16)=AMN
	infile(20:21)=AMN
         idy1=1
	idy2=im(imn)
	mndy=im(imn)
C
C  Day-to-day cycle
          DO 52 idy=idy1,idy2 !DAYYYYYYYYYYYYYY
	call blet2(idy,ADY)
	infile(22:23)=ADY
C
  330	CONTINUE
C
	IF ((AMN.eq.'01').and.(ADY.eq.'01')) THEN
	write(*,31) (autout(k),k=0,23)
	write(11,31) (autout(k),k=0,23)
	write(12,31) (autout(k),k=0,23)
	write(13,31) (autout(k),k=0,23)
	ENDIF
C  UT cycle
C
      DO k=0,24
	iwneg(k)=0
	iwpos(k)=0
	iwran(k)=0
	ENDDO
	 wdyp=0.
	wdyn=0.
	wdyr=0.
C
	DO 53 iut=0,23  ! UTTTTTTTTTTTTTT
	call blet2(iut,AUT)
      infile(24:25)=aut
C
   	OPEN(10,file=infile,action='READ')
	sumn=0.		 ! count of Wf >=1
	sump=0.		 ! count of Wf <=-1
	icntp=0
	swegp=0.
      icntn=0
	swegn=0.
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
      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.)
	close(unit=10)
   53	CONTINUE           ! end UT cycle
       iwpos(24)=nint(wdyp/24.*100.)
	iwneg(24)=nint(wdyn/24.*100.)
	iwran(24)=nint(wdyr/24.*100.)
C	write(*,33) AYR,AMN,ADY,(iwpos(k),k=0,24)
	write(12,33) AYR,AMN,ADY,(iwpos(k),k=0,24)
C	write(*,33) AYR,AMN,ADY,(iwneg(k),k=0,24)
	write(11,33) AYR,AMN,ADY,(iwneg(k),k=0,24)
C	write(*,33) AYR,AMN,ADY,(iwran(k),k=0,24)
	write(13,33) AYR,AMN,ADY,(iwran(k),k=0,24)
   33 format(3A2,24(1X,I4),2X,I4)
   52 CONTINUE  ! daily cycle
       call subws3g(AYR,AMN,idy1,idy2)
   51 CONTINUE  ! Monthly cycle
      close(unit=11)
	close(unit=12)
	close(unit=13)
  700 CONTINUE              ! END YEAR CYCLE
      pause ' '
	STOP
	END
