C       special output from rpress, to fit ISLAB plotting
C       Sven Ivansson 1995

C       ------------------------------------------------
C       "STAEDAT" juni 2019; old code in utspec.fOLD1
C       ------------------------------------------------


	SUBROUTINE UTSPEC (KFWTYP, NFR, JF,FREQ,PM01M,PM01M_NY,IDB, 
     $  MHQEFF,FILM, FILO,FILG, 
     $  CPRESSW, 
     $  MAINAM,IPOSAM, MAINPH,IPOSPH)
	
	USE INMODELa
	USE INRANGEa

	IMPLICIT NONE

	INTEGER KFWTYP, NFR, JF,IDB, MHQEFF, 
     $   IPOSAM, IPOSPH
	REAL*8 FREQ,PM01M,PM01M_NY
	COMPLEX*16 CPRESSW(*)
	CHARACTER*32 FILM, FILO,FILG, MAINAM, MAINPH

	INTEGER IPOS, IH,IR,IHR, ISLASK
	REAL*8 SLASK, DATAN2DQ
	COMPLEX*16 CSLASK
	CHARACTER*32 FILAM, FILPH, FILJ, filq, FILNAM
	complex*8 ceslask

ccc	IF (MAINAM.ne.' ') then
C         write the "filq" file
	  FILq = 'rsegRZdat.'
	  IPOS = 11
	  ISLASK = FREQ
	  CALL PUTINTP (ISLASK,IPOS,FILq)
	  IF (ISLASK.NE.FREQ) THEN
	    IF (IPOS.GT.32) STOP 460
	    FILq(IPOS:IPOS) = 'X'
	    IPOS = IPOS+1
	    CALL PUTINTP (JF,IPOS,FILq)
	  END IF
	  OPEN(UNIT=24,FILE=FILq,STATUS='UNKNOWN')
ccc	  write(*,*) 'UTSPEC opened filq=',filq
ccc	  ihr = 0
ccc	  do ih = 1, nhyd
ccc	    do ir = 1, li
ccc	      ihr = ihr + 1
ccc	      cslask = cpressw(ihr) / pm01m_ny
ccc	      write(24,*) dreal(cslask), dimag(cslask)
ccc	    end do
ccc	  end do
	  do ir = 1, li
	    ihr = ir
	    do ih = 1, nhyd
	      cslask = cpressw(ihr) / pm01m_ny !ihr=(ih-1)*li+ir
	      write(24,*) dreal(cslask), dimag(cslask)
	      ihr = ihr + li  ! for next ih
	    end do
	  end do
	  close(24)
C	  write the "r0" file
	  FILNAM = 'r0.'
	  IPOS = 4
	  ISLASK = FREQ
	  CALL PUTINTP (ISLASK,IPOS,FILNAM)
	  IF (ISLASK.NE.FREQ) THEN
	    IF (IPOS.GT.72) STOP 460
	    FILNAM(IPOS:IPOS) = 'X'
	    IPOS = IPOS+1
	    CALL PUTINTP (JF,IPOS,FILNAM)
	  END IF
	  OPEN(24,FILE=FILNAM,STATUS='UNKNOWN')
	  DO IR = 1, LI
	    WRITE(24,*) OR(IR)
	  END DO
	  CLOSE(24)
C	  write the "z0" file
	  FILNAM = 'z0.'
	  IPOS = 4
	  ISLASK = FREQ
	  CALL PUTINTP (ISLASK,IPOS,FILNAM)
	  IF (ISLASK.NE.FREQ) THEN
	     IF (IPOS.GT.72) STOP 460
	     FILNAM(IPOS:IPOS) = 'X'
	     IPOS = IPOS+1
	     CALL PUTINTP (JF,IPOS,FILNAM)
	  END IF
	  OPEN(24,FILE=FILNAM,STATUS='UNKNOWN')
	  DO IH = 1, NHYD
	    WRITE(24,*) ZD(IDHYD(IH))
	  END DO
	  CLOSE(24)
	  IF (NHYD.GT.1) THEN
	    IF (LI.GT.1) THEN
C	      write the "r1" file
	      FILNAM = 'r1.'
	      IPOS = 4
	      ISLASK = FREQ
	      CALL PUTINTP (ISLASK,IPOS,FILNAM)
	      IF (ISLASK.NE.FREQ) THEN
	        IF (IPOS.GT.72) STOP 460
	        FILNAM(IPOS:IPOS) = 'X'
	        IPOS = IPOS+1
	        CALL PUTINTP (JF,IPOS,FILNAM)
	      END IF
	      OPEN(24,FILE=FILNAM,STATUS='UNKNOWN')
	      SLASK = 0.5D0 * DABS((OR(LI)-OR(1)))/(LI-1)  ! simplification
	      DO IR = 1, LI
	        WRITE(24,*) OR(IR) - SLASK  ! inner cell radius
	      END DO
	      CLOSE(24)
	    END IF
C	    write the "z1" file
	    FILNAM = 'z1.'
	    IPOS = 4
	    ISLASK = FREQ
	    CALL PUTINTP (ISLASK,IPOS,FILNAM)
	    IF (ISLASK.NE.FREQ) THEN
	       IF (IPOS.GT.72) STOP 460
	       FILNAM(IPOS:IPOS) = 'X'
	       IPOS = IPOS+1
	       CALL PUTINTP (JF,IPOS,FILNAM)
	    END IF
	    OPEN(24,FILE=FILNAM,STATUS='UNKNOWN')
	    SLASK = 0.5D0 * DABS((ZD(IDHYD(NHYD))-ZD(IDHYD(1))))/
     &	     (NHYD-1)  ! simplification
	    DO IH = 1, NHYD
	      WRITE(24,*) ZD(IDHYD(IH))-SLASK ! lower value for z patch
	    END DO
	    CLOSE(24)
	  END IF
ccc	end if

ccc	ceslask = cpressw(1) / pm01m_ny
ccc	write(87,*) sngl(freq), ceslask, sngl(cabs(ceslask))

	RETURN
	END
