      program calmed381
C..........................................................Nov 2021
C produce median from indices for 477 storms
C
      character*64 infile,infile0,infile1,infile2,infile3
	+,outfile,outhss
	     character*1 gns,gns4(4)
	real dati(477,72),rmed(72),xi(72)
	integer med(72),ires(72)
	DATA gns4/'p','m','n','s'/
		COMMON /BL1/dati,RMED
C	infile0='hsswp2d.txt'  !0
	infile1='hsswu2dx.txt' !1
	infile2='hsswl2dx.txt' !2
	infile3='hsswe2dx.txt' !3
        infile0='hsssw3d.txt'  ! HSS (replace 0 by median)
	outhss='hsssw3c.txt'
	outfile='calmed381.res'
	OPEN(10,file=outfile,access='APPEND')
	OPEN(15,file=outhss)
C
        igns=1
C       igns=4
  17        select case(igns)
	case (1)
   1	infile=infile0
      gns=gns4(1)
   	OPEN(11,file=infile,action='READ')
	DO ll=1,381	   !+++++++++
	read(11,*,end=41) xi
	do k=1,72
	dati(ll,k)=xi(k)
	enddo
	ENDDO		  !+++++++++
   41	close(unit=11)
	call submed477(381,72)
     	write(*,30) (rmed(k),k=1,72)
	write(10,30) (rmed(k),k=1,72)
	goto 43
	case (2)
   2	infile=infile1
	goto 24
	case (3)
   3	infile=infile2
	goto 24
	case (4)
   4	infile=infile3
	goto 24
	case (5)
    5	 GOTO 222 ! stop
	end select
C
C
   24	DO 42 inp=1,3
      gns=gns4(inp+1)
	infile(8:8)=gns
C
   25	OPEN(11,file=infile,action='READ')
	DO ll=1,477	   !+++++++++
	read(11,*,end=44) xi
	do k=1,72
	if (igns.eq.3) then
	dati(ll,k)=-xi(k)
	                else
	dati(ll,k)=xi(k)
	endif
	enddo
	ENDDO		  !+++++++++
   44	close(unit=11)
	call submed477(477,72)
   30	format(72(1X,F5.0))
   31	format(72(1X,I4))
   32	format(1X,A1,I1,'=[',A16)
	write(*,32) gns,inp,infile
	write(10,32) gns,inp,infile
	do k=1,72
	if (igns.eq.3) then
	med(k)=-int(rmed(k))
	              else
	med(k)=int(rmed(k))
	endif
	enddo
	write(*,31)	(med(k),k=1,72)
	write(10,31) (med(k),k=1,72)
   42	CONTINUE
   43 CONTINUE
      DO nn=1,381
	DO k=1,72
	if (dati(nn,k).eq.0.) dati(nn,k)=rmed(k)
	ires(k)=nint(dati(nn,k))
	ENDDO
	write(*,31)	(ires(k),k=1,72)
	write(15,31) (ires(k),k=1,72)
	ENDDO
	close(unit=15)
C--      igns=igns+1
C-- (igns.lt.5) GOTO 17
  222	close(unit=10)
       pause ' '
	STOP
	END