      program datngdc2x
C.....................................................................Apr 2020
C extract data NGDC in .dat format
C produce result = (Xave(UT-30m)+Xave(ut+30m))/2
C
C-	DIMENSION im(12)
      character*128 infile,outfilef,outfileh,txt,m_data
	+,outfile  ! write infile in order of ut=0,1,...,24 exclkuding title-lines           ! 
      character*3 DDD
	character*2 AYR,AMN,ADY,st,BYR,CYR,CMN,CDY,ADY1
	+,STS_OUT(68)
		character*5 STCOD
	+,NUM_STS(68)
      integer count,f_strings,h_strings
     	dimension ifc(0:23),ihc(0:23)
	+,fc30(0:23,2),hc30(0:23,2)
C
      DATA STS_OUT/
	+ 'al','as','ad','au','bb','bc','bp','br'
	+,'cj','cb','ch','cn','cq','cs','cw','db'
     +,'dw','er','eg','ei','ff','fz','ga','gg'
     +,'gr','gu','ha','he','ho','ic','if','jb'
     +,'jj','jm','jr','kh','kk','ll','lm','mg'
     +,'ml','mm','mo','mw','nc','nd','ni','ok'
     +,'pa','pe','pq','rm','ro','sa','sd','sj'
     +,'sq','tk','tr','tu','tv','vt','wk','wp'
     +,'yg','lv','mb','gm'/
      DATA NUM_STS/
	+ 'AL945','AS00Q','AS237','AU930','BBJ3R','BC840','BP440','BR52P'
	+,'CAJ2M','CB53N','RL052','CN53L','09429','CS31K','CW46O','DB049'
     +,'DW41K','EA653','EG931','EI764','FF051','FZA0M','GA762','GU513'
     +,'GR13L','GU421','HA419','HE13N','HO54K','IC437','IF843','JB57N'
     +,'JJ433','JI91J','JR055','TO536','KB548','LL721','LM42J','MG560'
     +,'ML449','MM168','MA155','MW26P','NI135','ND61R','NI63_','OK426'
     +,'PA836','PE43K','PQ052','PRJ18','RA041','SAA0K','SD266','SJJ18'
     +,'SQ67Q','TK356','TR170','TUJ2O','TV51R','VT139','WK546','WI937'
     +,'YG431','LV12P','MU12K','GM037'/  
C
		AYR='22'   ! TEMP
C	AYR='19'
C	AYR='20'
      infile='d:\web\graf\ngdc\SAA0K_2020DDD.dat' ! 
	infile(26:27)=AYR
      read(AYR,*) ryr
	iyr=int(ryr)
	write(*,*) ' Enter mn:'
	read(*,*) iMN
	call blet2(imn,AMN)
      write(*,*) ' Enter dy1,dy2:'
	read(*,*) idy1,idy2
	 call blet2(idy1,ADY1)
	call blet2(idy2,ADY)
C
	outfilef='d:\web\graf\2020\stYRf.br'
	outfilef(15:16)=AYR
	outfilef(20:21)=AYR
C
   30 format(A110)
c#          UT_Time      foF2      hmF2      foF1      hmF1      foE       zmE       foEs      fxI       TEC       
c#          Columns       4       5       6       7       8       9      10      11      12
c 2020 050 00:00:00       3.787   189.110     0.000     0.000     0.000     0.000     0.000     0.000     0.944
   31 format(3X,A2,1X,I3,2(1X,I2),9X,F6.3,3X,F7.3)
C
C Cycle on stations
Cnorm       DO 51 ist=1,65
       ist=21
	st=STS_OUT(ist)
	stcod=NUM_STS(ist)
C
	infile(18:22)=STCOD
      outfilef(18:19)=st
	outfileh=outfilef
	outfileh(22:22)='h'
C+++++++++++++++++++++++++
  333 FORMAT(A128)
      count=0
      OPEN(88,FILE=outfilef)
  255 READ(88, 333, END=244) m_data 
	CYR=m_data(1:2)
	CMN=m_data(3:4)
	CDY=m_data(5:6)
	if ((cyr.eq.ayr).and.(cmn.eq.amn).and.(cdy.eq.ady1)) then	
	goto 244
	                  else
         count=count+1
      GOTO 255
      CONTINUE
                  	endif
  244 f_strings = count
      REWIND (88)
      count=0
      do while (count.lt.f_strings)
         READ(88, 333, END=248) m_data 
         count=count+1
      enddo
  248 print*,'YES!',st
      ENDFILE 88
      close(UNIT=88)
C TLG ADD!
CCC
      count=0
      OPEN(55,FILE=outfileh)
  355 READ(55, 333, END=344) m_data 
	CYR=m_data(1:2)
	CMN=m_data(3:4)
	CDY=m_data(5:6)
	if ((cyr.eq.ayr).and.(cmn.eq.amn).and.(cdy.eq.ady1)) then  
	goto 344
	                  else
         count=count+1
      GOTO 355
      CONTINUE
                  	endif
  344 h_strings = count
      REWIND (55)
      count=0
      do while (count.lt.h_strings)
         READ(55, 333, END=348) m_data 
         count=count+1
      enddo
  348 print*,'YES!',st
      ENDFILE 55
      close(UNIT=55)
