      program extpiania
C.........................................................Nov. 2020
C Weighted PIA -pos ion activity, NIA - neg ion activity
C
      CHARACTER*64 infile, outfile
	CHARACTER*4 YYMM
	DIMENSION weg(4)
      INTEGER ivsr(180,96) ! diurnal-15min set of Vsigma indices 
		DATA WEG/0.,0.6931,1.0986,1.3863/  ! =ln1, ln2, ln3, ln4

C
      infile='vix035.j06'
	outfile='piania035.res'
   31 format(1X,A4,1X,I2,2X,F5.1)
   32 FORMAT(96(1X,I4))
C
   	OPEN(10,file=infile,action='READ')
	OPEN(11,file=outfile,access='APPEND')
C cycle for 4 days:
      DO 100 idy=1,4
	read(10,31) YYMM,idy, glon
      write(11,31) YYMM,idy, glon
	  do ilat=1,180
	  read(10,32) (ivsr(ilat,k),k=1,96) 
	  enddo
C
	DO 101 it=1,96
	sumn=0.		 !15min count of Vs >=1
	sump=0.		 !15min count of Vs <=-1
	icntp=0
	swegp=0.
      icntn=0
	swegn=0.
C
	DO 333 ilat=1,180
          if (ivsr(ilat,it).gt.1) then
        iad=ivsr(ilat,it)
	sump=sump+weg(iad)*ivsr(ilat,it)
	icntp=icntp+1
	swegp=swegp+weg(iad)
	goto 333
	endif
          if (ivsr(ilat,it).lt.-1) then
        iad=-ivsr(ilat,it)
	sumn=sumn+weg(iad)*ivsr(ilat,it)
	icntn=icntn+1
	swegn=swegn+weg(iad)
	goto 333
	endif
  333	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
	write(*,33) it,xpos,xneg,xrange
	write(11,33) it,xpos,xneg,xrange
   33 format(1X,I2,3(1X,F7.2))
  101 CONTINUE
  100 CONTINUE
      close(unit=10)
       close(unit=11)
      pause ' '
	STOP
	END
