C.......................................................May 2025
C Monthly median GIM-TEC from JPL magn coord
C
C					                                  Dec. 2014
C
C       Count global median afc(UT) and ahc*UT) for maps TEC
C input maps for given UT 
      subroutine smedmap31m(AYEAR,AMN,IMED)
C
C      output	fcYYMMUT.mut; hcYYMMUT.mut; tcYYMMUT.mut;
C
      DIMENSION im(12),da(31,71,0:72),av(0:72),iav(0:72)
	+,CC(31,0:72)  ! calculate med-map line-by-line for 71 latitudes
	+,IMED(71,0:72,0:23) ! results of median maps 
      character*2 ayr,amn,ady,aut
	character*1 fht
	character*45 infile,outfile
	character*4 AYEAR
	DATA  IM/31,28,31,30,31,30,31,31,30,31,30,31/

c
c Add cycle on fm, hm, tm:
c
c-	if (nnn.eq.1) fht='t'
c-	if (nnn.eq.2) fht='f'
c-	if (nnn.eq.3) fht='h'
        nnn=1       ! TEC
	fht='t'
c
C ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
c
	AYR=AYEAR(3:4)
		read(ayr,*) yr
		read(amn,*) rmn
	mn=int(rmn)
C
 	iyr=int(yr)
	if(int(yr/4.)*4.eq.iyr) THEN 
	im(2)=29
	                          ELSE
	IM(2)=28
	ENDIF
	ndnr=im(mn)
C
C 
C
C       infile='f:\web\graf\dtu\yy\mm\ut\tcmmddut00.uyr'	 !Tamara
       infile='f:\web\dts\yy\mm\tommddut.jyr'	 !Tamara
	infile(9:9)=fht								 !Tamara
	infile(18:18)=fht								 !Tamara
	infile(12:13)=ayr								 !Tamara
	infile(28:29)=ayr								 !Tamara+
		infile(15:16)=amn	
		infile(20:21)=amn	
C
Crem        outfile='f:\web\dts\yy\mm\tomm00ut.myr'
      outfile=infile
	outfile(22:23)='00'
	outfile(27:27)='m'
C UT cycle
	DO 203 iut=0,23
	call blet2(iut,AUT)
	infile(24:25)=AUT								 !Tamara
	outfile(24:25)=AUT
C Day-to-day cycle
	DO 201 idy=1,ndnr
	call blet2(idy,ady)
	infile(22:23)=ady								 !Tamara
C
      OPEN(11,file=infile,action='READ')
   18	continue
C
  700 format(A128)
C
C
	do lat=1,71
  107 format(73(1X,F4.0))	! 
	READ (11,107,END=99,ERR=2) (da(idy,lat,k),k=0,72) !map lines
	enddo				   ! end lati
   99	close(unit=11)
 
  201  CONTINUE				  ! end day-to-day cycle
C
	OPEN(14,file=outfile)
C
	do 301 lat=1,71
	do k=0,72
	av(k)=0.
	enddo
C combile all days for selected lati:
	do idy=1,ndnr
      	do lon=0,72
	    cc(idy,lon)=da(idy,lat,lon)
	    enddo
	enddo
	call smedian2n(ndnr,CC,AV)
C
	  	do lon=0,72
	iav(lon)=nint(av(lon))
	IMED(lat,lon,iut)=iav(lon) ! results of median map
	    enddo
C+	write(*,109) (iav(k),k=0,72)
	write(14,109) (iav(k),k=0,72)
  301	continue                 ! end lat cycle of median map
 	close(14)
  203 continue   ! end ut cycle
  108 format(3A2,24(1X,I4))	! 
  109 format(73(1X,I4))	! 
C
       GOTO 1234
    2 CONTINUE
       write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY ',infile
 	pause ' '
	goto 34
 1234	continue  !
c//      write(*,*) infile,outfile
c//      pause ' ' 
   34 RETURN
       END

C=========================================================================
c VERIFICATION OF MEDIAN ================
      subroutine smedian2n(ndnr,CC,SME)
C Input file: CC(ndnr,0:72) for ndnr days, longitudes -180:5:180
C Number days for median : ndnr=7
C Output array: SME(0:72) for longitudes -180:5:180
	DIMENSION CC(31,0:72),SME(0:72),X1(31),X2(31)
c-		COMMON /BL1/CC,SME
C "MEDIAN:"
       DO 1030 M=0,72
      MM=0
      DO N=1,ndnr
      X1(N)=CC(N,M)
      ENDDO 
  880 XX=0.
CREM MAX X1(N)
  890 DO N=1,ndnr
  900 IF (XX.lt.X1(N)) THEN 
      XX=X1(N)
	ENDIF
      ENDDO      !N
  920 IF (XX.eq.0) GOTO 980
C
  930 DO 960 N=1,ndnr
      IF (X1(N).lt.XX) GOTO 960
      MM=MM+1 
      X2(MM)=XX 
	X1(N)=0.
  960 CONTINUE      ! N
  970 IF (MM.lt.NDNR) GOTO 880
  980 XMM=FLOAT(MM)
      XD1=XMM/2.
      ND1=INT(XD1)
	CD1=FLOAT(ND1) 
      ND2=ND1+1 
      IF (CD1.LT.XD1) THEN 
	XMD=X2(ND2)
	else
	XMD=(X2(ND1)+X2(ND2))/2.
	ENDIF
C
 1020 SME(M)=XMD
 1030 CONTINUE
c
 1050 RETURN
      END
C=============================================================================