      program extpianiaWe
C.........................................................Dec. 2020
C Include weight e(glat)
C
C Weighted PIA -pos ion activity, NIA - neg ion activity
C extracted from global Wc(TEC) index derived from JPL GIM-TEC (IONEX)
C
	DIMENSION weg(4),IM(12),iwneg(0:24),iwpos(0:24),iwran(0:24)
	CHARACTER*128 INFILE,OUTFILEN,OUTFILEP,OUTFILER,indate !global weighted Wfp,Wfn,Wfr = range index
	CHARACTER*2 AYR,AUT,AMN,ADY,AUTOUT(0:23),AMN_cur,DD2,AMN_pre
	+,ADY_pre
	CHARACTER*4 YEAR,YEAR_pre
C	+,PYR,PMN,PDY,AYRI,AMNI
	INTEGER ivsr(0:72) !  set of Wf indices for long=-10: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
	indate='d:/web/graf/date'    !!! Tamara !!!!!!!	 
	open(10,file=indate)							 
	read(10,799) YEAR,AMN_cur,DD2,YEAR_pre,AMN_pre,ADY_pre,lda,lda_pre  !FORMAT NUMBER = 799
	WRITE(*,798) YEAR,AMN_cur,DD2,YEAR_pre,AMN_pre,ADY_pre,lda,lda_pre !FORMAT NUMBER = 798
	close(unit=10)
  798	format(' YEAR, AMN, DD2 = ',A4,2(1X,A2),1X,A4,2(1X,A2),2(1X,I3))
  799	format(A4,2(1X,A2),1X,A4,2(1X,A2),2(1X,I3))	
C+      AYR=YEAR(3:4)  ! for current day  	
        read(AMN_cur,*) rmn_cur
	imn_cur=int(rmn_cur)
        read(DD2,*) rdy_cur
	idy_cur=int(rdy_cur)

C
C      infile='d:\web\dwf\YY\MM\wfMMDDUT.jYY'   ! current mn
      infile='d:\web\graf\dwc\YY\MM\ut\wcMMDDUT.jYY'   ! current mn
C	outfilen='wfnYR.txt'   ! Cnt(Wf-) 
C	outfilep='wfpYR.txt'   ! Cnt(Wf+) 
C	outfiler='wfrYR.txt'
      outfilen='d:\web\graf\datw\wlnYY.txt'   !  Cnt(W-) 	 WL
      outfilep='d:\web\graf\datw\wupYY.txt'   !  Cnt(W+) 	 WU
      outfiler='d:\web\graf\datw\werYY.txt'   !  Cnt(Wrange) WE
	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
  330	WRITE(*,*)' ENTER YR OF INPUT FILE=	'
	read(*,17) AYR
	read(AYR,*) ryr
	IYR=int(ryr)   ! current yr
	if (iyr.lt.90) then
	iyyyy=2000+iyr
	              else
	iyyyy=1900+iyr
	endif
	z1=iyyyy/4.0
      jz=int(z1)*4

      IF(jz.EQ.iyyyy) THEN
               IM(2)=29
	dnr=366.
        ELSE
                IM(2)=28
	  	dnr=365.
	       ENDIF
	infile(17:18)=ayr
	infile(36:37)=ayr
	outfilep(21:22)=ayr
	outfilen(21:22)=ayr
	outfiler(21:22)=ayr
C
	OPEN(11,file=outfilep,access='APPEND')
	OPEN(12,file=outfilen,access='APPEND')
	OPEN(13,file=outfiler,access='APPEND')

C
C month-to-month cycle
C
      imn2=12
	if (iyr.eq.20) imn2=imn_cur  ! TEMP
      DO 51 imn=1,imn2
	call blet2(imn,amn)
	infile(20:21)=amn
	infile(28:29)=amn
	mndy=im(imn)
C
C  Day-to-day cycle
C
	if ((iyr.eq.20).and.(imn.eq.imn_cur)) mndy=idy_cur  ! TEMP
      DO 52 idy=1,mndy
	call blet2(idy,ADY)
	infile(30:31)=ady
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.
	DO 53 iut=0,23
	call blet2(iut,AUT)
      infile(32:33)=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.)
	write(*,33) AYR,AMN,ADY,(iwpos(k),k=0,24)
	write(11,33) AYR,AMN,ADY,(iwpos(k),k=0,24)
	write(*,33) AYR,AMN,ADY,(iwneg(k),k=0,24)
	write(12,33) AYR,AMN,ADY,(iwneg(k),k=0,24)
	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
   51 CONTINUE  ! Monthly cycle
      close(unit=11)
	close(unit=12)
	close(unit=13)
      if (iyr.ne.20) GOTO 330
      pause ' '
	STOP
	END
