      program extocdy9
C....................................................................Dec. 2018
C
C Extract daily peak & daily mean Vs index at geogr. lats (~ 9 mlats) at ranges of glongs for IONEX -180:5:180
C     (1) mlat < -70
C     (2) mlat -70:-50
C     (3) mlat -50:-30
C     (4) mlat -30:-10
C     (5) mlat -10: 10
C     (6) mlat  10: 30
C     (7) mlat  30: 50
C     (8) mlat  50: 70
C     (9) mlat  >= 70
C output at separate j = 1,...,9 file & 0 file = global mean 
C Output 'extocdyj.txt': RYEAR MN DY Vsnmax,Vsnegav, Vsposmax,Vsposav, Vstotmax,Vstotav 
C
      dimension IM(12),aven(9),avep(9),avet(9)
	+,ixn(0:23),ixp(0:23),ixt(0:23)
      CHARACTER*2 AYR,AMN,ADY,CYR,CMN,CDY
	CHARACTER*1 JN
      CHARACTER*60 infile1,infile2,infile3,outfile,txt
      DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/
C
	outfile='extocdyJ.txt'
   30	format(A60)
   31	format(3A2,24(1X,I4),2X,F5.2)
   32	format(1X,F8.3,2(1X,A2),6(1X,F5.2))
   33	format(3A2,24(1X,I4),2X,F5.2,1X,I1)
C
C Cycle on J:
C
Crem      DO 44 jj=0,9
      DO 44 jj=1,9
	call blet1(jj,JN)
	if (jj.eq.0) then
      infile1='c:\msdev\projects\vsigma\nstocYR.txt'
	infile2='c:\msdev\projects\vsigma\pstocYR.txt'
	infile3='c:\msdev\projects\vsigma\vstocYR.txt'
	else
      infile1='nvstocYR.txt'
      infile2='pvstocYR.txt'
      infile3='tvstocYR.txt'
	endif
	outfile(8:8)=JN
	OPEN(10,file=outfile,access='APPEND')
C
C Year cycle
C         
      DO 50 iyear=1994,2018
	iyr=iyear-(iyear/100)*100
	call blet2(iyr,AYR)
          ryr=float(iyr)
	if(int(ryr/4.)*4.eq.iyr) THEN 
	ndnr=366
	im(2)=29
	                          ELSE
	ndnr=365
	im(2)=28
	ENDIF
	delyr=1.0/ndnr
C
      if (jj.eq.0) then
	infile1(31:32)=AYR
	infile2(31:32)=AYR
	infile3(31:32)=AYR
	else
	infile1(7:8)=AYR
	infile2(7:8)=AYR
	infile3(7:8)=AYR
	endif
	OPEN(11,file=infile1,action='READ')
	OPEN(12,file=infile2,action='READ')
	OPEN(13,file=infile3,action='READ')
      read(11,30) txt !title line
	read(12,30) txt !title line
	read(13,30) txt !title line
C
C Monthly cycle
C
      DO 51 imn=1,12
	call blet2(imn,AMN)
	IDYs=1
	IDYS2=im(imn)
	if ((iyear.eq.1994).and.(imn.eq.1)) idys=16
	if ((iyear.eq.2018).and.(imn.eq.12)) idys2=14
C
C Daily cycle
C
       DO 52 idy=idys,idys2
	lda=ndoy(iyr,imn,idy)
	call blet2(idy,ADY)
	      ryear=float(iyear)+(lda-1)*delyr
	if (jj.eq.0) then
      read(11,31,end=53) CYR,CMN,CDY,(ixn(i),i=0,23),vsneg
	read(12,31,end=53) CYR,CMN,CDY,(ixp(i),i=0,23),vspos
	read(13,31,end=53) CYR,CMN,CDY,(ixt(i),i=0,23),vstot
      ivmneg=0
	ivmpos=0
	ivmtot=0
	do i=0,23
	if (ivmneg.lt.ixn(i)) ivmneg=ixn(i)
	if (ivmpos.lt.ixp(i)) ivmpos=ixp(i)
	if (ivmtot.lt.ixt(i)) ivmtot=ixt(i)
	enddo
	vmneg=float(ivmneg)/100.
	vmpos=float(ivmpos)/100.
	vmtot=float(ivmtot)/100.
	goto 55 ! to output
	            else
      DO k=1,9          !!
      read(11,33,end=53) CYR,CMN,CDY,(ixn(i),i=0,23),aven(k),kn
	read(12,33,end=53) CYR,CMN,CDY,(ixp(i),i=0,23),avep(k),kn
	read(13,33,end=53) CYR,CMN,CDY,(ixt(i),i=0,23),avet(k),kn
	if (kn.eq.jj) then		   !++
	vsneg=aven(kn)
	vspos=avep(kn)
	vstot=avet(kn)
      ivmneg=0
	ivmpos=0
	ivmtot=0
	do i=0,23				!
	if (ivmneg.lt.ixn(i)) ivmneg=ixn(i)
	if (ivmpos.lt.ixp(i)) ivmpos=ixp(i)
	if (ivmtot.lt.ixt(i)) ivmtot=ixt(i)
	enddo					!
	vmneg=float(ivmneg)/100.
	vmpos=float(ivmpos)/100.
	vmtot=float(ivmtot)/100.
	endif                          !++
	ENDDO					 !!
	           endif
   55	if ((cyr.eq.ayr).and.(cmn.eq.amn).and.(cdy.eq.ady)) then
C output results
	write(*,32) ryear,amn,ady,vmneg,vsneg,vmpos,vspos,vmtot,vstot
	write(10,32) ryear,amn,ady,vmneg,vsneg,vmpos,vspos,vmtot,vstot
	endif
   52 CONTINUE	 ! end daily cycle
   51 CONTINUE	  ! end monthly cycle

   53 close(unit=11)
      close(unit=12)
	close(unit=13)
C
   50 CONTINUE     ! end yearly cycle
      close(unit=10)
   44 CONTINUE     ! end indices cycle
C
	pause ' '
	STOP
	END
