      program calmed477
C..........................................................Apr. 2022
C
C..........................................................Nov 2021
C produce median from indices for 477 storms
C
      character*64 infile,infile0,infile1,infile2,infile3
	+,outfile
	     character*1 gns,gns4(4),kps
	real dati(477,72),rmed(72),xi(72)
	integer med(72)
	DATA gns4/'p','m','n','s'/
		COMMON /BL1/dati,RMED
C	infile0='hsswp2d.txt'  !0
C	infile1='hsswu2dx.txt' !1
C	infile2='hsswl2dx.txt' !2
C	infile3='hsswe2dx.txt' !3
C	outfile='calmed477.res'
	infile0='dstwp5dx.txt'  !0
	infile1='dstwudzx.txt' !1
	infile2='dstwldzx.txt' !2
	infile3='dstwedzx.txt' !3
	outfile='calmed_1_2.res'
	OPEN(10,file=outfile,access='APPEND')
C
       kps='1'	   ! moderate storms
231	infile0(8:8)=kps
	infile1(8:8)=kps
	infile2(8:8)=kps
	infile3(8:8)=kps
	if (kps.eq.'1') then
	neve=433
	                else
	neve=111
	               endif
C       igns=1
C        igns=4
  17  continue
C        select case(igns)
C	case (1)
   1	infile=infile0
      gns=gns4(1)
   	OPEN(11,file=infile,action='READ')
	DO ll=1,neve	   !+++++++++
	read(11,*,end=41) xi
	do k=1,72
	dati(ll,k)=xi(k)
	enddo
	ENDDO		  !+++++++++
   41	close(unit=11)
	call submed477(neve,72)
     	write(*,30) (rmed(k),k=1,72)
	write(10,30) (rmed(k),k=1,72)
C	goto 43
       goto 24
C-----------------------------------------------
C	case (2)
   2	infile=infile1
	goto 24
C	case (3)
   3	infile=infile2
	goto 24
C	case (4)
   4	infile=infile3
	goto 24
C	case (5)
    5	 GOTO 222 ! stop
C	end select
C----------------------------------------------
C
C   24	DO 42 inp=1,3
   24	DO 42 inp=1,3
        igns=inp+1
        if (igns.eq.2) infile=infile1
	if (igns.eq.3) infile=infile2
	if (igns.eq.4) infile=infile3
	DO 142 ii=1,3
      gns=gns4(ii+1)
	infile(7:7)=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(neve,72)
   30	format(72(1X,F4.1))
   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)=-nint(rmed(k))
	              else
	med(k)=nint(rmed(k))
	endif
	enddo
	write(*,31)	(med(k),k=1,72)
	write(10,31) (med(k),k=1,72)
 142	CONTINUE
   42	CONTINUE
C?   43   igns=igns+1
C?	if (igns.lt.5) GOTO 17
  222	if (kps.eq.'1') then
	     kps='2'
	goto 231
	           endif
  	close(unit=10)
       pause ' '
	STOP
	END