C      enddo
C=============================================================== 


	OPEN(11,file=outfilef,access='APPEND')
	OPEN(12,file=outfileh,access='APPEND')
          do k=0,23
	do i=1,2
	fc30(k,i)=0.
	hc30(k,i)=0.
	enddo
	enddo
	f30_pre=0. ! last 30min for prec day
	h30_pre=0. ! last 30min for prec day
       DO 41 idy=idy1,idy2
	lda=ndoy(iyr,imn,idy)
	call blet2(idy,ADY)
	call blet3(lda,DDD)
	infile(28:30)=DDD
	outfile=infile
	outfile(23:23)='x'
	OPEN(33,file=outfile)
C
	OPEN(10,file=infile,action='READ')
   5	read(10,30,end=34) txt
	k=index(txt,'2020') 
	if (k.gt.0) then
	write(33,30) txt
	else
	goto 5
	endif
      goto 5
   34	close(unit=10)
      close(unit=33)
C
Cx   	read(10,30,end=50) txt !extra line
Cx	read(10,30,end=50) txt !extra line
c	read(10,30,end=50) txt !1st line
c	backspace(10)
	do k=0,23
	ifc(k)=0
	ihc(k)=0
	enddo
C
	DO 40 iut=0,23
	OPEN(10,file=outfile,action='READ')
	sum1f=0.
	sum2f=0.
	sum1h=0.
	sum2h=0.
	icnf1=0
	icnh1=0
	icnf2=0
	icnh2=0
c-   1	 read(10,30,end=50) txt !extra line
c-      n1=index(txt,'foF2')
c-	if (n1.gt.0) goto 1
c-      n2=index(txt,'Columns')
c-	if (n2.gt.0) then
c-	goto 1
c-	else
c-	backspace(10)
c-	endif
   1   read(10,31,end=50) BYR,lda,MUT,MIN,fof2,hmf2
	if (mut.ne.iut) then        !1
c-	backspace(10)
        goto 1
	endif
      IF (min.eq.0) THEN					  !6
	ifc(iut)=nint(foF2*10.)
	ihc(iut)=nint(hmF2)
	ENDIF									   !6

   3	    if ((min.ge.0).and.(min.le.30)) then	  !7
	 if (fof2.gt.0.) then					  !8
	 icnf1=icnf1+1
	sum1f=sum1f+fof2
	endif									  !8
	 if (hmf2.gt.0.) then					  !9
	 icnh1=icnh1+1
	sum1h=sum1h+hmf2
	endif									  !9
	     endif                                    !7
	 if ((min.ge.30).and.(min.lt.60)) then	  !10
	 if (fof2.gt.0.) then                      !11
	 icnf2=icnf2+1
	sum2f=sum2f+fof2
	endif									   !11
	 if (hmf2.gt.0.) then					   !12
	 icnh2=icnh2+1
	sum2h=sum2h+hmf2
	endif									   !12
       endif									  !10
c-	read(10,30,end=50) txt !extra line
c-	read(10,30,end=50) txt !extra line
	goto 1
   50 close(unit=10)
C
	if (icnf1.gt.0) then 		!2
	fc30(iut,1)=sum1f/icnf1
	else						!2
	fc30(iut,1)=0.
	endif						!2
      if (icnf2.gt.0) then		!3
	fc30(iut,2)=sum2f/icnf2
	else						!3
	fc30(iut,2)=0.
	endif						!3
	if (icnh1.gt.0) then 		!4
	hc30(iut,1)=sum1h/icnh1
	else						!4
	hc30(iut,1)=0.
	endif						!4
      if (icnh2.gt.0) then		!5
	hc30(iut,2)=sum2h/icnh2
	else						!5
	hc30(iut,2)=0.
	endif						!5
C
	goto 40

   40 CONTINUE
C 
C	do iut=0,23
	do iut=0,mut
	fut=0.
      if ((f30_pre.gt.0.).and.(fc30(iut,1).gt.0.)) 
	+fut=(f30_pre+fc30(iut,1))/2.
	if ((f30_pre.gt.0.).and.(fc30(iut,1).eq.0.)) fut=f30_pre
	if ((f30_pre.eq.0.).and.(fc30(iut,1).gt.0.)) fut=fc30(iut,1)
 	ifc(iut)=nint(fut*10.)
	hut=0.
      if ((h30_pre.gt.0.).and.(hc30(iut,1).gt.0.)) 
	+hut=(h30_pre+hc30(iut,1))/2.
	if ((h30_pre.gt.0.).and.(hc30(iut,1).eq.0.)) hut=h30_pre
	if ((h30_pre.eq.0.).and.(hc30(iut,1).gt.0.)) hut=hc30(iut,1)
      ihc(iut)=nint(hut)
	f30_pre=fc30(iut,2)
	h30_pre=hc30(iut,2)
	enddo
C
      write(*,32) AYR,AMN,ADY,(ifc(k),k=0,23)
	write(11,32) AYR,AMN,ADY,(ifc(k),k=0,23)
	write(*,32) AYR,AMN,ADY,(ihc(k),k=0,23)
	write(12,32) AYR,AMN,ADY,(ihc(k),k=0,23)
   32 format(3A2,2X,24(I3,2X))
   41 CONTINUE
      close(unit=11)
      close(unit=12)
   51	CONTINUE
	pause ' '
	STOP
	END